]> git.cworth.org Git - apitrace/blob - image/image_pnm.cpp
9e9d0e1be9cb3542a63814730a081354000c20cf
[apitrace] / image / image_pnm.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2008-2010 VMware, Inc.
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <stdio.h>
32
33 #include <fstream>
34
35 #include "image.hpp"
36
37
38 namespace image {
39
40 /**
41  * http://en.wikipedia.org/wiki/Netpbm_format
42  * http://netpbm.sourceforge.net/doc/ppm.html
43  */
44 void
45 Image::writePNM(std::ostream &os, const char *comment) const
46 {
47
48     os << (channels == 1 ? "P5" : "P6") << "\n";
49     if (comment) {
50         os << "#" << comment << "\n";
51     }
52     os << width << " " << height << "\n";
53     os << "255" << "\n";
54
55     const unsigned char *row;
56
57     if (channels == 1 || channels == 3) {
58         for (row = start(); row != end(); row += stride()) {
59             os.write((const char *)row, width*channels);
60         }
61     } else {
62         unsigned char *tmp = new unsigned char[width*3];
63         if (channels == 4) {
64             for (row = start(); row != end(); row += stride()) {
65                 const uint32_t *src = (const uint32_t *)row;
66                 uint32_t *dst = (uint32_t *)tmp;
67                 unsigned x;
68                 for (x = 0; x + 4 <= width; x += 4) {
69                     /*
70                      * It's much faster to access dwords than bytes.
71                      *
72                      * FIXME: Big-endian version.
73                      */
74
75                     uint32_t rgba0 = *src++ & 0xffffff;
76                     uint32_t rgba1 = *src++ & 0xffffff;
77                     uint32_t rgba2 = *src++ & 0xffffff;
78                     uint32_t rgba3 = *src++ & 0xffffff;
79                     uint32_t rgb0 = rgba0
80                                   | (rgba1 << 24);
81                     uint32_t rgb1 = (rgba1 >> 8)
82                                   | (rgba2 << 16);
83                     uint32_t rgb2 = (rgba2 >> 16)
84                                   | (rgba3 << 8);
85                     *dst++ = rgb0;
86                     *dst++ = rgb1;
87                     *dst++ = rgb2;
88                 }
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];
93                 }
94                 os.write((const char *)tmp, width*3);
95             }
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) {
101                     *dst++ = *src++;
102                     *dst++ = *src++;
103                     *dst++ = 0;
104                 }
105                 os.write((const char *)tmp, width*3);
106             }
107         } else {
108             assert(0);
109         }
110         delete [] tmp;
111     }
112 }
113
114
115 bool
116 Image::writePNM(const char *filename, const char *comment) const
117 {
118     std::ofstream os(filename, std::ofstream::binary);
119     if (!os) {
120         return false;
121     }
122     writePNM(os, comment);
123     return true;
124 }
125
126
127 const char *
128 readPNMHeader(const char *buffer, size_t bufferSize, unsigned *channels, unsigned *width, unsigned *height)
129 {
130     *channels = 0;
131     *width = 0;
132     *height = 0;
133
134     const char *currentBuffer = buffer;
135     const char *nextBuffer;
136
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
141         return buffer;
142     }
143     // convert channel token to number of channels
144     *channels = (*channels == 5) ? 1 : 3;
145
146     // advance past channel line
147     nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
148     bufferSize -= nextBuffer - currentBuffer;
149     currentBuffer = nextBuffer;
150
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;
157     }
158
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
163         return buffer;
164     }
165
166     // advance past dimension line
167     nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
168     bufferSize -= nextBuffer - currentBuffer;
169     currentBuffer = nextBuffer;
170
171     // skip over "255\n" at end of header
172     nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
173
174     // return start of image data
175     return nextBuffer;
176 }
177
178 } /* namespace image */