问题在哪里
以下代码在一些显示器能运行!但一些会出现out of range(超出范围)错误·请高手指点:(图片名为logo是BMP类型)#include <dos.h>
#include <stdio.h>
#include <mem.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>
#define VBE800X600X64K 0X114
#define RGB(r,g,b) ((((unsigned int)(r)>>3)<<11) + (((unsigned int)(g)>>2)<<5) + ((b)>>3))
#define SCREEN_WIDTH 800L
#define SCREEN_HIGH 600L
#define VARM_GRAPH_800_600_256(x,y) (((unsigned long)y<<9L)+((unsigned long)y<<8L)+((unsigned long)y<<5L)+((unsigned long)(x)))
#define VARM_GRAPH_800_600_16M(x,y) (((((unsigned long)y<<9L)+((unsigned long)y<<8L)+((unsigned long)y<<5L))<<1L) + (((unsigned long)x)<<1L))
#define PALETTE_READ 0x3C7
#define PALETTE_WRITE 0x3C8
#define PALETTE_DATA 0x3C9
typedef struct tagBITMAPFILEHEADER
{
unsigned int bfType;
unsigned long bfSize;
unsigned int Reserved1;
unsigned int reserved2;
unsigned long bfOffset;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;
unsigned long biWidth;
unsigned long biHeight;
unsigned int biPlanes;
unsigned int biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXpolsPerMeter;
unsigned long biYpelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char Blue;
unsigned char Green;
unsigned char Red;
unsigned char Reserved;
}RGBQUAD;
typedef struct tagRGB16M
{
unsigned char Blue;
unsigned char Green;
unsigned char Red;
}RGB16M;
int g_cur_vbe_page = 0;
void _Cdecl InitGraph(unsigned int mode)
{
_AX = 0x4f02;
_BX = mode;
__int__(0x10);
if(_AH != 0)
{
puts("Can't Initialize the graphics mode!");
exit(1);
}
}
void _Cdecl CloseGraph(void)
{
_AX = 0x4f02;
_BX = 0x03;
__int__(0x10);
}
void _Cdecl set_vbe_page(int page)
{
if (g_cur_vbe_page != page)
{
_BX = 0;
_DX = g_cur_vbe_page = page;
_AX = 0x4F05;
__int__(0x10);
}
}
void PutPixel16M(int x,int y,int Color)
{
int page;
char far *videoptr = (char far *)0xa0000000L;
long addr = VARM_GRAPH_800_600_16M(x,y);/*移动xy*/
if (x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HIGH)
{
page = (int)(addr >> 16);
set_vbe_page(page);
*(videoptr + (unsigned int)(addr & 0xFFFF))= Color & 0xFF;
*(videoptr + (unsigned int)(addr & 0xFFFF)+1)= Color>>0x08;
}
}
void Show_BMP(char *File_Name,int k)/*k为缩小倍数*/
{
int i, j,width ;
register BITMAPFILEHEADER *FileHead;
register BITMAPINFOHEADER *InfoHead;
FILE *fp;
if ((FileHead = (BITMAPFILEHEADER *)malloc(sizeof(BITMAPFILEHEADER))) == NULL)
return;
if ((InfoHead = (BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER))) == NULL)
return;
if ((fp = fopen(File_Name,"rb")) == NULL)
{
printf("BMP File not exist ...");
return;
}
fread(FileHead,sizeof(BITMAPFILEHEADER),1,fp);
if (FileHead->bfType!='BM')
{
printf("BMP File type Error ...");
fclose(fp);
return;
}
fread(InfoHead,sizeof(BITMAPINFOHEADER),1,fp);
if (InfoHead->biCompression !=0 || (InfoHead->biBitCount!=8 && InfoHead->biBitCount!=24))
{
printf("BMP File not Support Compression type ...");
fclose(fp);
return;
}
width =((int)InfoHead->biWidth+3)/4*4;
if ((int)InfoHead->biBitCount == 24)
{
register RGB16M *buffer;
if ((buffer = malloc(width*sizeof(RGB16M))) == NULL)
{
fclose(fp);
return;
}
for (j = (int)InfoHead->biHeight-1 ; j >= 0 ; j--)
{
fread(buffer,width,sizeof(RGB16M),fp);
for (i = 0 ; i < width ; i++)
PutPixel16M(i/k,j/k,RGB(buffer[i].Red,buffer[i].Green,buffer[i].Blue));/*缩放*/
}
}
free(FileHead);
free(InfoHead);
fclose(fp);
}
void main(void)
{
InitGraph(VBE800X600X64K);
Show_BMP("logo.BMP",1);/*图像大小为800*600没有缩小*/
getch();/*按回车显示下一幅图片*/
CloseGraph();
}