Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // testBMP.cpp: определяет точку входа для консольного приложения.
- //
- #include <iostream>
- #include <fstream> //подключили библиотеку для работы с map
- #include "conio.h"
- //using namespace std;
- void write(char *img, char *msg);
- void read();
- void main()
- {
- read();
- write(/*KUKUSIKI*/, "Hello!");
- system("pause");
- }
- void write(char *img, char *msg) {
- std::ofstream os("temp.bmp", std::ios::binary);
- unsigned char signature[2] = { 'B', 'M' };
- unsigned int fileSize = 14 + 40 + 1000 * 1000 * 4;
- unsigned int reserved = 0;
- unsigned int offset = 14 + 40;
- unsigned int headerSize = 40;
- unsigned int dimensions[2] = { 1000, 1000 };
- unsigned short colorPlanes = 1;
- unsigned short bpp = 32;
- unsigned int compression = 0;
- unsigned int imgSize = 1000 * 1000 * 4;
- unsigned int resolution[2] = { 2795, 2795 };
- unsigned int pltColors = 0;
- unsigned int impColors = 0;
- os.write(reinterpret_cast<char*>(signature), sizeof(signature));
- os.write(reinterpret_cast<char*>(&fileSize), sizeof(fileSize));
- os.write(reinterpret_cast<char*>(&reserved), sizeof(reserved));
- os.write(reinterpret_cast<char*>(&offset), sizeof(offset));
- os.write(reinterpret_cast<char*>(&headerSize), sizeof(headerSize));
- os.write(reinterpret_cast<char*>(dimensions), sizeof(dimensions));
- os.write(reinterpret_cast<char*>(&colorPlanes), sizeof(colorPlanes));
- os.write(reinterpret_cast<char*>(&bpp), sizeof(bpp));
- os.write(reinterpret_cast<char*>(&compression), sizeof(compression));
- os.write(reinterpret_cast<char*>(&imgSize), sizeof(imgSize));
- os.write(reinterpret_cast<char*>(resolution), sizeof(resolution));
- os.write(reinterpret_cast<char*>(&pltColors), sizeof(pltColors));
- os.write(reinterpret_cast<char*>(&impColors), sizeof(impColors));
- unsigned char x, r, g, b;
- unsigned msg_size = strlen(msg);
- unsigned msg_pos = 0;
- for (int i = 0; i < dimensions[1] * dimensions[0] * 4; ++i)
- {
- b = img[i++];
- g = img[i++];
- r = img[i++];
- x = img[i++];
- /*x = 0;
- b = 0;
- g = 0;
- r = rand() % 256;
- g = rand() % 256;
- b = rand() % 256;
- if (i > 100 && i < 900)//квадратик
- if(j > 100 && j < 900)
- r = 255;*/
- if (msg_pos <= msg_size) {
- char ch = msg[msg_pos];
- char ar = (ch & 0b11100000) >> 5;
- char ag = (ch & 0b00011100) >> 2;
- char ab = (ch & 0b00000011);
- r ^= ar;
- g ^= ag;
- b ^= ab;
- msg_pos++;
- }
- os.write(reinterpret_cast<char*>(&b), sizeof(b));
- os.write(reinterpret_cast<char*>(&g), sizeof(g));
- os.write(reinterpret_cast<char*>(&r), sizeof(r));
- os.write(reinterpret_cast<char*>(&x), sizeof(x));
- }
- os.close();
- }
- void read()
- {
- std::ifstream os("temp.bmp", std::ios::binary);
- unsigned char signature[2] = { 'B', 'M' };
- unsigned int fileSize = 14 + 40 + 1000 * 1000 * 4;
- unsigned int reserved = 0;
- unsigned int offset = 14 + 40;
- unsigned int headerSize = 40;
- unsigned int dimensions[2] = { 1000, 1000 };
- unsigned short colorPlanes = 1;
- unsigned short bpp = 32;
- unsigned int compression = 0;
- unsigned int imgSize = 1000 * 1000 * 4;
- unsigned int resolution[2] = { 2795, 2795 };
- unsigned int pltColors = 0;
- unsigned int impColors = 0;
- os.read(reinterpret_cast<char*>(signature), sizeof(signature)); cout << "signature=" << signature << endl;
- os.read(reinterpret_cast<char*>(&fileSize), sizeof(fileSize)); cout << "fileSize=" << fileSize << endl;
- os.read(reinterpret_cast<char*>(&reserved), sizeof(reserved)); cout << "reserved=" << reserved << endl;
- os.read(reinterpret_cast<char*>(&offset), sizeof(offset));
- os.read(reinterpret_cast<char*>(&headerSize), sizeof(headerSize)); cout << "headerSize=" << headerSize << endl;
- os.read(reinterpret_cast<char*>(dimensions), sizeof(dimensions)); cout << "dimensions=" << dimensions[0] << " " << dimensions[1] << endl;
- os.read(reinterpret_cast<char*>(&colorPlanes), sizeof(colorPlanes)); cout << "colorPlanes=" << colorPlanes << endl;
- os.read(reinterpret_cast<char*>(&bpp), sizeof(bpp)); cout << "bpp=" << bpp << endl;
- os.read(reinterpret_cast<char*>(&compression), sizeof(compression)); cout << "compression=" << compression << endl;
- os.read(reinterpret_cast<char*>(&imgSize), sizeof(imgSize)); cout << "imgSize=" << imgSize << endl;
- os.read(reinterpret_cast<char*>(resolution), sizeof(resolution)); cout << "resolution=" << resolution[0] << " " << resolution[1] << endl;
- os.read(reinterpret_cast<char*>(&pltColors), sizeof(pltColors)); cout << "pltColors=" << pltColors << endl;
- os.read(reinterpret_cast<char*>(&impColors), sizeof(impColors)); cout << "impColors=" << impColors << endl;
- char *img;
- unsigned char x, r, g, b;
- for (int i = 0; i < dimensions[1]; ++i)
- {
- for (int j = 0; j < dimensions[0]; ++j)
- {
- os >> b;
- os >> g;
- os >> r;
- os >> x;
- img[i*dimensions[1] + j] = b;
- img[i*dimensions[1] + j + 1] = g;
- img[i*dimensions[1] + j + 2] = r;
- img[i*dimensions[1] + j+ 3 ] = x;
- }
- }
- //getch();
- }
- void Change() {
- }
Add Comment
Please, Sign In to add comment