]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/ImageIO.cpp
cff8dd194afb2d61fd7f7221f05b9b0cbb6a61e4
[lugaru.git] / Source / Utils / ImageIO.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Utils/ImageIO.hpp"
22
23 #include "Game.hpp"
24 #include "Utils/Folders.hpp"
25
26 #include <jpeglib.h>
27 #include <png.h>
28 #include <stdio.h>
29 #include <zlib.h>
30
31 /* These two are needed for screenshot */
32 extern int kContextWidth;
33 extern int kContextHeight;
34
35 static bool load_png(const char* fname, ImageRec& tex);
36 static bool load_jpg(const char* fname, ImageRec& tex);
37 static bool save_screenshot_png(const char* fname);
38
39 ImageRec::ImageRec()
40 {
41     data = (GLubyte*)malloc(1024 * 1024 * 4);
42 }
43
44 ImageRec::~ImageRec()
45 {
46     free(data);
47     data = NULL;
48 }
49
50 bool load_image(const char* file_name, ImageRec& tex)
51 {
52     Game::LoadingScreen();
53
54     if (tex.data == NULL) {
55         return false;
56     }
57
58     const char* ptr = strrchr((char*)file_name, '.');
59     if (ptr) {
60         if (strcasecmp(ptr + 1, "png") == 0) {
61             return load_png(file_name, tex);
62         } else if (strcasecmp(ptr + 1, "jpg") == 0) {
63             return load_jpg(file_name, tex);
64         }
65     }
66
67     std::cerr << "Unsupported image type" << std::endl;
68     return false;
69 }
70
71 bool save_screenshot(const char* file_name)
72 {
73     const char* ptr = strrchr((char*)file_name, '.');
74     if (ptr) {
75         if (strcasecmp(ptr + 1, "png") == 0) {
76             return save_screenshot_png((Folders::getScreenshotDir() + '/' + file_name).c_str());
77         }
78     }
79
80     std::cerr << "Unsupported image type" << std::endl;
81     return false;
82 }
83
84 struct my_error_mgr
85 {
86     struct jpeg_error_mgr pub; /* "public" fields */
87     jmp_buf setjmp_buffer;     /* for return to caller */
88 };
89 typedef struct my_error_mgr* my_error_ptr;
90
91 static void my_error_exit(j_common_ptr cinfo)
92 {
93     struct my_error_mgr* err = (struct my_error_mgr*)cinfo->err;
94     longjmp(err->setjmp_buffer, 1);
95 }
96
97 /* stolen from public domain example.c code in libjpg distribution. */
98 static bool load_jpg(const char* file_name, ImageRec& tex)
99 {
100     struct jpeg_decompress_struct cinfo;
101     struct my_error_mgr jerr;
102     JSAMPROW buffer[1]; /* Output row buffer */
103     int row_stride;     /* physical row width in output buffer */
104     errno = 0;
105     FILE* infile = fopen(file_name, "rb");
106
107     if (infile == NULL) {
108         perror((std::string("Couldn't open file ") + file_name).c_str());
109         return false;
110     }
111
112     cinfo.err = jpeg_std_error(&jerr.pub);
113     jerr.pub.error_exit = my_error_exit;
114     if (setjmp(jerr.setjmp_buffer)) {
115         jpeg_destroy_decompress(&cinfo);
116         fclose(infile);
117         return false;
118     }
119
120     jpeg_create_decompress(&cinfo);
121     jpeg_stdio_src(&cinfo, infile);
122     (void)jpeg_read_header(&cinfo, TRUE);
123
124     cinfo.out_color_space = JCS_RGB;
125     cinfo.quantize_colors = 0;
126     (void)jpeg_calc_output_dimensions(&cinfo);
127     (void)jpeg_start_decompress(&cinfo);
128
129     row_stride = cinfo.output_width * cinfo.output_components;
130     tex.sizeX = cinfo.output_width;
131     tex.sizeY = cinfo.output_height;
132     tex.bpp = 24;
133
134     while (cinfo.output_scanline < cinfo.output_height) {
135         buffer[0] = (JSAMPROW)(char*)tex.data +
136                     ((cinfo.output_height - 1) - cinfo.output_scanline) * row_stride;
137         (void)jpeg_read_scanlines(&cinfo, buffer, 1);
138     }
139
140     (void)jpeg_finish_decompress(&cinfo);
141     jpeg_destroy_decompress(&cinfo);
142     fclose(infile);
143
144     return true;
145 }
146
147 /* stolen from public domain example.c code in libpng distribution. */
148 static bool load_png(const char* file_name, ImageRec& tex)
149 {
150     bool hasalpha = false;
151     png_structp png_ptr = NULL;
152     png_infop info_ptr = NULL;
153     png_uint_32 width, height;
154     int bit_depth, color_type, interlace_type;
155     bool retval = false;
156     png_byte** row_pointers = NULL;
157     errno = 0;
158     FILE* fp = fopen(file_name, "rb");
159
160     if (fp == NULL) {
161         perror((std::string("Couldn't open file ") + file_name).c_str());
162         return false;
163     }
164
165     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
166     if (png_ptr == NULL)
167         goto png_done;
168
169     info_ptr = png_create_info_struct(png_ptr);
170     if (info_ptr == NULL)
171         goto png_done;
172
173     if (setjmp(png_jmpbuf(png_ptr)))
174         goto png_done;
175
176     png_init_io(png_ptr, fp);
177     png_read_png(png_ptr, info_ptr,
178                  PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING,
179                  NULL);
180     png_get_IHDR(png_ptr, info_ptr, &width, &height,
181                  &bit_depth, &color_type, &interlace_type, NULL, NULL);
182
183     if (bit_depth != 8) // transform SHOULD handle this...
184         goto png_done;
185
186     if (color_type & PNG_COLOR_MASK_PALETTE) // !!! FIXME?
187         goto png_done;
188
189     if ((color_type & PNG_COLOR_MASK_COLOR) == 0) // !!! FIXME?
190         goto png_done;
191
192     hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0);
193     row_pointers = png_get_rows(png_ptr, info_ptr);
194     if (!row_pointers)
195         goto png_done;
196
197     if (!hasalpha) {
198         png_byte* dst = tex.data;
199         for (int i = height - 1; i >= 0; i--) {
200             png_byte* src = row_pointers[i];
201             for (unsigned j = 0; j < width; j++) {
202                 dst[0] = src[0];
203                 dst[1] = src[1];
204                 dst[2] = src[2];
205                 dst[3] = 0xFF;
206                 src += 3;
207                 dst += 4;
208             }
209         }
210     }
211
212     else {
213         png_byte* dst = tex.data;
214         int pitch = width * 4;
215         for (int i = height - 1; i >= 0; i--, dst += pitch)
216             memcpy(dst, row_pointers[i], pitch);
217     }
218
219     tex.sizeX = width;
220     tex.sizeY = height;
221     tex.bpp = 32;
222     retval = true;
223
224 png_done:
225     if (!retval) {
226         cerr << "There was a problem loading " << file_name << endl;
227     }
228     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
229     if (fp)
230         fclose(fp);
231     return (retval);
232 }
233
234 static bool save_screenshot_png(const char* file_name)
235 {
236     FILE* fp = NULL;
237     png_structp png_ptr = NULL;
238     png_infop info_ptr = NULL;
239     bool retval = false;
240
241     errno = 0;
242     fp = fopen(file_name, "wb");
243     if (fp == NULL) {
244         perror((std::string("Couldn't open file ") + file_name).c_str());
245         return false;
246     }
247
248     png_bytep* row_pointers = new png_bytep[kContextHeight];
249     png_bytep screenshot = new png_byte[kContextWidth * kContextHeight * 3];
250     if ((!screenshot) || (!row_pointers))
251         goto save_png_done;
252
253     glGetError();
254     glReadPixels(0, 0, kContextWidth, kContextHeight,
255                  GL_RGB, GL_UNSIGNED_BYTE, screenshot);
256     if (glGetError() != GL_NO_ERROR)
257         goto save_png_done;
258
259     for (int i = 0; i < kContextHeight; i++)
260         row_pointers[i] = screenshot + ((kContextWidth * ((kContextHeight - 1) - i)) * 3);
261
262     png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
263     if (png_ptr == NULL)
264         goto save_png_done;
265
266     info_ptr = png_create_info_struct(png_ptr);
267     if (info_ptr == NULL)
268         goto save_png_done;
269
270     if (setjmp(png_jmpbuf(png_ptr)))
271         goto save_png_done;
272
273     png_init_io(png_ptr, fp);
274
275     if (setjmp(png_jmpbuf(png_ptr)))
276         goto save_png_done;
277
278     png_set_IHDR(png_ptr, info_ptr, kContextWidth, kContextHeight,
279                  8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
280                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
281
282     png_write_info(png_ptr, info_ptr);
283
284     if (setjmp(png_jmpbuf(png_ptr)))
285         goto save_png_done;
286
287     png_write_image(png_ptr, row_pointers);
288
289     if (setjmp(png_jmpbuf(png_ptr)))
290         goto save_png_done;
291
292     png_write_end(png_ptr, NULL);
293     retval = true;
294
295 save_png_done:
296     png_destroy_write_struct(&png_ptr, &info_ptr);
297     delete[] screenshot;
298     delete[] row_pointers;
299     if (fp)
300         fclose(fp);
301     if (!retval)
302         unlink(file_name);
303     return retval;
304 }