JPEG Memory Decoder (jpegd)
jpegd is a lightweight JPEG decoder for complete in-memory buffers. Marker parsing, Huffman entropy decoding, dequantization, IDCT, chroma upsampling, and color conversion are implemented inside libx, with no dependency on jpeg-9d, libjpeg, or another external JPEG library.
Decoded pixels use the XSTAR Surface format directly: each pixel is native-endian 0xAARRGGBB, with alpha fixed at 0xff. On little-endian systems, the four bytes in memory are B, G, R, A. The header is <libx/jpegd.h> and is also included indirectly by <xos/xos.h>.
Format Support
The decoder supports:
- 8-bit baseline and extended sequential DCT JPEG (
SOF0andSOF1); - grayscale, YCbCr, RGB, CMYK, and YCCK images;
- single-component and interleaved scans;
- common 4:4:4, 4:2:2, and 4:2:0 sampling, plus other horizontal and vertical sampling combinations allowed by the frame header;
- 8-bit and 16-bit quantization tables;
- Huffman tables carried in the file;
- standard luminance and chrominance Huffman tables when
DHTis absent, as required by many MJPEG streams; DRIrestart intervals andRST0throughRST7restart markers;- RGB, CMYK, and YCCK conversion selected by Adobe
APP14markers; - center-aligned bilinear chroma upsampling.
Progressive, arithmetic-coded, lossless, and hierarchical JPEG are not supported. jpegd_info and jpegd_decode reject these frame types.
API
#include <libx/jpegd.h>
int jpegd_info(const void * buf, int len, int * width, int * height);
int jpegd_decode(const void * buf, int len, void * pixels, int stride);
Both functions return 1 on success and 0 on failure.
jpegd_info
Checks the JPEG SOI, walks markers until it finds a supported sequential-DCT frame header, and returns the image dimensions. Either width or height may be NULL.
This function validates only the markers and frame header needed to identify the image. It does not decode scan data or fully validate quantization tables, Huffman tables, and entropy-coded data. Success therefore does not guarantee that jpegd_decode will succeed.
jpegd_decode
Decodes the complete JPEG in buf into caller-provided pixels:
lenis the JPEG buffer size in bytes; it must be positive and representable byint;pixelsmust have room for at leaststride * heightbytes;pixelsand the start of each row should be 4-byte aligned;strideis the byte distance between adjacent output rows, must be at leastwidth * 4, and should normally be a multiple of four;- each pixel is written directly as native-endian
0xffRRGGBB; - row padding introduced by
strideis left untouched.
The input and output buffers must not overlap. The output buffer may have been partially modified when decoding fails and must then be discarded. The API reports only success or failure and does not provide detailed error codes.
Example
The following function decodes an in-memory JPEG into a tightly packed premultiplied-ARGB buffer. JPEG has no alpha channel, so every output pixel is fully opaque.
static void * decode_jpeg(const void * buf, int len, int * width, int * height)
{
void * pixels;
int w;
int h;
int stride;
if(!jpegd_info(buf, len, &w, &h))
return NULL;
stride = w * 4;
pixels = xos_mem_malloc((size_t)stride * h);
if(!pixels)
return NULL;
if(!jpegd_decode(buf, len, pixels, stride))
{
xos_mem_free(pixels);
return NULL;
}
if(width)
*width = w;
if(height)
*height = h;
return pixels;
}
Release the returned buffer with xos_mem_free when it is no longer needed.
Using jpegd with a Surface
To create a graphics Surface from JPEG data, normally use:
struct surface_t * s = surface_alloc_from_buf(buf, len);
To load from XFS, use:
struct surface_t * s = surface_alloc_from_xfs(ctx, "image.jpg");
Both functions detect JPEG automatically. Because jpegd_decode already produces the native-endian ARGB format used by a Surface, no channel swap or additional pixel conversion is needed after decoding.
Camera frames in CAMERA_FORMAT_MJPG also use jpegd_decode directly. Frames with no DHT use the built-in standard Huffman tables.
Decoding Process and Memory Use
The decoder reads quantization tables, Huffman tables, the frame header, and scan headers. It then decodes DC and AC coefficients by MCU, performs dequantization and integer IDCT, writes the results to component sample planes, and finally performs upsampling and color conversion.
In addition to the caller-provided output buffer, one MCU-padded 8-bit sample plane is allocated for each image component. For a typical 4:2:0 JPEG, these planes total approximately width * height * 1.5, plus fixed-size decoder state and one 8×8 coefficient block. All temporary memory is released on success and failure.
Marker lengths, table indices, sampling factors, Huffman code space, coefficient ranges, restart sequences, dimension arithmetic, and input boundaries are validated.
Limitations
- JPEG decoding only; encoding is not provided;
- complete memory buffers only; file-handle and incremental streaming APIs are not provided;
- no progressive, arithmetic-coded, lossless, or hierarchical JPEG;
- EXIF Orientation is not applied; output orientation follows the encoded JPEG data;
- ICC color management is not performed;
- output is fixed to opaque native-endian ARGB;
- no scaled decoding or detailed error information.