fixed graphics bug
[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   LPTAG t;
40   RGBA rgb;
41   LPSHAPE s;
42   MATRIX m;
43   SRECT r;
44   LPJPEGBITS 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     { pal[x].r = (x&0xf)*16;
79       pal[x].g = (x*2)&0xff;
80       pal[x].b = x&0xf0;
81       pal[x].a = (x==0xff)?0:0xff;
82     }
83
84     for (y=0;y<dy;y++)
85       for (x=0;x<dx;x++)
86         bitmap16[y*(bps16>>1)+x] = ((x&0xf0)==(y&0xf0))?0xffff:(x&0x0f)<(y&0xf)?BM16_RED|BM16_GREEN:BM16_BLUE;
87
88     for (y=0;y<dy;y++)
89       for (x=0;x<dx;x++)
90         { bitmap32[y*(bps32>>2)+x].r = /*((x&0x10)==(y&0x10))?*/((x&4)==(y&4))?y:x;
91           bitmap32[y*(bps32>>2)+x].g = x;
92           bitmap32[y*(bps32>>2)+x].b = y;
93         }
94
95   } 
96   
97   // put texture into flash movie
98
99   memset(&swf,0x00,sizeof(SWF));
100
101   swf.fileVersion       = 4;
102   swf.frameRate         = 0x1800;
103   swf.movieSize.xmax    = 20*WIDTH;
104   swf.movieSize.ymax    = 20*HEIGHT;
105
106   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
107   t = swf.firstTag;
108
109     rgb.r = 0xff;
110     rgb.b = 0xff;
111     rgb.g = 0xff;
112     swf_SetRGB(t,&rgb);
113
114   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);
115
116     swf_SetU16(t,ID_BITS);
117     swf_SetLosslessBits(t,dx,dy,bitmap32,BMF_32BIT);
118     
119   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS2);
120
121     /* be careful with ST_DEFINEBITSLOSSLESS2, because
122        the Flash player produces great bugs if you use too many
123        alpha colors in your palette. The only sensible result that
124        can be archeived is setting one color to r=0,b=0,g=0,a=0 to
125        make transparent parts in sprites. That's the cause why alpha
126        handling is implemented in lossless routines of rfxswf.
127     */
128
129     swf_SetU16(t,ID_BITS+1);
130     swf_SetLosslessBitsIndexed(t,dx,dy,bitmap8,pal,256);
131     
132   t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);
133
134     swf_SetU16(t,ID_BITS+2);
135     swf_SetLosslessBits(t,dx,dy,bitmap16,BMF_16BIT);
136
137   /* By the way: ST_DEFINELOSSLESS2 produces stange output on
138      16 and 32 bits image data, too.... it seems that the
139      ming developers deal with the same problem.
140   */
141
142   for (i=0;i<9;i++)
143   {
144     t = swf_InsertTag(t,ST_DEFINESHAPE);
145     
146     swf_ShapeNew(&s);
147     rgb.b = rgb.g = rgb.r = 0x00;
148     ls = swf_ShapeAddLineStyle(s,10,&rgb);  
149
150     swf_GetMatrix(NULL,&m);
151     m.sx = (6*WIDTH/dx)<<16;
152     m.sy = (6*HEIGHT/dy)<<16;
153
154     fs = swf_ShapeAddBitmapFillStyle(s,&m,ID_BITS+((i+(i/3))%3),0);
155     
156     swf_SetU16(t,ID_SHAPE+i);   // ID   
157
158     r.xmin = 0;
159     r.ymin = 0;
160     r.xmax = 6*WIDTH;
161     r.ymax = 6*HEIGHT;
162
163     swf_SetRect(t,&r);
164
165     swf_SetShapeStyles(t,s);
166     swf_ShapeCountBits(s,NULL,NULL);
167     swf_SetShapeBits(t,s);
168
169     swf_ShapeSetAll(t,s,0,0,ls,fs,0);
170
171     swf_ShapeSetLine(t,s,6*WIDTH,0);
172     swf_ShapeSetLine(t,s,0,6*HEIGHT);
173     swf_ShapeSetLine(t,s,-6*WIDTH,0);
174     swf_ShapeSetLine(t,s,0,-6*HEIGHT);
175     swf_ShapeSetEnd(t);
176
177     swf_GetMatrix(NULL,&m);
178     m.tx = (i%3) * (6*WIDTH+60);
179     m.ty = (i/3) * (6*HEIGHT+60);
180
181   t = swf_InsertTag(t,ST_PLACEOBJECT2);
182     swf_ObjectPlace(t,ID_SHAPE+i,1+i,&m,NULL,NULL);
183   }
184
185   t = swf_InsertTag(t,ST_SHOWFRAME);
186
187   t = swf_InsertTag(t,ST_END);
188
189 //  swf_WriteCGI(&swf);
190
191   f = open("zlibtest.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
192   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
193   close(f);
194
195   swf_FreeTags(&swf);
196
197 #ifdef __NT__
198   system("start ..\\zlibtest.swf");
199 #endif
200   
201   free(pal);
202   free(bitmap8);
203   free(bitmap16);
204   free(bitmap32);  
205   return 0;
206 }