commit 7a0f9778fef9a2fb1e81ef7b81f9332df8ccb28f
parent 8d1a2cfbb0d1af1a4ade2c411f9d52745e2956fa
Author: Brian Swetland <swetland@frotz.net>
Date: Wed, 16 Jan 2013 22:10:45 -0800
add a flag to load_png_*() to control y mirroring
Diffstat:
7 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/loadpng.c b/loadpng.c
@@ -21,7 +21,7 @@
#include "util.h"
-void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch) {
+void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch, int inverty) {
png_structp png;
png_infop info;
png_uint_32 w, h;
@@ -90,8 +90,12 @@ void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch) {
if (!(data = malloc(w * h * ch)))
png_error(png, "cannot allocate image buffer");
- for (i = h-1; i >= 0; i--)
- png_read_row(png, data + (i * w * ch), NULL);
+ if (inverty)
+ for (i = h-1; i >= 0; i--)
+ png_read_row(png, data + (i * w * ch), NULL);
+ else
+ for (i = 0; i < h; i++)
+ png_read_row(png, data + (i * w * ch), NULL);
*_width = w;
*_height = h;
@@ -107,11 +111,11 @@ exit:
return data;
}
-void *load_png_rgba(const char *fn, unsigned *_width, unsigned *_height) {
- return _load_png(fn, _width, _height, 4);
+void *load_png_rgba(const char *fn, unsigned *_width, unsigned *_height, int texture) {
+ return _load_png(fn, _width, _height, 4, texture);
}
-void *load_png_gray(const char *fn, unsigned *_width, unsigned *_height) {
- return _load_png(fn, _width, _height, 1);
+void *load_png_gray(const char *fn, unsigned *_width, unsigned *_height, int texture) {
+ return _load_png(fn, _width, _height, 1, texture);
}
diff --git a/mksdf.c b/mksdf.c
@@ -87,7 +87,7 @@ int main(int argc, char **argv) {
return -1;
}
- tex = load_png_gray(argv[1], &tw, &th);
+ tex = load_png_gray(argv[1], &tw, &th, 0);
if (!tex) {
fprintf(stderr,"cannot load source image '%s'\n", argv[1]);
return -1;
@@ -97,7 +97,7 @@ int main(int argc, char **argv) {
/* output an ascii PGM for now */
printf("P2\n%d %d\n255\n", mw, mh);
- for (y = mh - 1; y >= 0; y--) {
+ for (y = 0; y < mh; y++) {
for (x = 0; x < mw; x++)
printf("%d ", map[y*mw + x]);
printf("\n");
diff --git a/test1.c b/test1.c
@@ -46,7 +46,7 @@ GLfloat texcoords[] = {
};
int scene_init(struct ctxt *c) {
- if (!(texdata = load_png_rgba("texture1.png", &texw, &texh)))
+ if (!(texdata = load_png_rgba("texture1.png", &texw, &texh, 1)))
return -1;
if (!(vert_src = load_file("test1.vertex.glsl", 0)))
return -1;
diff --git a/test2.c b/test2.c
@@ -81,7 +81,7 @@ GLubyte indices[] = {
int scene_init(struct ctxt *c) {
float aspect = ((float) c->width) / ((float) c->height);
- if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh)))
+ if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1)))
return -1;
if (!(vert_src = load_file("test1.vertex.glsl", 0)))
return -1;
diff --git a/test3.c b/test3.c
@@ -42,7 +42,7 @@ struct model *m;
int scene_init(struct ctxt *c) {
float aspect = ((float) c->width) / ((float) c->height);
- if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh)))
+ if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1)))
return -1;
if (!(vert_src = load_file("test1.vertex.glsl", 0)))
return -1;
diff --git a/test4.c b/test4.c
@@ -50,7 +50,7 @@ GLfloat texcoords[] = {
};
int scene_init(struct ctxt *c) {
- if (!(texdata = load_png_gray("texture.sdf.png", &texw, &texh)))
+ if (!(texdata = load_png_gray("texture.sdf.png", &texw, &texh, 1)))
return -1;
if (!(vert_src = load_file("test4.vertex.glsl", 0)))
return -1;
diff --git a/util.h b/util.h
@@ -49,8 +49,8 @@ void mtx_perspective(mat4 out,
float znear, float zfar);
/* file io helpers */
-void *load_png_rgba(const char *fn, unsigned *width, unsigned *height);
-void *load_png_gray(const char *fn, unsigned *width, unsigned *height);
+void *load_png_rgba(const char *fn, unsigned *width, unsigned *height, int texture);
+void *load_png_gray(const char *fn, unsigned *width, unsigned *height, int texture);
void *load_file(const char *fn, unsigned *sz);
/* model helpers */