Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: example.c

Issue 1953443002: Update to libjpeg_turbo 1.4.90 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libjpeg_turbo.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « doxygen-extra.css ('k') | jaricom.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * example.c 2 * example.c
3 * 3 *
4 * This file illustrates how to use the IJG code as a subroutine library 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 5 * to read or write JPEG image files. You should look at this code in
6 * conjunction with the documentation file libjpeg.txt. 6 * conjunction with the documentation file libjpeg.txt.
7 * 7 *
8 * This code will not do anything useful as-is, but it may be helpful as a 8 * This code will not do anything useful as-is, but it may be helpful as a
9 * skeleton for constructing routines that call the JPEG library. 9 * skeleton for constructing routines that call the JPEG library.
10 * 10 *
11 * We present these routines in the same coding style used in the JPEG code 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 12 * (ANSI function definitions, etc); but you are of course free to code your
13 * routines in a different style if you prefer. 13 * routines in a different style if you prefer.
14 */ 14 */
15 15
16 #include <stdio.h> 16 #include <stdio.h>
17 17
18 /* 18 /*
19 * Include file for users of JPEG library. 19 * Include file for users of JPEG library.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 * If you are working with color data, then the color values for each pixel 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 52 * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
53 * RGB color. 53 * RGB color.
54 * 54 *
55 * For this example, we'll assume that this data structure matches the way 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 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 57 * pointer to our image buffer. In particular, let's say that the image is
58 * RGB color and is described by: 58 * RGB color and is described by:
59 */ 59 */
60 60
61 extern JSAMPLE * image_buffer;» /* Points to large array of R,G,B-order data */ 61 extern JSAMPLE *image_buffer; /* Points to large array of R,G,B-order data */
62 extern int image_height;» /* Number of rows in image */ 62 extern int image_height; /* Number of rows in image */
63 extern int image_width;»» /* Number of columns in image */ 63 extern int image_width; /* Number of columns in image */
64 64
65 65
66 /* 66 /*
67 * Sample routine for JPEG compression. We assume that the target file name 67 * Sample routine for JPEG compression. We assume that the target file name
68 * and a compression quality factor are passed in. 68 * and a compression quality factor are passed in.
69 */ 69 */
70 70
71 GLOBAL(void) 71 GLOBAL(void)
72 write_JPEG_file (char * filename, int quality) 72 write_JPEG_file (char *filename, int quality)
73 { 73 {
74 /* This struct contains the JPEG compression parameters and pointers to 74 /* This struct contains the JPEG compression parameters and pointers to
75 * working space (which is allocated as needed by the JPEG library). 75 * working space (which is allocated as needed by the JPEG library).
76 * It is possible to have several such structures, representing multiple 76 * It is possible to have several such structures, representing multiple
77 * compression/decompression processes, in existence at once. We refer 77 * compression/decompression processes, in existence at once. We refer
78 * to any one struct (and its associated working data) as a "JPEG object". 78 * to any one struct (and its associated working data) as a "JPEG object".
79 */ 79 */
80 struct jpeg_compress_struct cinfo; 80 struct jpeg_compress_struct cinfo;
81 /* This struct represents a JPEG error handler. It is declared separately 81 /* This struct represents a JPEG error handler. It is declared separately
82 * because applications often want to supply a specialized error handler 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 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 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. 85 * print a message on stderr and call exit() if compression fails.
86 * Note that this struct must live as long as the main JPEG parameter 86 * Note that this struct must live as long as the main JPEG parameter
87 * struct, to avoid dangling-pointer problems. 87 * struct, to avoid dangling-pointer problems.
88 */ 88 */
89 struct jpeg_error_mgr jerr; 89 struct jpeg_error_mgr jerr;
90 /* More stuff */ 90 /* More stuff */
91 FILE * outfile;» » /* target file */ 91 FILE *outfile; /* target file */
92 JSAMPROW row_pointer[1];» /* pointer to JSAMPLE row[s] */ 92 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
93 int row_stride;» » /* physical row width in image buffer */ 93 int row_stride; /* physical row width in image buffer */
94 94
95 /* Step 1: allocate and initialize JPEG compression object */ 95 /* Step 1: allocate and initialize JPEG compression object */
96 96
97 /* We have to set up the error handler first, in case the initialization 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.) 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 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. 100 * address which we place into the link field in cinfo.
101 */ 101 */
102 cinfo.err = jpeg_std_error(&jerr); 102 cinfo.err = jpeg_std_error(&jerr);
103 /* Now we can initialize the JPEG compression object. */ 103 /* Now we can initialize the JPEG compression object. */
(...skipping 11 matching lines...) Expand all
115 fprintf(stderr, "can't open %s\n", filename); 115 fprintf(stderr, "can't open %s\n", filename);
116 exit(1); 116 exit(1);
117 } 117 }
118 jpeg_stdio_dest(&cinfo, outfile); 118 jpeg_stdio_dest(&cinfo, outfile);
119 119
120 /* Step 3: set parameters for compression */ 120 /* Step 3: set parameters for compression */
121 121
122 /* First we supply a description of the input image. 122 /* First we supply a description of the input image.
123 * Four fields of the cinfo struct must be filled in: 123 * Four fields of the cinfo struct must be filled in:
124 */ 124 */
125 cinfo.image_width = image_width; » /* image width and height, in pixels */ 125 cinfo.image_width = image_width; /* image width and height, in pixels */
126 cinfo.image_height = image_height; 126 cinfo.image_height = image_height;
127 cinfo.input_components = 3;» » /* # of color components per pixel */ 127 cinfo.input_components = 3; /* # of color components per pixel */
128 cinfo.in_color_space = JCS_RGB; » /* colorspace of input image */ 128 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
129 /* Now use the library's routine to set default compression parameters. 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, 130 * (You must set at least cinfo.in_color_space before calling this,
131 * since the defaults depend on the source color space.) 131 * since the defaults depend on the source color space.)
132 */ 132 */
133 jpeg_set_defaults(&cinfo); 133 jpeg_set_defaults(&cinfo);
134 /* Now you can set any non-default parameters you wish to. 134 /* Now you can set any non-default parameters you wish to.
135 * Here we just illustrate the use of quality (quantization table) scaling: 135 * Here we just illustrate the use of quality (quantization table) scaling:
136 */ 136 */
137 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); 137 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
138 138
139 /* Step 4: Start compressor */ 139 /* Step 4: Start compressor */
140 140
141 /* TRUE ensures that we will write a complete interchange-JPEG file. 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. 142 * Pass TRUE unless you are very sure of what you're doing.
143 */ 143 */
144 jpeg_start_compress(&cinfo, TRUE); 144 jpeg_start_compress(&cinfo, TRUE);
145 145
146 /* Step 5: while (scan lines remain to be written) */ 146 /* Step 5: while (scan lines remain to be written) */
147 /* jpeg_write_scanlines(...); */ 147 /* jpeg_write_scanlines(...); */
148 148
149 /* Here we use the library's state variable cinfo.next_scanline as the 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. 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 151 * To keep things simple, we pass one scanline per call; you can pass
152 * more if you wish, though. 152 * more if you wish, though.
153 */ 153 */
154 row_stride = image_width * 3;»/* JSAMPLEs per row in image_buffer */ 154 row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
155 155
156 while (cinfo.next_scanline < cinfo.image_height) { 156 while (cinfo.next_scanline < cinfo.image_height) {
157 /* jpeg_write_scanlines expects an array of pointers to scanlines. 157 /* jpeg_write_scanlines expects an array of pointers to scanlines.
158 * Here the array is only one element long, but you could pass 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. 159 * more than one scanline at a time if that's more convenient.
160 */ 160 */
161 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; 161 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
162 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); 162 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
163 } 163 }
164 164
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 * establish the return point. We want the replacement error_exit to do a 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 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 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 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.) 245 * were making a subclass of the regular error handler.)
246 * 246 *
247 * Here's the extended error handler struct: 247 * Here's the extended error handler struct:
248 */ 248 */
249 249
250 struct my_error_mgr { 250 struct my_error_mgr {
251 struct jpeg_error_mgr pub;» /* "public" fields */ 251 struct jpeg_error_mgr pub; /* "public" fields */
252 252
253 jmp_buf setjmp_buffer;» /* for return to caller */ 253 jmp_buf setjmp_buffer; /* for return to caller */
254 }; 254 };
255 255
256 typedef struct my_error_mgr * my_error_ptr; 256 typedef struct my_error_mgr *my_error_ptr;
257 257
258 /* 258 /*
259 * Here's the routine that will replace the standard error_exit method: 259 * Here's the routine that will replace the standard error_exit method:
260 */ 260 */
261 261
262 METHODDEF(void) 262 METHODDEF(void)
263 my_error_exit (j_common_ptr cinfo) 263 my_error_exit (j_common_ptr cinfo)
264 { 264 {
265 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ 265 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
266 my_error_ptr myerr = (my_error_ptr) cinfo->err; 266 my_error_ptr myerr = (my_error_ptr) cinfo->err;
267 267
268 /* Always display the message. */ 268 /* Always display the message. */
269 /* We could postpone this until after returning, if we chose. */ 269 /* We could postpone this until after returning, if we chose. */
270 (*cinfo->err->output_message) (cinfo); 270 (*cinfo->err->output_message) (cinfo);
271 271
272 /* Return control to the setjmp point */ 272 /* Return control to the setjmp point */
273 longjmp(myerr->setjmp_buffer, 1); 273 longjmp(myerr->setjmp_buffer, 1);
274 } 274 }
275 275
276 276
277 /* 277 /*
278 * Sample routine for JPEG decompression. We assume that the source file name 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. 279 * is passed in. We want to return 1 on success, 0 on error.
280 */ 280 */
281 281
282 282
283 GLOBAL(int) 283 GLOBAL(int)
284 read_JPEG_file (char * filename) 284 read_JPEG_file (char *filename)
285 { 285 {
286 /* This struct contains the JPEG decompression parameters and pointers to 286 /* This struct contains the JPEG decompression parameters and pointers to
287 * working space (which is allocated as needed by the JPEG library). 287 * working space (which is allocated as needed by the JPEG library).
288 */ 288 */
289 struct jpeg_decompress_struct cinfo; 289 struct jpeg_decompress_struct cinfo;
290 /* We use our private extension JPEG error handler. 290 /* We use our private extension JPEG error handler.
291 * Note that this struct must live as long as the main JPEG parameter 291 * Note that this struct must live as long as the main JPEG parameter
292 * struct, to avoid dangling-pointer problems. 292 * struct, to avoid dangling-pointer problems.
293 */ 293 */
294 struct my_error_mgr jerr; 294 struct my_error_mgr jerr;
295 /* More stuff */ 295 /* More stuff */
296 FILE * infile;» » /* source file */ 296 FILE *infile; /* source file */
297 JSAMPARRAY buffer;» » /* Output row buffer */ 297 JSAMPARRAY buffer; /* Output row buffer */
298 int row_stride;» » /* physical row width in output buffer */ 298 int row_stride; /* physical row width in output buffer */
299 299
300 /* In this example we want to open the input file before doing anything else, 300 /* In this example we want to open the input file before doing anything else,
301 * so that the setjmp() error recovery below can assume the file is open. 301 * so that the setjmp() error recovery below can assume the file is open.
302 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that 302 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
303 * requires it in order to read binary files. 303 * requires it in order to read binary files.
304 */ 304 */
305 305
306 if ((infile = fopen(filename, "rb")) == NULL) { 306 if ((infile = fopen(filename, "rb")) == NULL) {
307 fprintf(stderr, "can't open %s\n", filename); 307 fprintf(stderr, "can't open %s\n", filename);
308 return 0; 308 return 0;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 (void) jpeg_start_decompress(&cinfo); 349 (void) jpeg_start_decompress(&cinfo);
350 /* We can ignore the return value since suspension is not possible 350 /* We can ignore the return value since suspension is not possible
351 * with the stdio data source. 351 * with the stdio data source.
352 */ 352 */
353 353
354 /* We may need to do some setup of our own at this point before reading 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 355 * the data. After jpeg_start_decompress() we have the correct scaled
356 * output image dimensions available, as well as the output colormap 356 * output image dimensions available, as well as the output colormap
357 * if we asked for color quantization. 357 * if we asked for color quantization.
358 * In this example, we need to make an output work buffer of the right size. 358 * In this example, we need to make an output work buffer of the right size.
359 */ 359 */
360 /* JSAMPLEs per row in output buffer */ 360 /* JSAMPLEs per row in output buffer */
361 row_stride = cinfo.output_width * cinfo.output_components; 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 */ 362 /* Make a one-row-high sample array that will go away when done with image */
363 buffer = (*cinfo.mem->alloc_sarray) 363 buffer = (*cinfo.mem->alloc_sarray)
364 » » ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); 364 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
365 365
366 /* Step 6: while (scan lines remain to be read) */ 366 /* Step 6: while (scan lines remain to be read) */
367 /* jpeg_read_scanlines(...); */ 367 /* jpeg_read_scanlines(...); */
368 368
369 /* Here we use the library's state variable cinfo.output_scanline as the 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. 370 * loop counter, so that we don't have to keep track ourselves.
371 */ 371 */
372 while (cinfo.output_scanline < cinfo.output_height) { 372 while (cinfo.output_scanline < cinfo.output_height) {
373 /* jpeg_read_scanlines expects an array of pointers to scanlines. 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 374 * Here the array is only one element long, but you could ask for
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 * 424 *
425 * Scanlines are returned in the same order as they appear in the JPEG file, 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, 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 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. 428 * to invert the data. See wrbmp.c for an example.
429 * 429 *
430 * As with compression, some operating modes may require temporary files. 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 431 * On some systems you may need to set up a signal handler to ensure that
432 * temporary files are deleted if the program is interrupted. See libjpeg.txt. 432 * temporary files are deleted if the program is interrupted. See libjpeg.txt.
433 */ 433 */
OLDNEW
« no previous file with comments | « doxygen-extra.css ('k') | jaricom.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698