2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
24 #include "ifilesystem.h"
26 typedef unsigned char byte;
29 #include "bytestreamutils.h"
32 typedef unsigned char PaletteEntry[4];
36 unsigned long fileSize;
37 unsigned long reserved0;
38 unsigned long bitmapDataOffset;
39 unsigned long bitmapHeaderSize;
42 unsigned short planes;
43 unsigned short bitsPerPixel;
44 unsigned long compression;
45 unsigned long bitmapDataSize;
49 unsigned long importantColors;
50 PaletteEntry palette[256];
55 PaletteEntry* m_palette;
57 ReadPixel8(PaletteEntry* palette) : m_palette(palette)
60 void operator()(PointerInputStream& inputStream, byte*& pixbuf) const
63 inputStream.read(&palIndex, 1);
64 *pixbuf++ = m_palette[palIndex][2];
65 *pixbuf++ = m_palette[palIndex][1];
66 *pixbuf++ = m_palette[palIndex][0];
74 void operator()(PointerInputStream& inputStream, byte*& pixbuf) const
76 unsigned short shortPixel;
77 inputStream.read(reinterpret_cast<byte*>(&shortPixel), sizeof(unsigned short)); //!\todo Is this endian safe?
78 *pixbuf++ = static_cast<byte>(shortPixel & (31 << 10)) >> 7;
79 *pixbuf++ = static_cast<byte>(shortPixel & (31 << 5)) >> 2;
80 *pixbuf++ = static_cast<byte>(shortPixel & (31)) << 3;
88 void operator()(PointerInputStream& inputStream, byte*& pixbuf) const
91 inputStream.read(bgr, 3);
102 void operator()(PointerInputStream& inputStream, byte*& pixbuf) const
105 inputStream.read(bgra, 4);
113 template<typename ReadPixel>
114 void ReadBMP(PointerInputStream& inputStream, byte* bmpRGBA, int rows, int columns, ReadPixel readPixel)
116 for (int row = rows - 1; row >= 0; row--)
118 byte* pixbuf = bmpRGBA + row * columns * 4;
120 for (int column = 0; column < columns; column++)
122 readPixel(inputStream, pixbuf);
127 Image* LoadBMPBuff(PointerInputStream& inputStream, std::size_t length)
129 BMPHeader_t bmpHeader;
130 inputStream.read(reinterpret_cast<byte*>(bmpHeader.id), 2);
131 bmpHeader.fileSize = istream_read_uint32_le(inputStream);
132 bmpHeader.reserved0 = istream_read_uint32_le(inputStream);
133 bmpHeader.bitmapDataOffset = istream_read_uint32_le(inputStream);
134 bmpHeader.bitmapHeaderSize = istream_read_uint32_le(inputStream);
135 bmpHeader.width = istream_read_uint32_le(inputStream);
136 bmpHeader.height = istream_read_uint32_le(inputStream);
137 bmpHeader.planes = istream_read_uint16_le(inputStream);
138 bmpHeader.bitsPerPixel = istream_read_uint16_le(inputStream);
139 bmpHeader.compression = istream_read_uint32_le(inputStream);
140 bmpHeader.bitmapDataSize = istream_read_uint32_le(inputStream);
141 bmpHeader.hRes = istream_read_uint32_le(inputStream);
142 bmpHeader.vRes = istream_read_uint32_le(inputStream);
143 bmpHeader.colors = istream_read_uint32_le(inputStream);
144 bmpHeader.importantColors = istream_read_uint32_le(inputStream);
146 if (bmpHeader.bitsPerPixel == 8)
148 int paletteSize = bmpHeader.colors * 4;
149 inputStream.read(reinterpret_cast<byte*>(bmpHeader.palette), paletteSize);
152 if (bmpHeader.id[0] != 'B' && bmpHeader.id[1] != 'M')
154 globalErrorStream() << "LoadBMP: only Windows-style BMP files supported\n";
157 if (bmpHeader.fileSize != length)
159 globalErrorStream() << "LoadBMP: header size does not match file size (" << Unsigned(bmpHeader.fileSize) << " vs. " << Unsigned(length) << ")\n";
162 if (bmpHeader.compression != 0)
164 globalErrorStream() << "LoadBMP: only uncompressed BMP files supported\n";
167 if (bmpHeader.bitsPerPixel < 8)
169 globalErrorStream() << "LoadBMP: monochrome and 4-bit BMP files not supported\n";
173 int columns = bmpHeader.width;
174 int rows = bmpHeader.height;
178 RGBAImage* image = new RGBAImage(columns, rows);
180 switch(bmpHeader.bitsPerPixel)
183 ReadBMP(inputStream, image->getRGBAPixels(), rows, columns, ReadPixel8(bmpHeader.palette));
186 ReadBMP(inputStream, image->getRGBAPixels(), rows, columns, ReadPixel16());
189 ReadBMP(inputStream, image->getRGBAPixels(), rows, columns, ReadPixel24());
192 ReadBMP(inputStream, image->getRGBAPixels(), rows, columns, ReadPixel32());
195 globalErrorStream() << "LoadBMP: illegal pixel_size '" << bmpHeader.bitsPerPixel << "'\n";
202 Image* LoadBMP(ArchiveFile& file)
204 ScopedArchiveBuffer buffer(file);
205 PointerInputStream inputStream(buffer.buffer);
206 return LoadBMPBuff(inputStream, buffer.length);