Advertisement
Necto

someone help me

Sep 19th, 2021
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cmath>
  5. using namespace std;
  6. unsigned char* ReadBMP()
  7. {
  8.     FILE* f = fopen("testbnp.bmp", "rb");
  9.     if (f == NULL)
  10.         throw "Argument Exception";
  11.     unsigned char info[54];
  12.     fread(info, sizeof(unsigned char), 54, f);
  13.     int width = *(int*)&info[18];
  14.     int height = *(int*)&info[22];
  15.     //cout << "Width: " << width << endl;
  16.     //cout << "Height: " << height << endl;
  17.     int w = width, h = height;
  18.     int row_padded = (width * 3 + 3) & (~3);
  19.     unsigned char** datapi = new unsigned char* [h];
  20.     for (int i = 0; i < height; ++i) {
  21.         datapi[i] = new unsigned char[row_padded];
  22.     }
  23.     unsigned char** textpi = new unsigned char* [h];
  24.     for (int i = 0; i < height; ++i) {
  25.         textpi[i] = new unsigned char[row_padded/3];
  26.     }
  27.     FILE* a;
  28.     a = fopen("atest.txt", "wb");
  29.     string asci = "##@%=+/*:-.,`";
  30.     int len = asci.length();
  31.     unsigned char* text = new unsigned char[row_padded];
  32.     //cout << endl << row_padded;
  33.     unsigned char* data = new unsigned char[row_padded];
  34.     for (int i = 0; i < height; ++i) {
  35.         fread(data, sizeof(unsigned char), row_padded, f);
  36.         for (int j = 0; j < row_padded; j++) {
  37.             datapi[i][j] = data[j];
  38.         }
  39.         for (int k = 0; k < row_padded; k += 3) {
  40.             int temp = (int)datapi[i][k] * 0.114 + (int)datapi[i][k + 1] * 0.587 + (int)datapi[i][k + 2] * 0.229;
  41.             datapi[i][k] = (unsigned char)temp;
  42.             datapi[i][k + 1] = (unsigned char)temp;
  43.             datapi[i][k + 2] = (unsigned char)temp;
  44.             text[k] = asci[ceil(((len - 1) * temp) / 255)];
  45.         }
  46.         int b = 0;
  47.         for (int j = 0; j < row_padded; j+=3, b++) {
  48.             textpi[h - i - 1][b] = text[j];
  49.         }
  50.         //0.299R + 0.587G + 0.114B
  51.     }
  52.  
  53.  
  54.     for (int i = 0; i < h; ++i) {
  55.         fwrite(textpi[i], 1, row_padded/3, a);
  56.         fputc('\n', a);
  57.     }
  58.     unsigned char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
  59.     unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 };
  60.     int filesize = 3 * w * h + 54;
  61.     bmpfileheader[2] = (unsigned char)(filesize);
  62.     bmpfileheader[3] = (unsigned char)(filesize >> 8);
  63.     bmpfileheader[4] = (unsigned char)(filesize >> 16);
  64.     bmpfileheader[5] = (unsigned char)(filesize >> 24);
  65.     int temp1 = *(int*)&info[10];
  66.     bmpfileheader[10] = (unsigned char)(temp1);
  67.     bmpfileheader[11] = (unsigned char)(temp1 >> 8);
  68.     bmpfileheader[12] = (unsigned char)(temp1 >> 16);
  69.     bmpfileheader[13] = (unsigned char)(temp1 >> 24);
  70.     bmpinfoheader[4] = (unsigned char)(w);
  71.     bmpinfoheader[5] = (unsigned char)(w >> 8);
  72.     bmpinfoheader[6] = (unsigned char)(w >> 16);
  73.     bmpinfoheader[7] = (unsigned char)(w >> 24);
  74.     bmpinfoheader[8] = (unsigned char)(h);
  75.     bmpinfoheader[9] = (unsigned char)(h >> 8);
  76.     bmpinfoheader[10] = (unsigned char)(h >> 16);
  77.     bmpinfoheader[11] = (unsigned char)(h >> 24);
  78.  
  79.     FILE* l;
  80.     l = fopen("img.bmp", "wb");
  81.     fwrite(bmpfileheader, 1, 14, l);
  82.     fwrite(bmpinfoheader, 1, 40, l);
  83.     for (int i = 0; i < h; i++)
  84.     {
  85.         fwrite(datapi[i], 1, row_padded, l);
  86.     }
  87.     fclose(l);
  88.     for (int i = 0; i < height; i++) {
  89.         delete[] datapi[i];
  90.     }
  91.     delete[] datapi;
  92.     delete[] data;
  93.     delete[] text;
  94.     fclose(f);
  95.     fclose(a);
  96.     return info;
  97. }
  98. int main(int argc, char* argv[])
  99. {
  100.     unsigned char* data = ReadBMP();
  101.     cout << "done";
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement