- #include<iostream>
- #include<unistd.h>
- #include<vector>
- #include<string>
- #include<fstream>
- //using std::cout;
- //using std::endl;
- //using std::ofstream;
- //using std::ifstream;
- using namespace std;
- #pragma pack(1)
- #pragma once
- typedef int LONG;
- typedef unsigned short WORD;
- typedef unsigned int DWORD;
- typedef struct tagBITMAPFILEHEADER
- {
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffBits;
- } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
- typedef struct tagBITMAPINFOHEADER
- {
- DWORD biSize;
- LONG biWidth;
- LONG biHeight;
- WORD biPlanes;
- WORD biBitCount;
- DWORD biCompression;
- DWORD biSizeImage;
- LONG biXPelsPerMeter;
- LONG biYPelsPerMeter;
- DWORD biClrUsed;
- DWORD biClrImportant;
- } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
- unsigned char** reds;
- unsigned char** greens;
- unsigned char** blues;
- unsigned char** grays;
- int rows;
- int cols;
- int bitrate;
- vector <string> slownik;
- void inicjuj_bmp(vector <string>& baza)
- {
- string x;
- for(int i=0; i<255; i++)
- {
- x=(char)i;
- baza.push_back(x);
- }
- }
- bool przeszukajSlownik(string napis,vector <string> baza)
- {
- for(unsigned int i=0; i<baza.size(); i++)
- {
- if(baza[i]==napis)
- return true;
- }
- }
- int wypiszIndexVektor(string napis,vector <string> baza)
- {
- for(unsigned int i=0; i<baza.size(); i++)
- {
- if(baza[i] ==napis)
- return i;
- }
- }
- void wypiszVektor(vector <string> baza)
- {
- for(unsigned int i=0; i<baza.size(); i++)
- {
- cout<<"zawartosc slownika index-> "<<i<<" wartosc -> "<<baza[i]<<endl;
- }
- }
- void wypiszKolor24()
- {
- int pixel=0;
- for(int i = 0; i < rows; i++)
- {
- //fread(data, sizeof(unsigned char), row_padded, f);
- for(int j = 0; j < cols; j ++)
- {
- std::cout<<"pixel "<<pixel<<"r "<<(int)reds[i][j]<<"g "<<(int)greens[i][j]<<"b "<<(int)blues[i][j]<<std::endl;
- pixel++;
- }
- }
- }
- void wypiszKolor8()
- {
- int pixel=0;
- for(int i = 0; i < rows; i++)
- {
- //fread(data, sizeof(unsigned char), row_padded, f);
- for(int j = 0; j < cols; j ++)
- {
- std::cout<<"pixel "<<pixel<<"szary "<<(int)grays[i][j]<<std::endl;
- pixel++;
- }
- }
- }
- void ZamienSzarosc()
- {
- for(int i = 0; i < rows; i++)
- {
- //fread(data, sizeof(unsigned char), row_padded, f);
- for(int j = 0; j < cols; j ++)
- {
- grays[i][j] = int(0.212671 * reds[i][j] +
- 0.715160 * greens[i][j] +
- 0.072169 * blues[i][j] + 0.5);
- if (grays[i][j]<0)
- grays[i][j] = 0;
- if (grays[i][j]>255)
- grays[i][j]= 255;
- }
- }
- }
- void RGB_Allocate(unsigned char**& dude)
- {
- dude = new unsigned char*[rows];
- for (int i = 0; i < rows; i++)
- dude[i] = new unsigned char[cols];
- }
- bool FillAndAllocate(char*& buffer, const char* Picture, int& rows, int& cols, int& BufferSize,int& bitrate) //1 bez bledow, 0 sa problemy
- {
- ifstream file(Picture);
- if (file)
- {
- file.seekg(0, std::ios::end);
- std::streampos length = file.tellg();
- file.seekg(0, std::ios::beg);
- buffer = new char[length];
- file.read(&buffer[0], length);
- PBITMAPFILEHEADER file_header;
- PBITMAPINFOHEADER info_header;
- file_header = (PBITMAPFILEHEADER) (&buffer[0]);
- info_header = (PBITMAPINFOHEADER) (&buffer[0] + sizeof(BITMAPFILEHEADER));
- rows = info_header->biHeight;
- cols = info_header->biWidth;
- bitrate = info_header-> biBitCount;
- BufferSize = file_header->bfSize;
- return 1;
- }
- else
- {
- cout << "File" << Picture << " don't Exist!" << endl;
- return 0;
- }
- }
- void GetPixlesFromBMP24(unsigned char** reds, unsigned char** greens, unsigned char** blues, int end, int rows, int cols, char* FileReadBuffer)
- {
- int count = 1;
- int extra = cols % 4;
- for (int i = 0; i < rows; i++)
- {
- count += extra;
- for (int j = cols - 1; j >= 0; j--)
- for (int k = 0; k < 3; k++)
- {
- switch (k)
- {
- case 0:
- reds[i][j] = FileReadBuffer[end - count++];
- break;
- case 1:
- greens[i][j] = FileReadBuffer[end - count++];
- break;
- case 2:
- blues[i][j] = FileReadBuffer[end - count++];
- break;
- }
- }
- }
- }
- void GetPixlesFromBMP8(unsigned char** grays, int end, int rows, int cols, char* FileReadBuffer) // end is BufferSize (total size of file)
- {
- int count = 1;
- ofstream kolor("grays.txt");
- int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
- for (int i = 0; i < rows; i++)
- {
- //count += extra;
- for (int j = cols - 1; j >= 0; j--)
- //for (int k = 0; k < 3; k++)
- {
- grays[i][j] = FileReadBuffer[end - count++];
- kolor<<(int)grays[i][j]<<endl;
- // cout<<" zawarotsc grays "<<grays[i][j]<<endl;
- // cout<<" zawarotsc (int) grays "<<(int)grays[i][j]<<endl;
- }
- }
- }
- void WriteOutBmp24(char* FileBuffer, const char* NameOfFileToCreate, int BufferSize)
- {
- std::ofstream write(NameOfFileToCreate);
- if (!write)
- {
- cout << "Failed to write " << NameOfFileToCreate << endl;
- return;
- }
- int count = 1;
- int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
- for (int i = 0; i < rows; i++)
- {
- count += extra;
- for (int j = cols - 1; j >= 0; j--)
- for (int k = 0; k < 3; k++)
- {
- switch (k)
- {
- case 0: //reds
- FileBuffer[BufferSize - count] = reds[i][j];
- break;
- case 1: //green
- FileBuffer[BufferSize - count] = greens[i][j];
- break;
- case 2: //blue
- FileBuffer[BufferSize - count] = blues[i][j];
- break;
- }
- count++;
- }
- }
- write.write(FileBuffer, BufferSize);
- }
- void WriteOutBmp8(char* FileBuffer, const char* NameOfFileToCreate, int BufferSize)
- {
- std::ofstream write(NameOfFileToCreate);
- if (!write)
- {
- cout << "Failed to write " << NameOfFileToCreate << endl;
- return;
- }
- int count = 1;
- // int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
- for (int i = 0; i < rows; i++)
- {
- // count +=extra;
- for (int j = cols - 1; j >= 0; j--)
- {
- // for (int k = 0; k < 3; k++) {
- FileBuffer[BufferSize] = grays[i][j];
- // count++;
- }
- // }
- }
- write.write(FileBuffer, BufferSize);
- }
- int LZW(unsigned char** grays)
- {
- ofstream wynik("wynik.txt");
- // vector <string> slownik;
- std::string kontener,lancuch;
- char znak;
- inicjuj_bmp(slownik);
- for(int i = 0; i < rows; i++)
- {
- for(int j = 0; j < cols; j ++)
- {
- if(i&&j==0)
- kontener= grays[0][0];
- else
- {
- znak=grays[i][j];
- lancuch=kontener+znak;
- if (przeszukajSlownik(lancuch,slownik))
- {
- kontener=kontener+znak;
- cout<<"lancuch w slowniku "<<lancuch<<endl;
- cout<<"kontener lancucha w slowniku "<<kontener<<endl;
- }
- else
- {
- cout<<"lancuch brak lancucha w slowniku "<<lancuch<<endl;
- cout<<"kontener brak lancucha w slowniku "<<kontener<<endl;
- slownik.push_back(lancuch);
- wynik<<wypiszIndexVektor(kontener,slownik)//<<" oraz znak "<<slownik[wypiszIndexVektor(kontener,slownik)]
- <<endl;
- cout<<"index kontenera w slowniku"<<wypiszIndexVektor(kontener,slownik)<<endl; // w przysz³osci wynik zapisujemy do odobnego pliku wynikowego
- kontener=znak;
- cout<<"kontener nowy znak "<<kontener<<endl;
- }
- cout<<"lancuch po ifie to "<<lancuch<<endl;
- cout<<"kontener po ifie to "<<kontener<<endl;
- cout<<"slownik jest rozmiaru : "<<slownik.size()<<endl;
- cout<<endl;
- //wypiszVektor(slownik);
- }
- }
- }
- return 1;
- }
- int rozpakuj ()
- {
- vector <string> slownik;
- ifstream plik;
- ofstream wynik("wynik2.bmp");
- //ofstream wynik2("wynik2INT.txt");
- plik.open("wynik.txt",ios_base::in);
- char znak,koniec;
- int index;
- std::string lancuch,kontener,liczba;
- //tu koncza sie zmienne
- // slownik.push_back("a");
- // slownik.push_back("b");
- // slownik.push_back("c");
- // slownik.push_back("d");
- // slownik.push_back("_");
- inicjuj_bmp(slownik);
- if (plik==NULL)
- perror ("Error opening file");
- else
- {
- plik>>index;
- kontener=slownik[index];
- wynik<<kontener<<endl;
- // wynik2<<(int)slownik[index]<<endl;
- while(plik>>index)
- {
- cout<<"wartosc index w petli index==slownik "<<index<<endl;
- cout<<"wartosc slownika w petli index==slownik "<<slownik[index]<<endl;
- if(index==slownik.size())
- {
- lancuch=kontener+kontener[0];
- slownik.push_back(lancuch);
- }
- else
- {kontener=slownik[index];}
- wynik<<kontener;
- lancuch=kontener+slownik[index][0];
- slownik.push_back(lancuch);
- cout<<"kontener petla index==slownik "<<kontener<<endl;
- cout<<"lancuch petla index==slownik "<<lancuch<<endl;
- // wynik2<<(int) kontener<<endl;
- };
- cout<<"rozmiar wektora slownika"<<slownik.size()<<endl;
- for(int n=0; n<slownik.size(); n++)
- {
- std::cout << slownik[n] << '\n';
- }
- wynik.close();
- plik.close();
- cout << "Hello world!" << endl;
- }
- }
- int main(int args, char** cat)
- {
- char* FileBuffer;
- int BufferSize;
- vector <string> slownik;
- #define Picture "ReadInPicture3.bmp"
- // #define Picture "OutputPicture.bmp"
- if (!FillAndAllocate(FileBuffer, Picture, rows, cols, BufferSize,bitrate))
- {
- cout << "File read error" << endl;
- return 0;
- }
- cout << "Rows: " << rows << " Cols: " << cols <<"bitrate "<<bitrate<<"bufersize "<<BufferSize<<"filebuffer"<<FileBuffer<<endl;
- RGB_Allocate(reds);
- RGB_Allocate(greens);
- RGB_Allocate(blues);
- RGB_Allocate(grays);
- //GetPixlesFromBMP24( reds, greens, blues,BufferSize, rows, cols, FileBuffer);
- GetPixlesFromBMP8(grays,BufferSize,rows,cols,FileBuffer);
- //wypiszKolor24();
- //wypiszKolor8(); //wypisuje inta z grays[i][j]
- //LZW(grays);
- // (*PBITMAPINFOHEADER ).biBitCount=8; // biBitCount=8;
- //ZamienSzarosc();
- #define plik_zapisu "OutputPicture.bmp"
- bitrate=24;
- cout<<BufferSize;
- WriteOutBmp8(FileBuffer, plik_zapisu,BufferSize);
- rozpakuj();
- //WriteOutBmp24(FileBuffer, WriteOutFile,BufferSize);
- return 1;
- }