write_jpeg.c

Go to the documentation of this file.
00001 //
00002 // Copyright 2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <jpeglib.h>
00015 #include "write_jpeg.h"
00016 
00017 extern void jpeg_mem_dest(j_compress_ptr cinfo, JOCTET* buf, size_t bufsize);
00018 extern int  jpeg_mem_size(j_compress_ptr cinfo);
00019 
00020 int
00021 write_jpeg_mem(unsigned char* YCbCr,
00022                int w, int h, int quality,
00023                unsigned char* dest, int destsize)
00024 {
00025     JSAMPLE* image_buffer = (JSAMPLE*)YCbCr;
00026     int      image_width  = w;
00027     int      image_height = h;
00028 
00029     struct jpeg_compress_struct cinfo;
00030     struct jpeg_error_mgr jerr;
00031 
00032     JSAMPROW row_pointer[1];    /* pointer to JSAMPLE row[s] */
00033     int row_stride;             /* physical row width in image buffer */
00034     int jpegsize;
00035 
00036     cinfo.err = jpeg_std_error(&jerr);
00037     jpeg_create_compress(&cinfo);
00038 
00039     jpeg_mem_dest(&cinfo, dest, destsize);
00040 
00041     cinfo.image_width = image_width;
00042     cinfo.image_height = image_height;
00043     cinfo.input_components = 3;         /* # of color components per pixel */
00044     cinfo.in_color_space = JCS_YCbCr; /* colorspace of input image */
00045 
00046     jpeg_set_defaults(&cinfo);
00047 
00048     jpeg_set_quality(&cinfo,
00049                      quality, TRUE /* limit to baseline-JPEG values */);
00050 
00051     jpeg_start_compress(&cinfo, TRUE);
00052 
00053     row_stride = image_width * 3;       /* JSAMPLEs per row in image_buffer */
00054 
00055     while (cinfo.next_scanline < cinfo.image_height) {
00056         row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00057         (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00058     }
00059 
00060     jpeg_finish_compress(&cinfo);
00061     jpegsize = jpeg_mem_size(&cinfo);
00062 
00063     jpeg_destroy_compress(&cinfo);
00064 
00065     return jpegsize;
00066 }
00067 
00068 void
00069 write_jpeg_file(unsigned char* YCbCr, int w, int h, int quality, FILE* outfile)
00070 {
00071     JSAMPLE* image_buffer = (JSAMPLE*)YCbCr;
00072     int      image_width  = w;
00073     int      image_height = h;
00074 
00075     struct jpeg_compress_struct cinfo;
00076     struct jpeg_error_mgr jerr;
00077 
00078     JSAMPROW row_pointer[1];    /* pointer to JSAMPLE row[s] */
00079     int row_stride;             /* physical row width in image buffer */
00080 
00081     cinfo.err = jpeg_std_error(&jerr);
00082     jpeg_create_compress(&cinfo);
00083 
00084     jpeg_stdio_dest(&cinfo, outfile);
00085 
00086     cinfo.image_width = image_width;
00087     cinfo.image_height = image_height;
00088     cinfo.input_components = 3;         /* # of color components per pixel */
00089     cinfo.in_color_space = JCS_YCbCr; /* colorspace of input image */
00090 
00091     jpeg_set_defaults(&cinfo);
00092 
00093     jpeg_set_quality(&cinfo,
00094                      quality, TRUE /* limit to baseline-JPEG values */);
00095     
00096     jpeg_start_compress(&cinfo, TRUE);
00097 
00098     row_stride = image_width * 3;       /* JSAMPLEs per row in image_buffer */
00099 
00100     while (cinfo.next_scanline < cinfo.image_height) {
00101         row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00102         (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00103     }
00104 
00105     jpeg_finish_compress(&cinfo);
00106 
00107     jpeg_destroy_compress(&cinfo);
00108 }

Generated on Sun Dec 2 23:04:31 2007 for openSDK by  doxygen 1.3.9.1