]> git.cworth.org Git - apitrace/blob - bmp.hpp
Add brief comments.
[apitrace] / bmp.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  * BMP image writing.
28  */
29
30 #ifndef _BMP_HPP_
31 #define _BMP_HPP_
32
33
34 #include <fstream>
35
36
37 namespace BMP {
38
39 #pragma pack(push,2)
40 struct FileHeader {
41     uint16_t bfType;
42     uint32_t bfSize;
43     uint16_t bfReserved1;
44     uint16_t bfReserved2;
45     uint32_t bfOffBits;
46 };
47 #pragma pack(pop)
48
49 struct InfoHeader {
50     uint32_t biSize;
51     int32_t biWidth;
52     int32_t biHeight;
53     uint16_t biPlanes;
54     uint16_t biBitCount;
55     uint32_t biCompression;
56     uint32_t biSizeImage;
57     int32_t biXPelsPerMeter;
58     int32_t biYPelsPerMeter;
59     uint32_t biClrUsed;
60     uint32_t biClrImportant;
61 };
62
63 struct Pixel {
64     uint8_t rgbBlue;
65     uint8_t rgbGreen;
66     uint8_t rgbRed;
67     uint8_t rgbAlpha;
68 };
69
70
71 static inline void
72 write(const char *filename,
73       const unsigned char *rgba, 
74       unsigned width, unsigned height,
75       unsigned stride,
76       bool flip = false)
77 {
78     struct FileHeader bmfh;
79     struct InfoHeader bmih;
80     unsigned x, y;
81
82     bmfh.bfType = 0x4d42;
83     bmfh.bfSize = 14 + 40 + height*width*4;
84     bmfh.bfReserved1 = 0;
85     bmfh.bfReserved2 = 0;
86     bmfh.bfOffBits = 14 + 40;
87
88     bmih.biSize = 40;
89     bmih.biWidth = width;
90     bmih.biHeight = height;
91     bmih.biPlanes = 1;
92     bmih.biBitCount = 32;
93     bmih.biCompression = 0;
94     bmih.biSizeImage = height*width*4;
95     bmih.biXPelsPerMeter = 0;
96     bmih.biYPelsPerMeter = 0;
97     bmih.biClrUsed = 0;
98     bmih.biClrImportant = 0;
99
100     std::ofstream stream(filename, std::ofstream::binary);
101
102     stream.write((const char *)&bmfh, 14);
103     stream.write((const char *)&bmih, 40);
104
105     if (flip) {
106         y = height;
107         while (y--) {
108             const unsigned char *ptr = rgba + y * stride;
109             for (x = 0; x < width; ++x) {
110                 struct Pixel pixel;
111                 pixel.rgbRed   = ptr[x*4 + 0];
112                 pixel.rgbGreen = ptr[x*4 + 1];
113                 pixel.rgbBlue  = ptr[x*4 + 2];
114                 pixel.rgbAlpha = ptr[x*4 + 3];
115                 stream.write((const char *)&pixel, 4);
116             }
117         }
118     } else {
119         for (y = 0; y < height; ++y) {
120             const unsigned char *ptr = rgba + y * stride;
121             for (x = 0; x < width; ++x) {
122                 struct Pixel pixel;
123                 pixel.rgbRed   = ptr[x*4 + 0];
124                 pixel.rgbGreen = ptr[x*4 + 1];
125                 pixel.rgbBlue  = ptr[x*4 + 2];
126                 pixel.rgbAlpha = ptr[x*4 + 3];
127                 stream.write((const char *)&pixel, 4);
128             }
129         }
130     }
131
132     stream.close();
133 }
134
135
136 } /* namespace BMP */
137
138
139 #endif /* _BMP_HPP_ */