What is ScanLine? How can I use ScanLine in my C++ app built using the VCL?
The ScanLine method provides indexed access to each line of pixels of bitmaps, in VCL apps, i.e Image1->Picture-<Bitmap
. ScanLine is used only with DIBs (Device Independent Bitmaps) for image editing tools that do low-level pixel work.
How to use Scanline in bitmaps in a C++ app?
Scanline provides indexed access to each line of pixels. It’s used only with DIBs (Device Independent Bitmaps) for image editing tools that do low-level pixel work. Here’s a simple example of how to use Scanline
1 2 3 4 |
TBitmap bmp; TRGBTriple *rgb = reinterpret_cast<TRGBTriple *> (bmp->ScanLine[y] ); |
where y is an integer that shows the line of bitmap scanned ( maximum is the height-1 of bitmap)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <vcl.h> #include <memory> // for the auto_ptr #pragma hdrstop #include "ScanLine_VCL_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { std::auto_ptr<Graphics::TBitmap> Bitmap(new Graphics::TBitmap); TRGBTriple *ptr; TPixelFormat pixForm; try { Bitmap->LoadFromFile("D:/test.bmp"); // Load a bitmap pixForm = Bitmap->PixelFormat; Bitmap->PixelFormat = pf24bit; for (int y = 0; y < Bitmap->Height; y++) { ptr = reinterpret_cast<TRGBTriple *>(Bitmap->ScanLine[y]); for (int x = 0; x < Bitmap->Width; x++) { // ... Operate on each lines of bitmap } } } catch (...) { ShowMessage("Could not load or alter bitmap"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { } |
How to use Scanline to scale bitmaps in C++ Builder VCL C++ app?
This official C++ Builder Scanline example shows how to create and draw a scaled bitmap directly by using ScanLine method. It loads a bitmap from a file and then copies it to another bitmap twice its size. Then the two bitmaps are displayed on the form canvas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <vcl.h> #include <memory> // for the auto_ptr #pragma hdrstop #include "ScanLine_VCL_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { std::auto_ptr<Graphics::TBitmap> Bitmap(new Graphics::TBitmap); std::auto_ptr<Graphics::TBitmap> BigBitmap(new Graphics::TBitmap); TRGBTriple *ptr, *bigPtr; // Use a (byte *) for pf8bit color. TPixelFormat pixForm, bigpixForm; try { Bitmap->LoadFromFile("D:/test.bmp"); pixForm = Bitmap->PixelFormat; bigpixForm = BigBitmap->PixelFormat; Bitmap->PixelFormat = pf24bit; BigBitmap->PixelFormat = pf24bit; BigBitmap->Height = Bitmap->Height * 2; BigBitmap->Width = Bitmap->Width * 2; for (int y = 0; y < Bitmap->Height; y++) { ptr = reinterpret_cast<TRGBTriple *>(Bitmap->ScanLine[y]); for (int x = 0; x < Bitmap->Width; x++) { int bx = x * 2; int by = y * 2; bigPtr = reinterpret_cast<TRGBTriple *>(BigBitmap->ScanLine[by]); bigPtr[bx] = ptr[x]; bigPtr[bx + 1] = ptr[x]; bigPtr = reinterpret_cast<TRGBTriple *>(BigBitmap->ScanLine[by + 1]); bigPtr[bx] = ptr[x]; bigPtr[bx + 1] = ptr[x]; } } Canvas->Draw(0, 0, Bitmap.get()); Canvas->Draw(200, 200, BigBitmap.get()); } catch (...) { ShowMessage("Could not load or alter bitmap"); } } |
How to use Scanline in a C++ Builder FireMonkey C++ App?
In FireMonkey (FMX) application we can use TBitmapData property to read and write ARGB values of a bitmap, For a FMX application on bitmaps please check this example below:
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.