]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/source/netradiant-src/libs/jpeg6/jpgload.cpp
Update netRadiant
[voretournament/voretournament.git] / misc / source / netradiant-src / libs / jpeg6 / jpgload.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22     
23 #include "radiant_jpeglib.h"
24 #include "jerror.h"
25 #include <memory.h>
26
27 GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic, int *width, int *height ) 
28 {
29
30   /* This struct contains the JPEG decompression parameters and pointers to
31    * working space (which is allocated as needed by the JPEG library).
32    */
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.
37    */
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.
45    */
46
47   struct jpeg_error_mgr jerr;
48   /* More stuff */
49   JSAMPARRAY buffer;            /* Output row buffer */
50   int row_stride;               /* physical row width in output buffer */
51   unsigned char *out, *bbuf;
52   int nSize;
53   int jmpret;
54   int i;
55
56   // Rad additions: initialize the longjmp buffer
57   jmpret = setjmp( rad_loadfailed );
58   if (jmpret != 0)
59   {
60     *pic = (unsigned char *)rad_errormsg;
61     return -1;
62   }
63
64   /* Step 1: allocate and initialize JPEG decompression object */
65
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.
70    */
71   cinfo.err = jpeg_std_error(&jerr);
72
73   /* Now we can initialize the JPEG decompression object. */
74   jpeg_create_decompress(&cinfo);
75
76   /* Step 2: specify data source (eg, a file) */
77
78   jpeg_stdio_src(&cinfo, fbuffer, bufsize);
79
80   /* Step 3: read file parameters with jpeg_read_header() */
81
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.
87    */
88
89   /* Step 4: set parameters for decompression */
90
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.
93    */
94
95   /* Step 5: Start decompressor */
96
97   (void) jpeg_start_decompress(&cinfo);
98   /* We can ignore the return value since suspension is not possible
99    * with the stdio data source.
100    */
101   
102   if( cinfo.output_components != 1 && cinfo.output_components != 3 && cinfo.output_components != 4 )
103   {
104     *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Non-Y/RGB/RGBA JPEG encountered (unsupported)"));
105     return -1;
106   }
107
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.
113    */ 
114   
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;
118   
119   out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );
120   memset( out, 255, nSize + 1 );
121   
122   *pic = out;
123   *width = cinfo.output_width;
124   *height = cinfo.output_height;
125
126   /* Step 6: while (scan lines remain to be read) */
127   /*           jpeg_read_scanlines(...); */
128
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.
131    */
132   while (cinfo.output_scanline < cinfo.output_height)
133   {
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.
137      */
138         bbuf = out + row_stride * cinfo.output_scanline;
139         buffer = &bbuf;
140         (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
141
142         // we convert downwards to not overwrite values we want to read later
143         switch(cinfo.output_components)
144         {
145                 case 4:
146                         // clear all the alphas to 255
147                         for(i = cinfo.output_width; i-- > 0; )
148                         {
149                                 bbuf[i*4+3] = 255;
150                         }
151                         break;
152                 case 3:
153                         // expand 3 to 4
154                         for(i = cinfo.output_width; i-- > 0; )
155                         {
156                                 bbuf[i*4+3] = 255;
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];
160                         }
161                         break;
162                 case 1:
163                         // expand 1 to 4
164                         for(i = cinfo.output_width; i-- > 0; )
165                         {
166                                 bbuf[i*4+3] = 255;
167                                 bbuf[i*4+2] = bbuf[i];
168                                 bbuf[i*4+1] = bbuf[i];
169                                 bbuf[i*4+0] = bbuf[i];
170                         }
171                         break;
172         }
173   }
174
175   /* Step 7: Finish decompression */
176
177   (void) jpeg_finish_decompress(&cinfo);
178   /* We can ignore the return value since suspension is not possible
179    * with the stdio data source.
180    */
181
182   /* Step 8: Release JPEG decompression object */
183
184   /* This is an important step since it will release a good deal of memory. */
185   jpeg_destroy_decompress(&cinfo);
186
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...)
191    */
192   //free (fbuffer);
193
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).
196    */
197
198   /* And we're done! */
199   return 0;
200 }