Skip to content

Commit

Permalink
inline to speed up image processing
Browse files Browse the repository at this point in the history
  • Loading branch information
ATTron committed Jul 17, 2024
1 parent 798ee01 commit 5bbbe6f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ pub fn build(b: *std.Build) void {
});

lib.root_module.addImport("zigimg", zigimg_dependency.module("zigimg"));
astroz_mod.addImport("zigimg", zigimg_dependency.module("zigimg"));

const cfitsio_dep = b.dependency("cfitsio", .{
.target = target,
.optimize = optimize,
});

lib.root_module.addImport("cfitsio", cfitsio_dep.module("cfitsio"));
astroz_mod.addImport("cfitsio", cfitsio_dep.module("cfitsio"));

const lib_install = b.addInstallArtifact(lib, .{});
lib_step.dependOn(&lib_install.step);
Expand Down
16 changes: 13 additions & 3 deletions src/Fits.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn toImage(self: *Fits, input_path: []const u8, output_path: []const u8, opt
try self.applyStretch(pixels, width, height, output_path, options);
}

fn applyStretch(self: *Fits, pixels: []f32, width: usize, height: usize, output_path: []const u8, options: StretchOptions) !void {
inline fn applyStretch(self: *Fits, pixels: []f32, width: usize, height: usize, output_path: []const u8, options: StretchOptions) !void {
const sorted_pixels = try self.allocator.dupe(f32, pixels);
defer self.allocator.free(sorted_pixels);
std.sort.heap(f32, sorted_pixels, {}, std.sort.asc(f32));
Expand Down Expand Up @@ -72,13 +72,13 @@ fn applyStretch(self: *Fits, pixels: []f32, width: usize, height: usize, output_
try image.writeToFilePath(output_path, .{ .png = .{} });
}

fn sineStretch(x: f32, options: StretchOptions) f32 {
inline fn sineStretch(x: f32, options: StretchOptions) f32 {
const stretch: f32 = options.stretch;
const bend: f32 = options.bend;
return std.math.asinh((x - bend) / stretch) / std.math.asinh((1 - bend) / stretch) * 0.5 + 0.5;
}

fn applyColorMap(value: f32) [3]f32 {
inline fn applyColorMap(value: f32) [3]f32 {
return .{ value, value, value };
}

Expand All @@ -94,5 +94,15 @@ pub const FitsError = error{

test Fits {
var fits_png = Fits.init(std.testing.allocator);
var fits_png1 = Fits.init(std.testing.allocator);
var fits_png2 = Fits.init(std.testing.allocator);
var fits_png3 = Fits.init(std.testing.allocator);
var fits_png4 = Fits.init(std.testing.allocator);
var fits_png5 = Fits.init(std.testing.allocator);
try fits_png.toImage("test/sample_fits.fits", "test/test.png", .{});
try fits_png1.toImage("test/sample_fits.fits", "test/test.png", .{});
try fits_png2.toImage("test/sample_fits.fits", "test/test.png", .{});
try fits_png3.toImage("test/sample_fits.fits", "test/test.png", .{});
try fits_png4.toImage("test/sample_fits.fits", "test/test.png", .{});
try fits_png5.toImage("test/sample_fits.fits", "test/test.png", .{});
}

0 comments on commit 5bbbe6f

Please sign in to comment.