Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: Use top-down bitmaps


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 11 - Last Post: Sep 5, 2017 12:04 PM Last Post By: Mike Versteeg Threads: [ Previous | Next ]
Mike Versteeg

Posts: 116
Registered: 9/16/07
Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 8:30 AM
Is there a way to convert all bitmaps in my app to top-down? I know this is possible in the underlying DIB, but cannot find it documented in VCL so not even sure if it is supported. The problem is the world around my app offers and requires top-down, so I am converting all incoming and outgoing bitmaps, wasting resources.
Remy Lebeau (Te...


Posts: 8,637
Registered: 12/23/01
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 10:25 AM   in response to: Mike Versteeg in response to: Mike Versteeg
Mike Versteeg wrote:

Is there a way to convert all bitmaps in my app to top-down?

Sure, but you have to do it manually.

I know this is possible in the underlying DIB, but cannot find it
documented in VCL so not even sure if it is supported.

The VCL doesn't care about top-down or bottom-up, and as such does not
offer any functionality to work with image orientation. That is
generally handled at the OS layer, unless you are accessing the image
scanlines directly.

The problem is the world around my app offers and requires top-down

Bitmaps in Windows are usually bottom-up, unless you are dealing with
YUV images. Are your bitmaps YUV?

--
Remy Lebeau (TeamB)
Mike Versteeg

Posts: 116
Registered: 9/16/07
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 11:05 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
My data, which I memcpy into the bitmap and is top-down, is RGB. I realise I can work with that as is, but when I display the bitmap it'll be upside-down. I wish to avoid having to flip them.
Remy Lebeau (Te...


Posts: 8,637
Registered: 12/23/01
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 12:12 PM   in response to: Mike Versteeg in response to: Mike Versteeg
Mike Versteeg wrote:

My data, which I memcpy into the bitmap and is top-down, is RGB.

RGB bitmaps are typically bottom-up, not top-down. Why is your data
reversed?

I realise I can work with that as is, but when I display the bitmap
it'll be upside-down.

How are you displaying it?

--
Remy Lebeau (TeamB)
Mike Versteeg

Posts: 116
Registered: 9/16/07
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 12:32 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
It's origin lies in video, where top-down is the norm (YUV).
I'm displaying the bitmaps by copying them to a (paintbox) canvas.
Remy Lebeau (Te...


Posts: 8,637
Registered: 12/23/01
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 1:55 PM   in response to: Mike Versteeg in response to: Mike Versteeg
Mike Versteeg wrote:

I'm displaying the bitmaps by copying them to a (paintbox) canvas.

But HOW exactly? Because if you have a TBitmap containing the pixels,
it should render to a TCanvas correctly whether it is top-down or
bottom-up, as long as it KNOWS the correct orientation. It sounds like
maybe you are creating a bottom-up bitmap containing top-down pixels,
that would explain why it renders upside down.

--
Remy Lebeau (TeamB)
Mike Versteeg

Posts: 116
Registered: 9/16/07
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 25, 2017 12:21 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
I draw using whatever drawing method works best, usually Canvas->Draw. What does it matter?
But it's great VCL drawing looks at orientation, exactly what I want! However I still don't know how to flag a bitmap orientation, am I missing the obvious?
Remy Lebeau (Te...


Posts: 8,637
Registered: 12/23/01
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2017 10:43 AM   in response to: Mike Versteeg in response to: Mike Versteeg
Mike Versteeg wrote:

I draw using whatever drawing method works best, usually
Canvas->Draw. What does it matter? But it's great VCL drawing looks
at orientation, exactly what I want! However I still don't know how
to flag a bitmap orientation, am I missing the obvious?

You missed my point. This is not a VCL issue, it is a Win32 issue. A
bitmap knows its own orientation, it is specified in the bitmap's
header:

Top-Down vs. Bottom-Up DIBs
https://msdn.microsoft.com/en-us/library/windows/desktop/dd407212.aspx

Drawing functions at the OS layer are aware of this orientation so they
can process the pixels correctly.

You say you are memcpy'ing pixel data, which means you are accessing
the bitmap's raw scanline data, so your pixel data must match the
bitmap's orientation. If you create a bottom-up bitmap and fill it
with top-down data, the bitmap will render upside down.

Bitmaps are bottom-up by default. If you have top-down pixel data, you
need to create a top-down bitmap before you then fill it with top-down
pixel data. Otherwise, you have to reverse your top-down pixel data
when filling a bottom-up bitmap.

To create a top-down TBitmap, you have to first create a top-down
HBITMAP with the Win32 API, then you can assign it to the
TBitmap::Handle property.

On the other hand, you don't need to worry about orientation at all if
you copy the pixel data scanline-by-scanline instead of all at one
time. The TBitmap::Scanline property takes orientation into account.
Scanline[0] is always the first row of the bitmap, whether it is the
bottom row in a bottom-up bitmap or the top row in a top-down bitmap.
And Scanline[Height-1] is always the last row of the bitmap, whether it
is the top row in a bottom-up bitmap or the bottom row in a top-down
bitmap.

--
Remy Lebeau (TeamB)
Mike Versteeg

Posts: 116
Registered: 9/16/07
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2017 11:32 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Line by line is many microseconds slower than a straight copy, which is a big deal for me.
But thank you for explaining how to make a top-down VCL bitmap!

Edited by: Mike Versteeg on Aug 29, 2017 12:41 AM
Mike Versteeg

Posts: 116
Registered: 9/16/07
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 3, 2017 12:37 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Would the same trick work to create a VCL bitmap that uses the YCbCr colour space instead of RGB?
Remy Lebeau (Te...


Posts: 8,637
Registered: 12/23/01
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2017 11:56 AM   in response to: Mike Versteeg in response to: Mike Versteeg
Mike Versteeg wrote:

Would the same trick work to create a VCL bitmap that uses the
YCbCr colour space instead of RGB?

GDI bitmaps only support RGB(A). You can't create a YCbCr bitmap. WIC
(Windows Imaging Component) supports YCbCr but only for JPGs (and the
VCL has a TWICImage wrapper for the WIC API). But you can define
custom formats by creating and registering a custom pixel format
converter with WIC.

https://msdn.microsoft.com/en-us/library/windows/desktop/ee719797.aspx

--
Remy Lebeau (TeamB)
Mike Versteeg

Posts: 116
Registered: 9/16/07
Re: Use top-down bitmaps  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2017 12:04 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks. Speed is essential so WIC, which AFAIK isn't hardware accelerated, is not an option.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02