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;
55 // Rad additions: initialize the longjmp buffer
56 jmpret = setjmp( rad_loadfailed );
59 *pic = (unsigned char *)rad_errormsg;
63 /* Step 1: allocate and initialize JPEG decompression object */
65 /* We have to set up the error handler first, in case the initialization
66 * step fails. (Unlikely, but it could happen if you are out of memory.)
67 * This routine fills in the contents of struct jerr, and returns jerr's
68 * address which we place into the link field in cinfo.
70 cinfo.err = jpeg_std_error(&jerr);
72 /* Now we can initialize the JPEG decompression object. */
73 jpeg_create_decompress(&cinfo);
75 /* Step 2: specify data source (eg, a file) */
77 jpeg_stdio_src(&cinfo, fbuffer, bufsize);
79 /* Step 3: read file parameters with jpeg_read_header() */
81 (void) jpeg_read_header(&cinfo, TRUE);
82 /* We can ignore the return value from jpeg_read_header since
83 * (a) suspension is not possible with the stdio data source, and
84 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
85 * See libjpeg.doc for more info.
88 /* Step 4: set parameters for decompression */
90 /* In this example, we don't need to change any of the defaults set by
91 * jpeg_read_header(), so we do nothing here.
94 /* Step 5: Start decompressor */
96 (void) jpeg_start_decompress(&cinfo);
97 /* We can ignore the return value since suspension is not possible
98 * with the stdio data source.
101 /* ydnar: radiant only handles RGB, non-progressive format jpegs */
102 if( cinfo.output_components != 4 )
104 *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Non-RGB JPEG encountered (unsupported)"));
107 if( cinfo.progressive_mode )
109 *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Progressive JPEG encountered (unsupported)"));
113 /* We may need to do some setup of our own at this point before reading
114 * the data. After jpeg_start_decompress() we have the correct scaled
115 * output image dimensions available, as well as the output colormap
116 * if we asked for color quantization.
117 * In this example, we need to make an output work buffer of the right size.
120 /* JSAMPLEs per row in output buffer */
121 row_stride = cinfo.output_width * cinfo.output_components;
122 nSize = cinfo.output_width*cinfo.output_height*cinfo.output_components;
124 out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );
125 memset( out, 255, nSize + 1 );
128 *width = cinfo.output_width;
129 *height = cinfo.output_height;
131 /* Step 6: while (scan lines remain to be read) */
132 /* jpeg_read_scanlines(...); */
134 /* Here we use the library's state variable cinfo.output_scanline as the
135 * loop counter, so that we don't have to keep track ourselves.
137 while (cinfo.output_scanline < cinfo.output_height)
139 /* jpeg_read_scanlines expects an array of pointers to scanlines.
140 * Here the array is only one element long, but you could ask for
141 * more than one scanline at a time if that's more convenient.
143 bbuf = out + row_stride * cinfo.output_scanline;
145 (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
148 // clear all the alphas to 255
155 j = cinfo.output_width * cinfo.output_height * 4;
156 for ( i = 3 ; i < j ; i+=4 ) {
161 /* Step 7: Finish decompression */
163 (void) jpeg_finish_decompress(&cinfo);
164 /* We can ignore the return value since suspension is not possible
165 * with the stdio data source.
168 /* Step 8: Release JPEG decompression object */
170 /* This is an important step since it will release a good deal of memory. */
171 jpeg_destroy_decompress(&cinfo);
173 /* After finish_decompress, we can close the input file.
174 * Here we postpone it until after no more JPEG errors are possible,
175 * so as to simplify the setjmp error logic above. (Actually, I don't
176 * think that jpeg_destroy can do an error exit, but why assume anything...)
180 /* At this point you may want to check to see whether any corrupt-data
181 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
184 /* And we're done! */