1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
4 * Copyright 2008-2010 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
41 * http://en.wikipedia.org/wiki/Netpbm_format
42 * http://netpbm.sourceforge.net/doc/ppm.html
43 * http://netpbm.sourceforge.net/doc/pfm.html
46 Image::writePNM(std::ostream &os, const char *comment) const
48 const char *identifier;
51 switch (channelType) {
74 os << identifier << "\n";
77 os << "#" << comment << "\n";
79 os << width << " " << height << "\n";
81 if (channelType == TYPE_UNORM8) {
85 const unsigned char *row;
87 if (channels == outChannels) {
89 * Write whole pixel spans straight from the image buffer.
92 for (row = start(); row != end(); row += stride()) {
93 os.write((const char *)row, width*bytesPerPixel);
97 * Need to add/remove channels, one pixel at a time.
100 unsigned char *tmp = new unsigned char[width*bytesPerPixel];
102 if (channelType == TYPE_UNORM8) {
104 * Optimized path for 8bit unorms.
108 for (row = start(); row != end(); row += stride()) {
109 const uint32_t *src = (const uint32_t *)row;
110 uint32_t *dst = (uint32_t *)tmp;
112 for (x = 0; x + 4 <= width; x += 4) {
114 * It's much faster to access dwords than bytes.
116 * FIXME: Big-endian version.
119 uint32_t rgba0 = *src++ & 0xffffff;
120 uint32_t rgba1 = *src++ & 0xffffff;
121 uint32_t rgba2 = *src++ & 0xffffff;
122 uint32_t rgba3 = *src++ & 0xffffff;
123 uint32_t rgb0 = rgba0
125 uint32_t rgb1 = (rgba1 >> 8)
127 uint32_t rgb2 = (rgba2 >> 16)
133 for (; x < width; ++x) {
134 tmp[x*3 + 0] = row[x*4 + 0];
135 tmp[x*3 + 1] = row[x*4 + 1];
136 tmp[x*3 + 2] = row[x*4 + 2];
138 os.write((const char *)tmp, width*3);
140 } else if (channels == 2) {
141 for (row = start(); row != end(); row += stride()) {
142 const unsigned char *src = row;
143 unsigned char *dst = tmp;
144 for (unsigned x = 0; x < width; ++x) {
149 os.write((const char *)tmp, width*3);
156 * General path for float images.
159 assert(channelType == TYPE_FLOAT);
161 for (row = start(); row != end(); row += stride()) {
162 const float *src = (const float *)row;
163 float *dst = (float *)tmp;
164 for (unsigned x = 0; x < width; ++x) {
165 unsigned channel = 0;
166 for (; channel < channels; ++channel) {
169 for (; channel < channels; ++channel) {
173 os.write((const char *)tmp, width*bytesPerPixel);
183 Image::writePNM(const char *filename, const char *comment) const
185 std::ofstream os(filename, std::ofstream::binary);
189 writePNM(os, comment);
197 * Returns pointer to data start, or NULL on failure.
200 readPNMHeader(const char *buffer, size_t bufferSize, PNMInfo &info)
206 const char *currentBuffer = buffer;
207 const char *nextBuffer;
209 // parse number of channels
211 int scannedChannels = sscanf(currentBuffer, "P%c\n", &c);
212 if (scannedChannels != 1) { // validate scanning of channels
213 // invalid channel line
216 // convert channel token to number of channels
220 info.channelType = TYPE_UNORM8;
224 info.channelType = TYPE_UNORM8;
228 info.channelType = TYPE_FLOAT;
232 info.channelType = TYPE_FLOAT;
238 // advance past channel line
239 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
240 bufferSize -= nextBuffer - currentBuffer;
241 currentBuffer = nextBuffer;
243 // skip over optional comment
244 if (*currentBuffer == '#') {
245 // advance past comment line
246 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
247 bufferSize -= nextBuffer - currentBuffer;
248 currentBuffer = nextBuffer;
251 // parse dimensions of image
252 int scannedDimensions = sscanf(currentBuffer, "%u %u\n", &info.width, &info.height);
253 if (scannedDimensions != 2) { // validate scanning of dimensions
254 // invalid dimension line
258 // advance past dimension line
259 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
260 bufferSize -= nextBuffer - currentBuffer;
261 currentBuffer = nextBuffer;
263 if (info.channelType == TYPE_UNORM8) {
264 // skip over "255\n" at end of header
265 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
268 // return start of image data
274 readPNM(const char *buffer, size_t bufferSize)
278 const char *headerEnd = readPNMHeader(buffer, bufferSize, info);
280 // if invalid PNM header was encountered, ...
281 if (headerEnd == NULL) {
282 std::cerr << "error: invalid PNM header";
286 Image *image = new Image(info.width, info.height, info.channels, false, info.channelType);
288 size_t rowBytes = info.width * image->bytesPerPixel;
289 for (unsigned char *row = image->start(); row != image->end(); row += image->stride()) {
290 memcpy(row, headerEnd, rowBytes);
291 headerEnd += rowBytes;
299 } /* namespace image */