Facebook
From Diminutive Owl, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 234
  1. #include<iostream>
  2. #include<unistd.h>
  3. #include<vector>
  4. #include<string>
  5. #include<fstream>
  6.  
  7. //using std::cout;
  8. //using std::endl;
  9. //using std::ofstream;
  10. //using std::ifstream;
  11. using namespace std;
  12.  
  13. #pragma pack(1)
  14. #pragma once
  15.  
  16. typedef int LONG;
  17. typedef unsigned short WORD;
  18. typedef unsigned int DWORD;
  19.  
  20. typedef struct tagBITMAPFILEHEADER
  21. {
  22.     WORD bfType;
  23.     DWORD bfSize;
  24.     WORD bfReserved1;
  25.     WORD bfReserved2;
  26.     DWORD bfOffBits;
  27. } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
  28.  
  29. typedef struct tagBITMAPINFOHEADER
  30. {
  31.     DWORD biSize;
  32.     LONG biWidth;
  33.     LONG biHeight;
  34.     WORD biPlanes;
  35.     WORD biBitCount;
  36.     DWORD biCompression;
  37.     DWORD biSizeImage;
  38.     LONG biXPelsPerMeter;
  39.     LONG biYPelsPerMeter;
  40.     DWORD biClrUsed;
  41.     DWORD biClrImportant;
  42. } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
  43.  
  44. unsigned char** reds;
  45. unsigned char** greens;
  46. unsigned char** blues;
  47. unsigned char** grays;
  48. int rows;
  49. int cols;
  50. int bitrate;
  51. vector <string> slownik;
  52.  
  53.  
  54. void inicjuj_bmp(vector <string>& baza)
  55. {
  56.     string x;
  57.     for(int i=0; i<255; i++)
  58.     {
  59.         x=(char)i;
  60.         baza.push_back(x);
  61.     }
  62. }
  63.  
  64. bool przeszukajSlownik(string napis,vector <string> baza)
  65. {
  66.     for(unsigned int i=0; i<baza.size(); i++)
  67.     {
  68.         if(baza[i]==napis)
  69.             return true;
  70.  
  71.     }
  72. }
  73. int wypiszIndexVektor(string napis,vector <string> baza)
  74. {
  75.     for(unsigned int i=0; i<baza.size(); i++)
  76.     {
  77.         if(baza[i] ==napis)
  78.             return i;
  79.     }
  80. }
  81.  
  82. void wypiszVektor(vector <string> baza)
  83. {
  84.     for(unsigned int i=0; i<baza.size(); i++)
  85.     {
  86.         cout<<"zawartosc slownika index-> "<<i<<" wartosc -> "<<baza[i]<<endl;
  87.     }
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. void wypiszKolor24()
  97. {
  98.     int pixel=0;
  99.     for(int i = 0; i < rows; i++)
  100.     {
  101.         //fread(data, sizeof(unsigned char), row_padded, f);
  102.         for(int j = 0; j < cols; j ++)
  103.         {
  104.  
  105.             std::cout<<"pixel "<<pixel<<"r "<<(int)reds[i][j]<<"g "<<(int)greens[i][j]<<"b "<<(int)blues[i][j]<<std::endl;
  106.             pixel++;
  107.         }
  108.     }
  109. }
  110.  
  111. void wypiszKolor8()
  112. {
  113.     int pixel=0;
  114.     for(int i = 0; i < rows; i++)
  115.     {
  116.         //fread(data, sizeof(unsigned char), row_padded, f);
  117.         for(int j = 0; j < cols; j ++)
  118.         {
  119.  
  120.             std::cout<<"pixel "<<pixel<<"szary "<<(int)grays[i][j]<<std::endl;
  121.             pixel++;
  122.         }
  123.     }
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. void ZamienSzarosc()
  131. {
  132.     for(int i = 0; i < rows; i++)
  133.     {
  134.         //fread(data, sizeof(unsigned char), row_padded, f);
  135.         for(int j = 0; j < cols; j ++)
  136.         {
  137.  
  138.             grays[i][j] = int(0.212671 * reds[i][j] +
  139.                               0.715160 * greens[i][j] +
  140.                               0.072169 * blues[i][j] + 0.5);
  141.             if (grays[i][j]<0)
  142.                 grays[i][j] = 0;
  143.             if (grays[i][j]>255)
  144.                 grays[i][j]= 255;
  145.         }
  146.     }
  147.  
  148. }
  149.  
  150.  
  151. void RGB_Allocate(unsigned char**& dude)
  152. {
  153.     dude = new unsigned char*[rows];
  154.     for (int i = 0; i < rows; i++)
  155.         dude[i] = new unsigned char[cols];
  156. }
  157.  
  158. bool FillAndAllocate(char*& buffer, const char* Picture, int& rows, int& cols, int& BufferSize,int& bitrate)   //1 bez bledow, 0 sa problemy
  159. {
  160.     ifstream file(Picture);
  161.  
  162.     if (file)
  163.     {
  164.         file.seekg(0, std::ios::end);
  165.         std::streampos length = file.tellg();
  166.         file.seekg(0, std::ios::beg);
  167.  
  168.         buffer = new char[length];
  169.         file.read(&buffer[0], length);
  170.  
  171.         PBITMAPFILEHEADER file_header;
  172.         PBITMAPINFOHEADER info_header;
  173.  
  174.         file_header = (PBITMAPFILEHEADER) (&buffer[0]);
  175.         info_header = (PBITMAPINFOHEADER) (&buffer[0] + sizeof(BITMAPFILEHEADER));
  176.         rows = info_header->biHeight;
  177.         cols = info_header->biWidth;
  178.         bitrate = info_header-> biBitCount;
  179.         BufferSize = file_header->bfSize;
  180.         return 1;
  181.     }
  182.     else
  183.     {
  184.         cout << "File" << Picture << " don't Exist!" << endl;
  185.         return 0;
  186.     }
  187. }
  188.  
  189. void GetPixlesFromBMP24(unsigned char** reds, unsigned char** greens, unsigned char** blues, int end, int rows, int cols, char* FileReadBuffer)
  190. {
  191.     int count = 1;
  192.     int extra = cols % 4;
  193.     for (int i = 0; i < rows; i++)
  194.     {
  195.         count += extra;
  196.         for (int j = cols - 1; j >= 0; j--)
  197.             for (int k = 0; k < 3; k++)
  198.             {
  199.                 switch (k)
  200.                 {
  201.                 case 0:
  202.                     reds[i][j] = FileReadBuffer[end - count++];
  203.                     break;
  204.                 case 1:
  205.                     greens[i][j] = FileReadBuffer[end - count++];
  206.                     break;
  207.                 case 2:
  208.                     blues[i][j] = FileReadBuffer[end - count++];
  209.                     break;
  210.                 }
  211.             }
  212.     }
  213. }
  214.  
  215. void GetPixlesFromBMP8(unsigned char** grays, int end, int rows, int cols, char* FileReadBuffer)   // end is BufferSize (total size of file)
  216. {
  217.     int count = 1;
  218.     ofstream kolor("grays.txt");
  219.     int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
  220.     for (int i = 0; i < rows; i++)
  221.     {
  222. //count += extra;
  223.         for (int j = cols - 1; j >= 0; j--)
  224.             //for (int k = 0; k < 3; k++)
  225.         {
  226.             grays[i][j] = FileReadBuffer[end - count++];
  227.             kolor<<(int)grays[i][j]<<endl;
  228.             //  cout<<" zawarotsc grays "<<grays[i][j]<<endl;
  229.             // cout<<" zawarotsc (int) grays "<<(int)grays[i][j]<<endl;
  230.         }
  231.     }
  232. }
  233.  
  234.  
  235. void WriteOutBmp24(char* FileBuffer, const char* NameOfFileToCreate, int BufferSize)
  236. {
  237.     std::ofstream write(NameOfFileToCreate);
  238.     if (!write)
  239.     {
  240.         cout << "Failed to write " << NameOfFileToCreate << endl;
  241.         return;
  242.     }
  243.     int count = 1;
  244.     int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
  245.     for (int i = 0; i < rows; i++)
  246.     {
  247.         count += extra;
  248.         for (int j = cols - 1; j >= 0; j--)
  249.             for (int k = 0; k < 3; k++)
  250.             {
  251.                 switch (k)
  252.                 {
  253.                 case 0: //reds
  254.                     FileBuffer[BufferSize - count] = reds[i][j];
  255.                     break;
  256.                 case 1: //green
  257.                     FileBuffer[BufferSize - count] = greens[i][j];
  258.                     break;
  259.                 case 2: //blue
  260.                     FileBuffer[BufferSize - count] = blues[i][j];
  261.                     break;
  262.                 }
  263.                 count++;
  264.             }
  265.     }
  266.     write.write(FileBuffer, BufferSize);
  267. }
  268.  
  269. void WriteOutBmp8(char* FileBuffer, const char* NameOfFileToCreate, int BufferSize)
  270. {
  271.     std::ofstream write(NameOfFileToCreate);
  272.     if (!write)
  273.     {
  274.         cout << "Failed to write " << NameOfFileToCreate << endl;
  275.         return;
  276.     }
  277.     int count = 1;
  278.     // int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
  279.     for (int i = 0; i < rows; i++)
  280.     {
  281.         // count +=extra;
  282.         for (int j = cols - 1; j >= 0; j--)
  283.         {
  284.             //  for (int k = 0; k < 3; k++) {
  285.  
  286.  
  287.             FileBuffer[BufferSize] = grays[i][j];
  288.  
  289.             // count++;
  290.         }
  291.  
  292.  
  293.  
  294.         //  }
  295.     }
  296.  
  297.     write.write(FileBuffer, BufferSize);
  298. }
  299.  
  300. int LZW(unsigned char** grays)
  301. {
  302.     ofstream wynik("wynik.txt");
  303. //   vector <string> slownik;
  304.     std::string kontener,lancuch;
  305.     char znak;
  306.  
  307.     inicjuj_bmp(slownik);
  308.  
  309.     for(int i = 0; i < rows; i++)
  310.     {
  311.         for(int j = 0; j < cols; j ++)
  312.         {
  313.             if(i&&j==0)
  314.                 kontener= grays[0][0];
  315.             else
  316.             {
  317.                 znak=grays[i][j];
  318.                 lancuch=kontener+znak;
  319.  
  320.  
  321.                 if (przeszukajSlownik(lancuch,slownik))
  322.                 {
  323.                     kontener=kontener+znak;
  324.                     cout<<"lancuch w slowniku "<<lancuch<<endl;
  325.                     cout<<"kontener lancucha w slowniku "<<kontener<<endl;
  326.                 }
  327.                 else
  328.                 {
  329.                     cout<<"lancuch  brak lancucha w slowniku "<<lancuch<<endl;
  330.                     cout<<"kontener brak lancucha w slowniku "<<kontener<<endl;
  331.                     slownik.push_back(lancuch);
  332.                     wynik<<wypiszIndexVektor(kontener,slownik)//<<" oraz znak "<<slownik[wypiszIndexVektor(kontener,slownik)]
  333.                          <<endl;
  334.                     cout<<"index kontenera w slowniku"<<wypiszIndexVektor(kontener,slownik)<<endl;    // w przysz³osci wynik zapisujemy do odobnego pliku wynikowego
  335.                     kontener=znak;
  336.                     cout<<"kontener nowy znak "<<kontener<<endl;
  337.                 }
  338.  
  339.                 cout<<"lancuch po ifie to "<<lancuch<<endl;
  340.                 cout<<"kontener po ifie to "<<kontener<<endl;
  341.                 cout<<"slownik jest rozmiaru : "<<slownik.size()<<endl;
  342.  
  343.                 cout<<endl;
  344. //wypiszVektor(slownik);
  345.             }
  346.         }
  347.     }
  348.     return 1;
  349. }
  350.  
  351. int rozpakuj ()
  352. {
  353.     vector <string> slownik;
  354.  
  355.     ifstream plik;
  356.     ofstream wynik("wynik2.bmp");
  357.     //ofstream wynik2("wynik2INT.txt");
  358.     plik.open("wynik.txt",ios_base::in);
  359.     char znak,koniec;
  360.     int index;
  361.     std::string lancuch,kontener,liczba;
  362.  
  363.     //tu koncza sie zmienne
  364.  
  365.     //  slownik.push_back("a");
  366.     //  slownik.push_back("b");
  367.     //  slownik.push_back("c");
  368.     //  slownik.push_back("d");
  369.     //  slownik.push_back("_");
  370.     inicjuj_bmp(slownik);
  371.  
  372.  
  373.  
  374.     if (plik==NULL)
  375.         perror ("Error opening file");
  376.     else
  377.     {
  378.         plik>>index;
  379.  
  380.         kontener=slownik[index];
  381.         wynik<<kontener<<endl;
  382. //    wynik2<<(int)slownik[index]<<endl;
  383.         while(plik>>index)
  384.         {
  385.  
  386.             cout<<"wartosc index w petli index==slownik "<<index<<endl;
  387.             cout<<"wartosc slownika w petli index==slownik "<<slownik[index]<<endl;
  388.  
  389.  
  390.             if(index==slownik.size())
  391.             {
  392.                 lancuch=kontener+kontener[0];
  393.                 slownik.push_back(lancuch);
  394.  
  395.             }
  396.             else
  397.             {kontener=slownik[index];}
  398.                 wynik<<kontener;
  399.                 lancuch=kontener+slownik[index][0];
  400.                 slownik.push_back(lancuch);
  401.  
  402.                 cout<<"kontener petla index==slownik "<<kontener<<endl;
  403.                 cout<<"lancuch petla index==slownik "<<lancuch<<endl;
  404.  
  405.  
  406. //    wynik2<<(int) kontener<<endl;
  407.  
  408.         };
  409.         cout<<"rozmiar wektora slownika"<<slownik.size()<<endl;
  410.  
  411.         for(int n=0; n<slownik.size(); n++)
  412.         {
  413.             std::cout << slownik[n] << '\n';
  414.         }
  415.  
  416.  
  417.         wynik.close();
  418.         plik.close();
  419.         cout << "Hello world!" << endl;
  420.     }
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427. }
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434. int main(int args, char** cat)
  435. {
  436.     char* FileBuffer;
  437.     int BufferSize;
  438.     vector <string> slownik;
  439.  
  440.  
  441. #define Picture "ReadInPicture3.bmp"
  442.     // #define Picture "OutputPicture.bmp"
  443.     if (!FillAndAllocate(FileBuffer, Picture, rows, cols, BufferSize,bitrate))
  444.     {
  445.         cout << "File read error" << endl;
  446.         return 0;
  447.     }
  448.     cout << "Rows: " << rows << " Cols: " << cols <<"bitrate "<<bitrate<<"bufersize "<<BufferSize<<"filebuffer"<<FileBuffer<<endl;
  449.  
  450.  
  451.     RGB_Allocate(reds);
  452.     RGB_Allocate(greens);
  453.     RGB_Allocate(blues);
  454.     RGB_Allocate(grays);
  455. //GetPixlesFromBMP24( reds,  greens, blues,BufferSize, rows, cols, FileBuffer);
  456.     GetPixlesFromBMP8(grays,BufferSize,rows,cols,FileBuffer);
  457.  
  458.     //wypiszKolor24();
  459. //wypiszKolor8(); //wypisuje inta z grays[i][j]
  460. //LZW(grays);
  461.     // (*PBITMAPINFOHEADER ).biBitCount=8;  // biBitCount=8;
  462.     //ZamienSzarosc();
  463. #define plik_zapisu "OutputPicture.bmp"
  464.     bitrate=24;
  465.     cout<<BufferSize;
  466.     WriteOutBmp8(FileBuffer,  plik_zapisu,BufferSize);
  467.  
  468.     rozpakuj();
  469.  
  470. //WriteOutBmp24(FileBuffer,  WriteOutFile,BufferSize);
  471.     return 1;
  472. }
  473.