pure C read write bmp file
2013-08-22 18:58
253 查看
#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
相关文章推荐
- read and write '.bmp' file
- Pure_C_Read and Write PPM、PGM、PBM Three Image File Formats
- oc write read file
- WriteReadFileDemo
- (10) MPI-IO -- fileview: MPI_File_read_all MPI_File_write_all
- Sample of C++ read/write a text file
- 【体会Python之美】【文件】file,open,read,readline,readlines,write,close
- IniFile Read Write
- C language : write/read for file
- 在linux内核中操作文件的方法--使用get_fs()和set_fs(KERNEL_DS) .bin file write/read
- Database 'xxx' cannot be upgraded because it is read-only or has read-only file Make the database or files writeable, and rerun recovery.
- Read and Write NSArray, NSDictionary and NSSet to a File
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- Qt Write and Read XML File 读写XML文件
- 作品[RFileRead/WriteStream 练习] for s60 fp2
- C# modify file (read all and modify and write all)
- Example : Linux kernal file read and write
- <Win32 API> 文件操作CreateFile/ReadFile/WriteFile
- Read / Write Excel file in Java using Apache POI
- How to mount partition with ntfs file system and read write access