Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

JPEGSource.cpp

Go to the documentation of this file.
00001 /**************************************************************************
00008  *
00009  * Source code for Real Time Image Library (libRTImage)
00010  *
00011  * Leeds Vision Group give permission for this code to be copied, modified
00012  * and distributed within the University of Leeds subject to the following
00013  * conditions:-
00014  *
00015  * - The code is not to be used for commercial gain.
00016  * - The code and use thereof will be attributed to the author where
00017  *   appropriate (inluding demonstrations which rely on it's use).
00018  * - All modified, distributions of the source files will retain this header.
00019  *
00020  ****************************************************************************/
00021 
00022 #include "JPEGSource.h"
00023 
00024 JPEGSource::JPEGSource(char* jpeg_name)
00031 {
00032     loadJPEG(jpeg_name);
00033     
00034     // default palette for this ImageSource is colour
00035     type      = Image::COLOUR;
00036 }
00037 
00038 
00044 ImageSource& JPEGSource::operator >> (ImageRGB& img)
00045 {
00046     PixelRGB      *img_ptr = img.data; 
00047     unsigned char *src_ptr;
00048     
00049     src_ptr  = image + 4 * width * height;
00050     src_ptr += 4 * width;
00051 
00052     for (int y_cnt = 0; y_cnt < height; y_cnt++) {
00053         src_ptr -= 2 * 4 * width;
00054       
00055         for (int x_cnt = 0; x_cnt < width; x_cnt++) {
00056             PixelRGB pix((int) *(src_ptr++),
00057                          (int) *(src_ptr++),
00058                          (int) *(src_ptr++));
00059             *(img_ptr++) = pix ;
00060             src_ptr++;
00061         }
00062     }
00063     
00064     return *this;
00065 }
00066 
00072 ImageSource& JPEGSource::operator >> (ImageGrey& img)
00073 {
00074     PixelGrey     *img_ptr = img.brightness;
00075     unsigned char *src_ptr;
00076     src_ptr  = image + 4 * width * height;
00077     src_ptr += 4 * width;
00078 
00079     for (int y_cnt = 0; y_cnt < height; y_cnt++) {
00080         src_ptr -= 2 * 4 * width;
00081       
00082         for (int x_cnt = 0 ; x_cnt < width; x_cnt++) {
00083             PixelGrey pix(int (float(RED_COEF   * (*src_ptr++)) + 
00084                                float(GREEN_COEF * (*src_ptr++)) +
00085                                float(BLUE_COEF  * (*src_ptr++)) ));
00086             *(img_ptr++) = pix;
00087             src_ptr++;
00088         }
00089     }
00090     
00091     return *this;                                   
00092 }
00093 
00102 ImageSource& JPEGSource::operator >> (Image& img)
00103 {
00104         (type == Image::COLOUR) ? *this >> (ImageRGB& )img
00105                                 : *this >> (ImageGrey&)img;
00106 
00107         return *this;
00108 }
00109                     
00110 
00116 void JPEGSource::loadJPEG(char* filename)
00117 {
00118     FILE *infile = fopen(filename, "rb");
00119     
00120     jpeg_error_mgr         jerr;
00121     jpeg_decompress_struct cinfo;
00122     
00123     cinfo.err = jpeg_std_error(&jerr);
00124  
00125     jpeg_create_decompress(&cinfo);
00126     jpeg_stdio_src(&cinfo, infile);
00127     jpeg_read_header(&cinfo, TRUE);
00128  
00129     jpeg_start_decompress(&cinfo);
00130  
00131     int rowStride = cinfo.output_width  * cinfo.num_components;
00132     int total     = cinfo.output_height * rowStride;
00133  
00134     JSAMPLE *cdata = new JSAMPLE[total];
00135     JSAMPLE *base = cdata;
00136  
00137     int numread;
00138     while (cinfo.output_scanline < cinfo.output_height)
00139     {
00140         numread = jpeg_read_scanlines(&cinfo, &cdata, cinfo.rec_outbuf_height);
00141         cdata  += numread * rowStride;
00142     }
00143 
00144     fclose(infile);
00145     
00146     int ArowStride = cinfo.output_width  * (cinfo.num_components + 1);
00147     int Atotal     = cinfo.output_height * ArowStride;
00148     
00149     JSAMPLE* idata = new JSAMPLE[Atotal];
00150  
00151     // GL expects the array to start at the bottom left,
00152     // but the decoded data starts at the top left
00153     for (int j=Atotal-ArowStride, i=0; j>=0; j-=2*ArowStride)
00154     {
00155         for (int k=0; k<rowStride; k+=3, i+=3, j+=4)
00156         {
00157         idata[j]   = base[i];
00158         idata[j+1] = base[i+1];
00159         idata[j+2] = base[i+2];
00160         idata[j+3] = 0;
00161         }
00162     }
00163     delete [] base;
00164     base = idata;              
00165                  
00166     // Image data now decompressed and stored in cdata
00167     image  = base;
00168     width  = cinfo.output_width;
00169     height = cinfo.output_height;
00170  
00171     jpeg_finish_decompress(&cinfo);
00172 }
00173                                

Generated at Fri Aug 13 17:29:21 2004 for libRTImage by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001