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