Image Scaling Function

Source: Internet
Author: User

Image scaling using CImage.

 

 

[Cpp]
// Used for image scaling
Bool CDIGTLSView: myScale (float keyX, float keyY) // when defining keyx and keyy as int, an error occurs.
{
// Programming: Li lizong lilizong@gmail.com
// 2012-8-6
KeyX = keyX;
KeyY = keyY;
// Srand (unsigned) time (NULL ));
// Float keyX, keyY; // magnification
// KeyX = (rand () % 20 + 1)/10.0 );
// KeyY = (rand () % 20 + 1)/10.0 );
// For a long time, there was always a problem during debugging. Later I found that the key value may be 0 at the beginning, so there was a problem. Remember !!!
// 2012-8-6
// CString str;
// Str. Format (TEXT ("% f"), key );
// MessageBox (str );
If (! MyImage2.IsNull ())
MyImage2.Destroy ();
If (! MyImage1.IsNull ())
MyImage1.Destroy ();
If (myImage1.IsNull ())
// OnOpenResourceFile ();
MyImage1.LoadFromResource (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDB_BITMAP3 ));
// OnOpenCustomeFile ();
// MyImage2.Destroy ();
If (myImage2.IsNull ())
MyImage2.Create (int) (myImage1.GetWidth () * keyX), (int) (myImage1.GetHeight () * keyY), 24, 0 );
COLORREF pixel;
Int maxY = myImage2.GetHeight ();
Int maxX = myImage2.GetWidth ();
// Int maxX1 = myImage1.GetWidth ();
// Int maxY1 = myImage1.GetHeight ();
// CString str;
// Str. Format (TEXT ("% d"), maxX );
// MessageBox (str );
// Str. Format (TEXT ("% d"), maxX1 );
// MessageBox (str );
// Str. Format (TEXT ("% d"), maxY );
// MessageBox (str );
// Str. Format (TEXT ("% d"), maxY1 );
// MessageBox (str );
Int r, g, B, avg;
Double c;
Byte * pRealData;
Byte * pRealData2;
PRealData = (byte *) myImage1.GetBits ();
PRealData2 = (byte *) myImage2.GetBits ();
Int pit = myImage1.GetPitch ();
Int pit2 = myImage2.GetPitch ();
// Note that the values of pit and pit2 are not the same, so if you use a value, different results will appear.
// CString str;
// Str. Format (TEXT ("% d"), pit2 );
// MessageBox (str );
// Str. Format (TEXT ("% d"), pit2 );
// MessageBox (str );
Int bitCount = myImage1.GetBPP ()/8;
Int bitCount2 = myImage2.GetBPP ()/8;
// CString str;
// Str. Format (TEXT ("% d"), bitCount );
// MessageBox (str );
// Str. Format (TEXT ("% d"), bitCount2 );
// MessageBox (str );
Int newValue;
Int tempR, tempG, tempB;
Int newX, newY;
For (int y = 0; y <maxY; y ++ ){
For (int x = 0; x <maxX; x ++ ){
NewX = (int) (x/keyX );
NewY = (int) (y/keyY );
TempR = (int) (* (pRealData + pit * newY + newX * bitCount ));
If (bitCount = 1)
{
TempG = tempR;
TempB = tempR;
}
Else
{
TempG = (int) (* (pRealData + pit * newY + newX * bitCount + 1 ));
TempB = (int) (* (pRealData + pit * newY + newX * bitCount + 2 ));
}
* (PRealData2 + pit2 * y + x * bitCount2) = tempR;
* (PRealData2 + pit2 * y + x * bitCount2 + 1) = tempG;
* (PRealData2 + pit2 * y + x * bitCount2 + 2) = tempB;
}
}
Invalidate ();
Return false;
}

Author; superdont

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.