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

msvideo1.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  // Based off ffmpeg's msvideo.cpp
00024 
00025 #include "image/codecs/msvideo1.h"
00026 #include "common/stream.h"
00027 #include "common/textconsole.h"
00028 
00029 namespace Image {
00030 
00031 #define CHECK_STREAM_PTR(n) \
00032   if ((stream.pos() + n) > stream.size() ) { \
00033     warning ("MS Video-1: Stream out of bounds (%d >= %d) d%d", stream.pos() + n, stream.size(), n); \
00034     return; \
00035   }
00036 
00037 MSVideo1Decoder::MSVideo1Decoder(uint16 width, uint16 height, byte bitsPerPixel) : Codec() {
00038     _surface = new Graphics::Surface();
00039     _surface->create(width, height, (bitsPerPixel == 8) ? Graphics::PixelFormat::createFormatCLUT8() :
00040                                                           Graphics::PixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0));
00041 
00042     _bitsPerPixel = bitsPerPixel;
00043 }
00044 
00045 MSVideo1Decoder::~MSVideo1Decoder() {
00046     _surface->free();
00047     delete _surface;
00048 }
00049 
00050 void MSVideo1Decoder::decode8(Common::SeekableReadStream &stream) {
00051     byte colors[8];
00052     byte *pixels = (byte *)_surface->getPixels();
00053     uint16 stride = _surface->w;
00054 
00055     int skipBlocks = 0;
00056     uint16 blocks_wide = _surface->w / 4;
00057     uint16 blocks_high = _surface->h / 4;
00058     uint32 totalBlocks = blocks_wide * blocks_high;
00059     uint32 blockInc = 4;
00060     uint16 rowDec = stride + 4;
00061 
00062     for (uint16 block_y = blocks_high; block_y > 0; block_y--) {
00063         uint32 blockPtr = (block_y * 4 - 1) * stride;
00064         for (uint16 block_x = blocks_wide; block_x > 0; block_x--) {
00065             // check if this block should be skipped
00066             if (skipBlocks > 0) {
00067                 blockPtr += blockInc;
00068                 skipBlocks--;
00069                 totalBlocks--;
00070                 continue;
00071             }
00072 
00073             uint32 pixelPtr = blockPtr;
00074 
00075             /* get the next two bytes in the encoded data stream */
00076             CHECK_STREAM_PTR(2);
00077             byte byte_a = stream.readByte();
00078             byte byte_b = stream.readByte();
00079 
00080             /* check if the decode is finished */
00081             if (byte_a == 0 && byte_b == 0 && totalBlocks == 0) {
00082                 return;
00083             } else if ((byte_b & 0xFC) == 0x84) {
00084                 // skip code, but don't count the current block
00085                 skipBlocks = ((byte_b - 0x84) << 8) + byte_a - 1;
00086             } else if (byte_b < 0x80) {
00087                 // 2-color encoding
00088                 uint16 flags = (byte_b << 8) | byte_a;
00089 
00090                 CHECK_STREAM_PTR(2);
00091                 colors[0] = stream.readByte();
00092                 colors[1] = stream.readByte();
00093 
00094                 for (byte pixel_y = 0; pixel_y < 4; pixel_y++) {
00095                     for (byte pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00096                         pixels[pixelPtr++] = colors[(flags & 0x1) ^ 1];
00097                     pixelPtr -= rowDec;
00098                 }
00099             } else if (byte_b >= 0x90) {
00100                 // 8-color encoding
00101                 uint16 flags = (byte_b << 8) | byte_a;
00102 
00103                 CHECK_STREAM_PTR(8);
00104                 for (byte i = 0; i < 8; i++)
00105                     colors[i] = stream.readByte();
00106 
00107                 for (byte pixel_y = 0; pixel_y < 4; pixel_y++) {
00108                     for (byte pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00109                         pixels[pixelPtr++] = colors[((pixel_y & 0x2) << 1) + (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
00110                     pixelPtr -= rowDec;
00111                 }
00112             } else {
00113                 // 1-color encoding
00114                 colors[0] = byte_a;
00115 
00116                 for (byte pixel_y = 0; pixel_y < 4; pixel_y++) {
00117                     for (byte pixel_x = 0; pixel_x < 4; pixel_x++)
00118                         pixels[pixelPtr++] = colors[0];
00119                     pixelPtr -= rowDec;
00120                 }
00121             }
00122 
00123             blockPtr += blockInc;
00124             totalBlocks--;
00125         }
00126     }
00127 }
00128 
00129 void MSVideo1Decoder::decode16(Common::SeekableReadStream &stream) {
00130     /* decoding parameters */
00131     uint16 colors[8];
00132     uint16 *pixels = (uint16 *)_surface->getPixels();
00133     int32 stride = _surface->w;
00134 
00135     int32 skip_blocks = 0;
00136     int32 blocks_wide = _surface->w / 4;
00137     int32 blocks_high = _surface->h / 4;
00138     int32 total_blocks = blocks_wide * blocks_high;
00139     int32 block_inc = 4;
00140     int32 row_dec = stride + 4;
00141 
00142     for (int32 block_y = blocks_high; block_y > 0; block_y--) {
00143         int32 block_ptr = ((block_y * 4) - 1) * stride;
00144         for (int32 block_x = blocks_wide; block_x > 0; block_x--) {
00145             /* check if this block should be skipped */
00146             if (skip_blocks) {
00147                 block_ptr += block_inc;
00148                 skip_blocks--;
00149                 total_blocks--;
00150                 continue;
00151             }
00152 
00153             int32 pixel_ptr = block_ptr;
00154 
00155             /* get the next two bytes in the encoded data stream */
00156             CHECK_STREAM_PTR(2);
00157             byte byte_a = stream.readByte();
00158             byte byte_b = stream.readByte();
00159 
00160             /* check if the decode is finished */
00161             if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
00162                 return;
00163             } else if ((byte_b & 0xFC) == 0x84) {
00164                 /* skip code, but don't count the current block */
00165                 skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
00166             } else if (byte_b < 0x80) {
00167                 /* 2- or 8-color encoding modes */
00168                 uint16 flags = (byte_b << 8) | byte_a;
00169 
00170                 CHECK_STREAM_PTR(4);
00171                 colors[0] = stream.readUint16LE();
00172                 colors[1] = stream.readUint16LE();
00173 
00174                 if (colors[0] & 0x8000) {
00175                     /* 8-color encoding */
00176                     CHECK_STREAM_PTR(12);
00177                     colors[2] = stream.readUint16LE();
00178                     colors[3] = stream.readUint16LE();
00179                     colors[4] = stream.readUint16LE();
00180                     colors[5] = stream.readUint16LE();
00181                     colors[6] = stream.readUint16LE();
00182                     colors[7] = stream.readUint16LE();
00183 
00184                     for (int pixel_y = 0; pixel_y < 4; pixel_y++) {
00185                         for (int pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00186                             pixels[pixel_ptr++] =
00187                                 colors[((pixel_y & 0x2) << 1) +
00188                                     (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
00189                         pixel_ptr -= row_dec;
00190                     }
00191                 } else {
00192                     /* 2-color encoding */
00193                     for (int pixel_y = 0; pixel_y < 4; pixel_y++) {
00194                         for (int pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
00195                             pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
00196                         pixel_ptr -= row_dec;
00197                     }
00198                 }
00199             } else {
00200                 /* otherwise, it's a 1-color block */
00201                 colors[0] = (byte_b << 8) | byte_a;
00202 
00203                 for (int pixel_y = 0; pixel_y < 4; pixel_y++) {
00204                     for (int pixel_x = 0; pixel_x < 4; pixel_x++)
00205                         pixels[pixel_ptr++] = colors[0];
00206                     pixel_ptr -= row_dec;
00207                 }
00208             }
00209 
00210             block_ptr += block_inc;
00211             total_blocks--;
00212         }
00213     }
00214 }
00215 
00216 const Graphics::Surface *MSVideo1Decoder::decodeFrame(Common::SeekableReadStream &stream) {
00217     if (_bitsPerPixel == 8)
00218         decode8(stream);
00219     else
00220         decode16(stream);
00221 
00222     return _surface;
00223 }
00224 
00225 } // End of namespace Image


Generated on Sat Sep 14 2019 05:01:34 for ResidualVM by doxygen 1.7.1
curved edge   curved edge