Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MAXCOLORTC
- #include "wingraph.h"
- #include <iostream>
- using namespace std;
- struct FileHeader {
- WORD bfType;
- DWORD bfSize;
- WORD bfReserved1;
- WORD bfReserved2;
- DWORD bfOffbits;
- } file_header;
- 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;
- struct RGBquad {
- byte rgbRed;
- byte rgbGreen;
- byte rgbBlue;
- byte rgbReserved;
- };
- int main() {
- FILE *f1 = fopen("image_tc.bmp", "rb");
- fread(&file_header, sizeof(file_header), 1, f1);
- 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);
- 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;
- resize(width, 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 padding = 0;
- if(width % 4)
- padding = 4 - (width % 4);
- int n, line = 0;
- do {
- n = fread(buffer, 1, width, f1);
- for(int i = 0, k = 0; i < n; i++, k += 2) {
- byte pixel1 = buffer[i] & 0xf;
- byte pixel2 = buffer[i] & 0x0f;
- putpixel(k, height - line, RGB((int)BmiColors[pixel1].rgbBlue,
- (int)BmiColors[pixel1].rgbGreen, (int)BmiColors[pixel1].rgbRed));
- putpixel(k + 1, height - line, (int)RGB(BmiColors[pixel2].rgbBlue,
- (int)BmiColors[pixel2].rgbGreen, (int)BmiColors[pixel2].rgbRed));
- }
- if(padding)
- fseek(f1, padding, SEEK_CUR);
- line++;
- }while(n == width);
- delete [] buffer;
- }else if(map_info.BitCount == 8) {
- 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++)
- putpixel(i, height - line, RGB(BmiColors[buffer[i]].rgbBlue,
- BmiColors[buffer[i]].rgbGreen, BmiColors[buffer[i]].rgbRed));
- line++;
- }while(n == width);
- delete [] buffer;
- }else if(map_info.BitCount == 24) {
- width *= 3;
- int padding = 0;
- RGBquad rgb;
- if(width % 4)
- padding = 4 - (width % 4);
- byte *buffer = new byte[width];
- int n, line = 0;
- do {
- n = fread(buffer, 1, width, f1);
- for(int i = 0, col = 0; i < n; i += 3, col++) {
- putpixel(col, height - line, RGB(buffer[i + 2],
- buffer[i + 1], buffer[i]));
- }
- line++;
- if(padding != 0)
- fread(&rgb, padding, 1, f1);
- }while(n == width);
- delete [] buffer;
- }else
- cout << "Unsupported image color type" << endl;
- fclose(f1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement