Thomas
Thomas

Reputation: 1

How to set an negative value to the resolution of a BMP image in HEX

There has been people asking these questions regarding BMP files, but no one really explain how you can put an negative height value directly into a Hex editor of an BMP-image. My image is 640x480, and the problem is (as most know) that BMP images are scanned from left bottom side through each line to the top. This makes my images mirror wait As they are ment to be scanned top down (and source code does that). The positive value for the height is 80 02 in hex (640). What would the negative value be in HEX for minus 640?

Upvotes: 0

Views: 1150

Answers (1)

Harald K
Harald K

Reputation: 27094

What you need, is to invert the height only (not the width, there seems to be some confusion from reading the comments). This should make a BMP decoder read the image top/down, rather than the normal bottom/up.

Note that BMP 16 and 32 bit values are always stored using little endian (Intel) byte order.

The 40 bytes BMP header you use (as seen by the 0x28 00 00 00 at offset 14, and also known as "Windows Bitmap Info Header"), uses 32 bit values for width/height.

So, as a 32 bit value in LE byte order, -480 is written as 0x20 FE FF FF (at offset 22).


As a proof of concept, here's g/rgb24.bmp from BMP Suite, before and after conversion (height 64/0x40 00 00 00 and -64/0xC0 FF FF FF respectively, at offset 22):

original rgb24.bmp flipped rgb24.bmp

These both open fine using Preview on OS X (and obviously SO/imgur, as they conveniently converted the files to PNG).

Here's a download link for the flipped file, as BMP.

Upvotes: 1

Related Questions