Geometric changes in digital image processing-VC code

Source: Internet
Author: User

Digital image processing performs geometric operations on the image, including image translation, image rotation, image scaling, image placement, and image mirroring.

Image Translation:

/*************************************** **********************************
* Function name: Translation ()
* Function parameters:
* LPSTR lpSrcStartBits refers to the pointer to the starting pixel of the source DIB
* Long lWidth, DIB image width
* Long lHeight: the height of the DIB image
* Long xOff, X offset
* Long yOff, Y offset
* Long lLineBytes: Number of row bytes of the DIB image, a multiple of 4
* Function type: BOOL
* Function: This function is used to translate DIB images.
**************************************** ********************************/
BOOL Translate (LPSTR lpSrcStartBits, long lWidth, long lHeight,
Long xOff, long yOff, long lLineBytes, int bitCount)
{
HLOCAL hDstDibBits;
LPSTR lpDstStartBits;
LPSTR lpDstDibBits;
LPSTR lpSrcDibBits;

HDstDibBits = LocalAlloc (LHND, lWidth * lLineBytes );
LpDstStartBits = (char *) LocalLock (hDstDibBits );
// A temporary pointer pointing to the transformed data
If (hDstDibBits = NULL)
{
Return false;
}

If (bitCount = 8) // 256 color image
{
For (int I = 0; I <lHeight; I ++)
{
For (int j = 0; j <lWidth; j ++)
{
// Pointer to the pixel of column j of row I in temporary dib
LpDstDibBits = (char *) lpDstStartBits +
LLineBytes * (lHeight-1-i) + j;

If (j-xOff> 0) & (j-xOff <lWidth)
& (I-yOff> 0) & (I-yOff <lHeight ))
{
// Pointer to the source DIB row i0 and pixel j0
LpSrcDibBits = (char *) lpSrcStartBits +
LLineBytes * (lHeight-1-(I-yOff) + (j-xOff );
* LpDstDibBits = * lpSrcDibBits; // copy the pixel
}
Else
{
* (Unsigned char *) lpDstDibBits) = 255; // The value out of the range is 255.
}

}
}
}
Else if (bitCount = 24) // 24-bit true color image
{
For (int I = 0; I <lHeight; I ++)
{
For (int j = 0; j <lWidth; j ++)
{
// Pointer to the pixel of column j of row I in temporary dib
LpDstDibBits = (char *) lpDstStartBits +
LLineBytes * (lHeight-1-i) + j * 3;

If (j-xOff> 0) & (j-xOff <lWidth)
& (I-yOff> 0) & (I-yOff <lHeight ))
{
// Pointer to the source DIB row i0 and pixel j0
LpSrcDibBits = (char *) lpSrcStartBits +
LLineBytes * (lHeight-1-(I-yOff) + (j-xOff) * 3;
* LpDstDibBits = * lpSrcDibBits; // copy the pixel
LpDstDibBits ++;
LpSrcDibBits ++;
* LpDstDibBits = * lpSrcDibBits;
LpDstDibBits ++;
LpSrcDibBits ++;
* LpDstDibBits = * lpSrcDibBits;
}
Else
{
* (Unsigned char *) lpDstDibBits) = 255; // The value out of the range is 255.
LpDstDibBits ++;
* (Unsigned char *) lpDstDibBits) = 255;
LpDstDibBits ++;
* (Unsigned char *) lpDstDibBits) = 255;
}

}
}
}
Else
{
Return false;
}

Memcpy (lpSrcStartBits, lpDstStartBits, lLineBytes * lHeight );

LocalUnlock (hDstDibBits );
Localfree (hdstdibbits );
Return true;
}

Image rotation:

/*************************************** **********************************
* Function name: Rotate ()
* Function parameters:
* Lpstr lpsrcstartbits refers to the pointer to the starting pixel of the source Dib
* Long lwidth, source DIB image width
* Long lheight, source DIB image height
* Double angle, Rotation Angle
* Int xcenter: the position of the new center after rotation
* Int ycenter: the vertical position of the new center after rotation
* Long llinebytes, source DIB image byte width (multiples of 4)
* Int bitcount, number of image digits
* Function type: bool
* Function: Used to rotate the DIB image.
**************************************** ********************************/
Bool rotate (lpstr lpsrcstartbits, long lwidth, long lheight, double angle,
Int xcenter, int ycenter, long llinebytes, int bitcount)
{
Int xoricenter = (INT) lwidth/2;
Int yoricenter = (INT) lheight/2;
Int I, j, tempi, tempj;

Hlocal hdstdibbits;
Lpstr lpdststartbits;
Lpstr lpdstdibbits;
Lpstr lpsrcdibbits;

Hdstdibbits = localalloc (lhnd, lwidth * llinebytes );
Lpdststartbits = (char *) locallock (hdstdibbits );
// A temporary pointer pointing to the transformed data
If (hDstDibBits = NULL)
{
Return false;
}

If (bitCount = 8) // 256 color image
{
For (I = 0; I <lHeight; I ++)
{
For (j = 0; j <lWidth; j ++)
{
// Pointer to the pixel of column j of row I in temporary dib
LpDstDibBits = (char *) lpDstStartBits +
LLineBytes * (lHeight-1-i) + j;
 
TempJ = (int) (j * cos (angle) + I * sin (angle)
-XCenter * cos (angle)-yCenter * sin (angle) + xOriCenter + 0.5 );
Tempi = (INT) (-J * sin (angle) + I * Cos (angle)
+ Xcenter * sin (angle)-ycenter * Cos (angle) + yoricenter + 0.5 );

If (tempi> 0) & (tempi <lheight)
& (Tempj> 0) & (tempj <lwidth ))
{
// Pointer to the source Dib row I0 and pixel J0
Lpsrcdibbits = (char *) lpsrcstartbits +
Llinebytes * (lHeight-1-tempI) + tempj;
* Lpdstdibbits = * lpsrcdibbits; // copy the pixel
}
Else
{
* (Unsigned char *) lpdstdibbits) = 255; // The value out of the range is 255.
}

}
}
}
Else if (bitcount = 24) // 24-bit true color image
{
For (INT I = 0; I <lheight; I ++)
{
For (Int J = 0; j <lwidth; j ++)
{
// Pointer to the pixel of column J of row I in temporary Dib
Lpdstdibbits = (char *) lpdststartbits +
Llinebytes * (lHeight-1-i) + J * 3;

Tempj = (INT) (J * Cos (angle) + I * sin (angle)
-Xcenter * Cos (angle)-ycenter * sin (angle) + xoricenter + 0.5 );
TempI = (int) (-j * sin (angle) + I * cos (angle)
+ XCenter * sin (angle)-yCenter * cos (angle) + yOriCenter + 0.5 );

If (tempI> 0) & (tempI <lHeight)
& (TempJ> 0) & (tempJ <lWidth ))
{
// Pointer to the source DIB row i0 and pixel j0
LpSrcDibBits = (char *) lpSrcStartBits +
LLineBytes * (lHeight-1-tempI) + tempJ * 3;
* LpDstDibBits = * lpSrcDibBits; // copy the pixel
LpDstDibBits ++;
LpSrcDibBits ++;
* LpDstDibBits = * lpSrcDibBits;
Lpdstdibbits ++;
Lpsrcdibbits ++;
* Lpdstdibbits = * lpsrcdibbits;
}
Else
{
* (Unsigned char *) lpdstdibbits) = 255; // The value out of the range is 255.
Lpdstdibbits ++;
* (Unsigned char *) lpdstdibbits) = 255;
Lpdstdibbits ++;
* (Unsigned char *) lpdstdibbits) = 255;
}
}
}
}
Else
{
Return false;
}

Memcpy (lpsrcstartbits, lpdststartbits, llinebytes * lheight );

Localunlock (hdstdibbits );
Localfree (hdstdibbits );
Return true;

}

Image Scaling:

/*************************************** **********************************
* Function name: Zoom ()
* Function parameters:
* Lpstr lpsrcdib refers to the pointer to the source Dib
* Lpstr lpsrcstartbits refers to the pointer to the starting pixel of the source Dib
* Long lwidth, source DIB image width
* Long lheight, source DIB image height
* Long llinebytes, source DIB image byte width (multiples of 4)
* Double xratio, horizontal scaling ratio
* Double yRatio, vertical scaling ratio
* Int bitCount, number of image digits
* WORD palSize, source DIB image palette size
* Function type: HGLOBAL
* Function: Used to scale the DIB image.
**************************************** ********************************/

HGLOBAL Zoom (LPSTR lpSrcDib, LPSTR lpSrcStartBits, long lWidth, long lHeight,
Long lLineBytes, double xRatio, double yRatio, DWORD palSize, int bitCount)
{
LPSTR lpDstDib; // pointer to a temporary Image
LPSTR lpSrcDIBBits; // pointer to the source Pixel
LPSTR lpDstDIBBits; // pointer to the pixel corresponding to the temporary Image
LPSTR lpDstStartBits; // pointer to the pixel corresponding to the temporary Image

LPBITMAPINFOHEADER lpbmi; // pointer to the BITMAPINFO Structure
Long lDstWidth = (long) (lWidth x xRatio + 0.5 );
Long lDstHeight = (long) (lHeight * yRatio + 0.5 );
Int lDstLineBytes = (int) (lDstWidth * bitCount + 31)/32*4;
Int I, j, i1, j1;

// Allocate memory to save the scaled DIB
HGLOBAL hDIB = (HGLOBAL): GlobalAlloc (GHND, lDstLineBytes * lDstHeight
+ * (LPDWORD) lpSrcDib + palSize );
// Note: the first data that lpSrcDib points to is DWORD biSize;
If (hDIB = NULL)
{
Return FALSE;
}
LpDstDib = (char *): GlobalLock (HGLOBAL) hDIB );
 
// Copy the DIB information header and palette
Memcpy (lpDstDib, lpSrcDib, * (LPDWORD) lpSrcDib + palSize );

// Update the image information header in DIB
Lpbmi = (LPBITMAPINFOHEADER) lpDstDib;
Lpbmi-> biWidth = lDstWidth;
Lpbmi-> biHeight = lDstHeight;

// Update the pixel data in DIB
LpDstStartBits = lpDstDib + * (LPDWORD) lpDstDib
+ PalSize;

If (bitCount = 8)
{

For (I = 0; I <lDstHeight; I ++) // row operation
{
For (j = 0; j <lDstWidth; j ++) // column operation
{
// Pointer to row I and pixel j of the new DIB
LpDstDIBBits = (char *) lpDstStartBits
+ Ldstlinebytes * (lDstHeight-1-i) + J;
I1 = (long) (I/yratio + 0.5); // calculates the coordinates of the pixel in the source DiB.
J1. = (long) (J/xratio + 0.5 );

If (J1> = 0) & (J1 <lwidth) & (I1> = 0) & (I1 <lheight ))
{// Determine whether the source image is used
Lpsrcdibbits = (char *) lpsrcstartbits
+ Llinebytes * (lheight-1-I1) + J1; // pointer to row I of source DiB and pixel J
* Lpdstdibbits = * lpsrcdibbits; // copy the pixel
}
Else
{
* (Unsigned char *) lpDstDIBBits) = 255; // pixels that do not exist in the source image, assigned 255
}
}
}
Return hDIB;
}

Else if (bitCount = 24)
{
For (I = 0; I <lDstHeight; I ++) // row operation
{
For (j = 0; j <lDstWidth; j ++) // column operation
{
// Pointer to row I and pixel j of the new DIB
LpDstDIBBits = (char *) lpDstStartBits
+ LDstLineBytes * (lDstHeight-1-i) + j * 3;
I1 = (long) (I/yRatio + 0.5); // calculates the coordinates of the pixel in the source DIB.
J1. = (long) (j/xRatio + 0.5 );

If (j1> = 0) & (j1 <lWidth) & (i1> = 0) & (i1 <lHeight ))
{// Determine whether the source image is used
LpSrcDIBBits = (char *) lpSrcStartBits
+ LLineBytes * (lHeight-1-i1) + j1 * 3; // pointer to row I of source DIB and row j Pixel
* LpDstDIBBits = * lpSrcDIBBits; // copy the pixel
LpDstDIBBits ++;
LpSrcDIBBits ++;
* LpDstDIBBits = * lpSrcDIBBits;
LpDstDIBBits ++;
LpSrcDIBBits ++;
* LpDstDIBBits = * lpSrcDIBBits;
}
Else
{
* (Unsigned char *) lpDstDIBBits) = 255; // pixels that do not exist in the source image, assigned 255
LpDstDIBBits ++;
* (Unsigned char *) lpDstDIBBits) = 255;
LpDstDIBBits ++;
* (Unsigned char *) lpDstDIBBits) = 255;
}
}
}
Return hDIB;
}

Else
{
Return NULL;
}
 
}

Image transpose:

/*************************************** **********************************
* Function name: Transpose ()
* Function parameters:
* LPSTR lpSrcDib refers to the pointer to the source DIB
* LPSTR lpSrcStartBits refers to the pointer to the starting pixel of the source DIB
* Long lWidth, source DIB image width
* Long lHeight, source DIB image height
* WORD palSize, source DIB image palette size
* Int bitCount, number of image digits
* Function type: BOOL
* Function: Used to transpose a DIB image.
**************************************** ********************************/
BOOL Transpose (LPSTR lpSrcDib, LPSTR lpSrcStartBits, long lWidth, long lHeight,
DWORD palSize, int bitCount)
{
LPSTR lpDstDib; // pointer to a temporary Image
LPSTR lpSrcDIBBits; // pointer to the source Pixel
LPSTR lpDstDIBBits; // pointer to the pixel corresponding to the temporary Image
LPSTR lpDstStartBits; // pointer to the pixel corresponding to the temporary Image

LPBITMAPINFOHEADER lpbmi; // pointer to the BITMAPINFO Structure
Long lDstWidth = lHeight;
Long lDstHeight = lWidth;
Int lSrcLineBytes = (int) (lWidth * bitCount + 31)/32*4;
Int lDstLineBytes = (int) (lDstWidth * bitCount + 31)/32*4;

// Allocate memory to save the scaled DIB
HGLOBAL hDIB = (HGLOBAL): GlobalAlloc (GHND, lDstLineBytes * lDstHeight
+ * (LPDWORD) lpSrcDib + palSize );
// Note: the first data that lpSrcDib points to is DWORD biSize;
If (hDIB = NULL)
{
Return FALSE;
}
LpDstDib = (char *): GlobalLock (HGLOBAL) hDIB );

// Update the image information header in DIB
Lpbmi = (LPBITMAPINFOHEADER) lpSrcDib;
Lpbmi-> biWidth = lDstWidth;
Lpbmi-> biHeight = lDstHeight;

// Update the pixel data in DIB
LpDstStartBits = lpDstDib + * (LPDWORD) lpDstDib
+ PalSize;

If (bitCount = 8)
{
For (int I = 0; I <lDstHeight; I ++) // row operation
{
For (int j = 0; j <lDstWidth; j ++) // column operation
{
// Pointer to row I and pixel j of the new DIB
LpDstDIBBits = (char *) lpDstStartBits
+ LDstLineBytes * (lDstHeight-1-i) + j;

// Refers to the pointer to the source DIB row j and the I Pixel
LpSrcDIBBits = (char *) lpSrcStartBits
+ LSrcLineBytes * (lHeight-1-j) + I;

* LpDstDIBBits = * lpSrcDIBBits; // copy the pixel

}
}
 
Memcpy (lpSrcStartBits, lpDstStartBits, lDstLineBytes * lDstHeight );
LocalUnlock (hDIB );
LocalFree (hDIB );

Return true;

}
Else if (bitCount = 24)
{
For (int I = 0; I <lDstHeight; I ++) // row operation
{
For (int j = 0; j <lDstWidth; j ++) // column operation
{
// Pointer to row I and pixel j of the new DIB
LpDstDIBBits = (char *) lpDstStartBits
+ LDstLineBytes * (lDstHeight-1-i) + j * 3;

// Refers to the pointer to the source DIB row j and the I Pixel
LpSrcDIBBits = (char *) lpSrcStartBits
+ LSrcLineBytes * (lHeight-1-j) + I * 3;

* LpDstDIBBits = * lpSrcDIBBits; // copy the pixel

LpDstDIBBits ++;
LpSrcDIBBits ++;
* LpDstDIBBits = * lpSrcDIBBits;
LpDstDIBBits ++;
LpSrcDIBBits ++;
* LpDstDIBBits = * lpSrcDIBBits;

}
}
 
Memcpy (lpSrcStartBits, lpDstStartBits, lDstLineBytes * lDstHeight );
LocalUnlock (hDIB );
LocalFree (hDIB );

Return true;
}
Else
{
Return false;
}

}

Image:

/*************************************** **********************************
* Function name: Mirror ()
* Function parameters:
* LPSTR lpSrcStartBits refers to the pointer to the starting pixel of the source DIB
* Long lWidth, source DIB image width
* Long lHeight, source DIB image height
* Int bitCount, number of image digits
* Int mirrorType: Image Type
* Function type: BOOL
* Function: Used to transpose a DIB image.
**************************************** ********************************/
BOOL Mirror (LPSTR lpSrcStartBits, long lWidth,
Long lHeight, int bitCount, int mirrorType)
{
HLOCAL hDstDibBits;
LPSTR lpDstStartBits;
LPSTR lpDstDibBits;
LPSTR lpSrcDibBits;
Long lLineBytes = (lWidth * bitCount + 31)/32*4;
 
HDstDibBits = LocalAlloc (LHND, lWidth * lLineBytes );
LpDstStartBits = (char *) LocalLock (hDstDibBits );
// A temporary pointer pointing to the transformed data
If (hDstDibBits = NULL)
{
Return false;
}

If (bitCount = 8) // 256 color image
{
For (int I = 0; I <lHeight; I ++)
{
For (int j = 0; j <lWidth; j ++)
{
// Pointer to the pixel of column j of row I in temporary dib
LpDstDibBits = (char *) lpDstStartBits +
LLineBytes * (lHeight-1-i) + j;

If (mirrorType = MIRRORTYPE_VER)
{
LpSrcDibBits = (char *) lpSrcStartBits +
LLineBytes * I + j;
}
Else if (mirrorType = MIRRORTYPE_HORI)
{
LpSrcDibBits = (char *) lpSrcStartBits +
LLineBytes * (lHeight-1-i) + lWidth-1-j;
}
Else
{
Return false;
}

* LpDstDibBits = * lpSrcDibBits; // copy the pixel

}
}
}
Else if (bitCount = 24) // 24-bit true color image
{
For (int I = 0; I <lHeight; I ++)
{
For (int j = 0; j <lWidth; j ++)
{
// Pointer to the pixel of column j of row I in temporary dib
LpDstDibBits = (char *) lpDstStartBits +
LLineBytes * (lHeight-1-i) + j * 3;

If (mirrortype = mirrortype_ver)
{
Lpsrcdibbits = (char *) lpsrcstartbits +
Llinebytes * I + J * 3;
}
Else if (mirrortype = mirrortype_hori)
{
Lpsrcdibbits = (char *) lpsrcstartbits +
Llinebytes * (lHeight-1-i) + (lWidth-1-j) * 3;
}
Else
{
Return false;
}

* Lpdstdibbits = * lpsrcdibbits; // copy the pixel
LpDstDibBits ++;
LpSrcDibBits ++;
* LpDstDibBits = * lpSrcDibBits;
LpDstDibBits ++;
LpSrcDibBits ++;
* LpDstDibBits = * lpSrcDibBits;
}
}
}
Else
{
Return false;
}

Memcpy (lpSrcStartBits, lpDstStartBits, lLineBytes * lHeight );

LocalUnlock (hDstDibBits );
LocalFree (hDstDibBits );
Return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.