10 void gfximage_save_jpeg(gfximage_t*img, const char*filename, int quality)
13 int l = img->width*img->height;
14 unsigned char*data = (unsigned char*)rfx_alloc(img->width*img->height*3);
16 for(t=0,s=0;t<l;s+=3,t++) {
17 data[s+0] = img->data[t].r;
18 data[s+1] = img->data[t].g;
19 data[s+2] = img->data[t].b;
21 jpeg_save(data, img->width, img->height, quality, filename);
24 void gfximage_save_png(gfximage_t*image, const char*filename)
26 writePNG(filename, (void*)image->data, image->width, image->height);
29 typedef struct scale_lookup {
34 typedef struct rgba_int {
38 static int bicubic = 0;
40 static scale_lookup_t**make_scale_lookup(int width, int newwidth)
42 scale_lookup_t*lookupx = (scale_lookup_t*)rfx_alloc((width>newwidth?width:newwidth)*2*sizeof(scale_lookup_t));
43 scale_lookup_t**lblockx = (scale_lookup_t**)rfx_alloc((newwidth+1)*sizeof(scale_lookup_t**));
44 double fx = ((double)width)/((double)newwidth);
47 scale_lookup_t*p_x = lookupx;
50 for(x=0;x<newwidth;x++) {
54 double rem = fromx+1-px;
55 int i = (int)(256/fx);
56 int xweight = (int)(rem*256/fx);
60 if(tox>=width) tox = width-1;
61 for(xx=fromx;xx<=tox;xx++) {
62 if(xx==fromx && xx==tox) p_x->weight = 256;
63 else if(xx==fromx) p_x->weight = xweight;
64 else if(xx==tox) p_x->weight = 256-w;
73 for(x=0;x<newwidth;x++) {
75 int ix2 = ((int)px)+1;
77 if(ix2>=width) ix2=width-1;
81 p_x[0].weight = (int)(256*(1-r));
83 p_x[1].weight = 256-p_x[0].weight;
89 lblockx[newwidth] = p_x;
93 static void encodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
96 int len = width*height;
98 U32* img = (U32*)data;
102 if(img[t] != color1) {
107 *(U32*)&colors[0] = color1;
108 *(U32*)&colors[1] = color2;
110 if(img[t] == color1) {
118 static void decodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
121 int len = width*height;
125 data[t].r = (colors[0].r * (255-m) + colors[1].r * m) >> 8;
126 data[t].g = (colors[0].g * (255-m) + colors[1].g * m) >> 8;
127 data[t].b = (colors[0].b * (255-m) + colors[1].b * m) >> 8;
128 data[t].a = (colors[0].a * (255-m) + colors[1].a * m) >> 8;
132 void blurImage(gfxcolor_t*src, int width, int height, int r) __attribute__ ((noinline));
134 void blurImage(gfxcolor_t*src, int width, int height, int r)
136 int e = 2; // r times e is the sampling interval
137 double*gauss = (double*)rfx_alloc(r*e*sizeof(double));
141 double t = (x - r*e/2.0)/r;
142 gauss[x] = exp(-0.5*t*t);
145 int*weights = (int*)rfx_alloc(r*e*sizeof(int));
147 weights[x] = (int)(gauss[x]*65536.0001/sum);
151 gfxcolor_t*tmp = rfx_alloc(sizeof(gfxcolor_t)*width*height);
154 for(y=0;y<height;y++) {
155 gfxcolor_t*s = &src[y*width];
156 gfxcolor_t*d = &tmp[y*width];
157 for(x=0;x<range;x++) {
160 for(x=range;x<width-range;x++) {
167 for(xx=x-range;xx<x+range;xx++) {
179 for(x=width-range;x<width;x++) {
184 for(x=0;x<width;x++) {
185 gfxcolor_t*s = &tmp[x];
186 gfxcolor_t*d = &src[x];
188 for(y=0;y<range;y++) {
192 for(y=range;y<height-range;y++) {
198 int cy,cyy=yy-range*width;
199 for(cy=y-range;cy<y+range;cy++) {
200 r += s[cyy].r * f[0];
201 g += s[cyy].g * f[0];
202 b += s[cyy].b * f[0];
203 a += s[cyy].a * f[0];
213 for(y=0;y<range;y++) {
224 int swf_ImageGetNumberOfPaletteEntries2(gfxcolor_t*_img, int width, int height)
226 int len = width*height;
228 U32* img = (U32*)_img;
232 if(img[t] != color1) {
241 if(img[t] != color1 && img[t] != color2) {
248 gfximage_t* gfximage_rescale(gfximage_t*image, int newwidth, int newheight)
252 scale_lookup_t *p, **lblockx,**lblocky;
255 gfxcolor_t monochrome_colors[2];
257 if(newwidth<1 || newheight<1)
260 int width = image->width;
261 int height = image->height;
262 gfxcolor_t*data = image->data;
264 if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2) {
266 encodeMonochromeImage(data, width, height, monochrome_colors);
267 int r1 = width / newwidth;
268 int r2 = height / newheight;
271 /* high-resolution monochrome images are usually dithered, so
272 low-pass filter them first to get rid of any moire patterns */
273 blurImage(data, width, height, r+1);
277 tmpline = (rgba_int_t*)rfx_alloc(width*sizeof(rgba_int_t));
278 newdata = (gfxcolor_t*)rfx_alloc(newwidth*newheight*sizeof(gfxcolor_t));
280 lblockx = make_scale_lookup(width, newwidth);
281 lblocky = make_scale_lookup(height, newheight);
283 for(p=lblocky[0];p<lblocky[newheight];p++)
286 for(y=0;y<newheight;y++) {
287 gfxcolor_t*destline = &newdata[y*newwidth];
289 /* create lookup table for y */
290 rgba_int_t*l = tmpline;
291 scale_lookup_t*p_y,*p_x;
292 memset(tmpline, 0, width*sizeof(rgba_int_t));
293 for(p_y=lblocky[y];p_y<lblocky[y+1];p_y++) {
294 gfxcolor_t*line = &data[p_y->pos];
296 int weight = p_y->weight;
297 for(x=0;x<width;x++) {
298 tmpline[x].r += line[x].r*weight;
299 tmpline[x].g += line[x].g*weight;
300 tmpline[x].b += line[x].b*weight;
301 tmpline[x].a += line[x].a*weight;
305 /* process x direction */
307 for(x=0;x<newwidth;x++) {
308 unsigned int r=0,g=0,b=0,a=0;
309 scale_lookup_t*p_x_to = lblockx[x+1];
311 rgba_int_t* col = &tmpline[p_x->pos];
312 unsigned int weight = p_x->weight;
318 } while (p_x<p_x_to);
320 destline->r = r >> 16;
321 destline->g = g >> 16;
322 destline->b = b >> 16;
323 destline->a = a >> 16;
330 decodeMonochromeImage(newdata, newwidth, newheight, monochrome_colors);
338 gfximage_t*image2 = (gfximage_t*)malloc(sizeof(gfximage_t));
339 image2->data = newdata;
340 image2->width = newwidth;
341 image2->height = newheight;
345 void gfximage_free(gfximage_t*b)