13 //#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)
199 static int bmp_load(const char *filename, int *x, int *y)
201 unsigned char buff[4];
203 int fd = open(filename, O_RDONLY);
204 if (fd == -1) return 0;
205 if (lseek(fd, BMP_SIZE_OFFSET, SEEK_SET) == -1) return 0;
207 *x = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
209 *y = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
210 if (lseek(fd, BMP_TORASTER_OFFSET, SEEK_SET) == -1) return 0;
212 int raster = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
213 if (lseek(fd, BMP_BPP_OFFSET, SEEK_SET) == -1) return 0;
215 int bpp = buff[0] + (buff[1] << 8);
217 //printf("x=%d, y=%d,bpp=%d\n",*x, *y, bpp);
218 pic_buffer = new unsigned char[(*x) * (*y) * 3];
219 unsigned char *wr_buffer = pic_buffer + (*x) * ((*y) - 1) * 3;
225 int skip = fill4B((*x) * 3);
226 lseek(fd, raster, SEEK_SET);
228 for (int i = 0; i < (*y); i++)
230 read(fd, wr_buffer, (*x) * 3);
231 for (int j = 0; j < (*x) * 3 ; j = j + 3)
234 wr_buffer[j] = wr_buffer[j + 2];
235 wr_buffer[j + 2] = c;
238 read(fd, buff, skip);
239 wr_buffer -= (*x) * 3;
251 //---------------------------------------------------------------------------------------------
253 static int png_load(const char *filename, int *x, int *y)
255 static const png_color_16 my_background = {0, 0, 0, 0, 0};
259 png_uint_32 width, height;
261 int bit_depth, color_type, interlace_type;
262 int number_passes, pass;
266 if (!(fh = fopen(filename, "rb"))) return 0;
268 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
271 info_ptr = png_create_info_struct(png_ptr);
272 if (info_ptr == NULL)
274 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
279 if (setjmp(png_ptr->jmpbuf))
281 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
286 png_init_io(png_ptr, fh);
288 png_read_info(png_ptr, info_ptr);
289 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
291 if (color_type == PNG_COLOR_TYPE_PALETTE)
293 png_set_palette_to_rgb(png_ptr);
294 png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
297 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
299 png_set_gray_to_rgb(png_ptr);
300 png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
303 if (color_type & PNG_COLOR_MASK_ALPHA)
304 png_set_strip_alpha(png_ptr);
306 if (bit_depth < 8) png_set_packing(png_ptr);
307 if (bit_depth == 16) png_set_strip_16(png_ptr);
309 number_passes = png_set_interlace_handling(png_ptr);
310 png_read_update_info(png_ptr, info_ptr);
312 if (width * 3 != png_get_rowbytes(png_ptr, info_ptr))
314 eDebug("[PNG] Error processing");
318 pic_buffer = new unsigned char[width * height * 3];
322 for(pass = 0; pass < number_passes; pass++)
324 fbptr = (png_byte *)pic_buffer;
325 for (i = 0; i < height; i++, fbptr += width * 3)
326 png_read_row(png_ptr, fbptr, NULL);
328 png_read_end(png_ptr, info_ptr);
329 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
334 //---------------------------------------------------------------------------------------------
336 PyObject *getExif(const char *filename)
339 Cexif *m_exif = new Cexif();
340 if(m_exif->DecodeExif(filename))
342 if(m_exif->m_exifinfo->IsExif)
346 list = PyList_New(22);
347 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->Version));
348 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->CameraMake));
349 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->CameraModel));
350 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->DateTime));
351 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->Comments));
352 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d x %d", m_exif->m_exifinfo->Width, m_exif->m_exifinfo->Height));
353 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->Orientation));
354 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->MeteringMode));
355 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->ExposureProgram));
356 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->LightSource));
357 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->FlashUsed));
358 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d", m_exif->m_exifinfo->CompressionLevel));
359 PyList_SET_ITEM(list, pos++, PyString_FromFormat("%d", m_exif->m_exifinfo->ISOequivalent));
360 sprintf(tmp, "%.2f", m_exif->m_exifinfo->Xresolution);
361 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
362 sprintf(tmp, "%.2f", m_exif->m_exifinfo->Yresolution);
363 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
364 PyList_SET_ITEM(list, pos++, PyString_FromString(m_exif->m_exifinfo->ResolutionUnit));
365 sprintf(tmp, "%.2f", m_exif->m_exifinfo->Brightness);
366 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
367 sprintf(tmp, "%.5f sec.", m_exif->m_exifinfo->ExposureTime);
368 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
369 sprintf(tmp, "%.5f", m_exif->m_exifinfo->ExposureBias);
370 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
371 sprintf(tmp, "%.5f", m_exif->m_exifinfo->Distance);
372 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
373 sprintf(tmp, "%.5f", m_exif->m_exifinfo->CCDWidth);
374 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
375 sprintf(tmp, "%.2f", m_exif->m_exifinfo->ApertureFNumber);
376 PyList_SET_ITEM(list, pos++, PyString_FromString(tmp));
380 list = PyList_New(1);
381 PyList_SET_ITEM(list, 0, PyString_FromString(m_exif->m_szLastError));
387 list = PyList_New(1);
388 PyList_SET_ITEM(list, 0, PyString_FromString(m_exif->m_szLastError));
393 return list ? list : PyList_New(0);
396 //---------------------------------------------------------------------------------------------
398 int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspect, int resize_mode, int rotate, int background, std::string cachefile)
401 int ox=0, oy=0, imx, imy;
405 if(cachefile.length())
408 if(jpeg_load(cachefile.c_str(), &ox, &oy))
409 eDebug("[CACHEPIC] x-size=%d, y-size=%d", ox, oy);
414 unsigned int pos = filename.find_last_of(".");
415 if (pos == std::string::npos)
416 pos = filename.length() - 1;
417 std::string ext = filename.substr(pos);
418 std::transform(ext.begin(), ext.end(), ext.begin(), (int(*)(int)) toupper);
419 if(ext == ".JPEG" || ext == ".JPG")
420 jpeg_load(filename.c_str(), &ox, &oy);
421 else if(ext == ".BMP")
422 bmp_load(filename.c_str(), &ox, &oy);
423 else if(ext == ".PNG")
424 png_load(filename.c_str(), &ox, &oy);
427 eDebug("[PIC] <format not supportet>");
431 eDebug("[FULLPIC] x-size=%d, y-size=%d", ox, oy);
439 case 1: aspect_ratio = 1.777 / ((double)720/576); break; //16:9
440 case 2: aspect_ratio = 1.600 / ((double)720/576); break; //16:10
441 //case 3: aspect_ratio = 1.250 / ((double)720/576); break; //5:4
442 default: aspect_ratio = 1.333 / ((double)720/576); //4:3
445 if((aspect_ratio * oy * w / ox) <= h)
448 imy = (int)(aspect_ratio*oy*w/ox);
452 imx = (int)((1.0/aspect_ratio)*ox*h/oy);
456 if(resize_mode) pic_buffer = color_resize(pic_buffer, ox, oy, imx, imy);
457 else pic_buffer = simple_resize(pic_buffer, ox, oy, imx, imy);
464 jpeg_save(pic_buffer, cachefile.c_str(), 50, oy, ox);
465 eDebug("[SAVEPIC] x-size=%d, y-size=%d", ox, oy);
471 result=new gPixmap(eSize(w, h), 32);
472 gSurface *surface = result->surface;
475 int o_y=0, u_y=0, v_x=0, h_x=0;
476 unsigned char clear[4] = {0x00,0x00,0x00,0x00};
477 if(background) clear[3]=0xFF;
478 unsigned char *tmp_buffer = new unsigned char[4];
491 //eDebug("o_y=%d u_y=%d v_x=%d h_x=%d", o_y, u_y, v_x, h_x);
494 for(a=0; a<(o_y*ox)+1; a++, nc+=4)
496 memcpy(tmp_buffer, clear, sizeof(clear));
497 tmp_buffer=((unsigned char *)(surface->data)) + nc;
503 for(b=0; b<v_x; b++, nc+=4)
505 memcpy(tmp_buffer, clear, sizeof(clear));
506 tmp_buffer=((unsigned char *)(surface->data)) + nc;
509 for(b=0; b<(ox*3); b+=3, nc+=4)
512 tmp_buffer[2]=pic_buffer[oc++];
513 tmp_buffer[1]=pic_buffer[oc++];
514 tmp_buffer[0]=pic_buffer[oc++];
516 tmp_buffer=((unsigned char *)(surface->data)) + nc;
520 for(b=0; b<h_x; b++, nc+=4)
522 memcpy(tmp_buffer, clear, sizeof(clear));
523 tmp_buffer=((unsigned char *)(surface->data)) + nc;
528 for(a=0; a<(u_y*ox)+1; a++, nc+=4)
530 memcpy(tmp_buffer, clear, sizeof(clear));
531 tmp_buffer=((unsigned char *)(surface->data)) + nc;
534 //eDebug("[PIC] buffer=%d, nc=%d oc=%d ox=%d, oy=%d",w*h*4, nc, oc, ox, oy);
536 surface->clut.data=0;
537 surface->clut.colors=0;
538 surface->clut.start=0;
540 delete [] pic_buffer;