jpegmemcd.c
Go to the documentation of this file.
1 /* jpegmemcd.c
2 * (c) 1997, Toshihiro Matsui, Electrotechnical Laboratory
3 * JPEG compression/decompression for in-core image/jpeg_data.
4 *
5 * int JPEG_compress(RGBimage, width, height, JPEGimage,size, quality);
6 * int JPEG_decompress(JPEGimage, jpeg_size,
7 * RGBimage, result_size, *width, *height)
8 */
9 
10 
11 #include <stdio.h>
12 #include "jpeglib.h"
13 #include <setjmp.h>
14 
15 
16 int JPEG_compress(JSAMPLE *image_buffer, long width, long height,
17  unsigned char *jpeg_image_buffer, long size, int quality)
18 {
19  struct jpeg_compress_struct cinfo;
20  struct jpeg_error_mgr jerr;
21  JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
22  int row_stride; /* physical row width in image buffer */
23  long data_count=size;
24 
25  cinfo.err = jpeg_std_error(&jerr);
26 
27  jpeg_create_compress(&cinfo);
28 
29  jpeg_memio_dest(&cinfo, jpeg_image_buffer, &data_count);
30 
31 /* fprintf(stderr,
32  "jpeg compress: w=%d h=%d src=%x dest=%x size=%d quality=%d\n", width, height,
33  image_buffer, jpeg_image_buffer, size, quality); */
34 
35  cinfo.image_width = width; /* image width and height, in pixels */
36  cinfo.image_height = height;
37  cinfo.input_components = 3; /* # of color components per pixel */
38  cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
39 
40  jpeg_set_defaults(&cinfo);
41  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
42 
43  jpeg_start_compress(&cinfo, TRUE);
44 
45  row_stride = width * 3; /* JSAMPLEs per row in image_buffer */
46 
47  while (cinfo.next_scanline < cinfo.image_height) {
48  row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
49  (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
50  }
51 
52  jpeg_finish_compress(&cinfo);
53  jpeg_destroy_compress(&cinfo);
54  return(data_count);
55 }
56 
57 
58 /****************************************************************/
59 struct my_error_mgr {
60  struct jpeg_error_mgr pub; /* "public" fields */
61 
62  jmp_buf setjmp_buffer; /* for return to caller */
63 };
64 
65 typedef struct my_error_mgr * my_error_ptr;
66 
67 
68 static void my_error_exit (j_common_ptr cinfo)
69 {
70  my_error_ptr myerr = (my_error_ptr) cinfo->err;
71 
72  (*cinfo->err->output_message) (cinfo);
73 
74  longjmp(myerr->setjmp_buffer, 1);
75 }
76 
77 extern int JPEG_header (JOCTET *jpeg_image, long jpeg_size,
78  long *width, long *height , long *components)
79 { struct jpeg_decompress_struct cinfo;
80  struct my_error_mgr jerr;
81  long total_size;
82 
83  cinfo.err = jpeg_std_error(&jerr.pub);
84  jerr.pub.error_exit = my_error_exit;
85 
86  if (setjmp(jerr.setjmp_buffer)) {
87  jpeg_destroy_decompress(&cinfo);
88  return 0; }
89 
90  jpeg_create_decompress(&cinfo);
91 
92  jpeg_memio_src(&cinfo, jpeg_image, jpeg_size);
93 
94  (void) jpeg_read_header(&cinfo, TRUE);
95 
96  jpeg_calc_output_dimensions(&cinfo);
97 
98  *width= cinfo.output_width;
99  *height= cinfo.output_height;
100  *components= cinfo.output_components;
101  total_size = cinfo.output_width * cinfo.output_height *
102  cinfo.output_components;
103 
104  jpeg_destroy_decompress(&cinfo);
105  return(total_size);
106 }
107 
108 
109 extern int JPEG_decompress (JOCTET *jpeg_image, long jpeg_size,
110  JOCTET *result_image,
111  long *width, long *height)
112 {
113  JOCTET *rimage;
114  struct jpeg_decompress_struct cinfo;
115  struct my_error_mgr jerr;
116  JSAMPLE *jsampbuf;
117  int row_stride; /* physical row width in output buffer */
118  long total_size;
119 
120  cinfo.err = jpeg_std_error(&jerr.pub);
121  jerr.pub.error_exit = my_error_exit;
122 
123  if (setjmp(jerr.setjmp_buffer)) {
124  jpeg_destroy_decompress(&cinfo);
125  return 0; }
126 
127  jpeg_create_decompress(&cinfo);
128 
129  jpeg_memio_src(&cinfo, jpeg_image, jpeg_size);
130 
131  (void) jpeg_read_header(&cinfo, TRUE);
132 
133  jpeg_calc_output_dimensions(&cinfo);
134 
135  *width= cinfo.output_width;
136  *height= cinfo.output_height;
137 
138  row_stride = cinfo.output_width * cinfo.output_components;
139  total_size = row_stride * cinfo.output_height;
140 
141  /* printf("read_JPEG: width=%d height=%d\n", *width, *height); */
142  /*if (total_size > result_size) {
143  fprintf(stderr, "jpeg_decompress: result too big. %d > %d\n",
144  total_size, result_size);
145  jpeg_destroy_decompress(&cinfo);
146  return(FALSE);} */
147 
148  /* start decompression */
149  (void) jpeg_start_decompress(&cinfo);
150 
151  while (cinfo.output_scanline < cinfo.output_height) {
152  jsampbuf= &result_image[cinfo.output_scanline * row_stride];
153  (void) jpeg_read_scanlines(&cinfo, &jsampbuf, 1);
154  }
155 
156  (void) jpeg_finish_decompress(&cinfo);
157 
158  jpeg_destroy_decompress(&cinfo);
159  return(total_size);
160 }
161 
162 
static void my_error_exit(j_common_ptr cinfo)
Definition: jpegmemcd.c:68
int JPEG_compress(JSAMPLE *image_buffer, long width, long height, unsigned char *jpeg_image_buffer, long size, int quality)
Definition: jpegmemcd.c:16
jpeg_memio_dest(j_compress_ptr cinfo, JOCTET *jpegimgbuf, long *size)
Definition: jmemdst.c:94
int JPEG_header(JOCTET *jpeg_image, long jpeg_size, long *width, long *height, long *components)
Definition: jpegmemcd.c:77
struct jpeg_error_mgr pub
Definition: jpegmemcd.c:60
jpeg_memio_src(j_decompress_ptr cinfo, JOCTET *buf, long size)
Definition: jmemsrc.c:151
jmp_buf setjmp_buffer
Definition: jpegmemcd.c:62
struct my_error_mgr * my_error_ptr
Definition: jpegmemcd.c:65
#define TRUE
Definition: arith.h:19
int JPEG_decompress(JOCTET *jpeg_image, long jpeg_size, JOCTET *result_image, long *width, long *height)
Definition: jpegmemcd.c:109


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 6 2019 20:00:44