1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include "load.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstdio>
const int W = 256;
const int H = 256;
bool LoadBMP(const char* sBMPFile, unsigned char* buffer)
{
int i,j;
std::ifstream file(sBMPFile, ios::in | ios::binary);
if ( file ) {
if (file.seekg(0x36)) {
for (i=0,j=0; i < W*H; i++,j+=3) {
buffer[j+2]=(unsigned char)file.get();
buffer[j+1]=(unsigned char)file.get();
buffer[j] =(unsigned char)file.get();
}
}
} else {
std::cout << "file not found" << std::endl;
return false;
}
return true;
}
int main()
{
// pixels represents the bits of the image.
// I assume an image sized 256 by 256 pixels (because that is
// what the LoadBMP() function assumes).
// The function loads pixels as triplets ordered as R,G,B.
unsigned char* pixels = new unsigned char[W * H * 3];
// Fill the pixels buffer from the file
LoadBMP( "test256x256.bmp", pixels );
// Draw the image on the console window
// first, find the console window
SetConsoleTitle( "ozone95's bitmap" );
HWND hConsole = FindWindow( NULL, "ozone95's bitmap" );
HDC hConsoleDC = GetDC( hConsole );
// then, loop through all the pixels and draw them
unsigned char* p = pixels;
for (int y = H-1; y >= 0; y--) {
for (int x = 0; x < W; x++) {
SetPixel( hConsoleDC, x, y, RGB( p[0], p[1], p[2] ) );
p += 3;
}
}
// finally, release the DC and free memory used
ReleaseDC( hConsole, hConsoleDC );
DeleteDC( hConsoleDC );
delete[] pixels;
// wait for the user to end
getchar();
return 0;
}
|