jpegmemcd.c
Go to the documentation of this file.
00001 /* jpegmemcd.c
00002 *   (c) 1997, Toshihiro Matsui, Electrotechnical Laboratory
00003 *   JPEG compression/decompression for in-core image/jpeg_data.
00004 *
00005 *   int JPEG_compress(RGBimage, width, height, JPEGimage,size, quality); 
00006 *   int JPEG_decompress(JPEGimage, jpeg_size,
00007 *                      RGBimage, result_size, *width, *height)
00008 */
00009 
00010 
00011 #include <stdio.h>
00012 #include "jpeglib.h"
00013 #include <setjmp.h>
00014 
00015 
00016 int JPEG_compress(JSAMPLE *image_buffer, long width, long height,
00017                   unsigned char *jpeg_image_buffer, long size, int quality)
00018 {
00019   struct jpeg_compress_struct cinfo;
00020   struct jpeg_error_mgr jerr;
00021   JSAMPROW row_pointer[1];      /* pointer to JSAMPLE row[s] */
00022   int row_stride;               /* physical row width in image buffer */
00023   long data_count=size;
00024 
00025   cinfo.err = jpeg_std_error(&jerr);
00026 
00027   jpeg_create_compress(&cinfo);
00028 
00029   jpeg_memio_dest(&cinfo, jpeg_image_buffer, &data_count);
00030 
00031 /*  fprintf(stderr, 
00032         "jpeg compress: w=%d h=%d src=%x dest=%x size=%d quality=%d\n", width, height,
00033         image_buffer, jpeg_image_buffer, size, quality); */
00034 
00035   cinfo.image_width = width;    /* image width and height, in pixels */
00036   cinfo.image_height = height;
00037   cinfo.input_components = 3;           /* # of color components per pixel */
00038   cinfo.in_color_space = JCS_RGB;       /* colorspace of input image */
00039 
00040   jpeg_set_defaults(&cinfo);
00041   jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
00042 
00043   jpeg_start_compress(&cinfo, TRUE);
00044 
00045   row_stride = width * 3;       /* JSAMPLEs per row in image_buffer */
00046 
00047   while (cinfo.next_scanline < cinfo.image_height) {
00048     row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00049     (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00050   }
00051 
00052   jpeg_finish_compress(&cinfo);
00053   jpeg_destroy_compress(&cinfo);
00054   return(data_count);
00055 }
00056 
00057 
00058 /****************************************************************/
00059 struct my_error_mgr {
00060   struct jpeg_error_mgr pub;    /* "public" fields */
00061 
00062   jmp_buf setjmp_buffer;        /* for return to caller */
00063 };
00064 
00065 typedef struct my_error_mgr * my_error_ptr;
00066 
00067 
00068 static void my_error_exit (j_common_ptr cinfo)
00069 {
00070   my_error_ptr myerr = (my_error_ptr) cinfo->err;
00071 
00072   (*cinfo->err->output_message) (cinfo);
00073 
00074   longjmp(myerr->setjmp_buffer, 1);
00075 }
00076 
00077 extern int JPEG_header (JOCTET *jpeg_image, long jpeg_size,
00078                         long *width, long *height , long *components)
00079 { struct jpeg_decompress_struct cinfo;
00080   struct my_error_mgr jerr;
00081   long total_size;
00082 
00083   cinfo.err = jpeg_std_error(&jerr.pub);
00084   jerr.pub.error_exit = my_error_exit;
00085 
00086   if (setjmp(jerr.setjmp_buffer)) {
00087     jpeg_destroy_decompress(&cinfo);
00088     return 0;  }
00089 
00090   jpeg_create_decompress(&cinfo);
00091 
00092   jpeg_memio_src(&cinfo, jpeg_image, jpeg_size);
00093 
00094   (void) jpeg_read_header(&cinfo, TRUE);
00095 
00096   jpeg_calc_output_dimensions(&cinfo);
00097 
00098   *width= cinfo.output_width;
00099   *height= cinfo.output_height;
00100   *components= cinfo.output_components;
00101   total_size = cinfo.output_width * cinfo.output_height * 
00102                 cinfo.output_components;
00103 
00104   jpeg_destroy_decompress(&cinfo);
00105   return(total_size);
00106 }
00107 
00108 
00109 extern int JPEG_decompress (JOCTET *jpeg_image, long jpeg_size,
00110                          JOCTET *result_image,
00111                          long   *width, long *height)
00112 {
00113   JOCTET *rimage;
00114   struct jpeg_decompress_struct cinfo;
00115   struct my_error_mgr jerr;
00116   JSAMPLE *jsampbuf;
00117   int row_stride;               /* physical row width in output buffer */
00118   long total_size;
00119 
00120   cinfo.err = jpeg_std_error(&jerr.pub);
00121   jerr.pub.error_exit = my_error_exit;
00122 
00123   if (setjmp(jerr.setjmp_buffer)) {
00124     jpeg_destroy_decompress(&cinfo);
00125     return 0;  }
00126 
00127   jpeg_create_decompress(&cinfo);
00128 
00129   jpeg_memio_src(&cinfo, jpeg_image, jpeg_size);
00130 
00131   (void) jpeg_read_header(&cinfo, TRUE);
00132 
00133   jpeg_calc_output_dimensions(&cinfo);
00134 
00135   *width= cinfo.output_width;
00136   *height= cinfo.output_height;
00137 
00138   row_stride = cinfo.output_width * cinfo.output_components;
00139   total_size = row_stride * cinfo.output_height;
00140 
00141   /*  printf("read_JPEG: width=%d height=%d\n", *width, *height); */
00142   /*if (total_size > result_size) {
00143     fprintf(stderr, "jpeg_decompress: result too big. %d > %d\n",
00144                 total_size, result_size);
00145     jpeg_destroy_decompress(&cinfo);
00146     return(FALSE);} */
00147 
00148   /* start decompression */
00149   (void) jpeg_start_decompress(&cinfo);
00150 
00151   while (cinfo.output_scanline < cinfo.output_height) {
00152     jsampbuf= &result_image[cinfo.output_scanline * row_stride]; 
00153     (void) jpeg_read_scanlines(&cinfo, &jsampbuf, 1);
00154   }
00155 
00156   (void) jpeg_finish_decompress(&cinfo); 
00157 
00158   jpeg_destroy_decompress(&cinfo);
00159   return(total_size);
00160 }
00161 
00162 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Sep 3 2015 10:36:20