3 #include "../gfxdevice.h"
4 #include "../gfxsource.h"
5 #include "../gfxtools.h"
12 typedef struct _map16_t
17 typedef struct _swf_page_internal
20 } swf_page_internal_t;
22 typedef struct _swf_doc_internal
36 typedef struct _character
44 typedef struct _placement
51 typedef struct _sprite
64 typedef struct _render render_t;
67 static void placement_free(placement_t*p)
69 swf_PlaceObjectFree(&p->po);
73 //---- object/depth handling ----
77 map16_t*map = rfx_calloc(sizeof(map16_t));
78 /* TODO: replace this by a more sophisticated data structure */
79 map->ids = (void**)rfx_calloc(sizeof(character_t)*65536);
82 character_t*map16_get_id(map16_t*map, int id)
88 void map16_free(map16_t*map)
92 void map16_add_id(map16_t*map, int nr, void*id)
95 fprintf(stderr, "Warning: ID %d defined more than once\n");
98 void map16_remove_id(map16_t*map, int nr)
102 void map16_enumerate(map16_t*map, void (*f)(void*self, int id, void*data), void*self)
105 for(t=0;t<65536;t++) {
107 f(self, t, map->ids[t]);
111 //---- conversion stuff ----
113 static void convertMatrix(MATRIX*from, gfxmatrix_t*to)
115 to->m00 = from->sx / 65536.0; to->m10 = from->r1 / 65536.0;
116 to->m01 = from->r0 / 65536.0; to->m11 = from->sy / 65536.0;
117 to->tx = from->tx/20.0;
118 to->ty = from->ty/20.0;
121 static void convertCXForm(CXFORM*from, gfxcxform_t*to)
123 memset(to, 0, sizeof(gfxcxform_t));
124 to->aa = from->a0 / 256.0;
125 to->rr = from->r0 / 256.0;
126 to->gg = from->g0 / 256.0;
127 to->bb = from->b0 / 256.0;
134 static gfxgradient_t* convertGradient(GRADIENT*from)
136 gfxgradient_t*g = rfx_calloc(from->num * sizeof(gfxgradient_t));
138 for(t=0;t<from->num;t++) {
139 g[t].pos = from->ratios[t] / 255.0;
140 g[t].color = *(gfxcolor_t*)&from->rgba[t];
149 gfxline_t* swfline_to_gfxline(SHAPELINE*line, int linestyle, int fillstyle0)
152 SCOORD x=0,y=0,xx=0,yy=0;
154 gfxdrawer_target_gfxline(&d);
155 if(line && line->type != moveTo) {
156 fprintf(stderr, "Warning: Shape doesn't start with a moveTo\n");
158 xx = line?line->x+1:0;
160 if(line->fillstyle0 == fillstyle0 ||
161 line->fillstyle1 == fillstyle0 ||
162 line->linestyle == linestyle) {
163 if(line->type == lineTo) {
164 if(xx!=x || yy!=y) d.moveTo(&d, x/20.0,y/20.0);
165 d.lineTo(&d, line->x/20.0,line->y/20.0);
168 } else if(line->type == splineTo) {
169 if(xx!=x || yy!=y) d.moveTo(&d, x/20.0,y/20.0);
170 d.splineTo(&d, line->sx/20.0, line->sy/20.0, line->x/20.0,line->y/20.0);
184 //---- bitmap handling ----
186 gfximage_t* gfximage_new(RGBA*data, int width, int height)
188 gfximage_t* b = (gfximage_t*)rfx_calloc(sizeof(gfximage_t));
189 b->data = (gfxcolor_t*)data;
195 void gfximage_free(gfximage_t*b)
202 static gfximage_t* findimage(render_t*r, U16 id)
204 character_t*c = (character_t*)map16_get_id(r->id2char, id);
205 assert(c && c->type == TYPE_BITMAP);
206 gfximage_t*img = (gfximage_t*)c->data;
209 sprintf(filename, "bitmap%d.png", id);
210 writePNG(filename, (unsigned char*)img->data, img->width, img->height);
211 printf("saving bitmap %d to %s\n", id, filename);*/
215 //---- shape handling ----
217 static void renderFilled(render_t*r, gfxline_t*line, FILLSTYLE*f, CXFORM*cx, MATRIX*po_m)
219 if(f->type == FILL_SOLID) {
220 gfxcolor_t c = *(gfxcolor_t*)&f->color;
221 r->device->fill(r->device, line, &c);
222 } else if(f->type == FILL_TILED || f->type == FILL_CLIPPED) {
223 gfximage_t* img = findimage(r, f->id_bitmap);
226 convertCXForm(cx, &gfxcx);
228 swf_MatrixJoin(&m2, po_m, &f->m);
229 convertMatrix(&m2, &m);
230 m.m00/=20.0; m.m10/=20.0;
231 m.m01/=20.0; m.m11/=20.0;
232 /* TODO: handle clipped */
233 r->device->fillbitmap(r->device, line, img, &m, &gfxcx);
234 } else if(f->type == FILL_LINEAR || f->type == FILL_RADIAL) {
237 convertMatrix(&f->m, &m);
238 g = convertGradient(&f->gradient);
239 r->device->fillgradient(r->device, line, g, f->type == FILL_LINEAR ? gfxgradient_linear : gfxgradient_radial, &m);
244 //---- font handling ----
252 typedef struct textcallbackblock
256 } textcallbackblock_t;
258 static void textcallback(void*self, int*chars, int*xpos, int nr, int fontid, int fontsize,
259 int xstart, int ystart, RGBA* color)
261 textcallbackblock_t * info = (textcallbackblock_t*)self;
264 character_t*cfont = map16_get_id(info->r->id2char, fontid);
266 fprintf(stderr, "Font %d unknown\n", fontid);
269 if(cfont->type != TYPE_FONT) {
270 fprintf(stderr, "ID %d is not a font\n", fontid);
276 int x = xstart + xpos[t];
282 p = swf_TurnPoint(p, &m);
284 m.sx = (m.sx * fontsize) / 1024;
285 m.sy = (m.sy * fontsize) / 1024;
286 m.r0 = (m.r0 * fontsize) / 1024;
287 m.r1 = (m.r1 * fontsize) / 1024;
292 convertMatrix(&m, &gm);
294 if(chars[t]<0 || chars[t]>= font->numchars) {
295 fprintf(stderr, "Character out of range: %d\n", chars[t]);
297 gfxline_t*line = gfxline_clone(font->glyphs[chars[t]]);
298 gfxline_transform(line, &gm);
302 renderFilled(info->r, line, &f, 0, 0);
309 //---- tag handling ----
311 static map16_t* extractDefinitions(SWF*swf)
313 map16_t*map = map16_new();
314 TAG*tag = swf->firstTag;
318 if(swf_isDefiningTag(tag)) {
319 id = swf_GetDefineID(tag);
322 if(tag->id == ST_DEFINESPRITE) {
323 character_t*c = rfx_calloc(sizeof(character_t));
324 sprite_t*s = rfx_calloc(sizeof(sprite_t));
325 swf_SetTagPos(tag, 0);
326 swf_GetU16(tag); //id
327 s->frameCount = swf_GetU16(tag); //frameno
329 c->type = TYPE_SPRITE;
331 map16_add_id(map, id, c);
333 else if(tag->id == ST_DEFINESHAPE ||
334 tag->id == ST_DEFINESHAPE2 ||
335 tag->id == ST_DEFINESHAPE3) {
336 character_t*c = rfx_calloc(sizeof(character_t));
338 c->type = TYPE_SHAPE;
339 map16_add_id(map, id, c);
341 else if(tag->id == ST_DEFINEFONT ||
342 tag->id == ST_DEFINEFONT2) {
343 character_t*c = rfx_calloc(sizeof(character_t));
345 font_t*font = (font_t*)rfx_calloc(sizeof(font_t));
346 swf_FontExtract(swf, id, &swffont);
347 font->numchars = swffont->numchars;
348 font->glyphs = (gfxline_t**)rfx_calloc(sizeof(gfxline_t*)*font->numchars);
350 RGBA color_white = {255,255,255,255};
351 for(t=0;t<font->numchars;t++) {
352 if(!swffont->glyph[t].shape->fillstyle.n) {
353 swf_ShapeAddSolidFillStyle(swffont->glyph[t].shape, &color_white);
355 SHAPE2*s2 = swf_ShapeToShape2(swffont->glyph[t].shape);
356 font->glyphs[t] = swfline_to_gfxline(s2->lines, 0, 1);
359 swf_FontFree(swffont);
364 map16_add_id(map, id, c);
366 else if(tag->id == ST_DEFINETEXT ||
367 tag->id == ST_DEFINETEXT2) {
368 character_t*c = rfx_calloc(sizeof(character_t));
372 map16_add_id(map, id, c);
374 else if(tag->id == ST_DEFINEBITSJPEG ||
375 tag->id == ST_DEFINEBITSJPEG2 ||
376 tag->id == ST_DEFINEBITSJPEG3 ||
377 tag->id == ST_DEFINEBITSLOSSLESS ||
378 tag->id == ST_DEFINEBITSLOSSLESS2) {
379 character_t*c = rfx_calloc(sizeof(character_t));
381 void*data = swf_ExtractImage(tag, &width, &height);
382 gfximage_t*b = gfximage_new(data, width, height);
384 c->type = TYPE_BITMAP;
386 map16_add_id(map, id, c);
394 void swf_FreeTaglist(TAG*tag)
398 TAG * tnew = tag->next;
406 static void increaseAge(void*self, int id, void*data)
408 placement_t*p = (placement_t*)data;
412 static map16_t* extractFrame(TAG*startTag, int frame_to_extract)
414 map16_t*depthmap = map16_new();
419 SWF*swf = rfx_calloc(sizeof(SWF));
420 swf->firstTag = startTag;
422 for(;tag;tag = tag->next) {
423 if(tag->id == ST_DEFINESPRITE) {
424 while(tag->id != ST_END)
428 if(tag->id == ST_PLACEOBJECT ||
429 tag->id == ST_PLACEOBJECT2) {
430 placement_t* p = rfx_calloc(sizeof(placement_t));
432 p->startFrame = frame;
433 swf_GetPlaceObject(tag, &p->po);
435 placement_t*old = (placement_t*)map16_get_id(depthmap, p->po.depth);
436 p->po.id = old->po.id;
437 map16_remove_id(depthmap, p->po.depth);
440 map16_add_id(depthmap, p->po.depth, p);
443 if(tag->id == ST_REMOVEOBJECT ||
444 tag->id == ST_REMOVEOBJECT2) {
445 U16 depth = swf_GetDepth(tag);
446 map16_remove_id(depthmap, depth);
448 if(tag->id == ST_SHOWFRAME || tag->id == ST_END || !tag->next) {
449 if(frame == frame_to_extract) {
452 if(tag->id == ST_SHOWFRAME) {
454 map16_enumerate(depthmap, increaseAge, 0);
457 if(tag->id == ST_END)
463 // ---- rendering ----
465 void swf_ShapeApplyMatrix(SHAPE2*shape, MATRIX*m)
469 RGBA swf_ColorTransform(RGBA*color, CXFORM*cx)
472 dest.r = (cx->r0*color->r + cx->r1*256) >> 8;
473 dest.g = (cx->g0*color->g + cx->g1*256) >> 8;
474 dest.b = (cx->b0*color->b + cx->b1*256) >> 8;
475 dest.a = (cx->a0*color->a + cx->a1*256) >> 8;
479 void renderOutline(render_t*r, gfxline_t*line, LINESTYLE*l, CXFORM*cx)
481 RGBA c = swf_ColorTransform(&l->color, cx);
482 gfxcoord_t width = l->width/20.0;
483 r->device->stroke(r->device, line, width, (gfxcolor_t*)&c, gfx_capRound, gfx_joinRound, 0.0);
486 void swf_ApplyMatrixToShape(SHAPE2*shape, MATRIX*m)
488 SHAPELINE*line = shape->lines;
491 p.x = line->x; p.y = line->y;
492 p = swf_TurnPoint(p, m);
493 line->x = p.x; line->y = p.y;
498 static void renderCharacter(render_t*r, placement_t*p, character_t*c)
500 if(c->type == TYPE_SHAPE) {
502 swf_ParseDefineShape(c->tag, &shape);
504 swf_MatrixJoin(&m, &r->m, &p->po.matrix);
505 swf_ApplyMatrixToShape(&shape, &m);
506 SHAPELINE*line = shape.lines;
508 for(t=1;t<=shape.numfillstyles;t++) {
510 line = swfline_to_gfxline(shape.lines, -1, t);
512 if(!p->po.clipdepth) {
513 renderFilled(r, line, &shape.fillstyles[t-1], &p->po.cxform, &p->po.matrix);
515 r->device->startclip(r->device, line);
516 r->clips_waiting[p->po.clipdepth]++;
520 /*line = swfline_to_gfxline(shape.lines, -1, -1, t);
521 if(line) renderFilled(r, line, &shape.fillstyles[t-1], &p->po.cxform);
522 gfxline_free(line);*/
524 for(t=1;t<=shape.numlinestyles;t++) {
525 gfxline_t*line = swfline_to_gfxline(shape.lines, t, -1);
526 if(line) renderOutline(r, line, &shape.linestyles[t-1], &p->po.cxform);
529 } else if(c->type == TYPE_TEXT) {
531 textcallbackblock_t info;
533 swf_SetTagPos(tag, 0);
536 swf_GetMatrix(tag,&mt);
538 swf_MatrixJoin(&mt2, &r->m, &mt);
539 swf_MatrixJoin(&info.m, &mt2, &p->po.matrix);
541 swf_ParseDefineText(tag, textcallback, &info);
547 static void placeObject(void*self, int id, void*data)
549 render_t*r = (render_t*)self;
550 placement_t*p = (placement_t*)data;
551 character_t*c = map16_get_id(r->id2char, p->po.id);
553 fprintf(stderr, "Error: ID %d unknown\n", p->po.id);
556 if(c->type == TYPE_SPRITE) {
557 int*old_clips_waiting = r->clips_waiting;
558 r->clips_waiting = rfx_calloc(sizeof(r->clips_waiting[0])*65536);
560 sprite_t* s = (sprite_t*)c->data;
562 map16_t* depths = extractFrame(c->tag->next, p->age % s->frameCount);
563 map16_enumerate(depths, placeObject, r);
566 for(t=0;t<65536;t++) {
568 for(i=0; i<r->clips_waiting[t]; i++) {
569 r->device->endclip(r->device);
572 free(r->clips_waiting);
573 r->clips_waiting = old_clips_waiting;
576 renderCharacter(r, p, c);
579 void swfpage_destroy(gfxpage_t*swf_page)
581 swf_page_internal_t*i= (swf_page_internal_t*)swf_page->internal;
582 free(swf_page->internal);swf_page->internal = 0;
583 free(swf_page);swf_page=0;
586 void swfpage_render(gfxpage_t*page, gfxdevice_t*output)
588 swf_page_internal_t*i = (swf_page_internal_t*)page->internal;
589 swf_doc_internal_t*pi = (swf_doc_internal_t*)page->parent->internal;
590 map16_t* depths = extractFrame(pi->swf.firstTag, i->frame);
592 r.id2char = pi->id2char;
596 r.clips_waiting = malloc(sizeof(r.clips_waiting[0])*65536);
597 memset(r.clips_waiting, 0, sizeof(r.clips_waiting[0])*65536);
600 for(t=0;t<65536;t++) {
603 for(i=0; i<r.clips_waiting[t]; i++) {
604 output->endclip(output);
608 placeObject(&r, t, depths->ids[t]);
611 free(r.clips_waiting);
614 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)
616 swf_doc_internal_t*pi = (swf_doc_internal_t*)page->parent->internal;
618 swfpage_render(page,output);
621 void swf_doc_destroy(gfxdocument_t*gfx)
623 swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal;
624 swf_FreeTags(&i->swf);
625 free(gfx->internal);gfx->internal=0;
629 void swf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
631 swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal;
634 gfxpage_t* swf_doc_getpage(gfxdocument_t*doc, int page)
636 swf_doc_internal_t*di= (swf_doc_internal_t*)doc->internal;
637 if(page < 1 || page > doc->num_pages)
640 gfxpage_t* swf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
641 swf_page_internal_t*pi= (swf_page_internal_t*)malloc(sizeof(swf_page_internal_t));
642 memset(pi, 0, sizeof(swf_page_internal_t));
646 swf_page->internal = pi;
647 swf_page->destroy = swfpage_destroy;
648 swf_page->render = swfpage_render;
649 swf_page->rendersection = swfpage_rendersection;
650 swf_page->width = di->width;
651 swf_page->height = di->height;
652 swf_page->parent = doc;
657 void swf_set_parameter(gfxsource_t*src, const char*name, const char*value)
659 msg("<verbose> setting parameter %s to \"%s\"", name, value);
662 gfxdocument_t*swf_open(gfxsource_t*src, const char*filename)
664 gfxdocument_t*swf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
665 memset(swf_doc, 0, sizeof(gfxdocument_t));
666 swf_doc_internal_t*i= (swf_doc_internal_t*)malloc(sizeof(swf_doc_internal_t));
667 memset(i, 0, sizeof(swf_doc_internal_t));
678 f = open(filename,O_RDONLY|O_BINARY);
680 perror("Couldn't open file: ");
683 if FAILED(swf_ReadSWF(f,&i->swf)) {
684 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
688 swf_UnFoldAll(&i->swf);
690 i->id2char = extractDefinitions(&i->swf);
691 i->width = (i->swf.movieSize.xmax - i->swf.movieSize.xmin) / 20;
692 i->height = (i->swf.movieSize.ymax - i->swf.movieSize.ymin) / 20;
694 swf_GetMatrix(0, &i->m);
695 i->m.tx = -i->swf.movieSize.xmin;
696 i->m.ty = -i->swf.movieSize.ymin;
698 swf_doc->num_pages = i->swf.frameCount;
699 swf_doc->internal = i;
701 swf_doc->destroy = swf_doc_destroy;
702 swf_doc->set_parameter = swf_doc_set_parameter;
703 swf_doc->getpage = swf_doc_getpage;
708 static void swf_destroy(gfxsource_t*src)
710 memset(src, 0, sizeof(*src));
715 gfxsource_t*gfxsource_swf_create()
717 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
718 memset(src, 0, sizeof(gfxsource_t));
719 src->set_parameter = swf_set_parameter;
720 src->open = swf_open;
721 src->destroy = swf_destroy;