Facebook
From Idiotic Cheetah, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 112
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <fstream>
  4.  
  5. using std::cout;
  6. using std::endl;
  7. using std::ofstream;
  8. using std::ifstream;
  9.  
  10. #pragma pack(1)
  11. #pragma once
  12.  
  13. typedef int LONG;
  14. typedef unsigned short WORD;
  15. typedef unsigned int DWORD;
  16.  
  17. typedef struct tagBITMAPFILEHEADER {
  18.     WORD bfType;
  19.     DWORD bfSize;
  20.     WORD bfReserved1;
  21.     WORD bfReserved2;
  22.     DWORD bfOffBits;
  23. } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
  24.  
  25. typedef struct tagBITMAPINFOHEADER {
  26.     DWORD biSize;
  27.     LONG biWidth;
  28.     LONG biHeight;
  29.     WORD biPlanes;
  30.     WORD biBitCount;
  31.     DWORD biCompression;
  32.     DWORD biSizeImage;
  33.     LONG biXPelsPerMeter;
  34.     LONG biYPelsPerMeter;
  35.     DWORD biClrUsed;
  36.     DWORD biClrImportant;
  37. } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
  38.  
  39. unsigned char** reds;
  40. unsigned char** greens;
  41. unsigned char** blues;
  42. unsigned char** grays;
  43. int rows;
  44. int cols;
  45. int bitrate;
  46.  
  47.  
  48. void wypiszKolor(){
  49. int pixel=0;
  50. for(int i = 0; i < rows; i++)
  51.     {
  52.        //fread(data, sizeof(unsigned char), row_padded, f);
  53.         for(int j = 0; j < cols; j ++){
  54.  
  55.             cout<<"pixel "<<pixel<<"r "<<(int)reds[i][j]<<"g "<<(int)greens[i][j]<<"b "<<(int)blues[i][j]<<endl;
  56. pixel++;
  57.         }
  58.     }
  59. }
  60.  
  61. void ZamienSzarosc()
  62. {
  63. for(int i = 0; i < rows; i++)
  64.     {
  65.        //fread(data, sizeof(unsigned char), row_padded, f);
  66.         for(int j = 0; j < cols; j ++){
  67.  
  68.             grays[i][j] = int(0.212671 * reds[i][j] +
  69.                             0.715160 * greens[i][j] +
  70.                              0.072169 * blues[i][j] + 0.5);
  71. if (grays[i][j]<0) grays[i][j] = 0;
  72. if (grays[i][j]>255) grays[i][j]= 255;
  73.         }
  74.     }
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. void ColorTest() {
  84.     // Makes Red Rectangle in top left corner. Rectangle stretches to right alot
  85.     for (int i = rows / 10; i < 3 * rows / 10; i++)
  86.         for (int j = cols / 10; j < 7 * cols / 10; j++)
  87.             reds[i][j] = 0xff;
  88.  
  89. // Makes small green box in bottom right
  90.     for (int i = 8 * rows / 10; i < rows; i++)
  91.         for (int j = 8 * cols / 10; j < cols; j++)
  92.             greens[i][j] = 0xff;
  93.  
  94. // Makes White box in the middle of the screeene
  95.     for (int i = rows * 4 / 10; i < rows * 6 / 10; i++)
  96.         for (int j = cols * 4 / 10; j < cols * 6 / 10; j++) {
  97.             greens[i][j] = 0xff;
  98.             reds[i][j] = 0xff;
  99.             blues[i][j] = 0xff;
  100.         }
  101.  
  102. // Blue verticle rectangle bottom left
  103.     for (int i = rows * 6 / 10; i < rows; i++)
  104.         for (int j = cols * 0; j < cols * 1 / 10; j++)
  105.             blues[i][j] = 0xff;
  106. }
  107.  
  108. void RGB_Allocate(unsigned char**& dude) {
  109.     dude = new unsigned char*[rows];
  110.     for (int i = 0; i < rows; i++)
  111.         dude[i] = new unsigned char[cols];
  112. }
  113.  
  114. bool FillAndAllocate(char*& buffer, const char* Picture, int& rows, int& cols, int& BufferSize,int& bitrate) { //Returns 1 if executed sucessfully, 0 if not sucessfull
  115.     std::ifstream file(Picture);
  116.  
  117.     if (file) {
  118.         file.seekg(0, std::ios::end);
  119.         std::streampos length = file.tellg();
  120.         file.seekg(0, std::ios::beg);
  121.  
  122.         buffer = new char[length];
  123.         file.read(&buffer[0], length);
  124.  
  125.         PBITMAPFILEHEADER file_header;
  126.         PBITMAPINFOHEADER info_header;
  127.  
  128.         file_header = (PBITMAPFILEHEADER) (&buffer[0]);
  129.         info_header = (PBITMAPINFOHEADER) (&buffer[0] + sizeof(BITMAPFILEHEADER));
  130.         rows = info_header->biHeight;
  131.         cols = info_header->biWidth;
  132.         bitrate = info_header-> biBitCount;
  133.         BufferSize = file_header->bfSize;
  134.         return 1;
  135.     }
  136.     else {
  137.         cout << "File" << Picture << " don't Exist!" << endl;
  138.         return 0;
  139.     }
  140. }
  141.  
  142. void GetPixlesFromBMP24(unsigned char** reds, unsigned char** greens, unsigned char** blues, int end, int rows, int cols, char* FileReadBuffer) { // end is BufferSize (total size of file)
  143.     int count = 1;
  144. int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
  145.     for (int i = 0; i < rows; i++){
  146. count += extra;
  147.     for (int j = cols - 1; j >= 0; j--)
  148.         for (int k = 0; k < 3; k++) {
  149.                 switch (k) {
  150.                 case 0:
  151.                     reds[i][j] = FileReadBuffer[end - count++];
  152.                     break;
  153.                 case 1:
  154.                     greens[i][j] = FileReadBuffer[end - count++];
  155.                     break;
  156.                 case 2:
  157.                     blues[i][j] = FileReadBuffer[end - count++];
  158.                     break;
  159.                 }
  160.             }
  161.             }
  162. }
  163.  
  164. void WriteOutBmp24(char* FileBuffer, const char* NameOfFileToCreate, int BufferSize) {
  165.     std::ofstream write(NameOfFileToCreate);
  166.     if (!write) {
  167.         cout << "Failed to write " << NameOfFileToCreate << endl;
  168.         return;
  169.     }
  170.     int count = 1;
  171.     int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
  172.     for (int i = 0; i < rows; i++){
  173.         count += extra;
  174.         for (int j = cols - 1; j >= 0; j--)
  175.             for (int k = 0; k < 3; k++) {
  176.                 switch (k) {
  177.                 case 0: //reds
  178.                     FileBuffer[BufferSize - count] = reds[i][j];
  179.                     break;
  180.                 case 1: //green
  181.                     FileBuffer[BufferSize - count] = greens[i][j];
  182.                     break;
  183.                 case 2: //blue
  184.                     FileBuffer[BufferSize - count] = blues[i][j];
  185.                     break;
  186.                 }
  187.                 count++;
  188.             }
  189.             }
  190.     write.write(FileBuffer, BufferSize);
  191. }
  192.  
  193. void WriteOutBmp8(char* FileBuffer, const char* NameOfFileToCreate, int BufferSize) {
  194.     std::ofstream write(NameOfFileToCreate);
  195.     if (!write) {
  196.         cout << "Failed to write " << NameOfFileToCreate << endl;
  197.         return;
  198.     }
  199.     int count = 1;
  200.     int extra = cols % 4; // The nubmer of bytes in a row (cols) will be a multiple of 4.
  201.     for (int i = 0; i < rows; i++){
  202.         count +=extra;
  203.         for (int j = cols - 1; j >= 0; j--)
  204.             {
  205.                 for (int k = 0; k < 3; k++) {
  206.  
  207.  
  208.                     FileBuffer[BufferSize - count  ] = grays[i][j];
  209.                 count++;
  210.                 }
  211.  
  212.  
  213.  
  214.             }
  215.             }
  216.  
  217.     write.write(FileBuffer, BufferSize);
  218. }
  219.  
  220.  
  221.  
  222.  
  223. int main(int args, char** cat) {
  224. char* FileBuffer; int BufferSize;
  225. PBITMAPINFOHEADER nowy;
  226.  
  227. #define Picture "obraz.bmp"
  228. if (!FillAndAllocate(FileBuffer, Picture, rows, cols, BufferSize,bitrate)){cout << "File read error" << endl; return 0;}
  229. cout << "Rows: " << rows << " Cols: " << cols <<"bitrate "<<bitrate<<endl;
  230.  
  231. RGB_Allocate(reds);
  232. RGB_Allocate(greens);
  233. RGB_Allocate(blues);
  234. RGB_Allocate(grays);
  235. GetPixlesFromBMP24( reds,  greens, blues,BufferSize, rows, cols, FileBuffer);
  236. //ColorTest();
  237. wypiszKolor();
  238. //nowy-> biBitCount=8;
  239. //ZamienSzarosc();
  240. #define WriteOutFile "wyjscie.bmp"
  241. //WriteOutBmp8(FileBuffer,  WriteOutFile,BufferSize);
  242. //WriteOutBmp24(FileBuffer,  WriteOutFile,BufferSize);
  243.     return 1;
  244. }
  245.