summaryrefslogtreecommitdiff
path: root/texturepng.cpp
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-02-09 11:38:58 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-02-09 11:38:58 +0100
commit19b6628549c8b80f23976d9b42f2cd853989422c (patch)
tree3d8ad793b39c0e96b3bb6d6ca7e0a2555a17a803 /texturepng.cpp
Initial commit.
Diffstat (limited to 'texturepng.cpp')
-rwxr-xr-xtexturepng.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/texturepng.cpp b/texturepng.cpp
new file mode 100755
index 0000000..19dd4ab
--- /dev/null
+++ b/texturepng.cpp
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <stdexcept>
+#include <png.h>
+#include "texturepng.h"
+
+TexturePNG::TexturePNG(const char* filename) {
+ FILE *file = fopen(filename, "rb");
+ if(!file) {
+ throw(std::runtime_error("TexturePNG file not found"));
+ }
+ png_byte header[8];
+ fread(header, 1, 8, file);
+ if(png_sig_cmp(header, 0, 8)) {
+ throw(std::runtime_error("Invalid PNG file"));
+ }
+ png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+ if(!png_ptr) {
+ throw(std::runtime_error("LibPNG Error (png_ptr)"));
+ }
+ png_infop info_ptr = png_create_info_struct(png_ptr);
+ if(!info_ptr) {
+ throw(std::runtime_error("LibPNG Error (info_ptr)"));
+ }
+ png_infop end_info = png_create_info_struct(png_ptr);
+ if(!end_info) {
+ throw(std::runtime_error("LibPNG Error (end_info)"));
+ }
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ throw(std::runtime_error("LibPNG Error"));
+ }
+ png_init_io(png_ptr, file);
+ png_set_sig_bytes(png_ptr, 8);
+ png_read_png(png_ptr, info_ptr, 0, 0);
+ png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr);
+ width = png_get_image_width(png_ptr, info_ptr);
+ height = png_get_image_height(png_ptr, info_ptr);
+ data = new unsigned char[width * height * 4];
+ for(int y = 0; y < height; y++) {
+ for(int x = 0; x < width * 4; x++) {
+ data[x + y*width*4] = row_pointers[y][x];
+ }
+ }
+ build();
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+}