dibijg.cpp
上传用户:lbr_007
上传日期:2019-05-31
资源大小:282k
文件大小:5k
- // demjpg.cpp
- //
- #include "stdafx.h"
- extern "C" {
- #include <jpeglib.h>
- }
- #include <dibsect.h>
- #include <setjmp.h>
- //extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
- //extern int image_height; /* Number of rows in image */
- //extern int image_width; /* Number of columns in image */
- struct my_error_mgr {
- struct jpeg_error_mgr pub; /* "public" fields */
- jmp_buf setjmp_buffer; /* for return to caller */
- };
- typedef struct my_error_mgr * my_error_ptr;
- /*
- * Here's the routine that will replace the standard error_exit method:
- */
- METHODDEF(void)
- my_error_exit (j_common_ptr cinfo)
- {
- /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
- my_error_ptr myerr = (my_error_ptr) cinfo->err;
- /* Always display the message. */
- /* We could postpone this until after returning, if we chose. */
- (*cinfo->err->output_message) (cinfo);
- /* Return control to the setjmp point */
- longjmp(myerr->setjmp_buffer, 1);
- }
- #define SWAP(a, b) {
- (a) ^= (b);
- (b) ^= (a);
- (a) ^= (b);
- }
- int OpenIJG2DIB(const char * filename, DIBSection& dib)
- {
- int result = 0;
- struct jpeg_decompress_struct cinfo;
- struct my_error_mgr jerr;
- FILE * infile; /* source file */
- JSAMPARRAY buffer; /* Output row buffer */
- int row_stride; /* physical row width in output buffer */
- if ((infile = fopen(filename, "rb")) == NULL)
- {
- //fprintf(stderr, "can't open %sn", filename);
- return result;
- }
- cinfo.err = jpeg_std_error(&jerr.pub);
- jerr.pub.error_exit = my_error_exit;
- if (setjmp(jerr.setjmp_buffer))
- {
- jpeg_destroy_decompress(&cinfo);
- fclose(infile);
- return result;
- }
- jpeg_create_decompress(&cinfo);
- jpeg_stdio_src(&cinfo, infile);
- jpeg_read_header(&cinfo, TRUE);
- jpeg_start_decompress(&cinfo);
- if (cinfo.num_components == 3)
- {
- dib.Create(cinfo.image_width, cinfo.image_height, 24);
- if (dib.IsCreated())
- {
- row_stride = cinfo.output_width * cinfo.output_components;
- buffer = (*cinfo.mem->alloc_sarray)
- ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
- unsigned char * dibBits = (unsigned char *)dib.GetBits();
- UINT32 off;
- UINT32 tw = dib.GetTotalWidth();
- UINT32 height = dib.Height();
- UINT32 imgWidth = dib.Width() * 3;
- UINT32 col;
- UINT32 row = 0;
- while (cinfo.output_scanline < cinfo.output_height){
- // pull the scanline out of the file and into
- // buffer
- jpeg_read_scanlines(&cinfo, buffer, 1);
- // swap all the red and blue pixels
- for (col=0;col<imgWidth;col+=3){
- SWAP(buffer[0][col],buffer[0][col+2]);
- }
- // copy the row over to the appropriate place in the
- // output dib
- off = (height - ++row) * 3 * tw;
- memcpy(&dibBits[off],buffer[0],row_stride);
- }
- }
- result++;
- }
- jpeg_finish_decompress(&cinfo);
- jpeg_destroy_decompress(&cinfo);
- fclose(infile);
- return result;
- }
- int SaveDIB2IJG(const char * filename, DIBSection& dib, INT32 quality)
- {
- int result = 0;
- if (dib.IsCreated() && (dib.GetBitCount() == 24))
- {
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- FILE * outfile;
- JSAMPROW row_pointer[1];
- int row_stride;
- memset(&cinfo,0,sizeof(jpeg_compress_struct));
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_compress(&cinfo);
- if ((outfile = fopen(filename, "wb")) == NULL)
- {
- fprintf(stderr, "can't open %sn", filename);
- exit(1);
- }
- jpeg_stdio_dest(&cinfo, outfile);
- cinfo.image_width = dib.Width();
- cinfo.image_height = dib.Height();
- cinfo.input_components = 3;
- cinfo.in_color_space = JCS_RGB;
- jpeg_set_defaults(&cinfo);
- jpeg_set_quality(&cinfo, quality, TRUE);
- jpeg_start_compress(&cinfo, TRUE);
- row_stride = cinfo.image_width * 3;
- unsigned char * dibBits = (unsigned char *)dib.GetBits();
- UINT32 off;
- UINT32 tw = dib.GetTotalWidth();
- UINT32 height = dib.Height();
- UINT32 imgWidth = dib.Width() * 3;
- UINT32 col;
- UINT32 row = 0;
- while (cinfo.next_scanline < cinfo.image_height){
- // copy the row over to the appropriate place in the
- // output dib
- off = (height - ++row) * 3 * tw;
- row_pointer[0] = &dibBits[off];
- // swap all the red and blue pixels
- for (col=0;col<imgWidth;col+=3){
- SWAP(row_pointer[0][col],row_pointer[0][col+2]);
- }
- jpeg_write_scanlines(&cinfo, row_pointer, 1);
- }
- jpeg_finish_compress(&cinfo);
- fclose(outfile);
- jpeg_destroy_compress(&cinfo);
- }
- return result;
- }