]> git.cworth.org Git - apitrace/blob - image.cpp
Bumple libpng source.
[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 void
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     stream.write((const char *)&bmfh, 14);
96     stream.write((const char *)&bmih, 40);
97
98     unsigned stride = width*4;
99
100     if (flipped) {
101         for (y = 0; y < height; ++y) {
102             const unsigned char *ptr = pixels + y * stride;
103             for (x = 0; x < width; ++x) {
104                 struct Pixel pixel;
105                 pixel.rgbRed   = ptr[x*4 + 0];
106                 pixel.rgbGreen = ptr[x*4 + 1];
107                 pixel.rgbBlue  = ptr[x*4 + 2];
108                 pixel.rgbAlpha = ptr[x*4 + 3];
109                 stream.write((const char *)&pixel, 4);
110             }
111         }
112     } else {
113         y = height;
114         while (y--) {
115             const unsigned char *ptr = pixels + y * stride;
116             for (x = 0; x < width; ++x) {
117                 struct Pixel pixel;
118                 pixel.rgbRed   = ptr[x*4 + 0];
119                 pixel.rgbGreen = ptr[x*4 + 1];
120                 pixel.rgbBlue  = ptr[x*4 + 2];
121                 pixel.rgbAlpha = ptr[x*4 + 3];
122                 stream.write((const char *)&pixel, 4);
123             }
124         }
125     }
126
127     stream.close();
128 }
129
130
131 } /* namespace Image */