2 #include "../gfxdevice.h"
3 #include "../gfxsource.h"
4 #include "../gfxtools.h"
11 typedef struct _map16_t
16 typedef struct _swf_page_internal
19 } swf_page_internal_t;
21 typedef struct _swf_doc_internal
35 typedef struct _character
43 typedef struct _placement
50 typedef struct _sprite
63 typedef struct _render render_t;
66 static void placement_free(placement_t*p)
68 swf_PlaceObjectFree(&p->po);
72 //---- object/depth handling ----
76 map16_t*map = rfx_calloc(sizeof(map16_t));
77 /* TODO: replace this by a more sophisticated data structure */
78 map->ids = (void**)rfx_calloc(sizeof(character_t)*65536);
81 character_t*map16_get_id(map16_t*map, int id)
87 void map16_free(map16_t*map)
91 void map16_add_id(map16_t*map, int nr, void*id)
94 fprintf(stderr, "Warning: ID %d defined more than once\n");
97 void map16_remove_id(map16_t*map, int nr)
101 void map16_enumerate(map16_t*map, void (*f)(void*self, int id, void*data), void*self)
104 for(t=0;t<65536;t++) {
106 f(self, t, map->ids[t]);
110 //---- conversion stuff ----
112 static void convertMatrix(MATRIX*from, gfxmatrix_t*to)
114 to->m00 = from->sx / 65536.0; to->m10 = from->r1 / 65536.0;
115 to->m01 = from->r0 / 65536.0; to->m11 = from->sy / 65536.0;
116 to->tx = from->tx/20.0;
117 to->ty = from->ty/20.0;
120 static void convertCXForm(CXFORM*from, gfxcxform_t*to)
122 memset(to, 0, sizeof(gfxcxform_t));
123 to->aa = from->a0 / 256.0;
124 to->rr = from->r0 / 256.0;
125 to->gg = from->g0 / 256.0;
126 to->bb = from->b0 / 256.0;
133 static gfxgradient_t* convertGradient(GRADIENT*from)
135 gfxgradient_t*g = rfx_calloc(from->num * sizeof(gfxgradient_t));
137 for(t=0;t<from->num;t++) {
138 g[t].pos = from->ratios[t] / 255.0;
139 g[t].color = *(gfxcolor_t*)&from->rgba[t];
148 gfxline_t* swfline_to_gfxline(SHAPELINE*line, int linestyle, int fillstyle0)
153 gfxdrawer_target_gfxline(&d);
154 if(line && line->type != moveTo) {
155 fprintf(stderr, "Warning: Shape doesn't start with a moveTo\n");
158 if(line->fillstyle0 == fillstyle0 || line->fillstyle1 == fillstyle0 ||
159 line->linestyle == linestyle) {
160 if(line->type == lineTo) {
161 d.moveTo(&d, x/20.0,y/20.0);
162 d.lineTo(&d, line->x/20.0,line->y/20.0);
163 } else if(line->type == splineTo) {
164 d.moveTo(&d, x/20.0,y/20.0);
165 d.splineTo(&d, line->sx/20.0, line->sy/20.0, line->x/20.0,line->y/20.0);
177 //---- bitmap handling ----
179 gfximage_t* gfximage_new(RGBA*data, int width, int height)
181 gfximage_t* b = (gfximage_t*)rfx_calloc(sizeof(gfximage_t));
182 b->data = (gfxcolor_t*)data;
188 void gfximage_free(gfximage_t*b)
195 static gfximage_t* findimage(render_t*r, U16 id)
197 character_t*c = (character_t*)map16_get_id(r->id2char, id);
198 assert(c && c->type == TYPE_BITMAP);
199 gfximage_t*img = (gfximage_t*)c->data;
202 sprintf(filename, "bitmap%d.png", id);
203 writePNG(filename, (unsigned char*)img->data, img->width, img->height);
204 printf("saving bitmap %d to %s\n", id, filename);*/
208 //---- shape handling ----
210 static void renderFilled(render_t*r, gfxline_t*line, FILLSTYLE*f, CXFORM*cx, MATRIX*po_m)
212 if(f->type == FILL_SOLID) {
213 gfxcolor_t c = *(gfxcolor_t*)&f->color;
214 r->device->fill(r->device, line, &c);
215 } else if(f->type == FILL_TILED || f->type == FILL_CLIPPED) {
216 gfximage_t* img = findimage(r, f->id_bitmap);
219 convertCXForm(cx, &gfxcx);
221 swf_MatrixJoin(&m2, po_m, &f->m);
222 convertMatrix(&m2, &m);
223 m.m00/=20.0; m.m10/=20.0;
224 m.m01/=20.0; m.m11/=20.0;
225 /* TODO: handle clipped */
226 r->device->fillbitmap(r->device, line, img, &m, &gfxcx);
227 } else if(f->type == FILL_LINEAR || f->type == FILL_RADIAL) {
230 convertMatrix(&f->m, &m);
231 g = convertGradient(&f->gradient);
232 r->device->fillgradient(r->device, line, g, f->type == FILL_LINEAR ? gfxgradient_linear : gfxgradient_radial, &m);
237 //---- font handling ----
245 typedef struct textcallbackblock
249 } textcallbackblock_t;
251 static void textcallback(void*self, int*chars, int*xpos, int nr, int fontid, int fontsize,
252 int xstart, int ystart, RGBA* color)
254 textcallbackblock_t * info = (textcallbackblock_t*)self;
257 character_t*cfont = map16_get_id(info->r->id2char, fontid);
259 fprintf(stderr, "Font %d unknown\n", fontid);
262 if(cfont->type != TYPE_FONT) {
263 fprintf(stderr, "ID %d is not a font\n", fontid);
269 int x = xstart + xpos[t];
275 p = swf_TurnPoint(p, &m);
277 m.sx = (m.sx * fontsize) / 1024;
278 m.sy = (m.sy * fontsize) / 1024;
279 m.r0 = (m.r0 * fontsize) / 1024;
280 m.r1 = (m.r1 * fontsize) / 1024;
285 convertMatrix(&m, &gm);
287 if(chars[t]<0 || chars[t]>= font->numchars) {
288 fprintf(stderr, "Character out of range: %d\n", chars[t]);
290 gfxline_t*line = gfxline_clone(font->glyphs[chars[t]]);
291 gfxline_transform(line, &gm);
295 renderFilled(info->r, line, &f, 0, 0);
302 //---- tag handling ----
304 static map16_t* extractDefinitions(SWF*swf)
306 map16_t*map = map16_new();
307 TAG*tag = swf->firstTag;
311 if(swf_isDefiningTag(tag)) {
312 id = swf_GetDefineID(tag);
315 if(tag->id == ST_DEFINESPRITE) {
316 character_t*c = rfx_calloc(sizeof(character_t));
317 sprite_t*s = rfx_calloc(sizeof(sprite_t));
318 swf_SetTagPos(tag, 0);
319 swf_GetU16(tag); //id
320 s->frameCount = swf_GetU16(tag); //frameno
322 c->type = TYPE_SPRITE;
324 map16_add_id(map, id, c);
326 else if(tag->id == ST_DEFINESHAPE ||
327 tag->id == ST_DEFINESHAPE2 ||
328 tag->id == ST_DEFINESHAPE3) {
329 character_t*c = rfx_calloc(sizeof(character_t));
331 c->type = TYPE_SHAPE;
332 map16_add_id(map, id, c);
334 else if(tag->id == ST_DEFINEFONT ||
335 tag->id == ST_DEFINEFONT2) {
336 character_t*c = rfx_calloc(sizeof(character_t));
338 font_t*font = (font_t*)rfx_calloc(sizeof(font_t));
339 swf_FontExtract(swf, id, &swffont);
340 font->numchars = swffont->numchars;
341 font->glyphs = (gfxline_t**)rfx_calloc(sizeof(gfxline_t*)*font->numchars);
343 RGBA color_white = {255,255,255,255};
344 for(t=0;t<font->numchars;t++) {
345 if(!swffont->glyph[t].shape->fillstyle.n) {
346 swf_ShapeAddSolidFillStyle(swffont->glyph[t].shape, &color_white);
348 SHAPE2*s2 = swf_ShapeToShape2(swffont->glyph[t].shape);
349 font->glyphs[t] = swfline_to_gfxline(s2->lines, 0, 1);
352 swf_FontFree(swffont);
357 map16_add_id(map, id, c);
359 else if(tag->id == ST_DEFINETEXT ||
360 tag->id == ST_DEFINETEXT2) {
361 character_t*c = rfx_calloc(sizeof(character_t));
365 map16_add_id(map, id, c);
367 else if(tag->id == ST_DEFINEBITSJPEG ||
368 tag->id == ST_DEFINEBITSJPEG2 ||
369 tag->id == ST_DEFINEBITSJPEG3 ||
370 tag->id == ST_DEFINEBITSLOSSLESS ||
371 tag->id == ST_DEFINEBITSLOSSLESS2) {
372 character_t*c = rfx_calloc(sizeof(character_t));
374 void*data = swf_ExtractImage(tag, &width, &height);
375 gfximage_t*b = gfximage_new(data, width, height);
377 c->type = TYPE_BITMAP;
379 map16_add_id(map, id, c);
387 void swf_FreeTaglist(TAG*tag)
391 TAG * tnew = tag->next;
399 static void increaseAge(void*self, int id, void*data)
401 placement_t*p = (placement_t*)data;
405 static map16_t* extractFrame(TAG*startTag, int frame_to_extract)
407 map16_t*depthmap = map16_new();
412 SWF*swf = rfx_calloc(sizeof(SWF));
413 swf->firstTag = startTag;
415 for(;tag;tag = tag->next) {
416 if(tag->id == ST_DEFINESPRITE) {
417 while(tag->id != ST_END)
421 if(tag->id == ST_PLACEOBJECT ||
422 tag->id == ST_PLACEOBJECT2) {
423 placement_t* p = rfx_calloc(sizeof(placement_t));
425 p->startFrame = frame;
426 swf_GetPlaceObject(tag, &p->po);
428 placement_t*old = (placement_t*)map16_get_id(depthmap, p->po.depth);
429 p->po.id = old->po.id;
430 map16_remove_id(depthmap, p->po.depth);
433 map16_add_id(depthmap, p->po.depth, p);
436 if(tag->id == ST_REMOVEOBJECT ||
437 tag->id == ST_REMOVEOBJECT2) {
438 U16 depth = swf_GetDepth(tag);
439 map16_remove_id(depthmap, depth);
441 if(tag->id == ST_SHOWFRAME || tag->id == ST_END || !tag->next) {
442 if(frame == frame_to_extract) {
445 if(tag->id == ST_SHOWFRAME) {
447 map16_enumerate(depthmap, increaseAge, 0);
450 if(tag->id == ST_END)
456 // ---- rendering ----
458 void swf_ShapeApplyMatrix(SHAPE2*shape, MATRIX*m)
462 RGBA swf_ColorTransform(RGBA*color, CXFORM*cx)
465 dest.r = (cx->r0*color->r + cx->r1*256) >> 8;
466 dest.g = (cx->g0*color->g + cx->g1*256) >> 8;
467 dest.b = (cx->b0*color->b + cx->b1*256) >> 8;
468 dest.a = (cx->a0*color->a + cx->a1*256) >> 8;
472 void renderOutline(render_t*r, gfxline_t*line, LINESTYLE*l, CXFORM*cx)
474 RGBA c = swf_ColorTransform(&l->color, cx);
475 gfxcoord_t width = l->width/20.0;
476 r->device->stroke(r->device, line, width, (gfxcolor_t*)&c, gfx_capRound, gfx_joinRound, 0.0);
479 void swf_ApplyMatrixToShape(SHAPE2*shape, MATRIX*m)
481 SHAPELINE*line = shape->lines;
484 p.x = line->x; p.y = line->y;
485 p = swf_TurnPoint(p, m);
486 line->x = p.x; line->y = p.y;
491 static void renderCharacter(render_t*r, placement_t*p, character_t*c)
493 if(c->type == TYPE_SHAPE) {
495 swf_ParseDefineShape(c->tag, &shape);
497 swf_MatrixJoin(&m, &r->m, &p->po.matrix);
498 swf_ApplyMatrixToShape(&shape, &m);
499 SHAPELINE*line = shape.lines;
501 for(t=1;t<=shape.numfillstyles;t++) {
503 line = swfline_to_gfxline(shape.lines, -1, t);
505 if(!p->po.clipdepth) {
506 renderFilled(r, line, &shape.fillstyles[t-1], &p->po.cxform, &p->po.matrix);
508 r->device->startclip(r->device, line);
509 r->clips_waiting[p->po.clipdepth]++;
513 /*line = swfline_to_gfxline(shape.lines, -1, -1, t);
514 if(line) renderFilled(r, line, &shape.fillstyles[t-1], &p->po.cxform);
515 gfxline_free(line);*/
517 for(t=1;t<=shape.numlinestyles;t++) {
518 gfxline_t*line = swfline_to_gfxline(shape.lines, t, -1);
519 if(line) renderOutline(r, line, &shape.linestyles[t-1], &p->po.cxform);
522 } else if(c->type == TYPE_TEXT) {
524 textcallbackblock_t info;
526 swf_SetTagPos(tag, 0);
529 swf_GetMatrix(tag,&mt);
531 swf_MatrixJoin(&mt2, &r->m, &mt);
532 swf_MatrixJoin(&info.m, &mt2, &p->po.matrix);
534 swf_ParseDefineText(tag, textcallback, &info);
540 static void placeObject(void*self, int id, void*data)
542 render_t*r = (render_t*)self;
543 placement_t*p = (placement_t*)data;
544 character_t*c = map16_get_id(r->id2char, p->po.id);
546 fprintf(stderr, "Error: ID %d unknown\n", p->po.id);
549 if(c->type == TYPE_SPRITE) {
550 int*old_clips_waiting = r->clips_waiting;
551 r->clips_waiting = rfx_calloc(sizeof(r->clips_waiting[0])*65536);
553 sprite_t* s = (sprite_t*)c->data;
555 map16_t* depths = extractFrame(c->tag->next, p->age % s->frameCount);
556 map16_enumerate(depths, placeObject, r);
559 for(t=0;t<65536;t++) {
561 for(i=0; i<r->clips_waiting[t]; i++) {
562 r->device->endclip(r->device);
565 free(r->clips_waiting);
566 r->clips_waiting = old_clips_waiting;
569 renderCharacter(r, p, c);
572 void swfpage_destroy(gfxpage_t*swf_page)
574 swf_page_internal_t*i= (swf_page_internal_t*)swf_page->internal;
575 free(swf_page->internal);swf_page->internal = 0;
576 free(swf_page);swf_page=0;
579 void swfpage_render(gfxpage_t*page, gfxdevice_t*output)
581 swf_page_internal_t*i = (swf_page_internal_t*)page->internal;
582 swf_doc_internal_t*pi = (swf_doc_internal_t*)page->parent->internal;
583 map16_t* depths = extractFrame(pi->swf.firstTag, i->frame);
585 r.id2char = pi->id2char;
589 r.clips_waiting = malloc(sizeof(r.clips_waiting[0])*65536);
590 memset(r.clips_waiting, 0, sizeof(r.clips_waiting[0])*65536);
593 for(t=0;t<65536;t++) {
596 for(i=0; i<r.clips_waiting[t]; i++) {
597 output->endclip(output);
601 placeObject(&r, t, depths->ids[t]);
604 free(r.clips_waiting);
607 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)
609 swf_doc_internal_t*pi = (swf_doc_internal_t*)page->parent->internal;
611 swfpage_render(page,output);
614 void swf_doc_destroy(gfxdocument_t*gfx)
616 swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal;
617 swf_FreeTags(&i->swf);
618 free(gfx->internal);gfx->internal=0;
622 void swf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
624 swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal;
627 gfxpage_t* swf_doc_getpage(gfxdocument_t*doc, int page)
629 swf_doc_internal_t*di= (swf_doc_internal_t*)doc->internal;
630 if(page < 1 || page > doc->num_pages)
633 gfxpage_t* swf_page = (gfxpage_t*)malloc(sizeof(gfxpage_t));
634 swf_page_internal_t*pi= (swf_page_internal_t*)malloc(sizeof(swf_page_internal_t));
635 memset(pi, 0, sizeof(swf_page_internal_t));
639 swf_page->internal = pi;
640 swf_page->destroy = swfpage_destroy;
641 swf_page->render = swfpage_render;
642 swf_page->rendersection = swfpage_rendersection;
643 swf_page->width = di->width;
644 swf_page->height = di->height;
645 swf_page->parent = doc;
650 void swf_set_parameter(gfxsource_t*src, const char*name, const char*value)
652 msg("<verbose> setting parameter %s to \"%s\"", name, value);
655 gfxdocument_t*swf_open(gfxsource_t*src, const char*filename)
657 gfxdocument_t*swf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
658 memset(swf_doc, 0, sizeof(gfxdocument_t));
659 swf_doc_internal_t*i= (swf_doc_internal_t*)malloc(sizeof(swf_doc_internal_t));
660 memset(i, 0, sizeof(swf_doc_internal_t));
671 f = open(filename,O_RDONLY|O_BINARY);
673 perror("Couldn't open file: ");
676 if FAILED(swf_ReadSWF(f,&i->swf)) {
677 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
681 swf_UnFoldAll(&i->swf);
683 i->id2char = extractDefinitions(&i->swf);
684 i->width = (i->swf.movieSize.xmax - i->swf.movieSize.xmin) / 20;
685 i->height = (i->swf.movieSize.ymax - i->swf.movieSize.ymin) / 20;
687 swf_GetMatrix(0, &i->m);
688 i->m.tx = -i->swf.movieSize.xmin;
689 i->m.ty = -i->swf.movieSize.ymin;
691 swf_doc->num_pages = i->swf.frameCount;
692 swf_doc->internal = i;
694 swf_doc->destroy = swf_doc_destroy;
695 swf_doc->set_parameter = swf_doc_set_parameter;
696 swf_doc->getpage = swf_doc_getpage;
701 gfxsource_t*gfxsource_swf_create()
703 gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t));
704 memset(src, 0, sizeof(gfxsource_t));
705 src->set_parameter = swf_set_parameter;
706 src->open = swf_open;