DEMO/PCX.CPP
2021-09-08 20:55:57 +02:00

191 lines
5.7 KiB
C++

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#define OK 0
#define ERROR 1
//#define RES_X 320
//#define RES_Y 200
#define RES_X 73+1
#define RES_Y 25
typedef unsigned char DacPalette256[256][3];
DacPalette256 Palette256;
int CargaPaleta(char *file );
unsigned char Movs[273][411][1];
int MuestraImagen( char *file );
void ProcesaPunto( int x, int y, char byte );
void Mem2CPP(void);
void main( int argc, char *argv[] )
{
int gd=DETECT, gm;
initgraph( &gd, &gm, "c:\\program\\borlandc\\bgi" );
MuestraImagen( argv[1] );
CargaPaleta( argv[1] );
getch();
Mem2CPP();
closegraph();
}
void Mem2CPP(void)
{
FILE *out;
int i, j, k;
if ( (out=fopen("foto.txt", "wb" ) ) != NULL )
{
for ( i = 0; i < 411; i ++ )
{
for ( j = 0; j < 273; j ++ )
{
fprintf( out, "%d", (int)Movs[j][i][0] );
}
fprintf( out, "\n" );
}
/*
fprintf( out, "unsigned char Movs[23][23][3] = { \n");
for ( k = 0; k < 3; k ++ )
{
fprintf( out, " {\n");
for ( i = 0; i < 22; i++ )
{
fprintf( out, " { ");
for ( j = 0; j < 23; j++ )
fprintf( out, "%03d, ", (int)Movs[j][i][k] );
fprintf( out, " }, \n");
}
i = 22;
fprintf( out, " { ");
for ( j = 0; j < 23; j++ )
fprintf( out, "%03d, ", (int)Movs[j][i][k] );
fprintf( out, " } \n");
fprintf( out, " },\n");
}
fprintf( out, " };");
fprintf( out, "\n\n\n");
for ( i = 0; i < 256; i++ )
fprintf ( out, "{ %03d, %03d, %03d }, \n", Palette256[i][0], Palette256[i][1], Palette256[i][2] );
*/
fclose(out);
}
}
/**************************************************************************\
|* *|
|* MuestraImagen *|
|* *|
|* Descripci¢n: *|
|* Descomprime y copia a la pagina indicada un PCX *|
|* *|
|* Entradas: nombre de la imagen *|
|* *|
|* *|
|* Salidas: OK Todo ha ido bien *|
|* ERROR Algo va mal *|
|* *|
\**************************************************************************/
int MuestraImagen( char *file )
{
int alto, ancho, contador;
unsigned char byte;
FILE *fp;
if ( (fp = fopen( file,"rb")) != NULL )
{
// Saltamos la cabecera
fseek( fp, 128, SEEK_SET );
for(alto=0; alto<RES_Y; alto++)
{
for(ancho=0; ancho<RES_X; )
{
byte=getc(fp);
if(byte<=0xC0)
{
// set_point (ancho, alto, byte);
ProcesaPunto( ancho, alto, byte );
ancho++;
} else {
contador=byte&0x3F; byte=getc(fp);
for(; contador>0; contador--)
{
// set_point (ancho, alto, byte);
ProcesaPunto( ancho, alto, byte );
ancho++;
}
}
}
}
fclose(fp);
} else return ERROR;
return OK;
}
/**************************************************************************\
|* *|
|* CargaPaleta *|
|* *|
|* Descripci¢n: *|
|* Carga la paleta con los colores por defecto *|
|* *|
|* *|
|* Entradas: achivo PCX de donde cargar la paleta *|
|* *|
|* Salidas: OK Todo ha ido bien *|
|* ERROR Algo va mal *|
|* *|
\**************************************************************************/
int CargaPaleta(char *file )
{
int index;
FILE *fp;
if ( (fp=fopen( file, "rb" ) ) == NULL )
return ERROR;
if ( fseek( fp, -768L, SEEK_END ) == 0 )
{
for (index=0; index<256; index++)
{
// get the red component
// get the green component
// get the blue component
// set components
Palette256[index][0] = getc(fp) >> 2;
Palette256[index][1] = getc(fp) >> 2;
Palette256[index][2] = getc(fp) >> 2;
} // end for index
}
fclose( fp );
return OK;
}
void ProcesaPunto( int x, int y, char byte )
{
Movs[x][y][0]=byte;
/*
if ( x > 0 && x < 23 && y > 0 && y < 23 )
Movs[x][y][0] = byte;
if ( x > 0 + 23 && x < 23 + 23 && y > 0 && y < 23 )
Movs[x-23][y][1] = byte;
if ( x > 0 + 23*2 && x < 23 + 23*2 && y > 0 && y < 23 )
Movs[x-23*2][y][2] = byte;
*/
putpixel( x /*+ x*2*/, y /*+ y*2*/, byte );
}