1 #include "../gfxdevice.h"
2 #include "../gfxsource.h"
3 #include "../gfxtools.h"
9 typedef struct _map16_t
14 typedef struct _swf_page_internal
17 } swf_page_internal_t;
19 typedef struct _swf_doc_internal
33 typedef struct _character
41 typedef struct _placement
48 typedef struct _sprite
53 static void placement_free(placement_t*p)
55 swf_PlaceObjectFree(&p->po);
59 //---- object/depth handling ----
63 map16_t*map = rfx_calloc(sizeof(map16_t));
64 /* TODO: replace this by a more sophisticated data structure */
65 map->ids = (void**)rfx_calloc(sizeof(character_t)*65536);
68 character_t*map16_get_id(map16_t*map, int id)
74 void map16_free(map16_t*map)
78 void map16_add_id(map16_t*map, int nr, void*id)
81 fprintf(stderr, "Warning: ID %d defined more than once\n");
84 void map16_remove_id(map16_t*map, int nr)
88 void map16_enumerate(map16_t*map, void (*f)(void*self, int id, void*data), void*self)
91 for(t=0;t<65536;t++) {
93 f(self, t, map->ids[t]);
98 //---- bitmap handling ----
100 typedef struct _bitmap
106 bitmap_t* bitmap_new(RGBA*data, int width, int height)
108 bitmap_t* b = (bitmap_t*)rfx_calloc(sizeof(bitmap_t));
115 void bitmap_free(bitmap_t*b)
124 static map16_t* extractDefinitions(SWF*swf)
126 map16_t*map = map16_new();
127 TAG*tag = swf->firstTag;
131 if(swf_isDefiningTag(tag)) {
132 id = swf_GetDefineID(tag);
135 if(tag->id == ST_DEFINESPRITE) {
136 character_t*c = rfx_calloc(sizeof(character_t));
137 sprite_t*s = rfx_calloc(sizeof(sprite_t));
138 swf_SetTagPos(tag, 0);
139 swf_GetU16(tag); //id
140 s->frameCount = swf_GetU16(tag); //frameno
142 c->type = TYPE_SPRITE;
144 map16_add_id(map, id, c);
146 else if(tag->id == ST_DEFINESHAPE ||
147 tag->id == ST_DEFINESHAPE2 ||
148 tag->id == ST_DEFINESHAPE3) {
149 character_t*c = rfx_calloc(sizeof(character_t));
151 c->type = TYPE_SHAPE;
152 map16_add_id(map, id, c);
154 else if(tag->id == ST_DEFINEFONT ||
155 tag->id == ST_DEFINEFONT2) {
156 character_t*c = rfx_calloc(sizeof(character_t));
158 swf_FontExtract(swf, id, &font);
162 map16_add_id(map, id, c);
164 else if(tag->id == ST_DEFINEBITSJPEG ||
165 tag->id == ST_DEFINEBITSJPEG2 ||
166 tag->id == ST_DEFINEBITSJPEG3 ||
167 tag->id == ST_DEFINEBITSLOSSLESS ||
168 tag->id == ST_DEFINEBITSLOSSLESS2) {
169 character_t*c = rfx_calloc(sizeof(character_t));
171 void*data = swf_ExtractImage(tag, &width, &height);
172 bitmap_t*b = bitmap_new(data, width, height);
174 c->type = TYPE_BITMAP;
176 map16_add_id(map, id, c);
184 void swf_FreeTaglist(TAG*tag)
188 TAG * tnew = tag->next;
196 static void increaseAge(void*self, int id, void*data)
198 placement_t*p = (placement_t*)data;
202 static map16_t* extractFrame(TAG*startTag, int frame_to_extract)
204 map16_t*depthmap = map16_new();
208 for(;tag;tag = tag->next) {
209 if(tag->id == ST_END)
211 if(tag->id == ST_DEFINESPRITE) {
212 while(tag->id != ST_END)
216 if(tag->id == ST_PLACEOBJECT ||
217 tag->id == ST_PLACEOBJECT2) {
218 placement_t* p = rfx_calloc(sizeof(placement_t));
220 p->startFrame = frame;
221 swf_GetPlaceObject(tag, &p->po);
223 placement_t*old = (placement_t*)map16_get_id(depthmap, p->po.depth);
224 p->po.id = old->po.id;
225 map16_remove_id(depthmap, p->po.depth);
228 map16_add_id(depthmap, p->po.depth, p);
231 if(tag->id == ST_REMOVEOBJECT ||
232 tag->id == ST_REMOVEOBJECT2) {
233 U16 depth = swf_GetDepth(tag);
234 map16_remove_id(depthmap, depth);
236 if(tag->id == ST_SHOWFRAME) {
237 if(frame == frame_to_extract) {
241 map16_enumerate(depthmap, increaseAge, 0);
247 // ---- render handling ----
249 typedef struct _render
256 static void stopClippings(int from, render_t*r)
259 for(t=from;t<r->clips;t++)
260 r->device->endclip(r->device);
264 gfxline_t* swfline_to_gfxline(SHAPELINE*line, int linestyle, int fillstyle0)
269 gfxdrawer_target_gfxline(&d);
270 if(line->type != moveTo) {
271 fprintf(stderr, "Warning: Shape doesn't start with a moveTo\n");
274 if(line->fillstyle0 == fillstyle0 || line->fillstyle1 == fillstyle0 ||
275 line->linestyle == linestyle) {
276 if(line->type == lineTo) {
277 d.moveTo(&d, x/20.0,y/20.0);
278 d.lineTo(&d, line->x/20.0,line->y/20.0);
279 } else if(line->type == splineTo) {
280 d.moveTo(&d, x/20.0,y/20.0);
281 d.splineTo(&d, line->sx/20.0, line->sy/20.0, line->x/20.0,line->y/20.0);
292 void swf_ShapeApplyMatrix(SHAPE2*shape, MATRIX*m)
296 static void convertMatrix(MATRIX*from, gfxmatrix_t*to)
298 to->m00 = from->sx / 65536.0; to->m10 = from->r1 / 65536.0;
299 to->m01 = from->r0 / 65536.0; to->m11 = from->sy / 65536.0;
300 to->tx = from->tx/20.0;
301 to->ty = from->ty/20.0;
304 static void convertCXForm(CXFORM*from, gfxcxform_t*to)
306 memset(to, 0, sizeof(gfxcxform_t));
307 to->aa = from->a0 / 256.0;
308 to->rr = from->r0 / 256.0;
309 to->gg = from->g0 / 256.0;
310 to->bb = from->b0 / 256.0;
317 static gfxgradient_t* convertGradient(GRADIENT*from)
319 gfxgradient_t*g = rfx_calloc(from->num * sizeof(gfxgradient_t));
321 for(t=0;t<from->num;t++) {
322 g[t].pos = from->ratios[t] / 255.0;
323 g[t].color = *(gfxcolor_t*)&from->rgba[t];
332 gfximage_t* findimage(render_t*r)
337 static void renderFilled(render_t*r, gfxline_t*line, FILLSTYLE*f, CXFORM*cx)
339 if(f->type == FILL_SOLID) {
340 gfxcolor_t c = *(gfxcolor_t*)&f->color;
341 r->device->fill(r->device, line, &c);
342 } else if(f->type == FILL_TILED || f->type == FILL_CLIPPED) {
343 gfximage_t* img = findimage(r);
346 convertCXForm(cx, &gfxcx);
347 convertMatrix(&f->m, &m);
348 /* TODO: handle clipped */
349 r->device->fillbitmap(r->device, line, img, &m, &gfxcx);
350 } else if(f->type == FILL_LINEAR || f->type == FILL_RADIAL) {
353 convertMatrix(&f->m, &m);
354 g = convertGradient(&f->gradient);
355 r->device->fillgradient(r->device, line, g, f->type == FILL_LINEAR ? gfxgradient_linear : gfxgradient_radial, &m);
360 RGBA swf_ColorTransform(RGBA*color, CXFORM*cx)
363 dest.r = (cx->r0*dest.r + cx->r1*256) >> 8;
364 dest.g = (cx->g0*dest.g + cx->g1*256) >> 8;
365 dest.b = (cx->b0*dest.b + cx->b1*256) >> 8;
366 dest.a = (cx->a0*dest.a + cx->a1*256) >> 8;
370 void renderOutline(render_t*r, gfxline_t*line, LINESTYLE*l, CXFORM*cx)
372 RGBA c = swf_ColorTransform(&l->color, cx);
373 gfxcoord_t width = l->width/20.0;
374 r->device->stroke(r->device, line, width, (gfxcolor_t*)&c, gfx_capRound, gfx_joinRound, 0.0);
377 void swf_ApplyMatrixToShape(SHAPE2*shape, MATRIX*m)
379 SHAPELINE*line = shape->lines;
382 p.x = line->x; p.y = line->y;
383 p = swf_TurnPoint(p, m);
384 line->x = p.x; line->y = p.y;
389 static void renderCharacter(render_t*r, placement_t*p, character_t*c)
391 if(c->type == TYPE_SHAPE) {
393 swf_ParseDefineShape(c->tag, &shape);
394 swf_ApplyMatrixToShape(&shape, &p->po.matrix);
395 SHAPELINE*line = shape.lines;
397 for(t=1;t<=shape.numfillstyles;t++) {
399 line = swfline_to_gfxline(shape.lines, -1, t);
401 if(!p->po.clipdepth) {
402 renderFilled(r, line, &shape.fillstyles[t-1], &p->po.cxform);
404 r->device->startclip(r->device, line);
409 /*line = swfline_to_gfxline(shape.lines, -1, -1, t);
410 if(line) renderFilled(r, line, &shape.fillstyles[t-1], &p->po.cxform);
411 gfxline_free(line);*/
413 for(t=1;t<=shape.numlinestyles;t++) {
414 gfxline_t*line = swfline_to_gfxline(shape.lines, t, -1);
415 if(line) renderOutline(r, line, &shape.linestyles[t-1], &p->po.cxform);
423 static void placeObject(void*self, int id, void*data)
425 render_t*r = (render_t*)self;
426 placement_t*p = (placement_t*)data;
427 character_t*c = map16_get_id(r->id2char, p->po.id);
429 fprintf(stderr, "Error: ID %d unknown\n", p->po.id);
431 if(c->type == TYPE_SPRITE) {
432 int oldclip = r->clips;
433 sprite_t* s = (sprite_t*)c->data;
434 map16_t* depths = extractFrame(c->tag, p->age % s->frameCount);
435 map16_enumerate(depths, placeObject, r);
436 stopClippings(oldclip, r);
439 renderCharacter(r, p, c);
442 void swfpage_destroy(gfxpage_t*swf_page)
444 swf_page_internal_t*i= (swf_page_internal_t*)swf_page->internal;
445 free(swf_page->internal);swf_page->internal = 0;
446 free(swf_page);swf_page=0;
449 void swfpage_render(gfxpage_t*page, gfxdevice_t*output)
451 swf_page_internal_t*i = (swf_page_internal_t*)page->internal;
452 swf_doc_internal_t*pi = (swf_doc_internal_t*)page->parent->internal;
453 map16_t* depths = extractFrame(pi->swf.firstTag, i->frame);
455 r.id2char = pi->id2char;
458 map16_enumerate(depths, placeObject, &r);
461 void swfpage_rendersection(gfxpage_t*page, gfxdevice_t*output, gfxcoord_t x, gfxcoord_t y, gfxcoord_t _x1, gfxcoord_t _y1, gfxcoord_t _x2, gfxcoord_t _y2)
463 swf_doc_internal_t*pi = (swf_doc_internal_t*)page->parent->internal;
465 swfpage_render(page,output);
468 void swf_doc_destroy(gfxdocument_t*gfx)
470 swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal;
471 swf_FreeTags(&i->swf);
472 free(gfx->internal);gfx->internal=0;
476 void swf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
478 swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal;
481 gfxpage_t* swf_doc_getpage(gfxdocument_t*doc, int page)
483 swf_doc_internal_t*di= (swf_doc_internal_t*)doc->internal;
484 if(page < 1 || page > doc->num_pages)
487 gfxpage_t* swf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
488 swf_page_internal_t*pi= (swf_page_internal_t*)malloc(sizeof(swf_page_internal_t));
489 memset(pi, 0, sizeof(swf_page_internal_t));
493 swf_page->internal = pi;
494 swf_page->destroy = swfpage_destroy;
495 swf_page->render = swfpage_render;
496 swf_page->rendersection = swfpage_rendersection;
497 swf_page->width = di->width;
498 swf_page->height = di->height;
499 swf_page->parent = doc;
504 void swf_set_parameter(char*name, char*value)
506 msg("<verbose> setting parameter %s to \"%s\"", name, value);
509 gfxdocument_t*swf_open(char*filename)
511 gfxdocument_t*swf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
512 memset(swf_doc, 0, sizeof(gfxdocument_t));
513 swf_doc_internal_t*i= (swf_doc_internal_t*)malloc(sizeof(swf_doc_internal_t));
514 memset(i, 0, sizeof(swf_doc_internal_t));
526 f = open(filename,O_RDONLY|O_BINARY);
528 perror("Couldn't open file: ");
531 if FAILED(swf_ReadSWF(f,&swf)) {
532 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
538 i->id2char = extractDefinitions(&swf);
539 i->width = (swf.movieSize.xmax - swf.movieSize.xmin) / 20;
540 i->height = (swf.movieSize.ymax - swf.movieSize.ymin) / 20;
542 swf_doc->num_pages = swf.frameCount;
543 swf_doc->internal = i;
545 swf_doc->destroy = swf_doc_destroy;
546 swf_doc->set_parameter = swf_doc_set_parameter;
547 swf_doc->getpage = swf_doc_getpage;
552 gfxsource_t*gfxsource_swf_create()
554 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
555 memset(src, 0, sizeof(gfxsource_t));
556 src->set_parameter = swf_set_parameter;
557 src->open = swf_open;