Guest User

Untitled

a guest
Apr 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. void BMP_Image::writeToFile(std::string& str){
  2. dword extra,i,j=0;
  3. byte gelezenByte;
  4.  
  5. ofstream ofile;
  6. ofile.open(str.c_str(),ios::binary);
  7.  
  8. //leest variabele fileheader bitmap en schrijft naar file str
  9. ofile.write((char*)(&fh.bfType),sizeof(word));
  10. ofile.write((char*)(&fh.bfSize),sizeof(dword));
  11. ofile.write((char*)(&fh.bfReserved1),sizeof(word));
  12. ofile.write((char*)(&fh.bfReserved2),sizeof(word));
  13. ofile.write((char*)(&fh.bfOffBits),sizeof(dword));
  14.  
  15. //leest variabele infohearder bitmap en schrijft naar file str
  16. ofile.write((char*)(&ih.biSize),sizeof(dword));
  17. ofile.write((char*)(&ih.biWidth),sizeof(dword));
  18. ofile.write((char*)(&ih.biHeight),sizeof(dword));
  19. ofile.write((char*)(&ih.biPlanes),sizeof(word));
  20. ofile.write((char*)(&ih.biBitCount),sizeof(word));
  21. ofile.write((char*)(&ih.biCompression),sizeof(dword));
  22. ofile.write((char*)(&ih.biSizeImage),sizeof(dword));
  23. ofile.write((char*)(&ih.biXpelsPerMeter),sizeof(dword));
  24. ofile.write((char*)(&ih.biYpelsPerMeter),sizeof(dword));
  25. ofile.write((char*)(&ih.biClrUsed),sizeof(dword));
  26. ofile.write((char*)(&ih.biClrImportant),sizeof(dword));
  27.  
  28. //ih.biWidth=aantal pixels breedte
  29. //1 pixel=24 bits (voor 24bits bitmap)
  30. //dan dienen we nog te aligneren
  31. //dit door optellen van 32-(rest bij 32)
  32. //stel 72 bits heeft rest 8 maar dit is tgv 64, dus daarom 32-8
  33. //dan nog in bytes zetten 8bit=1byte
  34. extra=((32-(ih.biWidth*24)%32)/8)%4;
  35.  
  36.  
  37. //image uitschrijven
  38. for(i=0;i<ih.biHeight;i++){ //schrijven zolang er bytes zijn
  39. //bepalen welk byte te schrijven
  40. //na elke rij nullen toevoegen totdat veelvoud van 32 bits
  41. for(j=0;j<ih.biWidth;j++){
  42. gelezenByte=image[i][j].getRed();
  43. ofile.write((char*)&gelezenByte,sizeof(byte));
  44. gelezenByte=image[i][j].getGreen();
  45. ofile.write((char*)&gelezenByte,sizeof(byte));
  46. gelezenByte=image[i][j].getBlue();
  47. ofile.write((char*)&gelezenByte,sizeof(byte));
  48. }
  49. gelezenByte=0;
  50. ofile.write((char*)(&gelezenByte),extra*sizeof(byte));
  51. }
  52.  
  53. ofile.close();
  54. }
Add Comment
Please, Sign In to add comment