[RecordTimer] fix repeat timer issue.
[vuplus_dvbapp] / lib / gdi / picload.cpp
index b1dfd1a..ab7a0b5 100644 (file)
@@ -1,4 +1,5 @@
-#include <png.h>       // must be included before Python.h because of setjmp
+#define PNG_SKIP_SETJMP_CHECK
+#include <png.h>
 #include <fcntl.h>
 
 #include <lib/gdi/picload.h>
@@ -44,11 +45,11 @@ static unsigned char *conv24to32(unsigned char *orgin, int size, unsigned char a
        return(cr);
 }
 
-static unsigned char *simple_resize(unsigned char *orgin, int ox, int oy, int dx, int dy)
+static unsigned char *simple_resize(unsigned char *orgin, int ox, int oy, int dx, int dy, int bypp)
 {
        unsigned char *cr, *p, *l;
        int i, j, k, ip;
-       cr = new unsigned char[dx * dy * 3];
+       cr = new unsigned char[dx * dy * bypp];
        if (cr == NULL)
        {
                eDebug("[Picload] Error malloc");
@@ -56,27 +57,28 @@ static unsigned char *simple_resize(unsigned char *orgin, int ox, int oy, int dx
        }
        l = cr;
 
-       for (j = 0; j < dy; j++,l += dx * 3)
+       for (j = 0; j < dy; j++,l += dx * bypp)
        {
-               p = orgin + (j * oy / dy * ox * 3);
-               for (i = 0, k = 0; i < dx; i++, k += 3)
+               p = orgin + (j * oy / dy * ox * bypp);
+               for (i = 0, k = 0; i < dx; i++, k += bypp)
                {
-                       ip = i * ox / dx * 3;
+                       ip = i * ox / dx * bypp;
                        l[k] = p[ip];
                        l[k+1] = p[ip + 1];
                        l[k+2] = p[ip + 2];
+                       if(bypp == 4) l[k+3] = p[ip + 3];
                }
        }
        delete [] orgin;
        return(cr);
 }
 
-static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
+static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx, int dy, int bypp)
 {
        unsigned char *cr, *p, *q;
        int i, j, k, l, xa, xb, ya, yb;
-       int sq, r, g, b;
-       cr = new unsigned char[dx * dy * 3];
+       int sq, r, g, b, a;
+       cr = new unsigned char[dx * dy * bypp];
        if (cr == NULL)
        {
                eDebug("[Picload] Error malloc");
@@ -86,7 +88,7 @@ static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx
 
        for (j = 0; j < dy; j++)
        {
-               for (i = 0; i < dx; i++, p += 3)
+               for (i = 0; i < dx; i++, p += bypp)
                {
                        xa = i * ox / dx;
                        ya = j * oy / dy;
@@ -96,15 +98,17 @@ static unsigned char *color_resize(unsigned char * orgin, int ox, int oy, int dx
                        yb = (j + 1) * oy / dy; 
                        if (yb >= oy)
                                yb = oy - 1;
-                       for (l = ya, r = 0, g = 0, b = 0, sq = 0; l <= yb; l++)
+                       for (l = ya, r = 0, g = 0, b = 0, a = 0, sq = 0; l <= yb; l++)
                        {
-                               q = orgin + ((l * ox + xa) * 3);
-                               for (k = xa; k <= xb; k++, q += 3, sq++)
+                               q = orgin + ((l * ox + xa) * bypp);
+                               for (k = xa; k <= xb; k++, q += bypp, sq++)
                                {
                                        r += q[0]; g += q[1]; b += q[2];
+                                       if(bypp == 4) a += q[3];
                                }
                        }
                        p[0] = r / sq; p[1] = g / sq; p[2] = b / sq;
+                       if(bypp == 4) p[3] = a / sq;
                }
        }
        delete [] orgin;
@@ -252,36 +256,35 @@ 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 unsigned char *png_load(const char *file, int *ox, int *oy, int *_bypp)
 {
-       //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))
+       if (setjmp(png_jmpbuf(png_ptr)))
        {
                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 +292,34 @@ 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)))
+       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_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
                png_set_gray_to_rgb(png_ptr);
 
-       number_passes = png_set_interlace_handling(png_ptr);
+       int number_passes = png_set_interlace_handling(png_ptr);
        png_read_update_info(png_ptr, info_ptr);
+       int bypp = png_get_rowbytes(png_ptr, info_ptr) / width;
 
-       int bpp =  png_get_rowbytes(png_ptr, info_ptr)/width;
-       if ((bpp !=4) && (bpp !=3))
+       if(bypp != 4 && bypp != 3)
        {
-               eDebug("[PNG] Error processing");
-               return 0;
-       }
-
-       if (width * height > 1000000) // 1000x1000 or equiv.
-       {
-               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 * bypp];
        *ox=width;
        *oy=height;
+       *_bypp = bypp;
 
-       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 * bypp)
                        png_read_row(png_ptr, fbptr, NULL);
        }
        png_read_end(png_ptr, info_ptr);
@@ -602,7 +599,7 @@ void ePicLoad::decodePic()
        
        switch(m_filepara->id)
        {
-               case F_PNG:     m_filepara->pic_buffer = png_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);  break;
+               case F_PNG:     m_filepara->pic_buffer = png_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy, &m_filepara->bypp);       break;
                case F_JPEG:    m_filepara->pic_buffer = jpeg_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy); break;
                case F_BMP:     m_filepara->pic_buffer = bmp_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);  break;
                case F_GIF:     m_filepara->pic_buffer = gif_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);  break;
@@ -685,7 +682,7 @@ void ePicLoad::decodeThumb()
 
        switch(m_filepara->id)
        {
-               case F_PNG:     m_filepara->pic_buffer = png_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);  break;
+               case F_PNG:     m_filepara->pic_buffer = png_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy, &m_filepara->bypp);       break;
                case F_JPEG:    m_filepara->pic_buffer = jpeg_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy); break;
                case F_BMP:     m_filepara->pic_buffer = bmp_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);  break;
                case F_GIF:     m_filepara->pic_buffer = gif_load(m_filepara->file, &m_filepara->ox, &m_filepara->oy);  break;
@@ -715,7 +712,7 @@ void ePicLoad::decodeThumb()
                                imy = (int)( (m_conf.thumbnailsize * ((double)m_filepara->oy)) / ((double)m_filepara->ox) );
                        }
 
-                       m_filepara->pic_buffer = color_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy);
+                       m_filepara->pic_buffer = color_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy, m_filepara->bypp);
                        m_filepara->ox = imx;
                        m_filepara->oy = imy;
 
@@ -743,9 +740,9 @@ void ePicLoad::resizePic()
        }
                
        if(m_conf.resizetype)
-               m_filepara->pic_buffer = color_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy);
+               m_filepara->pic_buffer = color_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy, m_filepara->bypp);
        else
-               m_filepara->pic_buffer = simple_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy);
+               m_filepara->pic_buffer = simple_resize(m_filepara->pic_buffer, m_filepara->ox, m_filepara->oy, imx, imy, m_filepara->bypp);
 
        m_filepara->ox = imx;
        m_filepara->oy = imy;
@@ -787,9 +784,9 @@ void ePicLoad::gotMessage(const Message &msg)
        }
 }
 
-int ePicLoad::startThread(int what, const char *file, int x, int y)
+int ePicLoad::startThread(int what, const char *file, int x, int y, bool async)
 {
-       if(threadrunning && m_filepara != NULL)
+       if(async && threadrunning && m_filepara != NULL)
        {
                eDebug("[Picload] thread running");
                m_filepara->callback = false;
@@ -833,22 +830,28 @@ int ePicLoad::startThread(int what, const char *file, int x, int y)
                return 1;
        }
        
-       if(what==1)
-               msg_thread.send(Message(Message::decode_Pic));
+       if (async) {
+               if(what==1)
+                       msg_thread.send(Message(Message::decode_Pic));
+               else
+                       msg_thread.send(Message(Message::decode_Thumb));
+               run();
+       }
+       else if (what == 1)
+               decodePic();
        else
-               msg_thread.send(Message(Message::decode_Thumb));
-       run();
+               decodeThumb();
        return 0;
 }
 
-RESULT ePicLoad::startDecode(const char *file, int x, int y)
+RESULT ePicLoad::startDecode(const char *file, int x, int y, bool async)
 {
-       return startThread(1, file, x, y);
+       return startThread(1, file, x, y, async);
 }
 
-RESULT ePicLoad::getThumbnail(const char *file, int x, int y)
+RESULT ePicLoad::getThumbnail(const char *file, int x, int y, bool async)
 {
-       return startThread(0, file, x, y);
+       return startThread(0, file, x, y, async);
 }
 
 PyObject *ePicLoad::getInfo(const char *filename)
@@ -919,8 +922,11 @@ 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);
-       
+       if(m_filepara->bypp == 3)
+       {
+               m_filepara->pic_buffer = conv24to32(m_filepara->pic_buffer, m_filepara->ox * m_filepara->oy);
+       }
+
        result=new gPixmap(eSize(m_filepara->max_x, m_filepara->max_y), 32);
        gSurface *surface = result->surface;
        int a=0, b=0;
@@ -1000,28 +1006,29 @@ int ePicLoad::getData(ePtr<gPixmap> &result)
 
 RESULT ePicLoad::setPara(PyObject *val)
 {
-       if (!PyList_Check(val))
+       if (!PySequence_Check(val))
                return 0;
-       if (PyList_Size(val) < 6)
+       if (PySequence_Size(val) < 7)
                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));
+       else {
+               ePyObject fast = PySequence_Fast(val, "");
+               m_conf.max_x            = PyInt_AsLong( PySequence_Fast_GET_ITEM(fast, 0));
+               m_conf.max_y            = PyInt_AsLong( PySequence_Fast_GET_ITEM(fast, 1));
+               m_conf.aspect_ratio     = (double)PyInt_AsLong( PySequence_Fast_GET_ITEM(fast, 2)) / PyInt_AsLong(PySequence_Fast_GET_ITEM(fast, 3));
+               m_conf.usecache         = PyInt_AsLong( PySequence_Fast_GET_ITEM(fast, 4));
+               m_conf.resizetype       = PyInt_AsLong( PySequence_Fast_GET_ITEM(fast, 5));
+               const char *bg_str      = PyString_AsString( PySequence_Fast_GET_ITEM(fast, 6));
        
-       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
+               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]);
        }
-       
-       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;
 }
 
@@ -1030,37 +1037,35 @@ RESULT ePicLoad::setPara(PyObject *val)
 //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)
 {
+       long asp1, asp2;
        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
+               case 1:         asp1 = 16*576, asp2 = 9*720; break; //16:9
+               case 2:         asp1 = 16*576, asp2 = 10*720; break; //16:10
+               case 3:         asp1 = 5*576, asp2 = 4*720; break; //5:4
+               default:        asp1 = 4*576, asp2 = 3*720; break; //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));
+
+       ePyObject tuple = PyTuple_New(7);
+       PyTuple_SET_ITEM(tuple, 0,  PyLong_FromLong(x));
+       PyTuple_SET_ITEM(tuple, 1,  PyLong_FromLong(y));
+       PyTuple_SET_ITEM(tuple, 2,  PyLong_FromLong(asp1));
+       PyTuple_SET_ITEM(tuple, 3,  PyLong_FromLong(asp2));
+       PyTuple_SET_ITEM(tuple, 4,  PyLong_FromLong(0));
+       PyTuple_SET_ITEM(tuple, 5,  PyLong_FromLong(resize_mode));
        if(background)
-               PyList_SET_ITEM(list, 5,  PyString_FromString("#ff000000"));
+               PyTuple_SET_ITEM(tuple, 6,  PyString_FromString("#ff000000"));
        else
-               PyList_SET_ITEM(list, 5,  PyString_FromString("#00000000"));
+               PyTuple_SET_ITEM(tuple, 6,  PyString_FromString("#00000000"));
 
-       mPL.setPara(list);
+       mPL.setPara(tuple);
 
-       if(!mPL.startDecode(filename.c_str()))
-       {
-               mPL.waitFinished(); // this blocks until the thread is finished
+       if(!mPL.startDecode(filename.c_str(), 0, 0, false))
                mPL.getData(result);
-       }
 
        return 0;
 }