doom3-bfg/neo/swf/SWF_File.cpp

379 lines
7.6 KiB
C++
Raw Normal View History

2013-11-06 20:46:35 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
2015-09-13 15:21:58 +00:00
Copyright (C) 2013-2015 Robert Beckebans
2013-11-06 20:46:35 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "precompiled.h"
#pragma hdrstop
2013-11-06 20:46:35 +00:00
idFile_SWF::~idFile_SWF()
{
if( file != NULL )
{
delete file;
file = NULL;
}
}
2015-10-11 18:10:57 +00:00
int idFile_SWF::BitCountS( const int64 value, bool isSigned )
2013-11-12 22:05:59 +00:00
{
2015-10-11 18:10:57 +00:00
int number = idMath::Abs( value );
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
int64 x = 1;
int i;
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
for( i = 1; i <= 64; i++ )
{
x <<= 1;
if( x > number )
2019-11-11 19:27:44 +00:00
{
2015-10-11 18:10:57 +00:00
break;
2019-11-11 19:27:44 +00:00
}
2013-11-12 22:05:59 +00:00
}
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
return ( int )( i + ( ( isSigned ) ? 1 : 0 ) );
2013-11-12 22:05:59 +00:00
}
2013-11-06 20:46:35 +00:00
2013-11-12 22:05:59 +00:00
int idFile_SWF::BitCountU( const int value )
2013-11-06 20:46:35 +00:00
{
2015-10-11 18:10:57 +00:00
//int nBits = idMath::BitCount( value );
int nBits = BitCountS( value, false );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
return nBits;
2013-11-06 20:46:35 +00:00
}
2013-11-12 22:05:59 +00:00
int idFile_SWF::BitCountFloat( const float value )
{
int value2 = ( int ) value;
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
int nBits = BitCountS( value2, value < 0.0F );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
return nBits;
}
int idFile_SWF::EnlargeBitCountS( const int value, int numBits )
{
2015-10-11 18:10:57 +00:00
if( value == 0 )
{
return numBits;
}
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
int n = BitCountS( value, true );
2013-11-12 22:05:59 +00:00
if( n > numBits )
{
numBits = n;
}
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
return numBits;
}
int idFile_SWF::EnlargeBitCountU( const int value, int numBits )
{
2015-10-11 18:10:57 +00:00
if( value == 0 )
{
return numBits;
}
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
int n = BitCountU( value );
if( n > numBits )
{
numBits = n;
}
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
return numBits;
}
2013-11-06 20:46:35 +00:00
int idFile_SWF::Write( const void* buffer, int len )
{
2013-11-13 12:59:58 +00:00
ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
return file->Write( buffer, len );
}
void idFile_SWF::WriteByte( byte bits )
{
file->WriteUnsignedChar( bits );
}
void idFile_SWF::WriteUBits( int value, int numBits )
{
for( int bit = 0; bit < numBits; bit++ )
{
int nb = ( int )( ( value >> ( numBits - 1 - bit ) ) & 1 );
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
NBits += nb * ( 1 << ( 7 - bitPos ) );
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
bitPos++;
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
if( bitPos == 8 )
{
2015-10-11 18:10:57 +00:00
WriteByte( ( byte )( NBits & 0xFF ) );
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
bitPos = 0;
NBits = 0;
}
}
}
void idFile_SWF::WriteSBits( int value, int numBits )
{
2015-10-11 18:10:57 +00:00
int32 tmp = value & 0x7FFFFFFF;
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
if( value < 0 )
{
tmp |= ( 1L << ( ( int32 )numBits - 1 ) );
}
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
WriteUBits( tmp, numBits );
2013-11-06 20:46:35 +00:00
}
2013-11-09 23:34:51 +00:00
void idFile_SWF::WriteU8( uint8 value )
{
ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-09 23:34:51 +00:00
WriteByte( value );
}
2013-11-06 20:46:35 +00:00
void idFile_SWF::WriteU16( uint16 value )
{
ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
WriteByte( value & 0xFF );
2015-10-11 20:01:02 +00:00
WriteByte( ( value >> 8 ) & 0xFF );
2013-11-06 20:46:35 +00:00
}
2013-11-09 23:34:51 +00:00
void idFile_SWF::WriteU32( uint32 value )
2013-11-06 20:46:35 +00:00
{
2013-11-09 23:34:51 +00:00
ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-09 23:34:51 +00:00
WriteByte( value & 0xFF );
WriteByte( ( value >> 8 ) & 0xFF );
WriteByte( ( value >> 16 ) & 0xFF );
WriteByte( ( value >> 24 ) & 0xFF );
}
void idFile_SWF::WriteRect( const swfRect_t& rect )
{
2013-11-12 22:05:59 +00:00
int tl_x = FLOAT2SWFTWIP( rect.tl.x );
int br_x = FLOAT2SWFTWIP( rect.br.x );
int tl_y = FLOAT2SWFTWIP( rect.tl.y );
int br_y = FLOAT2SWFTWIP( rect.br.y );
2019-11-11 19:27:44 +00:00
2013-11-13 12:59:58 +00:00
int nBits = 0;
2019-11-11 19:27:44 +00:00
2013-11-13 12:59:58 +00:00
nBits = EnlargeBitCountS( tl_x, nBits );
nBits = EnlargeBitCountS( br_x, nBits );
nBits = EnlargeBitCountS( tl_y, nBits );
nBits = EnlargeBitCountS( br_y, nBits );
2019-11-11 19:27:44 +00:00
2013-11-06 20:46:35 +00:00
WriteUBits( nBits, 5 );
WriteSBits( tl_x, nBits );
WriteSBits( br_x, nBits );
WriteSBits( tl_y, nBits );
WriteSBits( br_y, nBits );
2019-11-11 19:27:44 +00:00
2013-11-13 12:59:58 +00:00
ByteAlign();
2013-11-06 20:46:35 +00:00
}
2013-11-12 22:05:59 +00:00
void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix )
{
//ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
bool hasScale = ( matrix.xx != 1.0f || matrix.yy != 1.0f );
WriteUBits( hasScale ? 1 : 0, 1 );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
if( hasScale )
{
int nBits = 0;
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
int xx = FLOAT2SWFFIXED16( matrix.xx );
int yy = FLOAT2SWFFIXED16( matrix.yy );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
nBits = EnlargeBitCountS( xx, nBits );
nBits = EnlargeBitCountS( yy, nBits );
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
WriteUBits( nBits, 5 );
2013-11-12 22:05:59 +00:00
WriteSBits( xx, nBits );
WriteSBits( yy, nBits );
}
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
bool hasRotate = ( matrix.yx != 0 && matrix.xy != 0 );
WriteUBits( hasRotate ? 1 : 0, 1 );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
if( hasRotate )
{
int nBits = 0;
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
int yx = FLOAT2SWFFIXED16( matrix.yx );
int xy = FLOAT2SWFFIXED16( matrix.xy );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
nBits = EnlargeBitCountS( yx, nBits );
nBits = EnlargeBitCountS( xy, nBits );
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
WriteUBits( nBits, 5 );
2013-11-12 22:05:59 +00:00
WriteSBits( yx, nBits );
WriteSBits( xy, nBits );
}
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
int nBits = 0;
int tx = FLOAT2SWFTWIP( matrix.tx );
int ty = FLOAT2SWFTWIP( matrix.ty );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
nBits = EnlargeBitCountS( tx, nBits );
nBits = EnlargeBitCountS( ty, nBits );
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
WriteUBits( nBits, 5 );
2013-11-12 22:05:59 +00:00
WriteSBits( tx, nBits );
WriteSBits( ty, nBits );
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
ByteAlign();
}
2015-09-13 15:21:58 +00:00
void idFile_SWF::WriteColorXFormRGBA( const swfColorXform_t& xcf )
{
bool hasAddTerms = ( xcf.add.x != 0.0f || xcf.add.y != 0.0f || xcf.add.z != 0.0f || xcf.add.w != 0.0f );
WriteUBits( hasAddTerms ? 1 : 0, 1 );
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
bool hasMulTerms = ( xcf.mul.x != 0.0f || xcf.mul.y != 0.0f || xcf.mul.z != 0.0f || xcf.mul.w != 0.0f );
WriteUBits( hasMulTerms ? 1 : 0, 1 );
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
int nBits = 0;
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
int mulRed = 0;
int mulGreen = 0;
int mulBlue = 0;
int mulAlpha = 0;
if( hasMulTerms )
{
mulRed = FLOAT2SWFFIXED8( xcf.mul.x );
mulGreen = FLOAT2SWFFIXED8( xcf.mul.y );
mulBlue = FLOAT2SWFFIXED8( xcf.mul.z );
mulAlpha = FLOAT2SWFFIXED8( xcf.mul.w );
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
nBits = EnlargeBitCountS( mulRed, nBits );
nBits = EnlargeBitCountS( mulGreen, nBits );
nBits = EnlargeBitCountS( mulBlue, nBits );
nBits = EnlargeBitCountS( mulAlpha, nBits );
}
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
int red = 0;
int green = 0;
int blue = 0;
int alpha = 0;
if( hasAddTerms )
{
red = FLOAT2SWFFIXED8( xcf.add.x );
green = FLOAT2SWFFIXED8( xcf.add.y );
blue = FLOAT2SWFFIXED8( xcf.add.z );
alpha = FLOAT2SWFFIXED8( xcf.add.w );
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
nBits = EnlargeBitCountS( red, nBits );
nBits = EnlargeBitCountS( green, nBits );
nBits = EnlargeBitCountS( blue, nBits );
nBits = EnlargeBitCountS( alpha, nBits );
}
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
WriteUBits( nBits, 4 );
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
if( hasMulTerms )
{
WriteSBits( mulRed, nBits );
WriteSBits( mulGreen, nBits );
WriteSBits( mulBlue, nBits );
WriteSBits( mulAlpha, nBits );
}
2019-11-11 19:27:44 +00:00
2015-09-13 15:21:58 +00:00
if( hasAddTerms )
{
WriteSBits( red, nBits );
WriteSBits( green, nBits );
WriteSBits( blue, nBits );
WriteSBits( alpha, nBits );
}
2019-11-11 19:27:44 +00:00
2015-10-11 18:10:57 +00:00
ByteAlign();
2015-09-13 15:21:58 +00:00
}
2013-11-12 22:05:59 +00:00
void idFile_SWF::WriteColorRGB( const swfColorRGB_t& color )
{
2013-11-13 12:59:58 +00:00
ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
WriteByte( color.r );
WriteByte( color.g );
WriteByte( color.b );
}
void idFile_SWF::WriteColorRGBA( const swfColorRGBA_t& color )
{
2013-11-13 12:59:58 +00:00
ByteAlign();
2019-11-11 19:27:44 +00:00
2013-11-12 22:05:59 +00:00
WriteByte( color.r );
WriteByte( color.g );
WriteByte( color.b );
WriteByte( color.a );
}
2013-11-09 23:34:51 +00:00
void idFile_SWF::WriteTagHeader( swfTag_t tag, int32 tagLength )
{
uint16 tagIDLength = ( ( int ) tag << 6 );
2019-11-11 19:27:44 +00:00
2013-11-09 23:34:51 +00:00
if( tagLength < 0x3F )
{
tagIDLength += tagLength;
WriteU16( tagIDLength );
}
else
{
tagIDLength += 0x3F;
WriteU16( tagIDLength );
WriteU32( tagLength );
}
}
2013-11-12 17:17:03 +00:00
int32 idFile_SWF::GetTagHeaderSize( swfTag_t tag, int32 tagLength )
{
int32 size = 2;
2019-11-11 19:27:44 +00:00
2013-11-12 17:17:03 +00:00
if( tagLength >= 0x3F )
{
size = 6;
}
2019-11-11 19:27:44 +00:00
2013-11-12 17:17:03 +00:00
return size;
}
2013-11-06 20:46:35 +00:00