Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orientation of a BMP file: upright or upside-down?

I'm working on a program to resize a BMP file and store it in a new file. I noticed that some BMPs are stored totally upside-down while others are just stored upright. So I made two solutions to handle both cases. My question is How to know if the BMP is stored upright or upside-down beforehand? So that I can decide which solution to use instead of trying both solutions with every BMP!

like image 770
Moaz Naseem Avatar asked Apr 05 '18 08:04

Moaz Naseem


People also ask

Why are bitmaps stored upside down?

They are upside-down for compatibility with OS/2 Presentation Manager. It can actually be either, top line first bitmaps have a negative height. @HansPassant, the ability to have a negative height is a feature added well after the bitmap format was popularized.

How is a BMP file structured?

The order of the pixels in BMP file is as follows: from left to right, from bottom to top (first pixel is from lower left corner of the picture). In the first approach, the picture can be displayed upside down, just to test the reading data from memory.

Is BMP a file format?

The BMP format is an uncompressed raster file designed to display high-quality images on Windows and store printable photos.

How are BMP files stored?

BMPs use a very simple form of compression called Run Length Encoded (RLE). Instead of storing a value for each pixel RLE stores a number, N, followed by an index. This means that the next N pixels are of the color for this index.


1 Answers

The answer lies within the BITMAPINFOHEADER-structure.

From: http://msdn.microsoft.com/en-us/library/windows/desktop/dd318229(v=vs.85).aspx

For uncompressed RGB bitmaps, if biHeight is positive, the bitmap is a bottom-up DIB with the origin at the lower left corner. If biHeight is negative, the bitmap is a top-down DIB with the origin at the upper left corner.

like image 80
vmt Avatar answered Nov 03 '22 01:11

vmt