// SPDX-FileCopyrightText: © 2024 Mitchell Hashimoto, Ghostty contributors // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // 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_netpbm); /// Decode a Netpbm image. /// /// WUFFS handles only the binary variants -- `P5` (grayscale) and `P6` /// (RGB). The plain ASCII ones, `P1` through `P3`, and the `P7` PAM format /// are rejected as an unsupported Netpbm file. pub fn decode(alloc: Allocator, data: []const u8) Error!decoder.Image { return decoder.decode("netpbm", log, alloc, data); } test "netpbm_decode_P6_000000" { const data = try decode(std.testing.allocator, "P6\n1 1\n255\n\x00\x00\x00"); 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 "netpbm_decode_P6_FFFFFF" { const data = try decode(std.testing.allocator, "P6\n1 1\n255\n\xFF\xFF\xFF"); 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 "netpbm_decode_P5_grayscale" { const data = try decode(std.testing.allocator, "P5\n1 1\n255\n\x80"); 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, &.{ 0x80, 0x80, 0x80, 255 }, data.data); } // The plain ASCII variants are the ones the pixels fixtures happen to be // written in, which is why this library cannot use them. test "netpbm_decode_P3_is_unsupported" { // The decoder logs a warning 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; try std.testing.expectError( error.WuffsError, decode(std.testing.allocator, "P3\n1 1\n255\n0\n0\n0\n"), ); }