]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jpgload.cpp
get the basics of a new scons build system together
[xonotic/netradiant.git] / libs / jpeg6 / jpgload.cpp
1 \r
2     \r
3 #include "radiant_jpeglib.h"\r
4 #include "jerror.h"\r
5 #include <memory.h>\r
6 \r
7 GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic, int *width, int *height ) \r
8 {\r
9 \r
10   /* This struct contains the JPEG decompression parameters and pointers to\r
11    * working space (which is allocated as needed by the JPEG library).\r
12    */\r
13   struct jpeg_decompress_struct cinfo;\r
14   /* We use our private extension JPEG error handler.\r
15    * Note that this struct must live as long as the main JPEG parameter\r
16    * struct, to avoid dangling-pointer problems.\r
17    */\r
18   /* This struct represents a JPEG error handler.  It is declared separately\r
19    * because applications often want to supply a specialized error handler\r
20    * (see the second half of this file for an example).  But here we just\r
21    * take the easy way out and use the standard error handler, which will\r
22    * print a message on stderr and call exit() if compression fails.\r
23    * Note that this struct must live as long as the main JPEG parameter\r
24    * struct, to avoid dangling-pointer problems.\r
25    */\r
26 \r
27   struct jpeg_error_mgr jerr;\r
28   /* More stuff */\r
29   JSAMPARRAY buffer;            /* Output row buffer */\r
30   int row_stride;               /* physical row width in output buffer */\r
31   unsigned char *out, *bbuf;\r
32   int nSize;\r
33   int jmpret;\r
34 \r
35   // Rad additions: initialize the longjmp buffer\r
36   jmpret = setjmp( rad_loadfailed );\r
37   if (jmpret != 0)\r
38   {\r
39     *pic = (unsigned char *)rad_errormsg;\r
40     return -1;\r
41   }\r
42 \r
43   /* Step 1: allocate and initialize JPEG decompression object */\r
44 \r
45   /* We have to set up the error handler first, in case the initialization\r
46    * step fails.  (Unlikely, but it could happen if you are out of memory.)\r
47    * This routine fills in the contents of struct jerr, and returns jerr's\r
48    * address which we place into the link field in cinfo.\r
49    */\r
50   cinfo.err = jpeg_std_error(&jerr);\r
51 \r
52   /* Now we can initialize the JPEG decompression object. */\r
53   jpeg_create_decompress(&cinfo);\r
54 \r
55   /* Step 2: specify data source (eg, a file) */\r
56 \r
57   jpeg_stdio_src(&cinfo, fbuffer, bufsize);\r
58 \r
59   /* Step 3: read file parameters with jpeg_read_header() */\r
60 \r
61   (void) jpeg_read_header(&cinfo, TRUE);\r
62   /* We can ignore the return value from jpeg_read_header since\r
63    *   (a) suspension is not possible with the stdio data source, and\r
64    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.\r
65    * See libjpeg.doc for more info.\r
66    */\r
67 \r
68   /* Step 4: set parameters for decompression */\r
69 \r
70   /* In this example, we don't need to change any of the defaults set by\r
71    * jpeg_read_header(), so we do nothing here.\r
72    */\r
73 \r
74   /* Step 5: Start decompressor */\r
75 \r
76   (void) jpeg_start_decompress(&cinfo);\r
77   /* We can ignore the return value since suspension is not possible\r
78    * with the stdio data source.\r
79    */\r
80   \r
81   /* ydnar: radiant only handles RGB, non-progressive format jpegs */\r
82   if( cinfo.output_components != 4 )\r
83   {\r
84         *pic = (unsigned char*) "Non-RGB JPEG encountered (unsupported)";\r
85     return -1;\r
86   }\r
87   if( cinfo.progressive_mode )\r
88   {\r
89         *pic = (unsigned char*) "Progressive JPEG encountered (unsupported)";\r
90     return -1;\r
91   }\r
92   \r
93   /* We may need to do some setup of our own at this point before reading\r
94    * the data.  After jpeg_start_decompress() we have the correct scaled\r
95    * output image dimensions available, as well as the output colormap\r
96    * if we asked for color quantization.\r
97    * In this example, we need to make an output work buffer of the right size.\r
98    */ \r
99   \r
100   /* JSAMPLEs per row in output buffer */\r
101   row_stride = cinfo.output_width * cinfo.output_components;\r
102   nSize = cinfo.output_width*cinfo.output_height*cinfo.output_components;\r
103   \r
104   out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );\r
105   memset( out, 255, nSize + 1 );\r
106   \r
107   *pic = out;\r
108   *width = cinfo.output_width;\r
109   *height = cinfo.output_height;\r
110 \r
111   /* Step 6: while (scan lines remain to be read) */\r
112   /*           jpeg_read_scanlines(...); */\r
113 \r
114   /* Here we use the library's state variable cinfo.output_scanline as the\r
115    * loop counter, so that we don't have to keep track ourselves.\r
116    */\r
117   while (cinfo.output_scanline < cinfo.output_height)\r
118   {\r
119     /* jpeg_read_scanlines expects an array of pointers to scanlines.\r
120      * Here the array is only one element long, but you could ask for\r
121      * more than one scanline at a time if that's more convenient.\r
122      */\r
123         bbuf = out + row_stride * cinfo.output_scanline;\r
124         buffer = &bbuf;\r
125     (void) jpeg_read_scanlines( &cinfo, buffer, 1 );\r
126   }\r
127 \r
128   // clear all the alphas to 255\r
129   {\r
130     int i, j;\r
131     unsigned char *buf;\r
132 \r
133     buf = *pic;\r
134 \r
135     j = cinfo.output_width * cinfo.output_height * 4;\r
136     for ( i = 3 ; i < j ; i+=4 ) {\r
137       buf[i] = 255;\r
138     }\r
139   }\r
140 \r
141   /* Step 7: Finish decompression */\r
142 \r
143   (void) jpeg_finish_decompress(&cinfo);\r
144   /* We can ignore the return value since suspension is not possible\r
145    * with the stdio data source.\r
146    */\r
147 \r
148   /* Step 8: Release JPEG decompression object */\r
149 \r
150   /* This is an important step since it will release a good deal of memory. */\r
151   jpeg_destroy_decompress(&cinfo);\r
152 \r
153   /* After finish_decompress, we can close the input file.\r
154    * Here we postpone it until after no more JPEG errors are possible,\r
155    * so as to simplify the setjmp error logic above.  (Actually, I don't\r
156    * think that jpeg_destroy can do an error exit, but why assume anything...)\r
157    */\r
158   //free (fbuffer);\r
159 \r
160   /* At this point you may want to check to see whether any corrupt-data\r
161    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).\r
162    */\r
163 \r
164   /* And we're done! */\r
165   return 0;\r
166 }\r
167 \r