24 fp(fopen(name, (readMode ?
"rb" :
"wb"))),
42 png_destroy_read_struct(&png_ptr, &info_ptr,
nullptr);
46 png_destroy_write_struct(&png_ptr, &info_ptr);
67 helper.
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
73 helper.info_ptr = png_create_info_struct(helper.png_ptr);
79 if (setjmp(png_jmpbuf(helper.png_ptr)))
84 png_init_io(helper.png_ptr, helper.fp);
85 png_set_sig_bytes(helper.png_ptr, 0);
87 png_read_png(helper.png_ptr, helper.info_ptr, 0,
nullptr);
90 int bit_depth, color_type, interlace_method, compression_method, filter_method;
91 png_get_IHDR(helper.png_ptr, helper.info_ptr, &width, &height, &bit_depth,
92 &color_type, &interlace_method, &compression_method, &filter_method);
94 if (bit_depth != 8 && bit_depth != 16)
100 if (color_type & PNG_COLOR_MASK_PALETTE)
106 if (interlace_method != 0 || filter_method != 0)
112 png_bytepp rows = png_get_rows(helper.png_ptr, helper.info_ptr);
114 std::shared_ptr<Texture2>
texture;
117 uint8_t
const*
src =
nullptr;
118 uint8_t* trg =
nullptr;
119 if (color_type == PNG_COLOR_TYPE_RGBA)
122 trg = texture->Get<uint8_t>();
125 src =
reinterpret_cast<uint8_t const*
>(rows[
y]);
126 for (png_uint_32
x = 0;
x <
width; ++
x)
128 for (
int i = 0; i < 4; ++i)
130 *trg++ = src[4 *
x + i];
135 else if (color_type == PNG_COLOR_TYPE_RGB)
138 trg = texture->Get<uint8_t>();
141 src =
reinterpret_cast<uint8_t const*
>(rows[
y]);
142 for (png_uint_32
x = 0;
x <
width; ++
x)
144 for (
int i = 0; i < 3; ++i)
146 *trg++ = src[3 *
x + i];
152 else if (color_type == PNG_COLOR_TYPE_GRAY)
155 trg = texture->Get<uint8_t>();
158 src =
reinterpret_cast<uint8_t const*
>(rows[
y]);
159 for (png_uint_32
x = 0;
x <
width; ++
x)
165 else if (color_type == PNG_COLOR_TYPE_GA)
168 trg = texture->Get<uint8_t>();
171 src =
reinterpret_cast<uint8_t const*
>(rows[
y]);
172 for (png_uint_32
x = 0;
x <
width; ++
x)
174 for (
int i = 0; i < 2; ++i)
176 *trg++ = src[2 *
x + i];
184 uint16_t
const*
src =
nullptr;
185 uint16_t* trg =
nullptr;
186 if (color_type == PNG_COLOR_TYPE_RGBA)
189 trg = texture->Get<uint16_t>();
192 src =
reinterpret_cast<uint16_t const*
>(rows[
y]);
193 for (png_uint_32
x = 0;
x <
width; ++
x)
195 for (
int i = 0; i < 4; ++i)
197 *trg++ = src[4 *
x + i];
202 else if (color_type == PNG_COLOR_TYPE_RGB)
205 trg = texture->Get<uint16_t>();
208 src =
reinterpret_cast<uint16_t const*
>(rows[
y]);
209 for (png_uint_32
x = 0;
x <
width; ++
x)
211 for (
int i = 0; i < 3; ++i)
213 *trg++ = src[3 *
x + i];
219 else if (color_type == PNG_COLOR_TYPE_GRAY)
222 trg = texture->Get<uint16_t>();
225 src =
reinterpret_cast<uint16_t const*
>(rows[
y]);
226 for (png_uint_32
x = 0;
x <
width; ++
x)
232 else if (color_type == PNG_COLOR_TYPE_GA)
235 trg = texture->Get<uint16_t>();
238 src =
reinterpret_cast<uint16_t const*
>(rows[
y]);
239 for (png_uint_32
x = 0;
x <
width; ++
x)
241 for (
int i = 0; i < 2; ++i)
243 *trg++ = src[2 *
x + i];
262 png_uint_32 bytes_per_pixel = 0;
263 switch (texture->GetFormat())
268 color_type = PNG_COLOR_TYPE_RGBA;
274 color_type = PNG_COLOR_TYPE_GA;
279 color_type = PNG_COLOR_TYPE_GRAY;
284 color_type = PNG_COLOR_TYPE_RGBA;
289 color_type = PNG_COLOR_TYPE_GA;
294 color_type = PNG_COLOR_TYPE_GRAY;
309 helper.
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr,
nullptr,
nullptr);
315 helper.info_ptr = png_create_info_struct(helper.png_ptr);
316 if (!helper.info_ptr)
321 if (setjmp(png_jmpbuf(helper.png_ptr)))
326 png_init_io(helper.png_ptr, helper.fp);
328 png_uint_32
width = texture->GetWidth();
329 png_uint_32
height = texture->GetHeight();
330 int interlace_method = PNG_INTERLACE_NONE;
331 int compression_method = PNG_COMPRESSION_TYPE_BASE;
332 int filter_method = PNG_FILTER_TYPE_BASE;
334 png_set_IHDR(helper.png_ptr, helper.info_ptr, width, height, bit_depth,
335 color_type, interlace_method, compression_method, filter_method);
337 std::vector<uint8_t*> row_pointers(height);
338 uint8_t* texels = texture->Get<uint8_t>();
341 row_pointers[
y] = texels +
y * width * bytes_per_pixel;
344 png_write_info(helper.png_ptr, helper.info_ptr);
345 png_write_image(helper.png_ptr, row_pointers.data());
346 png_write_end(helper.png_ptr, helper.info_ptr);
WICFileIOPrivateHelper(char const *name, bool readMode)
GLuint const GLchar * name
GLsizei const GLchar *const * string
GLint GLsizei GLsizei height
static std::shared_ptr< Texture2 > Load(std::string const &filename, bool wantMipmaps)
static bool SaveToPNG(std::string const &filename, std::shared_ptr< Texture2 > const &texture)
~WICFileIOPrivateHelper()