Baidu search cdib.zip http://www.axu.cn/tech_doc/index.htm ager digital http://www.axu.cn/tech_doc/pcbook/imageProcess/
CDIBDATA IS A UTILITY CLASS for: Loading, Saving, And Manipulating Bitmaps. Http://www.codeproject.com/bitmap/jscdibdata.asp
Cexbitmap: a cbitmap extension class with undo / redohttp://www.codeproject.com/bitmap/jrscexbitmap.asp
Interview Question: Rotate a Bitmap by 90 Degrees 8/18/2004
http://blog.joycode.com/fyuan/articles/30945.aspx
#
RE: Interview Question: Rotate a Bitmap by 90 Degrees
8/20/2004 6:27 PM
enoloo
Help: I have been commissioned for 2 hours, and there is still a problem with rotation. Here is a simple test -
// Dib.h
#ifndef _dib_h_
#define _dib_h_
#include
#pragma Pack (Push, 1)
TYPEDEF STRUCT {byte a; byte B; byte C;
#pragma pack (POP)
Class CDIB
{
PUBLIC:
CDIB ();
~ CDIB ();
BOOL LOAD (LPCTSTR STRFILENAME);
Bool Save (LPCTSTR STRFILENAME);
Void Show (HDC HDC, POINT PT);
// Closewise Rotate 90 Degree. And Reset The Bitmap File Also.
CDIB * Rotate90 () Const;
PUBLIC:
Long m_nbpl; // bytes per line
Long M_SZRGBQUAD;
BitmapInfoHeader M_Bmheader;
RGBQUAD * m_Prgbquad;
BYTE * m_PDATA;
}
#ENDIF
// Dib.cpp
#include "dib.h"
#include
#include
Template
Void g_rotate90 (const cdib * psrc, cdib * pdes)
{
Long deswidth = pdes-> m_bmHeader.biwidth;
Long desheight = pdes-> m_bmHeader.biheight;
Long desheightdec = pdes-> m_bmHeader.biheight - 1;
Long srcheightdec = psrc-> m_bmHeader.biheight - 1;
Long srcbpl = psrc-> m_nbpl;
Long desbpl = pdes-> m_nbpl;
BYTE * psrcdata = psrc-> m_pdata;
BYTE * pDESDATA = PDES-> M_PDATA;
INT I, J;
For (i = 0; i Const Pixel * ps = (Pixel *) ((byte *) psrcdata srcbpl * srcheightdec) i; Pixel * Pd = (Pixel *) ((Byte *) PDESData Desbpl * (DesheightDec - I)); for (j = 0; j * PD = * ps; PS = (Pixel *) ((Byte *) PS - SRCBPL); } } } CDIB :: CDIB () { m_prgbquad = null; M_PDATA = NULL; m_szrgbquad = 0; m_nbpl = 0; } CDIB :: ~ CDIB () { m_szrgbquad = 0; m_nbpl = 0; IF (m_pdata! = NULL) { Delete M_PDATA; M_PDATA = NULL; } IF (M_Prgbquad! = NULL) { delete m_prgbquad; m_prgbquad = null; } } Bool CDIB :: Load (LPCTSTR STRFILENAME) { Std :: ifstream infile (strfilename, std :: ios :: binary | std :: ios :: in); IF (! infile.is_open ()) Return False; // Get the file size. Infile.seekg (0, std :: os :: end); Const long szfile = infile.tellg (); IF (SZFILE <= 0) { Infile.Close (); Return False; } Word Bmflag [7]; Infile.seekg (std :: ios :: beg); Infile.read ((char *) BMFLAG, SIZEOF (WORD) * 7); IF (Bmflag [0]! = ((Word) 'B' | 'M' << 8) // it's not a bitmap file. { Infile.Close (); Return False; } Infile.read ((char *) & m_bmheader, sizeof (bitmapInfoheader); Long width = m_bmHeader.biwidth; Long height = m_bmHeader.biheight; Long bpp = m_bmHeader.biBitcount; IF (BPP <1) { Infile.Close (); Return False; } // Bytes per line = (BITS 31) / 32 * 4. M_nbpl = ((Width * BPP 31) >> 5) << 2; Long clrused = m_bmHeader.biclrused; IF (CLRUSED == 0) { Switch (bpp) { Case 1: M_SZRGBQUAD = 2; Break; Case 4: m_szrgbquad = 16; Break; Case 8: m_szrgbquad = 256; Break; Case 16: // no color table, the case: m_bmHeader.biCompression == bi_rgb m_szrgbquad = 0; Break; DEFAULT: m_szrgbquad = 0; } } Else { m_szrgbquad = clrused; } // Match the color table. IF (m_szrgbquad! = 0) { m_prgbquad = new rgbquad [m_szrgbquad]; Infile.read ((char *) m_prgbquad, sizeof (rgbquad) * m_szrgbquad); } Long ndata = height * m_nbpl; m_pdata = new byte [ndata]; // allocate a buffer for BM Data. Infile.read ((char *) m_pdata, sizeof (byte) * ndata; Infile.Close (); Return True; } Bool CDIB :: Save (LPCTSTR STRFILENAME) { Std :: OFStream Outfile (Strfilename, std :: ios :: binary | std :: os :: ut | std :: os :: trunc; IF (! outfile.is_open ()) Return False; Word Bmflag [7]; // for bitmapfileHeader with 1 alignment. Bmflag [0] = (Word ('b') | 'm' << 8); Long ndata = m_bmHeader.biheight * m_nbpl; DWORD SZFILE = (Sizeof (Word) * 7) sizeof (BitmapInfoHeader) (SIZEOF (RGBQUAD) * m_szrgbquad) (SIZEOF (BYTE) * NDATA); Bmflag [1] = loword (szfile); BMFLAG [2] = HiWord (SZFile); DWORD SZOFFDATA = (SizeOf (Word) * 7) Sizeof (BitmapInfoHeader) (SIZEOF (RGBQUAD) * m_szrgbquad; Bmflag [5] = loword (szoffdata); Bmflag [6] = HiWord (SzoffData); Outfile.write ((char *) BMFLAG, SIZEOF (WORD) * 7); Outfile.write ((char *) & m_bmheader, sizeof (bitmapinfoheader); IF (m_szrgbquad! = 0) Outfile.write ((char *) m_prgbquad, sizeof (rgbquad) * m_szrgbquad); Outfile.write ((char *) m_pdata, sizeof (byte) * ndata); Outfile.Close (); Return True; } Void CDIB :: Show (HDC HDC, POINT PT) { ASSERT (HDC); Hbitmap hbmp = createdibitmap (HDC, & M_BmHeader, CBM_INIT, M_PDATA, (BitmapInfo *) & m_bmheader, dib_rgb_colors; HDC HMEMDC = CreateCompatiPLEDC (HDC); Hbitmap Hold = (Hbitmap) SelectObject (HMEMDC, HBMP); Bitblt (HDC, PT.x, Pt.y, M_BmHeader.biwidth, M_BmHeader.biheight, HMEMDC, 0, 0, SRCCOPY; SelectObject (HMEMDC, HOLD); DeleteObject (HBMP); Deletedc (HMEMDC); Return; } CDIB * CDIB :: Rotate90 () Const { // first, check! Int bitcount = m_bmHeader.bibitcount; IF (! m_pdata || bitcount% 8) Return NULL; Long newwidth = m_bmHeader.biheight; Long newheight = m_bmHemer.biwidth; // Second, Create A New Instance of CDIB. CDIB * PRET = New CDIB (); PRET-> M_BMHEADER = M_BMHEADER; PRET-> m_bmHeader.biheight = newheight; Pret-> m_bmHeader.biwidth = newwidth; Pret-> m_nbpl = ((NewWidth * Bitcount 31) >> 5) << 2; PRET-> m_szrgbquad = m_szrgbquad; IF (m_szrgbquad! = 0) Memcpy (pret-> m_prgbquad, m_prgbquad, sizeof (rgbquad) * m_szrgbquad); PRET-> m_pdata = new byte [Pret-> m_nbpl * newheight]; IF (! PRET-> M_PDATA) RETURN NULL; // Third, Copy The Data. Switch (bitcount) { Case 8: g_rotate90 (this, PRET); Break; Case 15: Case 16: g_rotate90 (this, PRET); Break; Case 24: g_rotate90 (this, PRET); Break; Case 32: g_rotate90 (this, PRET); Break; Case 64: g_rotate90 (this, PRET); Break; DEFAULT: Assert (false); Delete Pret; PRET = NULL; } Return PRET; } # RE: Interview Question: Rotate a Bitmap by 90 Degrees 8/20/2004 6:33 PM enoloo Test code (Win32 HelloWorld Project.): // ...(slightly) Case id_save: // Save g_pdib-> save ("abc.bmp"); Break; Case ID_ROTATE: / / Rotate PDIB = g_pdib-> rotate90 (); Delete g_pdib; g_pdib = PDIB; GetClientRect (hwnd, & review); INVALIDATERECT (HWND, & Rect, False); Break; // ...(slightly) Case WM_Paint: // Display HDC = BeginPaint (HWND, & PS); // Todo: add any drawing code here ... RECT RT; GetClientRect (hwnd, & rt); // DrawText (HDC, Szhello, Strlen (Szhello), & RT, DT_CENTER; PT.X = Pt.y = 0; g_pdib-> show (HDC, PT); Endpaint (hwnd, & ps); Break; ----- Questions are as follows --- When the code is executed: For (i = 0; i Const Pixel * ps = (Pixel *) ((byte *) psrcdata srcbpl * srcheightdec) i; Pixel * Pd = (Pixel *) ((Byte *) PDESData Desbpl * (DesheightDec - i)); For (j = 0; j * PD = * ps; PS = (Pixel *) ((Byte *) PS - SRCBPL); } } When i = 888, the program error is exceeded. Debugged for 2 hours, there is no result. Thank you for your attention.