* updated examples to new rfxswf name conventions
[swftools.git] / lib / example / zlibtest.c
1 /* zlibtest.c
2
3    Little example for rfxswf's lossless bitmap functions.
4    This program creates a swf with three zlib compressed
5    images: 8 bit indexed, 16 and 32 bit
6
7    Part of the swftools package.
8
9    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
10  
11    This file is distributed under the GPL, see file COPYING for details 
12
13 */
14
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <math.h>
18 #include <zlib.h>     
19 #include "../rfxswf.h"
20
21 #define WIDTH           256
22 #define HEIGHT          256
23
24 #define ID_BITS         1
25 #define ID_SHAPE        16
26
27
28 int main ( int argc, char ** argv)
29 { SWF swf;
30   LPTAG t;
31   RGBA rgb;
32   LPSHAPE s;
33   MATRIX m;
34   SRECT r;
35   LPJPEGBITS jpeg;
36   int i,f;
37   
38   int ls; // line style
39   int fs; // fill style
40
41   int dx = 256; // bitmap size
42   int dy = 256;
43   int bps8, bps16, bps32;    // bytes per scanline
44
45   U8 * bitmap8;
46   U16 * bitmap16;
47   RGBA * bitmap32;
48   RGBA * pal;
49
50   // create test texture
51
52   bps8  = BYTES_PER_SCANLINE(dx*sizeof(U8));
53   bps16 = BYTES_PER_SCANLINE(dx*sizeof(U16));
54   bps32 = BYTES_PER_SCANLINE(dx*sizeof(U32));
55   
56   pal = malloc(256*sizeof(RGBA));
57
58   bitmap8 = malloc(bps8*dy);
59   bitmap16 = malloc(bps16*dy);
60   bitmap32 = malloc(bps32*dy);
61   
62   if ((bitmap8) && (pal) && (bitmap16))
63   { int x,y;
64     for (y=0;y<dy;y++)
65       for (x=0;x<dx;x++)
66         bitmap8[y*bps8+x] = (y/16)*16+(x/16);
67
68     for (x=0;x<256;x++)
69     { pal[x].r = (x&0xf)*16;
70       pal[x].g = (x*2)&0xff;
71       pal[x].b = x&0xf0;
72       pal[x].a = (x==0xff)?0:0xff;
73     }
74
75     for (y=0;y<dy;y++)
76       for (x=0;x<dx;x++)
77         bitmap16[y*(bps16>>1)+x] = ((x&0xf0)==(y&0xf0))?0xffff:(x&0x0f)<(y&0xf)?BM16_RED|BM16_GREEN:BM16_BLUE;
78
79     for (y=0;y<dy;y++)
80       for (x=0;x<dx;x++)
81         { bitmap32[y*(bps32>>2)+x].r = /*((x&0x10)==(y&0x10))?*/((x&4)==(y&4))?y:x;
82           bitmap32[y*(bps32>>2)+x].g = x;
83           bitmap32[y*(bps32>>2)+x].b = y;
84         }
85
86   } 
87   
88   // put texture into flash movie
89
90   memset(&swf,0x00,sizeof(SWF));
91
92   swf.fileVersion       = 4;
93   swf.frameRate         = 0x1800;
94   swf.movieSize.xmax    = 20*WIDTH;
95   swf.movieSize.ymax    = 20*HEIGHT;
96
97   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
98   t = swf.firstTag;
99
100     rgb.r = 0xff;
101     rgb.b = 0xff;
102     rgb.g = 0xff;
103     swf_SetRGB(t,&rgb);
104
105   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);
106
107     swf_SetU16(t,ID_BITS);
108     swf_SetLosslessBits(t,dx,dy,bitmap32,BMF_32BIT);
109     
110   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS2);
111
112     /* be careful with ST_DEFINEBITSLOSSLESS2, because
113        the Flash player produces great bugs if you use too many
114        alpha colors in your palette. The only sensible result that
115        can be archeived is setting one color to r=0,b=0,g=0,a=0 to
116        make transparent parts in sprites. That's the cause why alpha
117        handling is implemented in lossless routines of rfxswf.
118     */
119
120     swf_SetU16(t,ID_BITS+1);
121     swf_SetLosslessBitsIndexed(t,dx,dy,bitmap8,pal,256);
122     
123   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);
124
125     swf_SetU16(t,ID_BITS+2);
126     swf_SetLosslessBits(t,dx,dy,bitmap16,BMF_16BIT);
127
128   /* By the way: ST_DEFINELOSSLESS2 produces stange output on
129      16 and 32 bits image data, too.... it seems that the
130      ming developers deal with the same problem.
131   */
132
133   for (i=0;i<9;i++)
134   {
135     t = swf_InsertTag(t,ST_DEFINESHAPE);
136     
137     swf_ShapeNew(&s);
138     rgb.b = rgb.g = rgb.r = 0x00;
139     ls = swf_ShapeAddLineStyle(s,10,&rgb);  
140
141     swf_GetMatrix(NULL,&m);
142     m.sx = (6*WIDTH/dx)<<16;
143     m.sy = (6*HEIGHT/dy)<<16;
144
145     fs = swf_ShapeAddBitmapFillStyle(s,&m,ID_BITS+((i+(i/3))%3),0);
146     
147     swf_SetU16(t,ID_SHAPE+i);   // ID   
148
149     r.xmin = 0;
150     r.ymin = 0;
151     r.xmax = 6*WIDTH;
152     r.ymax = 6*HEIGHT;
153
154     swf_SetRect(t,&r);
155
156     swf_SetShapeStyles(t,s);
157     swf_ShapeCountBits(s,NULL,NULL);
158     swf_SetShapeBits(t,s);
159
160     swf_ShapeSetAll(t,s,0,0,ls,fs,0);
161
162     swf_ShapeSetLine(t,s,6*WIDTH,0);
163     swf_ShapeSetLine(t,s,0,6*HEIGHT);
164     swf_ShapeSetLine(t,s,-6*WIDTH,0);
165     swf_ShapeSetLine(t,s,0,-6*HEIGHT);
166     swf_ShapeSetEnd(t);
167
168     swf_GetMatrix(NULL,&m);
169     m.tx = (i%3) * (6*WIDTH+60);
170     m.ty = (i/3) * (6*HEIGHT+60);
171
172   t = swf_InsertTag(t,ST_PLACEOBJECT2);
173     swf_ObjectPlace(t,ID_SHAPE+i,1+i,&m,NULL,NULL);
174   }
175
176   t = swf_InsertTag(t,ST_SHOWFRAME);
177
178   t = swf_InsertTag(t,ST_END);
179
180 //  swf_WriteCGI(&swf);
181
182   f = open("zlibtest.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
183   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
184   close(f);
185
186   swf_FreeTags(&swf);
187
188 #ifdef __NT__
189   system("start ..\\zlibtest.swf");
190 #endif
191   
192   free(pal);
193   free(bitmap8);
194   free(bitmap16);
195   free(bitmap32);  
196   return 0;
197 }