Thứ Bảy, 8 tháng 2, 2014

Xử lý ảnh số -P17

Phụ lục - Xử lý ảnh
SetRGBPalette(i,(pals+i)->rgbRed,(pals+i) ->rgbGreen,
(pals+i) - >rgbBlue);
}
}
//********************************************* //
// Đọc File BMP vào mảng bộ nhớ //
int GetBitmapToArray(char *fname)
{
int i,j,tmp,numcolor,BytePerLine,bmfilehand;
BITMAPINFOHEADER bminfohdr;
BITMAPFILEHEADER bmfilehdr;
RGBQUAD *bmpalettes;
tmp=OpenBitmapFile(fname,&bmfilehand,&bmfilehdr,
&bminfohdr);
chieucao= bminfohdr.biHeight;
chieurong= bminfohdr.biWidth;
switch(tmp)
{case -1: return(1); //break; //không mở đợc file
case -2: return(2); //break; //lỗi file
}
numcolor=1 << (bminfohdr.biBitCount);
BytePerLine=bminfohdr.biSizeImage/bminfohdr.biHeight;
bmpalettes=(RGBQUAD far*)
malloc(numcolor*sizeof(RGBQUAD));
if(bmpalettes==NULL) return (3);
tmp=ReadBitmapPalette(bmfilehand,numcolor,bmpalettes);
if (tmp != 0) return(1);
SetRGBPalettes(bmpalettes,numcolor);
farfree(bmpalettes);
lseek(bmfilehand,bmfilehdr.bfoffBits,SEEK_SET);
if((InImage =(BYTE huge *)
farcalloc(bminfohdr.biHeight*bminfohdr.biWidth,
sizeof(BYTE)))==NULL)
return(3);
p= 0;
for(i=0;i<bminfohdr.biHeight;i++)
{ if (bminfohdr.biCompression==0)
tmp=GetScanLine(bmfilehand,BytePerLine,&bminfohdr);
}
Nhập môn xử lý ảnh số - ĐHBK Hà nội 15
Phụ lục - Xử lý ảnh
close(bmfilehand);
return(0);
}
PCX.H
// Chứa các khai báo của File PCX
//
#define TRUE 1
#define FALSE 0
#define VIDEO 0x10
#define Enter 13
#define BackSpace 8
#define MAXSCREENWIDTH 640
#define MAXSCREENHEIGHT 480
#define BITSPERBYTE 8
#define MAXPLANES 4
#define MAXBYTESPERSCAN 640
#define MAXPALETTECOLORS 16
#define MAX256PALETTECOLORS 256
#ifndef_BYTE
#define _BYTE
typedef char BYTE;
#endif
#define NoError 0
#define EBadParms -1
#define EFileNotFound -2
#define EReadFileHdr -3
#define ENotPCXFile -4
#define ECorrupt -5
#define EWrtFileHdr -6
#define EWrtOutFile -7
#define EWrtScanLine -8
#define EPCCFile -9
#define PCXHdrTag 0x0A
#define MaxRepCount 0x3F
#define PCX256ColorTag 0x0C
typedef struct
{
BYTE Red;
Nhập môn xử lý ảnh số - ĐHBK Hà nội 16
Phụ lục - Xử lý ảnh
BYTE Green;
BYTE Blue;
}ColorRegister;
struct PCXFileHeader
{
BYTE Header;
BYTE Version;
BYTE Encode;
BYTE BitPerPix;
unsigned X1;
unsigned Y1;
unsigned X2;
unsigned Y2;
unsigned Hres;
unsigned Vres;
};
struct PCXInfo
{
BYTE Vmode;
BYTE NumOfPlanes;
unsigned BytesPerLine;
BYTE unused[60];
};
struct ExtendedPalette
{
BYTE ExtendedPalette;
ColorRegister Palette[MAX256PALETTECOLORS];
};
struct PCX_File
{
struct PCXFileHeader PCXHeader;
ColorRegister Palette[MAXPALETTECOLORS];
struct PCXInfo Info;
} PCX_FileType;
int looping;
struct PCX_File PCXData;
unsigned ImageWidth,ImageHeight;
FILE *PCXFile;
BYTE ScanLine[MAXBYTESPERSCAN];
Nhập môn xử lý ảnh số - ĐHBK Hà nội 17
Phụ lục - Xử lý ảnh
BYTE PixelColorNum[MAXSCREENWIDTH];
struct ExtendedPalette Color256Palette;
unsigned ImageWidth,ImageHeight,NumOfPlanes;
unsigned Is256ColorFile=FALSE;
struct PCX_File PCXData;
int ReadPCXFileHdr(char *Filename,int Verbose);
static int ExpandScanLine(FILE *InFile);
unsigned InstallPCXFilePalette(void);
void DisplayPCXFile(char *FileName,int Verbose);
void Set256ColorMode(void);
void interrupt (*oldkb)();
void interrupt newkb();
int Gwrite(int x,int y,char *gtext,int inc)
{
WriteXYB(x,y,gtext,1,1,15,0,0,0);
return (x+inc+textwidth(gtext));
}
int GwriteDel(int x,int y,char *gtext,int inc)
{
WriteXYB(x,y,gtext,1,1,0,0,0,0);
return (x+inc+textwidth(gtext));
}
int Gwriteln(int x,int y,char *gtext)
{
WriteXYB(x,y,gtext,1,1,15,0,0,0);
return (y+10+textheight(gtext));
}
char *Gread(int x,int y)
{
char *gtext,text[2];
char ch;
gtext[0]=0x0;
do
{
ch=getch();
if (ch!=Enter)
{
if (ch!=BackSpace)
{
Nhập môn xử lý ảnh số - ĐHBK Hà nội 18
Phụ lục - Xử lý ảnh
text[0]=ch;text[1]='\0';
x=Gwrite(x,y,text,0);
strcat(gtext,text);
}
else
if (strcmp(gtext,""))
{
text[0]=gtext[strlen(gtext)-1];text[1]='\0';
x=x-textwidth(text);
x=GwriteDel(x,y,"",0);
x=x-textwidth(text);
gtext[strlen(gtext)-1]='\0';
}
}
} while (ch!=Enter);
return gtext;
}
void interrupt newkb()
{
if(inportb(0x60)==1) looping=0;
oldkb();
}
void Set256ColorMode(void)
{
union REGS regs;
regs.h.ah=0;
regs.h.al=0x13;
int86(VIDEO,&regs,&regs);
}
int ReadPCXFileHdr(char *Filename,int Verbose)
{
unsigned Index,i;
int row=100,col=100;
for(i=0;i<=strlen(Filename);i++) Filename[i]=toupper(Filename[i]);
if(!strchr(Filename,'.')) strcat(Filename,".PCX");
if((PCXFile=fopen(Filename,"rb"))==NULL)
{
Gwrite(col,row,"Không tìm thấy File ",0);
return(EFileNotFound);
Nhập môn xử lý ảnh số - ĐHBK Hà nội 19
Phụ lục - Xử lý ảnh
}
if(fread(&PCXData,sizeof(struct PCX_File),1,PCXFile)!=1)
{
Gwrite(col,row,"Lỗi đọc Header File ",0);
return(EReadFileHdr);
}
if(PCXData.PCXHeader.Header!=PCXHdrTag)
{
Gwrite(col,row,"Không phải PCX File ",0);
return(ENotPCXFile);
}
if (Verbose)
{
clrscr();
printf("PCX Image Information for file %s\n\n",Filename);
printf("\tVersion %d\n",PCXData.PCXHeader.Version);
printf("\tCompression %s\n",
PCXData.PCXHeader.Encode==0?"none":"RLL");
printf("\tBit per Pixel %d\n",PCXData.PCXHeader.BitPerPix);
printf("\tX1: %d\n",PCXData.PCXHeader.X1);
printf("\tY1: %d\n",PCXData.PCXHeader.Y1);
printf("\tX2: %d\n",PCXData.PCXHeader.X2);
printf("\tY2: %d\n",PCXData.PCXHeader.Y2);
printf("\tHoriz Resolution: %d\n",PCXData.PCXHeader.Hres);
printf("\tVert Resolution: %d\n",PCXData.PCXHeader.Vres);
printf("\tVmode: %d\n",PCXData.Info.Vmode);
printf("\tNumber of Planes: %d\n",PCXData.Info.NumOfPlanes);
printf("\tByte per Scan Line of Plane :%d\n"
,PCXData.Info.BytesPerLine);
printf("\n\tHit any key to proceed");
getch();
clrscr();
if
((PCXData.PCXHeader.Hres==320)&&(PCXData.PCXHeader.Vres==
200)&&
(PCXData.Info.NumOfPlanes==1))
Is256ColorFile=TRUE;
printf("\n\tColor Register Value of PCX file: %s\n\n",Filename);
Nhập môn xử lý ảnh số - ĐHBK Hà nội 20
Phụ lục - Xử lý ảnh
if (Is256ColorFile)
for(Index=0;Index<MAX256PALETTECOLORS;Index++)
{
printf("\tPalette[ %X ] : R=%6X G=%6X B=%6X
/n,Index,PCXData.Palette[Index].Red,
PCXData.Palette[Index].Green,PCXData.Palette[Index].Blue);
if ((Index+1)%16==0)
{
getch();
clrscr();
if(Index!=0xFF)
printf("\n\tColor Register Value of PCX file: %s\n\n",
Filename);
}
}
else
for (Index=0;Index<MAXPALETTECOLORS;Index++)
{
printf("\tPalette[ %X ] : R=%6X G=%6X B=%6X
\n",Index,PCXData.Palette[Index].Red,
PCXData.Palette[Index].Green,PCXData.Palette[Index].Blue);
}
printf("\n\tHit any key to continue");
getch();
}
return(NoError);
}
static int ExpandScanLine(FILE *InFile)
{
register short BitNum;
register unsigned ByteNum;
register short CharRead;
unsigned InPtr,RepCount,PixelsData;
unsigned BytesToRead,PlaneNum;
unsigned ByteOffset,BitOffset;
BytesToRead=PCXData.Info.NumOfPlanes*PCXData.Info.BytesPerLin
e;
InPtr=0;
Nhập môn xử lý ảnh số - ĐHBK Hà nội 21
Phụ lục - Xử lý ảnh
do
{
CharRead=getc(InFile);
if(CharRead==EOF) return(FALSE);
if((CharRead & 0xC0)==0xC0)
{
RepCount=CharRead & ~0xC0;
CharRead=getc(InFile);
if(CharRead==EOF) return(FALSE);
while (RepCount ) ScanLine[InPtr++] = CharRead;
}
else
ScanLine[InPtr++]=CharRead;
} while (InPtr < BytesToRead);
return(TRUE);
}
unsigned InstallPCXFilePalette(void)
{
struct palettetype palette;
union REGS regs;
unsigned Index;
if(PCXData.PCXHeader.Version!=3)
{
if(Is256ColorFile)
{
for (Index=0;Index<MAX256PALETTECOLORS;Index++)
{
Color256Palette.Palette[Index].Red>>=2;
Color256Palette.Palette[Index].Green>>=2;
Color256Palette.Palette[Index].Blue>>=2;
}
regs.h.ah=0x10;
regs.h.al=0x12;
regs.x.bx=0;
regs.x.cx=MAX256PALETTECOLORS;
_ES=FP_SEG(&Color256Palette.Palette);
regs.x.dx=FP_OFF(&Color256Palette.Palette);
int86(VIDEO,&regs,&regs);
return(TRUE);
Nhập môn xử lý ảnh số - ĐHBK Hà nội 22
Phụ lục - Xử lý ảnh
}
else
{
palette.size=MAXPALETTECOLORS;
for (Index=0;Index<MAXPALETTECOLORS;Index++)
{
palette.colors[Index]=Index;
PCXData.Palette[Index].Red>>=2;
PCXData.Palette[Index].Green>>=2;
PCXData.Palette[Index].Blue>>=2;
}
regs.h.ah=0x10;
regs.h.al=0x12;
regs.x.bx=0;
regs.x.cx=MAXPALETTECOLORS;
_ES=FP_SEG(&PCXData.Palette);
int86(VIDEO,&regs,&regs);
setallpalette(&palette);
return(TRUE);
}
}
else return(FALSE);
}
void DisplayPCXFile(char *FileName,int Verbose)
{
int i;
BYTE far *PtrScreen;
register unsigned ScanNum;
register unsigned ColNum;
unsigned OffsetDisplay,BytePerLine;
int PCXError,color,Plane;
long CurrentPos;
if((PCXError=ReadPCXFileHdr(FileName,Verbose))!
=NoError)exit(PCXError);
if((PCXData.PCXHeader.X1!=0) || (PCXData.PCXHeader.Y1!=0))
{
printf("\nError PCC file not PCX file\n");
exit(EPCCFile);
}
Nhập môn xử lý ảnh số - ĐHBK Hà nội 23
Phụ lục - Xử lý ảnh
if(PCXData.PCXHeader.X2==319)
{
Set256ColorMode();
ImageWidth = 320;
ImageHeight= 200;
}
else
{
ImageWidth=640;
switch(PCXData.PCXHeader.Y2)
{
case 479: setgraphmode(VGAHI);
ImageHeight=480;break;
case 349: setgraphmode(VGAMED);
ImageHeight=350;break;
case 199: setgraphmode(VGALO);
ImageHeight=200;break;
}
}
CurrentPos=ftell(PCXFile);
fseek(PCXFile,-769,SEEK_END);
if(fread(&Color256Palette.ExtendedPalette,
sizeof(struct ExtendedPalette) ,1,PCXFile)==TRUE)
if(Color256Palette.ExtendedPalette==PCX256ColorTag)
Is256ColorFile=TRUE;
InstallPCXFilePalette();
fseek(PCXFile,CurrentPos,SEEK_SET);
for(ScanNum=0;ScanNum<ImageHeight;ScanNum++)
{
if(ExpandScanLine(PCXFile) != TRUE)
{
closegraph();
printf("\nScanLine corrupt in PCX file\n");
exit(ECorrupt);
}
PtrScreen=MK_FP(0xA000,0);
if(ImageWidth==320)
{
Nhập môn xử lý ảnh số - ĐHBK Hà nội 24

Xem chi tiết: Xử lý ảnh số -P17


Không có nhận xét nào:

Đăng nhận xét