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) {
65 } else if (channels <= 3) {
69 // Non-standard extension for 4 floats
78 os << identifier << "\n";
81 os << "#" << comment << "\n";
83 os << width << " " << height << "\n";
85 if (channelType == TYPE_UNORM8) {
91 const unsigned char *row;
93 if (channels == outChannels) {
95 * Write whole pixel spans straight from the image buffer.
98 for (row = start(); row != end(); row += stride()) {
99 os.write((const char *)row, width*bytesPerPixel);
103 * Need to add/remove channels, one pixel at a time.
106 unsigned char *tmp = new unsigned char[width*outChannels*bytesPerChannel];
108 if (channelType == TYPE_UNORM8) {
110 * Optimized path for 8bit unorms.
114 for (row = start(); row != end(); row += stride()) {
115 const uint32_t *src = (const uint32_t *)row;
116 uint32_t *dst = (uint32_t *)tmp;
118 for (x = 0; x + 4 <= width; x += 4) {
120 * It's much faster to access dwords than bytes.
122 * FIXME: Big-endian version.
125 uint32_t rgba0 = *src++ & 0xffffff;
126 uint32_t rgba1 = *src++ & 0xffffff;
127 uint32_t rgba2 = *src++ & 0xffffff;
128 uint32_t rgba3 = *src++ & 0xffffff;
129 uint32_t rgb0 = rgba0
131 uint32_t rgb1 = (rgba1 >> 8)
133 uint32_t rgb2 = (rgba2 >> 16)
139 for (; x < width; ++x) {
140 tmp[x*3 + 0] = row[x*4 + 0];
141 tmp[x*3 + 1] = row[x*4 + 1];
142 tmp[x*3 + 2] = row[x*4 + 2];
144 os.write((const char *)tmp, width*3);
146 } else if (channels == 2) {
147 for (row = start(); row != end(); row += stride()) {
148 const unsigned char *src = row;
149 unsigned char *dst = tmp;
150 for (unsigned x = 0; x < width; ++x) {
155 os.write((const char *)tmp, width*3);
162 * General path for float images.
165 unsigned copyChannels = std::min(channels, outChannels);
167 assert(channelType == TYPE_FLOAT);
169 for (row = start(); row != end(); row += stride()) {
170 const float *src = (const float *)row;
171 float *dst = (float *)tmp;
172 for (unsigned x = 0; x < width; ++x) {
173 unsigned channel = 0;
174 for (; channel < copyChannels; ++channel) {
177 for (; channel < outChannels; ++channel) {
181 os.write((const char *)tmp, width*outChannels*bytesPerChannel);
191 Image::writePNM(const char *filename, const char *comment) const
193 std::ofstream os(filename, std::ofstream::binary);
197 writePNM(os, comment);
205 * Returns pointer to data start, or NULL on failure.
208 readPNMHeader(const char *buffer, size_t bufferSize, PNMInfo &info)
214 const char *currentBuffer = buffer;
215 const char *nextBuffer;
217 // parse number of channels
219 int scannedChannels = sscanf(currentBuffer, "P%c\n", &c);
220 if (scannedChannels != 1) { // validate scanning of channels
221 // invalid channel line
224 // convert channel token to number of channels
228 info.channelType = TYPE_UNORM8;
232 info.channelType = TYPE_UNORM8;
236 info.channelType = TYPE_FLOAT;
240 info.channelType = TYPE_FLOAT;
244 info.channelType = TYPE_FLOAT;
250 // advance past channel line
251 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
252 bufferSize -= nextBuffer - currentBuffer;
253 currentBuffer = nextBuffer;
255 // skip over optional comment
256 if (*currentBuffer == '#') {
257 // advance past comment line
258 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
259 bufferSize -= nextBuffer - currentBuffer;
260 currentBuffer = nextBuffer;
263 // parse dimensions of image
264 int scannedDimensions = sscanf(currentBuffer, "%u %u\n", &info.width, &info.height);
265 if (scannedDimensions != 2) { // validate scanning of dimensions
266 // invalid dimension line
270 // advance past dimension line
271 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
272 bufferSize -= nextBuffer - currentBuffer;
273 currentBuffer = nextBuffer;
275 // skip scale factor / endianness line
276 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
278 // return start of image data
284 readPNM(const char *buffer, size_t bufferSize)
288 const char *headerEnd = readPNMHeader(buffer, bufferSize, info);
290 // if invalid PNM header was encountered, ...
291 if (headerEnd == NULL) {
292 std::cerr << "error: invalid PNM header";
296 Image *image = new Image(info.width, info.height, info.channels, false, info.channelType);
298 size_t rowBytes = info.width * image->bytesPerPixel;
299 for (unsigned char *row = image->start(); row != image->end(); row += image->stride()) {
300 memcpy(row, headerEnd, rowBytes);
301 headerEnd += rowBytes;
309 } /* namespace image */