Post · 5 min read· pp. 33–38

16bpp/15bpp to 32bpp pixel conversions — different methods

Contents

Introduction

Basically this kind of conversion needs following steps:

  • extract components R, G and B (using bitwise and)
  • extend words from 6 or 5 bits to 8 bits (shift left)
  • place components at desired places in a 32-bit word (shift, bitwise or)
text
R = (pixel16 and 0x001f) shl 3
G = (pixel16 and 0x07e0) shr 5
B = (pixel16 and 0xf800) shr 11

pixel32 = R or (G shl 8) or (B shl 16)

Since there aren't many pixels (32 or 64 thousand) lookup tables can be used. First approach is to use one big table indexed by pixels treated as natural numbers: this table has size 65536 * 4 bytes = 262144 bytes. Just one memory access is needed to get 32bpp pixel, however the table size is big, and even if it fits in a L2 cache, then the memory latency kill performance.

text
pixel32 = LUT[pixel16]

Another approach needs two tables indexed by the lower and the higher byte of a pixel, the final pixel is result of bitwise or. These tables has size 2 * 256 * 4 bytes = 2048 bytes — perfectly fit in a L1 cache.

text
pixel32 = LUT_hi[pixel16 shr 8] or LUT_lo[pixel16 and 0xff]

Sample program

Sample program is available at github, and includes following procedures:

  • naive — is a straightforward implementation of the algorithm described in the beginning;
  • lookup16 — uses single, large lookup table;
  • lookup8 — use two, small lookup tables; it converts one pixel per iteration;
  • lookup8(2) — improved lookup8 — converts two pixels per iteration, requires single fetch of 4 bytes;
  • MMX — naive algorithm using MMX instructions;
  • SSE2 — naive algorithm using SSE2 instructions; converts 8 pixels per iteration;
  • SSE2(2) — unrolled SSE2 version that converts 16 pixels per iteration;

Test results

Core 2

Program was compiled with following flags:

text
gcc -O3 pixconv16bpp-32bpp.c -o test

gcc -O3 -DNONTEMPORAL pixconv16bpp-32bpp.c -o testnta

Here are timing from my Core 2 Duo E8200 @ 2.6GHz. Each procedure was called 10 times, results are average.

Image 320x200

proceduretime [us]speedup
naive16625100%==========
lookup813584120%============
lookup162443865%======
lookup8(2)6175270%===========================
MMX7862210%=====================
SSE24103405%========================================
SSE2(2)4604360%====================================

Image 640x480

proceduretime [us]speedup
naive49371100%==========
lookup840177120%============
lookup167305165%======
lookup8(2)18574265%==========================
MMX23483210%==========================
SSE212703390%=======================================
SSE2(2)13716360%====================================

Image 800x600

proceduretime [us]speedup
naive77634100%==========
lookup862893120%============
lookup1611515665%======
lookup8(2)28830270%===========================
MMX36452210%=====================
SSE219217400%========================================
SSE2(2)21696360%====================================

Image 1024x768

proceduretime [us]speedup
naive130867100%==========
lookup8106543120%============
lookup1620542160%======
lookup8(2)48503270%===========================
MMX62737210%=====================
SSE237881345%==================================
SSE2(2)44162295%=============================

Comparison

speedup
procedure320x200640x480800x6001024x768
naive100%100%100%100%
lookup8120%120%120%120%
lookup1665%65%65%60%
lookup8(2)270%265%270%270%
MMX210%210%210%210%
SSE2405%390%400%345%
SSE2(2)360%360%360%295%

As we see the worst results have lookup16. Single lookup8 is a bit faster than naive implementation, lookup8(2) that reads 2 pixels in one iteration is almost 2 times faster. I think memory latencies can help to understand these results: L1 latency is small, 3 cycles or less, L2 cache — around 10-15 cycles.

SIMD versions are naturally much faster, however SSE2(2) is a bit slower than basic SSE2.

Core i5

CPU: Core i5 CPU M 540 @ 2.53GHz

The test program was run 100 times, the minimum measurement were considered.

Image 320x200

proceduretime [us]speedup
naive11,1361.00==========
lookup88,5841.30=============
lookup1610,4331.06==========
lookup8(2)3,3603.31=================================
MMX3,7502.97================================
SSE22,2624.92=================================================
SSE2(2)2,1925.08==================================================

Image 640x480

proceduretime [us]speedup
naive53,7481.00==========
lookup841,3331.30=============
lookup1652,9481.02==========
lookup8(2)15,8533.39=================================
MMX18,1142.97==============================
SSE211,8134.55=============================================
SSE2(2)11,4114.71===============================================

Image 800x600

proceduretime [us]speedup
naive82,9741.00==========
lookup866,4511.25============
lookup1698,9510.83========
lookup8(2)24,8343.34=================================
MMX31,5252.63==========================
SSE225,6253.24================================
SSE2(2)23,8933.47===================================

Image 1024x768

proceduretime [us]speedup
naive141,7841.00==========
lookup8112,4391.26=============
lookup16191,7980.73=======
lookup8(2)45,3623.13===============================
MMX78,0911.82==================
SSE275,2301.88===================
SSE2(2)75,4031.88===================

Comparison

speedup
procedure320x200640x480800x6001024x768
naive1.001.001.001.00
lookup81.301.301.251.26
lookup161.061.020.830.73
lookup8(2)3.313.393.343.13
MMX2.972.972.631.82
SSE24.924.553.241.88
SSE2(2)5.084.713.471.88
  • The unrolled SSE implementation beats others in most cases.
  • The improved lookup8(2) is surprisingly good. It keeps the speedup at the same level regardless of the image size.