udprmon/jmemio.c
/* write_JPEG_memory: store JPEG compressed image into memory.
**
** Note that the "compressed image" has the entire JFIF formatted
** file structure, including header info.
**
** img_buf: (in) pointer to uncompressed source image data in RGB format
** buffer_size: (in) size of the source image buffer, in bytes
** width: (in) width of source image in pixels
** height: (in) height of source image in pixels
** jpeg_buffer: (in/out) pointer to compressed image data buffer
** jpeg_buffer_size: (in) maximum size of the compressed image data buffer
** quality: (in) a compression quality factor
** jpeg_comp_size: (out) size of the destination compressed image, in bytes
*/
store JPEG compressed image into memory.
Note that the "compressed image" has the entire JFIF formatted
file structure, including header info.


store JPEG compressed image into memory . Note that the " compressed image " has the entire JFIF formatted file structure including header info .
GLOBAL(void)
write_JPEG_memory (unsigned char *img_buf, int width, int height,
                   unsigned char *jpeg_buffer, unsigned long jpeg_buffer_size,
                   int quality, unsigned long *jpeg_comp_size, int input_components)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  JSAMPROW row_pointer[1];      
  int row_stride;               
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_memory_dest(&cinfo, jpeg_buffer, jpeg_buffer_size);
  cinfo.image_width = width;      
  cinfo.image_height = height;    
  cinfo.input_components = input_components;     
  cinfo.in_color_space = input_components == 1 ? JCS_GRAYSCALE : JCS_RGB; 
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE );
  jpeg_start_compress(&cinfo, TRUE);
  row_stride = width * input_components; 
  while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer[0] = & img_buf[cinfo.next_scanline * row_stride];
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }
  jpeg_finish_compress(&cinfo);
  {
    mem_dest_ptr dest = (mem_dest_ptr) cinfo.dest;
    *jpeg_comp_size = dest->buffer_size - dest->pub.free_in_buffer;
  }
  jpeg_destroy_compress(&cinfo);
}
GLOBAL ( void ) write_JPEG_memory ( unsigned char * img_buf , int width , int height , unsigned char * jpeg_buffer , unsigned long jpeg_buffer_size , int quality , unsigned long * jpeg_comp_size , int input_components ) { struct jpeg_compress_struct cinfo ; struct jpeg_error_mgr jerr ; JSAMPROW row_pointer [ 1 ] ; int row_stride ; cinfo . err = jpeg_std_error ( & jerr ) ; jpeg_create_compress ( & cinfo ) ; jpeg_memory_dest ( & cinfo , jpeg_buffer , jpeg_buffer_size ) ; cinfo . image_width = width ; cinfo . image_height = height ; cinfo . input_components = input_components ; cinfo . in_color_space = input_components == 1 ? JCS_GRAYSCALE : JCS_RGB ; jpeg_set_defaults ( & cinfo ) ; jpeg_set_quality ( & cinfo , quality , TRUE ) ; jpeg_start_compress ( & cinfo , TRUE ) ; row_stride = width * input_components ; while ( cinfo . next_scanline < cinfo . image_height ) { row_pointer [ 0 ] = & img_buf [ cinfo . next_scanline * row_stride ] ; ( void ) jpeg_write_scanlines ( & cinfo , row_pointer , 1 ) ; } jpeg_finish_compress ( & cinfo ) ; { mem_dest_ptr dest = ( mem_dest_ptr ) cinfo . dest ; * jpeg_comp_size = dest -> buffer_size - dest -> pub . free_in_buffer ; } jpeg_destroy_compress ( & cinfo ) ; }
write_JPEG_memory: store JPEG compressed image into memory.
write_JPEG_memory : store JPEG compressed image into memory .
/* jpeg_compress_struct contains the JPEG compression parameters
  ** and pointers to working space (which is allocated as needed by
  ** the JPEG library).  It is possible to have several such
  ** structures, representing multiple compression/decompression
  ** processes, in existence at once.  We refer to any one struct
  ** (and its associated working data) as a "JPEG object".
  */ /* jpeg_error_mgr represents a JPEG error handler.  It is declared
  ** separately because applications often want to supply a
  ** specialized error handler (see the second half of this file for
  ** an example).  But here we just take the easy way out and use the
  ** standard error handler, which will print a message on stderr and
  ** call exit() if compression fails.  Note that this struct must
  ** live as long as the main JPEG parameter struct, to avoid
  ** dangling-pointer problems.
  */ /* More stuff */ /* pointer to JSAMPLE row[s] */ /* physical row width in image buffer */ /* Step 1: allocate and initialize JPEG compression object */ /* We have to set up the error handler first, in case the initialization
   * step fails.  (Unlikely, but it could happen if you are out of memory.)
   * This routine fills in the contents of struct jerr, and returns jerr's
   * address which we place into the link field in cinfo.
   */ /* Now we can initialize the JPEG compression object. */ /* (Note: steps 2 and 3 can be done in either order.) */ /* Step 2: specify data destination */ /* Step 3: set parameters for compression */ /* First we supply a description of the input image.
   * Four fields of the cinfo struct must be filled in:
   */ /* image width in pixels */ /* image height in pixels */ /* Assume that input_components is the same as BYTES_PER_SAMPLE */ /* # of color components per pixel */ /* colorspace of input image */ /* Now use the library's routine to set default compression parameters.
   * (You must set at least cinfo.in_color_space before calling this,
   * since the defaults depend on the source color space.)
   */ /* Now you can set any non-default parameters you wish to.
   * Here we just illustrate the use of quality (quantization table) scaling:
   */ /* limit to baseline-JPEG values */ /* Step 4: Start compressor */ /* TRUE ensures that we will write a complete interchange-JPEG file.
   * Pass TRUE unless you are very sure of what you're doing.
   */ /* Step 5: while (scan lines remain to be written) */ /*           jpeg_write_scanlines(...); */ /* Here we use the library's state variable cinfo.next_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   * To keep things simple, we pass one scanline per call; you can pass
   * more if you wish, though.
   */ /* JSAMPLEs per row in image_buffer */ /* jpeg_write_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could pass
     * more than one scanline at a time if that's more convenient.
     */ /* Step 6: Finish compression */ /* Compute the size of the compressed image.
  **
  ** This must be done after the image is done compressing, (i.e.
  ** after jpeg_finish_compress) but before jpeg_destroy_compress,
  ** because jpeg_destroy_compress frees the memory containing the
  ** information we want from the mem_dest_ptr within the cinfo
  ** structure.
  */ /* mem_dest_ptr is a pointer to the memory destination object.
    ** From it, the total size of the compressed data can be
    ** determined.
    */ /* Step 7: release JPEG compression object */ /* This is an important step since it will release a good deal of memory. */ /* Now it is expected that some external routine will do something
  ** useful with the compressed data, such as send it over a network, or
  ** write it to a file (although there is a stdio destiation module that
  ** writes the data to a file in a more elegant way.)
  **
  ** Also, it is expected that some external routine will free the image
  ** buffer once it's done being used.
  */