3 #include "radiant_jpeglib.h"
\r
7 GLOBAL int LoadJPGBuff(unsigned char *fbuffer, int bufsize, unsigned char **pic, int *width, int *height )
\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
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
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
27 struct jpeg_error_mgr jerr;
\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
35 // Rad additions: initialize the longjmp buffer
\r
36 jmpret = setjmp( rad_loadfailed );
\r
39 *pic = (unsigned char *)rad_errormsg;
\r
43 /* Step 1: allocate and initialize JPEG decompression object */
\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
50 cinfo.err = jpeg_std_error(&jerr);
\r
52 /* Now we can initialize the JPEG decompression object. */
\r
53 jpeg_create_decompress(&cinfo);
\r
55 /* Step 2: specify data source (eg, a file) */
\r
57 jpeg_stdio_src(&cinfo, fbuffer, bufsize);
\r
59 /* Step 3: read file parameters with jpeg_read_header() */
\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
68 /* Step 4: set parameters for decompression */
\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
74 /* Step 5: Start decompressor */
\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
81 /* ydnar: radiant only handles RGB, non-progressive format jpegs */
\r
82 if( cinfo.output_components != 4 )
\r
84 *pic = (unsigned char*) "Non-RGB JPEG encountered (unsupported)";
\r
87 if( cinfo.progressive_mode )
\r
89 *pic = (unsigned char*) "Progressive JPEG encountered (unsupported)";
\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
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
104 out = reinterpret_cast<unsigned char*>( malloc( nSize+ 1 ) );
\r
105 memset( out, 255, nSize + 1 );
\r
108 *width = cinfo.output_width;
\r
109 *height = cinfo.output_height;
\r
111 /* Step 6: while (scan lines remain to be read) */
\r
112 /* jpeg_read_scanlines(...); */
\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
117 while (cinfo.output_scanline < cinfo.output_height)
\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
123 bbuf = out + row_stride * cinfo.output_scanline;
\r
125 (void) jpeg_read_scanlines( &cinfo, buffer, 1 );
\r
128 // clear all the alphas to 255
\r
131 unsigned char *buf;
\r
135 j = cinfo.output_width * cinfo.output_height * 4;
\r
136 for ( i = 3 ; i < j ; i+=4 ) {
\r
141 /* Step 7: Finish decompression */
\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
148 /* Step 8: Release JPEG decompression object */
\r
150 /* This is an important step since it will release a good deal of memory. */
\r
151 jpeg_destroy_decompress(&cinfo);
\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
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
164 /* And we're done! */
\r