]> git.cworth.org Git - apitrace/blob - image/image_bmp.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / image / image_bmp.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 <stdint.h>
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     assert(channels == 4);
72
73     struct FileHeader bmfh;
74     struct InfoHeader bmih;
75     unsigned x, y;
76
77     bmfh.bfType = 0x4d42;
78     bmfh.bfSize = 14 + 40 + height*width*4;
79     bmfh.bfReserved1 = 0;
80     bmfh.bfReserved2 = 0;
81     bmfh.bfOffBits = 14 + 40;
82
83     bmih.biSize = 40;
84     bmih.biWidth = width;
85     bmih.biHeight = height;
86     bmih.biPlanes = 1;
87     bmih.biBitCount = 32;
88     bmih.biCompression = 0;
89     bmih.biSizeImage = height*width*4;
90     bmih.biXPelsPerMeter = 0;
91     bmih.biYPelsPerMeter = 0;
92     bmih.biClrUsed = 0;
93     bmih.biClrImportant = 0;
94
95     std::ofstream stream(filename, std::ofstream::binary);
96
97     if (!stream) {
98         return false;
99     }
100
101     stream.write((const char *)&bmfh, 14);
102     stream.write((const char *)&bmih, 40);
103
104     unsigned stride = width*4;
105
106     if (flipped) {
107         for (y = 0; y < height; ++y) {
108             const unsigned char *ptr = pixels + 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         y = height;
120         while (y--) {
121             const unsigned char *ptr = pixels + y * stride;
122             for (x = 0; x < width; ++x) {
123                 struct Pixel pixel;
124                 pixel.rgbRed   = ptr[x*4 + 0];
125                 pixel.rgbGreen = ptr[x*4 + 1];
126                 pixel.rgbBlue  = ptr[x*4 + 2];
127                 pixel.rgbAlpha = ptr[x*4 + 3];
128                 stream.write((const char *)&pixel, 4);
129             }
130         }
131     }
132
133     stream.close();
134
135     return true;
136 }
137
138
139 } /* namespace image */