10 #include "../../stdafx.h"
11 #include "../../openttd.h"
12 #include "../../debug.h"
13 #include "../../gfx_func.h"
14 #include "../../fileio_func.h"
15 #include "../../blitter/factory.hpp"
16 #include "../../core/mem_func.hpp"
24 #include "../../safeguards.h"
32 static void PNGAPI
png_my_error(png_structp png_ptr, png_const_charp message)
34 DEBUG(misc, 0,
"[libpng] error: %s - %s", message, (
char *)png_get_error_ptr(png_ptr));
35 longjmp(png_jmpbuf(png_ptr), 1);
44 static void PNGAPI
png_my_warning(png_structp png_ptr, png_const_charp message)
46 DEBUG(misc, 1,
"[libpng] warning: %s - %s", message, (
char *)png_get_error_ptr(png_ptr));
55 if (f ==
nullptr)
return;
58 fread(header,
sizeof(png_byte), 8, f);
59 if (png_sig_cmp(header, 0, 8) != 0) {
66 if (png_ptr ==
nullptr) {
71 png_infop info_ptr = png_create_info_struct(png_ptr);
72 if (info_ptr ==
nullptr) {
73 png_destroy_read_struct(&png_ptr, (png_infopp)
nullptr, (png_infopp)
nullptr);
78 png_infop end_info = png_create_info_struct(png_ptr);
79 if (end_info ==
nullptr) {
80 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)
nullptr);
85 if (setjmp(png_jmpbuf(png_ptr))) {
86 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
91 png_init_io(png_ptr, f);
92 png_set_sig_bytes(png_ptr, 8);
94 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY,
nullptr);
96 uint
width = png_get_image_width(png_ptr, info_ptr);
97 uint
height = png_get_image_height(png_ptr, info_ptr);
98 uint bit_depth = png_get_bit_depth(png_ptr, info_ptr);
99 uint color_type = png_get_color_type(png_ptr, info_ptr);
101 if (color_type != PNG_COLOR_TYPE_PALETTE || bit_depth != 8) {
102 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
107 if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) {
108 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
115 png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
117 png_bytep *row_pointers = png_get_rows(png_ptr, info_ptr);
119 if (
width > (uint) _screen.width)
width = _screen.width;
120 if (
height > (uint) _screen.height)
height = _screen.height;
122 uint xoff = (_screen.width -
width) / 2;
123 uint yoff = (_screen.height -
height) / 2;
127 uint8 *dst_ptr = (uint8 *)_screen.dst_ptr;
129 MemSetT(dst_ptr, 0xff, _screen.pitch * _screen.height);
131 for (uint y = 0; y <
height; y++) {
132 uint8 *src = row_pointers[y];
133 uint8 *dst = dst_ptr + (yoff + y) * _screen.pitch + xoff;
135 memcpy(dst, src,
width);
138 for (
int i = 0; i < num_palette; i++) {
155 uint32 *dst_ptr = (uint32 *)_screen.dst_ptr;
157 MemSetT(dst_ptr, 0, _screen.pitch * _screen.height);
159 for (uint y = 0; y <
height; y++) {
160 uint8 *src = row_pointers[y];
161 uint32 *dst = dst_ptr + (yoff + y) * _screen.pitch + xoff;
163 for (uint x = 0; x <
width; x++) {
164 dst[x] = palette[src[x]].blue | (palette[src[x]].green << 8) | (palette[src[x]].red << 16) | 0xff000000;
171 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);