fix scaling and conversion bugs
[enigma2.git] / lib / gdi / picload.cpp
index e3cdc035606e5aaa39405fa03ac2691ae3a906e6..a94341db8072758121ba4ab1fc305b4714483e24 100644 (file)
@@ -1,9 +1,8 @@
+#include <png.h>       // must be included before Python.h because of setjmp
 #include <lib/gdi/picload.h>
 #include "picexif.h"
 #include <lib/python/python.h>
 
-#include <png.h>
-
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -14,7 +13,6 @@ extern "C" {
 #include <gif_lib.h>
 //#include "transupp.h"
 }
-#include <setjmp.h>
 
 unsigned char *pic_buffer=NULL;
 
@@ -393,7 +391,16 @@ static int png_load(const char *filename,  int *x, int *y)
                eDebug("[PNG] Error processing");
                return 0;
        }
-       
+
+       if (width * height > 1000000) // 1000x1000 or equiv.
+       {
+               eDebug("[png_load] image size is %u x %u, which is \"too large\".", (unsigned int)width, (unsigned int)height);
+               png_read_end(png_ptr, info_ptr);
+               png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
+               fclose(fh);
+               return 0;
+       }
+
        pic_buffer = new unsigned char[width * height * 3];
        *x=width;
        *y=height;
@@ -642,9 +649,9 @@ int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspec
                double aspect_ratio;
                switch(aspect)
                {
-                       case 1:         aspect_ratio = 1.777 / ((double)720/576); break; //16:9
+                       case 1:         aspect_ratio = 1.778 / ((double)720/576); break; //16:9
                        case 2:         aspect_ratio = 1.600 / ((double)720/576); break; //16:10
-                       //case 3:       aspect_ratio = 1.250 / ((double)720/576); break; //5:4
+                       case 3:         aspect_ratio = 1.250 / ((double)720/576); break; //5:4
                        default:        aspect_ratio = 1.333 / ((double)720/576); //4:3
                }
 
@@ -681,7 +688,7 @@ int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspec
        int o_y=0, u_y=0, v_x=0, h_x=0;
        unsigned char clear[4] = {0x00,0x00,0x00,0x00};
        if(background)  clear[3]=0xFF;
-       unsigned char *tmp_buffer = new unsigned char[4];
+       unsigned char *tmp_buffer=((unsigned char *)(surface->data));
 
        if(oy < h)
        {
@@ -699,8 +706,8 @@ int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspec
        if(oy < h)
                for(a=0; a<(o_y*ox)+1; a++, nc+=4)
                {
-                       memcpy(tmp_buffer, clear, sizeof(clear));
                        tmp_buffer=((unsigned char *)(surface->data)) + nc;
+                       memcpy(tmp_buffer, clear, sizeof(clear));
                }
        
        for(a=0; a<oy; a++)
@@ -708,33 +715,33 @@ int loadPic(ePtr<gPixmap> &result, std::string filename, int w, int h, int aspec
                if(ox < w)
                        for(b=0; b<v_x; b++, nc+=4)
                        {
-                               memcpy(tmp_buffer, clear, sizeof(clear));
                                tmp_buffer=((unsigned char *)(surface->data)) + nc;
+                               memcpy(tmp_buffer, clear, sizeof(clear));
                        }
 
                for(b=0; b<(ox*3); b+=3, nc+=4)
                {
+                       tmp_buffer=((unsigned char *)(surface->data)) + nc;
                        tmp_buffer[3]=0xFF;
                        tmp_buffer[2]=pic_buffer[oc++];
                        tmp_buffer[1]=pic_buffer[oc++];
                        tmp_buffer[0]=pic_buffer[oc++];
 
-                       tmp_buffer=((unsigned char *)(surface->data)) + nc;
                }
                
                if(ox < w)
                        for(b=0; b<h_x; b++, nc+=4)
                        {
-                               memcpy(tmp_buffer, clear, sizeof(clear));
                                tmp_buffer=((unsigned char *)(surface->data)) + nc;
+                               memcpy(tmp_buffer, clear, sizeof(clear));
                        }
        }
 
        if(oy < h)
                for(a=0; a<(u_y*ox)+1; a++, nc+=4)
                {
-                       memcpy(tmp_buffer, clear, sizeof(clear));
                        tmp_buffer=((unsigned char *)(surface->data)) + nc;
+                       memcpy(tmp_buffer, clear, sizeof(clear));
                }
        
        //eDebug("[PIC] buffer=%d, nc=%d oc=%d ox=%d, oy=%d",w*h*4, nc, oc, ox, oy);