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);
25 void gfximage_save_png(gfximage_t*image, const char*filename)
27 writePNG(filename, (void*)image->data, image->width, image->height);
30 typedef struct scale_lookup {
35 typedef struct rgba_int {
39 static int bicubic = 0;
41 static scale_lookup_t**make_scale_lookup(int width, int newwidth)
43 scale_lookup_t*lookupx = (scale_lookup_t*)rfx_alloc((width>newwidth?width:newwidth)*2*sizeof(scale_lookup_t));
44 scale_lookup_t**lblockx = (scale_lookup_t**)rfx_alloc((newwidth+1)*sizeof(scale_lookup_t**));
45 double fx = ((double)width)/((double)newwidth);
48 scale_lookup_t*p_x = lookupx;
51 for(x=0;x<newwidth;x++) {
55 double rem = fromx+1-px;
56 int i = (int)(256/fx);
57 int xweight = (int)(rem*256/fx);
61 if(tox>=width) tox = width-1;
62 for(xx=fromx;xx<=tox;xx++) {
63 if(xx==fromx && xx==tox) p_x->weight = 256;
64 else if(xx==fromx) p_x->weight = xweight;
65 else if(xx==tox) p_x->weight = 256-w;
74 for(x=0;x<newwidth;x++) {
76 int ix2 = ((int)px)+1;
78 if(ix2>=width) ix2=width-1;
82 p_x[0].weight = (int)(256*(1-r));
84 p_x[1].weight = 256-p_x[0].weight;
90 lblockx[newwidth] = p_x;
94 static void encodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
97 int len = width*height;
99 U32* img = (U32*)data;
103 if(img[t] != color1) {
108 *(U32*)&colors[0] = color1;
109 *(U32*)&colors[1] = color2;
111 if(img[t] == color1) {
119 static void decodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
122 int len = width*height;
126 data[t].r = (colors[0].r * (255-m) + colors[1].r * m) >> 8;
127 data[t].g = (colors[0].g * (255-m) + colors[1].g * m) >> 8;
128 data[t].b = (colors[0].b * (255-m) + colors[1].b * m) >> 8;
129 data[t].a = (colors[0].a * (255-m) + colors[1].a * m) >> 8;
133 void blurImage(gfxcolor_t*src, int width, int height, int r) __attribute__ ((noinline));
135 void blurImage(gfxcolor_t*src, int width, int height, int r)
137 int e = 2; // r times e is the sampling interval
138 double*gauss = (double*)rfx_alloc(r*e*sizeof(double));
142 double t = (x - r*e/2.0)/r;
143 gauss[x] = exp(-0.5*t*t);
146 int*weights = (int*)rfx_alloc(r*e*sizeof(int));
148 weights[x] = (int)(gauss[x]*65536.0001/sum);
152 gfxcolor_t*tmp = rfx_alloc(sizeof(gfxcolor_t)*width*height);
155 for(y=0;y<height;y++) {
156 gfxcolor_t*s = &src[y*width];
157 gfxcolor_t*d = &tmp[y*width];
158 for(x=0;x<range;x++) {
161 for(x=range;x<width-range;x++) {
168 for(xx=x-range;xx<x+range;xx++) {
180 for(x=width-range;x<width;x++) {
185 for(x=0;x<width;x++) {
186 gfxcolor_t*s = &tmp[x];
187 gfxcolor_t*d = &src[x];
189 for(y=0;y<range;y++) {
193 for(y=range;y<height-range;y++) {
199 int cy,cyy=yy-range*width;
200 for(cy=y-range;cy<y+range;cy++) {
201 r += s[cyy].r * f[0];
202 g += s[cyy].g * f[0];
203 b += s[cyy].b * f[0];
204 a += s[cyy].a * f[0];
214 for(y=0;y<range;y++) {
225 int swf_ImageGetNumberOfPaletteEntries2(gfxcolor_t*_img, int width, int height)
227 int len = width*height;
229 U32* img = (U32*)_img;
233 if(img[t] != color1) {
242 if(img[t] != color1 && img[t] != color2) {
249 gfximage_t* gfximage_rescale(gfximage_t*image, int newwidth, int newheight)
253 scale_lookup_t *p, **lblockx,**lblocky;
256 gfxcolor_t monochrome_colors[2];
258 if(newwidth<1 || newheight<1)
261 int width = image->width;
262 int height = image->height;
263 gfxcolor_t*data = image->data;
265 if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2) {
267 encodeMonochromeImage(data, width, height, monochrome_colors);
268 int r1 = width / newwidth;
269 int r2 = height / newheight;
272 /* high-resolution monochrome images are usually dithered, so
273 low-pass filter them first to get rid of any moire patterns */
274 blurImage(data, width, height, r+1);
278 tmpline = (rgba_int_t*)rfx_alloc(width*sizeof(rgba_int_t));
279 newdata = (gfxcolor_t*)rfx_alloc(newwidth*newheight*sizeof(gfxcolor_t));
281 lblockx = make_scale_lookup(width, newwidth);
282 lblocky = make_scale_lookup(height, newheight);
284 for(p=lblocky[0];p<lblocky[newheight];p++)
287 for(y=0;y<newheight;y++) {
288 gfxcolor_t*destline = &newdata[y*newwidth];
290 /* create lookup table for y */
291 rgba_int_t*l = tmpline;
292 scale_lookup_t*p_y,*p_x;
293 memset(tmpline, 0, width*sizeof(rgba_int_t));
294 for(p_y=lblocky[y];p_y<lblocky[y+1];p_y++) {
295 gfxcolor_t*line = &data[p_y->pos];
297 int weight = p_y->weight;
298 for(x=0;x<width;x++) {
299 tmpline[x].r += line[x].r*weight;
300 tmpline[x].g += line[x].g*weight;
301 tmpline[x].b += line[x].b*weight;
302 tmpline[x].a += line[x].a*weight;
306 /* process x direction */
308 for(x=0;x<newwidth;x++) {
309 unsigned int r=0,g=0,b=0,a=0;
310 scale_lookup_t*p_x_to = lblockx[x+1];
312 rgba_int_t* col = &tmpline[p_x->pos];
313 unsigned int weight = p_x->weight;
319 } while (p_x<p_x_to);
321 destline->r = r >> 16;
322 destline->g = g >> 16;
323 destline->b = b >> 16;
324 destline->a = a >> 16;
331 decodeMonochromeImage(newdata, newwidth, newheight, monochrome_colors);
339 gfximage_t*image2 = (gfximage_t*)malloc(sizeof(gfximage_t));
340 image2->data = newdata;
341 image2->width = newwidth;
342 image2->height = newheight;
346 void gfximage_free(gfximage_t*b)