10 gfximage_t*gfximage_new(int width, int height)
12 gfximage_t*i = rfx_calloc(sizeof(gfximage_t));
13 i->data = rfx_calloc(width*height*4);
19 void gfximage_save_jpeg(gfximage_t*img, const char*filename, int quality)
22 int l = img->width*img->height;
23 unsigned char*data = (unsigned char*)rfx_alloc(img->width*img->height*3);
25 for(t=0,s=0;t<l;s+=3,t++) {
26 data[s+0] = img->data[t].r;
27 data[s+1] = img->data[t].g;
28 data[s+2] = img->data[t].b;
30 jpeg_save(data, img->width, img->height, quality, filename);
34 void gfximage_save_png(gfximage_t*image, const char*filename)
36 writePNG(filename, (void*)image->data, image->width, image->height);
39 typedef struct scale_lookup {
44 typedef struct rgba_int {
48 static int bicubic = 0;
50 static scale_lookup_t**make_scale_lookup(int width, int newwidth)
52 scale_lookup_t*lookupx = (scale_lookup_t*)rfx_alloc((width>newwidth?width:newwidth)*2*sizeof(scale_lookup_t));
53 scale_lookup_t**lblockx = (scale_lookup_t**)rfx_alloc((newwidth+1)*sizeof(scale_lookup_t**));
54 double fx = ((double)width)/((double)newwidth);
57 scale_lookup_t*p_x = lookupx;
60 for(x=0;x<newwidth;x++) {
64 double rem = fromx+1-px;
65 int i = (int)(256/fx);
66 int xweight = (int)(rem*256/fx);
70 if(tox>=width) tox = width-1;
71 for(xx=fromx;xx<=tox;xx++) {
72 if(xx==fromx && xx==tox) p_x->weight = 256;
73 else if(xx==fromx) p_x->weight = xweight;
74 else if(xx==tox) p_x->weight = 256-w;
83 for(x=0;x<newwidth;x++) {
85 int ix2 = ((int)px)+1;
87 if(ix2>=width) ix2=width-1;
91 p_x[0].weight = (int)(256*(1-r));
93 p_x[1].weight = 256-p_x[0].weight;
99 lblockx[newwidth] = p_x;
103 static void encodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
106 int len = width*height;
108 U32* img = (U32*)data;
112 if(img[t] != color1) {
117 *(U32*)&colors[0] = color1;
118 *(U32*)&colors[1] = color2;
120 if(img[t] == color1) {
128 static void decodeMonochromeImage(gfxcolor_t*data, int width, int height, gfxcolor_t*colors)
131 int len = width*height;
135 data[t].r = (colors[0].r * (255-m) + colors[1].r * m) >> 8;
136 data[t].g = (colors[0].g * (255-m) + colors[1].g * m) >> 8;
137 data[t].b = (colors[0].b * (255-m) + colors[1].b * m) >> 8;
138 data[t].a = (colors[0].a * (255-m) + colors[1].a * m) >> 8;
142 void blurImage(gfxcolor_t*src, int width, int height, int r) __attribute__ ((noinline));
144 void blurImage(gfxcolor_t*src, int width, int height, int r)
146 int e = 2; // r times e is the sampling interval
147 double*gauss = (double*)rfx_alloc(r*e*sizeof(double));
151 double t = (x - r*e/2.0)/r;
152 gauss[x] = exp(-0.5*t*t);
155 int*weights = (int*)rfx_alloc(r*e*sizeof(int));
157 weights[x] = (int)(gauss[x]*65536.0001/sum);
161 gfxcolor_t*tmp = rfx_alloc(sizeof(gfxcolor_t)*width*height);
164 for(y=0;y<height;y++) {
165 gfxcolor_t*s = &src[y*width];
166 gfxcolor_t*d = &tmp[y*width];
167 for(x=0;x<range && x<width;x++) {
170 for(;x<width-range;x++) {
177 for(xx=x-range;xx<x+range;xx++) {
194 for(x=0;x<width;x++) {
195 gfxcolor_t*s = &tmp[x];
196 gfxcolor_t*d = &src[x];
198 for(y=0;y<range&&y<height;y++) {
202 for(;y<height-range;y++) {
208 int cy,cyy=yy-range*width;
209 for(cy=y-range;cy<y+range;cy++) {
210 r += s[cyy].r * f[0];
211 g += s[cyy].g * f[0];
212 b += s[cyy].b * f[0];
213 a += s[cyy].a * f[0];
234 int swf_ImageGetNumberOfPaletteEntries2(gfxcolor_t*_img, int width, int height)
236 int len = width*height;
238 U32* img = (U32*)_img;
242 if(img[t] != color1) {
251 if(img[t] != color1 && img[t] != color2) {
258 gfximage_t* gfximage_rescale(gfximage_t*image, int newwidth, int newheight)
262 scale_lookup_t *p, **lblockx,**lblocky;
265 gfxcolor_t monochrome_colors[2];
272 int width = image->width;
273 int height = image->height;
274 gfxcolor_t*data = image->data;
276 if(swf_ImageGetNumberOfPaletteEntries2(data, width, height) == 2) {
278 encodeMonochromeImage(data, width, height, monochrome_colors);
279 int r1 = width / newwidth;
280 int r2 = height / newheight;
283 /* high-resolution monochrome images are usually dithered, so
284 low-pass filter them first to get rid of any moire patterns */
285 blurImage(data, width, height, r+1);
289 tmpline = (rgba_int_t*)rfx_alloc(width*sizeof(rgba_int_t));
290 newdata = (gfxcolor_t*)rfx_alloc(newwidth*newheight*sizeof(gfxcolor_t));
292 lblockx = make_scale_lookup(width, newwidth);
293 lblocky = make_scale_lookup(height, newheight);
295 for(p=lblocky[0];p<lblocky[newheight];p++)
298 for(y=0;y<newheight;y++) {
299 gfxcolor_t*destline = &newdata[y*newwidth];
301 /* create lookup table for y */
302 rgba_int_t*l = tmpline;
303 scale_lookup_t*p_y,*p_x;
304 memset(tmpline, 0, width*sizeof(rgba_int_t));
305 for(p_y=lblocky[y];p_y<lblocky[y+1];p_y++) {
306 gfxcolor_t*line = &data[p_y->pos];
308 int weight = p_y->weight;
309 for(x=0;x<width;x++) {
310 tmpline[x].r += line[x].r*weight;
311 tmpline[x].g += line[x].g*weight;
312 tmpline[x].b += line[x].b*weight;
313 tmpline[x].a += line[x].a*weight;
317 /* process x direction */
319 for(x=0;x<newwidth;x++) {
320 unsigned int r=0,g=0,b=0,a=0;
321 scale_lookup_t*p_x_to = lblockx[x+1];
323 rgba_int_t* col = &tmpline[p_x->pos];
324 unsigned int weight = p_x->weight;
330 } while (p_x<p_x_to);
332 destline->r = r >> 16;
333 destline->g = g >> 16;
334 destline->b = b >> 16;
335 destline->a = a >> 16;
342 decodeMonochromeImage(newdata, newwidth, newheight, monochrome_colors);
350 gfximage_t*image2 = (gfximage_t*)malloc(sizeof(gfximage_t));
351 image2->data = newdata;
352 image2->width = newwidth;
353 image2->height = newheight;
357 void gfximage_free(gfximage_t*b)