Fix build for libretro.
[pcsx_rearmed.git] / frontend / common / readpng.c
index b7bad57..540f543 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (C) Gražvydas "notaz" Ignotas, 2008-2010
+ * (C) Gražvydas "notaz" Ignotas, 2008-2011
  *
  * This work is licensed under the terms of any of these licenses
  * (at your option):
@@ -9,6 +9,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <png.h>
 #include "readpng.h"
@@ -59,7 +60,7 @@ int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req
                goto done;
        }
 
-       // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)info_ptr->width, (int)info_ptr->height, info_ptr->pixel_depth);
+       // lprintf("%s: %ix%i @ %ibpp\n", fname, (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), png_get_bit_depth(png_ptr, info_ptr));
 
        switch (what)
        {
@@ -67,15 +68,15 @@ int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req
                {
                        int height, width, h;
                        unsigned short *dst = dest;
-                       if (info_ptr->pixel_depth != 24)
+                       if (png_get_bit_depth(png_ptr, info_ptr) != 8)
                        {
-                               lprintf(__FILE__ ": bg image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
+                               lprintf(__FILE__ ": bg image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
                                break;
                        }
-                       height = info_ptr->height;
+                       height = png_get_image_width(png_ptr, info_ptr);
                        if (height > req_h)
                                height = req_h;
-                       width = info_ptr->width;
+                       width = png_get_image_height(png_ptr, info_ptr);
                        if (width > req_w)
                                width = req_w;
 
@@ -101,15 +102,15 @@ int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req
                {
                        int x, y, x1, y1;
                        unsigned char *dst = dest;
-                       if (info_ptr->width != req_w || info_ptr->height != req_h)
+                       if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
                        {
                                lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n",
-                                       (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
+                                       (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
                                break;
                        }
-                       if (info_ptr->pixel_depth != 8)
+                       if (png_get_bit_depth(png_ptr, info_ptr) != 8)
                        {
-                               lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
+                               lprintf(__FILE__ ": font image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
                                break;
                        }
                        for (y = 0; y < 16; y++)
@@ -134,15 +135,15 @@ int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req
                {
                        int x1, y1;
                        unsigned char *dst = dest;
-                       if (info_ptr->width != req_w || info_ptr->height != req_h)
+                       if (png_get_image_width(png_ptr, info_ptr) != req_w || png_get_image_height(png_ptr, info_ptr) != req_h)
                        {
                                lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n",
-                                       (int)info_ptr->width, (int)info_ptr->height, req_w, req_h);
+                                       (int)png_get_image_width(png_ptr, info_ptr), (int)png_get_image_height(png_ptr, info_ptr), req_w, req_h);
                                break;
                        }
-                       if (info_ptr->pixel_depth != 8)
+                       if (png_get_bit_depth(png_ptr, info_ptr) != 8)
                        {
-                               lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth);
+                               lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", png_get_bit_depth(png_ptr, info_ptr));
                                break;
                        }
                        for (y1 = 0; y1 < req_h; y1++)
@@ -158,15 +159,15 @@ int readpng(void *dest, const char *fname, readpng_what what, int req_w, int req
                {
                        int height, width, h;
                        unsigned char *dst = dest;
-                       if (info_ptr->pixel_depth != 24)
+                       if (png_get_bit_depth(png_ptr, info_ptr) != 8)
                        {
-                               lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth);
+                               lprintf(__FILE__ ": image uses %ibpc, needed 8bpc\n", png_get_bit_depth(png_ptr, info_ptr));
                                break;
                        }
-                       height = info_ptr->height;
+                       height = png_get_image_height(png_ptr, info_ptr);
                        if (height > req_h)
                                height = req_h;
-                       width = info_ptr->width;
+                       width = png_get_image_width(png_ptr, info_ptr);
                        if (width > req_w)
                                width = req_w;
 
@@ -190,4 +191,76 @@ done:
        return ret;
 }
 
+int writepng(const char *fname, unsigned short *src, int w, int h)
+{
+       png_structp png_ptr = NULL;
+       png_infop info_ptr = NULL;
+       png_bytepp row_pointers;
+       int i, j, ret = -1;
+       FILE *f;
+
+       f = fopen(fname, "wb");
+       if (f == NULL) {
+               lprintf(__FILE__ ": failed to open \"%s\"\n", fname);
+               return -1;
+       }
+
+       row_pointers = calloc(h, sizeof(row_pointers[0]));
+       if (row_pointers == NULL)
+               goto end1;
+
+       for (i = 0; i < h; i++) {
+               unsigned char *dst = malloc(w * 3);
+               if (dst == NULL)
+                       goto end2;
+               row_pointers[i] = dst;
+               for (j = 0; j < w; j++, src++, dst += 3) {
+                       dst[0] = (*src & 0xf800) >> 8;
+                       dst[1] = (*src & 0x07e0) >> 3;
+                       dst[2] = (*src & 0x001f) << 3;
+               }
+       }
+
+       /* initialize stuff */
+       png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+       if (png_ptr == NULL) {
+               fprintf(stderr, "png_create_write_struct() failed");
+               goto end2;
+       }
+
+       info_ptr = png_create_info_struct(png_ptr);
+       if (info_ptr == NULL) {
+               fprintf(stderr, "png_create_info_struct() failed");
+               goto end3;
+       }
+
+       if (setjmp(png_jmpbuf(png_ptr)) != 0) {
+               fprintf(stderr, "error in png code\n");
+               goto end4;
+       }
+
+       png_init_io(png_ptr, f);
+
+       png_set_IHDR(png_ptr, info_ptr, w, h,
+               8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
+               PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+       png_write_info(png_ptr, info_ptr);
+       png_write_image(png_ptr, row_pointers);
+       png_write_end(png_ptr, NULL);
+
+       ret = 0;
+
+end4:
+//     png_destroy_info_struct(png_ptr, &info_ptr); // freed below
+end3:
+       png_destroy_write_struct(&png_ptr, &info_ptr);
+end2:
+       for (i = 0; i < h; i++)
+               free(row_pointers[i]);
+       free(row_pointers);
+end1:
+       fclose(f);
+       return ret;
+}