Skip to content

Commit

Permalink
Merge pull request #60 from vancluever/alpha-greyscale
Browse files Browse the repository at this point in the history
export_png: allow exporting of alpha surfaces to greyscale
  • Loading branch information
vancluever authored Nov 20, 2024
2 parents 578630c + ae6cb42 commit 8a276ad
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
7 changes: 4 additions & 3 deletions spec/047_fill_triangle_alpha_gray.zig
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
Binary file modified spec/files/047_fill_triangle_alpha_gray_pixelated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified spec/files/047_fill_triangle_alpha_gray_smooth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 21 additions & 9 deletions src/export_png.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, .{});
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8a276ad

Please sign in to comment.