picload.cpp: fix 32bpp pngs
[enigma2.git] / lib / gdi / picload.cpp
index 2ef901f53930887223af7d5c325b3936c2367013..b9ca31aab97e7cc6c5ec65b90fd54218032900df 100644 (file)
@@ -254,34 +254,33 @@ static unsigned char *bmp_load(const char *file,  int *x, int *y)
 
 static unsigned char *png_load(const char *file, int *ox, int *oy)
 {
-       //static const png_color_16 my_background = {0, 0, 0, 0, 0};
-       png_structp png_ptr;
-       png_infop info_ptr;
+       static const png_color_16 my_background = {0, 0, 0, 0, 0};
+
        png_uint_32 width, height;
        unsigned int i;
        int bit_depth, color_type, interlace_type;
-       int number_passes, pass;
-       png_byte * fbptr;
-       FILE * fh;
+       png_byte *fbptr;
+       FILE *fh;
 
-       if (!(fh = fopen(file, "rb"))) return NULL;
+       if (!(fh = fopen(file, "rb")))
+               return NULL;
 
-       png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+       png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
        if (png_ptr == NULL)
                return NULL;
-       info_ptr = png_create_info_struct(png_ptr);
+       png_infop info_ptr = png_create_info_struct(png_ptr);
        if (info_ptr == NULL)
        {
                png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
-               fclose(fh); 
+               fclose(fh);
                return NULL;
        }
 
        if (setjmp(png_ptr->jmpbuf))
        {
                png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
-               fclose(fh); 
-               return 0;
+               fclose(fh);
+               return NULL;
        }
 
        png_init_io(png_ptr, fh);
@@ -289,40 +288,44 @@ static unsigned char *png_load(const char *file, int *ox, int *oy)
        png_read_info(png_ptr, info_ptr);
        png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
 
-       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)))
-               png_set_expand(png_ptr);
-       if (bit_depth == 16)
-               png_set_strip_16(png_ptr);
+       if (color_type == PNG_COLOR_TYPE_PALETTE)
+       {
+               png_set_palette_to_rgb(png_ptr);
+               png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
+       }
        if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+       {
                png_set_gray_to_rgb(png_ptr);
+               png_set_background(png_ptr, (png_color_16 *)&my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
+       }
+       if (color_type & PNG_COLOR_MASK_ALPHA)
+               png_set_strip_alpha(png_ptr);
 
-       number_passes = png_set_interlace_handling(png_ptr);
-       png_read_update_info(png_ptr, info_ptr);
+       if (bit_depth < 8)
+               png_set_packing(png_ptr);
 
-       int bpp =  png_get_rowbytes(png_ptr, info_ptr)/width;
-       if ((bpp !=4) && (bpp !=3))
-       {
-               eDebug("[PNG] Error processing");
-               return 0;
-       }
+       if (bit_depth == 16)
+               png_set_strip_16(png_ptr);
 
-       if (width * height > 1000000) // 1000x1000 or equiv.
+       int number_passes = png_set_interlace_handling(png_ptr);
+       png_read_update_info(png_ptr, info_ptr);
+
+       if (width * 3 != png_get_rowbytes(png_ptr, info_ptr))
        {
-               eDebug("[png_load] image size is %d x %d, which is \"too large\".", (int)width, (int)height);
-               png_read_end(png_ptr, info_ptr);
+               eDebug("[Picload] Error processing");
                png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
                fclose(fh);
-               return 0;
+               return NULL;
        }
 
-       unsigned char *pic_buffer = new unsigned char[width * height * bpp];
+       unsigned char *pic_buffer = new unsigned char[height * width * 3];
        *ox=width;
        *oy=height;
 
-       for(pass = 0; pass < number_passes; pass++)
+       for(int pass = 0; pass < number_passes; pass++)
        {
                fbptr = (png_byte *)pic_buffer;
-               for (i = 0; i < height; i++, fbptr += width * bpp)
+               for (i = 0; i < height; i++, fbptr += width * 3)
                        png_read_row(png_ptr, fbptr, NULL);
        }
        png_read_end(png_ptr, info_ptr);
@@ -569,19 +572,29 @@ ePicLoad::ePicLoad()
        m_conf.thumbnailsize = 180;
 }
 
-ePicLoad::~ePicLoad()
+void ePicLoad::waitFinished()
 {
        msg_thread.send(Message(Message::quit));
        kill();
+}
 
+ePicLoad::~ePicLoad()
+{
+       if (threadrunning)
+               waitFinished();
        if(m_filepara != NULL)
                delete m_filepara;
 }
 
+void ePicLoad::thread_finished()
+{
+       threadrunning=false;
+}
 
 void ePicLoad::thread()
 {
        hasStarted();
+       threadrunning=true;
        nice(4);
        runLoop();
 }
@@ -759,7 +772,6 @@ void ePicLoad::gotMessage(const Message &msg)
                        break;
                case Message::decode_finished: // called from main thread
                        //eDebug("[Picload] decode finished... %s", m_filepara->file);
-                       threadrunning=false;
                        if(m_filepara->callback)
                        {
                                PictureData(m_filepara->picinfo.c_str());
@@ -824,7 +836,6 @@ int ePicLoad::startThread(int what, const char *file, int x, int y)
                return 1;
        }
        
-       threadrunning=true;
        if(what==1)
                msg_thread.send(Message(Message::decode_Pic));
        else
@@ -843,33 +854,6 @@ RESULT ePicLoad::getThumbnail(const char *file, int x, int y)
        return startThread(0, file, x, y);
 }
 
-RESULT ePicLoad::setPara(PyObject *val)
-{
-       if (!PyList_Check(val))
-               return 0;
-       if (PyList_Size(val) < 6)
-               return 0;
-
-       m_conf.max_x            = PyInt_AsLong( PyList_GET_ITEM(val, 0));
-       m_conf.max_y            = PyInt_AsLong( PyList_GET_ITEM(val, 1));
-       m_conf.aspect_ratio     = PyFloat_AsDouble( PyList_GET_ITEM(val, 2));
-       m_conf.usecache         = PyInt_AsLong( PyList_GET_ITEM(val, 3));
-       m_conf.resizetype       = PyInt_AsLong( PyList_GET_ITEM(val, 4));
-       const char *bg_str      = PyString_AsString( PyList_GET_ITEM(val, 5));
-       
-       if(bg_str[0] == '#' && strlen(bg_str)==9)
-       {
-               int bg = strtoul(bg_str+1, NULL, 16);
-               m_conf.background[0] = bg&0xFF;         //BB
-               m_conf.background[1] = (bg>>8)&0xFF;    //GG
-               m_conf.background[2] = (bg>>16)&0xFF;   //RR
-               m_conf.background[3] = bg>>24;          //AA
-       }
-       
-       eDebug("[Picload] setPara max-X=%d max-Y=%d aspect_ratio=%lf cache=%d resize=%d bg=#%02X%02X%02X%02X", m_conf.max_x, m_conf.max_y, m_conf.aspect_ratio, (int)m_conf.usecache, (int)m_conf.resizetype, m_conf.background[3], m_conf.background[2], m_conf.background[1], m_conf.background[0]);
-       return 1;
-}
-
 PyObject *ePicLoad::getInfo(const char *filename)
 {
        ePyObject list;
@@ -935,6 +919,7 @@ PyObject *ePicLoad::getInfo(const char *filename)
 
 int ePicLoad::getData(ePtr<gPixmap> &result)
 {
+       result = 0;
        if(m_filepara->pic_buffer == NULL) return 0;
        
        m_filepara->pic_buffer = conv24to32(m_filepara->pic_buffer, m_filepara->ox * m_filepara->oy);
@@ -1015,3 +1000,70 @@ int ePicLoad::getData(ePtr<gPixmap> &result)
 
        return 0;
 }
+
+RESULT ePicLoad::setPara(PyObject *val)
+{
+       if (!PyList_Check(val))
+               return 0;
+       if (PyList_Size(val) < 6)
+               return 0;
+
+       m_conf.max_x            = PyInt_AsLong( PyList_GET_ITEM(val, 0));
+       m_conf.max_y            = PyInt_AsLong( PyList_GET_ITEM(val, 1));
+       m_conf.aspect_ratio     = PyFloat_AsDouble( PyList_GET_ITEM(val, 2));
+       m_conf.usecache         = PyInt_AsLong( PyList_GET_ITEM(val, 3));
+       m_conf.resizetype       = PyInt_AsLong( PyList_GET_ITEM(val, 4));
+       const char *bg_str      = PyString_AsString( PyList_GET_ITEM(val, 5));
+       
+       if(bg_str[0] == '#' && strlen(bg_str)==9)
+       {
+               int bg = strtoul(bg_str+1, NULL, 16);
+               m_conf.background[0] = bg&0xFF;         //BB
+               m_conf.background[1] = (bg>>8)&0xFF;    //GG
+               m_conf.background[2] = (bg>>16)&0xFF;   //RR
+               m_conf.background[3] = bg>>24;          //AA
+       }
+       
+       eDebug("[Picload] setPara max-X=%d max-Y=%d aspect_ratio=%lf cache=%d resize=%d bg=#%02X%02X%02X%02X", m_conf.max_x, m_conf.max_y, m_conf.aspect_ratio, (int)m_conf.usecache, (int)m_conf.resizetype, m_conf.background[3], m_conf.background[2], m_conf.background[1], m_conf.background[0]);
+       return 1;
+}
+
+//------------------------------------------------------------------------------------
+
+//for old plugins
+SWIG_VOID(int) loadPic(ePtr<gPixmap> &result, std::string filename, int x, int y, int aspect, int resize_mode, int rotate, int background, std::string cachefile)
+{
+       result = 0;
+       eDebug("deprecated loadPic function used!!! please use the non blocking version! you can see demo code in Pictureplayer plugin... this function is removed in the near future!");
+       ePicLoad mPL;
+
+       double  aspect_ratio;
+       switch(aspect)
+       {
+               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
+               default:        aspect_ratio = 1.333 / ((double)720/576); //4:3
+       }
+       
+       ePyObject list = PyList_New(6);
+       PyList_SET_ITEM(list, 0,  PyLong_FromLong(x));
+       PyList_SET_ITEM(list, 1,  PyLong_FromLong(y));
+       PyList_SET_ITEM(list, 2,  PyFloat_FromDouble(aspect_ratio));
+       PyList_SET_ITEM(list, 3,  PyLong_FromLong(0));
+       PyList_SET_ITEM(list, 4,  PyLong_FromLong(resize_mode));
+       if(background)
+               PyList_SET_ITEM(list, 5,  PyString_FromString("#ff000000"));
+       else
+               PyList_SET_ITEM(list, 5,  PyString_FromString("#00000000"));
+
+       mPL.setPara(list);
+
+       if(!mPL.startDecode(filename.c_str()))
+       {
+               mPL.waitFinished(); // this blocks until the thread is finished
+               mPL.getData(result);
+       }
+
+       return 0;
+}