#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)
int *height,
int *bytes)
{
- FILE *fp;
+ fs_file fh;
png_structp readp = NULL;
png_infop infop = NULL;
/* 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)))
/* 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);
/* Free all resources. */
png_destroy_read_struct(&readp, &infop, NULL);
- fclose(fp);
+ fs_close(fh);
return p;
}
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;
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. */
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
- fclose(fp);
+ fs_close(fp);
}
return p;
}
}
+/*
+ * 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;
+}
+
/*---------------------------------------------------------------------------*/