]> git.cworth.org Git - apitrace/blob - image/image.hpp
glretrace: Always pass a format appropriate for the internalFormat when reading pixels.
[apitrace] / image / image.hpp
1 /**************************************************************************
2  *
3  * Copyright 2008-2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * Image I/O.
28  */
29
30 #ifndef _IMAGE_HPP_
31 #define _IMAGE_HPP_
32
33
34 #include <iostream>
35
36
37 namespace image {
38
39
40 enum ChannelType {
41     TYPE_UNORM8 = 0,
42     TYPE_FLOAT
43 };
44
45
46 class Image {
47 public:
48     unsigned width;
49     unsigned height;
50     unsigned channels;
51     ChannelType channelType;
52     unsigned bytesPerChannel;
53     unsigned bytesPerPixel;
54
55     // Flipped vertically or not
56     bool flipped;
57
58     // Pixels in RGBA format
59     unsigned char *pixels;
60
61     inline Image(unsigned w, unsigned h, unsigned c = 4, bool f = false, ChannelType t = TYPE_UNORM8) :
62         width(w),
63         height(h),
64         channels(c),
65         channelType(t),
66         bytesPerChannel(t == TYPE_FLOAT ? 4 : 1),
67         bytesPerPixel(channels * bytesPerChannel),
68         flipped(f),
69         pixels(new unsigned char[h*w*bytesPerPixel])
70     {}
71
72     inline ~Image() {
73         delete [] pixels;
74     }
75
76     // Absolute stride
77     inline unsigned
78     _stride() const {
79         return width*bytesPerPixel;
80     }
81
82     inline unsigned char *start(void) {
83         return flipped ? pixels + (height - 1)*_stride() : pixels;
84     }
85
86     inline const unsigned char *start(void) const {
87         return flipped ? pixels + (height - 1)*_stride() : pixels;
88     }
89
90     inline unsigned char *end(void) {
91         return flipped ? pixels - _stride() : pixels + height*_stride();
92     }
93
94     inline const unsigned char *end(void) const {
95         return flipped ? pixels - _stride() : pixels + height*_stride();
96     }
97
98     inline signed stride(void) const {
99         return flipped ? -(signed)_stride() : _stride();
100     }
101
102     bool
103     writeBMP(const char *filename) const;
104
105     void
106     writePNM(std::ostream &os, const char *comment = NULL) const;
107
108     bool
109     writePNM(const char *filename, const char *comment = NULL) const;
110
111     bool
112     writePNG(std::ostream &os) const;
113
114     bool
115     writePNG(const char *filename) const;
116
117     void
118     writeRAW(std::ostream &os) const;
119
120     bool
121     writeRAW(const char *filename) const;
122 };
123
124
125 Image *
126 readPNG(std::istream &is);
127
128 Image *
129 readPNG(const char *filename);
130
131
132 struct PNMInfo
133 {
134     unsigned width;
135     unsigned height;
136     unsigned channels;
137     ChannelType channelType;
138 };
139
140 const char *
141 readPNMHeader(const char *buffer, size_t size, PNMInfo &info);
142
143 Image *
144 readPNM(const char *buffer, size_t bufferSize);
145
146
147 } /* namespace image */
148
149
150 #endif /* _IMAGE_HPP_ */