Computing Section Programs I've Made
This Section is a bit bare, but so I can say there is somethin ere i'll put the code for my C++
PCX Viewer, its has had a lot of work done so, please credit me for it, thanks.
It uses command line parameters to load the image, it fades in and fades out WITH NO FLICKERING
/*
This Program Was Created By jOn Hardcastle
** Original Generation **
@ HcAt @ 13/11/97
16/11/97
22/11/97
26/11/97
v4.0
** Restarted Generation, With Memory Saving **
@ HcAt @ 27/11/97 v1.0
27/11/97 <<- v1.1 err later on
27/11/97 <<- v1.2 even later, now has fadein and fadeout
29/11/97 <<- v2.0 Now does everthing 3x faster and uses pointers for fades
32/11/97 <<- v2.1 prints screen data 2x as fast
v2.1
Title:
Displays PCX Picture, Fades The Picture In Then Fades It Out
Errors encountered:
1). Is palette data suposed to be a char because in function get_pal
i used sizeof(int) to get info and numbers were messed up but when
i put in 1,1(<<-char size) it works, ???
2). Had SO MANY problems with memory that i started again with
memory managment in mind.
3). The most stupid error ever because all i need from the header
is the x and y size i seek there to get the numbers, and ran the
program and i couldn't figure out why the image was scrolled around
then i hit me, i was seeking 8 bytes in and getting data, but not
seeking to the end of the header.
Return codes:
Return 1 = Normal Exit
Return 2 = No File Specified
Return 3 = Unable To Open Pcx File
Return 4 = Error: Unable To Allocate Enough Memory
Return 5 = No Palette Found
*/
//**************************************************************************
#include
#include
#include
#include
#ifndef __COMPACT__
#ifndef __LARGE__
#ifndef __HUGE__
#error Please use COMPACT or LARGE or HUGE memory model.
#endif
#endif
#endif
#define PAL_SIZE 768
#define PALETTE_MASK 0x3C6
#define PALETTE_REGISTER_WR 0x3C8
#define PALETTE_DATA 0x3C9
#define VGA256 0x13
#define TEXT_MODE 0x03
#define VERSION 2.1
#define RETRACE \
{ \
while ( (inport(0x3Da) & 0x08 )); \
while (!(inport(0x3da) & 0x08 )); \
}
//*******************************************************
void error(char error_type[],int return_code) ;
//*******************************************************
void set_mode(unsigned char mode) ;
unsigned char get_mode(void) ;
//*******************************************************
void set_pal(unsigned char l_palette[]) ;
void get_pal(FILE *l_pcx_file,unsigned char l_palette[]) ;
//*******************************************************
void fadein (const unsigned char l_palette[]) ;
void fadeout(unsigned char l_palette[]) ;
//*******************************************************
unsigned char far* gptr = (unsigned char far*)0xA0000000L;
int main(int argc,char *argv[])
{
unsigned char *image_data, data, data2, saved,palette[PAL_SIZE]={0};
unsigned int width, height, x_max, y_max;
unsigned long count,rle;
FILE *pcx_file;
if(argc!=2)
error("No Pcx file specified: ",2);
if((pcx_file=fopen(argv[1],"rb"))==NULL)
error("Unable to open pcx file",3);
/*
seeks to the position of X++ and Y++ size and reads ONLY them
because this program is very specific,
MAX size of 320x200x256 and it saves on memory
*/
fseek(pcx_file,8,SEEK_SET);
fread(&x_max,sizeof(unsigned int),1,pcx_file);
fread(&y_max,sizeof(unsigned int),1,pcx_file);
x_max++;y_max++;
/*
sets up image_data with enough memory to load picture
*/
if((image_data=(unsigned char *)malloc(x_max*y_max))==NULL)
error("Error: Unable to allocate enough memory",4);
/*
seeks to the end of the header
*/
fseek(pcx_file,128,SEEK_SET);
/*
gets the picture data and places it into image_data
*/
for(count=0L;count < (long)x_max * (long)y_max;){
fread(&data, sizeof(unsigned char),1, pcx_file);
if((data & 0xC0) == 0xC0){
fread(&data2, sizeof(unsigned char), 1, pcx_file);
for(rle=1L; rle<=(data & 0x3F); rle++)
*(image_data + count++) = data2;
}
else
*(image_data + count++) = data;
}
/*
gets the current mode
then sets mode to 320x200x256
*/
saved=get_mode();
set_mode(VGA256);
/*
seeks to the palette, to see if it is there
*/
fseek(pcx_file,-769L,SEEK_END);
if(fgetc(pcx_file)!=12)
error("No palette found",5);
set_pal(palette); //calls set palette to setup the palette with all black
get_pal(pcx_file,palette); // gets the proper palette from file and puts into palette
/*
places the contents of image_data into video memory twice as fast
*/
count=0L;
for(height=1; height <= y_max;height++){
for(width=1; width <= x_max; width++){
*gptr++ = *(image_data + count++);width++;
*gptr++ = *(image_data + count++);
}
gptr += 320 - x_max;
}
getch();
fadein(palette); // calls the fadein function and passes palette
getch();
fadeout(palette); // calls the fadeout function and passes palette
set_mode(saved); //restores the previous screen mode
free(image_data); //frees up the memory reserved for image_data preventing a 'Orphan' being created
fcloseall(); // closes all open files
error("",1); // calls my custom error function to print exit details
}
//*******************************************************
void error(char error_type[],int return_code)
{
/*
This function is very useful in that when ever there is an error you pass
the message and the return code and it processes it for you and if contains
my exit code so it doesn't clutter up main
*/
if(error_type[0]==0x0){
_setcursortype(_NOCURSOR);
printf("\n\nThis program was created by jOf Hardcastle\n"
"@ HcAt @ [%s] [%s]\n" //prints masterful information
"v%.2g",__DATE__,__TIME__,VERSION);
getch();
}
else
printf("%s",error_type);
_setcursortype(_NORMALCURSOR);
exit(return_code);
}
//*******************************************************
void set_mode(unsigned char mode)
{
/*
Sets the screen mode to that specified by mode
*/
union REGS graphics;
graphics.h.ah=0x0;
graphics.h.al=(unsigned char)mode;
int86(0x10,&graphics,&graphics);
}
//*******************************************************
unsigned char get_mode(void)
{
/*
Gets the current screen mode and returns it to 'saved' in main
*/
union REGS graphics;
graphics.h.ah=0x0F;
int86(0x10,&graphics,&graphics);
return graphics.h.al;
}
//*******************************************************
void set_pal(unsigned char l_palette[])
{
/*
sets the palette to that specified by l_palette(local version of palette frmo main)
puts in three colors for every cycle
*/
outportb(PALETTE_MASK, 0xFF);
outportb(PALETTE_REGISTER_WR, 0x0);
for(int counter=0;counter < PAL_SIZE;){
outportb(PALETTE_DATA, l_palette[counter++]);
outportb(PALETTE_DATA, l_palette[counter++]);
outportb(PALETTE_DATA, l_palette[counter++]);
}
}
//*******************************************************
void get_pal(FILE *l_pcx_file,unsigned char l_palette[])
{
/*
Gets the palette information from the file and puts it into
l_palette, does three every cycle
*/
for(int i=0;i < PAL_SIZE;){
l_palette[i++]=(unsigned char) fgetc(l_pcx_file) >n
l_palette[i++]=(unsigned char) fgetc(l_pcx_file) >n
l_palette[i++]=(unsigned char) fgetc(l_pcx_file) >n
}
}
//*******************************************************
void fadein(const unsigned char l_palette[])
{
/*
fades the image in by increasing fade_palette until it is equal to
l_palette(declared as a const to ensure its not changed) the palette
info is updated for every completed color cycle using set_pal
*/
unsigned char fade_palette[PAL_SIZE]={0};
for(int shades=1;shades � ;shades++){
for(int color=0;color < PAL_SIZE;){
if(* (fade_palette + (++color)) < *(l_palette + color) )
* (fade_palette + color) = shades;
if(* (fade_palette + (++color)) < *(l_palette + color) )
* (fade_palette + color) = shades;
if(* (fade_palette + (++color)) < *(l_palette + color) )
* (fade_palette + color) = shades;
}
delay(20);
RETRACE // waits until the screen retrace is due
set_pal(fade_palette);
}
}
//*******************************************************
void fadeout(unsigned char l_palette[])
{
/*
fades the image out by decreaing l_palette by one until it equal to 0
(black)
*/
for(int shades=64;shades l ;shades--){
for(int color=0;color < PAL_SIZE;){
if( * (l_palette +( ++color ) ) l )
(* (l_palette + color) )--;
if( * (l_palette +( ++color ) ) l )
(* (l_palette + color) )--;
if( * (l_palette +( ++color ) ) l )
(* (l_palette + color) )--;
}
delay(20);
RETRACE
set_pal(l_palette);
}
}
//**************************************************************************
Another program I added for a laugh
Excercise 20c.cpp