9 void gfximage_save_jpeg(gfximage_t*img, const char*filename, int quality)
12 int l = img->width*img->height;
13 unsigned char*data = (unsigned char*)rfx_alloc(img->width*img->height*3);
15 for(t=0,s=0;t<l;s+=3,t++) {
16 data[s+0] = img->data[t].r;
17 data[s+1] = img->data[t].g;
18 data[s+2] = img->data[t].b;
20 jpeg_save(data, img->width, img->height, quality, filename);
23 void gfximage_save_png(gfximage_t*image, const char*filename)
25 writePNG(filename, (void*)image->data, image->width, image->height);
28 typedef struct scale_lookup {
33 typedef struct rgba_int {
37 static int bicubic = 0;
39 static scale_lookup_t**make_scale_lookup(int width, int newwidth)
41 scale_lookup_t*lookupx = (scale_lookup_t*)rfx_alloc((width>newwidth?width:newwidth)*2*sizeof(scale_lookup_t));
42 scale_lookup_t**lblockx = (scale_lookup_t**)rfx_alloc((newwidth+1)*sizeof(scale_lookup_t**));
43 double fx = ((double)width)/((double)newwidth);
46 scale_lookup_t*p_x = lookupx;
49 for(x=0;x<newwidth;x++) {
53 double rem = fromx+1-px;
54 int i = (int)(256/fx);
55 int xweight = (int)(rem*256/fx);
59 if(tox>=width) tox = width-1;
60 for(xx=fromx;xx<=tox;xx++) {
61 if(xx==fromx && xx==tox) p_x->weight = 256;
62 else if(xx==fromx) p_x->weight = xweight;
63 else if(xx==tox) p_x->weight = 256-w;
72 for(x=0;x<newwidth;x++) {
74 int ix2 = ((int)px)+1;
76 if(ix2>=width) ix2=width-1;
80 p_x[0].weight = (int)(256*(1-r));
82 p_x[1].weight = 256-p_x[0].weight;
88 lblockx[newwidth] = p_x;
92 static void encodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
95 int len = width*height;
97 U32* img = (U32*)data;
101 if(img[t] != color1) {
106 *(U32*)&colors[0] = color1;
107 *(U32*)&colors[1] = color2;
109 if(img[t] == color1) {
117 static void decodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
120 int len = width*height;
124 data[t].r = (colors[0].r * (255-m) + colors[1].r * m) >> 8;
125 data[t].g = (colors[0].g * (255-m) + colors[1].g * m) >> 8;
126 data[t].b = (colors[0].b * (255-m) + colors[1].b * m) >> 8;
127 data[t].a = (colors[0].a * (255-m) + colors[1].a * m) >> 8;
131 void blurImage(gfxcolor_t*src, int width, int height, int r) __attribute__ ((noinline));
133 void blurImage(gfxcolor_t*src, int width, int height, int r)
135 int e = 2; // r times e is the sampling interval
136 double*gauss = (double*)rfx_alloc(r*e*sizeof(double));
140 double t = (x - r*e/2.0)/r;
141 gauss[x] = exp(-0.5*t*t);
144 int*weights = (int*)rfx_alloc(r*e*sizeof(int));
146 weights[x] = (int)(gauss[x]*65536.0001/sum);
150 gfxcolor_t*tmp = rfx_alloc(sizeof(gfxcolor_t)*width*height);
153 for(y=0;y<height;y++) {
154 gfxcolor_t*s = &src[y*width];
155 gfxcolor_t*d = &tmp[y*width];
156 for(x=0;x<range;x++) {
159 for(x=range;x<width-range;x++) {
166 for(xx=x-range;xx<x+range;xx++) {
178 for(x=width-range;x<width;x++) {
183 for(x=0;x<width;x++) {
184 gfxcolor_t*s = &tmp[x];
185 gfxcolor_t*d = &src[x];
187 for(y=0;y<range;y++) {
191 for(y=range;y<height-range;y++) {
197 int cy,cyy=yy-range*width;
198 for(cy=y-range;cy<y+range;cy++) {
199 r += s[cyy].r * f[0];
200 g += s[cyy].g * f[0];
201 b += s[cyy].b * f[0];
202 a += s[cyy].a * f[0];
212 for(y=0;y<range;y++) {
223 int swf_ImageGetNumberOfPaletteEntries2(gfxcolor_t*_img, int width, int height)
225 int len = width*height;
227 U32* img = (U32*)_img;
231 if(img[t] != color1) {
240 if(img[t] != color1 && img[t] != color2) {
247 gfximage_t* gfximage_rescale(gfximage_t*image, int newwidth, int newheight)
251 scale_lookup_t *p, **lblockx,**lblocky;
254 gfxcolor_t monochrome_colors[2];
256 if(newwidth<1 || newheight<1)
259 int width = image->width;
260 int height = image->height;
261 gfxcolor_t*data = image->data;
263 if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2) {
265 encodeMonochromeImage(data, width, height, monochrome_colors);
266 int r1 = width / newwidth;
267 int r2 = height / newheight;
270 /* high-resolution monochrome images are usually dithered, so
271 low-pass filter them first to get rid of any moire patterns */
272 blurImage(data, width, height, r+1);
276 tmpline = (rgba_int_t*)rfx_alloc(width*sizeof(rgba_int_t));
277 newdata = (gfxcolor_t*)rfx_alloc(newwidth*newheight*sizeof(gfxcolor_t));
279 lblockx = make_scale_lookup(width, newwidth);
280 lblocky = make_scale_lookup(height, newheight);
282 for(p=lblocky[0];p<lblocky[newheight];p++)
285 for(y=0;y<newheight;y++) {
286 gfxcolor_t*destline = &newdata[y*newwidth];
288 /* create lookup table for y */
289 rgba_int_t*l = tmpline;
290 scale_lookup_t*p_y,*p_x;
291 memset(tmpline, 0, width*sizeof(rgba_int_t));
292 for(p_y=lblocky[y];p_y<lblocky[y+1];p_y++) {
293 gfxcolor_t*line = &data[p_y->pos];
295 int weight = p_y->weight;
296 for(x=0;x<width;x++) {
297 tmpline[x].r += line[x].r*weight;
298 tmpline[x].g += line[x].g*weight;
299 tmpline[x].b += line[x].b*weight;
300 tmpline[x].a += line[x].a*weight;
304 /* process x direction */
306 for(x=0;x<newwidth;x++) {
307 unsigned int r=0,g=0,b=0,a=0;
308 scale_lookup_t*p_x_to = lblockx[x+1];
310 rgba_int_t* col = &tmpline[p_x->pos];
311 unsigned int weight = p_x->weight;
317 } while (p_x<p_x_to);
319 destline->r = r >> 16;
320 destline->g = g >> 16;
321 destline->b = b >> 16;
322 destline->a = a >> 16;
329 decodeMonochromeImage(newdata, newwidth, newheight, monochrome_colors);
337 gfximage_t*image2 = (gfximage_t*)malloc(sizeof(gfximage_t));
338 image2->data = newdata;
339 image2->width = newwidth;
340 image2->height = newheight;