Skip to content

Commit

Permalink
color: remove brightness related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Sep 24, 2024
1 parent 7e1f557 commit d24074f
Showing 1 changed file with 0 additions and 77 deletions.
77 changes: 0 additions & 77 deletions src/color.zig
Original file line number Diff line number Diff line change
Expand Up @@ -608,80 +608,3 @@ test "100 random colors" {
try expectEqualDeep(rgb, rgb_from_lab);
}
}

export fn computeBrightnessRgba(image: [*]const Rgba, rows: usize, cols: usize) f32 {
const size = rows * cols;
const img = image[0..size];
var r_sum: u64 = 0;
var g_sum: u64 = 0;
var b_sum: u64 = 0;
for (img) |p| {
r_sum += p.r;
g_sum += p.g;
b_sum += p.b;
}
return (@as(f32, @floatFromInt(r_sum)) * 0.2 +
@as(f32, @floatFromInt(g_sum)) * 0.7 +
@as(f32, @floatFromInt(b_sum)) * 0.1) /
@as(f32, @floatFromInt(size));
}

export fn computeBrightnessSimd(image: [*]const u8, size: usize) f32 {
const vector_len: usize = 512;
const pixel_len: usize = 4;
const repeat: usize = vector_len / pixel_len;
const img = image[0..size];
const r_idx: @Vector(vector_len, usize) = [_]usize{ 1, 0, 0, 0 } ** repeat;
const g_idx: @Vector(vector_len, usize) = [_]usize{ 0, 1, 0, 0 } ** repeat;
const b_idx: @Vector(vector_len, usize) = [_]usize{ 0, 0, 1, 0 } ** repeat;
var r_sum: u64 = 0;
var g_sum: u64 = 0;
var b_sum: u64 = 0;
var pos: usize = 0;
var rem = size;
var tmp: @Vector(vector_len, usize) = undefined;
while (rem > 0) {
if (rem < vector_len) {
while (pos < img.len) {
r_sum += img[pos];
g_sum += img[pos + 1];
b_sum += img[pos + 2];
pos += pixel_len;
rem -= pixel_len;
}
} else {
const v: @Vector(vector_len, u8) = img[pos..][0..vector_len].*;
tmp = r_idx * v;
r_sum += @reduce(.Add, tmp);
tmp = g_idx * v;
g_sum += @reduce(.Add, tmp);
tmp = b_idx * v;
b_sum += @reduce(.Add, tmp);
pos += vector_len;
rem -= vector_len;
}
}
return (@as(f32, @floatFromInt(r_sum)) * 0.2 +
@as(f32, @floatFromInt(g_sum)) * 0.7 +
@as(f32, @floatFromInt(b_sum)) * 0.1) /
@as(f32, @floatFromInt(size / pixel_len));
}

test "brightness" {
const rows: usize = 4321;
const cols: usize = 987;
const pixel_len = 4;
const size = rows * cols * pixel_len;
var img = [_]u8{1} ** size;
try std.testing.expectEqual(computeBrightnessRgba(@alignCast(@ptrCast(img[0..].ptr)), rows, cols), 1);
const seed: u64 = @truncate(@as(u128, @bitCast(std.time.nanoTimestamp())));
var prng = std.Random.DefaultPrng.init(seed);
var random = prng.random();
for (&img) |*p| {
p.* = random.int(u8);
}
try std.testing.expectEqual(
computeBrightnessRgba(@alignCast(@ptrCast(img[0..].ptr)), rows, cols),
computeBrightnessSimd(img[0..size].ptr, size),
);
}

0 comments on commit d24074f

Please sign in to comment.