jpeg.cpp

00001 
00002 /***************************************************************************
00003  *  jpeg.cpp - JPEG Reader
00004  *
00005  *  Generated: Sun Jun 04 23:18:06 2006 (watching Terminator 2)
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <core/exception.h>
00025 #include <fvutils/readers/jpeg.h>
00026 #include <fvutils/color/rgbyuv.h>
00027 
00028 #include <cstdio>
00029 #include <cstdlib>
00030 
00031 using namespace fawkes;
00032 
00033 namespace firevision {
00034 #if 0 /* just to make Emacs auto-indent happy */
00035 }
00036 #endif
00037 
00038 /** @class JpegReader <fvutils/readers/jpeg.h>
00039  * JPEG file reader.
00040  * @author Tim Niemueller
00041  */
00042 
00043 /** Constructor.
00044  * @param filename file to read
00045  */
00046 JpegReader::JpegReader(const char *filename)
00047 {
00048   opened = false;
00049   buffer = NULL;
00050 
00051   if ((infile = fopen(filename, "rb")) == NULL) {
00052     throw Exception("Cannot open JPEG file");
00053   }
00054 
00055   cinfo.err = jpeg_std_error( &jerr );
00056   jpeg_create_decompress( &cinfo );
00057   jpeg_stdio_src( &cinfo, infile );
00058 
00059   jpeg_read_header( &cinfo, true );
00060   jpeg_calc_output_dimensions( &cinfo );
00061 
00062   /*
00063   cout << "Read JPEG header, image info:" << endl
00064        << "  width:   " << cinfo.output_width << endl
00065        << "  height:  " << cinfo.output_height << endl;
00066   */
00067 
00068   opened = true;
00069 }
00070 
00071 
00072 /** Destructor. */
00073 JpegReader::~JpegReader()
00074 {
00075   jpeg_destroy_decompress( &cinfo );
00076   fclose( infile );
00077   opened = false;
00078 }
00079 
00080 
00081 void
00082 JpegReader::set_buffer(unsigned char *yuv422planar_buffer)
00083 {
00084   buffer = yuv422planar_buffer;
00085 }
00086 
00087 
00088 colorspace_t
00089 JpegReader::colorspace()
00090 {
00091   return YUV422_PLANAR;
00092 }
00093 
00094 
00095 unsigned int
00096 JpegReader::pixel_width()
00097 {
00098   if ( opened ) {
00099     return cinfo.output_width;
00100   } else {
00101     return 0;
00102   }
00103 }
00104 
00105 
00106 unsigned int
00107 JpegReader::pixel_height()
00108 {
00109   if ( opened ) {
00110     return cinfo.output_height;
00111   } else {
00112     return 0;
00113   }
00114 }
00115 
00116 
00117 void
00118 JpegReader::read()
00119 {
00120   if ( buffer == NULL ) {
00121     throw Exception("JpegReader::read: buffer == NULL");
00122   }
00123 
00124   jpeg_start_decompress( &cinfo );
00125   row_stride = cinfo.output_width * cinfo.output_components;
00126 
00127   row_buffer = (unsigned char *)malloc( row_stride );
00128 
00129   while ( cinfo.output_scanline < cinfo.output_height ) {
00130     jpeg_read_scanlines( &cinfo, &row_buffer, 1 );
00131     convert_line_rgb_to_yuv422planar( row_buffer, buffer,
00132                                       cinfo.output_width, cinfo.output_height,
00133                                       0, cinfo.output_scanline - 1 );
00134   }
00135 
00136   free( row_buffer );
00137   jpeg_finish_decompress( &cinfo );
00138 
00139 }
00140 
00141 } // end namespace firevision

Generated on Tue Feb 22 13:32:16 2011 for Fawkes API by  doxygen 1.4.7