#include <stdio.h> #include <stdlib.h> #include <string.h> int getWH(int *w, int *h, const char *fileName) { char bmpName[128]; // bmp file name FILE *fp=NULL; // FILE pointer unsigned char header[54]; // for bmp header look ahead int temp=0, width = 0, height = 0 ; // local variable for calculation int i = 0; // loop variable sprintf(bmpName,"%s.bmp",fileName);// 24bit bit map 未使用調色盤 if( !(fp = fopen(bmpName,"rb") ) ) return -1; fread( header, sizeof(unsigned char), 54, fp); // read bmp header into header[54] for( i = 0 ; i != 4 ; ++i ) // get width from byte 18 ~ byte 22 in header[54] { temp = (long)header[18+i]; temp = temp <<( 8*i ); width += temp; } for( i = 0 ; i != 4 ; ++i ) // get width from byte 22 ~ byte 26 in header[54] { temp = (long)header[22+i]; temp = temp <<( 8*i ); height += temp; } *w = width; *h = height; fclose(fp); return 0; }//end if getWT int bmp_read( unsigned char *image, int xSize, int ySize, const char *fileName ) { char fnameBmp[128]={0}; FILE *fp=NULL; unsigned char header[54]={0}; // for bmp header sprintf( fnameBmp,"%s.bmp",fileName); if( !(fp = fopen(fnameBmp,"rb") ) ) return -1; fread( header, sizeof(unsigned char), 54, fp); // read bmp header into header[54], based on byte fread( image, sizeof(unsigned char), (size_t)(long)xSize * ySize * 3, fp); // read image array, based on byte fclose(fp); return 0; }//end of function bmp read int bmp_write( unsigned char* image, int xSize, int ySize, char *fileName) { unsigned char header[54]={ 0x42,0x4d, 0, 0, 0, 0, 0, 0, 0 , 0, // 0~1 st byte : 'BM' 54, 0, 0, 0, 40, 0, 0, 0, 0 , 0, // 10th byte : offset = 54 = header size 0, 0, 0, 0, 0, 0, 1, 0,24, 0, // 26th byte : 圖層數,28th byte : 24 bit 點陣圖 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // sizeof(long) = 4 long fileSize = (long)xSize * (long)ySize * 3 +54 ; // image array size + 54 long temp = 0; // temp for byte convertion long width = xSize; // image width long height = ySize; // image height char bmpName[128]={0}; // filename int i = 0; // loop variable FILE *fp; for( i=0 ; i != 4 ; ++i) // write fileSize from byte2 ~ byte 6 { header[2+i] = (unsigned char)( fileSize >> (8*i) ) & 0x000000ff; } for( i=0 ; i != 4 ; ++i) // write width from byte18 ~ byte 22 { header[18+i] = (unsigned char)( width >> (8*i) ) & 0x000000ff; } for( i=0 ; i != 4 ; ++i) // write height from byte22 ~ byte 26 { header[22+i] = (unsigned char)( height >> (8*i) ) & 0x000000ff; } sprintf(bmpName,"%s.bmp",fileName); if( ! ( fp = fopen(bmpName,"wb") ) ) // 檔案名稱 return -1; fwrite(header, sizeof(unsigned char), 54, fp); // write header fwrite(image, sizeof(unsigned char),(size_t)(long)xSize*ySize*3, fp); // write image array fclose(fp); return 0; }//end of function bmp_write int main() { unsigned char *image=NULL; int xSize = 0; int ySize = 0; char fileName[128]={0}; printf("Please input the filename to load\n"); scanf("%s",fileName); getWH(&xSize, &ySize, fileName); // get image size by calling getWH image = (unsigned char*)malloc( (size_t) xSize * ySize * 3 ); if( image == NULL ) // if fail to open file return -1; bmp_read( image, xSize, ySize, fileName); strcat(fileName," copy"); // change file name with copy postfix bmp_write(image,xSize, ySize,fileName); // write to outpit file free(image); // relese 2D image array return 0; }//end of main
pure C read write bmp file
最新推荐文章于 2024-08-14 16:51:34 发布