BMP Implementation in C

2017-10-21, post № 182

C, programming, #bitmap, #file, #file format

C is one cool and important language. CPython and Unix are based on it, the Mars Curiosity rover is run by it and even the GCC C compiler itself is written in C. However, as C is some years old by now, it lacks a lot of higher-level features most modern languages possess, being more down to the silicon, as the cool kids say. Concepts like pointer manipulation, bit fiddling and its string implementation — just to name a few — are at times cumbersome, insecure and error-prone; nevertheless is there a certain appeal to writing in C.

Being only one abstraction level away from Assembly — which itself is only one abstraction level above raw byte code — and having access to file manipulation down to the individual bit, I set out to write a Microsoft Bitmap (.bmp) implementation in pure C. As Microsoft’s standard for this image file format is quite feature-rich, I decided to focus on the bare minimum — a bitmap with 𝟤𝟦-bit color depth (three colors, one byte per), one color plane, no compression, no palette and 𝟥𝟢𝟢 DPI.
My Bitmap implementation supports both reading and writing .bmp files, as well as generating some test images — including a Mandelbrot set fractal renderer, of course. Implementation source code can be downloaded (bmp.c) or seen below.

A Mandelbrot set fractal rendering.

Implementing a file format requires knowing its specification. Although it is not the best article I have ever seen, this Wikipedia article gave me some insights. The missing pieces were reverse engineered using Adobe Photoshop CC and the HxD hex editor.
The following is a snippet of the implementation’s savebmp function (full source code listed below). It illustrates the Bitmap file’s byte layout only showing the file header, omitting a lengthy data part concatenated to the header. S, K, W, H and B are all byte arrays of length four (little-endian format) which contain the file’s total size, the bitmap data offset (which is constant, since the header is always exactly 𝟧𝟦 bytes large), the image’s dimensions (horizontal and vertical) and the bitmap data’s section’s size, respectively.

/*  bitmap file header  */
0x42, 0x4D,             // BM
S[0], S[1], S[2], S[3], // file size
0x00, 0x00, 0x00, 0x00, // unused
K[0], K[1], K[2], K[3], // bitmap data offset
/*      DIB header      */
0x28, 0x00, 0x00, 0x00, // DIB size
W[0], W[1], W[2], W[3], // pixel width
H[0], H[1], H[2], H[3], // pixel height
0x01, 0x00,             // one color plane
0x18, 0x00,             // 24 bit color depth
0x00, 0x00, 0x00, 0x00, // no compression
B[0], B[1], B[2], B[3], // bitmap data size
0x23, 0x2E, 0x00, 0x00, // 300 DPI (horizontal)
0x23, 0x2E, 0x00, 0x00, // 300 DPI (vertical)
0x00, 0x00, 0x00, 0x00, // no palette
0x00, 0x00, 0x00, 0x00  // color importance
/*  data bytes follow   */

Key bytes to note are the first two identifying the file type (the ASCII-encoded letters BM) and the DPI bytes, 0x23, 0x2E, which indicate 0x00002E23 = 11811 pixels per meter in both the horizontal and vertical direction. Converting from pixels per meter to dots per inch results in 11811 / (1 meter / 1 inch) = 11811 * 127 / 5000 = 300 DPI (roughly).
Most values are represented using four bytes in little-endian format. Translating an 𝟥𝟤-bit integer into four little-endian formatted bytes can be achieved as follows.

/* unsigned 32-bit integer */
unsigned int n = 0b10100100010000100000100000010000;
/*                 < m sig><sm sig><sl sig>< l sig> */

/* byte (unsigned char) array of size four */
unsigned char N[4] = {
    (n & 0xff000000) >>  0, // most significant byte
    (n & 0x00ff0000) >>  8, // second most significant byte
    (n & 0x0000ff00) >> 16, // second least significant byte
    (n & 0x000000ff) >> 24  // least significant byte

Other than rendering a fractal, I also implemented three nested loops which output an image containing every possible color exactly once ((2**8)**3 = 16777216 pixels in total).

All sixteen million colors in one image.

An image’s data type is implemented as a struct image which contains three variables — width and height, two integers specifying the image’s dimensions, and *px, a pointer to an one-dimensional integer array of size width*height which holds the entire image data.
Defined functions are listed ahead.

Images shown in this post were converted to .png files as WordPress does not allow .bmp file uploads; the raw pixel data should, however, be identical. [1]

Source code: bmp-implementation-in-c_bmp.c


2017-10-07, post № 181

BASIC, PIL, programming, Python, TI-84 Plus, #bitmap, #image, #TI, #TI-BASIC

Texas Instrument’s TI-84 Plus is a graphing calculator with a variety of features. It has built-in support for both fractions and complex numbers, can differentiate and integrate given functions and supports programming capabilities. The latter allows to directly manipulate the calculator’s monochrome display’s 𝟧𝟫𝟪𝟧 pixels (the screen has dimensions 𝟫𝟧 ⨉ 𝟨𝟥). TImg is a Python program (source code is listed below and can also be downloaded) which takes in an image and outputs TI-BASIC source code which, when run on the graphing calculator, will produce the given image — in potentially lower quality.

TI-84 Plus’ screen dimensions (bitmap).

PIL — the Python Imaging Library — is used to read in the image and further for processing. The supplied image may be rotated and resized to better fit the TI-84’s screen and any color or even grayscale information is reduced to an actual bitmap — every pixel only has two distinct values.
Direct pixel manipulation on the TI-84 is done via the Graph screen. To get remove any pixels the system draws on its own, the first three commands are ClrDraw, GridOff and AxesOff which should result in a completely blank screen — assuming that no functions are currently being drawn. All subsequent commands are in charge of drawing the previously computed bitmap. To turn certain pixels on, Pxl-On(Y,X is used where 𝑌 and 𝑋 are the pixel’s coordinates.

A fractal (bitmap).

Since the TI-84 Plus only has 𝟤𝟦 kilobytes of available RAM, the source code for a program which would turn on every single pixel individually does not fit. Luckily, though, a program which only individually turns on half of the screen’s pixels fits. To ensure that TImg’s output fits on the hardware it is designed to be used with, an image’s bitmap is inverted when the required code would otherwise exceed 𝟥𝟧𝟢𝟢 lines — a value slightly above the required code to draw half of the pixels.

A fractal (input image).
Source code: timg.py


2017-09-23, post № 180

PIL, programming, Python, #color model, #hsl, #hsv, #image filter, #images, #nature, #photography, #rainbow

To digitally represent colors, one most often uses the RGB color system. By combining three fundamental light colors in certain ways, one can define a variety of different wavelengths of light. The human eye has three distinct photoreceptors for the aforementioned three colors, nearly all screens use pixels consisting of three parts in those colors and most image formats store the image data in the RGB color system.

Honey bee (original)

However, there are other color systems than RGB with other strengths. Cycling through the colors of the rainbow, for example, is a lot easier using the HSL (or HSV) color model, as it is simply controlled by the hue.

Fruit (original)

Rainbowify uses the HSL color model to rainbowify a given image. To do so, the image is first converted into a grayscale image (averaging all three color channels). A pixel’s brightness is then interpreted as its hue with its saturation and lightness set to the maximum. As a final touch, the hue gets offset by a pixel-position dependent amount to create the overall appearance of a rainbow.
Source code is listed below and can also be downloaded.

Sunflower (original)
Thistle (original)
Source code: rainbowify.py
Extra assets: rainbowify-120.jpg, rainbowify-120_rainbowified.jpg, rainbowify-121.jpg, rainbowify-121_rainbowified.jpg, rainbowify-122.jpg, rainbowify-122_rainbowified.jpg, rainbowify-125.jpg, rainbowify-125_rainbowified.jpg, rainbowify-126.jpg, rainbowify-126_rainbowified.jpg, rainbowify-127.jpg, rainbowify-127_rainbowified.jpg, rainbowify-128.jpg, rainbowify-128_rainbowified.jpg, rainbowify-129.jpg, rainbowify-129_rainbowified.jpg, rainbowify-130.jpg, rainbowify-130_rainbowified.jpg, rainbowify-131.jpg, rainbowify-131_rainbowified.jpg, rainbowify-132.jpg, rainbowify-132_rainbowified.jpg, rainbowify-133.jpg, rainbowify-133_rainbowified.jpg, rainbowify-134.jpg, rainbowify-134_rainbowified.jpg, rainbowify-135.jpg, rainbowify-135_rainbowified.jpg, rainbowify-136.jpg, rainbowify-136_rainbowified.jpg, rainbowify-138.jpg, rainbowify-138_rainbowified.jpg, rainbowify-139.jpg, rainbowify-139_rainbowified.jpg, rainbowify-140.jpg, rainbowify-140_rainbowified.jpg, rainbowify-141.jpg, rainbowify-141_rainbowified.jpg, rainbowify-142.jpg, rainbowify-142_rainbowified.jpg, rainbowify-143.jpg, rainbowify-143_rainbowified.jpg, rainbowify-144.jpg, rainbowify-144_rainbowified.jpg, rainbowify-145.jpg, rainbowify-145_rainbowified.jpg, rainbowify-146.jpg, rainbowify-146_rainbowified.jpg, rainbowify-147.jpg, rainbowify-147_rainbowified.jpg, rainbowify-148.jpg, rainbowify-148_rainbowified.jpg, rainbowify-149.jpg, rainbowify-149_rainbowified.jpg, rainbowify-150.jpg, rainbowify-150_rainbowified.jpg, rainbowify-151.jpg, rainbowify-151_rainbowified.jpg, rainbowify-152.jpg, rainbowify-152_rainbowified.jpg, rainbowify-153.jpg, rainbowify-153_rainbowified.jpg, rainbowify-155.jpg, rainbowify-155_rainbowified.jpg, rainbowify-156.jpg, rainbowify-156_rainbowified.jpg, rainbowify-157.jpg, rainbowify-157_rainbowified.jpg, rainbowify-158.jpg, rainbowify-158_rainbowified.jpg, rainbowify-159.jpg, rainbowify-159_rainbowified.jpg
Jonathan Frech's blog; built 2021/04/16 20:21:20 CEST