ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

bmp.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "image/bmp.h"
00024 
00025 #include "common/stream.h"
00026 #include "common/substream.h"
00027 #include "common/textconsole.h"
00028 #include "graphics/pixelformat.h"
00029 #include "graphics/surface.h"
00030 #include "image/codecs/codec.h"
00031 
00032 namespace Image {
00033 
00034 BitmapDecoder::BitmapDecoder() {
00035     _surface = 0;
00036     _palette = 0;
00037     _paletteColorCount = 0;
00038     _codec = 0;
00039 }
00040 
00041 BitmapDecoder::~BitmapDecoder() {
00042     destroy();
00043 }
00044 
00045 void BitmapDecoder::destroy() {
00046     _surface = 0;
00047 
00048     delete[] _palette;
00049     _palette = 0;
00050 
00051     _paletteColorCount = 0;
00052 
00053     delete _codec;
00054     _codec = 0;
00055 }
00056 
00057 bool BitmapDecoder::loadStream(Common::SeekableReadStream &stream) {
00058     destroy();
00059 
00060     if (stream.readByte() != 'B')
00061         return false;
00062 
00063     if (stream.readByte() != 'M')
00064         return false;
00065 
00066     /* uint32 fileSize = */ stream.readUint32LE();
00067     /* uint16 res1 = */ stream.readUint16LE();
00068     /* uint16 res2 = */ stream.readUint16LE();
00069     uint32 imageOffset = stream.readUint32LE();
00070 
00071     uint32 infoSize = stream.readUint32LE();
00072     if (infoSize != 40) {
00073         warning("Only Windows v3 bitmaps are supported");
00074         return false;
00075     }
00076 
00077     uint32 width = stream.readUint32LE();
00078     int32 height = stream.readSint32LE();
00079 
00080     if (width == 0 || height == 0)
00081         return false;
00082 
00083     if (height < 0) {
00084         warning("Right-side up bitmaps not supported");
00085         return false;
00086     }
00087 
00088     /* uint16 planes = */ stream.readUint16LE();
00089     uint16 bitsPerPixel = stream.readUint16LE();
00090 
00091     if (bitsPerPixel != 8 && bitsPerPixel != 24 && bitsPerPixel != 32) {
00092         warning("%dbpp bitmaps not supported", bitsPerPixel);
00093         return false;
00094     }
00095 
00096     uint32 compression = stream.readUint32BE();
00097     uint32 imageSize = stream.readUint32LE();
00098     /* uint32 pixelsPerMeterX = */ stream.readUint32LE();
00099     /* uint32 pixelsPerMeterY = */ stream.readUint32LE();
00100     _paletteColorCount = stream.readUint32LE();
00101     /* uint32 colorsImportant = */ stream.readUint32LE();
00102 
00103     if (bitsPerPixel == 8) {
00104         if (_paletteColorCount == 0)
00105             _paletteColorCount = 256;
00106 
00107         // Read the palette
00108         _palette = new byte[_paletteColorCount * 3];
00109         for (uint16 i = 0; i < _paletteColorCount; i++) {
00110             _palette[i * 3 + 2] = stream.readByte();
00111             _palette[i * 3 + 1] = stream.readByte();
00112             _palette[i * 3 + 0] = stream.readByte();
00113             stream.readByte();
00114         }
00115     }
00116 
00117     // Create the codec (it will warn about unhandled compression)
00118     _codec = createBitmapCodec(compression, width, height, bitsPerPixel);
00119     if (!_codec)
00120         return false;
00121 
00122     // If the image size is zero, set it to the rest of the stream.
00123     if (imageSize == 0)
00124         imageSize = stream.size() - imageOffset;
00125 
00126     // Grab the frame data
00127     Common::SeekableSubReadStream subStream(&stream, imageOffset, imageOffset + imageSize);
00128 
00129     // We only support raw bitmaps for now
00130     _surface = _codec->decodeFrame(subStream);
00131 
00132     return true;
00133 }
00134 
00135 bool writeBMP(Common::WriteStream &out, const Graphics::Surface &input, const bool bottomUp) {
00136 #ifdef SCUMM_LITTLE_ENDIAN
00137     const Graphics::PixelFormat requiredFormat_3byte(3, 8, 8, 8, 0, 16, 8, 0, 0);
00138 #else
00139     const Graphics::PixelFormat requiredFormat_3byte(3, 8, 8, 8, 0, 0, 8, 16, 0);
00140 #endif
00141 
00142     Graphics::Surface *tmp = NULL;
00143     const Graphics::Surface *surface;
00144 
00145     if (input.format == requiredFormat_3byte) {
00146         surface = &input;
00147     } else {
00148         surface = tmp = input.convertTo(requiredFormat_3byte);
00149     }
00150 
00151     int dstPitch = surface->w * 3;
00152     int extraDataLength = (dstPitch % 4) ? 4 - (dstPitch % 4) : 0;
00153     int padding = 0;
00154 
00155     out.writeByte('B');
00156     out.writeByte('M');
00157     out.writeUint32LE(surface->h * dstPitch + 54);
00158     out.writeUint32LE(0);
00159     out.writeUint32LE(54);
00160     out.writeUint32LE(40);
00161     out.writeUint32LE(surface->w);
00162     out.writeUint32LE(surface->h);
00163     out.writeUint16LE(1);
00164     out.writeUint16LE(24);
00165     out.writeUint32LE(0);
00166     out.writeUint32LE(0);
00167     out.writeUint32LE(0);
00168     out.writeUint32LE(0);
00169     out.writeUint32LE(0);
00170     out.writeUint32LE(0);
00171 
00172 
00173     if (bottomUp) {
00174         for (uint y = 0; y < surface->h; ++y) {
00175             out.write((const void *)surface->getBasePtr(0, y), dstPitch);
00176             out.write(&padding, extraDataLength);
00177         }
00178     } else {
00179         for (uint y = surface->h; y-- > 0;) {
00180             out.write((const void *)surface->getBasePtr(0, y), dstPitch);
00181             out.write(&padding, extraDataLength);
00182         }
00183     }
00184 
00185     // free tmp surface
00186     if (tmp) {
00187         tmp->free();
00188         delete tmp;
00189     }
00190 
00191     return true;
00192 }
00193 
00194 } // End of namespace Image


Generated on Sat Oct 12 2019 05:01:10 for ResidualVM by doxygen 1.7.1
curved edge   curved edge