00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00037 }
00038 #endif
00039
00040
00041
00042
00043
00044
00045
00046
00047
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
00060 char* line = (char*) malloc(80);
00061
00062
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
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
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
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
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 }