Post

Image Array Layout & Memory Structure

Image Array Layout & Memory Structure

๐Ÿง  Why Image Array Structure Matters

In computer vision, bugs often come not from algorithms,
but from wrong assumptions about image memory layout.

Understanding array structure is essential for:

  • Correct pixel access
  • Fast processing
  • Writing your own image loader
  • Debugging strange artifacts

๐Ÿงฑ Image as Raw Memory

An image in memory is a 1D byte array, interpreted as 2D or 3D.

Conceptually:

1
memory[]  โ†’  rows  โ†’  pixels  โ†’  channels

But rows are not always tightly packed.


๐Ÿ“ Basic Image Dimensions

SymbolMeaning
WWidth (pixels)
HHeight (pixels)
CChannels
BBytes per channel
RowBytesActual bytes per row
StrideDistance between row starts

๐Ÿ“ Stride (Pitch)

Stride = number of bytes between the start of two consecutive rows.

1
row y starts at: base + y * stride

Stride โ‰ฅ (W ร— C ร— B)

Why larger?

  • Alignment
  • Padding
  • SIMD / GPU constraints

๐Ÿงฉ Padding

Padding = unused bytes at the end of each row.

Purpose:

  • Memory alignment
  • Faster access
  • Hardware constraints

Padding bytes do not represent pixels.


๐ŸŸฆ Bitmap (BMP) Row Alignment Rule

The Rule

Each row in a BMP file must be aligned to 4-byte boundaries.

Meaning:

1
RowBytes = ceil((W ร— bitsPerPixel) / 32) ร— 4

Example: 24-bit BMP

  • Width = 3 pixels
  • 24 bits per pixel = 3 bytes

Raw row size:

1
3 ร— 3 = 9 bytes

BMP requires multiple of 4:

1
2
Padding = 3 bytes
RowBytes = 12 bytes

Visual Layout

1
[B G R][B G R][B G R][P][P][P]

P = padding byte


๐Ÿ”„ Bottom-Up Storage (BMP)

Most BMP files store rows bottom to top.

Memory order:

1
2
3
Last row
...
First row

If ignored:

  • Image appears vertically flipped

๐ŸŽจ Channel Interleaving

Interleaved (HWC)

1
[R G B][R G B][R G B]

Planar (CHW)

1
2
3
RRR...
GGG...
BBB...

BMP / PNG / JPEG files:

  • Usually interleaved

Deep learning frameworks:

  • Often planar

๐Ÿ“ฆ Pixel Size Calculation

Bytes per pixel:

1
PixelBytes = C ร— B

Total memory (excluding padding):

1
H ร— W ร— C ร— B

Actual memory:

1
H ร— stride

โš ๏ธ Common Bugs

โŒ Assuming stride == width ร— channels

  • Fails on BMP
  • Fails on aligned buffers

โŒ Ignoring bottom-up layout

  • Flipped images

โŒ Forgetting padding when copying

  • Shifted or torn images

โŒ Mixing RGB and BGR

  • Color swapped output

๐Ÿงช Safe Pixel Access Formula

To read pixel (x, y):

1
address = base + y * stride + x * PixelBytes

Never assume contiguous rows.


๐Ÿง  Practical CV Insight

Most libraries expose:

  • width
  • height
  • channels
  • stride (or step)

Always trust stride, not width.


๐Ÿ“Š Summary Table

ConceptWhy It Exists
StrideRow alignment
PaddingHardware efficiency
Bottom-upLegacy BMP
InterleavingSimplicity
PlanarVectorization

๐ŸŽฏ Takeaway

Images are memory structures first, pictures second.

If you understand:

  • Stride
  • Padding
  • Alignment
  • Layout

You can:

  • Write loaders
  • Avoid silent bugs
  • Optimize performance

This knowledge separates CV users from CV engineers ๐Ÿš€

This post is licensed under CC BY 4.0 by the author.