libpsxsave/frames.cpp

76 lines
1.9 KiB
C++
Raw Permalink Normal View History

2010-08-10 13:40:01 +00:00
#include "frames.h"
#include <string.h>
char* Frame::getString( int offset, int length ) {
char* data = new char[length];
strncpy( data, &bytes[offset], length );
data[length] = 0; // Properly terminate the returned string
return data;
}
int Frame::getInt32( int offset ) {
return *reinterpret_cast<int*>( &bytes[offset] );
}
int Frame::getInt16( int offset ) {
return *reinterpret_cast<short int*>( &bytes[offset] );
}
void Frame::putInt32( int offset, int value ) {
for( int i = 0; i < 4; i++ ) {
bytes[offset + i] = ( value & ( 0xFF << ( 8 * i ) ) ) >> ( 8 * i );
}
}
void Frame::putInt16( int offset, int value ) {
for( int i = 0; i < 2; i++ ) {
bytes[offset + i] = ( value & ( 0xFF << ( 8 * i ) ) ) >> ( 8 * i );
}
}
void Frame::putString( int offset, char* value, int maxLen ) {
maxLen = maxLen < 0 ? strlen( value ) : 0;
maxLen = (unsigned int)maxLen > strlen( value ) ? strlen( value ) : 0;
strncpy( &bytes[offset], value, maxLen );
//bytes[offset+maxLen] = 0;
}
void Frame::putChar( int offset, char value ) {
bytes[offset] = value;
}
bool XORFrame::getXOR() {
char x = 0;
for( int i = 0; i < 128; i++ ) x ^= bytes[i];
if( x == 0 ) return true;
return false;
}
void XORFrame::setXOR() {
char x = 0;
for( int i = 0; i < 127; i++ ) x ^= bytes[i];
bytes[127] = x;
}
bool IdentificationFrame::isValid() {
if( strcmp( id(), "MC" ) == 0 ) {
return true;
} else {
return false;
}
}
RGB SaveHeaderFrame::paletteColor( int index ) {
index = index < 0 || index >= 16 ? 0 : index;
char triplet[2] = { bytes[0x60 + ( index * 2 )], bytes[0x60 + ( index * 2 + 1 )] };
RGB color;
color.r = ( triplet[0] & 31 ) << 3;
color.g = ( ( triplet[0] & 224 ) >> 2 ) + ( ( triplet[1] & 3 ) << 6 );
color.b = ( triplet[1] & 124 ) << 1;
return color;
}
int IconFrame::colorAt( int x, int y ) {
int offset = x / 2;
if( x % 2 == 0 ) {
return bytes[(y*8)+offset] & 15;
} else {
return ( bytes[(y*8)+offset] & 240 ) >> 4;
}
}