2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant 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.
12 GtkRadiant 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "radiant_jpeglib.h"
27 GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic, int *width, int *height )
30 /* This struct contains the JPEG decompression parameters and pointers to
31 * working space (which is allocated as needed by the JPEG library).
33 struct jpeg_decompress_struct cinfo;
34 /* We use our private extension JPEG error handler.
35 * Note that this struct must live as long as the main JPEG parameter
36 * struct, to avoid dangling-pointer problems.
38 /* This struct represents a JPEG error handler. It is declared separately
39 * because applications often want to supply a specialized error handler
40 * (see the second half of this file for an example). But here we just
41 * take the easy way out and use the standard error handler, which will
42 * print a message on stderr and call exit() if compression fails.
43 * Note that this struct must live as long as the main JPEG parameter
44 * struct, to avoid dangling-pointer problems.
47 struct jpeg_error_mgr jerr;
49 JSAMPARRAY buffer; /* Output row buffer */
50 int row_stride; /* physical row width in output buffer */
51 unsigned char *out, *bbuf;
56 // Rad additions: initialize the longjmp buffer
57 jmpret = setjmp( rad_loadfailed );
60 *pic = (unsigned char *)rad_errormsg;
64 /* Step 1: allocate and initialize JPEG decompression object */
66 /* We have to set up the error handler first, in case the initialization
67 * step fails. (Unlikely, but it could happen if you are out of memory.)
68 * This routine fills in the contents of struct jerr, and returns jerr's
69 * address which we place into the link field in cinfo.
71 cinfo.err = jpeg_std_error(&jerr);
73 /* Now we can initialize the JPEG decompression object. */
74 jpeg_create_decompress(&cinfo);
76 /* Step 2: specify data source (eg, a file) */
78 jpeg_stdio_src(&cinfo, fbuffer, bufsize);
80 /* Step 3: read file parameters with jpeg_read_header() */
82 (void) jpeg_read_header(&cinfo, TRUE);
83 /* We can ignore the return value from jpeg_read_header since
84 * (a) suspension is not possible with the stdio data source, and
85 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
86 * See libjpeg.doc for more info.
89 /* Step 4: set parameters for decompression */
91 /* In this example, we don't need to change any of the defaults set by
92 * jpeg_read_header(), so we do nothing here.
95 /* Step 5: Start decompressor */
97 (void) jpeg_start_decompress(&cinfo);
98 /* We can ignore the return value since suspension is not possible
99 * with the stdio data source.
102 if( cinfo.output_components != 1 && cinfo.output_components != 3 && cinfo.output_components != 4 )
104 *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Non-Y/RGB/RGBA JPEG encountered (unsupported)"));
108 /* We may need to do some setup of our own at this point before reading
109 * the data. After jpeg_start_decompress() we have the correct scaled
110 * output image dimensions available, as well as the output colormap
111 * if we asked for color quantization.
112 * In this example, we need to make an output work buffer of the right size.
115 /* JSAMPLEs per row in output buffer */
116 row_stride = cinfo.output_width * cinfo.output_components;
117 nSize = cinfo.output_width*cinfo.output_height*4;
119 out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );
120 memset( out, 255, nSize + 1 );
123 *width = cinfo.output_width;
124 *height = cinfo.output_height;
126 /* Step 6: while (scan lines remain to be read) */
127 /* jpeg_read_scanlines(...); */
129 /* Here we use the library's state variable cinfo.output_scanline as the
130 * loop counter, so that we don't have to keep track ourselves.
132 while (cinfo.output_scanline < cinfo.output_height)
134 /* jpeg_read_scanlines expects an array of pointers to scanlines.
135 * Here the array is only one element long, but you could ask for
136 * more than one scanline at a time if that's more convenient.
138 bbuf = out + row_stride * cinfo.output_scanline;
140 (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
142 // we convert downwards to not overwrite values we want to read later
143 switch(cinfo.output_components)
146 // clear all the alphas to 255
147 for(i = cinfo.output_width; i-- > 0; )
154 for(i = cinfo.output_width; i-- > 0; )
157 bbuf[i*4+2] = bbuf[i*3+2];
158 bbuf[i*4+1] = bbuf[i*3+1];
159 bbuf[i*4+0] = bbuf[i*3+0];
164 for(i = cinfo.output_width; i-- > 0; )
167 bbuf[i*4+2] = bbuf[i];
168 bbuf[i*4+1] = bbuf[i];
169 bbuf[i*4+0] = bbuf[i];
175 /* Step 7: Finish decompression */
177 (void) jpeg_finish_decompress(&cinfo);
178 /* We can ignore the return value since suspension is not possible
179 * with the stdio data source.
182 /* Step 8: Release JPEG decompression object */
184 /* This is an important step since it will release a good deal of memory. */
185 jpeg_destroy_decompress(&cinfo);
187 /* After finish_decompress, we can close the input file.
188 * Here we postpone it until after no more JPEG errors are possible,
189 * so as to simplify the setjmp error logic above. (Actually, I don't
190 * think that jpeg_destroy can do an error exit, but why assume anything...)
194 /* At this point you may want to check to see whether any corrupt-data
195 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
198 /* And we're done! */