VC implementation generating BMP file (DDA algorithm drawing straight)

xiaoxiao2021-03-18  219

Recently, the teacher arranged a job, drawing straight lines with DDA algorithm, and I realized a class that generated the content of the customer zone as a BMP file.

Some of these details are worth noting, so they are posted to share ~~

DDA

Algorithm implementation line

Ye Xinzhou, Sun Yat-sen University

One.

Experimental content

Pain a line segment with the DDA algorithm idea.

This program is developed in the VC.NET platform, based on the MFC framework, implements the line segment according to the start coordinate (planar coordinate), and saves the client area image uncompressed BMP file.

two.

Algorithm ideology

The DDA of this program is different from the common DDA algorithm:

Using the X direction, it is incremented by 1, and then determines whether the Y coordinate of the previous point and the difference between the Y coordinate of the current point is greater than 1, then the link is two points in the intermediate link (at this time, all point X coordinates between two points. ).

three.

Algorithm core code

Void

CDRAWLINEVIEW :: DrawlineBydda (int NStartx, int NStarty, int NENDX, INT NENDY)

{

INT I;

/ / Define the coordinate system conversion

CRECT CRECT;

GetClientRect (CRECT);

M_noriginy = crect.height () - 50;

m_noriginx = 50;

Drawcoordinate (m_noriginx, m_noriginy); / / Draw the right angle coordinate system

CClientDC DC (this);

NSTARTX = M_NORIGINX NSTARTX;

Nendx = m_noriginx nendx;

NSTARTY = m_noriginy-nstarty;

Nendy = m_noriginy-nendy;

// end of defined coordinate system conversion

IF (nStartx == NENDX) // is vertical line

{

IF (nStarty> Nendy) // To ensure that NSTARTY is less than Nendy

{

Int T;

T = NSTARTY;

NSTARTY = NENDY;

Nendy = T;

}

For (i = nStarty; i <= nendy; i )

{

Dc.SETPIXEL (NStartX, I, M_CLINECOLOR);

}

}

ELSE / / is not vertical

{

INT X;

INT NBEGINX, NSTOPX, NBEGINY, NSTOPY;

Nbeginx = nStartX;

NStopx = NENDX;

Nbeginy = nStarty;

Nstopy = Nendy;

IF (NStartx> Nendx) / / Make sure (nbeginx, nbeginy) on the left

{

Nbeginx = nendx;

Nstopx = nStartX;

Nbeginy = nendy;

Nstopy = NSTARTY;

}

Double K = (nstopy * 1.0-nbeginy) / (nstopx-nbeginx);

INT OLDY = (INT) ((k * (nbeginx-nbeginx) nbeginy)); // Last Y position

For (x = nbeginx; x <= nstopx; x )

{

Dc.SetPixel (x, k * (x-nbeginx) nbeginy, m_clinecolor;

IF (Oldy- (X-Nbeginx) Nbeginy> 1) / / Indicates that there is a point in Y direction

{

For (int J = (k * (x-nbeginx) nbeginy); j <= = = = il; J )

Dc.SetPixel (X, J, M_CLINECOLOR);

}

IF (Oldy- (X-Nbeginx) Nbeginy <- 1) // Indicates that there is no fill in the Y direction {

For (int J = Oldy; j <= (k * (x-nbeginx) nbeginy); J )

Dc.SetPixel (X, J, M_CLINECOLOR);

}

Oldy = (int) ((k * (x-nbeginx) nbeginy);

}

}

ReleaseDC (& DC);

}

four.

Important function description

The biggest feature of this program is to save the customer area content into BMP files. In order to achieve this, I carefully studied the BMP file structure and implemented the function of assembling the BMP file under VC.NET.

In order to achieve BMP file generation, a separate class CMYBMP is generated, as follows:

//Mybmp.h

#pragma

ONCE

#include

Afx.h "

// CMYBMP

Class

CMYBMP: Public CWND

{

Declare_dynamic (CMYBMP)

public

:

CMYBMP (CSTRING FILENAME);

Virtual ~ cmybmp ();

protected

:

Declare_message_map ()

BitmapfileHeader M_Bmpheader; // BMP file header

BitmapInfo M_Bmpinfo; // BMP Information Block

BitmapInfoHeader M_BmpinfoHeader; / / BMP information head (ie, containing information header in BMP information block)

RGBQUAD M_BMPRGBQUAD; / / BMP color table (ie, the color table included in the BMP information block)

public

:

INT setBmpfileHeader (int Width, int Height);

CFILE M_BMPFILE;

Void Savetobmpfile (int Red, int green, int blue);

Struct MyPixel

{/ Note these domains,

PUBLIC:

BYTE B; / / Represents Blue

BYTE G; / / Representative Green

Byte r; // represents RED

}

Mypixel C1; // Defines a structure of a pixel point

}

//Mybmp.cpp

// mybmp.cpp: Implement file

//

#include

"stdafx.h"

#include

"Drawline.h"

#include

"Mybmp.h"

#include

"./mybmp.h"

// CMYBMP

Implement_dynamic (CMYBMP, CWND)

CMYBMP :: CMYBMP (CSTRING FileName)

{

m_bmpfile.open (filename, cfile :: modecreate | cfile :: modewrite); // Create a BMP file

}

CMYBMP :: ~ CMYBMP ()

{

// Turn off the BMP file

m_bmpfile.flush ();

m_bmpfile.close ();

}

Begin_MESSAGE_MAP (CMYBMP, CWND)

END_MESSAGE_MAP ()

// CMYBMP message handler

int

CMYBMP :: setBmpfileHeader (int width, int hotht)

{

m_bmpheader.bftype = 0x4d42;

m_bmpheader.bfsize = 3 * width * height 0x36; / / Indicates the entire BMP file byte, where 0x36 is the length of the file header

m_bmpheader.bfreserved1 = 0x0; m_bmpheader.bfreserved2 = 0x0;

m_bmpheader.bfoffbits = 0x36; // x36 is the length of the file header itself

// Total 14 bytes

m_BmpinfoHeader.bisize = sizeof (bitmapinfohead); / / indicator file information head size

m_BmpinfoHeader.biWidth = width; // Picture Width

m_BmpinfoHeader.biheight = height; // Picture Height

m_BmpinfoHeader.Biplanes = 1;

m_BmpinfoHeader.biBitcount = 24; // Picture digits, bit 24 bitmap

// Take a total of 14 16 bytes

m_BMPINFOHEADER.BICOMPRESSION = 0; // Indicates no compression

m_BmpinfoHeader.bisizeImage = 0x30; // Because there is no compression, it can be set to 0

M_BmpinfoHeader.bixpelspermeter = 0x0;

M_BmpinfoHeader.biypelspermeter = 0x0;

m_BmpinfoHeader.Biclrused = 0; // Indicates that all index colors

m_bmpinfoheader.birrimportant = 0; // Description Number of color indexes with important impact on image display, 0 is important.

// Take a total of 14 16 24 bytes

/*M_BMPRGBQUAD.RGBBLUE =0x0;

m_bmprgbquad.rgbgreen = 0x0;

m_bmprgbquad.rgbred = 0x0;

m_bmprgbquad.rgbreserved = 0x0;

* /

// m_bmpinfo.bmicolors [1] = NULL;

m_bmpinfo.bmiheader = m_bmpinfohead;

m_bmpfile.write (& (M_BmpHeader), SIZEOF (M_BmpHeader);

M_BmpFile.write (& M_BmpinfoHeader, sizeof (m_bmpinfo) --sizeof (m_bmprgbquad);

Return 1; // means successfully created a BMP file header

}

Void

CMYBMP :: Savetobmpfile (int Red, int green, int blue)

{

/ / Save the value of each pixel point

C1.B = Byte (blue);

C1.g = byte (green);

C1.R = Byte (RED);

m_bmpfile.write (& C1, SIZEOF (MyPixel));

}

Also attached one

CylineView class calls a CMYBMP class related functions

Void

CDRAWLINEVIEW :: OnsAveline ()

{

// This function saves the customer's draw content, and stores uncompressed BMP files.

// Technical key points: The storage of the map begins with the sit down angle, each pixel is expressed by three bytes, respectively BGR (note order),

/ / The number of bytes per line must be a multiple of 4, otherwise an error. . .

CRECT RECT;

GetClientRect (& Rect); // Get the customer area

Cfiledialog FDLG (False, "BMP", 0, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "*. BMP");

CString sfilename ("");

fdlg.domodal () == iDOK)

{

SfileName = fdlg.getFileName ();

IF (sfilename! = ")

{

OnDraw (getDC ()); / / forcibly refresh customer area

CMYBMP BMP1 (fdlg.getFileName ());

BMP1.SETBMPFILEHEADER (Rect.width () - (Rect.width ()% 4), Rect.Height ()); // Special attention! !

Int Red, Green, Blue;

CDC * PDC;

PDC = GetDC ();

For (int J = Rect.height (); j> = 1; J -)

For (int i = 1; i <= Rect.width () - (Rect.width ()% 4); i )

{

/ / Scan the client area every point, pay attention to the last right corner from the bottom corner

Blue = (PDC-> Getpixel (i, j)) / (256 * 256);

Green = (PDC-> Getpixel (i, j) -blue * (256 * 256)) / 256;

Red = PDC-> GetPixel (i, j) -blue * (256 * 256) -green * (256);

BMP1.SAVETOBMPFILE (Byte (Red), Byte (Green), Byte (Blue);

}

/ / Note The value obtained in RGB (A, B, C) is a * 1 b * 256 c * 256 * 256, inside the BMP file, the arrangement is BGR (address low-> high)

AFXMessageBox ("successfully created files!");

}

}

V. Procedure

(Establish straight line)

(Set background / line color)

(save Picture)

(Uncompressed BMP illustration of this program)

Six, experimental summary

Through this program, I understand the idea of ​​the DDA algorithm. Since I didn't refer to any DDA algorithm, I heard some in the class, so this DDA algorithm may be mostly the same.

In addition, since I saw the work of the classmates, I was deeply guilty. It turned out that the program did not take seriously. Therefore, on the basis of the original procedure, add some practical functions, especially the function of saving BMP files. The following focuses on the experience of the BMP file generation class development:

1.

Through this experiment, I check the relevant books and online information. I understand the full structure of the BMP file, and I also figure out some details, such as the number of scans per line byte scan (ie, aligned with DWORD), The expression of RGB in the BMP file, etc.

2.

Although I implemented the BMP file generation, but it is still unclear by some fields, mainly compressed

BiCompression, so I am set to 0, not compressed. Also vertical horizontal pixel value (ie

Bixpelspermeter and biypelspermeter are not unclear what is referring to, with reference information simply set to 0. Index color

BICLRUSED and its importance BICLRIMPORTANT I still don't understand what the use, but these should be able to find an answer online.

3.

When writing this part, I always use the debug tool to view the value of each byte, try to analyze their meaning, and facts prove that this method is still effective.

4.

I also found that some reference books said in BitmapInfoHead (in BitmapInfo) RGBQUAD (also in BitmapInfo), but some information said, but the corresponding bffbits You must set to 0x3e, I found that there is a write BMP picture does have such a value, but I am set to bfoffBits = 0x36, and there is no color table. Estimated this possible version problem? 5. Due to generation

The BMP process is the value of each pixel to collect the client area, so you must scan each point, the speed is really slow, this problem has not been resolved.

Seven, reference materials

Internet data, "BMP File Format Analysis", http://blog.9cbs.net/yxz149

March 15, 2006

转载请注明原文地址:https://www.9cbs.com/read-129904.html

New Post(0)