]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jpgload.cpp
ok
[xonotic/netradiant.git] / 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
55   // Rad additions: initialize the longjmp buffer
56   jmpret = setjmp( rad_loadfailed );
57   if (jmpret != 0)
58   {
59     *pic = (unsigned char *)rad_errormsg;
60     return -1;
61   }
62
63   /* Step 1: allocate and initialize JPEG decompression object */
64
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.
69    */
70   cinfo.err = jpeg_std_error(&jerr);
71
72   /* Now we can initialize the JPEG decompression object. */
73   jpeg_create_decompress(&cinfo);
74
75   /* Step 2: specify data source (eg, a file) */
76
77   jpeg_stdio_src(&cinfo, fbuffer, bufsize);
78
79   /* Step 3: read file parameters with jpeg_read_header() */
80
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.
86    */
87
88   /* Step 4: set parameters for decompression */
89
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.
92    */
93
94   /* Step 5: Start decompressor */
95
96   (void) jpeg_start_decompress(&cinfo);
97   /* We can ignore the return value since suspension is not possible
98    * with the stdio data source.
99    */
100   
101   /* ydnar: radiant only handles RGB, non-progressive format jpegs */
102   if( cinfo.output_components != 4 )
103   {
104     *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Non-RGB JPEG encountered (unsupported)"));
105     return -1;
106   }
107   if( cinfo.progressive_mode )
108   {
109     *pic = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>("Progressive JPEG encountered (unsupported)"));
110     return -1;
111   }
112   
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.
118    */ 
119   
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;
123   
124   out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );
125   memset( out, 255, nSize + 1 );
126   
127   *pic = out;
128   *width = cinfo.output_width;
129   *height = cinfo.output_height;
130
131   /* Step 6: while (scan lines remain to be read) */
132   /*           jpeg_read_scanlines(...); */
133
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.
136    */
137   while (cinfo.output_scanline < cinfo.output_height)
138   {
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.
142      */
143         bbuf = out + row_stride * cinfo.output_scanline;
144         buffer = &bbuf;
145     (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
146   }
147
148   // clear all the alphas to 255
149   {
150     int i, j;
151     unsigned char *buf;
152
153     buf = *pic;
154
155     j = cinfo.output_width * cinfo.output_height * 4;
156     for ( i = 3 ; i < j ; i+=4 ) {
157       buf[i] = 255;
158     }
159   }
160
161   /* Step 7: Finish decompression */
162
163   (void) jpeg_finish_decompress(&cinfo);
164   /* We can ignore the return value since suspension is not possible
165    * with the stdio data source.
166    */
167
168   /* Step 8: Release JPEG decompression object */
169
170   /* This is an important step since it will release a good deal of memory. */
171   jpeg_destroy_decompress(&cinfo);
172
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...)
177    */
178   //free (fbuffer);
179
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).
182    */
183
184   /* And we're done! */
185   return 0;
186 }