Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- unsigned char* ReadBMP()
- {
- FILE* f = fopen("testbnp.bmp", "rb");
- if (f == NULL)
- throw "Argument Exception";
- unsigned char info[54];
- fread(info, sizeof(unsigned char), 54, f);
- int width = *(int*)&info[18];
- int height = *(int*)&info[22];
- //cout << "Width: " << width << endl;
- //cout << "Height: " << height << endl;
- int w = width, h = height;
- int row_padded = (width * 3 + 3) & (~3);
- unsigned char** datapi = new unsigned char* [h];
- for (int i = 0; i < height; ++i) {
- datapi[i] = new unsigned char[row_padded];
- }
- unsigned char** textpi = new unsigned char* [h];
- for (int i = 0; i < height; ++i) {
- textpi[i] = new unsigned char[row_padded/3];
- }
- FILE* a;
- a = fopen("atest.txt", "wb");
- string asci = "##@%=+/*:-.,`";
- int len = asci.length();
- unsigned char* text = new unsigned char[row_padded];
- //cout << endl << row_padded;
- unsigned char* data = new unsigned char[row_padded];
- for (int i = 0; i < height; ++i) {
- fread(data, sizeof(unsigned char), row_padded, f);
- for (int j = 0; j < row_padded; j++) {
- datapi[i][j] = data[j];
- }
- for (int k = 0; k < row_padded; k += 3) {
- int temp = (int)datapi[i][k] * 0.114 + (int)datapi[i][k + 1] * 0.587 + (int)datapi[i][k + 2] * 0.229;
- datapi[i][k] = (unsigned char)temp;
- datapi[i][k + 1] = (unsigned char)temp;
- datapi[i][k + 2] = (unsigned char)temp;
- text[k] = asci[ceil(((len - 1) * temp) / 255)];
- }
- int b = 0;
- for (int j = 0; j < row_padded; j+=3, b++) {
- textpi[h - i - 1][b] = text[j];
- }
- //0.299R + 0.587G + 0.114B
- }
- for (int i = 0; i < h; ++i) {
- fwrite(textpi[i], 1, row_padded/3, a);
- fputc('\n', a);
- }
- unsigned char bmpfileheader[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
- unsigned char bmpinfoheader[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 };
- int filesize = 3 * w * h + 54;
- bmpfileheader[2] = (unsigned char)(filesize);
- bmpfileheader[3] = (unsigned char)(filesize >> 8);
- bmpfileheader[4] = (unsigned char)(filesize >> 16);
- bmpfileheader[5] = (unsigned char)(filesize >> 24);
- int temp1 = *(int*)&info[10];
- bmpfileheader[10] = (unsigned char)(temp1);
- bmpfileheader[11] = (unsigned char)(temp1 >> 8);
- bmpfileheader[12] = (unsigned char)(temp1 >> 16);
- bmpfileheader[13] = (unsigned char)(temp1 >> 24);
- bmpinfoheader[4] = (unsigned char)(w);
- bmpinfoheader[5] = (unsigned char)(w >> 8);
- bmpinfoheader[6] = (unsigned char)(w >> 16);
- bmpinfoheader[7] = (unsigned char)(w >> 24);
- bmpinfoheader[8] = (unsigned char)(h);
- bmpinfoheader[9] = (unsigned char)(h >> 8);
- bmpinfoheader[10] = (unsigned char)(h >> 16);
- bmpinfoheader[11] = (unsigned char)(h >> 24);
- FILE* l;
- l = fopen("img.bmp", "wb");
- fwrite(bmpfileheader, 1, 14, l);
- fwrite(bmpinfoheader, 1, 40, l);
- for (int i = 0; i < h; i++)
- {
- fwrite(datapi[i], 1, row_padded, l);
- }
- fclose(l);
- for (int i = 0; i < height; i++) {
- delete[] datapi[i];
- }
- delete[] datapi;
- delete[] data;
- delete[] text;
- fclose(f);
- fclose(a);
- return info;
- }
- int main(int argc, char* argv[])
- {
- unsigned char* data = ReadBMP();
- cout << "done";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement