pnm.cpp

00001 
00002 /***************************************************************************
00003  *  pnm.cpp - PNM reader
00004  *
00005  *  Generated: Sun Jan 13 16:23:08 2008
00006  *  Copyright  2007  Daniel Beck
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 <fvutils/readers/pnm.h>
00025 #include <fvutils/color/colorspaces.h>
00026 #include <fvutils/color/conversions.h>
00027 #include <core/exception.h>
00028 #include <core/exceptions/system.h>
00029 
00030 #include <cstdlib>
00031 #include <cstring>
00032 
00033 using namespace fawkes;
00034 
00035 namespace firevision {
00036 #if 0 /* just to make Emacs auto-indent happy */
00037 }
00038 #endif
00039 
00040 /** @class PNMReader <fvutils/readers/pnm.h>
00041  * PNM file reader.
00042  *
00043  * @author Daniel Beck
00044  */
00045 
00046 /** Constructor.
00047  * @param filename name of the PNM file
00048  */
00049 PNMReader::PNMReader(const char* filename)
00050 {
00051   m_filename = strdup(filename);
00052   m_pnmfile = fopen(m_filename, "rb");
00053 
00054   if ( m_pnmfile == NULL ) 
00055   {
00056     throw Exception("PNMReader::ctor: cannot open PNM file");
00057   }
00058   
00059   // read header
00060   char* line = (char*) malloc(80);
00061   
00062   // magic value
00063   if (fgets(line, 80, m_pnmfile) == NULL)
00064   {
00065     throw FileReadException(m_filename, "Failed to read magic value");
00066   }
00067 
00068   if ( strcmp("P6", line) > 0 )
00069   {
00070     throw Exception("PNMReader::ctor: unknown magic value");
00071   }
00072 
00073   // comments
00074   do
00075   {
00076     if (fgets(line, 80, m_pnmfile) == NULL)
00077     {
00078       throw FileReadException(m_filename, "Failed to read comments");
00079     }
00080   } while ( strncmp("#", line, 1) == 0);
00081   
00082   // width & height
00083   char* tmp = (char*) malloc(10);
00084   char* token;
00085   token = strtok(line, " ");
00086   if ( atoi(token) >= 0 ) { m_img_width = (unsigned int) atoi(token); }
00087   else { throw Exception("PNMReader::ctor: could not read out image width"); };
00088   token = strtok(NULL, " ");
00089   if ( atoi(token) >= 0 ) { m_img_height = (unsigned int) atoi(token); }
00090   else { throw Exception("PNMReader::ctor: could not read out image height"); };
00091   free(tmp);
00092 
00093   // depth
00094   if (fgets(line, 80, m_pnmfile) == NULL)
00095   {
00096     throw FileReadException(m_filename, "Failed to read depth");
00097   }
00098   int max = atoi(line);
00099   free(line);
00100   if ( max >= 0) 
00101   { 
00102     switch(max)
00103     {
00104     case 1:
00105       m_img_depth = 1;
00106       break;
00107           
00108     case 15:
00109       m_img_depth = 2;
00110       break;
00111 
00112     case 255:
00113       m_img_depth = 3;
00114       break;
00115           
00116     default:
00117       break;
00118     }
00119   }
00120   else
00121   {
00122     throw Exception("PNMReader::ctor: unknown color depth");
00123   }
00124 
00125   size_t img_size = m_img_width * m_img_height * m_img_depth;
00126   m_pnm_buffer = (unsigned char*) malloc(img_size);
00127 }
00128 
00129 /** Destructor. */
00130 PNMReader::~PNMReader()
00131 {
00132   free(m_filename);
00133   free(m_pnm_buffer);
00134 }
00135 
00136 void
00137 PNMReader::set_buffer(unsigned char* buffer)
00138 {
00139   m_yuv_buffer = buffer;
00140 }
00141 
00142 colorspace_t
00143 PNMReader::colorspace()
00144 {
00145   return YUV422_PLANAR;
00146 }
00147 
00148 unsigned int
00149 PNMReader::pixel_width()
00150 {
00151   return m_img_width;
00152 }
00153 
00154 unsigned int
00155 PNMReader::pixel_height()
00156 {
00157   return m_img_height;
00158 }
00159 
00160 void
00161 PNMReader::read()
00162 {
00163   if (m_yuv_buffer == NULL)
00164   {
00165     throw Exception("PNMReader::read: buffer = NULL");
00166   }
00167 
00168   if (fread(m_pnm_buffer, m_img_depth, m_img_width * m_img_height, m_pnmfile) != m_img_width * m_img_height)
00169   {
00170     throw fawkes::FileReadException(m_filename, "Failed to read data");
00171   }
00172   convert(RGB, YUV422_PLANAR, m_pnm_buffer, m_yuv_buffer, m_img_width, m_img_height);
00173 }
00174 
00175 } // end namespace firevision

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