Something went wrong. Try again.
Zig bindings to Google's WUFFS image decoders for PNG, JPEG, GIF and Netpbm
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");const Allocator = std.mem.Allocator;const decoder = @import("decoder.zig");const Error = @import("error.zig").Error;
const log = std.log.scoped(.wuffs_bmp);
/// Decode a BMP image.////// WUFFS reads the uncompressed `BI_RGB` form at 1, 2, 4, 8, 16, 24 and 32/// bits per pixel, the run-length encoded `BI_RLE8` and `BI_RLE4` forms, and/// `BI_BITFIELDS` at 16 and 32 bits per pixel with arbitrary channel masks./// Everything below 16 bits per pixel takes its colours from the palette/// between the header and the pixel data. It accepts the 12-byte/// BITMAPCOREHEADER and every later header up to the 124-byte/// BITMAPV5HEADER, so an alpha channel declared by a V4 or V5 header is/// honoured; a header length outside that set is an unsupported BMP file.////// A negative height means the rows run top to bottom rather than bottom to/// top, which WUFFS handles; so does a bottom-up image, which is the usual/// way round and is why a BMP written by hand often comes out upside down/// elsewhere.////// The one thing here that WUFFS will not decode is a BMP whose pixel data/// is an embedded JPEG or PNG (`BI_JPEG`, `BI_PNG`, compression 4 and/// 5). It answers those not with an image but with a request to be/// redirected to another decoder, and this one-buffer-in-one-image-out/// wrapper has nowhere to put that request, so such a file comes back as/// `error.WuffsError` with `@base: I/O redirect` logged. Those files are/// rare: they exist for print spoolers, and no ordinary BMP is one.pub fn decode(alloc: Allocator, data: []const u8) Error!decoder.Image { return decoder.decode("bmp", log, alloc, data);}
/// A one-pixel uncompressed 24-bit BMP of one colour. The whole file is 58/// bytes: a 14-byte file header, a 40-byte BITMAPINFOHEADER, and one row of/// one BGR pixel padded out to a multiple of four bytes.fn bgr24(b: u8, g: u8, r: u8) [58]u8 { return .{ // File header: "BM", the size of the file, four reserved bytes, and // the offset the pixel data starts at. 'B', 'M', 58, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, // BITMAPINFOHEADER: its own length, the width, the height, one // plane, the depth, BI_RGB, the size of the pixel data, and then the // two resolutions and two palette counts, none of which matter here. 40, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // The single row, which being one row is the same read either way // up: a BGR pixel and one byte of padding. b, g, r, 0, };}
test "bmp_decode_000000" { const data = try decode(std.testing.allocator, &bgr24(0x00, 0x00, 0x00)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(1, data.width); try std.testing.expectEqual(1, data.height); try std.testing.expectEqualSlices(u8, &.{ 0, 0, 0, 255 }, data.data);}
test "bmp_decode_FFFFFF" { const data = try decode(std.testing.allocator, &bgr24(0xFF, 0xFF, 0xFF)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(1, data.width); try std.testing.expectEqual(1, data.height); try std.testing.expectEqualSlices(u8, &.{ 255, 255, 255, 255 }, data.data);}
test "bmp_decode_channel_order" { // Blue in the file, which is the first byte of a BGR pixel and the last // of the RGBA that comes back. A grey image would not catch the order // being wrong; this does. const data = try decode(std.testing.allocator, &bgr24(0xFF, 0x00, 0x00)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, data.data);}
/// A one-pixel-wide, two-row 24-bit BMP: a red row and then a blue row, in/// that order in the file. A positive height means the rows run bottom to/// top, which is the usual way round for a BMP and so puts the red one at/// the bottom of the image; a negative one means they run top to bottom.fn twoRows(top_down: bool) [62]u8 { var file: [62]u8 = .{ // File header. 'B', 'M', 62, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, // BITMAPINFOHEADER: one pixel wide, two rows, 24 bits per pixel. 40, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Two rows of one BGR pixel each, padded to four bytes: red, blue. 0x00, 0x00, 0xFF, 0, 0xFF, 0x00, 0x00, 0, };
// biHeight is signed, and the sign is the whole of the difference. if (top_down) std.mem.writeInt(i32, file[22..26], -2, .little);
return file;}
test "bmp_decode_bottom_up" { const data = try decode(std.testing.allocator, &twoRows(false)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(1, data.width); try std.testing.expectEqual(2, data.height); // Top row first: the blue one, because the red one was written last and // so is the bottom of the image. try std.testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255, 255, 0, 0, 255, }, data.data);}
test "bmp_decode_top_down" { const data = try decode(std.testing.allocator, &twoRows(true)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(1, data.width); try std.testing.expectEqual(2, data.height); try std.testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255, 0, 0, 255, 255, }, data.data);}
/// A one-pixel 8-bit BMP whose colour comes from a two-entry palette. The/// number of palette entries is the gap between the end of the header and/// the pixel data -- WUFFS reads the palette out of that padding rather than/// trusting the header's count -- so the 62-byte offset below is what says/// there are two.const paletted8 = [_]u8{ // File header. 'B', 'M', 66, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, // BITMAPINFOHEADER, 8 bits per pixel and BI_RGB. 40, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, // The palette, BGRX per entry: black, then a mid green. 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, // One row: the index, padded out to four bytes. 1, 0, 0, 0,};
test "bmp_decode_paletted" { const data = try decode(std.testing.allocator, &paletted8); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(1, data.width); try std.testing.expectEqual(1, data.height); try std.testing.expectEqualSlices(u8, &.{ 0x00, 0x80, 0x00, 255 }, data.data);}
/// A two-pixel 8-bit BMP whose row is run-length encoded: `BI_RLE8`, one run/// of two pixels of palette entry 1, then the end-of-bitmap marker.const rle8 = [_]u8{ // File header. 'B', 'M', 66, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, // BITMAPINFOHEADER: two pixels wide, 8 bits per pixel, compression 1. 40, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, // The palette: black, then a mid blue. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, // The encoded row, then 00 01, end of bitmap. 2, 1, 0, 1,};
test "bmp_decode_rle8" { const data = try decode(std.testing.allocator, &rle8); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(2, data.width); try std.testing.expectEqual(1, data.height); try std.testing.expectEqualSlices(u8, &.{ 0x00, 0x00, 0x80, 255, 0x00, 0x00, 0x80, 255, }, data.data);}
/// A one-pixel 32-bit BMP with an alpha channel: a 56-byte header, which is/// the 40-byte one followed by the four channel masks, and `BI_BITFIELDS`./// The masks put the channels where a V4 header's defaults would, so the/// pixel is stored little-endian as blue, green, red, alpha.fn bitfields32(b: u8, g: u8, r: u8, a: u8) [74]u8 { return .{ // File header, with the pixel data 70 bytes in. 'B', 'M', 74, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, // The 40-byte header, with BI_BITFIELDS as the compression. 56, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 32, 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // The red, green, blue and alpha masks that make it 56 bytes. 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, // The one pixel. b, g, r, a, };}
test "bmp_decode_opaque_alpha" { const data = try decode(std.testing.allocator, &bitfields32(0x00, 0x00, 0xFF, 0xFF)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqual(1, data.width); try std.testing.expectEqual(1, data.height); try std.testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, data.data);}
// BMP stores alpha straight, and this library hands back premultiplied RGBA,// so the conversion happens on the way out and this is where it is pinned.test "bmp_decode_premultiplies_alpha" { const data = try decode(std.testing.allocator, &bitfields32(0x00, 0x00, 0xFF, 0x80)); defer std.testing.allocator.free(data.data);
try std.testing.expectEqualSlices(u8, &.{ 0x80, 0, 0, 0x80 }, data.data);}
// A BMP whose pixels are an embedded JPEG is answered with a redirect rather// than an image. If WUFFS ever decodes one in place, or this wrapper learns// to follow the redirect, this test is what will say so.test "bmp_decode_embedded_jpeg_is_unsupported" { // The decoder logs on the way out and this test expects it, so keep it // off the test runner's stderr. const prev = std.testing.log_level; std.testing.log_level = .err; defer std.testing.log_level = prev;
// A BITMAPINFOHEADER with zero bits per pixel and compression 4, which // is how a BMP says its pixel data is a JPEG file. var data = bgr24(0, 0, 0); data[28] = 0; // biBitCount data[30] = 4; // biCompression
try std.testing.expectError( error.WuffsError, decode(std.testing.allocator, &data), );}