more horizontal refactoring
[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 program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <math.h>
28 #include "../rfxswf.h"
29
30 #define WIDTH           256
31 #define HEIGHT          256
32
33 #define ID_BITS         1
34 #define ID_SHAPE        16
35
36
37 int main ( int argc, char ** argv)
38 { SWF swf;
39   TAG* t;
40   RGBA rgb;
41   SHAPE* s;
42   MATRIX m;
43   SRECT r;
44   JPEGBITS* jpeg;
45   int i,f;
46   
47   int ls; // line style
48   int fs; // fill style
49
50   int dx = 256; // bitmap size
51   int dy = 256;
52   int bps8, bps16, bps32;    // bytes per scanline
53
54   U8 * bitmap8;
55   U16 * bitmap16;
56   RGBA * bitmap32;
57   RGBA * pal;
58
59   // create test texture
60
61   bps8  = BYTES_PER_SCANLINE(dx*sizeof(U8));
62   bps16 = BYTES_PER_SCANLINE(dx*sizeof(U16));
63   bps32 = BYTES_PER_SCANLINE(dx*sizeof(U32));
64   
65   pal = malloc(256*sizeof(RGBA));
66
67   bitmap8 = malloc(bps8*dy);
68   bitmap16 = malloc(bps16*dy);
69   bitmap32 = malloc(bps32*dy);
70   
71   if ((bitmap8) && (pal) && (bitmap16))
72   { int x,y;
73     for (y=0;y<dy;y++)
74       for (x=0;x<dx;x++)
75         bitmap8[y*bps8+x] = (y/16)*16+(x/16);
76
77     for (x=0;x<256;x++)
78     {
79       pal[x].r = (x/16)*16;
80       pal[x].g = (x&15)*16;
81       pal[x].b = 0;
82       pal[x].a = x;
83       pal[x].r = (pal[x].r*pal[x].a)/255;
84       pal[x].g = (pal[x].g*pal[x].a)/255;
85       pal[x].b = (pal[x].b*pal[x].a)/255;
86     }
87
88     for (y=0;y<dy;y++)
89       for (x=0;x<dx;x++) {
90         U8 red = x;
91         U8 green = y;
92         U8 blue = x+y;
93         bitmap16[y*(bps16>>1)+x] = ((green/0x40)&0x03)|
94                                    ((red/4)&0x3f)<<2|
95                                    ((blue/8)&0x1f)<<8|
96                                    ((green/0x08)&0x07)<<13;
97       }
98
99     for (y=0;y<dy;y++)
100       for (x=0;x<dx;x++)
101         { bitmap32[y*(bps32>>2)+x].r = /*((x&0x10)==(y&0x10))?*/((x&4)==(y&4))?y:x;
102           bitmap32[y*(bps32>>2)+x].g = x;
103           bitmap32[y*(bps32>>2)+x].b = y;
104         }
105
106   } 
107   
108   // put texture into flash movie
109
110   memset(&swf,0x00,sizeof(SWF));
111
112   swf.fileVersion       = 4;
113   swf.frameRate         = 0x1800;
114   swf.movieSize.xmax    = 20*WIDTH;
115   swf.movieSize.ymax    = 20*HEIGHT;
116
117   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
118   t = swf.firstTag;
119
120     rgb.r = 0x00;
121     rgb.b = 0x00;
122     rgb.g = 0x00;
123     swf_SetRGB(t,&rgb);
124
125   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);
126
127     swf_SetU16(t,ID_BITS);
128     swf_SetLosslessBits(t,dx,dy,bitmap32,BMF_32BIT);
129     
130   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS2);
131
132     /* be careful with ST_DEFINEBITSLOSSLESS2, because
133        the Flash player produces great bugs if you use too many
134        alpha colors in your palette. The only sensible result that
135        can be archeived is setting one color to r=0,b=0,g=0,a=0 to
136        make transparent parts in sprites. That's the cause why alpha
137        handling is implemented in lossless routines of rfxswf.
138     */
139
140     swf_SetU16(t,ID_BITS+1);
141     swf_SetLosslessBitsIndexed(t,dx,dy,bitmap8,pal,256);
142     
143   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);
144
145     swf_SetU16(t,ID_BITS+2);
146     swf_SetLosslessBits(t,dx,dy,bitmap16,BMF_16BIT);
147
148   /* By the way: ST_DEFINELOSSLESS2 produces stange output on
149      16 and 32 bits image data, too.... it seems that the
150      ming developers deal with the same problem.
151   */
152
153   for (i=0;i<9;i++)
154   {
155     t = swf_InsertTag(t,ST_DEFINESHAPE);
156     
157     swf_ShapeNew(&s);
158     rgb.b = rgb.g = rgb.r = 0x00;
159     ls = swf_ShapeAddLineStyle(s,10,&rgb);  
160
161     swf_GetMatrix(NULL,&m);
162     m.sx = (6*WIDTH/dx)<<16;
163     m.sy = (6*HEIGHT/dy)<<16;
164
165     fs = swf_ShapeAddBitmapFillStyle(s,&m,ID_BITS+((i+(i/3))%3),0);
166     
167     swf_SetU16(t,ID_SHAPE+i);   // ID   
168
169     r.xmin = 0;
170     r.ymin = 0;
171     r.xmax = 6*WIDTH;
172     r.ymax = 6*HEIGHT;
173
174     swf_SetRect(t,&r);
175
176     swf_SetShapeStyles(t,s);
177     swf_ShapeCountBits(s,NULL,NULL);
178     swf_SetShapeBits(t,s);
179
180     swf_ShapeSetAll(t,s,0,0,ls,fs,0);
181
182     swf_ShapeSetLine(t,s,6*WIDTH,0);
183     swf_ShapeSetLine(t,s,0,6*HEIGHT);
184     swf_ShapeSetLine(t,s,-6*WIDTH,0);
185     swf_ShapeSetLine(t,s,0,-6*HEIGHT);
186     swf_ShapeSetEnd(t);
187
188     swf_GetMatrix(NULL,&m);
189     m.tx = (i%3) * (6*WIDTH+60);
190     m.ty = (i/3) * (6*HEIGHT+60);
191
192   t = swf_InsertTag(t,ST_PLACEOBJECT2);
193     swf_ObjectPlace(t,ID_SHAPE+i,1+i,&m,NULL,NULL);
194   }
195
196   t = swf_InsertTag(t,ST_SHOWFRAME);
197
198   t = swf_InsertTag(t,ST_END);
199
200 //  swf_WriteCGI(&swf);
201
202   f = open("zlibtest.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
203   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
204   close(f);
205
206   swf_FreeTags(&swf);
207
208 #ifdef __NT__
209   system("start ..\\zlibtest.swf");
210 #endif
211   
212   free(pal);
213   free(bitmap8);
214   free(bitmap16);
215   free(bitmap32);  
216   return 0;
217 }