49 FILE* fp = fopen(filename,
"wb");
52 fprintf(stderr,
"can't open %s\n", filename);
56 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
63 png_infop info_ptr = png_create_info_struct(png_ptr);
67 png_destroy_write_struct(&png_ptr, NULL);
71 if (setjmp(png_jmpbuf(png_ptr)))
74 png_destroy_write_struct(&png_ptr, &info_ptr);
78 png_init_io(png_ptr, fp);
80 int const bit_depth = 8;
86 PNG_COLOR_TYPE_RGB_ALPHA,
88 PNG_COMPRESSION_TYPE_BASE,
89 PNG_FILTER_TYPE_BASE);
92 static_cast<png_colorp
>(png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH *
sizeof(png_color)));
93 png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
100 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
102 png_write_info(png_ptr, info_ptr);
105 size_t const rowLength =
static_cast<size_t>(imageData.
GetWidth()) * 4U;
107 for (
unsigned int row = 0; row < imageData.
GetHeight(); ++row)
109 png_bytep row_pointer =
const_cast<png_bytep
>(
110 reinterpret_cast<png_const_bytep
>(imageDataVector.data() +
static_cast<size_t>(row) * rowLength));
112 png_write_row(png_ptr, row_pointer);
115 png_write_end(png_ptr, info_ptr);
117 png_free(png_ptr, palette);
120 png_destroy_write_struct(&png_ptr, &info_ptr);
135static bool ReadPngFile(
char const* filename,
unsigned int& width,
unsigned int& height, std::vector<std::uint8_t>& rgbaImageDataVector)
138 memset(&image, 0,
sizeof(image));
139 image.version = PNG_IMAGE_VERSION;
141 if (png_image_begin_read_from_file(&image, filename) == 0)
146 image.format = PNG_FORMAT_RGBA;
148 size_t const imageSize = PNG_IMAGE_SIZE(image);
149 png_bytep buffer =
new png_byte[imageSize];
153 png_image_free(&image);
157 if (png_image_finish_read(&image,
164 png_image_free(&image);
169 height = image.height;
171 rgbaImageDataVector.resize(imageSize);
172 std::memcpy(rgbaImageDataVector.data(), buffer, imageSize);
175 png_image_free(&image);
196 const unsigned char* memory,
199 unsigned int& height,
200 std::vector<std::uint8_t>& rgbaImageDataVector)
203 memset(&image, 0,
sizeof(image));
204 image.version = PNG_IMAGE_VERSION;
206 if (png_image_begin_read_from_memory(&image, memory, memorySize) == 0)
211 image.format = PNG_FORMAT_RGBA;
213 size_t const imageSize = PNG_IMAGE_SIZE(image);
214 png_bytep buffer =
new png_byte[imageSize];
218 png_image_free(&image);
222 if (png_image_finish_read(&image,
nullptr, buffer, 0,
nullptr) == 0)
225 png_image_free(&image);
230 height = image.height;
232 rgbaImageDataVector.resize(imageSize);
233 std::memcpy(rgbaImageDataVector.data(), buffer, imageSize);
236 png_image_free(&image);
Represents raw 2D image data stored in memory.
unsigned int GetWidth() const
Returns the image width.
std::vector< uint8_t > GetImageDataRGBA() const
Gets raw image data in RGBA format.
unsigned int GetHeight() const
Returns the image height.
static bool ReadPngFromMemory(const unsigned char *memory, size_t memorySize, unsigned int &width, unsigned int &height, std::vector< std::uint8_t > &rgbaImageDataVector)
Reads a PNG image from memory into RGBA image data.
static void SaveTextureFilePng(rendering_engine::ImageData const &imageData, char const *filename)
Saves image data to a PNG file.
static bool ReadPngFile(char const *filename, unsigned int &width, unsigned int &height, std::vector< std::uint8_t > &rgbaImageDataVector)
Reads a PNG file into RGBA image data.