8-bitButterfly
8-bitButterfly

Reputation: 203

Representation in memory for one pixel of image data

I can't get me mind to understand how a standard TGA 32bit image is arranged in memory after being loaded from file. I do however understand the idea of bytes per pixel, but i don't understand how the color components (RGBA) are layed out in memory for the one pixel. In other words, how the bytes for one pixel are arranged inside the array.

Will someone please give me some insight on how the RGBA information is stored in memory for one pixal of image data, is all the information for one pixel within the bytes per pixel range?

Also i am loading my image with these options:

ifstream reader_;

...

reader_.open(path.c_str(), ios_base::in | ios::binary);

...

reader_.read((char*)(&_image.data[0]), size);

Upvotes: 0

Views: 1557

Answers (3)

Alnitak
Alnitak

Reputation: 339816

If you treat your buffer as being of type uint32t * then each pixel will occupy one element of the buffer.

The precise arrangement of RGBA values within each uint32t will depend on the file format. You might find R in the lowest byte, and A in the highest byte:

uint8t r = (*p >>  0) & 0xff;       // cast as required
uint8t g = (*p >>  8) & 0xff;     
uint8t b = (*p >> 16) & 0xff;    
uint8t a = (*p >> 24) & 0xff;     

Or, you might not ;-). Which order the fields are packed also depends on the "endianess" of the machine running the code - this above would be for little-endian (e.g. Intel) architectures.

Upvotes: 2

Yochai Timmer
Yochai Timmer

Reputation: 49251

Another way, which in my opinion is better than extracting data, is using a cast.
Using operators and bit arithmetic has its cost, making the compiler cast to the right format is free.

struct RGBA
{
   uint8t r;
   uint8t g;
   uint8t b;
   uint8t a;
}

char* myBitmap = GetBitMapPointer();
RGBA* rgbaPointer = reinterpert_cast<RGBA*>(myBitmap);

//use pointer arithmetic from now on....
uint8t myFirstAvalue = rgbaPointer->a;

//second pixel
rgbaPointer++;
uint8t mySecondAvalue = rgbaPointer->a;

Upvotes: 3

Wagner Patriota
Wagner Patriota

Reputation: 5674

In memory everything becomes a "bitmap". It depends on the pixel format you use. In general, RGB or RGBA (being 8 bits/1 byte per channel) is the most common, but you can have other pixel formats, example:

  • bitmaps with less than 32-bit color, example: for 8-bit, where you have only 256 possible colors per pixel.
  • bitmaps with different bits per channel, example 16 bits "per channel", so you have more precision on your color information.
  • formats with different order of channels: formats like ARGB, ABGR, BGR
  • monochromatic (1 bit per pixel)... so in this case, 1 byte can have up to 8 pixels!

You can even "invent" one pixel format if you want...

Upvotes: 3

Related Questions