|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <png.h> |
| 4 | + |
| 5 | +#include <fstream> |
| 6 | +#include <stdexcept> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +class PNG { |
| 11 | + private: |
| 12 | + std::string fileName; |
| 13 | + std::ifstream input; |
| 14 | + std::ofstream output; |
| 15 | + png_structp pngPtr = nullptr; |
| 16 | + png_infop infoPtr = nullptr; |
| 17 | + int imageWidth = 0, imageHeight = 0; |
| 18 | + png_byte imageColorType = 0, imageBitDepth = 8; |
| 19 | + std::vector<png_byte> imageBuffer; |
| 20 | + |
| 21 | + static void png_write_callback(png_structp png, png_bytep data, |
| 22 | + png_size_t length) { |
| 23 | + auto stream = reinterpret_cast<std::ostream *>(png_get_io_ptr(png)); |
| 24 | + stream->write(reinterpret_cast<char *>(data), length); |
| 25 | + if (!*stream) png_error(png, "WRITE Error"); |
| 26 | + } |
| 27 | + |
| 28 | + static void png_read_callback(png_structp png, png_bytep data, |
| 29 | + png_size_t length) { |
| 30 | + auto stream = reinterpret_cast<std::istream *>(png_get_io_ptr(png)); |
| 31 | + stream->read(reinterpret_cast<char *>(data), length); |
| 32 | + if (!*stream) png_error(png, "READ Error"); |
| 33 | + } |
| 34 | + |
| 35 | + int get_channel_count() const { |
| 36 | + switch (imageColorType) { |
| 37 | + case PNG_COLOR_TYPE_GRAY: return 1; |
| 38 | + case PNG_COLOR_TYPE_GRAY_ALPHA: return 2; |
| 39 | + case PNG_COLOR_TYPE_RGB: return 3; |
| 40 | + case PNG_COLOR_TYPE_RGBA: return 4; |
| 41 | + default: return 3; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public: |
| 46 | + PNG(const std::string &fileName) : fileName(fileName) {} |
| 47 | + |
| 48 | + ~PNG() { |
| 49 | + if (pngPtr) { |
| 50 | + if (input.is_open()) png_destroy_read_struct(&pngPtr, &infoPtr, nullptr); |
| 51 | + if (output.is_open()) png_destroy_write_struct(&pngPtr, &infoPtr); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + bool read() { |
| 56 | + input.open(fileName, std::ios::binary); |
| 57 | + if (!input) return false; |
| 58 | + |
| 59 | + pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, |
| 60 | + nullptr); |
| 61 | + if (!pngPtr) return false; |
| 62 | + infoPtr = png_create_info_struct(pngPtr); |
| 63 | + if (!infoPtr || setjmp(png_jmpbuf(pngPtr))) return false; |
| 64 | + |
| 65 | + png_set_read_fn(pngPtr, static_cast<void *>(&input), png_read_callback); |
| 66 | + png_read_info(pngPtr, infoPtr); |
| 67 | + |
| 68 | + imageWidth = png_get_image_width(pngPtr, infoPtr); |
| 69 | + imageHeight = png_get_image_height(pngPtr, infoPtr); |
| 70 | + imageColorType = png_get_color_type(pngPtr, infoPtr); |
| 71 | + imageBitDepth = png_get_bit_depth(pngPtr, infoPtr); |
| 72 | + |
| 73 | + png_read_update_info(pngPtr, infoPtr); |
| 74 | + |
| 75 | + int rowBytes = png_get_rowbytes(pngPtr, infoPtr); |
| 76 | + imageBuffer.resize(imageHeight * rowBytes); |
| 77 | + std::vector<png_bytep> rows(imageHeight); |
| 78 | + for (int y = 0; y < imageHeight; y++) rows[y] = &imageBuffer[y * rowBytes]; |
| 79 | + |
| 80 | + png_read_image(pngPtr, rows.data()); |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + bool write() { |
| 85 | + output.open(fileName, std::ios::binary); |
| 86 | + if (!output) return false; |
| 87 | + |
| 88 | + int rowBytes = imageWidth * get_channel_count(); |
| 89 | + if (imageBuffer.size() != static_cast<size_t>(imageHeight * rowBytes)) |
| 90 | + imageBuffer.resize(imageHeight * rowBytes); |
| 91 | + |
| 92 | + pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, |
| 93 | + nullptr); |
| 94 | + if (!pngPtr) return false; |
| 95 | + infoPtr = png_create_info_struct(pngPtr); |
| 96 | + if (!infoPtr || setjmp(png_jmpbuf(pngPtr))) return false; |
| 97 | + |
| 98 | + png_set_write_fn(pngPtr, static_cast<void *>(&output), png_write_callback, |
| 99 | + nullptr); |
| 100 | + png_set_IHDR(pngPtr, infoPtr, imageWidth, imageHeight, imageBitDepth, |
| 101 | + imageColorType, PNG_INTERLACE_NONE, |
| 102 | + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 103 | + png_write_info(pngPtr, infoPtr); |
| 104 | + |
| 105 | + std::vector<png_bytep> rows(imageHeight); |
| 106 | + for (int y = 0; y < imageHeight; y++) rows[y] = &imageBuffer[y * rowBytes]; |
| 107 | + |
| 108 | + png_write_image(pngPtr, rows.data()); |
| 109 | + png_write_end(pngPtr, nullptr); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + png_byte &pixel(int x, int y, int channel = 0) { |
| 114 | + return imageBuffer[(y * imageWidth + x) * get_channel_count() + channel]; |
| 115 | + } |
| 116 | + |
| 117 | + // Getters |
| 118 | + int get_width() const { return imageWidth; } |
| 119 | + int get_height() const { return imageHeight; } |
| 120 | + png_byte get_color_type() const { return imageColorType; } |
| 121 | + png_byte get_bit_depth() const { return imageBitDepth; } |
| 122 | + const std::vector<png_byte> &get_buffer() const { return imageBuffer; } |
| 123 | + |
| 124 | + // Setters |
| 125 | + void set_width(int w) { imageWidth = w; } |
| 126 | + void set_height(int h) { imageHeight = h; } |
| 127 | + void set_color_type(png_byte ct) { imageColorType = ct; } |
| 128 | + void set_bit_depth(png_byte bd) { imageBitDepth = bd; } |
| 129 | + void set_buffer(const std::vector<png_byte> &buf) { imageBuffer = buf; } |
| 130 | +}; |
0 commit comments