diff options
author | Eugene Sandulenko | 2010-08-06 13:13:25 +0000 |
---|---|---|
committer | Eugene Sandulenko | 2010-10-12 22:35:55 +0000 |
commit | 47904bc7b2992189bb554833f00a79ff0fea9fb8 (patch) | |
tree | 1cec51758c6741b970bd064fafee77607b9f884f /engines/sword25/gfx/image/vectorimage.cpp | |
parent | ca17def625154e5f758b797e4fc48c76b0566320 (diff) | |
download | scummvm-rg350-47904bc7b2992189bb554833f00a79ff0fea9fb8.tar.gz scummvm-rg350-47904bc7b2992189bb554833f00a79ff0fea9fb8.tar.bz2 scummvm-rg350-47904bc7b2992189bb554833f00a79ff0fea9fb8.zip |
SWORD25: Mass-astyle.
svn-id: r53222
Diffstat (limited to 'engines/sword25/gfx/image/vectorimage.cpp')
-rw-r--r-- | engines/sword25/gfx/image/vectorimage.cpp | 376 |
1 files changed, 162 insertions, 214 deletions
diff --git a/engines/sword25/gfx/image/vectorimage.cpp b/engines/sword25/gfx/image/vectorimage.cpp index 5c7a5231a8..a8c84ee6b3 100644 --- a/engines/sword25/gfx/image/vectorimage.cpp +++ b/engines/sword25/gfx/image/vectorimage.cpp @@ -23,7 +23,7 @@ * */ -/* +/* * This code is based on Broken Sword 2.5 engine * * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer @@ -68,111 +68,94 @@ typedef int16_t s32; // Diese Klasse ist speziell dafür ausgestattet. // ----------------------------------------------------------------------------- -class BS_VectorImage::SWFBitStream -{ +class BS_VectorImage::SWFBitStream { public: - SWFBitStream(const unsigned char * pData, unsigned int DataSize) : - m_Pos(pData), m_End(pData + DataSize), m_WordMask(0) - {} - - inline u32 GetBits(unsigned int BitCount) - { - if (BitCount == 0 || BitCount > 32) - { - throw(runtime_error("SWFBitStream::GetBits() must read at least 1 and at most 32 bits at a time")); - } - - u32 value = 0; - while (BitCount) - { - if (m_WordMask == 0) FlushByte(); - - value <<= 1; - value |= ((m_Word & m_WordMask) != 0) ? 1 : 0; - m_WordMask >>= 1; - - --BitCount; - } - - return value; - } - - inline s32 GetSignedBits(unsigned int BitCount) - { - // Bits einlesen - u32 Temp = GetBits(BitCount); - - // Falls das Sign-Bit gesetzt ist, den Rest des Rückgabewertes mit 1-Bits auffüllen (Sign Extension) - if (Temp & 1 << (BitCount - 1)) - return (0xffffffff << BitCount) | Temp; - else - return Temp; - } - - inline u32 GetU32() - { - u32 Byte1 = GetU8(); - u32 Byte2 = GetU8(); - u32 Byte3 = GetU8(); - u32 Byte4 = GetU8(); - - return Byte1 | (Byte2 << 8) | (Byte3 << 16) | (Byte4 << 24); - } - - inline u16 GetU16() - { - u32 Byte1 = GetU8(); - u32 Byte2 = GetU8(); - - return Byte1 | (Byte2 << 8); - } - - inline u8 GetU8() - { - FlushByte(); - u8 Value = m_Word; - m_WordMask = 0; - FlushByte(); - - return Value; - } - - inline void FlushByte() - { - if (m_WordMask != 128) - { - if (m_Pos >= m_End) - { - throw(runtime_error("Attempted to read past end of file")); - } - else - { - m_Word = *m_Pos++; - m_WordMask = 128; - } - } - } - - inline void SkipBytes(unsigned int SkipLength) - { - FlushByte(); - if (m_Pos + SkipLength >= m_End) - { - throw(runtime_error("Attempted to read past end of file")); - } - else - { - m_Pos += SkipLength; - m_Word = *(m_Pos - 1); - } - } + SWFBitStream(const unsigned char *pData, unsigned int DataSize) : + m_Pos(pData), m_End(pData + DataSize), m_WordMask(0) + {} + + inline u32 GetBits(unsigned int BitCount) { + if (BitCount == 0 || BitCount > 32) { + throw(runtime_error("SWFBitStream::GetBits() must read at least 1 and at most 32 bits at a time")); + } + + u32 value = 0; + while (BitCount) { + if (m_WordMask == 0) FlushByte(); + + value <<= 1; + value |= ((m_Word & m_WordMask) != 0) ? 1 : 0; + m_WordMask >>= 1; + + --BitCount; + } + + return value; + } + + inline s32 GetSignedBits(unsigned int BitCount) { + // Bits einlesen + u32 Temp = GetBits(BitCount); + + // Falls das Sign-Bit gesetzt ist, den Rest des Rückgabewertes mit 1-Bits auffüllen (Sign Extension) + if (Temp & 1 << (BitCount - 1)) + return (0xffffffff << BitCount) | Temp; + else + return Temp; + } + + inline u32 GetU32() { + u32 Byte1 = GetU8(); + u32 Byte2 = GetU8(); + u32 Byte3 = GetU8(); + u32 Byte4 = GetU8(); + + return Byte1 | (Byte2 << 8) | (Byte3 << 16) | (Byte4 << 24); + } + + inline u16 GetU16() { + u32 Byte1 = GetU8(); + u32 Byte2 = GetU8(); + + return Byte1 | (Byte2 << 8); + } + + inline u8 GetU8() { + FlushByte(); + u8 Value = m_Word; + m_WordMask = 0; + FlushByte(); + + return Value; + } + + inline void FlushByte() { + if (m_WordMask != 128) { + if (m_Pos >= m_End) { + throw(runtime_error("Attempted to read past end of file")); + } else { + m_Word = *m_Pos++; + m_WordMask = 128; + } + } + } + + inline void SkipBytes(unsigned int SkipLength) { + FlushByte(); + if (m_Pos + SkipLength >= m_End) { + throw(runtime_error("Attempted to read past end of file")); + } else { + m_Pos += SkipLength; + m_Word = *(m_Pos - 1); + } + } private: - const unsigned char * m_Pos; - const unsigned char * m_End; + const unsigned char *m_Pos; + const unsigned char *m_End; - u8 m_Word; - unsigned int m_WordMask; + u8 m_Word; + unsigned int m_WordMask; }; @@ -180,69 +163,66 @@ private: // Konstanten und Hilfsfunktionen // ----------------------------------------------------------------------------- -namespace -{ - // ----------------------------------------------------------------------------- - // Konstanten - // ----------------------------------------------------------------------------- +namespace { +// ----------------------------------------------------------------------------- +// Konstanten +// ----------------------------------------------------------------------------- - const u32 MAX_ACCEPTED_FLASH_VERSION = 3; // Die höchste Flash-Dateiversion, die vom Lader akzeptiert wird +const u32 MAX_ACCEPTED_FLASH_VERSION = 3; // Die höchste Flash-Dateiversion, die vom Lader akzeptiert wird - // ----------------------------------------------------------------------------- - // Konvertiert SWF-Rechteckdaten in einem Bitstrom in BS_Rect-Objekte - // ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// Konvertiert SWF-Rechteckdaten in einem Bitstrom in BS_Rect-Objekte +// ----------------------------------------------------------------------------- - BS_Rect FlashRectToBSRect(BS_VectorImage::SWFBitStream & bs) - { - bs.FlushByte(); +BS_Rect FlashRectToBSRect(BS_VectorImage::SWFBitStream &bs) { + bs.FlushByte(); - // Feststellen mit wie vielen Bits die einzelnen Komponenten kodiert sind - u32 BitsPerValue = bs.GetBits(5); + // Feststellen mit wie vielen Bits die einzelnen Komponenten kodiert sind + u32 BitsPerValue = bs.GetBits(5); - // Die einzelnen Komponenten einlesen - s32 XMin = bs.GetSignedBits(BitsPerValue); - s32 XMax = bs.GetSignedBits(BitsPerValue); - s32 YMin = bs.GetSignedBits(BitsPerValue); - s32 YMax = bs.GetSignedBits(BitsPerValue); + // Die einzelnen Komponenten einlesen + s32 XMin = bs.GetSignedBits(BitsPerValue); + s32 XMax = bs.GetSignedBits(BitsPerValue); + s32 YMin = bs.GetSignedBits(BitsPerValue); + s32 YMax = bs.GetSignedBits(BitsPerValue); - return BS_Rect(XMin, YMin, XMax + 1, YMax + 1); - } + return BS_Rect(XMin, YMin, XMax + 1, YMax + 1); +} - // ----------------------------------------------------------------------------- - // Konvertiert SWF-Farben in AntiGrain Farben - // ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// Konvertiert SWF-Farben in AntiGrain Farben +// ----------------------------------------------------------------------------- - agg::rgba8 FlashColorToAGGRGBA8(unsigned int FlashColor) - { - agg::rgba8 ResultColor((FlashColor >> 16) & 0xff, (FlashColor >> 8) & 0xff, FlashColor & 0xff, FlashColor >> 24); - ResultColor.premultiply(); - return ResultColor; - } +agg::rgba8 FlashColorToAGGRGBA8(unsigned int FlashColor) { + agg::rgba8 ResultColor((FlashColor >> 16) & 0xff, (FlashColor >> 8) & 0xff, FlashColor & 0xff, FlashColor >> 24); + ResultColor.premultiply(); + return ResultColor; +} - // ----------------------------------------------------------------------------- - // Berechnet die Bounding-Box eines BS_VectorImageElement - // ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// Berechnet die Bounding-Box eines BS_VectorImageElement +// ----------------------------------------------------------------------------- + +struct CBBGetId { + CBBGetId(const BS_VectorImageElement &VectorImageElement_) : VectorImageElement(VectorImageElement_) {} + unsigned operator [](unsigned i) const { + return VectorImageElement.GetPathInfo(i).GetID(); + } + const BS_VectorImageElement &VectorImageElement; +}; - struct CBBGetId - { - CBBGetId(const BS_VectorImageElement & VectorImageElement_) : VectorImageElement(VectorImageElement_) {} - unsigned operator [] (unsigned i) const { return VectorImageElement.GetPathInfo(i).GetID(); } - const BS_VectorImageElement & VectorImageElement; - }; +BS_Rect CalculateBoundingBox(const BS_VectorImageElement &VectorImageElement) { + agg::path_storage Path = VectorImageElement.GetPaths(); + CBBGetId IdSource(VectorImageElement); - BS_Rect CalculateBoundingBox(const BS_VectorImageElement & VectorImageElement) - { - agg::path_storage Path = VectorImageElement.GetPaths(); - CBBGetId IdSource(VectorImageElement); + double x1, x2, y1, y2; + agg::bounding_rect(Path, IdSource, 0, VectorImageElement.GetPathCount(), &x1, &y1, &x2, &y2); - double x1, x2, y1, y2; - agg::bounding_rect(Path, IdSource, 0, VectorImageElement.GetPathCount(), &x1, &y1, &x2, &y2); - - return BS_Rect(static_cast<int>(x1), static_cast<int>(y1), static_cast<int>(x2) + 1, static_cast<int>(y2) + 1); - } + return BS_Rect(static_cast<int>(x1), static_cast<int>(y1), static_cast<int>(x2) + 1, static_cast<int>(y2) + 1); +} } @@ -250,41 +230,36 @@ namespace // Konstruktion // ----------------------------------------------------------------------------- -BS_VectorImage::BS_VectorImage(const unsigned char * pFileData, unsigned int FileSize, bool & Success) -{ +BS_VectorImage::BS_VectorImage(const unsigned char *pFileData, unsigned int FileSize, bool &Success) { Success = false; // Bitstream-Objekt erzeugen // Im Folgenden werden die Dateidaten aus diesem ausgelesen. SWFBitStream bs(pFileData, FileSize); - try - { + try { // SWF-Signatur überprüfen u32 Signature[3]; Signature[0] = bs.GetU8(); Signature[1] = bs.GetU8(); Signature[2] = bs.GetU8(); if (Signature[0] != 'F' || - Signature[1] != 'W' || - Signature[2] != 'S') - { + Signature[1] != 'W' || + Signature[2] != 'S') { BS_LOG_ERRORLN("File is not a valid SWF-file"); return; } // Versionsangabe überprüfen u32 Version = bs.GetU8(); - if (Version > MAX_ACCEPTED_FLASH_VERSION) - { + if (Version > MAX_ACCEPTED_FLASH_VERSION) { BS_LOG_ERRORLN("File is of version %d. Highest accepted version is %d.", Version, MAX_ACCEPTED_FLASH_VERSION); return; } // Dateigröße auslesen und mit der tatsächlichen Größe vergleichen u32 StoredFileSize = bs.GetU32(); - if (StoredFileSize != FileSize) - { + if (StoredFileSize != FileSize) { BS_LOG_ERRORLN("File is not a valid SWF-file"); return; } @@ -299,8 +274,7 @@ BS_VectorImage::BS_VectorImage(const unsigned char * pFileData, unsigned int Fil // Tags parsen // Da wir uns nur für das erste DefineShape-Tag interessieren bool KeepParsing = true; - while (KeepParsing) - { + while (KeepParsing) { // Tags beginnen immer an Bytegrenzen bs.FlushByte(); @@ -310,8 +284,7 @@ BS_VectorImage::BS_VectorImage(const unsigned char * pFileData, unsigned int Fil u32 TagLength = TagTypeAndLength & 0x3f; if (TagLength == 0x3f) TagLength = bs.GetU32(); - switch (TagType) - { + switch (TagType) { case 2: // DefineShape Success = ParseDefineShape(2, bs); @@ -330,8 +303,7 @@ BS_VectorImage::BS_VectorImage(const unsigned char * pFileData, unsigned int Fil } } - catch (runtime_error & e) - { + catch (runtime_error &e) { // Fehler loggen und Funktion verlassen // Success ist somit "false" und signalisiert dem Programmierer, dass die Konstruktion fehlgeschlagen ist. BS_LOG_ERRORLN("The following exception occured while parsing a SWF-file: %s", e.what()); @@ -345,8 +317,7 @@ BS_VectorImage::BS_VectorImage(const unsigned char * pFileData, unsigned int Fil // ----------------------------------------------------------------------------- -bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) -{ +bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream &bs) { u32 ShapeID = bs.GetU16(); // Bounding Box auslesen @@ -368,13 +339,11 @@ bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) // ------------------ bool EndOfShapeDiscovered = false; - while (!EndOfShapeDiscovered) - { + while (!EndOfShapeDiscovered) { u32 TypeFlag = bs.GetBits(1); // Non-Edge Record - if (TypeFlag == 0) - { + if (TypeFlag == 0) { // Feststellen welche Parameter gesetzt werden u32 StateNewStyles = bs.GetBits(1); u32 StateLineStyle = bs.GetBits(1); @@ -386,43 +355,37 @@ bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) if (!StateNewStyles && !StateLineStyle && !StateFillStyle0 && !StateFillStyle1 && !StateMoveTo) EndOfShapeDiscovered = true; // Parameter dekodieren - else - { + else { s32 MoveDeltaX = 0; s32 MoveDeltaY = 0; - if (StateMoveTo) - { + if (StateMoveTo) { u32 MoveToBits = bs.GetBits(5); MoveDeltaX = bs.GetSignedBits(MoveToBits); MoveDeltaY = bs.GetSignedBits(MoveToBits); } - if (StateFillStyle0) - { + if (StateFillStyle0) { if (NumFillBits > 0) FillStyle0 = bs.GetBits(NumFillBits); else FillStyle0 = 0; } - if (StateFillStyle1) - { + if (StateFillStyle1) { if (NumFillBits > 0) FillStyle1 = bs.GetBits(NumFillBits); else FillStyle1 = 0; } - if (StateLineStyle) - { + if (StateLineStyle) { if (NumLineBits) LineStyle = bs.GetBits(NumLineBits); else NumLineBits = 0; } - if (StateNewStyles) - { + if (StateNewStyles) { // An dieser Stelle werden in Flash die alten Style-Definitionen verworfen und mit den neuen überschrieben. // Es wird ein neues Element begonnen. m_Elements.resize(m_Elements.size() + 1); @@ -430,8 +393,7 @@ bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) } // Ein neuen Pfad erzeugen, es sei denn, es wurden nur neue Styles definiert - if (StateLineStyle || StateFillStyle0 || StateFillStyle1 || StateMoveTo) - { + if (StateLineStyle || StateFillStyle0 || StateFillStyle1 || StateMoveTo) { // Letzte Zeichenposition merken, beim Aufruf von start_new_path() wird die Zeichenpostionen auf 0, 0 zurückgesetzt double LastX = m_Elements.back().m_Paths.last_x(); double LastY = m_Elements.back().m_Paths.last_y(); @@ -449,14 +411,12 @@ bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) } } // Edge Record - else - { + else { u32 EdgeFlag = bs.GetBits(1); u32 NumBits = bs.GetBits(4) + 2; // Curved edge - if (EdgeFlag == 0) - { + if (EdgeFlag == 0) { s32 ControlDeltaX = bs.GetSignedBits(NumBits); s32 ControlDeltaY = bs.GetSignedBits(NumBits); s32 AnchorDeltaX = bs.GetSignedBits(NumBits); @@ -469,19 +429,15 @@ bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) m_Elements.back().m_Paths.curve3(ControlX, ControlY, AnchorX, AnchorY); } // Staight edge - else - { + else { s32 DeltaX = 0; s32 DeltaY = 0; u32 GeneralLineFlag = bs.GetBits(1); - if (GeneralLineFlag) - { + if (GeneralLineFlag) { DeltaX = bs.GetSignedBits(NumBits); DeltaY = bs.GetSignedBits(NumBits); - } - else - { + } else { u32 VertLineFlag = bs.GetBits(1); if (VertLineFlag) DeltaY = bs.GetSignedBits(NumBits); @@ -504,8 +460,7 @@ bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream & bs) // ----------------------------------------------------------------------------- -bool BS_VectorImage::ParseStyles(unsigned int ShapeType, SWFBitStream & bs, unsigned int & NumFillBits, unsigned int & NumLineBits) -{ +bool BS_VectorImage::ParseStyles(unsigned int ShapeType, SWFBitStream &bs, unsigned int &NumFillBits, unsigned int &NumLineBits) { bs.FlushByte(); // Fillstyles parsen @@ -518,15 +473,12 @@ bool BS_VectorImage::ParseStyles(unsigned int ShapeType, SWFBitStream & bs, unsi // Alle Fillstyles einlesen, falls ein Fillstyle mit Typ != 0 gefunden wird, wird das Parsen abgebrochen. // Es wird nur "solid fill" (Typ 0) unterstützt. m_Elements.back().m_FillStyles.reserve(FillStyleCount); - for (unsigned int i = 0; i < FillStyleCount; ++i) - { + for (unsigned int i = 0; i < FillStyleCount; ++i) { u8 Type = bs.GetU8(); u32 Color; - if (ShapeType == 3) - { + if (ShapeType == 3) { Color = (bs.GetU8() << 16) | (bs.GetU8() << 8) | bs.GetU8() | (bs.GetU8() << 24); - } - else + } else Color = bs.GetBits(24) | (0xff << 24); if (Type != 0) return false; @@ -542,8 +494,7 @@ bool BS_VectorImage::ParseStyles(unsigned int ShapeType, SWFBitStream & bs, unsi // Alle Linestyles einlesen m_Elements.back().m_LineStyles.reserve(LineStyleCount); - for (unsigned int i = 0; i < LineStyleCount; ++i) - { + for (unsigned int i = 0; i < LineStyleCount; ++i) { double Width = bs.GetU16(); u32 Color; if (ShapeType == 3) @@ -564,8 +515,7 @@ bool BS_VectorImage::ParseStyles(unsigned int ShapeType, SWFBitStream & bs, unsi // ----------------------------------------------------------------------------- -bool BS_VectorImage::Fill(const BS_Rect* pFillRect, unsigned int Color) -{ +bool BS_VectorImage::Fill(const BS_Rect *pFillRect, unsigned int Color) { BS_LOG_ERRORLN("Fill() is not supported."); return false; } @@ -573,16 +523,14 @@ bool BS_VectorImage::Fill(const BS_Rect* pFillRect, unsigned int Color) // ----------------------------------------------------------------------------- -unsigned int BS_VectorImage::GetPixel(int X, int Y) -{ +unsigned int BS_VectorImage::GetPixel(int X, int Y) { BS_LOG_ERRORLN("GetPixel() is not supported. Returning black."); return 0; } // ----------------------------------------------------------------------------- -bool BS_VectorImage::SetContent(const byte *Pixeldata, unsigned int Offset, unsigned int Stride) -{ +bool BS_VectorImage::SetContent(const byte *Pixeldata, unsigned int Offset, unsigned int Stride) { BS_LOG_ERRORLN("SetContent() is not supported."); return 0; } |