The following is a specific analysis of a C + + program (DrawBmp.cpp, Borland C + + Builder 6.0 compile-run)
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DrawBitmap.h"
//---------------------------------------------------------------------------
#pragma package (smart_init)
#pragma resource "*.DFM"
TForm1 *form1;
typedef enum//define execution result for display bitmap operation
{
OK,//success
Formaterror,//File format error
FileNotFound,//File not open
OVERFLOW,//Memory Overflow
Readerror//Read file error
}resultcode;
ResultCode drawbmp (int start_x, int start_y, char filename[]); Show Bitmap function definitions
Ansistring Filen; The name of the file that needs to be displayed
//---------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
Filen = InputBox ("256-bit graph display program", "Enter 256-bit diagram file name ...", filen);
}
//---------------------------------------------------------------------------
void __fastcall Tform1::formpaint (tobject *sender)
{
ResultCode res = drawbmp (0,0,filen.c_str ());
}
#include "stdio.h"
unsigned char dac[256][3]; Color Conversion matrix
ResultCode drawbmp (int start_x, int start_y, char filename[])
{
int i,j,k,n,r,g,b,width,length;
int xcnt = 0,ycnt = 0;
FILE *FP;
if ((Fp=fopen (filename, "RB"))//Open bitmap file (==null)
{
ShowMessage ("File open failed");
return filenotfound;
}
Fseek (Fp,28,seek_set); First 28 Bytes is bitmap file header basic information
Fread (&I,2,1,FP); 29, 30 bytes for bitmap color deep digits
if (i!=8)/* Check for 256-color bitmap * *
{
Fclose (FP);
return formaterror;
}
Fseek (Fp,18,seek_set);
Fread (&WIDTH,4,1,FP); 19,20,21,22 Storage Bitmap Width
Fread (&LENGTH,4,1,FP); 23,24,25,26 Storage represents bitmap length
Fseek (Fp,54,seek_set); 54 bytes After the Bitmap Color transformation matrix section
for (i=0;i<256;i++)
{
Dac[i][0] = fgetc (FP);
DAC[I][1] = fgetc (FP);
DAC[I][2] = fgetc (FP); /* Get R, G, B components, put in the color conversion table * *
FGETC (FP); Reserved bit, do not use
}
Form1->height = length+30; Set the form display properties, resize and position the form by bitmap size
Form1->width = Width;
Form1->left = (1024-form1->width)/2; Applicable display resolution 1024 * 768
Form1->top = (768-form1->height)/2;
ycnt = Start_y+length;
unsigned char *buffer; Set output buffering
Buffer = (unsigned char *) malloc (width); Allocate the space required for line output to buffer
if (!buffer)
{
return OVERFLOW;
}
while (ycnt > 0)
ycnt, xcnt for dot matrix counter, bitmap and display screen coordinate system is different, ycnt//also used for coordinate conversion
{
xcnt = 0;
if (Fread (BUFFER,WIDTH,1,FP)!= 1)//read one line
{
return readerror;
}
while (xcnt < width)//inline scan each point, alluding to the monitor
{
unsigned char data = buffer[xcnt];
Find a color conversion table, output a dot matrix to a form
FORM1->CANVAS->PIXELS[XCNT][YCNT] =
RGB (Dac[data][2],dac[data][1],dac[data][0]);
xcnt++;
}
ycnt--;
}
Fclose (FP);
return OK;
}
//---------------------------------------------------------------------------