Fawkes API
Fawkes Development Version
|
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 fclose(m_pnmfile); 00133 free(m_filename); 00134 free(m_pnm_buffer); 00135 } 00136 00137 void 00138 PNMReader::set_buffer(unsigned char* buffer) 00139 { 00140 m_yuv_buffer = buffer; 00141 } 00142 00143 colorspace_t 00144 PNMReader::colorspace() 00145 { 00146 return YUV422_PLANAR; 00147 } 00148 00149 unsigned int 00150 PNMReader::pixel_width() 00151 { 00152 return m_img_width; 00153 } 00154 00155 unsigned int 00156 PNMReader::pixel_height() 00157 { 00158 return m_img_height; 00159 } 00160 00161 void 00162 PNMReader::read() 00163 { 00164 if (m_yuv_buffer == NULL) 00165 { 00166 throw Exception("PNMReader::read: buffer = NULL"); 00167 } 00168 00169 if (fread(m_pnm_buffer, m_img_depth, m_img_width * m_img_height, m_pnmfile) != m_img_width * m_img_height) 00170 { 00171 throw fawkes::FileReadException(m_filename, "Failed to read data"); 00172 } 00173 convert(RGB, YUV422_PLANAR, m_pnm_buffer, m_yuv_buffer, m_img_width, m_img_height); 00174 } 00175 00176 } // end namespace firevision