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


euslisp
Author(s): Toshihiro Matsui
autogenerated on Mon Feb 28 2022 22:18:27