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