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