Debian Codegraph @

plugins/image/bmp.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "bmp.h"
23
24 #include "ifilesystem.h"
25
26 typedef unsigned char byte;
27
28 #include "imagelib.h"
29 #include "bytestreamutils.h"
30
31
32 typedef unsigned char PaletteEntry[4];
33 typedef struct
34 {
35 char id[2];
36 unsigned long fileSize;
37 unsigned long reserved0;
38 unsigned long bitmapDataOffset;
39 unsigned long bitmapHeaderSize;
40 unsigned long width;
41 unsigned long height;
42 unsigned short planes;
43 unsigned short bitsPerPixel;
44 unsigned long compression;
45 unsigned long bitmapDataSize;
46 unsigned long hRes;
47 unsigned long vRes;
48 unsigned long colors;
49 unsigned long importantColors;
50 PaletteEntry palette[256];
51 } BMPHeader_t;
52
53 class ReadPixel8
54 {
55 PaletteEntry* m_palette;
56 public:
57 ReadPixel8( PaletteEntry* palette ) : m_palette( palette ){
58 }
59 void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
60 byte palIndex;
61 inputStream.read( &palIndex, 1 );
62 *pixbuf++ = m_palette[palIndex][2];
63 *pixbuf++ = m_palette[palIndex][1];
64 *pixbuf++ = m_palette[palIndex][0];
65 *pixbuf++ = 0xff;
66 }
67 };
68
69 class ReadPixel16
70 {
71 public:
72 void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
73 unsigned short shortPixel;
74 inputStream.read( reinterpret_cast<byte*>( &shortPixel ), sizeof( unsigned short ) ); //!\todo Is this endian safe?
75 *pixbuf++ = static_cast<byte>( shortPixel & ( 31 << 10 ) ) >> 7;
76 *pixbuf++ = static_cast<byte>( shortPixel & ( 31 << 5 ) ) >> 2;
77 *pixbuf++ = static_cast<byte>( shortPixel & ( 31 ) ) << 3;
78 *pixbuf++ = 0xff;
79 }
80 };
81
82 class ReadPixel24
83 {
84 public:
85 void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
86 byte bgr[3];
87 inputStream.read( bgr, 3 );
88 *pixbuf++ = bgr[2];
89 *pixbuf++ = bgr[1];
90 *pixbuf++ = bgr[0];
91 *pixbuf++ = 255;
92 }
93 };
94
95 class ReadPixel32
96 {
97 public:
98 void operator()( PointerInputStream& inputStream, byte*& pixbuf ) const {
99 byte bgra[4];
100 inputStream.read( bgra, 4 );
101 *pixbuf++ = bgra[2];
102 *pixbuf++ = bgra[1];
103 *pixbuf++ = bgra[0];
104 *pixbuf++ = bgra[3];
105 }
106 };
107
108 template<typename ReadPixel>
109 void ReadBMP( PointerInputStream& inputStream, byte* bmpRGBA, int rows, int columns, ReadPixel readPixel ){
110 for ( int row = rows - 1; row >= 0; row-- )
111 {
112 byte* pixbuf = bmpRGBA + row * columns * 4;
113
114 for ( int column = 0; column < columns; column++ )
115 {
116 readPixel( inputStream, pixbuf );
117 }
118 }
119 }
120
121 Image* LoadBMPBuff( PointerInputStream& inputStream, std::size_t length ){
122 BMPHeader_t bmpHeader;
123 inputStream.read( reinterpret_cast<byte*>( bmpHeader.id ), 2 );
124 bmpHeader.fileSize = istream_read_uint32_le( inputStream );
125 bmpHeader.reserved0 = istream_read_uint32_le( inputStream );
126 bmpHeader.bitmapDataOffset = istream_read_uint32_le( inputStream );
127 bmpHeader.bitmapHeaderSize = istream_read_uint32_le( inputStream );
128 bmpHeader.width = istream_read_uint32_le( inputStream );
129 bmpHeader.height = istream_read_uint32_le( inputStream );
130 bmpHeader.planes = istream_read_uint16_le( inputStream );
131 bmpHeader.bitsPerPixel = istream_read_uint16_le( inputStream );
132 bmpHeader.compression = istream_read_uint32_le( inputStream );
133 bmpHeader.bitmapDataSize = istream_read_uint32_le( inputStream );
134 bmpHeader.hRes = istream_read_uint32_le( inputStream );
135 bmpHeader.vRes = istream_read_uint32_le( inputStream );
136 bmpHeader.colors = istream_read_uint32_le( inputStream );
137 bmpHeader.importantColors = istream_read_uint32_le( inputStream );
138
139 if ( bmpHeader.bitsPerPixel == 8 ) {
140 int paletteSize = bmpHeader.colors * 4;
141 inputStream.read( reinterpret_cast<byte*>( bmpHeader.palette ), paletteSize );
142 }
143
144 if ( bmpHeader.id[0] != 'B' && bmpHeader.id[1] != 'M' ) {
145 globalErrorStream() << "LoadBMP: only Windows-style BMP files supported\n";
146 return 0;
147 }
148 if ( bmpHeader.fileSize != length ) {
149 globalErrorStream() << "LoadBMP: header size does not match file size (" << Unsigned( bmpHeader.fileSize ) << " vs. " << Unsigned( length ) << ")\n";
150 return 0;
151 }
152 if ( bmpHeader.compression != 0 ) {
153 globalErrorStream() << "LoadBMP: only uncompressed BMP files supported\n";
154 return 0;
155 }
156 if ( bmpHeader.bitsPerPixel < 8 ) {
157 globalErrorStream() << "LoadBMP: monochrome and 4-bit BMP files not supported\n";
158 return 0;
159 }
160
161 int columns = bmpHeader.width;
162 int rows = bmpHeader.height;
163 if ( rows < 0 ) {
164 rows = -rows;
165 }
166
167 RGBAImage* image = new RGBAImage( columns, rows );
168
169 switch ( bmpHeader.bitsPerPixel )
170 {
171 case 8:
172 ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel8( bmpHeader.palette ) );
173 break;
174 case 16:
175 ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel16() );
176 break;
177 case 24:
178 ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel24() );
179 break;
180 case 32:
181 ReadBMP( inputStream, image->getRGBAPixels(), rows, columns, ReadPixel32() );
182 break;
183 default:
184 globalErrorStream() << "LoadBMP: illegal pixel_size '" << bmpHeader.bitsPerPixel << "'\n";
185 image->release();
186 return 0;
187 }
188 return image;
189 }
190
191 Image* LoadBMP( ArchiveFile& file ){
192 ScopedArchiveBuffer buffer( file );
193 PointerInputStream inputStream( buffer.buffer );
194 return LoadBMPBuff( inputStream, buffer.length );
195 }