读取位图函数Load_Bitmap_File

来源:互联网 发布:韶关网络布线公司 编辑:程序博客网 时间:2024/06/05 01:01
int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename){// this function opens a bitmap file and loads the data into bitmapint file_handle,  // the file handleindex;        // looping indexUCHAR   *temp_buffer = NULL; // used to convert 24 bit images to 16 bitOFSTRUCT file_data;          // the file data information// open the file if it existsif ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)return(0);// now load the bitmap file header_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));// test if this is a bitmap fileif (bitmap->bitmapfileheader.bfType!=BITMAP_ID){// close the file_lclose(file_handle);// return errorreturn(0);} // end if// now we know this is a bitmap, so read in all the sections// first the bitmap infoheader// now load the bitmap file header_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));// now load the color palette if there is oneif (bitmap->bitmapinfoheader.biBitCount == 8){_lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));// now set all the flags in the palette correctly and fix the reversed // BGR RGBQUAD data formatfor (index=0; index < MAX_COLORS_PALETTE; index++){// reverse the red and green fieldsint temp_color                = bitmap->palette[index].peRed;bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;bitmap->palette[index].peBlue = temp_color;// always set the flags word to thisbitmap->palette[index].peFlags = PC_NOCOLLAPSE;} // end for index} // end if// finally the image data itself_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);// now read in the image, if the image is 8 or 16 bit then simply read it// but if its 24 bit then read it into a temporary area and then convert// it to a 16 bit imageif (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 || bitmap->bitmapinfoheader.biBitCount==24){// delete the last image if there was oneif (bitmap->buffer)free(bitmap->buffer);// allocate the memory for the imageif (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage))){// close the file_lclose(file_handle);// return errorreturn(0);} // end if// now read it in_lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);} // end ifelse{// serious problemreturn(0);} // end else#if 0// write the file info out printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",filename,bitmap->bitmapinfoheader.biSizeImage,bitmap->bitmapinfoheader.biWidth,bitmap->bitmapinfoheader.biHeight,bitmap->bitmapinfoheader.biBitCount,bitmap->bitmapinfoheader.biClrUsed,bitmap->bitmapinfoheader.biClrImportant);#endif// close the file_lclose(file_handle);// flip the bitmapFlip_Bitmap(bitmap->buffer, bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8), bitmap->bitmapinfoheader.biHeight);// return successreturn(1);} // end Load_Bitmap_File

原创粉丝点击