Skip to main content

Look-up tables

Every tone operation of an 8-bit picture, gamma, levels, contrast curves, inversion, posterizing, is a function of one pixel value, and a pixel value has only 256 possibilities. So the fast way to apply one is to compute the answer for each of the 256 levels once, put them in a table, and replace every pixel by the table entry of its value. That is a look-up table, and apply_lut applies one at the speed of reading memory, however slow the function was.

import os

import libstb as stb

os.makedirs("out", exist_ok=True)
camera = stb.Image.open("camera.png")

# A look-up table has one entry for every level, 0 to 255: the new value of each pixel that has that level.
tables = {
"invert": bytes(255 - v for v in range(256)),
"posterize": bytes(v // 64 * 85 for v in range(256)), # four levels: 0, 85, 170, 255
"solarize": bytes(v if v < 128 else 255 - v for v in range(256)), # bright values are folded back
"smooth": bytes(v * v * (3 * 255 - 2 * v) // (255 * 255) for v in range(256)), # an S curve: more contrast in the middle
}
for name, table in tables.items():
stb.apply_lut(camera, table).write(f"out/{name}.png")

# The same picture through the table of a built-in tone function: gamma() is a table.
by_function = stb.gamma(camera, 1.8)
by_table = stb.apply_lut(camera, stb.gamma_lut(1.8))
print("gamma(1.8) is apply_lut of gamma_lut(1.8):", "yes" if by_function.tobytes() == by_table.tobytes() else "no")

# Applying a table is the same work for any picture of the same depth, however slow the table was to make.
print("invert twice gives the picture back:", "yes" if stb.apply_lut(stb.apply_lut(camera, tables["invert"]), tables["invert"]).tobytes() == camera.tobytes() else "no")
Output
gamma(1.8) is apply_lut of gamma_lut(1.8): yes
invert twice gives the picture back: yes
invert
invert
posterize, 4 levels
posterize, 4 levels
solarize
solarize
smooth S curve
smooth S curve

The table​

A table is 256 bytes: a bytes object, a bytearray or a uint8 array of shape (256,). Entry v is the new value of every pixel that has the value v.

With one table, every channel of the picture goes through it, alpha too: apply_lut is plain arithmetic on the numbers and nothing in it knows what a channel is. With one table for each channel (as many tables as channels, side by side) each channel has its own, which is how you change only red. For a 16-bit picture the table has 65 536 entries.

Ready-made tables​

gamma_lut(g) and levels_lut(in_black, in_white, g, out_black, out_white) are the tables of gamma and levels, with the same arguments, and the example shows that the table gives the very same picture as the function. Making them yourself costs the same as the 256 evaluations, so for a picture or two there is no gain; the table pays when you apply it to many pictures, to the frames of a video, or when your function is slow.

gamma_lut and levels_lut return NumPy arrays, so they need pip install "libstb[numpy]"; a table you make from bytes needs nothing.

API at a glance​

Apply a tablestb.apply_lut(img, table), img.apply_lut(table)
A table by handbytes(f(v) for v in range(256))
The table of gammastb.gamma_lut(g)
The table of levelsstb.levels_lut(in_black, in_white, g, out_black, out_white)