Skip to content

Instantly share code, notes, and snippets.

@Sembiance
Created May 15, 2021
Embed
What would you like to do?
kygFormat image parsing code
/*
--- Kyss presents ---
kygload.c : Kyss graphics format loader
Last update : 1992-02-28 version 0.10
for "TOWNS-GCC"
*/
extern char egbwork[];
struct /* KYG format header ver.0.10 (128bytes) */
{ char id[ 20 ];
char message[ 80 ];
unsigned char eom;
unsigned char padding;
unsigned short xSize, ySize;
unsigned short xPosition, yPosition;
unsigned long size; /* ���k��̃f�[�^�o�C�g���B�w�b�_�͊܂܂Ȃ���� */
unsigned short type; /* ���k�^�C�v�B0 �͖����k�A�ʏ�� 1
�͂����� 2 �ȏオ�g������́E�E�E���Ȃ��� */
unsigned short color; /* �F���B���Ԃ� 32768 �����g����Ǝv�����ǂ� */
char reserve[ 10 ];
} KYGheader;
int KYGload( char *fn, int mode, int page )
{
int i;
int x, y;
int x1, y1;
int dp;
unsigned short base;
unsigned char count;
unsigned char rd;
unsigned char nrn;
unsigned long dataSize;
unsigned char *iHeap;
unsigned short *oHeap;
FILE *ifp;
static int dps[] = { 0, 5, 2, 7, 4, 9, 1, 6, 3, 8 };
if( ( ifp = fopen( fn, "rb" ) ) == NULL )
return( ERR );
fread( &KYGheader, sizeof KYGheader, 1, ifp );
if( strncmp( KYGheader.id, "KYGformat", 9 ) != 0 )
{
printf( "not KYG format\n" );
return( ERR );
}
dataSize = KYGheader.xSize * KYGheader.ySize * 2;
if( ( oHeap = ( unsigned short * )( malloc( dataSize ) ) ) == NULL )
return( ERR );
if( ( iHeap = ( unsigned char * )( malloc( KYGheader.size ) ) ) == NULL )
return( ERR );
fread( iHeap, 1, KYGheader.size, ifp );
fclose( ifp );
for( i = dp = nrn = 0; /* dp < dataSize */ i < dataSize / 2; )
{
if( ( rd = *( iHeap + dp ) & 0x0F ) == 0 || nrn > 0 )
{
if( nrn == 0 )
nrn = ( *( iHeap + dp++ ) >> 4 ) - 1;
else
--nrn;
base = *( unsigned short * )( iHeap + dp );
dp += 2;
count = ( ( base & 0x8000 ) != 0 ) ? 1 : *( iHeap + dp++ );
base &= 0x7FFF;
}
else
{
nrn = *( iHeap + dp++ ) >> 4;
count = ( ( rd & 0x08 ) != 0 ) ? 1 : *( iHeap + dp++ );
base = *( oHeap + i - KYGheader.xSize - 4 + ( rd & 0x07 ) );
}
for( ; count > 0; --count, ++i )
*( oHeap + i ) = base;
}
if( mode == 0 )
{
EGB_writePage( egbwork, page );
EGB_putRect( egbwork, 0, oHeap, KYGheader.xPosition, KYGheader.yPosition, KYGheader.xPosition + KYGheader.xSize - 1, KYGheader.yPosition + KYGheader.ySize - 1 );
}
else
{
for( y1 = 0; y1 < 10; ++y1 )
{
for( x1 = 0; x1 < 10; ++x1 )
{
for( y = 0; y < 240; y += 10 )
{
for( x = 0; x < 320; x += 10 )
{
VRAM_putPixelW( ( x + dps[ x1 ] ) * 2 + ( y + dps[ y1 ] ) * 1024 + 0x40000 * page, *( oHeap + x + dps[ x1 ] + ( y + dps[ y1 ] ) * 320 ) );
}
}
}
}
/* EGB_writePage( egbwork, page );
EGB_writeMode( egbwork, PASTEL );
for( i = 1; i <= 256; i += 5 )
{
EGB_pastel( egbwork, i );
EGB_putRect( egbwork, 0, oHeap, KYGheader.xPosition, KYGheader.yPosition, KYGheader.xPosition + KYGheader.xSize - 1, KYGheader.yPosition + KYGheader.ySize - 1 );
}
EGB_writeMode( egbwork, PSET );
*/
}
free( iHeap );
free( oHeap );
return( 0 );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment