translations
[enigma2.git] / lib / gdi / epng.cpp
index d476ec3..9c12bb1 100644 (file)
@@ -3,13 +3,11 @@
 #include <lib/gdi/epng.h>
 #include <unistd.h>
 
-gImage *loadPNG(const char *filename)
+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 +52,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);
@@ -69,19 +66,20 @@ gImage *loadPNG(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);
+       eDebug("%s: %dx%dx%d png, %d", filename, (int)width, (int)height, (int)bit_depth, color_type);
        
        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 +87,38 @@ 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;
 }
 
 int savePNG(const char *filename, gPixmap *pixmap)
@@ -127,6 +126,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 +160,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);