#include #include #include #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); }