|
1 /* $Id$ */ |
|
2 |
|
3 /** @file grf.cpp */ |
|
4 |
|
5 #ifdef WITH_PNG |
|
6 |
|
7 #include "../stdafx.h" |
|
8 #include "../gfx.h" |
|
9 #include "../fileio.h" |
|
10 #include "../variables.h" |
|
11 #include "../debug.h" |
|
12 #include "png.hpp" |
|
13 #include <png.h> |
|
14 |
|
15 #define PNG_SLOT 62 |
|
16 |
|
17 static void PNGAPI png_my_read(png_structp png_ptr, png_bytep data, png_size_t length) |
|
18 { |
|
19 FioReadBlock(data, length); |
|
20 } |
|
21 |
|
22 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message) |
|
23 { |
|
24 DEBUG(sprite, 0, "ERROR (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr)); |
|
25 longjmp(png_ptr->jmpbuf, 1); |
|
26 } |
|
27 |
|
28 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message) |
|
29 { |
|
30 DEBUG(sprite, 0, "WARNING (libpng): %s - %s", message, (char *)png_get_error_ptr(png_ptr)); |
|
31 } |
|
32 |
|
33 static bool OpenPNGFile(const char *filename, uint32 id, bool mask) |
|
34 { |
|
35 char png_file[MAX_PATH]; |
|
36 |
|
37 snprintf(png_file, sizeof(png_file), "sprites" PATHSEP "%s" PATHSEP "%d%s.png", filename, id, mask ? "m" : ""); |
|
38 if (FioCheckFileExists(png_file)) { |
|
39 FioOpenFile(PNG_SLOT, png_file); |
|
40 return true; |
|
41 } |
|
42 |
|
43 /* TODO -- Add TAR support */ |
|
44 return false; |
|
45 } |
|
46 |
|
47 static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, bool mask) |
|
48 { |
|
49 png_byte header[8]; |
|
50 png_structp png_ptr; |
|
51 png_infop info_ptr, end_info; |
|
52 uint bit_depth, color_type; |
|
53 uint i, pixelsize; |
|
54 png_bytep row_pointer; |
|
55 SpriteLoader::CommonPixel *dst; |
|
56 |
|
57 if (!OpenPNGFile(filename, id, mask)) return mask; // If mask is true, and file not found, continue true anyway, as it isn't a show-stopper |
|
58 |
|
59 /* Check the header */ |
|
60 FioReadBlock(header, 8); |
|
61 if (png_sig_cmp(header, 0, 8) != 0) return false; |
|
62 |
|
63 /* Create the reader */ |
|
64 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning); |
|
65 if (png_ptr == NULL) return false; |
|
66 |
|
67 /* Create initial stuff */ |
|
68 info_ptr = png_create_info_struct(png_ptr); |
|
69 if (info_ptr == NULL) { |
|
70 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
|
71 return false; |
|
72 } |
|
73 end_info = png_create_info_struct(png_ptr); |
|
74 if (end_info == NULL) { |
|
75 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
|
76 return false; |
|
77 } |
|
78 |
|
79 /* Make sure that upon error, we can clean up graceful */ |
|
80 if (setjmp(png_jmpbuf(png_ptr))) { |
|
81 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
|
82 return false; |
|
83 } |
|
84 |
|
85 /* Read the file */ |
|
86 png_set_read_fn(png_ptr, NULL, png_my_read); |
|
87 png_set_sig_bytes(png_ptr, 8); |
|
88 |
|
89 png_read_info(png_ptr, info_ptr); |
|
90 |
|
91 if (!mask) { |
|
92 /* Read the text chunks */ |
|
93 png_textp text_ptr; |
|
94 int num_text = 0; |
|
95 png_get_text(png_ptr, info_ptr, &text_ptr, &num_text); |
|
96 if (num_text == 0) DEBUG(misc, 0, "Warning: PNG Sprite '%s/%d.png' doesn't have x_offs and y_offs; expect graphical problems", filename, id); |
|
97 for (int i = 0; i < num_text; i++) { |
|
98 /* x_offs and y_offs are in the text-chunk of PNG */ |
|
99 if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0); |
|
100 if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0); |
|
101 } |
|
102 |
|
103 sprite->height = info_ptr->height; |
|
104 sprite->width = info_ptr->width; |
|
105 sprite->data = CallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height); |
|
106 } |
|
107 |
|
108 bit_depth = png_get_bit_depth(png_ptr, info_ptr); |
|
109 color_type = png_get_color_type(png_ptr, info_ptr); |
|
110 |
|
111 if (mask && (bit_depth != 8 || color_type != PNG_COLOR_TYPE_PALETTE)) { |
|
112 DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id); |
|
113 return true; |
|
114 } |
|
115 |
|
116 if (!mask) { |
|
117 if (bit_depth == 16) png_set_strip_16(png_ptr); |
|
118 |
|
119 if (color_type == PNG_COLOR_TYPE_PALETTE) { |
|
120 png_set_palette_to_rgb(png_ptr); |
|
121 color_type = PNG_COLOR_TYPE_RGB; |
|
122 } |
|
123 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { |
|
124 png_set_gray_to_rgb(png_ptr); |
|
125 color_type = PNG_COLOR_TYPE_RGB; |
|
126 } |
|
127 |
|
128 #ifdef TTD_LITTLE_ENDIAN |
|
129 png_set_bgr(png_ptr); |
|
130 #else |
|
131 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) png_set_swap_alpha(png_ptr); |
|
132 #endif |
|
133 |
|
134 if (color_type == PNG_COLOR_TYPE_RGB) { |
|
135 #ifdef TTD_LITTLE_ENDIAN |
|
136 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
|
137 #else |
|
138 png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); |
|
139 #endif |
|
140 } |
|
141 |
|
142 pixelsize = sizeof(uint32); |
|
143 } else { |
|
144 pixelsize = sizeof(uint8); |
|
145 } |
|
146 |
|
147 row_pointer = (png_byte *)malloc(info_ptr->width * pixelsize); |
|
148 if (row_pointer == NULL) { |
|
149 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
|
150 return false; |
|
151 } |
|
152 |
|
153 for (i = 0; i < info_ptr->height; i++) { |
|
154 png_read_row(png_ptr, row_pointer, NULL); |
|
155 |
|
156 dst = sprite->data + i * info_ptr->width; |
|
157 |
|
158 for (uint x = 0; x < info_ptr->width; x++) { |
|
159 if (mask) { |
|
160 if (row_pointer[x * sizeof(uint8)] != 0) { |
|
161 dst[x].b = 0; |
|
162 dst[x].g = 0; |
|
163 dst[x].r = 0; |
|
164 /* Alpha channel is used from the original image (to allow transparency in remap colors) */ |
|
165 dst[x].m = row_pointer[x * sizeof(uint8)]; |
|
166 } |
|
167 } else { |
|
168 dst[x].b = row_pointer[x * sizeof(uint32) + 0]; |
|
169 dst[x].g = row_pointer[x * sizeof(uint32) + 1]; |
|
170 dst[x].r = row_pointer[x * sizeof(uint32) + 2]; |
|
171 dst[x].a = row_pointer[x * sizeof(uint32) + 3]; |
|
172 dst[x].m = 0; |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 free(row_pointer); |
|
178 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
|
179 |
|
180 return true; |
|
181 } |
|
182 |
|
183 bool SpriteLoaderPNG::LoadSprite(SpriteLoader::Sprite *sprite, const char *filename, uint32 file_pos) |
|
184 { |
|
185 if (!LoadPNG(sprite, filename, file_pos, false)) return false; |
|
186 if (!LoadPNG(sprite, filename, file_pos, true)) return false; |
|
187 return true; |
|
188 } |
|
189 |
|
190 #endif /* WITH_PNG */ |