tiny.simd.Image
Defined in image.
Source
Source: lib/simd/src/image/root.zig:69
zig
pub fn Image(comptime T: type) type { validateComponent(T); return struct { xsize_value: u32, ysize_value: u32, bytes_per_row_value: usize, storage: []u8, owned: bool, const Self = @This(); pub fn empty() Self { return zeroDimensions(0, 0); } fn zeroDimensions(width: usize, height: usize) Self { return .{ .xsize_value = @intCast(width), .ysize_value = @intCast(height), .bytes_per_row_value = 0, .storage = &.{}, .owned = false, }; } pub fn init(allocator: std.mem.Allocator, width: usize, height: usize) Error!Self { try validateDimensions(width, height); if (width == 0 or height == 0) return zeroDimensions(width, height); const stride = try bytesPerRowFor(width, @sizeOf(T), vector_size); const allocation_size = std.math.mul(usize, stride, height) catch return error.AllocationSizeOverflow; const storage = try allocator.alignedAlloc( u8, .fromByteUnits(storage_alignment), allocation_size, ); var result = Self{ .xsize_value = @intCast(width), .ysize_value = @intCast(height), .bytes_per_row_value = stride, .storage = storage, .owned = true, }; result.initializePadding(.round_up) catch |err| { allocator.free(storage); return err; }; return result; } pub fn initBorrowed( width: usize, height: usize, stride: usize, storage: []u8, ) GeometryError!Self { try validateDimensions(width, height); if (width == 0 or height == 0) { if (stride != 0) return error.InvalidStride; return zeroDimensions(width, height); } if (stride % vector_size != 0 or stride % @alignOf(T) != 0) { return error.InvalidStride; } const valid_bytes = std.math.mul(usize, width, @sizeOf(T)) catch return error.RowSizeOverflow; if (stride < valid_bytes) return error.InvalidStride; const required = std.math.mul(usize, stride, height) catch return error.AllocationSizeOverflow; if (storage.len < required) return error.BufferTooSmall; if (@intFromPtr(storage.ptr) % @max(vector_size, @alignOf(T)) != 0) { return error.MisalignedStorage; } return .{ .xsize_value = @intCast(width), .ysize_value = @intCast(height), .bytes_per_row_value = stride, .storage = storage[0..required], .owned = false, }; } pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { if (self.owned) { std.debug.assert(self.storage.len != 0); const aligned: []align(storage_alignment) u8 = @alignCast(self.storage); allocator.free(aligned); } self.* = empty(); } pub fn swap(self: *Self, other: *Self) void { std.mem.swap(Self, self, other); } pub fn shrinkTo(self: *Self, width: usize, height: usize) GeometryError!void { if (width > self.xsize_value or height > self.ysize_value) { return error.InvalidShrink; } self.xsize_value = @intCast(width); self.ysize_value = @intCast(height); } pub fn initializePaddingForUnalignedAccesses(self: *Self) GeometryError!void { try self.initializePadding(.unaligned); } pub fn xsize(self: *const Self) usize { return self.xsize_value; } pub fn ysize(self: *const Self) usize { return self.ysize_value; } pub fn bytesPerRow(self: *const Self) usize { return self.bytes_per_row_value; } pub fn pixelsPerRow(self: *const Self) usize { return self.bytes_per_row_value / @sizeOf(T); } pub fn bytes(self: *Self) []u8 { return self.storage; } pub fn constBytes(self: *const Self) []const u8 { return self.storage; } pub fn constRow(self: *const Self, y: usize) []const T { std.debug.assert(y < self.ysize_value); if (self.bytes_per_row_value == 0) return &.{}; const begin = y * self.bytes_per_row_value; const row = self.storage[begin..][0..self.bytes_per_row_value]; const ptr: [*]const T = @ptrCast(@alignCast(row.ptr)); return ptr[0..self.pixelsPerRow()]; } pub fn mutableRow(self: *const Self, y: usize) []T { std.debug.assert(y < self.ysize_value); if (self.bytes_per_row_value == 0) return &.{}; const begin = y * self.bytes_per_row_value; const row = self.storage[begin..][0..self.bytes_per_row_value]; const ptr: [*]T = @ptrCast(@alignCast(row.ptr)); return ptr[0..self.pixelsPerRow()]; } fn initializePadding(self: *Self, mode: Padding) GeometryError!void { if (self.xsize_value == 0 or self.ysize_value == 0 or vector_size == 1) return; const valid_bytes = std.math.mul( usize, self.xsize_value, @sizeOf(T), ) catch return error.RowSizeOverflow; const initialize_size = switch (mode) { .round_up => try roundUp(valid_bytes, vector_size), .unaligned => std.math.add( usize, valid_bytes, vector_size - @sizeOf(T), ) catch return error.RowSizeOverflow, }; if (initialize_size > self.bytes_per_row_value) { return error.InsufficientRowPadding; } for (0..self.ysize_value) |y| { const begin = y * self.bytes_per_row_value + valid_bytes; @memset(self.storage[begin..][0 .. initialize_size - valid_bytes], 0); } } };}Source: lib/simd/src/root.zig:521
zig
pub const Image = image.Image;Complete caller list
15 direct callers.
tiny.simd.image.Image3[function] atlib/simd/src/image/root.zig:246tiny.simd.Rect.constRow[method] atlib/simd/src/image/root.zig:474tiny.simd.Rect.mutableRow[method] atlib/simd/src/image/root.zig:480lib.simd.src.image.test.checkImageInitFailures[function] — private source atlib/simd/src/image/test.zig:60in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_image3_borrowed_planes_and_plane_transfer_reject_broken_invariants[function] — test source atlib/simd/src/image/test.zig:206in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_image3_owns_one_packed_allocation_and_consumes_matching_planes[function] — test source atlib/simd/src/image/test.zig:185in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_image_borrowed_storage_validates_alignment_stride_and_capacity[function] — test source atlib/simd/src/image/test.zig:97in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_image_capacity_boundaries_reject_overflow_before_allocation[function] — test source atlib/simd/src/image/test.zig:291in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_image_padding_initialization_preserves_valid_samples[function] — test source atlib/simd/src/image/test.zig:155in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_image_swap_and_shrink_preserve_storage_layout[function] — test source atlib/simd/src/image/test.zig:171in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_pinned_Highway_mirror_and_row_wrappers_agree[function] — test source atlib/simd/src/image/test.zig:269in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_rectangles_clamp_compose_and_address_image_rows[function] — test source atlib/simd/src/image/test.zig:254in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.test_zero-area_images_preserve_each_reported_dimension_without_allocation[function] — test source atlib/simd/src/image/test.zig:135in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.verifyAlignedWrites[function] — private source atlib/simd/src/image/test.zig:17in nearest public ownerlib.simd.src.image.testlib.simd.src.image.test.verifyUnalignedWrites[function] — private source atlib/simd/src/image/test.zig:39in nearest public ownerlib.simd.src.image.test
Audit
| Definitions | 1 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |