65,186
社区成员




/*****BMP.h*****/
#include <fstream>
#include <Windows.h>
#define N 384
using namespace std;
typedef struct {
WORD bfType; /* Magic identifier */
DWORD bfSize; /* File size in bytes */
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits; /* Offset to image data, bytes */
} HEADER;
typedef struct {
DWORD biSize; /* Header size in bytes */
LONG biWidth; /* Width and height of image */
LONG biHeight;
WORD biPlanes; /* Number of colour planes */
WORD biBitCount; /* Bits per pixel */
DWORD biCompression; /* Compression type */
DWORD biSizeImage; /* Image size in bytes */
LONG biXPelsPerMeter; /* Pixels per meter */
LONG biYPelsPerMeter;
DWORD biClrUsed; /* Number of colours */
DWORD biClrImportant; /* Important colours */
} INFOHEADER;
#include "BMP.h"
HEADER bmfh; //bitmap file header
INFOHEADER bmih; //bitmap info header
BYTE R[N][N], G[N][N], B[N][N]; //RGB value of every pixel
COLORREF color[N][N]; //color
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow (); //initialize the screen
void ReadBmp(char *BmpFileName);
void GetColor(int i, int j);
void main()
{
HWND hwnd; //handlers HWND and HDC
HDC hdc;
char BmpFileName[10];
int i, j;
hwnd = GetConsoleWindow(); //initialize the screen
hdc = GetDC(hwnd);
scanf("%s", BmpFileName);
ReadBmp(BmpFileName); //read the BMP file
for(i = 0; i < bmih.biHeight; i++)
for(j = 0; j < bmih.biWidth; j++)
SetPixel(hdc, i, j, color[bmih.biWidth - j][i]); //draw the BMP image
ReleaseDC(hwnd,hdc);
}
void ReadBmp(char *BmpFileName) //read the BMP file
{
int patch; //number of 0s for complement in every row
ifstream in(BmpFileName, ios_base::binary); //open the BMP file
in.read((char *)(&bmfh), sizeof(bmfh) - 2); //read in BITMAPFILEHEADER. Here sizeof returns a value larger than struct size, so "-2"
if(bmfh.bfType != 0x4d42) //if not BMP, exit
{
printf("File type is not BMP!\n");
exit(1);
}
in.read((char *)(&bmih),sizeof(bmih)); //read in BITMAPINFOHEADER
in.seekg(bmfh.bfOffBits, ios_base::beg); //seek bitmap data
patch = (4 - (bmih.biWidth * 3) % 4) % 4; //calculate number of 0s for complement in every row
for(int i = 0; i < abs(bmih.biHeight); i++)
{
for(int j = 0; j < abs(bmih.biWidth); j++)
{
in.read((char *)(&R[i][j]), 1); //read in data pixel by pixel
in.read((char *)(&G[i][j]), 1);
in.read((char *)(&B[i][j]), 1);
GetColor(i, j); //obtain the original and greyscaled color
}
in.seekg(patch, ios_base::cur); //skip 0s for complement in every row
}
}
void GetColor(int i, int j) //obtain the original and greyscaled color
{
int iB, iG, iR;
iB = (int)(B[i][j]);
iG = (int)(G[i][j]);
iR = (int)(R[i][j]);
color[i][j] = RGB(iB, iG, iR); //original color
}
#ifndef BMP_H
#define BMP_H
#include "qstring.h"
#include <qmessagebox.h>
#include <qimage.h>
#include "qpainter.h"
#include <fstream>
#include <Windows.h>
#define N 384
#define n 8
using namespace std;
#pragma pack(push)
#pragma pack(1)
typedef struct{ //self-defined BITMAPFILEHEADER
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} HEADER;
typedef struct{ //self-defined BITMAPINFOHEADER
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} INFOHEADER;
#pragma pack(pop)
class BMP
{
public:
BMP();
~BMP();
void ReadBmp(QString fileName);
//QImage ShowImage1(QString fileName);
//QImage ShowImage2(QString fileName);
//QImage ShowImage3(QString fileName);
QImage ShowImage(QString fileName, int i);
private:
QString fileName;
HEADER bmfh;
INFOHEADER bmih;
BYTE R[N][N], G[N][N], B[N][N];
COLORREF color[N][N], grey[N][N], dithered[N][N];
int iGrey[N][N];
void GetColor(int i, int j);
int Round(double x);
void OrderedDither();
};
#endif // BMP_H
#pragma pack(push)
#pragma pack(1)
#ifndef BMP_H
#define BMP_H
#include "qstring.h"
#include <qmessagebox.h>
#include <qimage.h>
#include "qpainter.h"
#include <fstream>
#include <Windows.h>
#define N 384
#define n 8
using namespace std;
typedef struct{ //self-defined BITMAPFILEHEADER
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} HEADER;
typedef struct{ //self-defined BITMAPINFOHEADER
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} INFOHEADER;
class BMP
{
public:
BMP();
~BMP();
void ReadBmp(QString fileName);
//QImage ShowImage1(QString fileName);
//QImage ShowImage2(QString fileName);
//QImage ShowImage3(QString fileName);
QImage ShowImage(QString fileName, int i);
private:
QString fileName;
HEADER bmfh;
INFOHEADER bmih;
BYTE R[N][N], G[N][N], B[N][N];
COLORREF color[N][N], grey[N][N], dithered[N][N];
int iGrey[N][N];
void GetColor(int i, int j);
int Round(double x);
void OrderedDither();
};
#pragma pack(pop)
#endif // BMP_H
in.read((char *)(&bmfh), sizeof(bmfh) - 2); //read in BITMAPFILEHEADER. Here sizeof returns a value larger than struct size, so "-2"
#pragma pack(push)
#pragma pack(1)
#pragma pack(pop)