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

bmp_raw.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/codecs/bmp_raw.h"
00024 
00025 #include "common/stream.h"
00026 #include "common/textconsole.h"
00027 #include "graphics/surface.h"
00028 
00029 namespace Image {
00030 
00031 BitmapRawDecoder::BitmapRawDecoder(int width, int height, int bitsPerPixel) : Codec(),
00032         _width(width), _height(height), _bitsPerPixel(bitsPerPixel) {
00033     _surface.create(_width, _height, getPixelFormat());
00034 }
00035 
00036 BitmapRawDecoder::~BitmapRawDecoder() {
00037     _surface.free();
00038 }
00039 
00040 const Graphics::Surface *BitmapRawDecoder::decodeFrame(Common::SeekableReadStream &stream) {
00041     Graphics::PixelFormat format = getPixelFormat();
00042 
00043     int srcPitch = _width * (_bitsPerPixel >> 3);
00044     int extraDataLength = (srcPitch % 4) ? 4 - (srcPitch % 4) : 0;
00045 
00046     if (_bitsPerPixel == 1) {
00047         srcPitch = (_width + 7) / 8;
00048         extraDataLength = (srcPitch % 2) ? 2 - (srcPitch % 2) : 0;
00049     }
00050 
00051     if (_bitsPerPixel == 1) {
00052         for (int i = 0; i < _height; i++) {
00053             byte *dst = (byte *)_surface.getBasePtr(0, i);
00054             for (int j = 0; j != _width;) {
00055                 byte color = stream.readByte();
00056                 for (int k = 0; k < 8; k++) {
00057                     *dst++ = (color & 0x80) ? 0x0f : 0x00;
00058                     color <<= 1;
00059                     j++;
00060                     if (j == _width) {
00061                         break;
00062                     }
00063                 }
00064             }
00065             stream.skip(extraDataLength);
00066         }
00067     } else if (_bitsPerPixel == 4) {
00068         for (int i = 0; i < _height; i++) {
00069             byte *dst = (byte *)_surface.getBasePtr(0, _height - i - 1);
00070             for (int j = 0; j < _width; j++) {
00071                 byte color = stream.readByte();
00072 
00073                 *dst++ = (color & 0xf0) >> 4;
00074                 j++;
00075 
00076                 if (j ==_width)
00077                     break;
00078 
00079                 *dst++ = color & 0x0f;
00080             }
00081 
00082             stream.skip(extraDataLength);
00083         }
00084     } else if (_bitsPerPixel == 8) {
00085         byte *dst = (byte *)_surface.getPixels();
00086 
00087         for (int i = 0; i < _height; i++) {
00088             stream.read(dst + (_height - i - 1) * _width, _width);
00089             stream.skip(extraDataLength);
00090         }
00091     } else if (_bitsPerPixel == 24) {
00092         byte *dst = (byte *)_surface.getBasePtr(0, _height - 1);
00093 
00094         for (int i = 0; i < _height; i++) {
00095             for (int j = 0; j < _width; j++) {
00096                 byte b = stream.readByte();
00097                 byte g = stream.readByte();
00098                 byte r = stream.readByte();
00099                 uint32 color = format.RGBToColor(r, g, b);
00100 
00101                 *((uint32 *)dst) = color;
00102                 dst += format.bytesPerPixel;
00103             }
00104 
00105             stream.skip(extraDataLength);
00106             dst -= _surface.pitch * 2;
00107         }
00108     } else { // 32 bpp
00109         byte *dst = (byte *)_surface.getBasePtr(0, _height - 1);
00110 
00111         for (int i = 0; i < _height; i++) {
00112             for (int j = 0; j < _width; j++) {
00113                 byte b = stream.readByte();
00114                 byte g = stream.readByte();
00115                 byte r = stream.readByte();
00116                 // Ignore the last byte, as in v3 it is unused
00117                 // and should thus NOT be used as alpha.
00118                 // ref: http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376%28v=vs.85%29.aspx
00119                 stream.readByte();
00120                 uint32 color = format.RGBToColor(r, g, b);
00121 
00122                 *((uint32 *)dst) = color;
00123                 dst += format.bytesPerPixel;
00124             }
00125 
00126             stream.skip(extraDataLength);
00127             dst -= _surface.pitch * 2;
00128         }
00129     }
00130 
00131     return &_surface;
00132 }
00133 
00134 Graphics::PixelFormat BitmapRawDecoder::getPixelFormat() const {
00135     switch (_bitsPerPixel) {
00136     case 1:
00137     case 4:
00138     case 8:
00139         return Graphics::PixelFormat::createFormatCLUT8();
00140     case 24:
00141     case 32:
00142         return Graphics::PixelFormat(4, 8, 8, 8, 8, 8, 16, 24, 0);
00143     }
00144 
00145     error("Unhandled BMP raw %dbpp", _bitsPerPixel);
00146 }
00147 
00148 } // End of namespace Image


Generated on Sat Mar 16 2019 05:01:20 for ResidualVM by doxygen 1.7.1
curved edge   curved edge