diff --git a/spec/047_fill_triangle_alpha_gray.zig b/spec/047_fill_triangle_alpha_gray.zig index dcdc45b..9f03e57 100644 --- a/spec/047_fill_triangle_alpha_gray.zig +++ b/spec/047_fill_triangle_alpha_gray.zig @@ -1,7 +1,8 @@ // SPDX-License-Identifier: 0BSD // Copyright © 2024 Chris Marchesi -//! Case: Renders and fills a triangle on a 300x300 surface. +//! Case: Renders and fills a triangle on a 300x300 surface using alpha8 for +//! grayscale. //! //! This is similar to the 003_fill_triangle.zig, but uses alpha8 as its source //! versus RGB. Also renders a gray triangle at half alpha to test optimized @@ -15,8 +16,8 @@ pub const filename = "047_fill_triangle_alpha_gray"; pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface { const width = 300; const height = 300; - var sfc = try z2d.Surface.initPixel( - .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White so that srcOver shows up correctly + var sfc = try z2d.Surface.init( + .image_surface_alpha8, alloc, width, height, diff --git a/spec/files/047_fill_triangle_alpha_gray_pixelated.png b/spec/files/047_fill_triangle_alpha_gray_pixelated.png index 096ce63..c7cb03d 100644 Binary files a/spec/files/047_fill_triangle_alpha_gray_pixelated.png and b/spec/files/047_fill_triangle_alpha_gray_pixelated.png differ diff --git a/spec/files/047_fill_triangle_alpha_gray_smooth.png b/spec/files/047_fill_triangle_alpha_gray_smooth.png index 0311be2..2d538ac 100644 Binary files a/spec/files/047_fill_triangle_alpha_gray_smooth.png and b/spec/files/047_fill_triangle_alpha_gray_smooth.png differ diff --git a/src/export_png.zig b/src/export_png.zig index 43fd0b0..496e0dd 100644 --- a/src/export_png.zig +++ b/src/export_png.zig @@ -39,12 +39,17 @@ pub fn writeToPNGFile( sfc: surface.Surface, filename: []const u8, ) WriteToPNGFileError!void { - switch (sfc.getFormat()) { - .rgba, .rgb => {}, - else => { - return error.UnsupportedSurfaceFormat; - }, - } + // TODO: There are currently no unsupported surface formats, and there + // might likely not be going forward (we had alpha as an unsupported format + // but now we just convert it to greyscale). Keeping this here for a bit + // just in case, but we likely can remove it. + // + // switch (sfc.getFormat()) { + // .rgba, .rgb, .alpha8 => {}, + // else => { + // return error.UnsupportedSurfaceFormat; + // }, + // } // Open and create the file. const file = try fs.cwd().createFile(filename, .{}); @@ -73,12 +78,12 @@ fn writePNGIHDR(file: fs.File, sfc: surface.Surface) (Error || fs.File.WriteErro const depth: u8 = switch (sfc.getFormat()) { .rgba => 8, .rgb => 8, - else => return error.UnsupportedSurfaceFormat, + .alpha8 => 8, }; const color_type: u8 = switch (sfc.getFormat()) { .rgba => 6, .rgb => 2, - else => return error.UnsupportedSurfaceFormat, + .alpha8 => 0, }; const compression: u8 = 0; const filter: u8 = 0; @@ -177,7 +182,14 @@ fn writePNGIDATStream( ); break :written 4; // 4 bytes }, - else => return error.UnsupportedSurfaceFormat, + .alpha8 => |px| { + mem.copyForwards( + u8, + pixel_buffer[nbytes..pixel_buffer.len], + &@as([1]u8, @bitCast(px)), + ); + break :written 1; // 1 byte + }, } }; if (try zlib_stream.write(pixel_buffer[0..nbytes]) != nbytes) {