Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <graphics.h>
- using namespace std;
- struct FileHeader {
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffbits;
- } file_header, file_header_logo;
- struct MAPINFO {
- DWORD Size;
- DWORD Width;
- DWORD Height;
- WORD Planes;
- WORD BitCount;
- DWORD Compression;
- DWORD SizeImage;
- long XPelsPerMeter;
- long YPelsPerMeter;
- DWORD ClrUsed;
- DWORD ClrImportant;
- } map_info, map_info_logo;
- struct RGBquad {
- byte rgbRed;
- byte rgbGreen;
- byte rgbBlue;
- byte rgbReserved;
- };
- int main() {
- FILE *f1 = fopen("image_tc.bmp", "rb");
- FILE *f2 = fopen("image_small_tc.bmp", "rb");
- FILE *f3 = fopen("output.bmp", "wb");
- fread(&file_header, sizeof(file_header), 1, f1);
- fwrite(&file_header, sizeof(file_header), 1, f3);
- fread(&file_header_logo, sizeof(file_header_logo), 1, f2);
- cout << "-----FILE HEADER" << endl;
- cout << "bfType - " << file_header.bfType << endl;
- cout << "bfSize - " << file_header.bfSize << endl;
- cout << "bfReserved1 - " << file_header.bfReserved1 << endl;
- cout << "bfReserved2 - " << file_header.bfReserved2 << endl;
- cout << "bfOffBits - " << file_header.bfOffbits << endl;
- fread(&map_info, sizeof(map_info), 1, f1);
- fwrite(&map_info, sizeof(map_info), 1, f3);
- fread(&map_info_logo, sizeof(map_info_logo), 1, f2);
- cout << "-----MAP INFO" << endl;
- cout << "Size - " << map_info.Size << endl;
- cout << "Width - " << map_info.Width << endl;
- cout << "Height - " << map_info.Height << endl;
- cout << "Planes - " << map_info.Planes << endl;
- cout << "BitCount - " << map_info.BitCount << endl;
- cout << "Compression - " << map_info.Compression << endl;
- cout << "SizeImage - " << map_info.SizeImage << endl;
- cout << "XPelsPerMeter - " << map_info.XPelsPerMeter << endl;
- cout << "YPelsPerMeter - " << map_info.YPelsPerMeter << endl;
- cout << "ClrUsed - " << map_info.ClrUsed << endl;
- cout << "ClrImportant - " << map_info.ClrImportant << endl;
- int width = map_info.Width;
- int height = map_info.Height;
- if(map_info.BitCount == 4) {
- // int N = map_info.ClrUsed;
- // RGBquad BmiColors[N];
- //
- // fread(BmiColors, sizeof(RGBquad), N, f1);
- //
- // byte *buffer = new byte[width];
- // int n, line = 0;
- // do {
- // n = fread(buffer, 1, width, f1);
- // for(int i = 0; i < n; i += 2) {
- // byte pixel1 = buffer[i] & 0xf;
- // byte pixel2 = buffer[i] & 0x0f;
- //
- // putpixel(i, height - line, RGB((int)BmiColors[pixel1].rgbBlue,
- // (int)BmiColors[pixel1].rgbGreen, (int)BmiColors[pixel1].rgbRed));
- //
- // putpixel(i + 1, height - line, (int)RGB(BmiColors[pixel2].rgbBlue,
- // (int)BmiColors[pixel2].rgbGreen, (int)BmiColors[pixel2].rgbRed));
- // }
- // line++;
- // }while(n == width);
- //
- // delete [] buffer;
- }else if(map_info.BitCount == 8) {
- int N = map_info.ClrUsed;
- RGBquad BmiColors[N], BmiColorsLogo[N];
- fread(BmiColors, sizeof(RGBquad), N, f1);
- fread(BmiColorsLogo, sizeof(RGBquad), N, f2);
- byte **bufferInp = new byte*[height];
- for(int i = 0; i < height; i++) {
- bufferInp[i] = new byte[width];
- fread(bufferInp[i], sizeof(byte), width, f1);
- }
- byte **bufferOut = new byte*[map_info_logo.Height];
- for(int i = 0; i < map_info_logo.Height; i++) {
- bufferOut[i] = new byte[map_info_logo.Width];
- fread(bufferOut[i], sizeof(byte), map_info_logo.Width, f2);
- }
- for(int i = 0; i < height; i++) {
- for(int j = 0; j < width; j++) {
- if(i < map_info_logo.Height){
- if(j < map_info_logo.Width) {
- bufferInp[i][j] = bufferOut[i][j];
- BmiColors[bufferInp[i][j]].rgbRed = BmiColorsLogo[bufferOut[i][j]].rgbRed;
- BmiColors[bufferInp[i][j]].rgbGreen = BmiColorsLogo[bufferOut[i][j]].rgbGreen;
- BmiColors[bufferInp[i][j]].rgbBlue = BmiColorsLogo[bufferOut[i][j]].rgbBlue;
- }
- }
- }
- }
- fwrite(BmiColors, sizeof(RGBquad), N, f3);
- for(int i = 0; i < height; i++) {
- fwrite(bufferInp[i], sizeof(byte), width, f3);
- }
- for(int i = 0; i < height; i++)
- delete[] bufferInp[i];
- delete [] bufferInp;
- for(int i = 0; i < map_info_logo.Height; i++)
- delete [] bufferOut[i];
- delete[] bufferOut;
- }else if(map_info.BitCount == 24) {
- width *= 3;
- int padding = 0;
- RGBquad rgb, rgbLogo;
- if(width % 4)
- padding = 4 - (width % 4);
- int paddingLogo = 0;
- if((map_info_logo.Width * 3) % 4)
- paddingLogo = 4 - ((map_info_logo.Width * 3) % 4);
- bool firstVal = false;
- byte *buffer = new byte[width];
- byte *bufferOut = new byte[map_info_logo.Width * 3];
- int n, line = 0, nl, val;
- do {
- n = fread(buffer, 1, width, f1);
- if(line < map_info_logo.Height) {
- nl = fread(bufferOut, 1, map_info_logo.Width * 3, f2);
- for(int i = 0, col = 0; i < n; i += 3, col++) {
- if(!firstVal) {
- val = (bufferOut[i] + bufferOut[i + 1] + bufferOut[i + 2]);
- firstVal = true;
- }
- if(i < map_info_logo.Width * 3) {
- if((bufferOut[i] + bufferOut[i + 1] + bufferOut[i + 2]) != val) {
- buffer[i] = bufferOut[i];
- buffer[i + 1] = bufferOut[i + 1];
- buffer[i + 2] = bufferOut[i + 2];
- }
- }
- }
- if(paddingLogo)
- fread(&rgbLogo, paddingLogo, 1, f2);
- }
- line++;
- fwrite(buffer, sizeof(byte), n, f3);
- if(padding != 0)
- fread(&rgb, padding, 1, f1);
- }while(n == width);
- delete [] buffer;
- delete [] bufferOut;
- }else
- cout << "Unsupported image color type" << endl;
- fclose(f1);
- fclose(f2);
- fclose(f3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement