* new reader functions (RGB, gradients)
[swftools.git] / lib / rfxswf.c
index f0f23ec..e1416dc 100644 (file)
@@ -239,6 +239,16 @@ int swf_SetRGB(TAG * t,RGBA * col)
   } else swf_SetBlock(t,NULL,3);
   return 0;
 }
+void swf_GetRGB(TAG * t, RGBA * col)
+{
+    RGBA dummy;
+    if(!col);
+       col = &dummy;
+    col->r = swf_GetU8(t);
+    col->g = swf_GetU8(t);
+    col->b = swf_GetU8(t);
+    col->a = 255;
+}
 
 int swf_SetRGBA(TAG * t,RGBA * col)
 { if (!t) return -1;
@@ -250,6 +260,36 @@ int swf_SetRGBA(TAG * t,RGBA * col)
   } else swf_SetBlock(t,NULL,4);
   return 0;
 }
+void swf_GetRGBA(TAG * t, RGBA * col)
+{
+    RGBA dummy;
+    if(!col);
+       col = &dummy;
+    col->r = swf_GetU8(t);
+    col->g = swf_GetU8(t);
+    col->b = swf_GetU8(t);
+    col->a = swf_GetU8(t);
+}
+
+void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
+{
+    GRADIENT dummy;
+    int t;
+    if(!gradient)
+       gradient = &dummy;
+    gradient->num = swf_GetU8(tag);
+    for(t=0;t<gradient->num;t++)
+    {
+       int s=t;
+       if(s>=8) //FIXME
+           s=7;
+       gradient->ratios[t] = swf_GetU8(tag);
+       if(!alpha)
+           swf_GetRGB(tag, &gradient->rgba[t]);
+       else
+           swf_GetRGBA(tag, &gradient->rgba[t]);
+    }
+}
 
 int swf_CountBits(U32 v,int nbits)
 { int n = 33;
@@ -668,6 +708,9 @@ int swf_WriteTag(int handle,TAG * t)
 int RFXSWF_DefineSprite_GetRealSize(TAG * t)
 // Sprite Handling: Helper function to pack DefineSprite-Tag
 { U32 len = t->len;
+  if(len>4) { // folded sprite
+      return t->len;
+  }
   do
   { t = swf_NextTag(t);
     if (t->id!=ST_DEFINESPRITE) len += RFXSWF_WriteTag(-1,t);
@@ -676,6 +719,67 @@ int RFXSWF_DefineSprite_GetRealSize(TAG * t)
   return len;
 }
 
+void swf_FoldSprite(TAG * t)
+{
+  TAG*sprtag=t,*tmp;
+  U16 id,frames,tmpid;
+  if(t->id!=ST_DEFINESPRITE)
+      return;
+  if(!t->len) {
+      fprintf(stderr, "Error: Sprite has no ID!");
+      return;
+  }
+
+  t->pos = 0;
+  id = swf_GetU16(t);
+  //frames = swf_GetU16(t);
+  free(t->data);
+  t->len = t->pos = t->memsize = 0;
+  t->data = 0;
+
+  frames = 0;
+
+  do 
+  { 
+    if(t->id==ST_SHOWFRAME) frames++;
+    t = swf_NextTag(t);
+  } while(t&&t!=ST_END);
+
+  t = swf_NextTag(sprtag);
+  swf_SetU16(sprtag, id);
+  swf_SetU16(sprtag, frames);
+
+  do
+  { 
+    tmpid= t->id;
+    if(t->len<0x3f) {
+       swf_SetU16(sprtag,t->len|(t->id<<6));
+    } else {
+       swf_SetU16(sprtag,0x3f|(t->id<<6));
+       swf_SetU32(sprtag,t->len);
+    }
+    if(t->len)
+       swf_SetBlock(sprtag,t->data, t->len);
+    tmp = t;
+    t = swf_NextTag(t);
+    swf_DeleteTag(tmp);
+  } 
+  while (t&&(tmpid!=ST_END));
+
+//  sprtag->next = t;
+//  t->prev = sprtag;
+}
+
+void swf_FoldAll(SWF*swf)
+{
+    TAG*tag = swf->firstTag;
+    while(tag) {
+       if(tag->id == ST_DEFINESPRITE)
+           swf_FoldSprite(tag);
+       tag = swf_NextTag(tag);
+    }
+}
+
 #define swf_ReadTag(a,b)  RFXSWF_ReadTag(a,b)
 #define swf_WriteTag(a,b)  RFXSWF_WriteTag(a,b)
 
@@ -712,6 +816,7 @@ int swf_InitSWF(void*data, int length, SWF * swf) /* copy a swf in memory into S
     swf->firstTag = t1.next;
     t1.next->prev = NULL;*/
   }
+  return 0;
 }
 
 int swf_ReadSWF(int handle,SWF * swf)   // Reads SWF to memory (malloc'ed), returns length or <0 if fails
@@ -833,6 +938,7 @@ int swf_WriteHeader(int handle,SWF * swf)
     memcpy(&myswf,swf,sizeof(SWF));
     myswf.firstTag = 0;
     swf_WriteSWF(handle, &myswf);
+    return 0;
 }
 
 int swf_WriteCGI(SWF * swf)
@@ -877,6 +983,7 @@ void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for t
 #include "modules\swfcgi.c"
 #include "modules\swfbits.c"
 #include "modules\swfaction.c"
+#include "modules\swfsound.c"
 
 #else
 
@@ -889,6 +996,7 @@ void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for t
 #include "modules/swfcgi.c"
 #include "modules/swfbits.c"
 #include "modules/swfaction.c"
+#include "modules/swfsound.c"
 
 #endif