Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * example.c |
| 3 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 4 | * This file illustrates how to use the IJG code as a subroutine library |
| 5 | * to read or write JPEG image files. You should look at this code in |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 6 | * conjunction with the documentation file libjpeg.txt. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 7 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 8 | * This code will not do anything useful as-is, but it may be helpful as a |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 9 | * skeleton for constructing routines that call the JPEG library. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * We present these routines in the same coding style used in the JPEG code |
| 12 | * (ANSI function definitions, etc); but you are of course free to code your |
| 13 | * routines in a different style if you prefer. |
| 14 | */ |
| 15 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 18 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 19 | * Include file for users of JPEG library. |
| 20 | * You will need to have included system headers that define at least |
| 21 | * the typedefs FILE and size_t before you can include jpeglib.h. |
| 22 | * (stdio.h is sufficient on ANSI-conforming systems.) |
| 23 | * You may also wish to include "jerror.h". |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 26 | #include "jpeglib.h" |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * <setjmp.h> is used for the optional error recovery mechanism shown in |
| 30 | * the second part of the example. |
| 31 | */ |
| 32 | |
| 33 | #include <setjmp.h> |
| 34 | |
| 35 | |
| 36 | |
| 37 | /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/ |
| 38 | |
| 39 | /* This half of the example shows how to feed data into the JPEG compressor. |
| 40 | * We present a minimal version that does not worry about refinements such |
| 41 | * as error recovery (the JPEG code will just exit() if it gets an error). |
| 42 | */ |
| 43 | |
| 44 | |
| 45 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 46 | * IMAGE DATA FORMATS: |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 47 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 48 | * The standard input image format is a rectangular array of pixels, with |
| 49 | * each pixel having the same number of "component" values (color channels). |
| 50 | * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars). |
| 51 | * If you are working with color data, then the color values for each pixel |
| 52 | * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit |
| 53 | * RGB color. |
| 54 | * |
| 55 | * For this example, we'll assume that this data structure matches the way |
| 56 | * our application has stored the image in memory, so we can just pass a |
| 57 | * pointer to our image buffer. In particular, let's say that the image is |
| 58 | * RGB color and is described by: |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 59 | */ |
| 60 | |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 61 | extern JSAMPLE *image_buffer; /* Points to large array of R,G,B-order data */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 62 | extern int image_height; /* Number of rows in image */ |
| 63 | extern int image_width; /* Number of columns in image */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 67 | * Sample routine for JPEG compression. We assume that the target file name |
| 68 | * and a compression quality factor are passed in. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 69 | */ |
| 70 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 71 | GLOBAL(void) |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 72 | write_JPEG_file (char *filename, int quality) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 73 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 74 | /* This struct contains the JPEG compression parameters and pointers to |
| 75 | * working space (which is allocated as needed by the JPEG library). |
| 76 | * It is possible to have several such structures, representing multiple |
| 77 | * compression/decompression processes, in existence at once. We refer |
| 78 | * to any one struct (and its associated working data) as a "JPEG object". |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 79 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 80 | struct jpeg_compress_struct cinfo; |
| 81 | /* This struct represents a JPEG error handler. It is declared separately |
| 82 | * because applications often want to supply a specialized error handler |
| 83 | * (see the second half of this file for an example). But here we just |
| 84 | * take the easy way out and use the standard error handler, which will |
| 85 | * print a message on stderr and call exit() if compression fails. |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 86 | * Note that this struct must live as long as the main JPEG parameter |
| 87 | * struct, to avoid dangling-pointer problems. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 88 | */ |
| 89 | struct jpeg_error_mgr jerr; |
| 90 | /* More stuff */ |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 91 | FILE *outfile; /* target file */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 92 | JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ |
| 93 | int row_stride; /* physical row width in image buffer */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 94 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 95 | /* Step 1: allocate and initialize JPEG compression object */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 96 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 97 | /* We have to set up the error handler first, in case the initialization |
| 98 | * step fails. (Unlikely, but it could happen if you are out of memory.) |
| 99 | * This routine fills in the contents of struct jerr, and returns jerr's |
| 100 | * address which we place into the link field in cinfo. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 101 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 102 | cinfo.err = jpeg_std_error(&jerr); |
| 103 | /* Now we can initialize the JPEG compression object. */ |
| 104 | jpeg_create_compress(&cinfo); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 105 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 106 | /* Step 2: specify data destination (eg, a file) */ |
| 107 | /* Note: steps 2 and 3 can be done in either order. */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 108 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 109 | /* Here we use the library-supplied code to send compressed data to a |
| 110 | * stdio stream. You can also write your own code to do something else. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 111 | * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that |
| 112 | * requires it in order to write binary files. |
| 113 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 114 | if ((outfile = fopen(filename, "wb")) == NULL) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 115 | fprintf(stderr, "can't open %s\n", filename); |
| 116 | exit(1); |
| 117 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 118 | jpeg_stdio_dest(&cinfo, outfile); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 119 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 120 | /* Step 3: set parameters for compression */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 121 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 122 | /* First we supply a description of the input image. |
| 123 | * Four fields of the cinfo struct must be filled in: |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 124 | */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 125 | cinfo.image_width = image_width; /* image width and height, in pixels */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 126 | cinfo.image_height = image_height; |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 127 | cinfo.input_components = 3; /* # of color components per pixel */ |
| 128 | cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 129 | /* Now use the library's routine to set default compression parameters. |
| 130 | * (You must set at least cinfo.in_color_space before calling this, |
| 131 | * since the defaults depend on the source color space.) |
| 132 | */ |
| 133 | jpeg_set_defaults(&cinfo); |
| 134 | /* Now you can set any non-default parameters you wish to. |
| 135 | * Here we just illustrate the use of quality (quantization table) scaling: |
| 136 | */ |
| 137 | jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); |
| 138 | |
| 139 | /* Step 4: Start compressor */ |
| 140 | |
| 141 | /* TRUE ensures that we will write a complete interchange-JPEG file. |
| 142 | * Pass TRUE unless you are very sure of what you're doing. |
| 143 | */ |
| 144 | jpeg_start_compress(&cinfo, TRUE); |
| 145 | |
| 146 | /* Step 5: while (scan lines remain to be written) */ |
| 147 | /* jpeg_write_scanlines(...); */ |
| 148 | |
| 149 | /* Here we use the library's state variable cinfo.next_scanline as the |
| 150 | * loop counter, so that we don't have to keep track ourselves. |
| 151 | * To keep things simple, we pass one scanline per call; you can pass |
| 152 | * more if you wish, though. |
| 153 | */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 154 | row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 155 | |
| 156 | while (cinfo.next_scanline < cinfo.image_height) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 157 | /* jpeg_write_scanlines expects an array of pointers to scanlines. |
| 158 | * Here the array is only one element long, but you could pass |
| 159 | * more than one scanline at a time if that's more convenient. |
| 160 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 161 | row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; |
| 162 | (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); |
| 163 | } |
| 164 | |
| 165 | /* Step 6: Finish compression */ |
| 166 | |
| 167 | jpeg_finish_compress(&cinfo); |
| 168 | /* After finish_compress, we can close the output file. */ |
| 169 | fclose(outfile); |
| 170 | |
| 171 | /* Step 7: release JPEG compression object */ |
| 172 | |
| 173 | /* This is an important step since it will release a good deal of memory. */ |
| 174 | jpeg_destroy_compress(&cinfo); |
| 175 | |
| 176 | /* And we're done! */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 180 | /* |
| 181 | * SOME FINE POINTS: |
| 182 | * |
| 183 | * In the above loop, we ignored the return value of jpeg_write_scanlines, |
| 184 | * which is the number of scanlines actually written. We could get away |
| 185 | * with this because we were only relying on the value of cinfo.next_scanline, |
| 186 | * which will be incremented correctly. If you maintain additional loop |
| 187 | * variables then you should be careful to increment them properly. |
| 188 | * Actually, for output to a stdio stream you needn't worry, because |
| 189 | * then jpeg_write_scanlines will write all the lines passed (or else exit |
| 190 | * with a fatal error). Partial writes can only occur if you use a data |
| 191 | * destination module that can demand suspension of the compressor. |
| 192 | * (If you don't know what that's for, you don't need it.) |
| 193 | * |
| 194 | * If the compressor requires full-image buffers (for entropy-coding |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 195 | * optimization or a multi-scan JPEG file), it will create temporary |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 196 | * files for anything that doesn't fit within the maximum-memory setting. |
| 197 | * (Note that temp files are NOT needed if you use the default parameters.) |
| 198 | * On some systems you may need to set up a signal handler to ensure that |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 199 | * temporary files are deleted if the program is interrupted. See libjpeg.txt. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 200 | * |
| 201 | * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG |
| 202 | * files to be compatible with everyone else's. If you cannot readily read |
| 203 | * your data in that order, you'll need an intermediate array to hold the |
| 204 | * image. See rdtarga.c or rdbmp.c for examples of handling bottom-to-top |
| 205 | * source data using the JPEG code's internal virtual-array mechanisms. |
| 206 | */ |
| 207 | |
| 208 | |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 209 | |
| 210 | /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/ |
| 211 | |
| 212 | /* This half of the example shows how to read data from the JPEG decompressor. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 213 | * It's a bit more refined than the above, in that we show: |
| 214 | * (a) how to modify the JPEG library's standard error-reporting behavior; |
| 215 | * (b) how to allocate workspace using the library's memory manager. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 216 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 217 | * Just to make this example a little different from the first one, we'll |
| 218 | * assume that we do not intend to put the whole image into an in-memory |
| 219 | * buffer, but to send it line-by-line someplace else. We need a one- |
| 220 | * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG |
| 221 | * memory manager allocate it for us. This approach is actually quite useful |
| 222 | * because we don't need to remember to deallocate the buffer separately: it |
| 223 | * will go away automatically when the JPEG object is cleaned up. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 224 | */ |
| 225 | |
| 226 | |
| 227 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 228 | * ERROR HANDLING: |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 229 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 230 | * The JPEG library's standard error handler (jerror.c) is divided into |
| 231 | * several "methods" which you can override individually. This lets you |
| 232 | * adjust the behavior without duplicating a lot of code, which you might |
| 233 | * have to update with each future release. |
| 234 | * |
| 235 | * Our example here shows how to override the "error_exit" method so that |
| 236 | * control is returned to the library's caller when a fatal error occurs, |
| 237 | * rather than calling exit() as the standard error_exit method does. |
| 238 | * |
| 239 | * We use C's setjmp/longjmp facility to return control. This means that the |
| 240 | * routine which calls the JPEG library must first execute a setjmp() call to |
| 241 | * establish the return point. We want the replacement error_exit to do a |
| 242 | * longjmp(). But we need to make the setjmp buffer accessible to the |
| 243 | * error_exit routine. To do this, we make a private extension of the |
| 244 | * standard JPEG error handler object. (If we were using C++, we'd say we |
| 245 | * were making a subclass of the regular error handler.) |
| 246 | * |
| 247 | * Here's the extended error handler struct: |
| 248 | */ |
| 249 | |
| 250 | struct my_error_mgr { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 251 | struct jpeg_error_mgr pub; /* "public" fields */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 252 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 253 | jmp_buf setjmp_buffer; /* for return to caller */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 256 | typedef struct my_error_mgr *my_error_ptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * Here's the routine that will replace the standard error_exit method: |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 260 | */ |
| 261 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 262 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 263 | my_error_exit (j_common_ptr cinfo) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 264 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 265 | /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ |
| 266 | my_error_ptr myerr = (my_error_ptr) cinfo->err; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 267 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 268 | /* Always display the message. */ |
| 269 | /* We could postpone this until after returning, if we chose. */ |
| 270 | (*cinfo->err->output_message) (cinfo); |
| 271 | |
| 272 | /* Return control to the setjmp point */ |
| 273 | longjmp(myerr->setjmp_buffer, 1); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
| 277 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 278 | * Sample routine for JPEG decompression. We assume that the source file name |
| 279 | * is passed in. We want to return 1 on success, 0 on error. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 280 | */ |
| 281 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 282 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 283 | GLOBAL(int) |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 284 | read_JPEG_file (char *filename) |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 285 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 286 | /* This struct contains the JPEG decompression parameters and pointers to |
| 287 | * working space (which is allocated as needed by the JPEG library). |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 288 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 289 | struct jpeg_decompress_struct cinfo; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 290 | /* We use our private extension JPEG error handler. |
| 291 | * Note that this struct must live as long as the main JPEG parameter |
| 292 | * struct, to avoid dangling-pointer problems. |
| 293 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 294 | struct my_error_mgr jerr; |
| 295 | /* More stuff */ |
DRC | bd49803 | 2016-02-19 08:53:33 -0600 | [diff] [blame] | 296 | FILE *infile; /* source file */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 297 | JSAMPARRAY buffer; /* Output row buffer */ |
| 298 | int row_stride; /* physical row width in output buffer */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 299 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 300 | /* In this example we want to open the input file before doing anything else, |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 301 | * so that the setjmp() error recovery below can assume the file is open. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 302 | * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that |
| 303 | * requires it in order to read binary files. |
| 304 | */ |
| 305 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 306 | if ((infile = fopen(filename, "rb")) == NULL) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 307 | fprintf(stderr, "can't open %s\n", filename); |
| 308 | return 0; |
| 309 | } |
| 310 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 311 | /* Step 1: allocate and initialize JPEG decompression object */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 312 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 313 | /* We set up the normal JPEG error routines, then override error_exit. */ |
| 314 | cinfo.err = jpeg_std_error(&jerr.pub); |
| 315 | jerr.pub.error_exit = my_error_exit; |
| 316 | /* Establish the setjmp return context for my_error_exit to use. */ |
| 317 | if (setjmp(jerr.setjmp_buffer)) { |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 318 | /* If we get here, the JPEG code has signaled an error. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 319 | * We need to clean up the JPEG object, close the input file, and return. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 320 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 321 | jpeg_destroy_decompress(&cinfo); |
| 322 | fclose(infile); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 325 | /* Now we can initialize the JPEG decompression object. */ |
| 326 | jpeg_create_decompress(&cinfo); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 327 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 328 | /* Step 2: specify data source (eg, a file) */ |
| 329 | |
| 330 | jpeg_stdio_src(&cinfo, infile); |
| 331 | |
| 332 | /* Step 3: read file parameters with jpeg_read_header() */ |
| 333 | |
| 334 | (void) jpeg_read_header(&cinfo, TRUE); |
| 335 | /* We can ignore the return value from jpeg_read_header since |
| 336 | * (a) suspension is not possible with the stdio data source, and |
| 337 | * (b) we passed TRUE to reject a tables-only JPEG file as an error. |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 338 | * See libjpeg.txt for more info. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 339 | */ |
| 340 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 341 | /* Step 4: set parameters for decompression */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 342 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 343 | /* In this example, we don't need to change any of the defaults set by |
| 344 | * jpeg_read_header(), so we do nothing here. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 345 | */ |
| 346 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 347 | /* Step 5: Start decompressor */ |
| 348 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 349 | (void) jpeg_start_decompress(&cinfo); |
| 350 | /* We can ignore the return value since suspension is not possible |
| 351 | * with the stdio data source. |
| 352 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 353 | |
| 354 | /* We may need to do some setup of our own at this point before reading |
| 355 | * the data. After jpeg_start_decompress() we have the correct scaled |
| 356 | * output image dimensions available, as well as the output colormap |
| 357 | * if we asked for color quantization. |
| 358 | * In this example, we need to make an output work buffer of the right size. |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 359 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 360 | /* JSAMPLEs per row in output buffer */ |
| 361 | row_stride = cinfo.output_width * cinfo.output_components; |
| 362 | /* Make a one-row-high sample array that will go away when done with image */ |
| 363 | buffer = (*cinfo.mem->alloc_sarray) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 364 | ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 365 | |
| 366 | /* Step 6: while (scan lines remain to be read) */ |
| 367 | /* jpeg_read_scanlines(...); */ |
| 368 | |
| 369 | /* Here we use the library's state variable cinfo.output_scanline as the |
| 370 | * loop counter, so that we don't have to keep track ourselves. |
| 371 | */ |
| 372 | while (cinfo.output_scanline < cinfo.output_height) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 373 | /* jpeg_read_scanlines expects an array of pointers to scanlines. |
| 374 | * Here the array is only one element long, but you could ask for |
| 375 | * more than one scanline at a time if that's more convenient. |
| 376 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 377 | (void) jpeg_read_scanlines(&cinfo, buffer, 1); |
| 378 | /* Assume put_scanline_someplace wants a pointer and sample count. */ |
| 379 | put_scanline_someplace(buffer[0], row_stride); |
| 380 | } |
| 381 | |
| 382 | /* Step 7: Finish decompression */ |
| 383 | |
| 384 | (void) jpeg_finish_decompress(&cinfo); |
| 385 | /* We can ignore the return value since suspension is not possible |
| 386 | * with the stdio data source. |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 387 | */ |
| 388 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 389 | /* Step 8: Release JPEG decompression object */ |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 390 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 391 | /* This is an important step since it will release a good deal of memory. */ |
| 392 | jpeg_destroy_decompress(&cinfo); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 393 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 394 | /* After finish_decompress, we can close the input file. |
| 395 | * Here we postpone it until after no more JPEG errors are possible, |
| 396 | * so as to simplify the setjmp error logic above. (Actually, I don't |
| 397 | * think that jpeg_destroy can do an error exit, but why assume anything...) |
Thomas G. Lane | 88aeed4 | 1992-12-10 00:00:00 +0000 | [diff] [blame] | 398 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 399 | fclose(infile); |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 400 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 401 | /* At this point you may want to check to see whether any corrupt-data |
| 402 | * warnings occurred (test whether jerr.pub.num_warnings is nonzero). |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 403 | */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 404 | |
| 405 | /* And we're done! */ |
| 406 | return 1; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 407 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 408 | |
| 409 | |
| 410 | /* |
| 411 | * SOME FINE POINTS: |
| 412 | * |
| 413 | * In the above code, we ignored the return value of jpeg_read_scanlines, |
| 414 | * which is the number of scanlines actually read. We could get away with |
| 415 | * this because we asked for only one line at a time and we weren't using |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 416 | * a suspending data source. See libjpeg.txt for more info. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 417 | * |
| 418 | * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress(); |
| 419 | * we should have done it beforehand to ensure that the space would be |
| 420 | * counted against the JPEG max_memory setting. In some systems the above |
| 421 | * code would risk an out-of-memory error. However, in general we don't |
| 422 | * know the output image dimensions before jpeg_start_decompress(), unless we |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 423 | * call jpeg_calc_output_dimensions(). See libjpeg.txt for more about this. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 424 | * |
| 425 | * Scanlines are returned in the same order as they appear in the JPEG file, |
| 426 | * which is standardly top-to-bottom. If you must emit data bottom-to-top, |
| 427 | * you can use one of the virtual arrays provided by the JPEG memory manager |
| 428 | * to invert the data. See wrbmp.c for an example. |
| 429 | * |
| 430 | * As with compression, some operating modes may require temporary files. |
| 431 | * On some systems you may need to set up a signal handler to ensure that |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 432 | * temporary files are deleted if the program is interrupted. See libjpeg.txt. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 433 | */ |