patch by Pieter Grimmerink: use ext3 largefile option only for disks > 4G
[enigma2.git] / lib / gdi / epng.cpp
index d476ec3..6b08c2d 100644 (file)
@@ -3,13 +3,15 @@
 #include <lib/gdi/epng.h>
 #include <unistd.h>
 
-gImage *loadPNG(const char *filename)
+extern "C" {
+#include <jpeglib.h>
+}
+
+int loadPNG(ePtr<gPixmap> &result, const char *filename)
 {
        __u8 header[8];
        FILE *fp=fopen(filename, "rb");
        
-       gImage *res=0;
-       
        if (!fp)
        {
 //             eDebug("couldn't open %s", filename );
@@ -54,8 +56,7 @@ gImage *loadPNG(const char *filename)
                eDebug("das war wohl nix");
                png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
                fclose(fp);
-               if (res)
-                       delete res;
+               result = 0;
                return 0;
        }
        png_init_io(png_ptr, fp);
@@ -73,15 +74,16 @@ gImage *loadPNG(const char *filename)
        
        if (color_type != 6)
        {
-               res=new gImage(eSize(width, height), bit_depth);
+               result=new gPixmap(eSize(width, height), bit_depth);
+               gSurface *surface = result->surface;
        
                png_bytep *rowptr=new png_bytep[height];
        
                for (unsigned int i=0; i<height; i++)
-                       rowptr[i]=((png_byte*)(res->data))+i*res->stride;
+                       rowptr[i]=((png_byte*)(surface->data))+i*surface->stride;
                png_read_rows(png_ptr, rowptr, 0, height);
        
-               delete rowptr;
+               delete [] rowptr;
        
                if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))
                {
@@ -89,37 +91,140 @@ gImage *loadPNG(const char *filename)
                        int num_palette;
                        png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
                        if (num_palette)
-                               res->clut.data=new gRGB[num_palette];
+                               surface->clut.data=new gRGB[num_palette];
                        else
-                               res->clut.data=0;
-                       res->clut.colors=num_palette;
+                               surface->clut.data=0;
+                       surface->clut.colors=num_palette;
                        
                        for (int i=0; i<num_palette; i++)
                        {
-                               res->clut.data[i].a=0;
-                               res->clut.data[i].r=palette[i].red;
-                               res->clut.data[i].g=palette[i].green;
-                               res->clut.data[i].b=palette[i].blue;
+                               surface->clut.data[i].a=0;
+                               surface->clut.data[i].r=palette[i].red;
+                               surface->clut.data[i].g=palette[i].green;
+                               surface->clut.data[i].b=palette[i].blue;
                        }
                        if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
                        {
                                png_byte *trans;
                                png_get_tRNS(png_ptr, info_ptr, &trans, &num_palette, 0);
                                for (int i=0; i<num_palette; i++)
-                                       res->clut.data[i].a=255-trans[i];
+                                       surface->clut.data[i].a=255-trans[i];
                        }
                } else
                {
-                       res->clut.data=0;
-                       res->clut.colors=0;
+                       surface->clut.data=0;
+                       surface->clut.colors=0;
                }
+               surface->clut.start=0;
                png_read_end(png_ptr, end_info);
        } else
-               res=0;
+               result=0;
 
        png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
        fclose(fp);
-       return res;
+       return 0;
+}
+
+struct my_error_mgr {
+       struct jpeg_error_mgr pub;
+       jmp_buf setjmp_buffer;
+};
+
+typedef struct my_error_mgr * my_error_ptr;
+
+static void
+my_error_exit (j_common_ptr cinfo)
+{
+       my_error_ptr myerr = (my_error_ptr) cinfo->err;
+       (*cinfo->err->output_message) (cinfo);
+       longjmp(myerr->setjmp_buffer, 1);
+}
+
+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, "rb");
+       result = 0;
+
+       if (alpha)
+       {
+               if (alpha->surface->bpp != 8)
+               {
+                       eWarning("alpha channel for jpg must be 8bit");
+                       alpha = 0;
+               }
+       }
+
+       if (!infile)
+               return -1;
+       cinfo.err = jpeg_std_error(&jerr.pub);
+       jerr.pub.error_exit = my_error_exit;
+       if (setjmp(jerr.setjmp_buffer)) {
+               result = 0;
+               jpeg_destroy_decompress(&cinfo);
+               fclose(infile);
+               return -1;
+       }
+       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;
+
+       if (alpha)
+       {
+               if (((int)cinfo.output_width != alpha->surface->x) || ((int)cinfo.output_height != alpha->surface->y))
+               {
+                       eWarning("alpha channel size (%dx%d) must match jpeg size (%dx%d)", alpha->surface->x, alpha->surface->y, cinfo.output_width, cinfo.output_height);
+                       alpha = 0;
+               }
+               if (grayscale)
+               {
+                       eWarning("we don't support grayscale + alpha at the moment");
+                       alpha = 0;
+               }
+       }
+
+       result = new gPixmap(eSize(cinfo.output_width, cinfo.output_height), grayscale ? 8 : 32);
+
+       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 * y;
+               unsigned char *src = (unsigned char*)buffer[0];
+               unsigned char *palpha = alpha ? ((unsigned char*)alpha->surface->data + alpha->surface->stride * y) : 0;
+               if (grayscale)
+                       memcpy(dst, src, cinfo.output_width);
+               else
+               {
+                       int x;
+                       for (x = 0; x < (int)cinfo.output_width; ++x)
+                       {
+                               *dst++ = src[2];
+                               *dst++ = src[1];
+                               *dst++ = src[0];
+                               src += 3;
+                               if (palpha)
+                                       *dst++ = *palpha++;
+                               else 
+                                       *dst++ = 0xFF;
+                       }
+               }
+       }
+       (void) jpeg_finish_decompress(&cinfo);
+       jpeg_destroy_decompress(&cinfo);
+       fclose(infile);
+       return 0;
 }
 
 int savePNG(const char *filename, gPixmap *pixmap)
@@ -127,6 +232,11 @@ int savePNG(const char *filename, gPixmap *pixmap)
        FILE *fp=fopen(filename, "wb");
        if (!fp)
                return -1;
+       
+       gSurface *surface = pixmap->surface;
+       if (!surface)
+               return -2;
+       
        png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
        if (!png_ptr)
        {
@@ -156,28 +266,28 @@ int savePNG(const char *filename, gPixmap *pixmap)
        png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
        png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
 
-       png_set_IHDR(png_ptr, info_ptr, pixmap->x, pixmap->y, pixmap->bpp, 
-               pixmap->clut.data ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY, 
+       png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp, 
+               surface->clut.data ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY, 
                PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-       if (pixmap->clut.data)
+       if (surface->clut.data)
        {
-               png_color palette[pixmap->clut.colors];
-               png_byte trans[pixmap->clut.colors];
-               for (int i=0; i<pixmap->clut.colors; ++i)
+               png_color palette[surface->clut.colors];
+               png_byte trans[surface->clut.colors];
+               for (int i=0; i<surface->clut.colors; ++i)
                {
-                       palette[i].red=pixmap->clut.data[i].r;
-                       palette[i].green=pixmap->clut.data[i].g;
-                       palette[i].blue=pixmap->clut.data[i].b;
-                       trans[i]=255-pixmap->clut.data[i].a;
+                       palette[i].red=surface->clut.data[i].r;
+                       palette[i].green=surface->clut.data[i].g;
+                       palette[i].blue=surface->clut.data[i].b;
+                       trans[i]=255-surface->clut.data[i].a;
                }
-               png_set_PLTE(png_ptr, info_ptr, palette, pixmap->clut.colors);
-               png_set_tRNS(png_ptr, info_ptr, trans, pixmap->clut.colors, 0);
+               png_set_PLTE(png_ptr, info_ptr, palette, surface->clut.colors);
+               png_set_tRNS(png_ptr, info_ptr, trans, surface->clut.colors, 0);
        }
        png_write_info(png_ptr, info_ptr);
        png_set_packing(png_ptr);
-       png_byte *row_pointers[pixmap->y];
-       for (int i=0; i<pixmap->y; ++i)
-               row_pointers[i]=((png_byte*)pixmap->data)+i*pixmap->stride;
+       png_byte *row_pointers[surface->y];
+       for (int i=0; i<surface->y; ++i)
+               row_pointers[i]=((png_byte*)surface->data)+i*surface->stride;
        png_write_image(png_ptr, row_pointers);
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);