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 /** @class JpegReader jpeg.h <fvutils/readers/jpeg.h>
00034  * JPEG file reader.
00035  * @author Tim Niemueller
00036  */
00037 
00038 /** Constructor.
00039  * @param filename file to read
00040  */
00041 JpegReader::JpegReader(const char *filename)
00042 {
00043   opened = false;
00044   buffer = NULL;
00045
00046   if ((infile = fopen(filename, "rb")) == NULL) {
00047     throw Exception("Cannot open JPEG file");
00048   }
00049
00050   cinfo.err = jpeg_std_error( &jerr );
00051   jpeg_create_decompress( &cinfo );
00052   jpeg_stdio_src( &cinfo, infile );
00053
00054   jpeg_read_header( &cinfo, true );
00055   jpeg_calc_output_dimensions( &cinfo );
00056
00057   /*
00058   cout << "Read JPEG header, image info:" << endl
00059        << "  width:   " << cinfo.output_width << endl
00060        << "  height:  " << cinfo.output_height << endl;
00061   */
00062
00063   opened = true;
00064 }
00065
00066 
00067 /** Destructor. */
00068 JpegReader::~JpegReader()
00069 {
00070   jpeg_destroy_decompress( &cinfo );
00071   fclose( infile );
00072   opened = false;
00073 }
00074
00075
00076 void
00077 JpegReader::set_buffer(unsigned char *yuv422planar_buffer)
00078 {
00079   buffer = yuv422planar_buffer;
00080 }
00081
00082
00083 colorspace_t
00084 JpegReader::colorspace()
00085 {
00086   return YUV422_PLANAR;
00087 }
00088
00089
00090 unsigned int
00091 JpegReader::pixel_width()
00092 {
00093   if ( opened ) {
00094     return cinfo.output_width;
00095   } else {
00096     return 0;
00097   }
00098 }
00099
00100
00101 unsigned int
00102 JpegReader::pixel_height()
00103 {
00104   if ( opened ) {
00105     return cinfo.output_height;
00106   } else {
00107     return 0;
00108   }
00109 }
00110
00111
00112 void
00113 JpegReader::read()
00114 {
00115   if ( buffer == NULL ) {
00116     throw Exception("JpegReader::read: buffer == NULL");
00117   }
00118
00119   jpeg_start_decompress( &cinfo );
00120   row_stride = cinfo.output_width * cinfo.output_components;
00121
00122   row_buffer = (unsigned char *)malloc( row_stride );
00123
00124   while ( cinfo.output_scanline < cinfo.output_height ) {
00125     jpeg_read_scanlines( &cinfo, &row_buffer, 1 );
00126     convert_line_rgb_to_yuv422planar( row_buffer, buffer,
00127                                       cinfo.output_width, cinfo.output_height,
00128                                       0, cinfo.output_scanline - 1 );
00129   }
00130
00131   free( row_buffer );
00132   jpeg_finish_decompress( &cinfo );
00133
00134 }