Fawkes API Fawkes Development Version
pnm.cpp
1
2/***************************************************************************
3 * pnm.cpp - PNM reader
4 *
5 * Generated: Sun Jan 13 16:23:08 2008
6 * Copyright 2007 Daniel Beck
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
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>
29
30#include <cstdlib>
31#include <cstring>
32
33using namespace fawkes;
34
35namespace firevision {
36
37/** @class PNMReader <fvutils/readers/pnm.h>
38 * PNM file reader.
39 *
40 * @author Daniel Beck
41 */
42
43/** Constructor.
44 * @param filename name of the PNM file
45 */
46PNMReader::PNMReader(const char *filename)
47{
48 m_filename = strdup(filename);
49 m_pnmfile = fopen(m_filename, "rb");
50
51 if (m_pnmfile == NULL) {
52 throw Exception("PNMReader::ctor: cannot open PNM file");
53 }
54
55 // read header
56 char *line = (char *)malloc(80);
57
58 // magic value
59 if (fgets(line, 80, m_pnmfile) == NULL) {
60 throw FileReadException(m_filename, "Failed to read magic value");
61 }
62
63 if (strcmp("P6", line) > 0) {
64 throw Exception("PNMReader::ctor: unknown magic value");
65 }
66
67 // comments
68 do {
69 if (fgets(line, 80, m_pnmfile) == NULL) {
70 throw FileReadException(m_filename, "Failed to read comments");
71 }
72 } while (strncmp("#", line, 1) == 0);
73
74 // width & height
75 char *tmp = (char *)malloc(10);
76 char *token;
77 token = strtok(line, " ");
78 if (atoi(token) >= 0) {
79 m_img_width = (unsigned int)atoi(token);
80 } else {
81 throw Exception("PNMReader::ctor: could not read out image width");
82 };
83 token = strtok(NULL, " ");
84 if (atoi(token) >= 0) {
85 m_img_height = (unsigned int)atoi(token);
86 } else {
87 throw Exception("PNMReader::ctor: could not read out image height");
88 };
89 free(tmp);
90
91 // depth
92 if (fgets(line, 80, m_pnmfile) == NULL) {
93 throw FileReadException(m_filename, "Failed to read depth");
94 }
95 int max = atoi(line);
96 free(line);
97 if (max >= 0) {
98 switch (max) {
99 case 1: m_img_depth = 1; break;
100
101 case 15: m_img_depth = 2; break;
102
103 case 255: m_img_depth = 3; break;
104
105 default: break;
106 }
107 } else {
108 throw Exception("PNMReader::ctor: unknown color depth");
109 }
110
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);
113}
114
115/** Destructor. */
117{
118 fclose(m_pnmfile);
119 free(m_filename);
120 free(m_pnm_buffer);
121}
122
123void
124PNMReader::set_buffer(unsigned char *buffer)
125{
126 m_yuv_buffer = buffer;
127}
128
129colorspace_t
131{
132 return YUV422_PLANAR;
133}
134
135unsigned int
137{
138 return m_img_width;
139}
140
141unsigned int
143{
144 return m_img_height;
145}
146
147void
149{
150 if (m_yuv_buffer == NULL) {
151 throw Exception("PNMReader::read: buffer = NULL");
152 }
153
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) {
156 throw fawkes::FileReadException(m_filename, "Failed to read data");
157 }
158 convert(RGB, YUV422_PLANAR, m_pnm_buffer, m_yuv_buffer, m_img_width, m_img_height);
159}
160
161} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
File could not be read.
Definition: system.h:62
virtual void set_buffer(unsigned char *yuv422planar_buffer)
Set buffer that the read image should be written to.
Definition: pnm.cpp:124
PNMReader(const char *filename)
Constructor.
Definition: pnm.cpp:46
virtual unsigned int pixel_width()
Get width of read image in pixels.
Definition: pnm.cpp:136
virtual ~PNMReader()
Destructor.
Definition: pnm.cpp:116
virtual unsigned int pixel_height()
Get height of read image in pixels.
Definition: pnm.cpp:142
virtual colorspace_t colorspace()
Get colorspace from the just read image.
Definition: pnm.cpp:130
virtual void read()
Read data from file.
Definition: pnm.cpp:148
Fawkes library namespace.