Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

jload.c File Reference

#include "../game/q_shared.h"
#include "../qcommon/qcommon.h"
#include "jpeglib.h"

Include dependency graph for jload.c:

Include dependency graph

Go to the source code of this file.

Functions

int LoadJPG (const char *filename, unsigned char **pic, int *width, int *height)


Function Documentation

int LoadJPG const char *  filename,
unsigned char **  pic,
int *  width,
int *  height
 

Definition at line 16 of file jload.c.

References buffer, fileHandle_t, FS_FCloseFile(), FS_FOpenFileRead(), height, jpeg_create_decompress(), jpeg_destroy_decompress(), jpeg_finish_decompress(), jpeg_read_header(), jpeg_read_scanlines(), jpeg_start_decompress(), jpeg_std_error(), jpeg_stdio_src(), JSAMPARRAY, jpeg_decompress_struct::output_components, jpeg_decompress_struct::output_height, jpeg_decompress_struct::output_scanline, jpeg_decompress_struct::output_width, qfalse, TRUE, width, and Z_Malloc().

00016                                                                                   {
00017   /* This struct contains the JPEG decompression parameters and pointers to
00018    * working space (which is allocated as needed by the JPEG library).
00019    */
00020   struct jpeg_decompress_struct cinfo;
00021   /* We use our private extension JPEG error handler.
00022    * Note that this struct must live as long as the main JPEG parameter
00023    * struct, to avoid dangling-pointer problems.
00024    */
00025   /* This struct represents a JPEG error handler.  It is declared separately
00026    * because applications often want to supply a specialized error handler
00027    * (see the second half of this file for an example).  But here we just
00028    * take the easy way out and use the standard error handler, which will
00029    * print a message on stderr and call exit() if compression fails.
00030    * Note that this struct must live as long as the main JPEG parameter
00031    * struct, to avoid dangling-pointer problems.
00032    */
00033   struct jpeg_error_mgr jerr;
00034   /* More stuff */
00035   fileHandle_t infile;      /* source file */
00036   JSAMPARRAY buffer;        /* Output row buffer */
00037   int row_stride;       /* physical row width in output buffer */
00038   unsigned char *out;
00039 
00040   /* In this example we want to open the input file before doing anything else,
00041    * so that the setjmp() error recovery below can assume the file is open.
00042    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
00043    * requires it in order to read binary files.
00044    */
00045 
00046   FS_FOpenFileRead( filename, &infile, qfalse );
00047   if (infile == 0) {
00048     return 0;
00049   }
00050 
00051   /* Step 1: allocate and initialize JPEG decompression object */
00052 
00053   /* We have to set up the error handler first, in case the initialization
00054    * step fails.  (Unlikely, but it could happen if you are out of memory.)
00055    * This routine fills in the contents of struct jerr, and returns jerr's
00056    * address which we place into the link field in cinfo.
00057    */
00058   cinfo.err = jpeg_std_error(&jerr);
00059 
00060   /* Now we can initialize the JPEG decompression object. */
00061   jpeg_create_decompress(&cinfo);
00062 
00063   /* Step 2: specify data source (eg, a file) */
00064 
00065   jpeg_stdio_src(&cinfo, infile);
00066 
00067   /* Step 3: read file parameters with jpeg_read_header() */
00068 
00069   (void) jpeg_read_header(&cinfo, TRUE);
00070   /* We can ignore the return value from jpeg_read_header since
00071    *   (a) suspension is not possible with the stdio data source, and
00072    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
00073    * See libjpeg.doc for more info.
00074    */
00075 
00076   /* Step 4: set parameters for decompression */
00077 
00078   /* In this example, we don't need to change any of the defaults set by
00079    * jpeg_read_header(), so we do nothing here.
00080    */
00081 
00082   /* Step 5: Start decompressor */
00083 
00084   (void) jpeg_start_decompress(&cinfo);
00085   /* We can ignore the return value since suspension is not possible
00086    * with the stdio data source.
00087    */
00088 
00089   /* We may need to do some setup of our own at this point before reading
00090    * the data.  After jpeg_start_decompress() we have the correct scaled
00091    * output image dimensions available, as well as the output colormap
00092    * if we asked for color quantization.
00093    * In this example, we need to make an output work buffer of the right size.
00094    */ 
00095   /* JSAMPLEs per row in output buffer */
00096   row_stride = cinfo.output_width * cinfo.output_components;
00097 
00098   out = Z_Malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components);
00099 
00100   *pic = out;
00101   *width = cinfo.output_width;
00102   *height = cinfo.output_height;
00103 
00104   /* Step 6: while (scan lines remain to be read) */
00105   /*           jpeg_read_scanlines(...); */
00106 
00107   /* Here we use the library's state variable cinfo.output_scanline as the
00108    * loop counter, so that we don't have to keep track ourselves.
00109    */
00110   while (cinfo.output_scanline < cinfo.output_height) {
00111     /* jpeg_read_scanlines expects an array of pointers to scanlines.
00112      * Here the array is only one element long, but you could ask for
00113      * more than one scanline at a time if that's more convenient.
00114      */
00115     buffer = (JSAMPARRAY)out+(row_stride*cinfo.output_scanline);
00116     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
00117   }
00118 
00119   /* Step 7: Finish decompression */
00120 
00121   (void) jpeg_finish_decompress(&cinfo);
00122   /* We can ignore the return value since suspension is not possible
00123    * with the stdio data source.
00124    */
00125 
00126   /* Step 8: Release JPEG decompression object */
00127 
00128   /* This is an important step since it will release a good deal of memory. */
00129   jpeg_destroy_decompress(&cinfo);
00130 
00131   /* After finish_decompress, we can close the input file.
00132    * Here we postpone it until after no more JPEG errors are possible,
00133    * so as to simplify the setjmp error logic above.  (Actually, I don't
00134    * think that jpeg_destroy can do an error exit, but why assume anything...)
00135    */
00136   FS_FCloseFile(infile);
00137 
00138   /* At this point you may want to check to see whether any corrupt-data
00139    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
00140    */
00141 
00142   /* And we're done! */
00143   return 1;
00144 }

Here is the call graph for this function:


Generated on Thu Aug 25 14:16:31 2005 for Quake III Arena by  doxygen 1.3.9.1