* updated examples to new rfxswf name conventions
[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 file is distributed under the GPL, see file COPYING for details 
10
11 */
12
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <math.h>       
16 #include "../rfxswf.h"
17
18 #define WIDTH           256
19 #define HEIGHT          256
20 #define QUALITY         85
21
22 #define ID_BITS         1
23 #define ID_SHAPE        2
24
25 int main( int argc, char ** argv)
26 { SWF swf;
27   LPTAG t;
28   RGBA rgb;
29   LPSHAPE s;
30   MATRIX m;
31   SRECT r;
32   LPJPEGBITS jpeg;
33
34   int f; // file handle
35   
36   int ls; // line style
37   int fs; // fill style
38   int frame;
39
40   memset(&swf,0x00,sizeof(SWF));
41
42   swf.fileVersion       = 4;
43   swf.frameRate         = 0x1800;
44   swf.movieSize.xmax    = 20*WIDTH;
45   swf.movieSize.ymax    = 20*HEIGHT;
46
47   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
48   t = swf.firstTag;
49
50     rgb.r = 0xff;
51     rgb.b = 0xff;
52     rgb.g = 0xff;
53     swf_SetRGB(t,&rgb);
54
55   t = swf_InsertTag(t,ST_DEFINEBITSJPEG2);
56
57     swf_SetU16(t,ID_BITS);
58 //    swf_SetJPEGBits(t,"test.jpg",QUALITY);  <- use this to include an image from disk
59     
60 //  That's the way to use memory bitmaps (24bit,RGB)
61
62     jpeg = swf_SetJPEGBitsStart(t,WIDTH,HEIGHT,QUALITY);
63     { int y;
64       for (y=0;y<HEIGHT;y++)
65       { U8 scanline[3*WIDTH];
66         int x,p = 0;
67         for (x=0;x<WIDTH;x++) 
68         { scanline[p++] = x;    // R
69           scanline[p++] = y;    // G
70           scanline[p++] = 0x80; // B          
71         }
72         swf_SetJPEGBitsLine(jpeg,scanline);
73       }
74     }
75     swf_SetJPEGBitsFinish(jpeg);
76
77 // do some rotation animation
78
79     for (frame=0;frame<64;frame++)
80     {
81       t = swf_InsertTag(t,ST_DEFINESHAPE);
82     
83           swf_ShapeNew(&s);
84           
85           rgb.b = rgb.g = rgb.r = 0x00;
86           ls = swf_ShapeAddLineStyle(s,40,&rgb);
87           
88           swf_GetMatrix(NULL,&m);
89           
90           m.sy = m.sx = (int)(cos(((float)(frame))/32*3.141)*0x80000);
91           m.r0 = (int)(sin(((float)(frame))/32*3.141)*0x80000);
92           m.r1 = -m.r0;
93        
94           fs = swf_ShapeAddBitmapFillStyle(s,&m,ID_BITS,0);
95     
96           swf_SetU16(t,ID_SHAPE+frame);   // ID   
97
98           r.xmin = 0;
99           r.ymin = 0;
100           r.xmax = 10*WIDTH;
101           r.ymax = 10*HEIGHT;
102
103           swf_SetRect(t,&r);
104
105           swf_SetShapeHeader(t,s);
106
107           swf_ShapeSetAll(t,s,0,0,ls,fs,0);
108
109           swf_ShapeSetLine(t,s,10*WIDTH,0);
110           swf_ShapeSetLine(t,s,-10*WIDTH,10*HEIGHT);
111 //    swf_ShapeSetLine(t,s,-10*WIDTH,-10*WIDTH);
112           swf_ShapeSetLine(t,s,0,-10*HEIGHT);
113           swf_ShapeSetEnd(t);
114
115           swf_ShapeFree(s);
116
117       if (frame) 
118       { t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,1);
119         t = swf_InsertTag(t,ST_REMOVEOBJECT2); swf_SetU16(t,2);
120       }
121          
122       t = swf_InsertTag(t,ST_PLACEOBJECT2);
123           swf_ObjectPlace(t,ID_SHAPE+frame,1,NULL,NULL,NULL); 
124
125       t = swf_InsertTag(t,ST_PLACEOBJECT2);
126           swf_GetMatrix(NULL,&m); // get default matrix with no transformation
127           
128           m.tx = m.ty = 10*WIDTH+frame*10;
129           m.sx = m.sy = 0xfffeffff;
130           swf_ObjectPlace(t,ID_SHAPE+frame,2,&m,NULL,NULL);
131
132
133       t = swf_InsertTag(t,ST_SHOWFRAME);
134       
135     } // frame loop
136
137   t = swf_InsertTag(t,ST_END);
138
139 //  swf_WriteCGI(&swf);
140
141   f = open("jpegtest.swf",O_WRONLY|O_CREAT, 0644);
142   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
143   close(f);
144   
145   swf_FreeTags(&swf); // cleanup
146   
147   return 0;
148 }