24 #include <core/exception.h> 25 #include <core/exceptions/system.h> 26 #include <fvutils/color/colorspaces.h> 27 #include <fvutils/color/conversions.h> 28 #include <fvutils/readers/pnm.h> 35 namespace firevision {
46 PNMReader::PNMReader(
const char *filename)
48 m_filename = strdup(filename);
49 m_pnmfile = fopen(m_filename,
"rb");
51 if (m_pnmfile == NULL) {
52 throw Exception(
"PNMReader::ctor: cannot open PNM file");
56 char *line = (
char *)malloc(80);
59 if (fgets(line, 80, m_pnmfile) == NULL) {
63 if (strcmp(
"P6", line) > 0) {
64 throw Exception(
"PNMReader::ctor: unknown magic value");
69 if (fgets(line, 80, m_pnmfile) == NULL) {
72 }
while (strncmp(
"#", line, 1) == 0);
75 char *tmp = (
char *)malloc(10);
77 token = strtok(line,
" ");
78 if (atoi(token) >= 0) {
79 m_img_width = (
unsigned int)atoi(token);
81 throw Exception(
"PNMReader::ctor: could not read out image width");
83 token = strtok(NULL,
" ");
84 if (atoi(token) >= 0) {
85 m_img_height = (
unsigned int)atoi(token);
87 throw Exception(
"PNMReader::ctor: could not read out image height");
92 if (fgets(line, 80, m_pnmfile) == NULL) {
99 case 1: m_img_depth = 1;
break;
101 case 15: m_img_depth = 2;
break;
103 case 255: m_img_depth = 3;
break;
108 throw Exception(
"PNMReader::ctor: unknown color depth");
111 size_t img_size = (size_t)m_img_width * m_img_height * m_img_depth;
112 m_pnm_buffer = (
unsigned char *)malloc(img_size);
116 PNMReader::~PNMReader()
124 PNMReader::set_buffer(
unsigned char *buffer)
126 m_yuv_buffer = buffer;
130 PNMReader::colorspace()
132 return YUV422_PLANAR;
136 PNMReader::pixel_width()
142 PNMReader::pixel_height()
150 if (m_yuv_buffer == NULL) {
151 throw Exception(
"PNMReader::read: buffer = NULL");
154 if (fread(m_pnm_buffer, m_img_depth, (
size_t)m_img_width * m_img_height, m_pnmfile)
155 != (
size_t)m_img_width * m_img_height) {
158 convert(RGB, YUV422_PLANAR, m_pnm_buffer, m_yuv_buffer, m_img_width, m_img_height);
Fawkes library namespace.
Base class for exceptions in Fawkes.