initial version: little example for zlib-compressed lossless images in swf
authorboehme <boehme>
Sun, 28 Oct 2001 18:51:36 +0000 (18:51 +0000)
committerboehme <boehme>
Sun, 28 Oct 2001 18:51:36 +0000 (18:51 +0000)
lib/example/zlibtest.c [new file with mode: 0644]

diff --git a/lib/example/zlibtest.c b/lib/example/zlibtest.c
new file mode 100644 (file)
index 0000000..4608b6a
--- /dev/null
@@ -0,0 +1,190 @@
+/* zlibtest.c\r
+\r
+   Little example for rfxswf's lossless bitmap functions.\r
+   This program gives swf cgi output of three zlib compressed\r
+   images: 8 bit indexed, 16 and 32 bit\r
+\r
+   Part of the swftools package.\r
+\r
+   Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>\r
\r
+   This file is distributed under the GPL, see file COPYING for details \r
+\r
+*/\r
+\r
+// There's no makefile so try to compile like this on linux/gcc:\r
+// cc zlibtest.c ../rfxswf.c -funsigned-char -o zlibtest -lm -ljpeg -lz; cp zlibtest /home/www/cgi-bin/zlibtest\r
+\r
+#include <stdio.h>\r
+#include <math.h>\r
+#include <zlib.h>     \r
+#include "../rfxswf.h"\r
+\r
+#define WIDTH           256\r
+#define HEIGHT          256\r
+\r
+#define ID_BITS         1\r
+#define ID_SHAPE        16\r
+\r
+\r
+int main ( int argc, char ** argv)\r
+{ SWF swf;\r
+  LPTAG t;\r
+  RGBA rgb;\r
+  LPSHAPE s;\r
+  MATRIX m;\r
+  SRECT r;\r
+  LPJPEGBITS jpeg;\r
+  int i;\r
+  \r
+  int ls; // line style\r
+  int fs; // fill style\r
+\r
+  int dx = 256; // bitmap size\r
+  int dy = 256;\r
+  int bps8, bps16, bps32;    // bytes per scanline\r
+\r
+  U8 * bitmap8;\r
+  U16 * bitmap16;\r
+  RGBA * bitmap32;\r
+  RGBA * pal;\r
+\r
+  // create test texture\r
+\r
+  bps8  = BYTES_PER_SCANLINE(dx*sizeof(U8));\r
+  bps16 = BYTES_PER_SCANLINE(dx*sizeof(U16));\r
+  bps32 = BYTES_PER_SCANLINE(dx*sizeof(U32));\r
+  \r
+  pal = malloc(256*sizeof(RGBA));\r
+\r
+  bitmap8 = malloc(bps8*dy);\r
+  bitmap16 = malloc(bps16*dy);\r
+  bitmap32 = malloc(bps32*dy);\r
+  \r
+  if ((bitmap8) && (pal) && (bitmap16))\r
+  { int x,y;\r
+    for (y=0;y<dy;y++)\r
+      for (x=0;x<dx;x++)\r
+        bitmap8[y*bps8+x] = (y/16)*16+(x/16);\r
+\r
+    for (x=0;x<256;x++)\r
+    { pal[x].r = (x&0xf)*16;\r
+      pal[x].g = (x*2)&0xff;\r
+      pal[x].b = x&0xf0;\r
+      pal[x].a = (x==0xff)?0:0xff;\r
+    }\r
+\r
+    for (y=0;y<dy;y++)\r
+      for (x=0;x<dx;x++)\r
+        bitmap16[y*(bps16>>1)+x] = ((x&0xf0)==(y&0xf0))?0xffff:(x&0x0f)<(y&0xf)?BM16_RED|BM16_GREEN:BM16_BLUE;\r
+\r
+    for (y=0;y<dy;y++)\r
+      for (x=0;x<dx;x++)\r
+        { bitmap32[y*(bps32>>2)+x].r = /*((x&0x10)==(y&0x10))?*/((x&4)==(y&4))?y:x;\r
+          bitmap32[y*(bps32>>2)+x].g = x;\r
+          bitmap32[y*(bps32>>2)+x].b = y;\r
+        }\r
+\r
+  } \r
+  \r
+  // put texture into flash movie\r
+\r
+  memset(&swf,0x00,sizeof(SWF));\r
+\r
+  swf.FileVersion       = 4;\r
+  swf.FrameRate         = 0x1800;\r
+  swf.MovieSize.xmax    = 20*WIDTH;\r
+  swf.MovieSize.ymax    = 20*HEIGHT;\r
+\r
+  swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);\r
+  t = swf.FirstTag;\r
+\r
+    rgb.r = 0xff;\r
+    rgb.b = 0xff;\r
+    rgb.g = 0xff;\r
+    SetRGB(t,&rgb);\r
+\r
+  t = InsertTag(t,ST_DEFINEBITSLOSSLESS);\r
+\r
+    SetU16(t,ID_BITS);\r
+    SetLosslessBits(t,dx,dy,bitmap32,BMF_32BIT);\r
+    \r
+  t = InsertTag(t,ST_DEFINEBITSLOSSLESS2);\r
+\r
+    /* be careful with ST_DEFINEBITSLOSSLESS2, because\r
+       the Flash player produces great bugs if you use too many\r
+       alpha colors in your palette. The only sensible result that\r
+       can be archeived is setting one color to r=0,b=0,g=0,a=0 to\r
+       make transparent parts in sprites. That's the cause why alpha\r
+       handling is implemented in lossless routines of rfxswf.\r
+    */\r
+\r
+    SetU16(t,ID_BITS+1);\r
+    SetLosslessBitsIndexed(t,dx,dy,bitmap8,pal,256);\r
+    \r
+  t = InsertTag(t,ST_DEFINEBITSLOSSLESS);\r
+\r
+    SetU16(t,ID_BITS+2);\r
+    SetLosslessBits(t,dx,dy,bitmap16,BMF_16BIT);\r
+\r
+  /* By the way: ST_DEFINELOSSLESS2 produces stange output on\r
+     16 and 32 bits image data, too.... it seems that the\r
+     ming developers deal with the same problem.\r
+  */\r
+\r
+  for (i=0;i<9;i++)\r
+  {\r
+    t = InsertTag(t,ST_DEFINESHAPE);\r
+    \r
+    NewShape(&s);\r
+    rgb.b = rgb.g = rgb.r = 0x00;\r
+    ls = ShapeAddLineStyle(s,10,&rgb);  \r
+\r
+    GetMatrix(NULL,&m);\r
+    m.sx = (6*WIDTH/dx)<<16;\r
+    m.sy = (6*HEIGHT/dy)<<16;\r
+\r
+    fs = ShapeAddBitmapFillStyle(s,&m,ID_BITS+((i+(i/3))%3),0);\r
+    \r
+    SetU16(t,ID_SHAPE+i);   // ID   \r
+\r
+    r.xmin = 0;\r
+    r.ymin = 0;\r
+    r.xmax = 6*WIDTH;\r
+    r.ymax = 6*HEIGHT;\r
+\r
+    SetRect(t,&r);\r
+\r
+    SetShapeStyles(t,s);\r
+    ShapeCountBits(s,NULL,NULL);\r
+    SetShapeBits(t,s);\r
+\r
+    ShapeSetAll(t,s,0,0,ls,fs,0);\r
+\r
+    ShapeSetLine(t,s,6*WIDTH,0);\r
+    ShapeSetLine(t,s,0,6*HEIGHT);\r
+    ShapeSetLine(t,s,-6*WIDTH,0);\r
+    ShapeSetLine(t,s,0,-6*HEIGHT);\r
+    ShapeSetEnd(t);\r
+\r
+    GetMatrix(NULL,&m);\r
+    m.tx = (i%3) * (6*WIDTH+60);\r
+    m.ty = (i/3) * (6*HEIGHT+60);\r
+\r
+  t = InsertTag(t,ST_PLACEOBJECT2);\r
+    ObjectPlace(t,ID_SHAPE+i,1+i,&m,NULL,NULL);\r
+  }\r
+\r
+  t = InsertTag(t,ST_SHOWFRAME);\r
+\r
+  t = InsertTag(t,ST_END);\r
+\r
+  WriteCGI(&swf);\r
+  FreeTags(&swf);\r
+\r
+  free(pal);\r
+  free(bitmap8);\r
+  free(bitmap16);\r
+  free(bitmap32);  \r
+  return 0;\r
+}\r