How to Make an Inverse Effect of the Image using Embarcadero C++ Builder XE5?
How to make an inverse effect of the image using Embarcadero C++ Builder XE5?¶
Introduction
If you want to make an inverse effect of the image, you can try this algorithm. To make a similar a negative (of a photo). That would be fun.
Equipment
Operation System: Microsoft Windows 7 (64 bit)
Development Utility: Embarcadero C++ Builder XE5
Usage
//--------------------------------------------------------------------------- #include#pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; // Create three points of TBitmap object Graphics::TBitmap *TheBitmap, *TempBitmap, *OriginBitmap; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Exit1Click(TObject *Sender) { // Exit the program Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::OpenFile1Click(TObject *Sender) { // If you got a file of the picture, you are able to load it. if ( OpenPictureDialog1->Execute() ) { // Disable automatic resize. Image1->AutoSize=false; // Enable an automatic stretch. Image1->Stretch=true; // Loads a picture file. Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); // Set a point to the picture of loading. TheBitmap=Image1->Picture->Bitmap; // Create a new TBitmap object to keep original picture. OriginBitmap = new Graphics::TBitmap(); // To keep original picture. OriginBitmap->Assign(TheBitmap); // Set up TImage object to keep original picture. Image1->Picture->Bitmap->Assign(TheBitmap); // If you open picture file successful and then set a flag. // The means that you have been opened a file. OpenFile = 1; } // If you have not opened a file, the flag will be zero, and then returns. if (OpenFile == 0) { return; } Byte *ptr, *tptr; // Set up the TImage restore to the original picture. Image1->Picture->Bitmap->Assign(OriginBitmap); // Create a new TBitmap. TempBitmap = new Graphics::TBitmap(); // To get a current bitmap. TempBitmap->Assign(TheBitmap); // To do scan line of the whole picture. for (int y=0; y < TheBitmap->Height; y++) { // Set Y position of the image point. ptr = (Byte*) TheBitmap->ScanLine[y]; // Set Y position of the image point. tptr = (Byte*) TempBitmap->ScanLine[y]; // To do scan X-axis of the line. for (int x=0; x < TheBitmap->Width; x++) { // Inverse the pixel. ptr[x] = (Byte) 255 - ptr[x]; } // End x } // End y // Release the temporal bitmap. delete TempBitmap; // Refresh and draw the TImage object. Repaint(); // Set result of inverse of the picture. Image1->Picture->Bitmap->Assign(TheBitmap); } //---------------------------------------------------------------------------
Exception
If you encounter the error, don’t worry. You just step by step to solve it.
Reference
[1] Gary Bradski and Adrian Kaehler, “Learning OpenCV: Computer Vision with the OpenCV Library,” O’REILLY, September 2008, ISBN:978-0-596-51613-0
Acknowledge
Thank you (Embarcadero C++ Builder XE5, Lenna Sjööblom) very much for this great development utility and beautiful photo.