1. 程式人生 > >(原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)

(原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)

{
 47InBlock.gif  constint headerSize =54;
 48InBlock.gif  
 49ExpandedSubBlockStart.gifContractedSubBlock.gif  char header[headerSize] =dot.gif{
 50InBlock.gif      0x420x4d00000000,
 51InBlock.gif        54000400000000000010240
 52InBlock.gif        00000000000000000000
 53InBlock.gif        0000 54ExpandedSubBlockEnd.gif    }
;
 55InBlock.gif    
 56InBlock.gif  int ysize = imageVec.size();
 57InBlock.gif  int xsize = imageVec[0].size();
 58InBlock.gif    
 59InBlock.gif  long file_size = (long)ysize * xsize *3+54;
 60InBlock.gif  header[2= (unsigned char)(file_size &0x000000ff);
 61InBlock.gif  header[3= (file_size >>8&0x000000ff;
 62InBlock.gif  header[4= (file_size >>16&0x000000ff;
 63InBlock.gif  header[5= (file_size >>24&0x000000ff;
 64InBlock.gif    
 65InBlock.gif  long width = xsize;
 66InBlock.gif  header[18
= width &0x000000ff;
 67InBlock.gif  header[19= (width >>8&0x000000ff;
 68InBlock.gif  header[20= (width >>16&0x000000ff;
 69InBlock.gif  header[21= (width >>24&0x000000ff;
 70InBlock.gif    
 71InBlock.gif  long height = ysize;
 72InBlock.gif  header[22= height &0x000000ff;
 73InBlock.gif  header[23= (height >>8&0x000000ff;
 74InBlock.gif  header[24= (height >>16
&0x000000ff;
 75InBlock.gif  header[25= (height >>24&0x000000ff;
 76InBlock.gif    
 77InBlock.gif  ofstream file(fileName,ios::out| ios::binary);
 78InBlock.gif  if (!file)
 79InBlock.gif    returnfalse;
 80InBlock.gif  
 81InBlock.gif  // write header   82InBlock.gif  file.write(header, headerSize);
 83InBlock.gif  // write body 84ExpandedSubBlockStart.gifContractedSubBlock.giffor(size_t y =0; y != imageVec.size(); ++y) dot.gif{
 85ExpandedSubBlockStart.gifContractedSubBlock.gif    for(size_t x =0; x != imageVec[0].size(); ++x) dot.gif{
 86InBlock.gif      char chB = imageVec[y][x].B;
 87InBlock.gif      char chG = imageVec[y][x].G;
 88InBlock.gif      char chR = imageVec[y][x].R;
 89InBlock.gif      
 90InBlock.gif      file.put(chB).put(chG).put(chR);
 91ExpandedSubBlockEnd.gif    }
 92ExpandedSubBlockEnd.gif  }
 93InBlock.gif  
 94InBlock.gif  file.close();
 95InBlock.gif  
 96InBlock.gif  returntrue;
 97ExpandedBlockEnd.gif}