Fix accidental switch/teleporter behavior changes
[neverball] / share / base_image.c
index 9aecd2d..b3c5808 100644 (file)
 #include <png.h>
 #include <jpeglib.h>
 #include <stdlib.h>
+#include <assert.h>
 
 #include "glext.h"
 #include "base_config.h"
 #include "base_image.h"
 
+#include "fs.h"
+#include "fs_png.h"
+#include "fs_jpg.h"
+
 /*---------------------------------------------------------------------------*/
 
 void image_size(int *W, int *H, int w, int h)
@@ -39,7 +44,7 @@ static void *image_load_png(const char *filename, int *width,
                                                   int *height,
                                                   int *bytes)
 {
-    FILE *fp;
+    fs_file fh;
 
     png_structp readp = NULL;
     png_infop   infop = NULL;
@@ -48,7 +53,7 @@ static void *image_load_png(const char *filename, int *width,
 
     /* Initialize all PNG import data structures. */
 
-    if (!(fp = fopen(filename, FMODE_RB)))
+    if (!(fh = fs_open(filename, "r")))
         return NULL;
 
     if (!(readp = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
@@ -65,7 +70,7 @@ static void *image_load_png(const char *filename, int *width,
 
         /* Read the PNG header. */
 
-        png_init_io(readp, fp);
+        png_set_read_fn(readp, fh, fs_png_read);
         png_read_info(readp, infop);
 
         png_set_expand(readp);
@@ -114,7 +119,7 @@ static void *image_load_png(const char *filename, int *width,
     /* Free all resources. */
 
     png_destroy_read_struct(&readp, &infop, NULL);
-    fclose(fp);
+    fs_close(fh);
 
     return p;
 }
@@ -124,9 +129,9 @@ static void *image_load_jpg(const char *filename, int *width,
                                                   int *bytes)
 {
     GLubyte *p = NULL;
-    FILE   *fp;
+    fs_file fp;
 
-    if ((fp = fopen(filename, FMODE_RB)))
+    if ((fp = fs_open(filename, "r")))
     {
         struct jpeg_decompress_struct cinfo;
         struct jpeg_error_mgr         jerr;
@@ -137,7 +142,10 @@ static void *image_load_jpg(const char *filename, int *width,
 
         cinfo.err = jpeg_std_error(&jerr);
         jpeg_create_decompress(&cinfo);
-        jpeg_stdio_src(&cinfo, fp);
+
+        /* Set up a VFS source manager. */
+
+        fs_jpg_src(&cinfo, fp);
 
         /* Grab the JPG header info. */
 
@@ -166,7 +174,7 @@ static void *image_load_jpg(const char *filename, int *width,
         jpeg_finish_decompress(&cinfo);
         jpeg_destroy_decompress(&cinfo);
 
-        fclose(fp);
+        fs_close(fp);
     }
 
     return p;
@@ -292,4 +300,38 @@ void image_white(void *p, int w, int h, int b)
         }
 }
 
+/*
+ * Allocate and return an image buffer of the given image flipped horizontally
+ * and/or vertically.
+ */
+void *image_flip(const void *p, int w, int h, int b, int hflip, int vflip)
+{
+    unsigned char *q;
+
+    assert(hflip || vflip);
+
+    if (!p)
+        return NULL;
+
+    if ((q = malloc(w * b * h)))
+    {
+        int r, c, i;
+
+        for (r = 0; r < h; r++)
+            for (c = 0; c < w; c++)
+                for (i = 0; i < b; i++)
+                {
+                    int pr = vflip ? h - r - 1 : r;
+                    int pc = hflip ? w - c - 1 : c;
+
+                    int qi = r  * w * b + c  * b + i;
+                    int pi = pr * w * b + pc * b + i;
+
+                    q[qi] = ((const unsigned char *) p)[pi];
+                }
+        return q;
+    }
+    return NULL;
+}
+
 /*---------------------------------------------------------------------------*/