]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/ImageIO.cpp
Sorted all source files in folders
[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 /**> HEADER FILES <**/
22
23 #include <stdio.h>
24 #include <jpeglib.h>
25 #include <png.h>
26 #include <zlib.h>
27
28 #include "Game.h"
29 #include "Utils/ImageIO.h"
30 #include "Utils/Folders.h"
31
32 extern bool visibleloading;
33
34 /* These two are needed for screenshot */
35 extern int kContextWidth;
36 extern int kContextHeight;
37
38 static bool load_png(const char * fname, ImageRec & tex);
39 static bool load_jpg(const char * fname, ImageRec & tex);
40 static bool save_screenshot_png(const char * fname);
41
42 ImageRec::ImageRec()
43 {
44     data = ( GLubyte* )malloc( 1024 * 1024 * 4 );
45 }
46
47 ImageRec::~ImageRec()
48 {
49     free(data);
50     data = NULL;
51 }
52
53 bool load_image(const char *file_name, ImageRec &tex)
54 {
55     if (visibleloading)
56         Game::LoadingScreen();
57
58     if ( tex.data == NULL )
59         return false;
60
61     const char *ptr = strrchr((char *)file_name, '.');
62     if (ptr) {
63         if (strcasecmp(ptr + 1, "png") == 0)
64             return load_png(file_name, tex);
65         else if (strcasecmp(ptr + 1, "jpg") == 0)
66             return load_jpg(file_name, tex);
67     }
68
69     STUBBED("Unsupported image type");
70     return false;
71 }
72
73 bool save_screenshot(const char *file_name)
74 {
75     const char *ptr = strrchr((char *)file_name, '.');
76     if (ptr) {
77         if (strcasecmp(ptr + 1, "png") == 0)
78             return save_screenshot_png((Folders::getScreenshotDir() + '/' + file_name).c_str());
79     }
80
81     STUBBED("Unsupported image type");
82     return false;
83 }
84
85 struct my_error_mgr {
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     FILE *infile = fopen(file_name, "rb");
105
106     if (infile == NULL)
107         return false;
108
109     cinfo.err = jpeg_std_error(&jerr.pub);
110     jerr.pub.error_exit = my_error_exit;
111     if (setjmp(jerr.setjmp_buffer)) {
112         jpeg_destroy_decompress(&cinfo);
113         fclose(infile);
114         return false;
115     }
116
117     jpeg_create_decompress(&cinfo);
118     jpeg_stdio_src(&cinfo, infile);
119     (void) jpeg_read_header(&cinfo, TRUE);
120
121     cinfo.out_color_space = JCS_RGB;
122     cinfo.quantize_colors = 0;
123     (void) jpeg_calc_output_dimensions(&cinfo);
124     (void) jpeg_start_decompress(&cinfo);
125
126     row_stride = cinfo.output_width * cinfo.output_components;
127     tex.sizeX = cinfo.output_width;
128     tex.sizeY = cinfo.output_height;
129     tex.bpp = 24;
130
131     while (cinfo.output_scanline < cinfo.output_height) {
132         buffer[0] = (JSAMPROW)(char *)tex.data +
133                     ((cinfo.output_height - 1) - cinfo.output_scanline) * row_stride;
134         (void) jpeg_read_scanlines(&cinfo, buffer, 1);
135     }
136
137     (void) jpeg_finish_decompress(&cinfo);
138     jpeg_destroy_decompress(&cinfo);
139     fclose(infile);
140
141     return true;
142 }
143
144 /* stolen from public domain example.c code in libpng distribution. */
145 static bool load_png(const char *file_name, ImageRec &tex)
146 {
147     bool hasalpha = false;
148     png_structp png_ptr = NULL;
149     png_infop info_ptr = NULL;
150     png_uint_32 width, height;
151     int bit_depth, color_type, interlace_type;
152     bool retval = false;
153     png_byte **row_pointers = NULL;
154     FILE *fp = fopen(file_name, "rb");
155
156     if (fp == NULL) {
157         cerr << file_name << " not found" << endl;
158         return false;
159     }
160
161     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
162     if (png_ptr == NULL)
163         goto png_done;
164
165     info_ptr = png_create_info_struct(png_ptr);
166     if (info_ptr == NULL)
167         goto png_done;
168
169     if (setjmp(png_jmpbuf(png_ptr)))
170         goto png_done;
171
172     png_init_io(png_ptr, fp);
173     png_read_png(png_ptr, info_ptr,
174                  PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING,
175                  NULL);
176     png_get_IHDR(png_ptr, info_ptr, &width, &height,
177                  &bit_depth, &color_type, &interlace_type, NULL, NULL);
178
179     if (bit_depth != 8)  // transform SHOULD handle this...
180         goto png_done;
181
182     if (color_type & PNG_COLOR_MASK_PALETTE)  // !!! FIXME?
183         goto png_done;
184
185     if ((color_type & PNG_COLOR_MASK_COLOR) == 0)  // !!! FIXME?
186         goto png_done;
187
188     hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0);
189     row_pointers = png_get_rows(png_ptr, info_ptr);
190     if (!row_pointers)
191         goto png_done;
192
193     if (!hasalpha) {
194         png_byte *dst = tex.data;
195         for (int i = height - 1; i >= 0; i--) {
196             png_byte *src = row_pointers[i];
197             for (unsigned j = 0; j < width; j++) {
198                 dst[0] = src[0];
199                 dst[1] = src[1];
200                 dst[2] = src[2];
201                 dst[3] = 0xFF;
202                 src += 3;
203                 dst += 4;
204             }
205         }
206     }
207
208     else {
209         png_byte *dst = tex.data;
210         int pitch = width * 4;
211         for (int i = height - 1; i >= 0; i--, dst += pitch)
212             memcpy(dst, row_pointers[i], pitch);
213     }
214
215     tex.sizeX = width;
216     tex.sizeY = height;
217     tex.bpp = 32;
218     retval = true;
219
220 png_done:
221     if (!retval) {
222         cerr << "There was a problem loading " << file_name << endl;
223     }
224     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
225     if (fp)
226         fclose(fp);
227     return (retval);
228 }
229
230 static bool save_screenshot_png(const char *file_name)
231 {
232     FILE *fp = NULL;
233     png_structp png_ptr = NULL;
234     png_infop info_ptr = NULL;
235     bool retval = false;
236
237     fp = fopen(file_name, "wb");
238     if (fp == NULL)
239         return false;
240
241     png_bytep *row_pointers = new png_bytep[kContextHeight];
242     png_bytep screenshot = new png_byte[kContextWidth * kContextHeight * 3];
243     if ((!screenshot) || (!row_pointers))
244         goto save_png_done;
245
246     glGetError();
247     glReadPixels(0, 0, kContextWidth, kContextHeight,
248                  GL_RGB, GL_UNSIGNED_BYTE, screenshot);
249     if (glGetError() != GL_NO_ERROR)
250         goto save_png_done;
251
252     for (int i = 0; i < kContextHeight; i++)
253         row_pointers[i] = screenshot + ((kContextWidth * ((kContextHeight - 1) - i)) * 3);
254
255     png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
256     if (png_ptr == NULL)
257         goto save_png_done;
258
259     info_ptr = png_create_info_struct(png_ptr);
260     if (info_ptr == NULL)
261         goto save_png_done;
262
263     if (setjmp(png_jmpbuf(png_ptr)))
264         goto save_png_done;
265
266     png_init_io(png_ptr, fp);
267
268     if (setjmp(png_jmpbuf(png_ptr)))
269         goto save_png_done;
270
271     png_set_IHDR(png_ptr, info_ptr, kContextWidth, kContextHeight,
272                  8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
273                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
274
275     png_write_info(png_ptr, info_ptr);
276
277     if (setjmp(png_jmpbuf(png_ptr)))
278         goto save_png_done;
279
280     png_write_image(png_ptr, row_pointers);
281
282     if (setjmp(png_jmpbuf(png_ptr)))
283         goto save_png_done;
284
285     png_write_end(png_ptr, NULL);
286     retval = true;
287
288 save_png_done:
289     png_destroy_write_struct(&png_ptr, &info_ptr);
290     delete[] screenshot;
291     delete[] row_pointers;
292     if (fp)
293         fclose(fp);
294     if (!retval)
295         unlink(file_name);
296     return retval;
297 }