1 #include <png.h> // must be included before Python.h because of setjmp
2 #include <lib/gdi/picload.h>
4 #include <lib/python/python.h>
11 #include <epng.h> // savePNG need it
16 //#include "transupp.h"
19 unsigned char *pic_buffer=NULL;
21 static unsigned char *conv24to32(unsigned char * orgin, int size, int background = 0)
24 unsigned char *cr = new unsigned char[size * 4];
27 printf("[CONV32] Error: malloc\n");
31 unsigned char alpha = 0x00;
32 if(background) alpha = 0xFF;
34 for (s = 0, d = 0 ; s < (size * 3); s += 3, d += 4 )
37 cr[d+1] = orgin[s + 1];
38 cr[d+2] = orgin[s + 2];
45 static unsigned char *simple_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
47 unsigned char *cr, *p, *l;
49 cr = new unsigned char[dx * dy * 4];
52 printf("[RESIZE] Error: malloc\n");
57 for (j = 0; j < dy; j++,l += dx * 4)
59 p = orgin + (j * oy / dy * ox * 4);
60 for (i = 0, k = 0; i < dx; i++, k += 4)
73 static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
75 unsigned char *cr, *p, *q;
76 int i, j, k, l, xa, xb, ya, yb;
78 cr = new unsigned char[dx * dy * 4];
81 printf("[RESIZE] Error: malloc\n");
86 for (j = 0; j < dy; j++)
88 for (i = 0; i < dx; i++, p += 4)
92 xb = (i + 1) * ox / dx;
95 yb = (j + 1) * oy / dy;
98 for (l = ya, r = 0, g = 0, b = 0, a = 0, sq = 0; l <= yb; l++)
100 q = orgin + ((l * ox + xa) * 4);
101 for (k = xa; k <= xb; k++, q += 4, sq++)
103 r += q[0]; g += q[1]; b += q[2]; a += q[3];
106 p[0] = r / sq; p[1] = g / sq; p[2] = b / sq; p[3] = a / sq;
113 //-------------------------------------------------------------------
115 struct r_jpeg_error_mgr
117 struct jpeg_error_mgr pub;
121 void jpeg_cb_error_exit(j_common_ptr cinfo)
123 struct r_jpeg_error_mgr *mptr;
124 mptr = (struct r_jpeg_error_mgr *) cinfo->err;
125 (*cinfo->err->output_message) (cinfo);
126 longjmp(mptr->envbuffer, 1);
129 static int jpeg_save(unsigned char *image_buffer, const char * filename, int quality, int image_height, int image_width)
131 struct jpeg_compress_struct cinfo;
132 struct jpeg_error_mgr jerr;
133 FILE * outfile; /* target file */
134 JSAMPROW row_pointer[1];/* pointer to JSAMPLE row[s] */
135 int row_stride; /* physical row width in image buffer */
137 cinfo.err = jpeg_std_error(&jerr);
138 jpeg_create_compress(&cinfo);
140 if ((outfile = fopen(filename, "wb")) == NULL)
142 eDebug("[JPEG] can't open %s", filename);
145 jpeg_stdio_dest(&cinfo, outfile);
147 cinfo.image_width = image_width;
148 cinfo.image_height = image_height;
149 cinfo.input_components = 3;
150 cinfo.in_color_space = JCS_RGB;
151 jpeg_set_defaults(&cinfo);
152 jpeg_set_quality(&cinfo, quality, TRUE );
153 jpeg_start_compress(&cinfo, TRUE);
154 row_stride = image_width * 3;
155 while (cinfo.next_scanline < cinfo.image_height)
157 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
158 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
160 jpeg_finish_compress(&cinfo);
162 jpeg_destroy_compress(&cinfo);
167 static int jpeg_load(const char *filename, int *x, int *y)
169 struct jpeg_decompress_struct cinfo;
170 struct jpeg_decompress_struct *ciptr = &cinfo;
171 struct r_jpeg_error_mgr emgr;
174 if (!(fh = fopen(filename, "rb")))
177 ciptr->err = jpeg_std_error(&emgr.pub);
178 emgr.pub.error_exit = jpeg_cb_error_exit;
179 if (setjmp(emgr.envbuffer) == 1)
181 jpeg_destroy_decompress(ciptr);
186 jpeg_create_decompress(ciptr);
187 jpeg_stdio_src(ciptr, fh);
188 jpeg_read_header(ciptr, TRUE);
189 ciptr->out_color_space = JCS_RGB;
190 ciptr->scale_denom = 1;
192 jpeg_start_decompress(ciptr);
194 *x=ciptr->output_width;
195 *y=ciptr->output_height;
197 if(ciptr->output_components == 3)
199 JSAMPLE *lb = (JSAMPLE *)(*ciptr->mem->alloc_small)((j_common_ptr) ciptr, JPOOL_PERMANENT, ciptr->output_width * ciptr->output_components);
200 pic_buffer = new unsigned char[ciptr->output_height * ciptr->output_width * ciptr->output_components];
201 unsigned char *bp = pic_buffer;
203 while (ciptr->output_scanline < ciptr->output_height)
205 jpeg_read_scanlines(ciptr, &lb, 1);
206 memcpy(bp, lb, ciptr->output_width * ciptr->output_components);
207 bp += ciptr->output_width * ciptr->output_components;
210 jpeg_finish_decompress(ciptr);
211 jpeg_destroy_decompress(ciptr);
216 //---------------------------------------------------------------------------------------------
218 #define BMP_TORASTER_OFFSET 10
219 #define BMP_SIZE_OFFSET 18
220 #define BMP_BPP_OFFSET 28
221 #define BMP_RLE_OFFSET 30
222 #define BMP_COLOR_OFFSET 54
224 #define fill4B(a) ((4 - ((a) % 4 )) & 0x03)
232 static void fetch_pallete(int fd, struct color pallete[], int count)
234 unsigned char buff[4];
235 lseek(fd, BMP_COLOR_OFFSET, SEEK_SET);
236 for (int i = 0; i < count; i++)
239 pallete[i].red = buff[2];
240 pallete[i].green = buff[1];
241 pallete[i].blue = buff[0];
245 static int bmp_load(const char *filename, int *x, int *y)
247 unsigned char buff[4];
248 struct color pallete[256];
250 int fd = open(filename, O_RDONLY);
251 if (fd == -1) return 0;
252 if (lseek(fd, BMP_SIZE_OFFSET, SEEK_SET) == -1) return 0;
254 *x = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
256 *y = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
257 if (lseek(fd, BMP_TORASTER_OFFSET, SEEK_SET) == -1) return 0;
259 int raster = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
260 if (lseek(fd, BMP_BPP_OFFSET, SEEK_SET) == -1) return 0;
262 int bpp = buff[0] + (buff[1] << 8);
264 pic_buffer = new unsigned char[(*x) * (*y) * 3];
265 unsigned char *wr_buffer = pic_buffer + (*x) * ((*y) - 1) * 3;
271 int skip = fill4B((*x) / 2 + (*x) % 2);
272 fetch_pallete(fd, pallete, 16);
273 lseek(fd, raster, SEEK_SET);
274 unsigned char * tbuffer = new unsigned char[*x / 2 + 1];
277 for (int i = 0; i < *y; i++)
279 read(fd, tbuffer, (*x) / 2 + *x % 2);
281 for (j = 0; j < (*x) / 2; j++)
283 unsigned char c1 = tbuffer[j] >> 4;
284 unsigned char c2 = tbuffer[j] & 0x0f;
285 *wr_buffer++ = pallete[c1].red;
286 *wr_buffer++ = pallete[c1].green;
287 *wr_buffer++ = pallete[c1].blue;
288 *wr_buffer++ = pallete[c2].red;
289 *wr_buffer++ = pallete[c2].green;
290 *wr_buffer++ = pallete[c2].blue;
294 unsigned char c1 = tbuffer[j] >> 4;
295 *wr_buffer++ = pallete[c1].red;
296 *wr_buffer++ = pallete[c1].green;
297 *wr_buffer++ = pallete[c1].blue;
300 read(fd, buff, skip);
301 wr_buffer -= (*x) * 6;
307 int skip = fill4B(*x);
308 fetch_pallete(fd, pallete, 256);
309 lseek(fd, raster, SEEK_SET);
310 unsigned char * tbuffer = new unsigned char[*x];
313 for (int i = 0; i < *y; i++)
315 read(fd, tbuffer, *x);
316 for (int j = 0; j < *x; j++)
318 wr_buffer[j * 3] = pallete[tbuffer[j]].red;
319 wr_buffer[j * 3 + 1] = pallete[tbuffer[j]].green;
320 wr_buffer[j * 3 + 2] = pallete[tbuffer[j]].blue;
323 read(fd, buff, skip);
324 wr_buffer -= (*x) * 3;
330 int skip = fill4B((*x) * 3);
331 lseek(fd, raster, SEEK_SET);
332 for (int i = 0; i < (*y); i++)
334 read(fd, wr_buffer, (*x) * 3);
335 for (int j = 0; j < (*x) * 3 ; j = j + 3)
337 unsigned char c = wr_buffer[j];
338 wr_buffer[j] = wr_buffer[j + 2];
339 wr_buffer[j + 2] = c;
342 read(fd, buff, skip);
343 wr_buffer -= (*x) * 3;
355 //---------------------------------------------------------------------------------------------
356 static int png_load(const char *filename, int *x, int *y)
358 static const png_color_16 my_background = {0, 0, 0, 0, 0};
362 png_uint_32 width, height;
364 int bit_depth, color_type, interlace_type;
365 int number_passes, pass;
369 if (!(fh = fopen(filename, "rb"))) return 0;
371 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
374 info_ptr = png_create_info_struct(png_ptr);
375 if (info_ptr == NULL)
377 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
382 if (setjmp(png_ptr->jmpbuf))
384 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
389 png_init_io(png_ptr, fh);
391 png_read_info(png_ptr, info_ptr);
392 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
394 if ((color_type == PNG_COLOR_TYPE_PALETTE)||(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)||(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
395 png_set_expand(png_ptr);
397 png_set_strip_16(png_ptr);
398 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
399 png_set_gray_to_rgb(png_ptr);
401 number_passes = png_set_interlace_handling(png_ptr);
402 png_read_update_info(png_ptr, info_ptr);
404 int bpp = png_get_rowbytes(png_ptr, info_ptr)/width;
405 if ((bpp !=4) && (bpp !=3))
407 eDebug("[PNG] Error processing");
411 if (width * height > 1000000) // 1000x1000 or equiv.
413 eDebug("[png_load] image size is %d x %d, which is \"too large\".", width, height);
414 png_read_end(png_ptr, info_ptr);
415 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
420 pic_buffer = new unsigned char[width * height * bpp];
424 for(pass = 0; pass < number_passes; pass++)
426 fbptr = (png_byte *)pic_buffer;
427 for (i = 0; i < height; i++, fbptr += width * bpp)
428 png_read_row(png_ptr, fbptr, NULL);
430 png_read_end(png_ptr, info_ptr);
431 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
434 pic_buffer = conv24to32(pic_buffer, width * height, 1);
438 //-------------------------------------------------------------------
440 inline void m_rend_gif_decodecolormap(unsigned char *cmb, unsigned char *rgbb, ColorMapObject *cm, int s, int l)
442 GifColorType *cmentry;
444 for (i = 0; i < l; i++)
446 cmentry = &cm->Colors[cmb[i]];
447 *(rgbb++) = cmentry->Red;
448 *(rgbb++) = cmentry->Green;
449 *(rgbb++) = cmentry->Blue;
453 static int gif_load(const char *filename, int *x, int *y)
455 int px, py, i, j, ibxs;
456 unsigned char *fbptr;
457 unsigned char *lb=NULL;
458 unsigned char *slb=NULL;
461 GifByteType *extension;
462 ColorMapObject *cmap;
466 gft = DGifOpenFileName(filename);
471 if (DGifGetRecordType(gft, &rt) == GIF_ERROR)
475 case IMAGE_DESC_RECORD_TYPE:
476 if (DGifGetImageDesc(gft) == GIF_ERROR)
478 *x = px = gft->Image.Width;
479 *y = py = gft->Image.Height;
480 pic_buffer = new unsigned char[px * py * 3];
481 lb = (unsigned char *)malloc(px * 3);
482 slb = (unsigned char *) malloc(px);
484 if (lb != NULL && slb != NULL)
486 cmap = (gft->Image.ColorMap ? gft->Image.ColorMap : gft->SColorMap);
487 cmaps = cmap->ColorCount;
491 if (!(gft->Image.Interlace))
493 for (i = 0; i < py; i++, fbptr += px * 3)
495 if (DGifGetLine(gft, slb, px) == GIF_ERROR)
497 m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
498 memcpy(fbptr, lb, px * 3);
503 for (j = 0; j < 4; j++)
506 for (i = 0; i < py; i++, fbptr += px * 3)
508 if (DGifGetLine(gft, slb, px) == GIF_ERROR)
510 m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
511 memcpy(fbptr, lb, px * 3);
527 case EXTENSION_RECORD_TYPE:
528 if (DGifGetExtension(gft, &extcode, &extension) == GIF_ERROR)
530 while (extension != NULL)
531 if (DGifGetExtensionNext(gft, &extension) == GIF_ERROR)
538 while (rt != TERMINATE_RECORD_TYPE);
543 eDebug("[GIF] Error");
550 //---------------------------------------------------------------------------------------------
552 PyObject *getExif(const char *filename)
556 if(exif.DecodeExif(filename))
558 if(exif.m_exifinfo->IsExif)
562 list = PyList_New(22);
563 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->Version));
564 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->CameraMake));
565 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->CameraModel));
566 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->DateTime));
567 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->Comments));
568 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d x %d", exif.m_exifinfo->Width, exif.m_exifinfo->Height));
569 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->Orientation));
570 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->MeteringMode));
571 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->ExposureProgram));
572 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->LightSource));
573 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->FlashUsed));
574 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d", exif.m_exifinfo->CompressionLevel));
575 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d", exif.m_exifinfo->ISOequivalent));
576 sprintf(tmp, "%.2f", exif.m_exifinfo->Xresolution);
577 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
578 sprintf(tmp, "%.2f", exif.m_exifinfo->Yresolution);
579 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
580 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->ResolutionUnit));
581 sprintf(tmp, "%.2f", exif.m_exifinfo->Brightness);
582 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
583 sprintf(tmp, "%.5f sec.", exif.m_exifinfo->ExposureTime);
584 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
585 sprintf(tmp, "%.5f", exif.m_exifinfo->ExposureBias);
586 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
587 sprintf(tmp, "%.5f", exif.m_exifinfo->Distance);
588 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
589 sprintf(tmp, "%.5f", exif.m_exifinfo->CCDWidth);
590 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
591 sprintf(tmp, "%.2f", exif.m_exifinfo->ApertureFNumber);
592 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
596 list = PyList_New(1);
597 PyList_SET_ITEM(list, 0, PyString_FromString(exif.m_szLastError));
603 list = PyList_New(1);
604 PyList_SET_ITEM(list, 0, PyString_FromString(exif.m_szLastError));
607 return list ? (PyObject*)list : (PyObject*)PyList_New(0);
610 //---------------------------------------------------------------------------------------------
611 enum {F_NONE, F_PNG, F_JPEG, F_BMP, F_GIF};
613 static int pic_id(const char *name)
615 unsigned char id[10];
616 int fd = open(name, O_RDONLY);
622 if(id[1] == 'P' && id[2] == 'N' && id[3] == 'G')
624 else if(id[6] == 'J' && id[7] == 'F' && id[8] == 'I' && id[9] == 'F')
626 else if(id[0] == 0xff && id[1] == 0xd8 && id[2] == 0xff)
628 else if(id[0] == 'B' && id[1] == 'M' )
630 else if(id[0] == 'G' && id[1] == 'I' && id[2] == 'F')
635 int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspect, int resize_mode, int rotate, int background, std::string cachefile)
638 int ox=0, oy=0, imx, imy;
642 if(cachefile.length())
644 if(png_load(cachefile.c_str(), &ox, &oy))
645 eDebug("[CACHEPIC] x-size=%d, y-size=%d", ox, oy);
650 switch(pic_id(filename.c_str()))
652 case F_PNG: png_load(filename.c_str(), &ox, &oy);break;
653 case F_JPEG: jpeg_load(filename.c_str(), &ox, &oy);pic_buffer = conv24to32(pic_buffer, ox*oy, 1); break;
654 case F_BMP: bmp_load(filename.c_str(), &ox, &oy);pic_buffer = conv24to32(pic_buffer, ox*oy, 1); break;
655 case F_GIF: gif_load(filename.c_str(), &ox, &oy);pic_buffer = conv24to32(pic_buffer, ox*oy, 1); break;
657 eDebug("[PIC] <format not supportet>");
667 case 1: aspect_ratio = 1.778 / ((double)720/576); break; //16:9
668 case 2: aspect_ratio = 1.600 / ((double)720/576); break; //16:10
669 case 3: aspect_ratio = 1.250 / ((double)720/576); break; //5:4
670 default: aspect_ratio = 1.333 / ((double)720/576); //4:3
673 if((aspect_ratio * oy * w / ox) <= h)
676 imy = (int)(aspect_ratio*oy*w/ox);
680 imx = (int)((1.0/aspect_ratio)*ox*h/oy);
684 if(resize_mode) pic_buffer = color_resize(pic_buffer, ox, oy, imx, imy);
685 else pic_buffer = simple_resize(pic_buffer, ox, oy, imx, imy);
692 result=new gPixmap(eSize(w, h), 32);
693 gSurface *surface = result->surface;
696 int o_y=0, u_y=0, v_x=0, h_x=0;
697 unsigned char clear[4] = {0x00,0x00,0x00,0x00};
698 if(background) clear[3]=0xFF;
699 unsigned char *tmp_buffer=((unsigned char *)(surface->data));
712 //eDebug("o_y=%d u_y=%d v_x=%d h_x=%d", o_y, u_y, v_x, h_x);
715 for(a=0; a<(o_y*ox); a++, nc+=4)
717 tmp_buffer=((unsigned char *)(surface->data)) + nc;
718 memcpy(tmp_buffer, clear, sizeof(clear));
724 for(b=0; b<v_x; b++, nc+=4)
726 tmp_buffer=((unsigned char *)(surface->data)) + nc;
727 memcpy(tmp_buffer, clear, sizeof(clear));
730 for(b=0; b<(ox*4); b+=4, nc+=4)
732 tmp_buffer=((unsigned char *)(surface->data)) + nc;
733 tmp_buffer[2]=pic_buffer[oc++];
734 tmp_buffer[1]=pic_buffer[oc++];
735 tmp_buffer[0]=pic_buffer[oc++];
736 tmp_buffer[3]=pic_buffer[oc++];
741 for(b=0; b<h_x; b++, nc+=4)
743 tmp_buffer=((unsigned char *)(surface->data)) + nc;
744 memcpy(tmp_buffer, clear, sizeof(clear));
749 for(a=0; a<(u_y*ox); a++, nc+=4)
751 tmp_buffer=((unsigned char *)(surface->data)) + nc;
752 memcpy(tmp_buffer, clear, sizeof(clear));
755 //eDebug("[PIC] buffer=%d, nc=%d oc=%d ox=%d, oy=%d",w*h*4, nc, oc, ox, oy);
757 surface->clut.data=0;
758 surface->clut.colors=0;
759 surface->clut.start=0;
761 delete [] pic_buffer;
763 if(cachefile.length() && !cache)
765 savePNG( cachefile.c_str(), result);
766 eDebug("[SAVEPIC] x-size=%d, y-size=%d", ox, oy);