jpeg.c

Go to the documentation of this file.
00001 /* Simple JPEG load/save interface,
00002  * based on examplecode in IJG's JPEG-library
00003  *
00004  * Author Fredrik Orderud, 2005
00005  */
00006 
00007 /*
00008  * example.c
00009  *
00010  * This file illustrates how to use the IJG code as a subroutine library
00011  * to read or write JPEG image files.  You should look at this code in
00012  * conjunction with the documentation file libjpeg.doc.
00013  *
00014  * This code will not do anything useful as-is, but it may be helpful as a
00015  * skeleton for constructing routines that call the JPEG library.
00016  *
00017  * We present these routines in the same coding style used in the JPEG code
00018  * (ANSI function definitions, etc); but you are of course free to code your
00019  * routines in a different style if you prefer.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <memory.h>
00025 
00026 #include "jpeg.h"
00027 
00028 /*
00029  * Include file for users of JPEG library.
00030  * You will need to have included system headers that define at least
00031  * the typedefs FILE and size_t before you can include jpeglib.h.
00032  * (stdio.h is sufficient on ANSI-conforming systems.)
00033  * You may also wish to include "jerror.h".
00034  */
00035 
00036 #include <setjmp.h>
00037 
00038 #ifdef __cplusplus
00039  extern "C" {  // stupid JPEG library
00040 #endif
00041 #include <jpeglib.h>
00042 #ifdef __cplusplus
00043  }
00044 #endif
00045 
00046 
00047 
00048 
00049 
00050 namespace HAIR
00051 {
00052 /****************************************************************************
00053 //*!TYPE........: Function
00054  * NAME........: write_JPEG_file
00055  * CREATED.....: 24-Jan-2006
00056  * MODIFIED....: 24-Jan-2005
00057  *****************************************************************************/
00058 //GLOBAL(void) write_JPEG_file (char * filename, int quality, imageRGB img) 
00059 bool write_JPEG_file (char * filename, int quality, imageRGB img) 
00060 {
00061 
00062         int image_width      = img.width;
00063         int image_height     = img.height;
00064         unsigned char* image = img.data;
00065    int ltrysave         = 0;
00066    bool booltrysave     = true;
00067    bool boolreturn      = false;
00068 
00069 
00070         JSAMPLE * image_buffer = (JSAMPLE *) image;
00071    struct jpeg_compress_struct cinfo;
00072    struct jpeg_error_mgr jerr;
00073    /* More stuff */
00074    FILE * outfile;               /* target file */
00075    JSAMPROW row_pointer[1];      /* pointer to JSAMPLE row[s] */
00076    int row_stride;               /* physical row width in image buffer */
00077 
00078    cinfo.err = jpeg_std_error(&jerr);
00079    jpeg_create_compress(&cinfo);
00080 
00081    while(booltrysave)
00082    {  
00083       ltrysave++;
00084       if ((outfile = fopen(filename, "wb")) == NULL) 
00085       {
00086          fprintf(stderr, " > can't open %s %d/4\n", filename,ltrysave);
00087       }
00088       else
00089       booltrysave = false;
00090 
00091       if(ltrysave == 4)
00092       return false;
00093    }
00094 
00095    jpeg_stdio_dest(&cinfo, outfile);
00096 
00097    cinfo.image_width = image_width;      /* image width and height, in pixels */
00098    cinfo.image_height = image_height;
00099    cinfo.input_components = 3;           /* # of color components per pixel */
00100    cinfo.in_color_space = JCS_RGB;       /* colorspace of input image */
00101    jpeg_set_defaults(&cinfo);
00102    jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
00103 
00104    jpeg_start_compress(&cinfo, TRUE);
00105 
00106    row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
00107 
00108    while (cinfo.next_scanline < cinfo.image_height) 
00109    {
00110       row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00111       (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00112    }
00113 
00114    jpeg_finish_compress(&cinfo);
00115    fclose(outfile);
00116    jpeg_destroy_compress(&cinfo);
00117  
00118    return true;
00119 
00120 }
00121 
00122 
00123 struct my_error_mgr 
00124 {
00125   struct jpeg_error_mgr pub;    /* "public" fields */
00126   jmp_buf setjmp_buffer;        /* for return to caller */
00127 };
00128 
00129 typedef struct my_error_mgr * my_error_ptr;
00130 
00131 METHODDEF(void) my_error_exit (j_common_ptr cinfo)
00132 {
00133   my_error_ptr myerr = (my_error_ptr) cinfo->err;
00134 
00135   (*cinfo->err->output_message) (cinfo);
00136 
00137   longjmp(myerr->setjmp_buffer, 1);
00138 }
00139 
00140 /****************************************************************************
00141 //*!TYPE........: Function
00142  * NAME........: read_JPEG_file
00143  * CREATED.....: 24-Jan-2006
00144  * MODIFIED....: 24-Jan-2005
00145  *****************************************************************************/
00146 GLOBAL(imageRGB) read_JPEG_file (char * filename)
00147 {
00148    struct jpeg_decompress_struct cinfo;
00149    struct my_error_mgr jerr;
00150    FILE * infile;                /* source file */
00151    JSAMPARRAY buffer;            /* Output row buffer */
00152    int row_stride;               /* physical row width in output buffer */
00153 
00154    if ((infile = fopen(filename, "rb")) == NULL) 
00155    {
00156       fprintf(stderr, "can't open %s\n", filename);
00157       imageRGB zero = {0,0,0};
00158       return zero;
00159    }
00160 
00161    cinfo.err = jpeg_std_error(&jerr.pub);
00162    jerr.pub.error_exit = my_error_exit;
00163    if (setjmp(jerr.setjmp_buffer)) 
00164    {
00165       jpeg_destroy_decompress(&cinfo);
00166       fclose(infile);
00167       imageRGB zero = {0,0,0};
00168       return zero;
00169   }
00170 
00171   jpeg_create_decompress(&cinfo);
00172   jpeg_stdio_src(&cinfo, infile);
00173   (void) jpeg_read_header(&cinfo, TRUE);
00174   (void) jpeg_start_decompress(&cinfo);
00175   row_stride = cinfo.output_width * cinfo.output_components;
00176   buffer = (*cinfo.mem->alloc_sarray)
00177                 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
00178         imageRGB img;
00179         img.width  = cinfo.output_width;
00180         img.height = cinfo.output_height;
00181 
00182         img.data = (unsigned char*)malloc(cinfo.output_components * img.width * img.height);
00183         unsigned char* ptr = img.data;
00184    while (cinfo.output_scanline < cinfo.output_height) 
00185    {
00186       (void) jpeg_read_scanlines(&cinfo, buffer, 1);
00187       memcpy(ptr, buffer[0], row_stride); ptr += row_stride;
00188    }
00189 
00190    (void) jpeg_finish_decompress(&cinfo);
00191    jpeg_destroy_decompress(&cinfo);
00192 
00193    fclose(infile);
00194 
00195    return img;
00196 }
00197 
00198 }
00199 
00200 

Generated on Tue Apr 3 11:59:07 2007 for NETi TECNOLOGIA - libHAIRCAPTURE by  doxygen 1.5.0-1