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
45 Image::writePNM(std::ostream &os, const char *comment) const
48 os << (channels == 1 ? "P5" : "P6") << "\n";
50 os << "#" << comment << "\n";
52 os << width << " " << height << "\n";
55 const unsigned char *row;
57 if (channels == 1 || channels == 3) {
58 for (row = start(); row != end(); row += stride()) {
59 os.write((const char *)row, width*channels);
62 unsigned char *tmp = new unsigned char[width*3];
64 for (row = start(); row != end(); row += stride()) {
65 const uint32_t *src = (const uint32_t *)row;
66 uint32_t *dst = (uint32_t *)tmp;
68 for (x = 0; x + 4 <= width; x += 4) {
70 * It's much faster to access dwords than bytes.
72 * FIXME: Big-endian version.
75 uint32_t rgba0 = *src++ & 0xffffff;
76 uint32_t rgba1 = *src++ & 0xffffff;
77 uint32_t rgba2 = *src++ & 0xffffff;
78 uint32_t rgba3 = *src++ & 0xffffff;
81 uint32_t rgb1 = (rgba1 >> 8)
83 uint32_t rgb2 = (rgba2 >> 16)
89 for (; x < width; ++x) {
90 tmp[x*3 + 0] = row[x*4 + 0];
91 tmp[x*3 + 1] = row[x*4 + 1];
92 tmp[x*3 + 2] = row[x*4 + 2];
94 os.write((const char *)tmp, width*3);
96 } else if (channels == 2) {
97 for (row = start(); row != end(); row += stride()) {
98 const unsigned char *src = row;
99 unsigned char *dst = tmp;
100 for (unsigned x = 0; x < width; ++x) {
105 os.write((const char *)tmp, width*3);
116 Image::writePNM(const char *filename, const char *comment) const
118 std::ofstream os(filename, std::ofstream::binary);
122 writePNM(os, comment);
128 readPNMHeader(const char *buffer, size_t bufferSize, unsigned *channels, unsigned *width, unsigned *height)
134 const char *currentBuffer = buffer;
135 const char *nextBuffer;
137 // parse number of channels
138 int scannedChannels = sscanf(currentBuffer, "P%d\n", channels);
139 if (scannedChannels != 1) { // validate scanning of channels
140 // invalid channel line
143 // convert channel token to number of channels
144 *channels = (*channels == 5) ? 1 : 3;
146 // advance past channel line
147 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
148 bufferSize -= nextBuffer - currentBuffer;
149 currentBuffer = nextBuffer;
151 // skip over optional comment
152 if (*currentBuffer == '#') {
153 // advance past comment line
154 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
155 bufferSize -= nextBuffer - currentBuffer;
156 currentBuffer = nextBuffer;
159 // parse dimensions of image
160 int scannedDimensions = sscanf(currentBuffer, "%d %d\n", width, height);
161 if (scannedDimensions != 2) { // validate scanning of dimensions
162 // invalid dimension line
166 // advance past dimension line
167 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
168 bufferSize -= nextBuffer - currentBuffer;
169 currentBuffer = nextBuffer;
171 // skip over "255\n" at end of header
172 nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
174 // return start of image data
178 } /* namespace image */