Skip to content

Commit

Permalink
Add convertNoFree() to add a convenience convert() that does not free…
Browse files Browse the repository at this point in the history
… the old pixel data
  • Loading branch information
mlarouche committed Jun 8, 2024
1 parent 4c8c598 commit 3e6c614
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ImageUnmanaged.zig
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pub fn writeToMemory(self: ImageUnmanaged, allocator: std.mem.Allocator, write_b

/// Convert the pixel format of the Image into another format.
/// It will allocate another pixel storage for the destination and free the old one
///
/// For the conversion to the indexed formats, no dithering is done.
pub fn convert(self: *ImageUnmanaged, allocator: std.mem.Allocator, destination_format: PixelFormat) ConvertError!void {
// Do nothing if the format is the same
Expand All @@ -246,6 +247,23 @@ pub fn convert(self: *ImageUnmanaged, allocator: std.mem.Allocator, destination_
self.pixels = new_pixels;
}

/// Convert the pixel format of the Image into another format.
/// It will allocate another pixel storage for the destination and not free the old one.
/// Ths is in the case the image doess not own the pixel data.
///
/// For the conversion to the indexed formats, no dithering is done.
pub fn convertNoFree(self: *ImageUnmanaged, allocator: std.mem.Allocator, destination_format: PixelFormat) ConvertError!void {
// Do nothing if the format is the same
if (std.meta.activeTag(self.pixels) == destination_format) {
return;
}

const new_pixels = try PixelFormatConverter.convert(allocator, &self.pixels, destination_format);
errdefer new_pixels.deinit(allocator);

self.pixels = new_pixels;
}

/// Iterate the pixel in pixel-format agnostic way. In the case of an animation, it returns an iterator for the first frame. The iterator is read-only.
pub fn iterator(self: *const ImageUnmanaged) color.PixelStorageIterator {
return color.PixelStorageIterator.init(&self.pixels);
Expand Down

0 comments on commit 3e6c614

Please sign in to comment.