Use OpenGL memory layout conventions for storing image data
[neverball] / share / base_image.c
index da6e543..9aecd2d 100644 (file)
  * General Public License for more details.
  */
 
-#include <SDL.h>
 #include <png.h>
 #include <jpeglib.h>
+#include <stdlib.h>
 
 #include "glext.h"
 #include "base_config.h"
+#include "base_image.h"
 
 /*---------------------------------------------------------------------------*/
 
@@ -52,7 +53,7 @@ static void *image_load_png(const char *filename, int *width,
 
     if (!(readp = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
         return NULL;
-        
+
     if (!(infop = png_create_info_struct(readp)))
         return NULL;
 
@@ -60,16 +61,19 @@ static void *image_load_png(const char *filename, int *width,
 
     if (setjmp(png_jmpbuf(readp)) == 0)
     {
-        int w, h, b, c, r, i;
+        int w, h, b, i;
 
         /* Read the PNG header. */
 
         png_init_io(readp, fp);
-        png_read_png(readp, infop,
-                     PNG_TRANSFORM_EXPAND   |
-                     PNG_TRANSFORM_STRIP_16 |
-                     PNG_TRANSFORM_PACKING, NULL);
-        
+        png_read_info(readp, infop);
+
+        png_set_expand(readp);
+        png_set_strip_16(readp);
+        png_set_packing(readp);
+
+        png_read_update_info(readp, infop);
+
         /* Extract and check image properties. */
 
         w = (int) png_get_image_width (readp, infop);
@@ -85,24 +89,25 @@ static void *image_load_png(const char *filename, int *width,
         default: longjmp(png_jmpbuf(readp), -1);
         }
 
-        /* Read the pixel data. */
-
-        if (!(bytep = png_get_rows(readp, infop)))
+        if (!(bytep = png_malloc(readp, h * png_sizeof(png_bytep))))
             longjmp(png_jmpbuf(readp), -1);
 
-        /* Allocate the final pixel buffer and copy pixels there. */
+        /* Allocate the final pixel buffer and read pixels there. */
 
         if ((p = (unsigned char *) malloc(w * h * b)))
         {
-            for (r = 0; r < h; r++)
-                for (c = 0; c < w; c++)
-                    for (i = 0; i < b; i++)
-                        p[r*w*b+c*b+i] = (unsigned char) bytep[r][c*b+i];
+            for (i = 0; i < h; i++)
+                bytep[i] = p + w * b * (h - i - 1);
+
+            png_read_image(readp, bytep);
+            png_read_end(readp, NULL);
 
             if (width)  *width  = w;
             if (height) *height = h;
             if (bytes)  *bytes  = b;
         }
+
+        png_free(readp, bytep);
     }
     else p = NULL;
 
@@ -149,7 +154,7 @@ static void *image_load_jpg(const char *filename, int *width,
         {
             while (cinfo.output_scanline < cinfo.output_height)
             {
-                GLubyte *buffer = p + w * b * i;
+                GLubyte *buffer = p + w * b * (h - i - 1);
                 i += jpeg_read_scanlines(&cinfo, &buffer, 1);
             }
 
@@ -172,7 +177,7 @@ void *image_load(const char *filename, int *width,
                                        int *bytes)
 {
     const char *ext = filename + strlen(filename) - 4;
-    
+
     if      (strcmp(ext, ".png") == 0 || strcmp(ext, ".PNG") == 0)
         return image_load_png(filename, width, height, bytes);
     else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".JPG") == 0)
@@ -277,9 +282,9 @@ void image_white(void *p, int w, int h, int b)
         {
             int k = (r * w + c) * b;
 
-            if (b <= 2)
-                s[k + 0] = 0xFF;
-            if (b <= 4)
+            s[k + 0] = 0xFF;
+
+            if (b > 2)
             {
                 s[k + 1] = 0xFF;
                 s[k + 2] = 0xFF;