fix typo
[enigma2.git] / lib / gdi / epng.cpp
index 4666cdf305c06a126a356d3910d7ae41e98005ff..0f8ea56843a0c7cf6f774ab09b6aeedb67cc4c49 100644 (file)
@@ -70,9 +70,7 @@ int loadPNG(ePtr<gPixmap> &result, const char *filename)
        
        png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
        
-//     eDebug("%s: %dx%dx%d png, %d", filename, (int)width, (int)height, (int)bit_depth, color_type);
-       
-       if (color_type != 6)
+       if (color_type == PNG_COLOR_TYPE_GRAY || color_type & PNG_COLOR_MASK_PALETTE)
        {
                result=new gPixmap(eSize(width, height), bit_depth);
                gSurface *surface = result->surface;
@@ -117,8 +115,10 @@ int loadPNG(ePtr<gPixmap> &result, const char *filename)
                }
                surface->clut.start=0;
                png_read_end(png_ptr, end_info);
-       } else
+       } else {
                result=0;
+               eDebug("%s: %dx%dx%d png, %d", filename, (int)width, (int)height, (int)bit_depth, color_type);
+       }
 
        png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
        fclose(fp);
@@ -140,14 +140,14 @@ my_error_exit (j_common_ptr cinfo)
        longjmp(myerr->setjmp_buffer, 1);
 }
 
-int loadJPG(ePtr<gPixmap> &result, const char *filename, gPixmap *alpha)
+int loadJPG(ePtr<gPixmap> &result, const char *filename, ePtr<gPixmap> alpha)
 {
        struct jpeg_decompress_struct cinfo;
        struct my_error_mgr jerr;
        FILE *infile;
        JSAMPARRAY buffer;
        int row_stride;
-       infile = fopen(filename, "r");
+       infile = fopen(filename, "rb");
        result = 0;
 
        if (alpha)
@@ -172,6 +172,10 @@ int loadJPG(ePtr<gPixmap> &result, const char *filename, gPixmap *alpha)
        jpeg_create_decompress(&cinfo);
        jpeg_stdio_src(&cinfo, infile);
        (void) jpeg_read_header(&cinfo, TRUE);
+       cinfo.out_color_space = JCS_RGB;
+       cinfo.scale_denom = 1;
+
+       (void) jpeg_start_decompress(&cinfo);
 
        int grayscale = cinfo.output_components == 1;
 
@@ -191,27 +195,28 @@ int loadJPG(ePtr<gPixmap> &result, const char *filename, gPixmap *alpha)
 
        result = new gPixmap(eSize(cinfo.output_width, cinfo.output_height), grayscale ? 8 : 32);
 
-       (void) jpeg_start_decompress(&cinfo);
        row_stride = cinfo.output_width * cinfo.output_components;
        buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
        while (cinfo.output_scanline < cinfo.output_height) {
+               int y = cinfo.output_scanline;
                (void) jpeg_read_scanlines(&cinfo, buffer, 1);
-               unsigned char *dst = ((unsigned char*)result->surface->data) + result->surface->stride * cinfo.output_scanline;
+               unsigned char *dst = ((unsigned char*)result->surface->data) + result->surface->stride * y;
                unsigned char *src = (unsigned char*)buffer[0];
-               unsigned char *palpha = alpha ? ((unsigned char*)alpha->surface->data + alpha->surface->stride * cinfo.output_scanline) : 0;
+               unsigned char *palpha = alpha ? ((unsigned char*)alpha->surface->data + alpha->surface->stride * y) : 0;
                if (grayscale)
-                       memcpy(dst, src, row_stride);
+                       memcpy(dst, src, cinfo.output_width);
                else
                {
                        int x;
                        for (x = 0; x < (int)cinfo.output_width; ++x)
                        {
-                               *dst++ = *src++;
-                               *dst++ = *src++;
-                               *dst++ = *src++;
-                               if (alpha)
+                               *dst++ = src[2];
+                               *dst++ = src[1];
+                               *dst++ = src[0];
+                               src += 3;
+                               if (palpha)
                                        *dst++ = *palpha++;
-                               else
+                               else 
                                        *dst++ = 0xFF;
                        }
                }