bmp2abmp.c:
#include
#include
#include
#include
#include "bmp2abmp.h"
#undef WINDOWS
#ifdef __GNUCC__
#define STRICMP strcasecmp
#else
#define STRICMP stricmp
#endif
#define resourceAttribute 0x0001
FILE *in = NULL;
FILE *out = NULL;
int interactive = true;
UInt8* inBitmap;
UInt8* outBitmap;
void SafeFree( void** p )
{
if ( *p != NULL ) {
free( *p );
*p = NULL;
}
}
void CleanUp( void )
{
SafeFree( ( void** )&inBitmap );
SafeFree( ( void** )&outBitmap );
fcloseall();
}
void msg(char* title, char *s, ...)
{
char out[ 2048 ];
va_list argptr;
if ( ! interactive )
return;
va_start(argptr,s);
vsprintf(out,s,argptr);
va_end(argptr);
#ifdef WINDOWS
MessageBox( NULL, out, title, MB_OK );
#else
fprintf( stderr, "%s\n", out );
#endif
CleanUp();
exit(1);
}
void err(char *s, ...)
{
char out[ 2048 ];
va_list argptr;
va_start(argptr,s);
vsprintf(out,s,argptr);
va_end(argptr);
#ifdef WINDOWS
MessageBox( NULL, out, "Error", MB_OK );
#else
fprintf( stderr, "Fatal error: %s.\n", out );
#endif
CleanUp();
exit(1);
}
FILE *myfopen(char *s,char *m)
{
FILE *f=fopen(s,m);
if(f==NULL) err("Cannot open %s.",s);
return f;
}
int myfgetc(FILE *f)
{
int c=fgetc(f);
if(c==-1) err("Error reading font!");
return c;
}
void* mymalloc( int n )
{
void* p = malloc( n );
if ( p == NULL ) err("Error allocating %ld bytes.", n );
return p;
}
int myfread( void* p, int n, int m, FILE* f )
{
if ( m != fread( p, n, m, f ) )
err("Error reading %d x %d bytes.", n, m );
return m;
}
void myseek(FILE *f, unsigned long x)
{
if(fseek(f,x,SEEK_SET)<0 ||
ftell(f)!=(long)x) err("Error seeking!");
return;
}
UInt32 UInt32LE( UInt32 in )
{
UInt8* p;
UInt32 n;
int i;
p = ( UInt8* )&n;
for ( i = 0 ; i < 4 ; i++ ) {
p[ i ] = in % 256;
in /= 256;
}
return n;
}
UInt16 UInt16LE( UInt16 in )
{
UInt8* p;
int i;
p = ( UInt8* )&in;
return p[ 0 ] + 256 * p[ 1 ];
}
UInt32 UInt32LEP( UInt16* inP )
{
return UInt16LE( inP[ 0 ] ) + 65536 * UInt16LE( inP[ 1 ] );
}
#ifdef WINDOWS
int getname(char *s)
{
int r;
OPENFILENAME ofn;
*s=0;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = s;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = "Palm databases\0*.prc;*.pdb\0All files\0*.*\0";
ofn.lpstrTitle = "Select database to compress";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
return GetOpenFileName(&ofn);
}
#endif
static void WrongFormat( void )
{
err("Unsupported input format");
}
static UInt16 Convert888To565( UInt8* c )
{
return ( ( c[ 2 ] >> 3 ) << 11 ) | ( ( c[ 1 ] >> 2 ) << 5 ) | ( c[ 0 ] >> 3 );
}
static void ConvertRow( UInt8* dest, UInt8* src, UInt16 width )
{
int x;
UInt16* dest16;
dest16 = ( void* )dest;
for ( x = 0 ; x < width ; x++ ) {
dest16[ x ] = UInt16LE( Convert888To565( src + x * 3 ) );
}
}
#define SEQUENCE_LIMIT 126
Int16 GetRunLength( UInt16* data, UInt16 cols )
{
UInt16 i;
for ( i = 1 ; i < cols && data[ i ] == data[ 0 ] && i < SEQUENCE_LIMIT ; i++ ) ;
return i;
}
UInt8* PackRow( UInt8* out, UInt16* in, UInt16 cols )
{
UInt16 x;
UInt16* inEnd;
while ( cols ) {
Int16 count;
count = GetRunLength( in, cols );
if ( 2 <= count ) {
*out++ = ( UInt8 )( 1 - count );
*out++ = *( UInt8* )in;
*out++ = *( 1 + ( UInt8*