Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct BITMAPFILEHEADER
- {
- int bfType;
- int bfSize;
- int bfReserved1;
- int bfReserved2;
- int bfOffBits;
- };
- struct BITMAPINFOHEADER
- {
- int biSize;
- int biWidth;
- int biHeight;
- int biPlanes;
- int biBitCount;
- int biCompression;
- int biSizeImage;
- int biXpelsPerMeter;
- int biYpelsPerMeter;
- int biCrlUses;
- int biCrlImportant;
- };
- struct kolor
- {
- unsigned char R;
- unsigned char G;
- unsigned char B;
- };
- void odczytaj_naglowek(fstream &obraz,BITMAPFILEHEADER &ob)
- {
- obraz.read(reinterpret_cast<char*>(&ob.bfType), 2);
- obraz.read(reinterpret_cast<char*>(&ob.bfSize), 4);
- obraz.read(reinterpret_cast<char*>(&ob.bfReserved1), 2);
- obraz.read(reinterpret_cast<char*>(&ob.bfReserved2), 2);
- obraz.read(reinterpret_cast<char*>(&ob.bfOffBits), 4);
- }
- int odczytaj_naglowek_obrazu(fstream &obraz,BITMAPINFOHEADER &ob)
- {
- obraz.read(reinterpret_cast<char*>(&ob.biSize), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biWidth ), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biHeight), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biPlanes), 2);
- obraz.read(reinterpret_cast<char*>(&ob.biBitCount), 2);
- obraz.read(reinterpret_cast<char*>(&ob.biCompression), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biSizeImage), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biXpelsPerMeter), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biYpelsPerMeter), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biCrlUses), 4);
- obraz.read(reinterpret_cast<char*>(&ob.biCrlImportant), 4);
- return obraz.tellg();
- }
- int policz(int i, char rgb, kolor tab[])
- {
- int p;
- switch (rgb)
- {
- case 'r':
- p = tab[i-1].R+3;
- if(p>255)
- {
- return 255;
- }
- else return p;
- break;
- case 'g':
- p= tab[i-1].G+3;
- if(p>255)
- {
- return 255;
- }
- else return p;
- break;
- case 'b':
- p= tab[i-1].B+3;;
- if(p>255)
- {
- return 255;
- }
- else return p;
- break;
- }
- }
- void odczytaj_obraz(fstream &obraz,unsigned char tab[], BITMAPINFOHEADER ob)
- {
- for(int i=0; i<ob.biSizeImage/3; i++)
- {
- obraz.read(reinterpret_cast<char*>(&tab[i]),3);
- }
- obraz.clear();
- }
- void koloruj(unsigned char tab[], kolor tab_k[], BITMAPINFOHEADER ob)
- {
- char rgb;
- for(int i=0; i<ob.biSizeImage/3; i++)
- {
- if(tab[i]==0)
- {
- tab_k[i].R=0;
- tab_k[i].G=0;
- tab_k[i].B=0;
- }
- NULL;
- if(tab[i]>0 && tab[i]<86)
- {
- rgb = 'r';
- tab_k[i].R=policz(i, rgb,tab_k);
- tab_k[i].G=0;
- tab_k[i].B=0;
- }
- NULL;
- if(tab[i]==86)
- {
- tab_k[i].R=255;
- tab_k[i].G=0;
- tab_k[i].B=0;
- }
- NULL;
- if(tab[i]>86 && tab[i]<171)
- {
- rgb = 'b';
- tab_k[i].R=255;
- tab_k[i].G=policz(i, rgb,tab_k);
- tab_k[i].B=0;
- }
- NULL;
- if(tab[i]==171)
- {
- tab_k[i].R=255;
- tab_k[i].G=255;
- tab_k[i].B=0;
- }
- NULL;
- if(tab[i]>171 && tab[i]<256)
- {
- rgb = 'b';
- tab_k[i].R=255;
- tab_k[i].G=255;
- tab_k[i].B=policz(i, rgb, tab_k);
- }
- NULL;
- }
- }
- void skopuj_obraz(fstream &obraz_src,fstream &obraz_des,kolor tab[], BITMAPINFOHEADER ob, BITMAPFILEHEADER ob1)
- {
- obraz_src.seekg(0);
- obraz_des.seekg(0);
- int k;
- for(int i=0; i<ob1.bfOffBits; i++)
- {
- obraz_src.read(reinterpret_cast<char*>(&k),1);
- obraz_des.write(reinterpret_cast<char*>(&k),1);
- }
- for(int i=0; i<ob.biSizeImage/3; i++)
- {
- obraz_des.put(tab[i].B);
- obraz_des.put(tab[i].G);
- obraz_des.put(tab[i].R);
- }
- }
- int main()
- {
- fstream obraz;
- BITMAPFILEHEADER FileInfo;
- BITMAPINFOHEADER PictureInfo;
- obraz.open("sonar_aktywny_szary.bmp", ios::in | ios::binary);
- if(!obraz)
- {
- cout<<"Nie mozna otworzyc pliku!"<<endl;
- return 0;
- }
- odczytaj_naglowek(obraz,FileInfo);
- odczytaj_naglowek_obrazu(obraz,PictureInfo);
- unsigned char *pixmap;
- pixmap=new unsigned char[PictureInfo.biSizeImage/3];
- kolor *kolormap;
- kolormap=new kolor [PictureInfo.biSizeImage/3];
- odczytaj_obraz(obraz,pixmap, PictureInfo);
- koloruj(pixmap, kolormap, PictureInfo);
- fstream obraz_des;
- obraz_des.open("sonar_aktywny_kolorowy2.bmp", ios::out | ios::binary);
- if(!obraz_des)
- {
- cout<<"Nie mozna otworzyc pliku!"<<endl;
- return 0;
- }
- skopuj_obraz(obraz,obraz_des,kolormap,PictureInfo,FileInfo);
- obraz.close();
- obraz_des.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement