summaryrefslogtreecommitdiff
path: root/libpng_wrapper.cpp
diff options
context:
space:
mode:
authorBrett Weiland <brett_weiland@bpcspace.com>2023-01-22 15:34:25 -0600
committerBrett Weiland <brett_weiland@bpcspace.com>2023-01-22 15:34:25 -0600
commit8db9e4cfba7de89b5492203ed1b225297be47f68 (patch)
tree4e21e076bbaabf7b0b37d54e0c9c2eefd8409d5f /libpng_wrapper.cpp
init
Diffstat (limited to 'libpng_wrapper.cpp')
-rw-r--r--libpng_wrapper.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/libpng_wrapper.cpp b/libpng_wrapper.cpp
new file mode 100644
index 0000000..a0d48e3
--- /dev/null
+++ b/libpng_wrapper.cpp
@@ -0,0 +1,46 @@
+#include <string>
+#include <stdio.h>
+#include <string.h>
+#include "libpng_wrapper.hpp"
+
+using namespace std;
+
+png::png(string filename, uint32_t width, uint32_t height) {
+ //have to use the old C way to access files for libPNGs sake
+ output_fp = fopen(filename.c_str(), "wb");
+ if(!output_fp) throw PNG_FILE_ERROR;
+
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if(!png_ptr) throw PNG_LIBPNG_ERROR;
+
+ png_info_ptr = png_create_info_struct(png_ptr);
+ if(!png_info_ptr) throw PNG_LIBPNG_ERROR;
+
+ png_init_io(png_ptr, output_fp);
+
+ png_set_IHDR(png_ptr, png_info_ptr, width, height, DEFAULT_BIT_DEPTH, DEFAULT_COLOR_TYPE,
+ (DEFAULT_INTERLACE) ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+ row_pointers = new png_bytep[height * sizeof(png_bytep)];
+ for(size_t row = 0; row < height; row++)
+ row_pointers[row] = new png_byte[png_get_rowbytes(png_ptr, png_info_ptr)];
+}
+
+png::~png() {
+ png_write_info(png_ptr, png_info_ptr);
+ png_write_image(png_ptr, row_pointers);
+ png_write_end(png_ptr, NULL);
+ png_destroy_write_struct(&png_ptr, &png_info_ptr);
+ fclose(output_fp);
+}
+
+void png::set_pixel(uint32_t x, uint32_t y, png_byte r, png_byte g, png_byte b) {
+ row_pointers[y][x * 3] = r;
+ row_pointers[y][(x * 3) + 1] = g;
+ row_pointers[y][(x * 3) + 2] = b;
+}
+
+
+uint32_t png::width() { return png_get_image_width(png_ptr, png_info_ptr); }
+uint32_t png::height() { return png_get_image_height(png_ptr, png_info_ptr); }