]> git.cworth.org Git - apitrace/blob - image/image.hpp
857e0621c1386e5d9a72eb9dba6e9ff467a35322
[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 bytesPerPixel;
53
54     // Flipped vertically or not
55     bool flipped;
56
57     // Pixels in RGBA format
58     unsigned char *pixels;
59
60     inline Image(unsigned w, unsigned h, unsigned c = 4, bool f = false, ChannelType t = TYPE_UNORM8) :
61         width(w),
62         height(h),
63         channels(c),
64         channelType(t),
65         bytesPerPixel(channels * (t == TYPE_FLOAT ? 4 : 1)),
66         flipped(f),
67         pixels(new unsigned char[h*w*bytesPerPixel])
68     {}
69
70     inline ~Image() {
71         delete [] pixels;
72     }
73
74     // Absolute stride
75     inline unsigned
76     _stride() const {
77         return width*bytesPerPixel;
78     }
79
80     inline unsigned char *start(void) {
81         return flipped ? pixels + (height - 1)*_stride() : pixels;
82     }
83
84     inline const unsigned char *start(void) const {
85         return flipped ? pixels + (height - 1)*_stride() : pixels;
86     }
87
88     inline unsigned char *end(void) {
89         return flipped ? pixels - _stride() : pixels + height*_stride();
90     }
91
92     inline const unsigned char *end(void) const {
93         return flipped ? pixels - _stride() : pixels + height*_stride();
94     }
95
96     inline signed stride(void) const {
97         return flipped ? -(signed)_stride() : _stride();
98     }
99
100     bool
101     writeBMP(const char *filename) const;
102
103     void
104     writePNM(std::ostream &os, const char *comment = NULL) const;
105
106     bool
107     writePNM(const char *filename, const char *comment = NULL) const;
108
109     bool
110     writePNG(std::ostream &os) const;
111
112     bool
113     writePNG(const char *filename) const;
114
115     void
116     writeRAW(std::ostream &os) const;
117
118     bool
119     writeRAW(const char *filename) const;
120 };
121
122
123 Image *
124 readPNG(std::istream &is);
125
126 Image *
127 readPNG(const char *filename);
128
129
130 struct PNMInfo
131 {
132     unsigned width;
133     unsigned height;
134     unsigned channels;
135     ChannelType channelType;
136 };
137
138 const char *
139 readPNMHeader(const char *buffer, size_t size, PNMInfo &info);
140
141 Image *
142 readPNM(const char *buffer, size_t bufferSize);
143
144
145 } /* namespace image */
146
147
148 #endif /* _IMAGE_HPP_ */