BBC BASIC
« Top-down BMP files »

Welcome Guest. Please Login or Register.
Sep 24th, 2017, 01:27am


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Top-down BMP files  (Read 175 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 587
xx Top-down BMP files
« Thread started on: Jun 23rd, 2017, 10:13pm »

The Windows bitmap (BMP) file format is a slippery beast, because it is effectively just a Windows DIB (Device Independent Bitmap) - a structure which normally resides in memory - with a small header. The trouble with adopting that definition is that the DIB structure is constantly evolving, with new options being introduced almost with every major update to Windows, so the BMP file format is similarly acquiring new variants. This obviously poses major problems for any application or utility which claims to read BMP files.

In practice, therefore, only a subset of possible DIB formats are generally encountered in BMP files. For example whilst a memory DIB can be either 'bottom up' (with a positive height value) or 'top down' (with a negative height value) BMP files are almost universally 'bottom up'. Indeed it often used to be the case that programs which read BMP files would accept only the 'bottom up' variant - including for example the version of MS Paint supplied with Windows 95.

In fact even to this day the Windows LoadImage API function - one of the main supported ways of loading a BMP file - will not recognise a 'top down' file. You probably will not be surprised, therefore, to learn that BB4W's *DISPLAY command will similarly not recognise such a BMP.

Except that you should be, because it was always my intention that it would! So I was actually quite surprised to find recently that it doesn't, and on investigating I discover that the cause is not so much a fault in BB4W but a Windows API function that does not work how its documentation would suggest. The API concerned is StretchDIBits and the questionable comment is that "StretchDIBits creates a mirror image of a bitmap if the signs of ... the nSrcHeight and nDestHeight parameters differ".

Note particularly the phrase: "if the signs differ". To me this implies that either nSrcHeight or nDestHeight can be negative. After all, if there was a requirement that nSrcHeight always be positive it would be sufficient - and clearer - to say "if the sign of nDestHeight is negative" rather than "if the signs of nSrcHeight and nDestHeight differ". But as far as I can tell nSrcHeight must indeed be positive, otherwise *DISPLAY would work with top-down BMPs.

TL; DR; The bottom line is that BB4W does not accept 'top down' BMPs, even though it was intended to; I do not consider the fault to be entirely mine.

Richard.
« Last Edit: Jun 23rd, 2017, 10:15pm by Richard Russell » User IP Logged

PatrickM
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx Re: Top-down BMP files
« Reply #1 on: Jul 3rd, 2017, 11:15pm »

Quote:
The bottom line is that BB4W does not accept 'top down' BMPs


How do you feel about that?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 587
xx Re: Top-down BMP files
« Reply #2 on: Jul 4th, 2017, 12:08am »

on Jul 3rd, 2017, 11:15pm, PatrickM wrote:
How do you feel about that?

Is that a serious question or are you trolling? Since it's been the same for more than 15 years and nobody has ever commented on it, or presumably even noticed it (even I hadn't noticed until very recently), it's clearly unimportant.

Richard.

User IP Logged

PatrickM
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx Re: Top-down BMP files
« Reply #3 on: Jul 4th, 2017, 01:05am »

Sorry, I just wondered how you were feeling about it. Anyway, as you say, it's clearly unimportant, since nobody noticed for so long.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 587
xx Re: Top-down BMP files
« Reply #4 on: Jul 4th, 2017, 08:18am »

on Jul 4th, 2017, 01:05am, PatrickM wrote:
Anyway, as you say, it's clearly unimportant, since nobody noticed for so long.

As I tried to explain in the original post, what constitutes a 'valid' BMP file is not well defined. Using the definition that a BMP file is simply a Windows DIB with a header, there are a large number of possible variants - with the potential for more as new versions of Windows are released - which is not a good recipe for a 'standardised' file format!

So in practice the great majority of BMP files 'in the wild' use only a small subset of the possible DIB formats, and the 'top-down' layout (with a negative height value) is one of those that you will rarely encounter. As I also said, the 'LoadImage' API itself does not accept a top-down BMP file, which is a pretty good indication that Microsoft themselves don't consider it to be a valid option.

So it's quite clear to me that the failure of BB4W's *DISPLAY command to accept top-down BMP files is of negligible significance. If it were ever a problem one only needs to load the file into a tool such as MS Paint and then re-save it with no changes. It's also not difficult to write BBC BASIC code which will accept such files, for example by calling the StretchDIBits API directly, or by using GDI+ (code for doing that is listed at the Wiki).

I only mentioned it because I felt I should share with the BBC BASIC community the discovery that I had made, especially as it wasn't what I had originally intended when BB4W was designed way back in 2000/2001. I did not expect to get quizzed on my emotional reaction! shocked

Richard.
User IP Logged

michael
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 94
xx Re: Top-down BMP files
« Reply #5 on: Jul 6th, 2017, 05:21am »

I haven't had problems with BMP images. I have yet to see a problem. You must be speaking of very old image format or perhaps one I am unaware of.
User IP Logged

I like reinventing the wheel, but for now I will work on tools for D3D
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls