|
Fonctions 13h (3) |
Quelques fonctions C et structures C utiles pour le mode 13h.
Librairie : lib_graph_c.zip
Fonctions de manipulation des structures IMAGE
Allocation memoire d'une structure IMAGE
void Set_Image( int x , int y , struct IMAGE *DATA )
{
DATA->x=x ;
DATA->y=y ;
if ( DATA->data == NULL )
DATA->data = ( unsigned char far * ) _fmalloc( x * y ) ;
else
DATA->data = ( unsigned char far * ) _frealloc( DATA->data , x * y ) ;
if ( DATA->data == NULL )
{
Mode_Text() ;
printf( "Erreur d'allocation memoire pour un Image" ) ;
exit( 0 ) ;
}
}
Coloriser uniformement une structure IMAGE
void Cls_Image( unsigned char color , struct IMAGE *DATA )
{
unsigned int pos, max ;
max = DATA->x * DATA->y ;
for ( pos = 0 ; pos < max ; pos ++ )
DATA->data[ pos ] = color ;
}
Charger un fichier image au format PCX dans une structure IMAGE
void Load_PCX(char *FileName, struct IMAGE *image, struct PALETTE *PAL )
{
FILE *fp ;
int num_bytes , index ;
unsigned int x_min, y_min, x_max, y_max, x_size , y_size ;
long count;
unsigned char data, compression ;
fp = fopen( FileName , "rb" ) ;
fseek( fp , 2 , 0 ) ;
fread( &compression , 1 , 1 , fp ) ;
fseek( fp , 8 , 0 ) ;
fread( &x_size , 2 , 1 , fp ) ;
fread( &y_size , 2 , 1 , fp ) ;
x_size ++ ;
y_size ++ ;
image->x = x_size ;
image->y = y_size ;
image->data = ( unsigned char far * ) _fmalloc( x_size * y_size ) ;
if ( image->data == NULL )
{
Mode_Text() ;
printf( "Erreur d'allocation memoire pour une image lors du chargement pcx" ) ;
exit( 0 ) ;
}
fseek( fp , 128 , 0 ) ;
count = 0 ;
if ( compression )
{
while( count <= x_size * y_size )
{
data = getc( fp ) ;
if ( data >= 192 && data <= 255 )
{
num_bytes = data - 192 ;
data = getc( fp );
while( num_bytes > 0 )
{
image->data[ count ] = data ;
count ++ ;
num_bytes -- ;
}
}
else
{
image->data[ count ] = data ;
count ++ ;
}
}
}
else
while( count <= x_size * y_size )
{
data = getc( fp ) ;
image->data[ count ] = data ;
count ++ ;
}
fseek( fp , -768 , 2 ) ;
for ( index = 0 ; index < 256 ; index++ )
{
PAL->red[index] = (getc(fp) >> 2);
PAL->green[index] = (getc(fp) >> 2);
PAL->blue[index] = (getc(fp) >> 2);
}
fclose(fp);
}
Zoomer / Dézoomer une structure selon un facteur choisi
void Scale_Image( float scale , struct IMAGE *SOURCE , struct IMAGE *DEST )
{
int x, y, i, j ;
float step_y ;
unsigned int pos1, pos2 ;
i = SOURCE->x * scale ;
j = SOURCE->y * scale ;
if ( DEST->data == NULL )
DEST->data = ( unsigned char far * ) _fmalloc( i * j ) ;
else
DEST->data = ( unsigned char far * ) _frealloc( DEST->data , i * j ) ;
DEST->x = i ;
DEST->y = j ;
if ( DEST->data == NULL )
{
Mode_Text() ;
printf( "Erreur d'allocation memoire pour une image lors d'un zoom" ) ;
exit( 0 ) ;
}
pos1 = 0 ;
pos2 = 0 ;
step_y = SOURCE->y / scale ;
for ( y = 0 ; y < DEST->y ; y++ )
{
for ( x = 0 ; x < DEST->x ; x++ )
DEST->data[ pos1 + x ] = SOURCE->data[ pos2 + ( int ) ( x / scale ) ] ;
pos1 = pos1 + DEST->x ;
pos2 = SOURCE->x * ( int ) ( y / scale ) ;
}
}
Afficher une structure IMAGE à l'écran
void Put_Image_Video( int x , int y , struct IMAGE *DATA )
{
int i , j , Xmax , Ymax , size_x ;
unsigned char couleur ;
unsigned int pos ;
if ( x < 0 )
x = 0 ;
if ( y < 0 )
y = 0 ;
Xmax = x + DATA->x ;
Ymax = y + DATA->y ;
size_x = DATA->x ;
if ( Xmax > 319 )
Xmax = 319 ;
if ( Ymax > 199 )
Ymax = 199 ;
for ( i = x ; i <= Xmax ; i++ )
for ( j = y ; j <= Ymax ; j++ )
{
pos = ( j - y ) * size_x + i - x ;
couleur = DATA->data[ pos ] ;
Put_Pixel_Video( i , j , couleur ) ;
}
}
Afficher une structure IMAGE à l'écran avec une couleur de transparence
void Put_Image_Video_Transparent( int x , int y , unsigned char color , struct IMAGE *DATA )
{
int i , j , Xmax , Ymax , size_x ;
unsigned char couleur ;
unsigned int pos ;
Xmax = x + DATA->x ;
Ymax = y + DATA->y ;
size_x = DATA->x ;
if ( Xmax > 319 )
Xmax = 319 ;
if ( Ymax > 199 )
Ymax = 199 ;
for ( i = x ; i <= Xmax ; i++ )
for ( j = y ; j <= Ymax ; j++ )
{
pos = ( j - y ) * size_x + i - x ;
couleur = DATA->data[ pos ] ;
if ( couleur != color )
Put_Pixel_Video( i , j , couleur ) ;
}
}
|
|