]> git.cworth.org Git - apitrace/blob - image/image_pnm.cpp
a0638dc59356c58840b03a539e80a7dead4049ba
[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  * http://netpbm.sourceforge.net/doc/pfm.html
44  */
45 void
46 Image::writePNM(std::ostream &os, const char *comment) const
47 {
48     const char *identifier;
49     unsigned outChannels;
50
51     switch (channelType) {
52     case TYPE_UNORM8:
53         if (channels == 1) {
54             identifier = "P5";
55             outChannels = 1;
56         } else {
57             identifier = "P6";
58             outChannels = 3;
59         }
60         break;
61     case TYPE_FLOAT:
62         if (channels == 1) {
63             identifier = "Pf";
64             outChannels = 1;
65         } else {
66             identifier = "PF";
67             outChannels = 3;
68         }
69         break;
70     }
71
72     os << identifier << "\n";
73
74     if (comment) {
75         os << "#" << comment << "\n";
76     }
77     os << width << " " << height << "\n";
78
79     if (channelType == TYPE_UNORM8) {
80         os << "255" << "\n";
81     }
82
83     const unsigned char *row;
84
85     if (channels == outChannels) {
86         /*
87          * Write whole pixel spans straight from the image buffer.
88          */
89
90         for (row = start(); row != end(); row += stride()) {
91             os.write((const char *)row, width*bytesPerPixel);
92         }
93     } else {
94         /*
95          * Need to add/remove channels, one pixel at a time.
96          */
97
98         unsigned char *tmp = new unsigned char[width*bytesPerPixel];
99
100         if (channelType == TYPE_UNORM8) {
101             /*
102              * Optimized path for 8bit unorms.
103              */
104
105             if (channels == 4) {
106                 for (row = start(); row != end(); row += stride()) {
107                     const uint32_t *src = (const uint32_t *)row;
108                     uint32_t *dst = (uint32_t *)tmp;
109                     unsigned x;
110                     for (x = 0; x + 4 <= width; x += 4) {
111                         /*
112                          * It's much faster to access dwords than bytes.
113                          *
114                          * FIXME: Big-endian version.
115                          */
116
117                         uint32_t rgba0 = *src++ & 0xffffff;
118                         uint32_t rgba1 = *src++ & 0xffffff;
119                         uint32_t rgba2 = *src++ & 0xffffff;
120                         uint32_t rgba3 = *src++ & 0xffffff;
121                         uint32_t rgb0 = rgba0
122                                       | (rgba1 << 24);
123                         uint32_t rgb1 = (rgba1 >> 8)
124                                       | (rgba2 << 16);
125                         uint32_t rgb2 = (rgba2 >> 16)
126                                       | (rgba3 << 8);
127                         *dst++ = rgb0;
128                         *dst++ = rgb1;
129                         *dst++ = rgb2;
130                     }
131                     for (; x < width; ++x) {
132                         tmp[x*3 + 0] = row[x*4 + 0];
133                         tmp[x*3 + 1] = row[x*4 + 1];
134                         tmp[x*3 + 2] = row[x*4 + 2];
135                     }
136                     os.write((const char *)tmp, width*3);
137                 }
138             } else if (channels == 2) {
139                 for (row = start(); row != end(); row += stride()) {
140                     const unsigned char *src = row;
141                     unsigned char *dst = tmp;
142                     for (unsigned x = 0; x < width; ++x) {
143                         *dst++ = *src++;
144                         *dst++ = *src++;
145                         *dst++ = 0;
146                     }
147                     os.write((const char *)tmp, width*3);
148                 }
149             } else {
150                 assert(0);
151             }
152         } else {
153             /*
154              * General path for float images.
155              */
156
157             assert(channelType == TYPE_FLOAT);
158
159             for (row = start(); row != end(); row += stride()) {
160                 const float *src = (const float *)row;
161                 float *dst = (float *)tmp;
162                 for (unsigned x = 0; x < width; ++x) {
163                     unsigned channel = 0;
164                     for (; channel < channels; ++channel) {
165                         *dst++ = *src++;
166                     }
167                     for (; channel < channels; ++channel) {
168                         *dst++ = 0;
169                     }
170                 }
171                 os.write((const char *)tmp, width*bytesPerPixel);
172             }
173         }
174
175         delete [] tmp;
176     }
177 }
178
179
180 bool
181 Image::writePNM(const char *filename, const char *comment) const
182 {
183     std::ofstream os(filename, std::ofstream::binary);
184     if (!os) {
185         return false;
186     }
187     writePNM(os, comment);
188     return true;
189 }
190
191
192 const char *
193 readPNMHeader(const char *buffer, size_t bufferSize, unsigned *channels, unsigned *width, unsigned *height)
194 {
195     *channels = 0;
196     *width = 0;
197     *height = 0;
198
199     const char *currentBuffer = buffer;
200     const char *nextBuffer;
201
202     // parse number of channels
203     int scannedChannels = sscanf(currentBuffer, "P%d\n", channels);
204     if (scannedChannels != 1) { // validate scanning of channels
205         // invalid channel line
206         return buffer;
207     }
208     // convert channel token to number of channels
209     *channels = (*channels == 5) ? 1 : 3;
210
211     // advance past channel line
212     nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
213     bufferSize -= nextBuffer - currentBuffer;
214     currentBuffer = nextBuffer;
215
216     // skip over optional comment
217     if (*currentBuffer == '#') {
218         // advance past comment line
219         nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
220         bufferSize -= nextBuffer - currentBuffer;
221         currentBuffer = nextBuffer;
222     }
223
224     // parse dimensions of image
225     int scannedDimensions = sscanf(currentBuffer, "%d %d\n", width, height);
226     if (scannedDimensions != 2) { // validate scanning of dimensions
227         // invalid dimension line
228         return buffer;
229     }
230
231     // advance past dimension line
232     nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
233     bufferSize -= nextBuffer - currentBuffer;
234     currentBuffer = nextBuffer;
235
236     // skip over "255\n" at end of header
237     nextBuffer = (const char *) memchr((const void *) currentBuffer, '\n', bufferSize) + 1;
238
239     // return start of image data
240     return nextBuffer;
241 }
242
243 } /* namespace image */