2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CImageLoaderPNG.h"
|
|
|
|
|
|
|
|
#include <png.h> // use system lib png
|
|
|
|
|
|
|
|
#include "CImage.h"
|
|
|
|
#include "CReadFile.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace video
|
|
|
|
{
|
|
|
|
|
|
|
|
// PNG function for error handling
|
|
|
|
static void png_cpexcept_error(png_structp png_ptr, png_const_charp msg)
|
|
|
|
{
|
|
|
|
os::Printer::log("PNG fatal error", msg, ELL_ERROR);
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PNG function for warning handling
|
|
|
|
static void png_cpexcept_warn(png_structp png_ptr, png_const_charp msg)
|
|
|
|
{
|
|
|
|
os::Printer::log("PNG warning", msg, ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PNG function for file reading
|
|
|
|
void PNGAPI user_read_data_fcn(png_structp png_ptr, png_bytep data, png_size_t length)
|
|
|
|
{
|
|
|
|
png_size_t check;
|
|
|
|
|
|
|
|
// changed by zola {
|
2024-03-20 19:35:52 +01:00
|
|
|
io::IReadFile *file = (io::IReadFile *)png_get_io_ptr(png_ptr);
|
|
|
|
check = (png_size_t)file->read((void *)data, length);
|
2023-10-03 20:37:00 +02:00
|
|
|
// }
|
|
|
|
|
|
|
|
if (check != length)
|
|
|
|
png_error(png_ptr, "Read Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns true if the file maybe is able to be loaded by this class
|
|
|
|
//! based on the file extension (e.g. ".tga")
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CImageLoaderPng::isALoadableFileExtension(const io::path &filename) const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
return core::hasFileExtension(filename, "png");
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//! returns true if the file maybe is able to be loaded by this class
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CImageLoaderPng::isALoadableFileFormat(io::IReadFile *file) const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
png_byte buffer[8];
|
|
|
|
// Read the first few bytes of the PNG file
|
|
|
|
if (file->read(buffer, 8) != 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if it really is a PNG file
|
|
|
|
return !png_sig_cmp(buffer, 0, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// load in the image data
|
2024-03-20 19:35:52 +01:00
|
|
|
IImage *CImageLoaderPng::loadImage(io::IReadFile *file) const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// Used to point to image rows
|
|
|
|
u8 **RowPointers = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
png_byte buffer[8];
|
|
|
|
// Read the first few bytes of the PNG file
|
2024-03-20 19:35:52 +01:00
|
|
|
if (file->read(buffer, 8) != 8) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("LOAD PNG: can't read file (filesize < 8)", file->getFileName(), ELL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it really is a PNG file
|
2024-03-20 19:35:52 +01:00
|
|
|
if (png_sig_cmp(buffer, 0, 8)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("LOAD PNG: not really a png (wrong signature)", file->getFileName(), ELL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the png read struct
|
|
|
|
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
2024-03-20 19:35:52 +01:00
|
|
|
NULL, (png_error_ptr)png_cpexcept_error, (png_error_ptr)png_cpexcept_warn);
|
|
|
|
if (!png_ptr) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("LOAD PNG: Internal PNG create read struct failure", file->getFileName(), ELL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the png info struct
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!info_ptr) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("LOAD PNG: Internal PNG create info struct failure", file->getFileName(), ELL_ERROR);
|
|
|
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for proper error handling
|
2024-03-20 19:35:52 +01:00
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
2023-10-03 20:37:00 +02:00
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] RowPointers;
|
2023-10-03 20:37:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// changed by zola so we don't need to have public FILE pointers
|
|
|
|
png_set_read_fn(png_ptr, file, user_read_data_fcn);
|
|
|
|
|
|
|
|
png_set_sig_bytes(png_ptr, 8); // Tell png that we read the signature
|
|
|
|
|
|
|
|
png_read_info(png_ptr, info_ptr); // Read the info section of the png file
|
|
|
|
|
|
|
|
u32 Width;
|
|
|
|
u32 Height;
|
|
|
|
s32 BitDepth;
|
|
|
|
s32 ColorType;
|
|
|
|
{
|
|
|
|
// Use temporary variables to avoid passing cast pointers
|
2024-03-20 19:35:52 +01:00
|
|
|
png_uint_32 w, h;
|
2023-10-03 20:37:00 +02:00
|
|
|
// Extract info
|
|
|
|
png_get_IHDR(png_ptr, info_ptr,
|
2024-03-20 19:35:52 +01:00
|
|
|
&w, &h,
|
|
|
|
&BitDepth, &ColorType, NULL, NULL, NULL);
|
|
|
|
Width = w;
|
|
|
|
Height = h;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkImageDimensions(Width, Height))
|
|
|
|
png_cpexcept_error(png_ptr, "Unreasonable size");
|
|
|
|
|
|
|
|
// Convert palette color to true color
|
2024-03-20 19:35:52 +01:00
|
|
|
if (ColorType == PNG_COLOR_TYPE_PALETTE)
|
2023-10-03 20:37:00 +02:00
|
|
|
png_set_palette_to_rgb(png_ptr);
|
|
|
|
|
|
|
|
// Convert low bit colors to 8 bit colors
|
2024-03-20 19:35:52 +01:00
|
|
|
if (BitDepth < 8) {
|
|
|
|
if (ColorType == PNG_COLOR_TYPE_GRAY || ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
|
2023-10-03 20:37:00 +02:00
|
|
|
png_set_expand_gray_1_2_4_to_8(png_ptr);
|
|
|
|
else
|
|
|
|
png_set_packing(png_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
|
|
|
|
png_set_tRNS_to_alpha(png_ptr);
|
|
|
|
|
|
|
|
// Convert high bit colors to 8 bit colors
|
|
|
|
if (BitDepth == 16)
|
|
|
|
png_set_strip_16(png_ptr);
|
|
|
|
|
|
|
|
// Convert gray color to true color
|
2024-03-20 19:35:52 +01:00
|
|
|
if (ColorType == PNG_COLOR_TYPE_GRAY || ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
|
2023-10-03 20:37:00 +02:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
|
|
|
|
int intent;
|
|
|
|
const double screen_gamma = 2.2;
|
|
|
|
|
|
|
|
if (png_get_sRGB(png_ptr, info_ptr, &intent))
|
|
|
|
png_set_gamma(png_ptr, screen_gamma, 0.45455);
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
2023-10-03 20:37:00 +02:00
|
|
|
double image_gamma;
|
|
|
|
if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
|
|
|
|
png_set_gamma(png_ptr, screen_gamma, image_gamma);
|
|
|
|
else
|
|
|
|
png_set_gamma(png_ptr, screen_gamma, 0.45455);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the changes in between, as we need to get the new color type
|
|
|
|
// for proper processing of the RGBA type
|
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
|
|
|
{
|
|
|
|
// Use temporary variables to avoid passing cast pointers
|
2024-03-20 19:35:52 +01:00
|
|
|
png_uint_32 w, h;
|
2023-10-03 20:37:00 +02:00
|
|
|
// Extract info
|
|
|
|
png_get_IHDR(png_ptr, info_ptr,
|
2024-03-20 19:35:52 +01:00
|
|
|
&w, &h,
|
|
|
|
&BitDepth, &ColorType, NULL, NULL, NULL);
|
|
|
|
Width = w;
|
|
|
|
Height = h;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert RGBA to BGRA
|
2024-03-20 19:35:52 +01:00
|
|
|
if (ColorType == PNG_COLOR_TYPE_RGB_ALPHA) {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef __BIG_ENDIAN__
|
|
|
|
png_set_swap_alpha(png_ptr);
|
|
|
|
#else
|
|
|
|
png_set_bgr(png_ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the image structure to be filled by png data
|
2024-03-20 19:35:52 +01:00
|
|
|
video::IImage *image = 0;
|
|
|
|
if (ColorType == PNG_COLOR_TYPE_RGB_ALPHA)
|
2023-10-03 20:37:00 +02:00
|
|
|
image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(Width, Height));
|
|
|
|
else
|
|
|
|
image = new CImage(ECF_R8G8B8, core::dimension2d<u32>(Width, Height));
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!image) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("LOAD PNG: Internal PNG create image struct failure", file->getFileName(), ELL_ERROR);
|
|
|
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create array of pointers to rows in image data
|
|
|
|
RowPointers = new png_bytep[Height];
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!RowPointers) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("LOAD PNG: Internal PNG create row pointers failure", file->getFileName(), ELL_ERROR);
|
|
|
|
png_destroy_read_struct(&png_ptr, NULL, NULL);
|
|
|
|
delete image;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill array of pointers to rows in image data
|
2024-03-20 19:35:52 +01:00
|
|
|
unsigned char *data = (unsigned char *)image->getData();
|
|
|
|
for (u32 i = 0; i < Height; ++i) {
|
|
|
|
RowPointers[i] = data;
|
2023-10-03 20:37:00 +02:00
|
|
|
data += image->getPitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
// for proper error handling
|
2024-03-20 19:35:52 +01:00
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
2023-10-03 20:37:00 +02:00
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] RowPointers;
|
2023-10-03 20:37:00 +02:00
|
|
|
delete image;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read data using the library function that handles all transformations including interlacing
|
|
|
|
png_read_image(png_ptr, RowPointers);
|
|
|
|
|
|
|
|
png_read_end(png_ptr, NULL);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] RowPointers;
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, 0); // Clean up memory
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
IImageLoader *createImageLoaderPNG()
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return new CImageLoaderPng();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
} // end namespace irr
|
|
|
|
} // end namespace video
|