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) {
87 const unsigned char *row;
89 if (channels == outChannels) {
91 * Write whole pixel spans straight from the image buffer.
94 for (row = start(); row != end(); row += stride()) {
95 os.write((const char *)row, width*bytesPerPixel);
99 * Need to add/remove channels, one pixel at a time.
102 unsigned char *tmp = new unsigned char[width*outChannels*bytesPerChannel];
104 if (channelType == TYPE_UNORM8) {
106 * Optimized path for 8bit unorms.
110 for (row = start(); row != end(); row += stride()) {
111 const uint32_t *src = (const uint32_t *)row;
112 uint32_t *dst = (uint32_t *)tmp;
114 for (x = 0; x + 4 <= width; x += 4) {
116 * It's much faster to access dwords than bytes.
118 * FIXME: Big-endian version.
121 uint32_t rgba0 = *src++ & 0xffffff;
122 uint32_t rgba1 = *src++ & 0xffffff;
123 uint32_t rgba2 = *src++ & 0xffffff;
124 uint32_t rgba3 = *src++ & 0xffffff;
125 uint32_t rgb0 = rgba0
127 uint32_t rgb1 = (rgba1 >> 8)
129 uint32_t rgb2 = (rgba2 >> 16)
135 for (; x < width; ++x) {
136 tmp[x*3 + 0] = row[x*4 + 0];
137 tmp[x*3 + 1] = row[x*4 + 1];
138 tmp[x*3 + 2] = row[x*4 + 2];
140 os.write((const char *)tmp, width*3);
142 } else if (channels == 2) {
143 for (row = start(); row != end(); row += stride()) {
144 const unsigned char *src = row;
145 unsigned char *dst = tmp;
146 for (unsigned x = 0; x < width; ++x) {
151 os.write((const char *)tmp, width*3);
158 * General path for float images.
161 unsigned copyChannels = std::min(channels, outChannels);
163 assert(channelType == TYPE_FLOAT);
165 for (row = start(); row != end(); row += stride()) {
166 const float *src = (const float *)row;
167 float *dst = (float *)tmp;
168 for (unsigned x = 0; x < width; ++x) {
169 unsigned channel = 0;
170 for (; channel < copyChannels; ++channel) {
173 for (; channel < outChannels; ++channel) {
177 os.write((const char *)tmp, width*outChannels*bytesPerChannel);
187 Image::writePNM(const char *filename, const char *comment) const
189 std::ofstream os(filename, std::ofstream::binary);
193 writePNM(os, comment);
201 * Returns pointer to data start, or NULL on failure.
204 readPNMHeader(const char *buffer, size_t bufferSize, PNMInfo &info)
210 const char *currentBuffer = buffer;
211 const char *nextBuffer;
213 // parse number of channels
215 int scannedChannels = sscanf(currentBuffer, "P%c\n", &c);
216 if (scannedChannels != 1) { // validate scanning of channels
217 // invalid channel line
220 // convert channel token to number of channels
224 info.channelType = TYPE_UNORM8;
228 info.channelType = TYPE_UNORM8;
232 info.channelType = TYPE_FLOAT;
236 info.channelType = TYPE_FLOAT;
242 // advance past channel line
243 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
244 bufferSize -= nextBuffer - currentBuffer;
245 currentBuffer = nextBuffer;
247 // skip over optional comment
248 if (*currentBuffer == '#') {
249 // advance past comment line
250 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
251 bufferSize -= nextBuffer - currentBuffer;
252 currentBuffer = nextBuffer;
255 // parse dimensions of image
256 int scannedDimensions = sscanf(currentBuffer, "%u %u\n", &info.width, &info.height);
257 if (scannedDimensions != 2) { // validate scanning of dimensions
258 // invalid dimension line
262 // advance past dimension line
263 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
264 bufferSize -= nextBuffer - currentBuffer;
265 currentBuffer = nextBuffer;
267 // skip scale factor / endianness line
268 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
270 // return start of image data
276 readPNM(const char *buffer, size_t bufferSize)
280 const char *headerEnd = readPNMHeader(buffer, bufferSize, info);
282 // if invalid PNM header was encountered, ...
283 if (headerEnd == NULL) {
284 std::cerr << "error: invalid PNM header";
288 Image *image = new Image(info.width, info.height, info.channels, false, info.channelType);
290 size_t rowBytes = info.width * image->bytesPerPixel;
291 for (unsigned char *row = image->start(); row != image->end(); row += image->stride()) {
292 memcpy(row, headerEnd, rowBytes);
293 headerEnd += rowBytes;
301 } /* namespace image */