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>
14 //#include "transupp.h"
17 unsigned char *pic_buffer=NULL;
19 static unsigned char *simple_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
21 unsigned char *cr, *p, *l;
23 cr = new unsigned char[dx * dy * 3];
26 printf("[RESIZE] Error: malloc\n");
31 for (j = 0; j < dy; j++,l += dx * 3)
33 p = orgin + (j * oy / dy * ox * 3);
34 for (i = 0, k = 0; i < dx; i++, k += 3)
46 static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
48 unsigned char *cr, *p, *q;
49 int i, j, k, l, xa, xb, ya, yb;
51 cr = new unsigned char[dx * dy * 3];
54 printf("[RESIZE] Error: malloc\n");
59 for (j = 0; j < dy; j++)
61 for (i = 0; i < dx; i++, p += 3)
65 xb = (i + 1) * ox / dx;
68 yb = (j + 1) * oy / dy;
71 for (l = ya, r = 0, g = 0, b = 0, sq = 0; l <= yb; l++)
73 q = orgin + ((l * ox + xa) * 3);
74 for (k = xa; k <= xb; k++, q += 3, sq++)
76 r += q[0]; g += q[1]; b += q[2];
79 p[0] = r / sq; p[1] = g / sq; p[2] = b / sq;
86 //-------------------------------------------------------------------
88 struct r_jpeg_error_mgr
90 struct jpeg_error_mgr pub;
94 void jpeg_cb_error_exit(j_common_ptr cinfo)
96 struct r_jpeg_error_mgr *mptr;
97 mptr = (struct r_jpeg_error_mgr *) cinfo->err;
98 (*cinfo->err->output_message) (cinfo);
99 longjmp(mptr->envbuffer, 1);
102 static int jpeg_save(unsigned char *image_buffer, const char * filename, int quality, int image_height, int image_width)
104 struct jpeg_compress_struct cinfo;
105 struct jpeg_error_mgr jerr;
106 FILE * outfile; /* target file */
107 JSAMPROW row_pointer[1];/* pointer to JSAMPLE row[s] */
108 int row_stride; /* physical row width in image buffer */
110 cinfo.err = jpeg_std_error(&jerr);
111 jpeg_create_compress(&cinfo);
113 if ((outfile = fopen(filename, "wb")) == NULL)
115 eDebug("[JPEG] can't open %s", filename);
118 jpeg_stdio_dest(&cinfo, outfile);
120 cinfo.image_width = image_width;
121 cinfo.image_height = image_height;
122 cinfo.input_components = 3;
123 cinfo.in_color_space = JCS_RGB;
124 jpeg_set_defaults(&cinfo);
125 jpeg_set_quality(&cinfo, quality, TRUE );
126 jpeg_start_compress(&cinfo, TRUE);
127 row_stride = image_width * 3;
128 while (cinfo.next_scanline < cinfo.image_height)
130 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
131 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
133 jpeg_finish_compress(&cinfo);
135 jpeg_destroy_compress(&cinfo);
140 static int jpeg_load(const char *filename, int *x, int *y)
142 struct jpeg_decompress_struct cinfo;
143 struct jpeg_decompress_struct *ciptr = &cinfo;
144 struct r_jpeg_error_mgr emgr;
147 if (!(fh = fopen(filename, "rb")))
150 ciptr->err = jpeg_std_error(&emgr.pub);
151 emgr.pub.error_exit = jpeg_cb_error_exit;
152 if (setjmp(emgr.envbuffer) == 1)
154 jpeg_destroy_decompress(ciptr);
159 jpeg_create_decompress(ciptr);
160 jpeg_stdio_src(ciptr, fh);
161 jpeg_read_header(ciptr, TRUE);
162 ciptr->out_color_space = JCS_RGB;
163 ciptr->scale_denom = 1;
165 jpeg_start_decompress(ciptr);
167 *x=ciptr->output_width;
168 *y=ciptr->output_height;
170 if(ciptr->output_components == 3)
172 JSAMPLE *lb = (JSAMPLE *)(*ciptr->mem->alloc_small)((j_common_ptr) ciptr, JPOOL_PERMANENT, ciptr->output_width * ciptr->output_components);
173 pic_buffer = new unsigned char[ciptr->output_height * ciptr->output_width * ciptr->output_components];
174 unsigned char *bp = pic_buffer;
176 while (ciptr->output_scanline < ciptr->output_height)
178 jpeg_read_scanlines(ciptr, &lb, 1);
179 memcpy(bp, lb, ciptr->output_width * ciptr->output_components);
180 bp += ciptr->output_width * ciptr->output_components;
183 jpeg_finish_decompress(ciptr);
184 jpeg_destroy_decompress(ciptr);
189 //---------------------------------------------------------------------------------------------
191 #define BMP_TORASTER_OFFSET 10
192 #define BMP_SIZE_OFFSET 18
193 #define BMP_BPP_OFFSET 28
194 #define BMP_RLE_OFFSET 30
195 #define BMP_COLOR_OFFSET 54
197 #define fill4B(a) ((4 - ((a) % 4 )) & 0x03)
205 static void fetch_pallete(int fd, struct color pallete[], int count)
207 unsigned char buff[4];
208 lseek(fd, BMP_COLOR_OFFSET, SEEK_SET);
209 for (int i = 0; i < count; i++)
212 pallete[i].red = buff[2];
213 pallete[i].green = buff[1];
214 pallete[i].blue = buff[0];
218 static int bmp_load(const char *filename, int *x, int *y)
220 unsigned char buff[4];
221 struct color pallete[256];
223 int fd = open(filename, O_RDONLY);
224 if (fd == -1) return 0;
225 if (lseek(fd, BMP_SIZE_OFFSET, SEEK_SET) == -1) return 0;
227 *x = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
229 *y = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
230 if (lseek(fd, BMP_TORASTER_OFFSET, SEEK_SET) == -1) return 0;
232 int raster = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
233 if (lseek(fd, BMP_BPP_OFFSET, SEEK_SET) == -1) return 0;
235 int bpp = buff[0] + (buff[1] << 8);
237 pic_buffer = new unsigned char[(*x) * (*y) * 3];
238 unsigned char *wr_buffer = pic_buffer + (*x) * ((*y) - 1) * 3;
244 int skip = fill4B((*x) / 2 + (*x) % 2);
245 fetch_pallete(fd, pallete, 16);
246 lseek(fd, raster, SEEK_SET);
247 unsigned char * tbuffer = new unsigned char[*x / 2 + 1];
250 for (int i = 0; i < *y; i++)
252 read(fd, tbuffer, (*x) / 2 + *x % 2);
254 for (j = 0; j < (*x) / 2; j++)
256 unsigned char c1 = tbuffer[j] >> 4;
257 unsigned char c2 = tbuffer[j] & 0x0f;
258 *wr_buffer++ = pallete[c1].red;
259 *wr_buffer++ = pallete[c1].green;
260 *wr_buffer++ = pallete[c1].blue;
261 *wr_buffer++ = pallete[c2].red;
262 *wr_buffer++ = pallete[c2].green;
263 *wr_buffer++ = pallete[c2].blue;
267 unsigned char c1 = tbuffer[j] >> 4;
268 *wr_buffer++ = pallete[c1].red;
269 *wr_buffer++ = pallete[c1].green;
270 *wr_buffer++ = pallete[c1].blue;
273 read(fd, buff, skip);
274 wr_buffer -= (*x) * 6;
280 int skip = fill4B(*x);
281 fetch_pallete(fd, pallete, 256);
282 lseek(fd, raster, SEEK_SET);
283 unsigned char * tbuffer = new unsigned char[*x];
286 for (int i = 0; i < *y; i++)
288 read(fd, tbuffer, *x);
289 for (int j = 0; j < *x; j++)
291 wr_buffer[j * 3] = pallete[tbuffer[j]].red;
292 wr_buffer[j * 3 + 1] = pallete[tbuffer[j]].green;
293 wr_buffer[j * 3 + 2] = pallete[tbuffer[j]].blue;
296 read(fd, buff, skip);
297 wr_buffer -= (*x) * 3;
303 int skip = fill4B((*x) * 3);
304 lseek(fd, raster, SEEK_SET);
305 for (int i = 0; i < (*y); i++)
307 read(fd, wr_buffer, (*x) * 3);
308 for (int j = 0; j < (*x) * 3 ; j = j + 3)
310 unsigned char c = wr_buffer[j];
311 wr_buffer[j] = wr_buffer[j + 2];
312 wr_buffer[j + 2] = c;
315 read(fd, buff, skip);
316 wr_buffer -= (*x) * 3;
328 //---------------------------------------------------------------------------------------------
330 static int png_load(const char *filename, int *x, int *y)
332 static const png_color_16 my_background = {0, 0, 0, 0, 0};
336 png_uint_32 width, height;
338 int bit_depth, color_type, interlace_type;
339 int number_passes, pass;
343 if (!(fh = fopen(filename, "rb"))) return 0;
345 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
348 info_ptr = png_create_info_struct(png_ptr);
349 if (info_ptr == NULL)
351 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
356 if (setjmp(png_ptr->jmpbuf))
358 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
363 png_init_io(png_ptr, fh);
365 png_read_info(png_ptr, info_ptr);
366 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
368 if (color_type == PNG_COLOR_TYPE_PALETTE)
370 png_set_palette_to_rgb(png_ptr);
371 png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
374 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
376 png_set_gray_to_rgb(png_ptr);
377 png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
380 if (color_type & PNG_COLOR_MASK_ALPHA)
381 png_set_strip_alpha(png_ptr);
383 if (bit_depth < 8) png_set_packing(png_ptr);
384 if (bit_depth == 16) png_set_strip_16(png_ptr);
386 number_passes = png_set_interlace_handling(png_ptr);
387 png_read_update_info(png_ptr, info_ptr);
389 if (width * 3 != png_get_rowbytes(png_ptr, info_ptr))
391 eDebug("[PNG] Error processing");
395 if (width * height > 1000000) // 1000x1000 or equiv.
397 eDebug("[png_load] image size is %d x %d, which is \"too large\".", width, height);
398 png_read_end(png_ptr, info_ptr);
399 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
404 pic_buffer = new unsigned char[width * height * 3];
408 for(pass = 0; pass < number_passes; pass++)
410 fbptr = (png_byte *)pic_buffer;
411 for (i = 0; i < height; i++, fbptr += width * 3)
412 png_read_row(png_ptr, fbptr, NULL);
414 png_read_end(png_ptr, info_ptr);
415 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
420 //-------------------------------------------------------------------
422 inline void m_rend_gif_decodecolormap(unsigned char *cmb, unsigned char *rgbb, ColorMapObject *cm, int s, int l)
424 GifColorType *cmentry;
426 for (i = 0; i < l; i++)
428 cmentry = &cm->Colors[cmb[i]];
429 *(rgbb++) = cmentry->Red;
430 *(rgbb++) = cmentry->Green;
431 *(rgbb++) = cmentry->Blue;
435 static int gif_load(const char *filename, int *x, int *y)
437 int px, py, i, j, ibxs;
438 unsigned char *fbptr;
439 unsigned char *lb=NULL;
440 unsigned char *slb=NULL;
443 GifByteType *extension;
444 ColorMapObject *cmap;
448 gft = DGifOpenFileName(filename);
453 if (DGifGetRecordType(gft, &rt) == GIF_ERROR)
457 case IMAGE_DESC_RECORD_TYPE:
458 if (DGifGetImageDesc(gft) == GIF_ERROR)
460 *x = px = gft->Image.Width;
461 *y = py = gft->Image.Height;
462 pic_buffer = new unsigned char[px * py * 3];
463 lb = (unsigned char *)malloc(px * 3);
464 slb = (unsigned char *) malloc(px);
466 if (lb != NULL && slb != NULL)
468 cmap = (gft->Image.ColorMap ? gft->Image.ColorMap : gft->SColorMap);
469 cmaps = cmap->ColorCount;
473 if (!(gft->Image.Interlace))
475 for (i = 0; i < py; i++, fbptr += px * 3)
477 if (DGifGetLine(gft, slb, px) == GIF_ERROR)
479 m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
480 memcpy(fbptr, lb, px * 3);
485 for (j = 0; j < 4; j++)
488 for (i = 0; i < py; i++, fbptr += px * 3)
490 if (DGifGetLine(gft, slb, px) == GIF_ERROR)
492 m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
493 memcpy(fbptr, lb, px * 3);
509 case EXTENSION_RECORD_TYPE:
510 if (DGifGetExtension(gft, &extcode, &extension) == GIF_ERROR)
512 while (extension != NULL)
513 if (DGifGetExtensionNext(gft, &extension) == GIF_ERROR)
520 while (rt != TERMINATE_RECORD_TYPE);
525 eDebug("[GIF] Error");
532 //---------------------------------------------------------------------------------------------
534 PyObject *getExif(const char *filename)
538 if(exif.DecodeExif(filename))
540 if(exif.m_exifinfo->IsExif)
544 list = PyList_New(22);
545 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->Version));
546 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->CameraMake));
547 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->CameraModel));
548 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->DateTime));
549 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->Comments));
550 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d x %d", exif.m_exifinfo->Width, exif.m_exifinfo->Height));
551 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->Orientation));
552 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->MeteringMode));
553 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->ExposureProgram));
554 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->LightSource));
555 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->FlashUsed));
556 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d", exif.m_exifinfo->CompressionLevel));
557 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d", exif.m_exifinfo->ISOequivalent));
558 sprintf(tmp, "%.2f", exif.m_exifinfo->Xresolution);
559 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
560 sprintf(tmp, "%.2f", exif.m_exifinfo->Yresolution);
561 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
562 PyList_SET_ITEM(list, pos++, PyString_FromString(exif.m_exifinfo->ResolutionUnit));
563 sprintf(tmp, "%.2f", exif.m_exifinfo->Brightness);
564 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
565 sprintf(tmp, "%.5f sec.", exif.m_exifinfo->ExposureTime);
566 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
567 sprintf(tmp, "%.5f", exif.m_exifinfo->ExposureBias);
568 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
569 sprintf(tmp, "%.5f", exif.m_exifinfo->Distance);
570 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
571 sprintf(tmp, "%.5f", exif.m_exifinfo->CCDWidth);
572 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
573 sprintf(tmp, "%.2f", exif.m_exifinfo->ApertureFNumber);
574 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
578 list = PyList_New(1);
579 PyList_SET_ITEM(list, 0, PyString_FromString(exif.m_szLastError));
585 list = PyList_New(1);
586 PyList_SET_ITEM(list, 0, PyString_FromString(exif.m_szLastError));
589 return list ? (PyObject*)list : (PyObject*)PyList_New(0);
592 //---------------------------------------------------------------------------------------------
593 enum {F_NONE, F_PNG, F_JPEG, F_BMP, F_GIF};
595 static int pic_id(const char *name)
597 unsigned char id[10];
598 int fd = open(name, O_RDONLY);
604 if(id[1] == 'P' && id[2] == 'N' && id[3] == 'G')
606 else if(id[6] == 'J' && id[7] == 'F' && id[8] == 'I' && id[9] == 'F')
608 else if(id[0] == 0xff && id[1] == 0xd8 && id[2] == 0xff)
610 else if(id[0] == 'B' && id[1] == 'M' )
612 else if(id[0] == 'G' && id[1] == 'I' && id[2] == 'F')
617 int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspect, int resize_mode, int rotate, int background, std::string cachefile)
620 int ox=0, oy=0, imx, imy;
624 if(cachefile.length())
627 if(jpeg_load(cachefile.c_str(), &ox, &oy))
628 eDebug("[CACHEPIC] x-size=%d, y-size=%d", ox, oy);
633 switch(pic_id(filename.c_str()))
635 case F_PNG: png_load(filename.c_str(), &ox, &oy); break;
636 case F_JPEG: jpeg_load(filename.c_str(), &ox, &oy); break;
637 case F_BMP: bmp_load(filename.c_str(), &ox, &oy); break;
638 case F_GIF: gif_load(filename.c_str(), &ox, &oy); break;
640 eDebug("[PIC] <format not supportet>");
644 eDebug("[FULLPIC] x-size=%d, y-size=%d", ox, oy);
652 case 1: aspect_ratio = 1.778 / ((double)702/576); break; //16:9
653 case 2: aspect_ratio = 1.600 / ((double)702/576); break; //16:10
654 //case 3: aspect_ratio = 1.250 / ((double)702/576); break; //5:4
655 default: aspect_ratio = 1.333 / ((double)702/576); //4:3
658 if((aspect_ratio * oy * w / ox) <= h)
661 imy = (int)(aspect_ratio*oy*w/ox);
665 imx = (int)((1.0/aspect_ratio)*ox*h/oy);
669 if(resize_mode) pic_buffer = color_resize(pic_buffer, ox, oy, imx, imy);
670 else pic_buffer = simple_resize(pic_buffer, ox, oy, imx, imy);
677 jpeg_save(pic_buffer, cachefile.c_str(), 50, oy, ox);
678 eDebug("[SAVEPIC] x-size=%d, y-size=%d", ox, oy);
684 result=new gPixmap(eSize(w, h), 32);
685 gSurface *surface = result->surface;
688 int o_y=0, u_y=0, v_x=0, h_x=0;
689 unsigned char clear[4] = {0x00,0x00,0x00,0x00};
690 if(background) clear[3]=0xFF;
691 unsigned char *tmp_buffer = new unsigned char[4];
704 //eDebug("o_y=%d u_y=%d v_x=%d h_x=%d", o_y, u_y, v_x, h_x);
707 for(a=0; a<(o_y*ox)+1; a++, nc+=4)
709 memcpy(tmp_buffer, clear, sizeof(clear));
710 tmp_buffer=((unsigned char *)(surface->data)) + nc;
716 for(b=0; b<v_x; b++, nc+=4)
718 memcpy(tmp_buffer, clear, sizeof(clear));
719 tmp_buffer=((unsigned char *)(surface->data)) + nc;
722 for(b=0; b<(ox*3); b+=3, nc+=4)
725 tmp_buffer[2]=pic_buffer[oc++];
726 tmp_buffer[1]=pic_buffer[oc++];
727 tmp_buffer[0]=pic_buffer[oc++];
729 tmp_buffer=((unsigned char *)(surface->data)) + nc;
733 for(b=0; b<h_x; b++, nc+=4)
735 memcpy(tmp_buffer, clear, sizeof(clear));
736 tmp_buffer=((unsigned char *)(surface->data)) + nc;
741 for(a=0; a<(u_y*ox)+1; a++, nc+=4)
743 memcpy(tmp_buffer, clear, sizeof(clear));
744 tmp_buffer=((unsigned char *)(surface->data)) + nc;
747 //eDebug("[PIC] buffer=%d, nc=%d oc=%d ox=%d, oy=%d",w*h*4, nc, oc, ox, oy);
749 surface->clut.data=0;
750 surface->clut.colors=0;
751 surface->clut.start=0;
753 delete [] pic_buffer;