+static int add_image(swfoutput_internal*i, gfximage_t*img, int targetwidth, int targetheight, int* newwidth, int* newheight)
+{
+ gfxdevice_t*dev = i->dev;
+ RGBA*newpic = 0;
+ RGBA*mem = (RGBA*)img->data;
+
+ int sizex = img->width;
+ int sizey = img->height;
+ int is_jpeg = i->jpeg;
+ i->jpeg = 0;
+
+ int newsizex=sizex, newsizey=sizey;
+
+ /// {
+ if(is_jpeg && i->config_jpegsubpixels) {
+ newsizex = (int)(targetwidth*i->config_jpegsubpixels+0.5);
+ newsizey = (int)(targetheight*i->config_jpegsubpixels+0.5);
+ } else if(!is_jpeg && i->config_ppmsubpixels) {
+ newsizex = (int)(targetwidth*i->config_ppmsubpixels+0.5);
+ newsizey = (int)(targetheight*i->config_ppmsubpixels+0.5);
+ }
+ /// }
+
+ if(sizex<=0 || sizey<=0 || newsizex<=0 || newsizey<=0)
+ return -1;
+
+ /* TODO: cache images */
+
+ if(newsizex<sizex || newsizey<sizey) {
+ msg("<verbose> Scaling %dx%d image to %dx%d", sizex, sizey, newsizex, newsizey);
+ newpic = swf_ImageScale(mem, sizex, sizey, newsizex, newsizey);
+ *newwidth = sizex = newsizex;
+ *newheight = sizey = newsizey;
+ mem = newpic;
+ } else {
+ *newwidth = newsizex = sizex;
+ *newheight = newsizey = sizey;
+ }
+
+ int num_colors = swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,0);
+ int has_alpha = swf_ImageHasAlpha(mem,sizex,sizey);
+
+ msg("<verbose> Drawing %dx%d %s%simage at size %dx%d (%dx%d), %s%d colors",
+ sizex, sizey,
+ has_alpha?(has_alpha==2?"semi-transparent ":"transparent "):"",
+ is_jpeg?"jpeg-":"",
+ newsizex, newsizey,
+ targetwidth, targetheight,
+ /*newsizex, newsizey,*/
+ num_colors>256?">":"", num_colors>256?256:num_colors);
+
+ /*RGBA* pal = (RGBA*)rfx_alloc(sizeof(RGBA)*num_colors);
+ swf_ImageGetNumberOfPaletteEntries(mem,sizex,sizey,pal);
+ int t;
+ for(t=0;t<num_colors;t++) {
+ printf("%02x%02x%02x%02x ",
+ pal[t].r, pal[t].g, pal[t].b, pal[t].a);
+ if((t&7)==7)
+ printf("\n");
+ }
+ printf("\n");*/
+
+ int bitid = -1;
+ int cacheid = imageInCache(dev, mem, sizex, sizey);
+
+ if(cacheid<=0) {
+ bitid = getNewID(dev);
+ i->tag = swf_AddImage(i->tag, bitid, mem, sizex, sizey, i->config_jpegquality);
+ addImageToCache(dev, mem, sizex, sizey);
+ } else {
+ bitid = cacheid;
+ }
+
+ if(newpic)
+ free(newpic);
+ return bitid;
+}
+
+static SRECT gfxline_getSWFbbox(gfxline_t*line)
+{
+ gfxbbox_t bbox = gfxline_getbbox(line);
+ SRECT r;
+ r.xmin = (int)(bbox.xmin*20);
+ r.ymin = (int)(bbox.ymin*20);
+ r.xmax = (int)(bbox.xmax*20);
+ r.ymax = (int)(bbox.ymax*20);
+ return r;
+}
+
+static void swf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
+{
+ swfoutput_internal*i = (swfoutput_internal*)dev->internal;
+
+ endshape(dev);
+ endtext(dev);
+
+ int targetx = (int)(sqrt(matrix->m00*matrix->m00 + matrix->m01*matrix->m01)*img->width);
+ int targety = (int)(sqrt(matrix->m10*matrix->m10 + matrix->m11*matrix->m11)*img->height);
+
+ int newwidth=0,newheight=0;
+ int bitid = add_image(i, img, targetx, targety, &newwidth, &newheight);
+ if(bitid<0)
+ return;
+ double fx = (double)img->width / (double)newwidth;
+ double fy = (double)img->height / (double)newheight;