1
2 #include "../qcommon/q_shared.h"
3 #include "../qcommon/qcommon.h"
4
5 /*
6 * Include file for users of JPEG library.
7 * You will need to have included system headers that define at least
8 * the typedefs FILE and size_t before you can include jpeglib.h.
9 * (stdio.h is sufficient on ANSI-conforming systems.)
10 * You may also wish to include "jerror.h".
11 */
12
13 #include "jpeglib.h"
14
15
LoadJPG(const char * filename,unsigned char ** pic,int * width,int * height)16 int LoadJPG( const char *filename, unsigned char **pic, int *width, int *height ) {
17 /* This struct contains the JPEG decompression parameters and pointers to
18 * working space (which is allocated as needed by the JPEG library).
19 */
20 struct jpeg_decompress_struct cinfo;
21 /* We use our private extension JPEG error handler.
22 * Note that this struct must live as long as the main JPEG parameter
23 * struct, to avoid dangling-pointer problems.
24 */
25 /* This struct represents a JPEG error handler. It is declared separately
26 * because applications often want to supply a specialized error handler
27 * (see the second half of this file for an example). But here we just
28 * take the easy way out and use the standard error handler, which will
29 * print a message on stderr and call exit() if compression fails.
30 * Note that this struct must live as long as the main JPEG parameter
31 * struct, to avoid dangling-pointer problems.
32 */
33 struct jpeg_error_mgr jerr;
34 /* More stuff */
35 fileHandle_t infile; /* source file */
36 JSAMPARRAY buffer; /* Output row buffer */
37 int row_stride; /* physical row width in output buffer */
38 unsigned char *out;
39
40 /* In this example we want to open the input file before doing anything else,
41 * so that the setjmp() error recovery below can assume the file is open.
42 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
43 * requires it in order to read binary files.
44 */
45
46 FS_FOpenFileRead( filename, &infile, qfalse );
47 if (infile == 0) {
48 return 0;
49 }
50
51 /* Step 1: allocate and initialize JPEG decompression object */
52
53 /* We have to set up the error handler first, in case the initialization
54 * step fails. (Unlikely, but it could happen if you are out of memory.)
55 * This routine fills in the contents of struct jerr, and returns jerr's
56 * address which we place into the link field in cinfo.
57 */
58 cinfo.err = jpeg_std_error(&jerr);
59
60 /* Now we can initialize the JPEG decompression object. */
61 jpeg_create_decompress(&cinfo);
62
63 /* Step 2: specify data source (eg, a file) */
64
65 jpeg_stdio_src(&cinfo, infile);
66
67 /* Step 3: read file parameters with jpeg_read_header() */
68
69 (void) jpeg_read_header(&cinfo, TRUE);
70 /* We can ignore the return value from jpeg_read_header since
71 * (a) suspension is not possible with the stdio data source, and
72 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
73 * See libjpeg.doc for more info.
74 */
75
76 /* Step 4: set parameters for decompression */
77
78 /* In this example, we don't need to change any of the defaults set by
79 * jpeg_read_header(), so we do nothing here.
80 */
81
82 /* Step 5: Start decompressor */
83
84 (void) jpeg_start_decompress(&cinfo);
85 /* We can ignore the return value since suspension is not possible
86 * with the stdio data source.
87 */
88
89 /* We may need to do some setup of our own at this point before reading
90 * the data. After jpeg_start_decompress() we have the correct scaled
91 * output image dimensions available, as well as the output colormap
92 * if we asked for color quantization.
93 * In this example, we need to make an output work buffer of the right size.
94 */
95 /* JSAMPLEs per row in output buffer */
96 row_stride = cinfo.output_width * cinfo.output_components;
97
98 out = Z_Malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components);
99
100 *pic = out;
101 *width = cinfo.output_width;
102 *height = cinfo.output_height;
103
104 /* Step 6: while (scan lines remain to be read) */
105 /* jpeg_read_scanlines(...); */
106
107 /* Here we use the library's state variable cinfo.output_scanline as the
108 * loop counter, so that we don't have to keep track ourselves.
109 */
110 while (cinfo.output_scanline < cinfo.output_height) {
111 /* jpeg_read_scanlines expects an array of pointers to scanlines.
112 * Here the array is only one element long, but you could ask for
113 * more than one scanline at a time if that's more convenient.
114 */
115 buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline);
116 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
117 }
118
119 /* Step 7: Finish decompression */
120
121 (void) jpeg_finish_decompress(&cinfo);
122 /* We can ignore the return value since suspension is not possible
123 * with the stdio data source.
124 */
125
126 /* Step 8: Release JPEG decompression object */
127
128 /* This is an important step since it will release a good deal of memory. */
129 jpeg_destroy_decompress(&cinfo);
130
131 /* After finish_decompress, we can close the input file.
132 * Here we postpone it until after no more JPEG errors are possible,
133 * so as to simplify the setjmp error logic above. (Actually, I don't
134 * think that jpeg_destroy can do an error exit, but why assume anything...)
135 */
136 FS_FCloseFile(infile);
137
138 /* At this point you may want to check to see whether any corrupt-data
139 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
140 */
141
142 /* And we're done! */
143 return 1;
144 }
145
146