1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-02-02 03:35:09 +01:00

Merge pull request #5013 from IntelOrca/refactor/imageio

Refactor image_io
This commit is contained in:
Ted John
2017-01-08 22:30:58 +00:00
committed by GitHub
7 changed files with 333 additions and 267 deletions

View File

@@ -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 = "<group>"; };
00EFEE701CF1D80B0035213B /* NetworkKey.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkKey.cpp; sourceTree = "<group>"; usesTabs = 0; };
00EFEE711CF1D80B0035213B /* NetworkKey.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; path = NetworkKey.h; sourceTree = "<group>"; usesTabs = 0; };
652076301E22EFE7000D0C04 /* Imaging.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Imaging.cpp; sourceTree = "<group>"; };
652076311E22EFE7000D0C04 /* Imaging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Imaging.h; sourceTree = "<group>"; };
791166F91D7486EF005912EA /* NetworkServerAdvertiser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkServerAdvertiser.cpp; sourceTree = "<group>"; };
791166FA1D7486EF005912EA /* NetworkServerAdvertiser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkServerAdvertiser.h; sourceTree = "<group>"; };
8594C05F1D885CF600235E93 /* track_data_old.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = track_data_old.c; sourceTree = "<group>"; };
@@ -782,8 +784,6 @@
D442710F1CC81B3200D84D28 /* editor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = editor.h; sourceTree = "<group>"; };
D44271101CC81B3200D84D28 /* game.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = game.c; sourceTree = "<group>"; };
D44271111CC81B3200D84D28 /* game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = game.h; sourceTree = "<group>"; };
D44271141CC81B3200D84D28 /* image_io.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = image_io.c; sourceTree = "<group>"; };
D44271151CC81B3200D84D28 /* image_io.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = image_io.h; sourceTree = "<group>"; };
D44271161CC81B3200D84D28 /* input.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = input.c; sourceTree = "<group>"; };
D44271171CC81B3200D84D28 /* input.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = input.h; sourceTree = "<group>"; };
D44271191CC81B3200D84D28 /* chat.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chat.c; sourceTree = "<group>"; };
@@ -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 */,

302
src/openrct2/Imaging.cpp Normal file
View File

@@ -0,0 +1,302 @@
#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 <png.h>
#include "core/Exception.hpp"
#include "core/FileStream.hpp"
#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
try
{
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);
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<uint8>(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);
// 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;
}
}
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;
// 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);
try
{
// 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)))
{
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, (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)
{
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;
}
try
{
// 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)))
{
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);
return result;
}
static void PngReadData(png_structp png_ptr, png_bytep data, png_size_t length)
{
auto * fs = static_cast<FileStream *>(png_get_io_ptr(png_ptr));
fs->Read(data, length);
}
static void PngWriteData(png_structp png_ptr, png_bytep data, png_size_t length)
{
auto * fs = static_cast<FileStream *>(png_get_io_ptr(png_ptr));
fs->Write(data, length);
}
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);
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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 <png.h>
#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)
{
}

View File

@@ -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"

View File

@@ -145,7 +145,7 @@
<ClCompile Include="editor.c" />
<ClCompile Include="game.c" />
<ClCompile Include="rct2\hook.c" />
<ClCompile Include="image_io.c" />
<ClCompile Include="Imaging.cpp" />
<ClCompile Include="input.c" />
<ClCompile Include="interface\chat.c" />
<ClCompile Include="interface\colour.c" />
@@ -487,7 +487,7 @@
<ClInclude Include="editor.h" />
<ClInclude Include="game.h" />
<ClInclude Include="rct2\hook.h" />
<ClInclude Include="image_io.h" />
<ClInclude Include="Imaging.h" />
<ClInclude Include="input.h" />
<ClInclude Include="interface\chat.h" />
<ClInclude Include="interface\colour.h" />