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 unsigned copyChannels = std::min(channels, outChannels);
161 assert(channelType == TYPE_FLOAT);
163 for (row = start(); row != end(); row += stride()) {
164 const float *src = (const float *)row;
165 float *dst = (float *)tmp;
166 for (unsigned x = 0; x < width; ++x) {
167 unsigned channel = 0;
168 for (; channel < copyChannels; ++channel) {
171 for (; channel < outChannels; ++channel) {
175 os.write((const char *)tmp, width*bytesPerPixel);
185 Image::writePNM(const char *filename, const char *comment) const
187 std::ofstream os(filename, std::ofstream::binary);
191 writePNM(os, comment);
199 * Returns pointer to data start, or NULL on failure.
202 readPNMHeader(const char *buffer, size_t bufferSize, PNMInfo &info)
208 const char *currentBuffer = buffer;
209 const char *nextBuffer;
211 // parse number of channels
213 int scannedChannels = sscanf(currentBuffer, "P%c\n", &c);
214 if (scannedChannels != 1) { // validate scanning of channels
215 // invalid channel line
218 // convert channel token to number of channels
222 info.channelType = TYPE_UNORM8;
226 info.channelType = TYPE_UNORM8;
230 info.channelType = TYPE_FLOAT;
234 info.channelType = TYPE_FLOAT;
240 // advance past channel line
241 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
242 bufferSize -= nextBuffer - currentBuffer;
243 currentBuffer = nextBuffer;
245 // skip over optional comment
246 if (*currentBuffer == '#') {
247 // advance past comment line
248 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
249 bufferSize -= nextBuffer - currentBuffer;
250 currentBuffer = nextBuffer;
253 // parse dimensions of image
254 int scannedDimensions = sscanf(currentBuffer, "%u %u\n", &info.width, &info.height);
255 if (scannedDimensions != 2) { // validate scanning of dimensions
256 // invalid dimension line
260 // advance past dimension line
261 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
262 bufferSize -= nextBuffer - currentBuffer;
263 currentBuffer = nextBuffer;
265 if (info.channelType == TYPE_UNORM8) {
266 // skip over "255\n" at end of header
267 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 */