summaryrefslogtreecommitdiff
path: root/lib/example/zlibtest.c
blob: 816f17d924a73e2a21e6d503bacf028eeaa4b17a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* zlibtest.c

   Little example for rfxswf's lossless bitmap functions.
   This program creates a swf with three zlib compressed
   images: 8 bit indexed, 16 and 32 bit

   Part of the swftools package.

   Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include "../rfxswf.h"

#define WIDTH           256
#define HEIGHT          256

#define ID_BITS         1
#define ID_SHAPE        16


int main ( int argc, char ** argv)
{ SWF swf;
  TAG* t;
  RGBA rgb;
  SHAPE* s;
  MATRIX m;
  SRECT r;
  JPEGBITS* jpeg;
  int i,f;
  
  int ls; // line style
  int fs; // fill style

  int dx = 256; // bitmap size
  int dy = 256;
  int bps8, bps16, bps32;    // bytes per scanline

  U8 * bitmap8;
  U16 * bitmap16;
  RGBA * bitmap32;
  RGBA * pal;

  // create test texture

  bps8  = BYTES_PER_SCANLINE(dx*sizeof(U8));
  bps16 = BYTES_PER_SCANLINE(dx*sizeof(U16));
  bps32 = BYTES_PER_SCANLINE(dx*sizeof(U32));
  
  pal = malloc(256*sizeof(RGBA));

  bitmap8 = malloc(bps8*dy);
  bitmap16 = malloc(bps16*dy);
  bitmap32 = malloc(bps32*dy);
  
  if ((bitmap8) && (pal) && (bitmap16))
  { int x,y;
    for (y=0;y<dy;y++)
      for (x=0;x<dx;x++)
        bitmap8[y*bps8+x] = (y/16)*16+(x/16);

    for (x=0;x<256;x++)
    {
      pal[x].r = (x/16)*16;
      pal[x].g = (x&15)*16;
      pal[x].b = 0;
      pal[x].a = x;
      pal[x].r = (pal[x].r*pal[x].a)/255;
      pal[x].g = (pal[x].g*pal[x].a)/255;
      pal[x].b = (pal[x].b*pal[x].a)/255;
    }

    for (y=0;y<dy;y++)
      for (x=0;x<dx;x++) {
	U8 red = x;
	U8 green = y;
	U8 blue = x+y;
        bitmap16[y*(bps16>>1)+x] = ((green/0x40)&0x03)|
	                           ((red/4)&0x3f)<<2|
				   ((blue/8)&0x1f)<<8|
				   ((green/0x08)&0x07)<<13;
      }

    for (y=0;y<dy;y++)
      for (x=0;x<dx;x++)
        { bitmap32[y*(bps32>>2)+x].r = /*((x&0x10)==(y&0x10))?*/((x&4)==(y&4))?y:x;
          bitmap32[y*(bps32>>2)+x].g = x;
          bitmap32[y*(bps32>>2)+x].b = y;
        }

  } 
  
  // put texture into flash movie

  memset(&swf,0x00,sizeof(SWF));

  swf.fileVersion       = 4;
  swf.frameRate         = 0x1800;
  swf.movieSize.xmax    = 20*WIDTH;
  swf.movieSize.ymax    = 20*HEIGHT;

  swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
  t = swf.firstTag;

    rgb.r = 0x00;
    rgb.b = 0x00;
    rgb.g = 0x00;
    swf_SetRGB(t,&rgb);

  t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);

    swf_SetU16(t,ID_BITS);
    swf_SetLosslessBits(t,dx,dy,bitmap32,BMF_32BIT);
    
  t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS2);

    /* be careful with ST_DEFINEBITSLOSSLESS2, because
       the Flash player produces great bugs if you use too many
       alpha colors in your palette. The only sensible result that
       can be archeived is setting one color to r=0,b=0,g=0,a=0 to
       make transparent parts in sprites. That's the cause why alpha
       handling is implemented in lossless routines of rfxswf.
    */

    swf_SetU16(t,ID_BITS+1);
    swf_SetLosslessBitsIndexed(t,dx,dy,bitmap8,pal,256);
    
  t = swf_InsertTag(t,ST_DEFINEBITSLOSSLESS);

    swf_SetU16(t,ID_BITS+2);
    swf_SetLosslessBits(t,dx,dy,bitmap16,BMF_16BIT);

  /* By the way: ST_DEFINELOSSLESS2 produces stange output on
     16 and 32 bits image data, too.... it seems that the
     ming developers deal with the same problem.
  */

  for (i=0;i<9;i++)
  {
    t = swf_InsertTag(t,ST_DEFINESHAPE);
    
    swf_ShapeNew(&s);
    rgb.b = rgb.g = rgb.r = 0x00;
    ls = swf_ShapeAddLineStyle(s,10,&rgb);  

    swf_GetMatrix(NULL,&m);
    m.sx = (6*WIDTH/dx)<<16;
    m.sy = (6*HEIGHT/dy)<<16;

    fs = swf_ShapeAddBitmapFillStyle(s,&m,ID_BITS+((i+(i/3))%3),0);
    
    swf_SetU16(t,ID_SHAPE+i);   // ID   

    r.xmin = 0;
    r.ymin = 0;
    r.xmax = 6*WIDTH;
    r.ymax = 6*HEIGHT;

    swf_SetRect(t,&r);

    swf_SetShapeStyles(t,s);
    swf_ShapeCountBits(s,NULL,NULL);
    swf_SetShapeBits(t,s);

    swf_ShapeSetAll(t,s,0,0,ls,fs,0);

    swf_ShapeSetLine(t,s,6*WIDTH,0);
    swf_ShapeSetLine(t,s,0,6*HEIGHT);
    swf_ShapeSetLine(t,s,-6*WIDTH,0);
    swf_ShapeSetLine(t,s,0,-6*HEIGHT);
    swf_ShapeSetEnd(t);

    swf_GetMatrix(NULL,&m);
    m.tx = (i%3) * (6*WIDTH+60);
    m.ty = (i/3) * (6*HEIGHT+60);

  t = swf_InsertTag(t,ST_PLACEOBJECT2);
    swf_ObjectPlace(t,ID_SHAPE+i,1+i,&m,NULL,NULL);
  }

  t = swf_InsertTag(t,ST_SHOWFRAME);

  t = swf_InsertTag(t,ST_END);

//  swf_WriteCGI(&swf);

  f = open("zlibtest.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
  if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
  close(f);

  swf_FreeTags(&swf);

#ifdef __NT__
  system("start ..\\zlibtest.swf");
#endif
  
  free(pal);
  free(bitmap8);
  free(bitmap16);
  free(bitmap32);  
  return 0;
}