Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
Delphi

Converting to grayscale with TBitmap.ScanLine property

the future of starts demands massive productivity

Author: Pawe Gowacki

In my previous post “Boian’s TBitmap Visualizer and converting to grayscale” I have described an algorithm for converting arbitrary TBitmap instances to gray using a selected formula. The problem is with the performance of the code that actually changes the color of each pixel. Accessing color information is done via TBitmap.Pixels property, which is a two-dimensional array of TColor values. Since Delphi 3 there is a more efficient way of manipulating color information – using TBitmap.Scanline property. It is of pointer type and provides direct access to memory where color information is stored. The drawback is the fact that you have to be aware of TBitmap instance PixelFormat property to understand the layout in memory of individual bytes that make up the complete color information.

It is not a new page, but “Manipulating Pixels With Delphi’s ScanLine Property” tech note from efg’s Computer Lab is probably the ultimate reference to the topic, full of Danny Thorpe’s comments explaining the inner workings of TBitmap class implementation.

Accessing ScanLine property can be expensive in terms of performance, so it is the best to calculate the offset in bytes for every row in a bitmap just once and use it for performing pointer arithmetics as demonstrated by ScanlineTiming test application that opens and compiles beautifully in Delphi 2010 on Windows 7.

What interests me more is to compare performance of changing bitmaps using “Pixels” and “ScanLine” properties. Delphi 2010 introduced into the VCL the new TStopwatch type in “Diagnostics” unit that should be the most convenient way of measuring time.

Here is my rewritten ToGray procedure that is now taking one additional parameter “ba: TBitmapAccess” that controls how bitmaps are accessed: via ScanLine or via Pixels properties.

On average my test application shows that using “ScanLine” property for bitmap access is approximately 30 times faster then using “Pixels”. That’s really more then I was expecting…

In the code above I have only implemented support for “pf24bit” pixel format.

In order to optimize code, I’m precalculating offset between scanlines to avoid too many calls to “ScanLine” as it may result in degraded performance. The other benefit of this approach is taking automatically into account byte padding in memory.

Using Pixels property (1556ms)

Using Pixels property (1556ms)

Using ScanLine property (17ms)

Using ScanLine property (17ms)

The source code for this test application can be downloaded from the Embarcadero Developer Network.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES