IJPEG.c
资源名称:ilib [点击查看]
上传用户:changbiao
上传日期:2007-01-13
资源大小:141k
文件大小:4k
源码类别:

图片显示

开发平台:

C/C++

  1. /*
  2.  * IJPEGC.c
  3.  *
  4.  * Image library
  5.  *
  6.  * Description:
  7.  * JPEG reading and writing
  8.  * Most of this code was created from the example.c file provided
  9.  * in the JPEG distribution.
  10.  *
  11.  * Get the JPEG distribution at:
  12.  * ftp://ftp.uu.net/graphics/jpeg/
  13.  *
  14.  *
  15.  * History:
  16.  * 22-Jul-99 Craig Knudsen cknudsen@radix.net
  17.  * Created
  18.  *
  19.  ****************************************************************************/
  20. #ifdef HAVE_JPEGLIB
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <memory.h>
  25. #include <jpeglib.h>
  26. #include <jerror.h>
  27. #include "Ilib.h"
  28. #include "IlibP.h"
  29. #define DEFAULT_JPEG_QUALITY 75
  30. IError _IWriteJPEG ( fp, image, options )
  31. FILE *fp;
  32. IImageP *image;
  33. IOptions options;
  34. {
  35.   struct jpeg_compress_struct cinfo;
  36.   struct jpeg_error_mgr jerr;
  37.   JSAMPROW row_pointer[1];
  38.   int loop;
  39.   /* Step 1: allocate and initialize JPEG compression object */
  40.   cinfo.err = jpeg_std_error ( &jerr );
  41.   jpeg_create_compress ( &cinfo );
  42.   /* Step 2: specify data destination (eg, a file) */
  43.   jpeg_stdio_dest ( &cinfo, fp );
  44.   /* Step 3: set parameters for compression */
  45.   cinfo.image_width = image->width;
  46.   cinfo.image_height = image->height;
  47.   if ( image->greyscale ) {
  48.     cinfo.input_components = 1; /* # of color components per pixel */
  49.     cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */
  50.   } else {
  51.     cinfo.input_components = 3; /* # of color components per pixel */
  52.     cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
  53.   }
  54.   jpeg_set_defaults ( &cinfo );
  55.   jpeg_set_quality ( &cinfo, DEFAULT_JPEG_QUALITY, TRUE );
  56.   /* Step 4: Start compressor */
  57.   jpeg_start_compress ( &cinfo, TRUE );
  58.   /* Step 5: while (scan lines remain to be written) */
  59.   for ( loop = 0; loop < image->height; loop++ ) {
  60.     row_pointer[0] = &image->data[loop * image->width * cinfo.input_components];
  61.     (void) jpeg_write_scanlines ( &cinfo, row_pointer, 1 );
  62.   }
  63.   /* Step 6: Finish compression */
  64.   jpeg_finish_compress ( &cinfo );
  65.   /* Step 7: release JPEG compression object */
  66.   jpeg_destroy_compress ( &cinfo );
  67.   return ( INoError );
  68. }
  69. IError _IReadJPEG ( fp, options, image_return )
  70. FILE *fp;
  71. IOptions options;
  72. IImageP **image_return;
  73. {
  74.   IImageP *image;
  75.   struct jpeg_decompress_struct cinfo;
  76.   struct jpeg_error_mgr jerr;
  77.   JSAMPROW row_pointer[1];
  78.   int loop;
  79.   
  80.   /* We set up the normal JPEG error routines */
  81.   cinfo.err = jpeg_std_error ( &jerr );
  82.   /* Now we can initialize the JPEG decompression object. */
  83.   jpeg_create_decompress ( &cinfo );
  84.   /* Step 2: specify data source (eg, a file) */
  85.   jpeg_stdio_src ( &cinfo, fp );
  86.   /* Step 3: read file parameters with jpeg_read_header() */
  87.   (void) jpeg_read_header ( &cinfo, TRUE );
  88.   /* Step 5: Start decompressor */
  89.   (void) jpeg_start_decompress ( &cinfo );
  90.   /* allocate Ilib image as either grayscale or rgb */
  91.   image = (IImageP *) ICreateImage ( cinfo.output_width, cinfo.output_height,
  92.     cinfo.output_components == 1 ? IOPTION_GREYSCALE : IOPTION_NONE );
  93.   image->comments = (char *) malloc ( strlen ( IDEFAULT_COMMENT ) + 1 );
  94.   strcpy ( image->comments, IDEFAULT_COMMENT );
  95.   /* Step 6: while (scan lines remain to be read) */
  96.   for ( loop = 0; loop < cinfo.output_height; loop++ ) {
  97.     row_pointer[0] = &image->data[loop * cinfo.output_width *
  98.       cinfo.output_components];
  99.     (void) jpeg_read_scanlines ( &cinfo, row_pointer, 1 );
  100.   }
  101.   /* Step 7: Finish decompression */
  102.   (void) jpeg_finish_decompress ( &cinfo );
  103.   /* Step 8: Release JPEG decompression object */
  104.   jpeg_destroy_decompress ( &cinfo );
  105.   *image_return = image;
  106.   return ( INoError );
  107. }
  108. #endif