ReactOS  0.4.13-dev-101-g0ca4b50
example.c File Reference
#include <stdio.h>
#include "jpeglib.h"
#include <setjmp.h>
Include dependency graph for example.c:

Go to the source code of this file.

Classes

struct  my_error_mgr
 

Typedefs

typedef struct my_error_mgrmy_error_ptr
 

Functions

 write_JPEG_file (char *filename, int quality)
 
 my_error_exit (j_common_ptr cinfo)
 
 read_JPEG_file (char *filename)
 

Variables

JSAMPLEimage_buffer
 
int image_height
 
int image_width
 

Typedef Documentation

◆ my_error_ptr

Definition at line 256 of file example.c.

Function Documentation

◆ my_error_exit()

my_error_exit ( j_common_ptr  cinfo)

Definition at line 263 of file example.c.

264 {
265  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
266  my_error_ptr myerr = (my_error_ptr) cinfo->err;
267 
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);
274 }
jmp_buf setjmp_buffer
Definition: example.c:253
int longjmp(jmp_buf buf, int retval)
struct my_error_mgr * my_error_ptr
Definition: example.c:256

Referenced by read_JPEG_file().

◆ read_JPEG_file()

read_JPEG_file ( char filename)

Definition at line 284 of file example.c.

285 {
286  /* This struct contains the JPEG decompression parameters and pointers to
287  * working space (which is allocated as needed by the JPEG library).
288  */
289  struct jpeg_decompress_struct cinfo;
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  */
294  struct my_error_mgr jerr;
295  /* More stuff */
296  FILE * infile; /* source file */
297  JSAMPARRAY buffer; /* Output row buffer */
298  int row_stride; /* physical row width in output buffer */
299 
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.
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 
306  if ((infile = fopen(filename, "rb")) == NULL) {
307  fprintf(stderr, "can't open %s\n", filename);
308  return 0;
309  }
310 
311  /* Step 1: allocate and initialize JPEG decompression object */
312 
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)) {
318  /* If we get here, the JPEG code has signaled an error.
319  * We need to clean up the JPEG object, close the input file, and return.
320  */
321  jpeg_destroy_decompress(&cinfo);
322  fclose(infile);
323  return 0;
324  }
325  /* Now we can initialize the JPEG decompression object. */
326  jpeg_create_decompress(&cinfo);
327 
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.
338  * See libjpeg.txt for more info.
339  */
340 
341  /* Step 4: set parameters for decompression */
342 
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.
345  */
346 
347  /* Step 5: Start decompressor */
348 
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  */
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.
359  */
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)
364  ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
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) {
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  */
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.
387  */
388 
389  /* Step 8: Release JPEG decompression object */
390 
391  /* This is an important step since it will release a good deal of memory. */
392  jpeg_destroy_decompress(&cinfo);
393 
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...)
398  */
399  fclose(infile);
400 
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).
403  */
404 
405  /* And we're done! */
406  return 1;
407 }
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
Definition: jdapimin.c:245
#define TRUE
Definition: types.h:120
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
my_error_exit(j_common_ptr cinfo)
Definition: example.c:263
static FILE * infile
Definition: rdjpgcom.c:65
GLuint buffer
Definition: glext.h:5915
const char * filename
Definition: ioapi.h:135
#define jpeg_create_decompress(cinfo)
Definition: jpeglib.h:961
#define JPOOL_IMAGE
Definition: jpeglib.h:808
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
smooth NULL
Definition: ftsmooth.c:416
jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile)
Definition: jdatasrc.c:210
jpeg_finish_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.c:373
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
JSAMPROW * JSAMPARRAY
Definition: jpeglib.h:76
jpeg_destroy_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.c:92
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
Definition: jdapistd.c:153
#define setjmp
Definition: setjmp.h:183
jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.c:232
FILE * stderr

◆ write_JPEG_file()

write_JPEG_file ( char filename,
int  quality 
)

Definition at line 72 of file example.c.

73 {
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".
79  */
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.
86  * Note that this struct must live as long as the main JPEG parameter
87  * struct, to avoid dangling-pointer problems.
88  */
89  struct jpeg_error_mgr jerr;
90  /* More stuff */
91  FILE * outfile; /* target file */
92  JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
93  int row_stride; /* physical row width in image buffer */
94 
95  /* Step 1: allocate and initialize JPEG compression object */
96 
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.
101  */
102  cinfo.err = jpeg_std_error(&jerr);
103  /* Now we can initialize the JPEG compression object. */
104  jpeg_create_compress(&cinfo);
105 
106  /* Step 2: specify data destination (eg, a file) */
107  /* Note: steps 2 and 3 can be done in either order. */
108 
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.
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  */
114  if ((outfile = fopen(filename, "wb")) == NULL) {
115  fprintf(stderr, "can't open %s\n", filename);
116  exit(1);
117  }
118  jpeg_stdio_dest(&cinfo, outfile);
119 
120  /* Step 3: set parameters for compression */
121 
122  /* First we supply a description of the input image.
123  * Four fields of the cinfo struct must be filled in:
124  */
125  cinfo.image_width = image_width; /* image width and height, in pixels */
126  cinfo.image_height = image_height;
127  cinfo.input_components = 3; /* # of color components per pixel */
128  cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
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  */
154  row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
155 
156  while (cinfo.next_scanline < cinfo.image_height) {
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  */
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! */
177 }
#define TRUE
Definition: types.h:120
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
jpeg_finish_compress(j_compress_ptr cinfo)
Definition: jcapimin.c:155
JSAMPLE FAR * JSAMPROW
Definition: jpeglib.h:75
jpeg_destroy_compress(j_compress_ptr cinfo)
Definition: jcapimin.c:96
static FILE * outfile
Definition: wrjpgcom.c:81
const char * filename
Definition: ioapi.h:135
jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
Definition: jdatadst.c:196
int image_height
jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
Definition: jcapistd.c:39
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
int image_width
JSAMPLE * image_buffer
smooth NULL
Definition: ftsmooth.c:416
jpeg_write_scanlines(j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines)
Definition: jcapistd.c:78
int quality
Definition: jpeglib.h:991
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
#define jpeg_create_compress(cinfo)
Definition: jpeglib.h:958
jpeg_set_defaults(j_compress_ptr cinfo)
Definition: jcparam.c:285
jpeg_std_error(struct jpeg_error_mgr *err)
Definition: jerror.c:232
FILE * stderr
void exit(int exitcode)
Definition: _exit.c:33
jpeg_set_quality(j_compress_ptr cinfo, int quality, boolean force_baseline)
Definition: jcparam.c:149

Variable Documentation

◆ image_buffer

JSAMPLE* image_buffer

Referenced by write_JPEG_file().

◆ image_height

int image_height

Referenced by process_SOFn(), and write_JPEG_file().

◆ image_width