new parameter -s textonly
[swftools.git] / lib / example / jpegtest.c
1 /* jpegtest.c
2
3    Example for including and mapping jpeg images to swf shapes
4    
5    Part of the swftools package.
6
7    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
8  
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <math.h>       
26 #include "../rfxswf.h"
27
28 #define WIDTH           256
29 #define HEIGHT          256
30 #define QUALITY         85
31
32 #define ID_BITS         1
33 #define ID_SHAPE        2
34
35 int main( int argc, char ** argv)
36 { SWF swf;
37   TAG* t;
38   RGBA rgb;
39   SHAPE* s;
40   MATRIX m;
41   SRECT r;
42   JPEGBITS* jpeg;
43
44   int f; // file handle
45   
46   int ls; // line style
47   int fs; // fill style
48   int frame;
49
50   memset(&swf,0x00,sizeof(SWF));
51
52   swf.fileVersion       = 4;
53   swf.frameRate         = 0x1800;
54   swf.movieSize.xmax    = 20*WIDTH;
55   swf.movieSize.ymax    = 20*HEIGHT;
56
57   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
58   t = swf.firstTag;
59
60     rgb.r = 0xff;
61     rgb.b = 0xff;
62     rgb.g = 0xff;
63     swf_SetRGB(t,&rgb);
64
65   t = swf_InsertTag(t,ST_DEFINEBITSJPEG2);
66
67     swf_SetU16(t,ID_BITS);
68 //    swf_SetJPEGBits(t,"test.jpg",QUALITY);  <- use this to include an image from disk
69     
70 //  That's the way to use memory bitmaps (24bit,RGB)
71
72     jpeg = swf_SetJPEGBitsStart(t,WIDTH,HEIGHT,QUALITY);
73     { int y;
74       for (y=0;y<HEIGHT;y++)
75       { U8 scanline[3*WIDTH];
76         int x,p = 0;
77         for (x=0;x<WIDTH;x++) 
78         { scanline[p++] = x;    // R
79           scanline[p++] = y;    // G
80           scanline[p++] = 0x80; // B          
81         }
82         swf_SetJPEGBitsLine(jpeg,scanline);
83       }
84     }
85     swf_SetJPEGBitsFinish(jpeg);
86
87 // do some rotation animation
88
89     for (frame=0;frame<64;frame++)
90     {
91       t = swf_InsertTag(t,ST_DEFINESHAPE);
92     
93           swf_ShapeNew(&s);
94           
95           rgb.b = rgb.g = rgb.r = 0x00;
96           ls = swf_ShapeAddLineStyle(s,40,&rgb);
97           
98           swf_GetMatrix(NULL,&m);
99           
100           m.sy = m.sx = (int)(cos(((float)(frame))/32*3.141)*0x80000);
101           m.r0 = (int)(sin(((float)(frame))/32*3.141)*0x80000);
102           m.r1 = -m.r0;
103        
104           fs = swf_ShapeAddBitmapFillStyle(s,&m,ID_BITS,0);
105     
106           swf_SetU16(t,ID_SHAPE+frame);   // ID   
107
108           r.xmin = 0;
109           r.ymin = 0;
110           r.xmax = 10*WIDTH;
111           r.ymax = 10*HEIGHT;
112
113           swf_SetRect(t,&r);
114
115           swf_SetShapeHeader(t,s);
116
117           swf_ShapeSetAll(t,s,0,0,ls,fs,0);
118
119           swf_ShapeSetLine(t,s,10*WIDTH,0);
120           swf_ShapeSetLine(t,s,-10*WIDTH,10*HEIGHT);
121 //    swf_ShapeSetLine(t,s,-10*WIDTH,-10*WIDTH);
122           swf_ShapeSetLine(t,s,0,-10*HEIGHT);
123           swf_ShapeSetEnd(t);
124
125           swf_ShapeFree(s);
126
127       if (frame) 
128       { t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,1);
129         t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,2);
130       }
131          
132       t = swf_InsertTag(t,ST_PLACEOBJECT2);
133           swf_ObjectPlace(t,ID_SHAPE+frame,1,NULL,NULL,NULL); 
134
135       t = swf_InsertTag(t,ST_PLACEOBJECT2);
136           swf_GetMatrix(NULL,&m); // get default matrix with no transformation
137           
138           m.tx = m.ty = 10*WIDTH+frame*10;
139           m.sx = m.sy = 0xfffeffff;
140           swf_ObjectPlace(t,ID_SHAPE+frame,2,&m,NULL,NULL);
141
142
143       t = swf_InsertTag(t,ST_SHOWFRAME);
144       
145     } // frame loop
146
147   t = swf_InsertTag(t,ST_END);
148
149 //  swf_WriteCGI(&swf);
150
151   f = open("jpegtest.swf",O_WRONLY|O_CREAT||O_BINARY, 0644);
152   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
153   close(f);
154   
155   swf_FreeTags(&swf); // cleanup
156   
157   return 0;
158 }