From a21cbae82a45d9e817e5af2ce7674180ab406c2b Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 7 Jan 2017 12:10:42 +0000 Subject: [PATCH 1/3] Move image_io to C++ --- src/openrct2/Imaging.cpp | 294 +++++++++++++++++++++++++ src/openrct2/{image_io.h => Imaging.h} | 24 +- src/openrct2/cmdline_sprite.c | 4 +- src/openrct2/image_io.c | 250 --------------------- src/openrct2/interface/screenshot.c | 2 +- src/openrct2/openrct2.vcxproj | 4 +- 6 files changed, 318 insertions(+), 260 deletions(-) create mode 100644 src/openrct2/Imaging.cpp rename src/openrct2/{image_io.h => Imaging.h} (53%) delete mode 100644 src/openrct2/image_io.c diff --git a/src/openrct2/Imaging.cpp b/src/openrct2/Imaging.cpp new file mode 100644 index 0000000000..222adf3693 --- /dev/null +++ b/src/openrct2/Imaging.cpp @@ -0,0 +1,294 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#include +#include "core/Guard.hpp" +#include "core/Memory.hpp" + +#include "Imaging.h" + +namespace Imaging +{ + static void PngReadData(png_structp png_ptr, png_bytep data, png_size_t length); + static void PngWriteData(png_structp png_ptr, png_bytep data, png_size_t length); + static void PngFlush(png_structp png_ptr); + static void PngWarning(png_structp png_ptr, const char * b); + static void PngError(png_structp png_ptr, const char * b); + + bool PngRead(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path) + { + png_structp png_ptr; + png_infop info_ptr; + unsigned int sig_read = 0; + + // Setup PNG structures + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); + if (png_ptr == nullptr) + { + return false; + } + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == nullptr) + { + png_destroy_read_struct(&png_ptr, nullptr, nullptr); + return false; + } + + // Open PNG file + SDL_RWops * fp = SDL_RWFromFile(path, "rb"); + if (fp == nullptr) + { + return false; + } + + // Set error handling + if (setjmp(png_jmpbuf(png_ptr))) + { + png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); + SDL_RWclose(fp); + return false; + } + + // Setup png reading + png_set_read_fn(png_ptr, fp, PngReadData); + png_set_sig_bytes(png_ptr, sig_read); + + // To simplify the reading process, convert 4-16 bit data to 24-32 bit data + png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, nullptr); + + // Read header + png_uint_32 pngWidth, pngHeight; + int bitDepth, colourType, interlaceType; + png_get_IHDR(png_ptr, info_ptr, &pngWidth, &pngHeight, &bitDepth, &colourType, &interlaceType, nullptr, nullptr); + + // Read pixels as 32bpp RGBA data + png_size_t rowBytes = png_get_rowbytes(png_ptr, info_ptr); + png_bytepp rowPointers = png_get_rows(png_ptr, info_ptr); + uint8 * pngPixels = Memory::Allocate(pngWidth * pngHeight * 4); + uint8 * dst = pngPixels; + if (colourType == PNG_COLOR_TYPE_RGB) + { + // 24-bit PNG (no alpha) + Guard::Assert(rowBytes == pngWidth * 3, GUARD_LINE); + for (png_uint_32 i = 0; i < pngHeight; i++) + { + uint8 * src = rowPointers[i]; + for (png_uint_32 x = 0; x < pngWidth; x++) + { + *dst++ = *src++; + *dst++ = *src++; + *dst++ = *src++; + *dst++ = 255; + } + } + } else + { + // 32-bit PNG (with alpha) + Guard::Assert(rowBytes == pngWidth * 4, GUARD_LINE); + for (png_uint_32 i = 0; i < pngHeight; i++) + { + Memory::Copy(dst, rowPointers[i], rowBytes); + dst += rowBytes; + } + } + + // Close the PNG + png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); + SDL_RWclose(fp); + + // Return the output data + *pixels = (uint8*)pngPixels; + if (width != nullptr) *width = pngWidth; + if (height != nullptr) *height = pngHeight; + return true; + } + + bool PngWrite(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path) + { + // Get image size + int stride = dpi->width + dpi->pitch; + + // Setup PNG + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); + if (png_ptr == nullptr) + { + return false; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == nullptr) + { + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); + return false; + } + + png_colorp png_palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); + for (int i = 0; i < 256; i++) + { + const rct_palette_entry *entry = &palette->entries[i]; + png_palette[i].blue = entry->blue; + png_palette[i].green = entry->green; + png_palette[i].red = entry->red; + } + + png_set_PLTE(png_ptr, info_ptr, png_palette, PNG_MAX_PALETTE_LENGTH); + + // Open file for writing + SDL_RWops * file = SDL_RWFromFile(path, "wb"); + if (file == nullptr) + { + png_free(png_ptr, png_palette); + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); + return false; + } + png_set_write_fn(png_ptr, file, PngWriteData, PngFlush); + + // Set error handler + if (setjmp(png_jmpbuf(png_ptr))) + { + png_free(png_ptr, png_palette); + png_destroy_write_struct(&png_ptr, &info_ptr); + SDL_RWclose(file); + return false; + } + + // Write header + png_set_IHDR( + png_ptr, info_ptr, dpi->width, dpi->height, 8, + PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT + ); + png_byte transparentIndex = 0; + png_set_tRNS(png_ptr, info_ptr, &transparentIndex, 1, nullptr); + png_write_info(png_ptr, info_ptr); + + // Write pixels + uint8 *bits = dpi->bits; + for (int y = 0; y < dpi->height; y++) + { + png_write_row(png_ptr, (png_byte *)bits); + bits += stride; + } + + // Finish + png_write_end(png_ptr, nullptr); + SDL_RWclose(file); + + png_free(png_ptr, png_palette); + png_destroy_write_struct(&png_ptr, &info_ptr); + return true; + } + + bool PngWrite32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path) + { + // Setup PNG + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PngError, PngWarning); + if (png_ptr == nullptr) + { + return false; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == nullptr) + { + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); + return false; + } + + // Open file for writing + SDL_RWops * file = SDL_RWFromFile(path, "wb"); + if (file == nullptr) + { + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); + return false; + } + png_set_write_fn(png_ptr, file, PngWriteData, PngFlush); + + // Set error handler + if (setjmp(png_jmpbuf(png_ptr))) + { + png_destroy_write_struct(&png_ptr, &info_ptr); + SDL_RWclose(file); + return false; + } + + // Write header + png_set_IHDR( + png_ptr, info_ptr, width, height, 8, + PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT + ); + png_write_info(png_ptr, info_ptr); + + // Write pixels + uint8 * bits = (uint8 *)pixels; + for (int y = 0; y < height; y++) + { + png_write_row(png_ptr, (png_byte *)bits); + bits += width * 4; + } + + // Finish + png_write_end(png_ptr, nullptr); + SDL_RWclose(file); + + png_destroy_write_struct(&png_ptr, &info_ptr); + return true; + } + + static void PngReadData(png_structp png_ptr, png_bytep data, png_size_t length) + { + SDL_RWops * file = (SDL_RWops *)png_get_io_ptr(png_ptr); + SDL_RWread(file, data, length, 1); + } + + static void PngWriteData(png_structp png_ptr, png_bytep data, png_size_t length) + { + SDL_RWops * file = (SDL_RWops *)png_get_io_ptr(png_ptr); + SDL_RWwrite(file, data, length, 1); + } + + static void PngFlush(png_structp png_ptr) + { + } + + static void PngWarning(png_structp png_ptr, const char * b) + { + log_warning(b); + } + + static void PngError(png_structp png_ptr, const char * b) + { + log_error(b); + } +} + +extern "C" +{ + bool image_io_png_read(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path) + { + return Imaging::PngRead(pixels, width, height, path); + } + + bool image_io_png_write(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path) + { + return Imaging::PngWrite(dpi, palette, path); + } + + bool image_io_png_write_32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path) + { + return Imaging::PngWrite32bpp(width, height, pixels, path); + } +} diff --git a/src/openrct2/image_io.h b/src/openrct2/Imaging.h similarity index 53% rename from src/openrct2/image_io.h rename to src/openrct2/Imaging.h index 82a45a91b4..f2fe5477a7 100644 --- a/src/openrct2/image_io.h +++ b/src/openrct2/Imaging.h @@ -14,15 +14,29 @@ *****************************************************************************/ #pragma endregion -#ifndef _IMAGE_IO_H_ -#define _IMAGE_IO_H_ +#pragma once #include "common.h" #include "drawing/drawing.h" -bool image_io_png_read(uint8 **pixels, uint32 *width, uint32 *height, const utf8 *path); +#ifdef __cplusplus -bool image_io_png_write(const rct_drawpixelinfo *dpi, const rct_palette *palette, const utf8 *path); -bool image_io_png_write_32bpp(sint32 width, sint32 height, const void *pixels, const utf8 *path); +namespace Imaging +{ + bool PngRead(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path); + bool PngWrite(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path); + bool PngWrite32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path); +} +#endif // __cplusplus + +#ifdef __cplusplus +extern "C" +{ +#endif + bool image_io_png_read(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path); + bool image_io_png_write(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path); + bool image_io_png_write_32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path); +#ifdef __cplusplus +} #endif diff --git a/src/openrct2/cmdline_sprite.c b/src/openrct2/cmdline_sprite.c index a791746ffa..b076e2785c 100644 --- a/src/openrct2/cmdline_sprite.c +++ b/src/openrct2/cmdline_sprite.c @@ -14,12 +14,12 @@ *****************************************************************************/ #pragma endregion +#include "cmdline_sprite.h" #include "drawing/drawing.h" -#include "image_io.h" +#include "Imaging.h" #include "OpenRCT2.h" #include "platform/platform.h" #include "util/util.h" -#include "cmdline_sprite.h" #define MODE_DEFAULT 0 #define MODE_CLOSEST 1 diff --git a/src/openrct2/image_io.c b/src/openrct2/image_io.c deleted file mode 100644 index 65d8a8cb20..0000000000 --- a/src/openrct2/image_io.c +++ /dev/null @@ -1,250 +0,0 @@ -#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers -/***************************************************************************** - * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. - * - * OpenRCT2 is the work of many authors, a full list can be found in contributors.md - * For more information, visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * A full copy of the GNU General Public License can be found in licence.txt - *****************************************************************************/ -#pragma endregion - -#include - -#include "image_io.h" - -static void my_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length); -static void my_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length); -static void my_png_flush(png_structp png_ptr); - -bool image_io_png_read(uint8 **pixels, uint32 *width, uint32 *height, const utf8 *path) -{ - png_structp png_ptr; - png_infop info_ptr; - unsigned int sig_read = 0; - - // Setup PNG structures - png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (png_ptr == NULL) { - return false; - } - - info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) { - png_destroy_read_struct(&png_ptr, NULL, NULL); - return false; - } - - // Open PNG file - SDL_RWops *fp = SDL_RWFromFile(path, "rb"); - if (fp == NULL) { - return false; - } - - // Set error handling - if (setjmp(png_jmpbuf(png_ptr))) { - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - SDL_RWclose(fp); - return false; - } - - // Setup png reading - png_set_read_fn(png_ptr, fp, my_png_read_data); - png_set_sig_bytes(png_ptr, sig_read); - - // To simplify the reading process, convert 4-16 bit data to 24-32 bit data - png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL); - - // Read header - png_uint_32 pngWidth, pngHeight; - int bit_depth, colour_type, interlace_type; - png_get_IHDR(png_ptr, info_ptr, &pngWidth, &pngHeight, &bit_depth, &colour_type, &interlace_type, NULL, NULL); - - // Read pixels as 32bpp RGBA data - png_size_t rowBytes = png_get_rowbytes(png_ptr, info_ptr); - png_bytepp rowPointers = png_get_rows(png_ptr, info_ptr); - uint8 *pngPixels = (uint8*)malloc(pngWidth * pngHeight * 4); - uint8 *dst = pngPixels; - if (colour_type == PNG_COLOR_TYPE_RGB) { - // 24-bit PNG (no alpha) - assert(rowBytes == pngWidth * 3); - for (png_uint_32 i = 0; i < pngHeight; i++) { - uint8 *src = rowPointers[i]; - for (png_uint_32 x = 0; x < pngWidth; x++) { - *dst++ = *src++; - *dst++ = *src++; - *dst++ = *src++; - *dst++ = 255; - } - } - } else { - // 32-bit PNG (with alpha) - assert(rowBytes == pngWidth * 4); - for (png_uint_32 i = 0; i < pngHeight; i++) { - memcpy(dst, rowPointers[i], rowBytes); - dst += rowBytes; - } - } - - // Close the PNG - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - SDL_RWclose(fp); - - // Return the output data - *pixels = (uint8*)pngPixels; - if (width != NULL) *width = pngWidth; - if (height != NULL) *height = pngHeight; - return true; -} - -bool image_io_png_write(const rct_drawpixelinfo *dpi, const rct_palette *palette, const utf8 *path) -{ - // Get image size - int stride = dpi->width + dpi->pitch; - - // Setup PNG - png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (png_ptr == NULL) { - return false; - } - - png_infop info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) { - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - return false; - } - - png_colorp png_palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); - for (int i = 0; i < 256; i++) { - const rct_palette_entry *entry = &palette->entries[i]; - png_palette[i].blue = entry->blue; - png_palette[i].green = entry->green; - png_palette[i].red = entry->red; - } - - png_set_PLTE(png_ptr, info_ptr, png_palette, PNG_MAX_PALETTE_LENGTH); - - // Open file for writing - SDL_RWops *file = SDL_RWFromFile(path, "wb"); - if (file == NULL) { - png_free(png_ptr, png_palette); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - return false; - } - png_set_write_fn(png_ptr, file, my_png_write_data, my_png_flush); - - // Set error handler - if (setjmp(png_jmpbuf(png_ptr))) { - png_free(png_ptr, png_palette); - png_destroy_write_struct(&png_ptr, &info_ptr); - SDL_RWclose(file); - return false; - } - - // Write header - png_set_IHDR( - png_ptr, info_ptr, dpi->width, dpi->height, 8, - PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT - ); - png_byte transparentIndex = 0; - png_set_tRNS(png_ptr, info_ptr, &transparentIndex, 1, NULL); - png_write_info(png_ptr, info_ptr); - - // Write pixels - uint8 *bits = dpi->bits; - for (int y = 0; y < dpi->height; y++) { - png_write_row(png_ptr, (png_byte *)bits); - bits += stride; - } - - // Finish - png_write_end(png_ptr, NULL); - SDL_RWclose(file); - - png_free(png_ptr, png_palette); - png_destroy_write_struct(&png_ptr, &info_ptr); - return true; -} - -static void image_io_png_warning(png_structp png_ptr, const char *b) -{ - log_warning(b); -} - -static void image_io_png_error(png_structp png_ptr, const char *b) -{ - log_error(b); -} - -bool image_io_png_write_32bpp(sint32 width, sint32 height, const void *pixels, const utf8 *path) -{ - // Setup PNG - png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, image_io_png_error, image_io_png_warning); - if (png_ptr == NULL) { - return false; - } - - png_infop info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) { - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - return false; - } - - // Open file for writing - SDL_RWops *file = SDL_RWFromFile(path, "wb"); - if (file == NULL) { - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - return false; - } - png_set_write_fn(png_ptr, file, my_png_write_data, my_png_flush); - - // Set error handler - if (setjmp(png_jmpbuf(png_ptr))) { - png_destroy_write_struct(&png_ptr, &info_ptr); - SDL_RWclose(file); - return false; - } - - // Write header - png_set_IHDR( - png_ptr, info_ptr, width, height, 8, - PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT - ); - png_write_info(png_ptr, info_ptr); - - // Write pixels - uint8 *bits = (uint8*)pixels; - for (int y = 0; y < height; y++) { - png_write_row(png_ptr, (png_byte *)bits); - bits += width * 4; - } - - // Finish - png_write_end(png_ptr, NULL); - SDL_RWclose(file); - - png_destroy_write_struct(&png_ptr, &info_ptr); - return true; -} - -static void my_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) -{ - SDL_RWops *file = (SDL_RWops*)png_get_io_ptr(png_ptr); - SDL_RWread(file, data, length, 1); -} - -static void my_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length) -{ - SDL_RWops *file = (SDL_RWops*)png_get_io_ptr(png_ptr); - SDL_RWwrite(file, data, length, 1); -} - -static void my_png_flush(png_structp png_ptr) -{ - -} diff --git a/src/openrct2/interface/screenshot.c b/src/openrct2/interface/screenshot.c index 5c0aea57b5..383e6ce881 100644 --- a/src/openrct2/interface/screenshot.c +++ b/src/openrct2/interface/screenshot.c @@ -18,7 +18,7 @@ #include "../config.h" #include "../drawing/drawing.h" #include "../game.h" -#include "../image_io.h" +#include "../Imaging.h" #include "../intro.h" #include "../localisation/localisation.h" #include "../OpenRCT2.h" diff --git a/src/openrct2/openrct2.vcxproj b/src/openrct2/openrct2.vcxproj index 2cb3c080b4..271095668d 100644 --- a/src/openrct2/openrct2.vcxproj +++ b/src/openrct2/openrct2.vcxproj @@ -145,7 +145,7 @@ - + @@ -487,7 +487,7 @@ - + From 4680aeab9b4b8a38d2a063e7ee4b6394c54f8531 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 7 Jan 2017 12:23:59 +0000 Subject: [PATCH 2/3] Use FileStream instead of SDL2 --- src/openrct2/Imaging.cpp | 268 ++++++++++++++++++++------------------- 1 file changed, 138 insertions(+), 130 deletions(-) diff --git a/src/openrct2/Imaging.cpp b/src/openrct2/Imaging.cpp index 222adf3693..81795ee03a 100644 --- a/src/openrct2/Imaging.cpp +++ b/src/openrct2/Imaging.cpp @@ -15,6 +15,8 @@ #pragma endregion #include +#include "core/Exception.hpp" +#include "core/FileStream.hpp" #include "core/Guard.hpp" #include "core/Memory.hpp" @@ -49,76 +51,84 @@ namespace Imaging } // Open PNG file - SDL_RWops * fp = SDL_RWFromFile(path, "rb"); - if (fp == nullptr) + try { - return false; - } + auto fs = FileStream(path, FILE_MODE_OPEN); - // Set error handling - if (setjmp(png_jmpbuf(png_ptr))) - { - png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); - SDL_RWclose(fp); - return false; - } - - // Setup png reading - png_set_read_fn(png_ptr, fp, PngReadData); - png_set_sig_bytes(png_ptr, sig_read); - - // To simplify the reading process, convert 4-16 bit data to 24-32 bit data - png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, nullptr); - - // Read header - png_uint_32 pngWidth, pngHeight; - int bitDepth, colourType, interlaceType; - png_get_IHDR(png_ptr, info_ptr, &pngWidth, &pngHeight, &bitDepth, &colourType, &interlaceType, nullptr, nullptr); - - // Read pixels as 32bpp RGBA data - png_size_t rowBytes = png_get_rowbytes(png_ptr, info_ptr); - png_bytepp rowPointers = png_get_rows(png_ptr, info_ptr); - uint8 * pngPixels = Memory::Allocate(pngWidth * pngHeight * 4); - uint8 * dst = pngPixels; - if (colourType == PNG_COLOR_TYPE_RGB) - { - // 24-bit PNG (no alpha) - Guard::Assert(rowBytes == pngWidth * 3, GUARD_LINE); - for (png_uint_32 i = 0; i < pngHeight; i++) + // Set error handling + if (setjmp(png_jmpbuf(png_ptr))) { - uint8 * src = rowPointers[i]; - for (png_uint_32 x = 0; x < pngWidth; x++) + png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); + return false; + } + + // Setup png reading + png_set_read_fn(png_ptr, &fs, PngReadData); + png_set_sig_bytes(png_ptr, sig_read); + + // To simplify the reading process, convert 4-16 bit data to 24-32 bit data + png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, nullptr); + + // Read header + png_uint_32 pngWidth, pngHeight; + int bitDepth, colourType, interlaceType; + png_get_IHDR(png_ptr, info_ptr, &pngWidth, &pngHeight, &bitDepth, &colourType, &interlaceType, nullptr, nullptr); + + // Read pixels as 32bpp RGBA data + png_size_t rowBytes = png_get_rowbytes(png_ptr, info_ptr); + png_bytepp rowPointers = png_get_rows(png_ptr, info_ptr); + uint8 * pngPixels = Memory::Allocate(pngWidth * pngHeight * 4); + uint8 * dst = pngPixels; + if (colourType == PNG_COLOR_TYPE_RGB) + { + // 24-bit PNG (no alpha) + Guard::Assert(rowBytes == pngWidth * 3, GUARD_LINE); + for (png_uint_32 i = 0; i < pngHeight; i++) { - *dst++ = *src++; - *dst++ = *src++; - *dst++ = *src++; - *dst++ = 255; + uint8 * src = rowPointers[i]; + for (png_uint_32 x = 0; x < pngWidth; x++) + { + *dst++ = *src++; + *dst++ = *src++; + *dst++ = *src++; + *dst++ = 255; + } } } - } else - { - // 32-bit PNG (with alpha) - Guard::Assert(rowBytes == pngWidth * 4, GUARD_LINE); - for (png_uint_32 i = 0; i < pngHeight; i++) + else { - Memory::Copy(dst, rowPointers[i], rowBytes); - dst += rowBytes; + // 32-bit PNG (with alpha) + Guard::Assert(rowBytes == pngWidth * 4, GUARD_LINE); + for (png_uint_32 i = 0; i < pngHeight; i++) + { + Memory::Copy(dst, rowPointers[i], rowBytes); + dst += rowBytes; + } } + + // Close the PNG + png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); + + // Return the output data + *pixels = (uint8*)pngPixels; + if (width != nullptr) *width = pngWidth; + if (height != nullptr) *height = pngHeight; + + return true; + } + catch (Exception) + { + *pixels = nullptr; + if (width != nullptr) *width = 0; + if (height != nullptr) *height = 0; + return false; } - - // Close the PNG - png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); - SDL_RWclose(fp); - - // Return the output data - *pixels = (uint8*)pngPixels; - if (width != nullptr) *width = pngWidth; - if (height != nullptr) *height = pngHeight; - return true; } bool PngWrite(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path) { + bool result = false; + // Get image size int stride = dpi->width + dpi->pitch; @@ -147,53 +157,52 @@ namespace Imaging png_set_PLTE(png_ptr, info_ptr, png_palette, PNG_MAX_PALETTE_LENGTH); - // Open file for writing - SDL_RWops * file = SDL_RWFromFile(path, "wb"); - if (file == nullptr) + try { - png_free(png_ptr, png_palette); - png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); - return false; - } - png_set_write_fn(png_ptr, file, PngWriteData, PngFlush); + // Open file for writing + auto fs = FileStream(path, FILE_MODE_WRITE); + png_set_write_fn(png_ptr, &fs, PngWriteData, PngFlush); - // Set error handler - if (setjmp(png_jmpbuf(png_ptr))) + // Set error handler + if (setjmp(png_jmpbuf(png_ptr))) + { + throw Exception("PNG ERROR"); + } + + // Write header + png_set_IHDR( + png_ptr, info_ptr, dpi->width, dpi->height, 8, + PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT + ); + png_byte transparentIndex = 0; + png_set_tRNS(png_ptr, info_ptr, &transparentIndex, 1, nullptr); + png_write_info(png_ptr, info_ptr); + + // Write pixels + uint8 * bits = dpi->bits; + for (int y = 0; y < dpi->height; y++) + { + png_write_row(png_ptr, (png_byte *)bits); + bits += stride; + } + + // Finish + png_write_end(png_ptr, nullptr); + result = true; + } + catch (Exception) { - png_free(png_ptr, png_palette); - png_destroy_write_struct(&png_ptr, &info_ptr); - SDL_RWclose(file); - return false; } - // Write header - png_set_IHDR( - png_ptr, info_ptr, dpi->width, dpi->height, 8, - PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT - ); - png_byte transparentIndex = 0; - png_set_tRNS(png_ptr, info_ptr, &transparentIndex, 1, nullptr); - png_write_info(png_ptr, info_ptr); - - // Write pixels - uint8 *bits = dpi->bits; - for (int y = 0; y < dpi->height; y++) - { - png_write_row(png_ptr, (png_byte *)bits); - bits += stride; - } - - // Finish - png_write_end(png_ptr, nullptr); - SDL_RWclose(file); - png_free(png_ptr, png_palette); - png_destroy_write_struct(&png_ptr, &info_ptr); - return true; + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); + return result; } bool PngWrite32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path) { + bool result = false; + // Setup PNG png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PngError, PngWarning); if (png_ptr == nullptr) @@ -208,56 +217,55 @@ namespace Imaging return false; } - // Open file for writing - SDL_RWops * file = SDL_RWFromFile(path, "wb"); - if (file == nullptr) + try { - png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); - return false; - } - png_set_write_fn(png_ptr, file, PngWriteData, PngFlush); + // Open file for writing + auto fs = FileStream(path, FILE_MODE_WRITE); + png_set_write_fn(png_ptr, &fs, PngWriteData, PngFlush); - // Set error handler - if (setjmp(png_jmpbuf(png_ptr))) + // Set error handler + if (setjmp(png_jmpbuf(png_ptr))) + { + throw Exception("PNG ERROR"); + } + + // Write header + png_set_IHDR( + png_ptr, info_ptr, width, height, 8, + PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT + ); + png_write_info(png_ptr, info_ptr); + + // Write pixels + uint8 * bits = (uint8 *)pixels; + for (int y = 0; y < height; y++) + { + png_write_row(png_ptr, (png_byte *)bits); + bits += width * 4; + } + + // Finish + png_write_end(png_ptr, nullptr); + result = true; + } + catch (Exception) { - png_destroy_write_struct(&png_ptr, &info_ptr); - SDL_RWclose(file); - return false; } - // Write header - png_set_IHDR( - png_ptr, info_ptr, width, height, 8, - PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT - ); - png_write_info(png_ptr, info_ptr); - - // Write pixels - uint8 * bits = (uint8 *)pixels; - for (int y = 0; y < height; y++) - { - png_write_row(png_ptr, (png_byte *)bits); - bits += width * 4; - } - - // Finish - png_write_end(png_ptr, nullptr); - SDL_RWclose(file); - png_destroy_write_struct(&png_ptr, &info_ptr); - return true; + return result; } static void PngReadData(png_structp png_ptr, png_bytep data, png_size_t length) { - SDL_RWops * file = (SDL_RWops *)png_get_io_ptr(png_ptr); - SDL_RWread(file, data, length, 1); + auto * fs = static_cast(png_get_io_ptr(png_ptr)); + fs->Read(data, length); } static void PngWriteData(png_structp png_ptr, png_bytep data, png_size_t length) { - SDL_RWops * file = (SDL_RWops *)png_get_io_ptr(png_ptr); - SDL_RWwrite(file, data, length, 1); + auto * fs = static_cast(png_get_io_ptr(png_ptr)); + fs->Write(data, length); } static void PngFlush(png_structp png_ptr) From 4fdc0e674f6bd79c9fd83b71673edbabb1c7d2d6 Mon Sep 17 00:00:00 2001 From: danidoedel Date: Sun, 8 Jan 2017 23:17:12 +0100 Subject: [PATCH 3/3] Update Xcode project --- OpenRCT2.xcodeproj/project.pbxproj | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index 3f63db88d9..5339c90591 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 008BF72B1CDAA5C30019A2AD /* track_design_save.c in Sources */ = {isa = PBXBuildFile; fileRef = 008BF7271CDAA5C30019A2AD /* track_design_save.c */; }; 008BF72C1CDAA5C30019A2AD /* track_design.c in Sources */ = {isa = PBXBuildFile; fileRef = 008BF7281CDAA5C30019A2AD /* track_design.c */; }; 00EFEE721CF1D80B0035213B /* NetworkKey.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00EFEE701CF1D80B0035213B /* NetworkKey.cpp */; }; + 652076321E22EFE7000D0C04 /* Imaging.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 652076301E22EFE7000D0C04 /* Imaging.cpp */; }; 791166FB1D7486EF005912EA /* NetworkServerAdvertiser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 791166F91D7486EF005912EA /* NetworkServerAdvertiser.cpp */; }; 85060FD31D8C17CC00DFA2B3 /* track_data_old.c in Sources */ = {isa = PBXBuildFile; fileRef = 8594C05F1D885CF600235E93 /* track_data_old.c */; }; 8594C0601D885CF600235E93 /* track_data_old.c in Sources */ = {isa = PBXBuildFile; fileRef = 8594C05F1D885CF600235E93 /* track_data_old.c */; }; @@ -221,9 +222,9 @@ C686F9581CDBC4C7009F9BFC /* vehicle_paint.c in Sources */ = {isa = PBXBuildFile; fileRef = C686F9561CDBC4C7009F9BFC /* vehicle_paint.c */; }; C6B5A7D41CDFE4CB00C9C006 /* S6Exporter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6B5A7D01CDFE4CB00C9C006 /* S6Exporter.cpp */; }; C6B5A7D51CDFE4CB00C9C006 /* S6Importer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6B5A7D21CDFE4CB00C9C006 /* S6Importer.cpp */; }; - C6CABA821E1466D600D33A6B /* FileClassifier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6CABA801E1466D600D33A6B /* FileClassifier.cpp */; }; C6CABA7E1E13F11C00D33A6B /* hook.c in Sources */ = {isa = PBXBuildFile; fileRef = C6E96E181E0406F00076A04F /* hook.c */; }; C6CABA7F1E13F14F00D33A6B /* addresses.c in Sources */ = {isa = PBXBuildFile; fileRef = C6E96E161E0406F00076A04F /* addresses.c */; }; + C6CABA821E1466D600D33A6B /* FileClassifier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6CABA801E1466D600D33A6B /* FileClassifier.cpp */; }; C6E96E121E04067A0076A04F /* File.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6E96E101E04067A0076A04F /* File.cpp */; }; C6E96E151E04069A0076A04F /* Zip.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6E96E131E04069A0076A04F /* Zip.cpp */; }; C6E96E1A1E0406F00076A04F /* addresses.c in Sources */ = {isa = PBXBuildFile; fileRef = C6E96E161E0406F00076A04F /* addresses.c */; }; @@ -280,7 +281,6 @@ D44272111CC81B3200D84D28 /* string.c in Sources */ = {isa = PBXBuildFile; fileRef = D442710B1CC81B3200D84D28 /* string.c */; }; D44272131CC81B3200D84D28 /* editor.c in Sources */ = {isa = PBXBuildFile; fileRef = D442710E1CC81B3200D84D28 /* editor.c */; }; D44272141CC81B3200D84D28 /* game.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271101CC81B3200D84D28 /* game.c */; }; - D44272161CC81B3200D84D28 /* image_io.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271141CC81B3200D84D28 /* image_io.c */; }; D44272171CC81B3200D84D28 /* input.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271161CC81B3200D84D28 /* input.c */; }; D44272181CC81B3200D84D28 /* chat.c in Sources */ = {isa = PBXBuildFile; fileRef = D44271191CC81B3200D84D28 /* chat.c */; }; D44272191CC81B3200D84D28 /* colour.c in Sources */ = {isa = PBXBuildFile; fileRef = D442711B1CC81B3200D84D28 /* colour.c */; }; @@ -516,6 +516,8 @@ 008BF7291CDAA5C30019A2AD /* track_design.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = track_design.h; sourceTree = ""; }; 00EFEE701CF1D80B0035213B /* NetworkKey.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkKey.cpp; sourceTree = ""; usesTabs = 0; }; 00EFEE711CF1D80B0035213B /* NetworkKey.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = NetworkKey.h; sourceTree = ""; usesTabs = 0; }; + 652076301E22EFE7000D0C04 /* Imaging.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Imaging.cpp; sourceTree = ""; }; + 652076311E22EFE7000D0C04 /* Imaging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Imaging.h; sourceTree = ""; }; 791166F91D7486EF005912EA /* NetworkServerAdvertiser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkServerAdvertiser.cpp; sourceTree = ""; }; 791166FA1D7486EF005912EA /* NetworkServerAdvertiser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkServerAdvertiser.h; sourceTree = ""; }; 8594C05F1D885CF600235E93 /* track_data_old.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = track_data_old.c; sourceTree = ""; }; @@ -782,8 +784,6 @@ D442710F1CC81B3200D84D28 /* editor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = editor.h; sourceTree = ""; }; D44271101CC81B3200D84D28 /* game.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = game.c; sourceTree = ""; }; D44271111CC81B3200D84D28 /* game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = game.h; sourceTree = ""; }; - D44271141CC81B3200D84D28 /* image_io.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = image_io.c; sourceTree = ""; }; - D44271151CC81B3200D84D28 /* image_io.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = image_io.h; sourceTree = ""; }; D44271161CC81B3200D84D28 /* input.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = input.c; sourceTree = ""; }; D44271171CC81B3200D84D28 /* input.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = input.h; sourceTree = ""; }; D44271191CC81B3200D84D28 /* chat.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chat.c; sourceTree = ""; }; @@ -1533,8 +1533,8 @@ C6CABA811E1466D600D33A6B /* FileClassifier.h */, D44271101CC81B3200D84D28 /* game.c */, D44271111CC81B3200D84D28 /* game.h */, - D44271141CC81B3200D84D28 /* image_io.c */, - D44271151CC81B3200D84D28 /* image_io.h */, + 652076301E22EFE7000D0C04 /* Imaging.cpp */, + 652076311E22EFE7000D0C04 /* Imaging.h */, D44271161CC81B3200D84D28 /* input.c */, D44271171CC81B3200D84D28 /* input.h */, D44271301CC81B3200D84D28 /* intro.c */, @@ -2814,6 +2814,7 @@ D442722F1CC81B3200D84D28 /* finance.c in Sources */, D44272831CC81B3200D84D28 /* shortcut_keys.c in Sources */, C686F9401CDBC3B7009F9BFC /* go_karts.c in Sources */, + 652076321E22EFE7000D0C04 /* Imaging.cpp in Sources */, C686F8B51CDBC37E009F9BFC /* litter.c in Sources */, D44272141CC81B3200D84D28 /* game.c in Sources */, C686F91A1CDBC3B7009F9BFC /* mine_train_coaster.c in Sources */, @@ -2844,7 +2845,6 @@ D44272511CC81B3200D84D28 /* sawyercoding.c in Sources */, C686F91F1CDBC3B7009F9BFC /* reverser_roller_coaster.c in Sources */, D442728C1CC81B3200D84D28 /* title_editor.c in Sources */, - D44272161CC81B3200D84D28 /* image_io.c in Sources */, D44272171CC81B3200D84D28 /* input.c in Sources */, D44272741CC81B3200D84D28 /* news.c in Sources */, D442723C1CC81B3200D84D28 /* linux.c in Sources */,