fix crash when ci state of cislot > 0 changed
[enigma2.git] / lib / gdi / picload.cpp
1 #include <lib/gdi/picload.h>
2 #include "picexif.h"
3 #include <lib/python/python.h>
4
5 #include <png.h>
6
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 extern "C" {
13 #include <jpeglib.h>
14 #include <gif_lib.h>
15 //#include "transupp.h"
16 }
17 #include <setjmp.h>
18
19 unsigned char *pic_buffer=NULL;
20
21 static unsigned char *simple_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
22 {
23         unsigned char *cr, *p, *l;
24         int i, j, k, ip;
25         cr = new unsigned char[dx * dy * 3]; 
26         if (cr == NULL)
27         {
28                 printf("[RESIZE] Error: malloc\n");
29                 return(orgin);
30         }
31         l = cr;
32
33         for (j = 0; j < dy; j++,l += dx * 3)
34         {
35                 p = orgin + (j * oy / dy * ox * 3);
36                 for (i = 0, k = 0; i < dx; i++, k += 3)
37                 {
38                         ip = i * ox / dx * 3;
39                         l[k] = p[ip];
40                         l[k+1] = p[ip + 1];
41                         l[k+2] = p[ip + 2];
42                 }
43         }
44         delete [] orgin;
45         return(cr);
46 }
47
48 static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
49 {
50         unsigned char *cr, *p, *q;
51         int i, j, k, l, xa, xb, ya, yb;
52         int sq, r, g, b;
53         cr = new unsigned char[dx * dy * 3];
54         if (cr == NULL)
55         {
56                 printf("[RESIZE] Error: malloc\n");
57                 return(orgin);
58         }
59         p = cr;
60
61         for (j = 0; j < dy; j++)
62         {
63                 for (i = 0; i < dx; i++, p += 3)
64                 {
65                         xa = i * ox / dx;
66                         ya = j * oy / dy;
67                         xb = (i + 1) * ox / dx; 
68                         if (xb >= ox)
69                                 xb = ox - 1;
70                         yb = (j + 1) * oy / dy; 
71                         if (yb >= oy)
72                                 yb = oy - 1;
73                         for (l = ya, r = 0, g = 0, b = 0, sq = 0; l <= yb; l++)
74                         {
75                                 q = orgin + ((l * ox + xa) * 3);
76                                 for (k = xa; k <= xb; k++, q += 3, sq++)
77                                 {
78                                         r += q[0]; g += q[1]; b += q[2];
79                                 }
80                         }
81                         p[0] = r / sq; p[1] = g / sq; p[2] = b / sq;
82                 }
83         }
84         delete [] orgin;
85         return(cr);
86 }
87
88 //-------------------------------------------------------------------
89
90 struct r_jpeg_error_mgr
91 {
92         struct jpeg_error_mgr pub;
93         jmp_buf envbuffer;
94 };
95
96 void jpeg_cb_error_exit(j_common_ptr cinfo)
97 {
98         struct r_jpeg_error_mgr *mptr;
99         mptr = (struct r_jpeg_error_mgr *) cinfo->err;
100         (*cinfo->err->output_message) (cinfo);
101         longjmp(mptr->envbuffer, 1);
102 }
103
104 static int jpeg_save(unsigned char *image_buffer, const char * filename, int quality, int image_height, int image_width)
105 {
106         struct jpeg_compress_struct cinfo;
107         struct jpeg_error_mgr jerr;
108         FILE * outfile;         /* target file */
109         JSAMPROW row_pointer[1];/* pointer to JSAMPLE row[s] */
110         int row_stride;         /* physical row width in image buffer */
111  
112         cinfo.err = jpeg_std_error(&jerr);
113         jpeg_create_compress(&cinfo);
114  
115         if ((outfile = fopen(filename, "wb")) == NULL) 
116         {
117                 eDebug("[JPEG] can't open %s", filename);
118                 return -1;
119         }
120         jpeg_stdio_dest(&cinfo, outfile);
121  
122         cinfo.image_width = image_width;
123         cinfo.image_height = image_height;
124         cinfo.input_components = 3;
125         cinfo.in_color_space = JCS_RGB;
126         jpeg_set_defaults(&cinfo);
127         jpeg_set_quality(&cinfo, quality, TRUE );
128         jpeg_start_compress(&cinfo, TRUE);
129         row_stride = image_width * 3;
130         while (cinfo.next_scanline < cinfo.image_height) 
131         {
132                 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
133                 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
134         }
135         jpeg_finish_compress(&cinfo);
136         fclose(outfile);
137         jpeg_destroy_compress(&cinfo);
138         return 0;
139 }
140
141
142 static int jpeg_load(const char *filename, int *x, int *y)
143 {
144         struct jpeg_decompress_struct cinfo;
145         struct jpeg_decompress_struct *ciptr = &cinfo;
146         struct r_jpeg_error_mgr emgr;
147         FILE *fh;
148
149         if (!(fh = fopen(filename, "rb")))
150                 return 0;
151
152         ciptr->err = jpeg_std_error(&emgr.pub);
153         emgr.pub.error_exit = jpeg_cb_error_exit;
154         if (setjmp(emgr.envbuffer) == 1)
155         {
156                 jpeg_destroy_decompress(ciptr);
157                 fclose(fh);
158                 return 0;
159         }
160
161         jpeg_create_decompress(ciptr);
162         jpeg_stdio_src(ciptr, fh);
163         jpeg_read_header(ciptr, TRUE);
164         ciptr->out_color_space = JCS_RGB;
165         ciptr->scale_denom = 1;
166
167         jpeg_start_decompress(ciptr);
168         
169         *x=ciptr->output_width;
170         *y=ciptr->output_height;
171
172         if(ciptr->output_components == 3)
173         {
174                 JSAMPLE *lb = (JSAMPLE *)(*ciptr->mem->alloc_small)((j_common_ptr) ciptr, JPOOL_PERMANENT, ciptr->output_width * ciptr->output_components);
175                 pic_buffer = new unsigned char[ciptr->output_height * ciptr->output_width * ciptr->output_components];
176                 unsigned char *bp = pic_buffer;
177
178                 while (ciptr->output_scanline < ciptr->output_height)
179                 {
180                         jpeg_read_scanlines(ciptr, &lb, 1);
181                         memcpy(bp, lb, ciptr->output_width * ciptr->output_components);
182                         bp += ciptr->output_width * ciptr->output_components;
183                 }
184         }
185         jpeg_finish_decompress(ciptr);
186         jpeg_destroy_decompress(ciptr);
187         fclose(fh);
188         return 1;
189 }
190
191 //---------------------------------------------------------------------------------------------
192
193 #define BMP_TORASTER_OFFSET 10
194 #define BMP_SIZE_OFFSET 18
195 #define BMP_BPP_OFFSET 28
196 #define BMP_RLE_OFFSET 30
197 #define BMP_COLOR_OFFSET 54
198
199 #define fill4B(a) ((4 - ((a) % 4 )) & 0x03)
200
201 struct color {
202         unsigned char red;
203         unsigned char green;
204         unsigned char blue;
205 };
206
207 static void fetch_pallete(int fd, struct color pallete[], int count)
208 {
209         unsigned char buff[4];
210         lseek(fd, BMP_COLOR_OFFSET, SEEK_SET);
211         for (int i = 0; i < count; i++)
212         {
213                 read(fd, buff, 4);
214                 pallete[i].red = buff[2];
215                 pallete[i].green = buff[1];
216                 pallete[i].blue = buff[0];
217         }
218 }
219
220 static int bmp_load(const char *filename,  int *x, int *y)
221 {
222         unsigned char buff[4];
223         struct color pallete[256];
224
225         int fd = open(filename, O_RDONLY);
226         if (fd == -1) return 0;
227         if (lseek(fd, BMP_SIZE_OFFSET, SEEK_SET) == -1) return 0;
228         read(fd, buff, 4);
229         *x = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
230         read(fd, buff, 4);
231         *y = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
232         if (lseek(fd, BMP_TORASTER_OFFSET, SEEK_SET) == -1) return 0;
233         read(fd, buff, 4);
234         int raster = buff[0] + (buff[1] << 8) + (buff[2] << 16) + (buff[3] << 24);
235         if (lseek(fd, BMP_BPP_OFFSET, SEEK_SET) == -1) return 0;
236         read(fd, buff, 2);
237         int bpp = buff[0] + (buff[1] << 8);
238
239         pic_buffer = new unsigned char[(*x) * (*y) * 3];
240         unsigned char *wr_buffer = pic_buffer + (*x) * ((*y) - 1) * 3;
241         
242         switch (bpp)
243         {
244                 case 4:
245                 {
246                         int skip = fill4B((*x) / 2 + (*x) % 2);
247                         fetch_pallete(fd, pallete, 16);
248                         lseek(fd, raster, SEEK_SET);
249                         unsigned char * tbuffer = new unsigned char[*x / 2 + 1];
250                         if (tbuffer == NULL)
251                                 return 0;
252                         for (int i = 0; i < *y; i++) 
253                         {
254                                 read(fd, tbuffer, (*x) / 2 + *x % 2);
255                                 int j;
256                                 for (j = 0; j < (*x) / 2; j++)
257                                 {
258                                         unsigned char c1 = tbuffer[j] >> 4;
259                                         unsigned char c2 = tbuffer[j] & 0x0f;
260                                         *wr_buffer++ = pallete[c1].red;
261                                         *wr_buffer++ = pallete[c1].green;
262                                         *wr_buffer++ = pallete[c1].blue;
263                                         *wr_buffer++ = pallete[c2].red;
264                                         *wr_buffer++ = pallete[c2].green;
265                                         *wr_buffer++ = pallete[c2].blue;
266                                 }
267                                 if ((*x) % 2)
268                                 {
269                                         unsigned char c1 = tbuffer[j] >> 4;
270                                         *wr_buffer++ = pallete[c1].red;
271                                         *wr_buffer++ = pallete[c1].green;
272                                         *wr_buffer++ = pallete[c1].blue;
273                                 }
274                                 if (skip)
275                                         read(fd, buff, skip);
276                                 wr_buffer -= (*x) * 6;
277                         }
278                         break;
279                 }
280                 case 8:
281                 {
282                         int skip = fill4B(*x);
283                         fetch_pallete(fd, pallete, 256);
284                         lseek(fd, raster, SEEK_SET);
285                         unsigned char * tbuffer = new unsigned char[*x];
286                         if (tbuffer == NULL)
287                                 return 0;
288                         for (int i = 0; i < *y; i++)
289                         {
290                                 read(fd, tbuffer, *x);
291                                 for (int j = 0; j < *x; j++)
292                                 {
293                                         wr_buffer[j * 3] = pallete[tbuffer[j]].red;
294                                         wr_buffer[j * 3 + 1] = pallete[tbuffer[j]].green;
295                                         wr_buffer[j * 3 + 2] = pallete[tbuffer[j]].blue;
296                                 }
297                                 if (skip)
298                                         read(fd, buff, skip);
299                                 wr_buffer -= (*x) * 3;
300                         }
301                         break;
302                 }
303                 case 24:
304                 {
305                         int skip = fill4B((*x) * 3);
306                         lseek(fd, raster, SEEK_SET);
307                         for (int i = 0; i < (*y); i++)
308                         {
309                                 read(fd, wr_buffer, (*x) * 3);
310                                 for (int j = 0; j < (*x) * 3 ; j = j + 3)
311                                 {
312                                         unsigned char c = wr_buffer[j];
313                                         wr_buffer[j] = wr_buffer[j + 2];
314                                         wr_buffer[j + 2] = c;
315                                 }
316                                 if (skip)
317                                         read(fd, buff, skip);
318                                 wr_buffer -= (*x) * 3;
319                         }
320                         break;
321                 }
322                 default:
323                         return 0;
324         }
325
326         close(fd);
327         return 1;
328 }
329
330 //---------------------------------------------------------------------------------------------
331
332 static int png_load(const char *filename,  int *x, int *y)
333 {
334         static const png_color_16 my_background = {0, 0, 0, 0, 0};
335
336         png_structp png_ptr;
337         png_infop info_ptr;
338         png_uint_32 width, height;
339         unsigned int i;
340         int bit_depth, color_type, interlace_type;
341         int number_passes, pass;
342         png_byte * fbptr;
343         FILE * fh;
344
345         if (!(fh = fopen(filename, "rb"))) return 0;
346
347         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
348         if (png_ptr == NULL)
349                 return 0;
350         info_ptr = png_create_info_struct(png_ptr);
351         if (info_ptr == NULL)
352         {
353                 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
354                 fclose(fh); 
355                 return 0;
356         }
357
358         if (setjmp(png_ptr->jmpbuf))
359         {
360                 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
361                 fclose(fh); 
362                 return 0;
363         }
364
365         png_init_io(png_ptr, fh);
366
367         png_read_info(png_ptr, info_ptr);
368         png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
369
370         if (color_type == PNG_COLOR_TYPE_PALETTE)
371         {
372                 png_set_palette_to_rgb(png_ptr);
373                 png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
374         }
375
376         if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
377         {
378                 png_set_gray_to_rgb(png_ptr);
379                 png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
380         }
381
382         if (color_type & PNG_COLOR_MASK_ALPHA)
383                 png_set_strip_alpha(png_ptr);
384
385         if (bit_depth < 8)      png_set_packing(png_ptr);
386         if (bit_depth == 16)    png_set_strip_16(png_ptr);
387
388         number_passes = png_set_interlace_handling(png_ptr);
389         png_read_update_info(png_ptr, info_ptr);
390
391         if (width * 3 != png_get_rowbytes(png_ptr, info_ptr))
392         {
393                 eDebug("[PNG] Error processing");
394                 return 0;
395         }
396         
397         pic_buffer = new unsigned char[width * height * 3];
398         *x=width;
399         *y=height;
400
401         for(pass = 0; pass < number_passes; pass++)
402         {
403                 fbptr = (png_byte *)pic_buffer;
404                 for (i = 0; i < height; i++, fbptr += width * 3)
405                         png_read_row(png_ptr, fbptr, NULL);
406         }
407         png_read_end(png_ptr, info_ptr);
408         png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
409         fclose(fh);
410         return 1;
411 }
412
413 //-------------------------------------------------------------------
414
415 inline void m_rend_gif_decodecolormap(unsigned char *cmb, unsigned char *rgbb, ColorMapObject *cm, int s, int l)
416 {
417         GifColorType *cmentry;
418         int i;
419         for (i = 0; i < l; i++)
420         {
421                 cmentry = &cm->Colors[cmb[i]];
422                 *(rgbb++) = cmentry->Red;
423                 *(rgbb++) = cmentry->Green;
424                 *(rgbb++) = cmentry->Blue;
425         }
426 }
427
428 static int gif_load(const char *filename, int *x, int *y)
429 {
430         int px, py, i, j, ibxs;
431         unsigned char *fbptr;
432         unsigned char *lb=NULL;
433         unsigned char *slb=NULL;
434         GifFileType *gft;
435         GifRecordType rt;
436         GifByteType *extension;
437         ColorMapObject *cmap;
438         int cmaps;
439         int extcode;
440         
441         gft = DGifOpenFileName(filename);
442         if (gft == NULL) 
443                 return 0;
444         do
445         {
446                 if (DGifGetRecordType(gft, &rt) == GIF_ERROR)
447                         goto ERROR_R;
448                 switch(rt)
449                 {
450                         case IMAGE_DESC_RECORD_TYPE:
451                                 if (DGifGetImageDesc(gft) == GIF_ERROR)
452                                         goto ERROR_R;
453                                 *x = px = gft->Image.Width;
454                                 *y = py = gft->Image.Height;
455                                 pic_buffer = new unsigned char[px * py * 3];
456                                 lb = (unsigned char *)malloc(px * 3);
457                                 slb = (unsigned char *) malloc(px);
458
459                                 if (lb != NULL && slb != NULL)
460                                 {
461                                         cmap = (gft->Image.ColorMap ? gft->Image.ColorMap : gft->SColorMap);
462                                         cmaps = cmap->ColorCount;
463
464                                         ibxs = ibxs * 3;
465                                         fbptr = pic_buffer;
466                                         if (!(gft->Image.Interlace))
467                                         {
468                                                 for (i = 0; i < py; i++, fbptr += px * 3)
469                                                 {
470                                                         if (DGifGetLine(gft, slb, px) == GIF_ERROR)
471                                                                 goto ERROR_R;
472                                                         m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
473                                                         memcpy(fbptr, lb, px * 3);
474                                                 }
475                                         }
476                                         else
477                                         {
478                                                 for (j = 0; j < 4; j++)
479                                                 {
480                                                         fbptr = pic_buffer;
481                                                         for (i = 0; i < py; i++, fbptr += px * 3)
482                                                         {
483                                                                 if (DGifGetLine(gft, slb, px) == GIF_ERROR)
484                                                                         goto ERROR_R;
485                                                                 m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
486                                                                 memcpy(fbptr, lb, px * 3);
487                                                         }
488                                                 }
489                                         }
490                                 }
491                                 if (lb)
492                                 {
493                                         free(lb);
494                                         lb=NULL;
495                                 }
496                                 if (slb)
497                                 {
498                                         free(slb);
499                                         slb=NULL;
500                                 }
501                                 break;
502                         case EXTENSION_RECORD_TYPE:
503                                 if (DGifGetExtension(gft, &extcode, &extension) == GIF_ERROR)
504                                         goto ERROR_R;
505                                 while (extension != NULL)
506                                         if (DGifGetExtensionNext(gft, &extension) == GIF_ERROR)
507                                                 goto ERROR_R;
508                                 break;
509                         default:
510                                 break;
511                 }
512         }
513         while (rt != TERMINATE_RECORD_TYPE);
514
515         DGifCloseFile(gft);
516         return 1;
517 ERROR_R:
518         eDebug("[GIF] Error");
519         if (lb)         free(lb);
520         if (slb)        free(slb);
521         DGifCloseFile(gft);
522         return 0;
523 }
524
525 //---------------------------------------------------------------------------------------------
526
527 PyObject *getExif(const char *filename)
528 {
529         ePyObject list;
530         Cexif exif;
531         if(exif.DecodeExif(filename))
532         {
533                 if(exif.m_exifinfo->IsExif)
534                 {
535                         int pos=0;
536                         char tmp[256];
537                         list = PyList_New(22);
538                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->Version));
539                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->CameraMake));
540                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->CameraModel));
541                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->DateTime));
542                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->Comments));
543                         PyList_SET_ITEM(list, pos++,  PyString_FromFormat("%d x %d", exif.m_exifinfo->Width, exif.m_exifinfo->Height));
544                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->Orientation));
545                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->MeteringMode));
546                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->ExposureProgram));
547                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->LightSource));
548                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->FlashUsed));
549                         PyList_SET_ITEM(list, pos++,  PyString_FromFormat("%d", exif.m_exifinfo->CompressionLevel));
550                         PyList_SET_ITEM(list, pos++,  PyString_FromFormat("%d", exif.m_exifinfo->ISOequivalent));
551                         sprintf(tmp, "%.2f", exif.m_exifinfo->Xresolution);
552                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
553                         sprintf(tmp, "%.2f", exif.m_exifinfo->Yresolution);
554                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
555                         PyList_SET_ITEM(list, pos++,  PyString_FromString(exif.m_exifinfo->ResolutionUnit));
556                         sprintf(tmp, "%.2f", exif.m_exifinfo->Brightness);
557                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
558                         sprintf(tmp, "%.5f sec.", exif.m_exifinfo->ExposureTime);
559                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
560                         sprintf(tmp, "%.5f", exif.m_exifinfo->ExposureBias);
561                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
562                         sprintf(tmp, "%.5f", exif.m_exifinfo->Distance);
563                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
564                         sprintf(tmp, "%.5f", exif.m_exifinfo->CCDWidth);
565                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
566                         sprintf(tmp, "%.2f", exif.m_exifinfo->ApertureFNumber);
567                         PyList_SET_ITEM(list, pos++,  PyString_FromString(tmp));
568                 }
569                 else
570                 {
571                         list = PyList_New(1);
572                         PyList_SET_ITEM(list, 0, PyString_FromString(exif.m_szLastError));
573                 }
574                 exif.ClearExif();
575         }
576         else
577         {
578                 list = PyList_New(1);
579                 PyList_SET_ITEM(list, 0, PyString_FromString(exif.m_szLastError));
580         }
581
582         return list ? (PyObject*)list : (PyObject*)PyList_New(0);
583 }
584
585 //---------------------------------------------------------------------------------------------
586 enum {F_NONE, F_PNG, F_JPEG, F_BMP, F_GIF};
587
588 static int pic_id(const char *name)
589 {
590         unsigned char id[10];
591         int fd = open(name, O_RDONLY); 
592         if (fd == -1) 
593                 return F_NONE;
594         read(fd, id, 10);
595         close(fd);
596
597         if(id[1] == 'P' && id[2] == 'N' && id[3] == 'G')
598                 return F_PNG;
599         else if(id[6] == 'J' && id[7] == 'F' && id[8] == 'I' && id[9] == 'F')
600                 return F_JPEG;
601         else if(id[0] == 0xff && id[1] == 0xd8 && id[2] == 0xff) 
602                 return F_JPEG;
603         else if(id[0] == 'B' && id[1] == 'M' )
604                 return F_BMP;
605         else if(id[0] == 'G' && id[1] == 'I' && id[2] == 'F')
606                 return F_GIF;
607         return F_NONE;
608 }
609
610 int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspect, int resize_mode, int rotate, int background, std::string cachefile)
611 {
612         result = 0;
613         int ox=0, oy=0, imx, imy;
614         pic_buffer=NULL;
615         bool cache=false;
616
617         if(cachefile.length())
618         {
619                 cache = true;
620                 if(jpeg_load(cachefile.c_str(), &ox, &oy))
621                         eDebug("[CACHEPIC] x-size=%d, y-size=%d", ox, oy);
622         }
623
624         if(pic_buffer==NULL)
625         {
626                 switch(pic_id(filename.c_str()))
627                 {
628                         case F_PNG:     png_load(filename.c_str(), &ox, &oy); break;
629                         case F_JPEG:    jpeg_load(filename.c_str(), &ox, &oy); break;
630                         case F_BMP:     bmp_load(filename.c_str(), &ox, &oy); break;
631                         case F_GIF:     gif_load(filename.c_str(), &ox, &oy); break;
632                         default:
633                                 eDebug("[PIC] <format not supportet>");
634                                 return 0;
635                 }
636         
637                 eDebug("[FULLPIC] x-size=%d, y-size=%d", ox, oy);
638
639                 if(pic_buffer==NULL)
640                         return 0;
641
642                 double aspect_ratio;
643                 switch(aspect)
644                 {
645                         case 1:         aspect_ratio = 1.777 / ((double)720/576); break; //16:9
646                         case 2:         aspect_ratio = 1.600 / ((double)720/576); break; //16:10
647                         //case 3:       aspect_ratio = 1.250 / ((double)720/576); break; //5:4
648                         default:        aspect_ratio = 1.333 / ((double)720/576); //4:3
649                 }
650
651                 if((aspect_ratio * oy * w / ox) <= h)
652                 {
653                         imx = w;
654                         imy = (int)(aspect_ratio*oy*w/ox);
655                 }
656                 else
657                 {
658                         imx = (int)((1.0/aspect_ratio)*ox*h/oy);
659                         imy = h;
660                 }
661
662                 if(resize_mode) pic_buffer = color_resize(pic_buffer, ox, oy, imx, imy);
663                 else            pic_buffer = simple_resize(pic_buffer, ox, oy, imx, imy);
664
665                 ox = imx;
666                 oy = imy;
667                 
668                 if(cache)
669                 {
670                         jpeg_save(pic_buffer, cachefile.c_str(), 50, oy, ox);
671                         eDebug("[SAVEPIC] x-size=%d, y-size=%d", ox, oy);
672                 }
673                 
674         }
675
676         
677         result=new gPixmap(eSize(w, h), 32);
678         gSurface *surface = result->surface;
679         int a=0, b=0;
680         int nc=0, oc=0;
681         int o_y=0, u_y=0, v_x=0, h_x=0;
682         unsigned char clear[4] = {0x00,0x00,0x00,0x00};
683         if(background)  clear[3]=0xFF;
684         unsigned char *tmp_buffer = new unsigned char[4];
685
686         if(oy < h)
687         {
688                 o_y=(h-oy)/2;
689                 u_y=h-oy-o_y;
690         }
691         if(ox < w)
692         {
693                 v_x=(w-ox)/2;
694                 h_x=w-ox-v_x;
695         }
696         
697         //eDebug("o_y=%d u_y=%d v_x=%d h_x=%d", o_y, u_y, v_x, h_x);
698
699         if(oy < h)
700                 for(a=0; a<(o_y*ox)+1; a++, nc+=4)
701                 {
702                         memcpy(tmp_buffer, clear, sizeof(clear));
703                         tmp_buffer=((unsigned char *)(surface->data)) + nc;
704                 }
705         
706         for(a=0; a<oy; a++)
707         {
708                 if(ox < w)
709                         for(b=0; b<v_x; b++, nc+=4)
710                         {
711                                 memcpy(tmp_buffer, clear, sizeof(clear));
712                                 tmp_buffer=((unsigned char *)(surface->data)) + nc;
713                         }
714
715                 for(b=0; b<(ox*3); b+=3, nc+=4)
716                 {
717                         tmp_buffer[3]=0xFF;
718                         tmp_buffer[2]=pic_buffer[oc++];
719                         tmp_buffer[1]=pic_buffer[oc++];
720                         tmp_buffer[0]=pic_buffer[oc++];
721
722                         tmp_buffer=((unsigned char *)(surface->data)) + nc;
723                 }
724                 
725                 if(ox < w)
726                         for(b=0; b<h_x; b++, nc+=4)
727                         {
728                                 memcpy(tmp_buffer, clear, sizeof(clear));
729                                 tmp_buffer=((unsigned char *)(surface->data)) + nc;
730                         }
731         }
732
733         if(oy < h)
734                 for(a=0; a<(u_y*ox)+1; a++, nc+=4)
735                 {
736                         memcpy(tmp_buffer, clear, sizeof(clear));
737                         tmp_buffer=((unsigned char *)(surface->data)) + nc;
738                 }
739         
740         //eDebug("[PIC] buffer=%d, nc=%d oc=%d ox=%d, oy=%d",w*h*4, nc, oc, ox, oy);
741         
742         surface->clut.data=0;
743         surface->clut.colors=0;
744         surface->clut.start=0;
745         
746         delete [] pic_buffer;
747
748         return 0;
749 }