線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2199
推到 Plurk!
推到 Facebook!

bmp讀檔及寫入的問題

尚未結案
dullvictory
一般會員


發表:5
回覆:2
積分:1
註冊:2005-08-16

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-08-16 23:23:53 IP:59.113.xxx.xxx 未訂閱
#include  #include "stdio.h" #include "stdlib.h" struct BmpHead { WORD bfType; DWORD bfSize; DWORD bfReserved; DWORD bfOffBits; DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; }; unsigned char* ReadImage(char* file,int ImageSize,int bfOffBits); BmpHead * ReadHead(char* file); char* ReadColorMap(char* file,int MapSize); void WriteToFile(char* filename,BmpHead* Head,char* ColorMap,unsigned char* Image); void main(void) { char* filename = "c:\\herogo.bmp"; BmpHead* bmHead; char* ColorMap; unsigned char* Image; bmHead = ReadHead(filename); ColorMap = ReadColorMap(filename,bmHead->bfOffBits-54); Image = ReadImage(filename,bmHead->biSizeImage,bmHead->bfOffBits); WriteToFile("d:\\gogo.bmp",bmHead,ColorMap,Image); } char* ReadColorMap(char* file,int MapSize){ FILE* pic; char* ColorMap; ColorMap = new char[MapSize]; if( (pic = fopen(file, "rb" )) == NULL ) exit( 0 ); fseek(pic,54,SEEK_SET); fread(ColorMap,sizeof(char),MapSize,pic); fclose(pic); return ColorMap; } unsigned char* ReadImage(char* file,int ImageSize,int bfOffBits){ unsigned char* raw_data; raw_data=new unsigned char[ImageSize]; FILE* pic; if( (pic = fopen(file, "rb" )) == NULL ) exit( 0 ); fseek(pic,bfOffBits,SEEK_SET); fread(raw_data,sizeof(char),ImageSize,pic); fclose(pic); return raw_data; } BmpHead* ReadHead(char* file){ FILE* pic; typedef long INT32; typedef unsigned short int INT16; typedef unsigned char U_CHAR; #define UCH(x) ((int) (x)) #define GET_2B(array,offset) ((INT16) UCH(array[offset]) (((INT16) UCH(array[offset 1])) << 8)) #define GET_4B(array,offset) ((INT32) UCH(array[offset]) (((INT32) UCH(array[offset 1])) << 8) (((INT32) UCH(array[offset 2])) << 16) (((INT32) UCH(array[offset 3])) << 24)) #define FREAD(file,buf,sizeofbuf) ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file))) U_CHAR bmpfileheader[14] = { 0 } ; U_CHAR bmpinfoheader[40] = { 0 } ; if( (pic = fopen(file, "rb" )) == NULL ) exit( 0 ); FREAD(pic,bmpfileheader,14); FREAD(pic,bmpinfoheader,40); BmpHead* bmHead; bmHead = new BmpHead; bmHead->bfType = GET_2B(bmpfileheader,0); bmHead->bfSize = GET_4B(bmpfileheader,2); bmHead->bfReserved = GET_4B(bmpfileheader,6); bmHead->bfOffBits = GET_4B(bmpfileheader,10); bmHead->biSize = GET_4B(bmpinfoheader,0); bmHead->biWidth = GET_4B(bmpinfoheader,4); bmHead->biHeight = GET_4B(bmpinfoheader,8); bmHead->biPlanes = GET_4B(bmpinfoheader,12); bmHead->biBitCount = GET_2B(bmpinfoheader,12); bmHead->biCompression = GET_2B(bmpinfoheader,16); bmHead->biSizeImage = GET_4B(bmpinfoheader,20); bmHead->biXPelsPerMeter = GET_4B(bmpinfoheader,24); bmHead->biYPelsPerMeter = GET_4B(bmpinfoheader,28); bmHead->biClrUsed = GET_4B(bmpinfoheader,32); bmHead->biClrImportant = GET_4B(bmpinfoheader,36); fclose(pic); printf("printm\n",sizeof(BmpHead)); printf("printm\n",bmHead[1]); return bmHead; } void WriteToFile(char* filename,BmpHead* Head,char* ColorMap,unsigned char* Image){ FILE *out; if( (out = fopen( filename, "wb" )) != NULL ) { int a; a=sizeof(BmpHead); a=a-2; printf("print:m\n",a); fwrite(Head,a,1,out); fwrite(ColorMap,sizeof(char),(Head->bfOffBits-54),out); fwrite(Image,sizeof(char),Head->biSizeImage,out); } fclose(out); } 各位大大們好..小弟有程式上的問題想請教一下 上面的程式主要是把..在C碟的一張BMP..把BMP讀出...再寫入至D碟變成一模一樣的BMP..二張圖是一樣的..功能有點像COPY...主要是因為老師想要我們去知道BMP的格式...但問題來了..就是..編譯成功.但卻..在D碟的BMP去開啟時卻有問題..開不起來..應該是失敗了..還有一點..一直覺得很怪就是我明明讀了54個BYTE到BmpHead,但是BmpHead卻是56Byte..因為我有用sizeof(BmpHead)看出來..拜託高手們..指點迷津....拜託...拜託..很急..><
系統時間:2024-04-25 19:51:52
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!