Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- [x] Celestial Bodies
- [x] Orbital Mechanics
- [x] Interplanetary Maneuvers
- [ ] FITS File Parsing - BROKEN DUE TO ZIGIMG DEPENDENCY BREAKING ON MAIN
- [x] FITS File Parsing
- [x] Image Generation
- [ ] Multi Image Parsing/Generation
- [x] Table Parsing
Expand Down Expand Up @@ -104,8 +104,6 @@ exe.root_module.addImport("astroz", astroz_mod);

- #### [Create CCSDS Packet with Config](examples/create_ccsds_packet_config.zig)

**NOTE THIS IS CURRENTLY BROKEN DUE TO ZIGIMG DEPENDENCY BREAKING ON THE MAIN BRANCH**

- #### [Generate Image from FITS File](examples/parse_fits_file.zig)

<img src="test/test.png" width="450" height="400" alt="sample fits image as png"/>
Expand Down
37 changes: 18 additions & 19 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ pub fn build(b: *std.Build) void {
.use_llvm = use_llvm,
});

// Currently not using cfitsio or zigimg due to breaking on the master branch
// const zigimg_dependency = b.dependency("zigimg", .{
// .target = target,
// .optimize = optimize,
// });
//
// 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 zignal_dependency = b.dependency("zignal", .{
.target = target,
.optimize = optimize,
});

lib.root_module.addImport("zignal", zignal_dependency.module("zignal"));
astroz_mod.addImport("zignal", zignal_dependency.module("zignal"));

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 Expand Up @@ -79,8 +78,8 @@ pub fn build(b: *std.Build) void {
.root_module = astroz_mod,
});

// tests.root_module.addImport("zigimg", zigimg_dependency.module("zigimg"));
// tests.root_module.addImport("cfitsio", cfitsio_dep.module("cfitsio"));
tests.root_module.addImport("zignal", zignal_dependency.module("zignal"));
tests.root_module.addImport("cfitsio", cfitsio_dep.module("cfitsio"));

const tests_run = b.addRunArtifact(tests);
tests_step.dependOn(&tests_run.step);
Expand Down Expand Up @@ -115,5 +114,5 @@ const EXAMPLE_NAMES = &.{
"precess_star",
"simple_spacecraft_orientation",
"transfer_propagation",
// "parse_fits_image",
"parse_fits_file",
};
10 changes: 7 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
.fingerprint = 0x2d8583fca695159f, // Changing this has security and trust implications.
.minimum_zig_version = "0.15.0-dev.1031+61eff7b6d",
.dependencies = .{
.zigimg = .{
.url = "git+https://github.com/zigimg/zigimg.git#29ff47f9e011065460d1d049cdc18784d733e23b",
.hash = "12201c288e763fb7d7e3207a638498faa439124e231d6f0cbcd4ebc618f706164ea8",
.cfitsio = .{
.url = "https://github.com/arrufat/cfitsio/archive/9a7161ff41600aaa28fd040989294fb60a58df38.tar.gz",
.hash = "cfitsio-4.6.2-LaSYAO4YAACT0MYEVI4nelpfX5lnyQi_MvupX-zSrdRH",
},
.zignal = .{
.url = "https://github.com/bfactory-ai/zignal/archive/refs/tags/0.4.1.tar.gz",
.hash = "zignal-0.4.1-OrZ3s9agDwDpPhRcFDfe3jVA2PVPjasQ3WZTuHUsfio6",
},
},
.paths = .{
Expand Down
4 changes: 2 additions & 2 deletions examples/parse_fits_file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const astroz = @import("astroz");
const Fits = astroz.Fits;

pub fn main() !void {
var dbga = std.heap.DebugAllocator(.{}).init;
var dbga: std.heap.DebugAllocator(.{}) = .init;
defer _ = dbga.deinit();
const allocator = dbga.allocator();

var fitsPng = try Fits.open_and_parse(allocator);
var fitsPng: Fits = try .open_and_parse("test/sample_fits.fits", allocator);
defer fitsPng.close();
}
22 changes: 12 additions & 10 deletions src/Fits.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const zigimg = @import("zigimg");
const zignal = @import("zignal");
const cfitsio = @import("cfitsio").c;

const Fits = @This();
Expand Down Expand Up @@ -137,8 +137,9 @@ pub fn readTable(self: *Fits, hdu_number: c_int, output_path: ?[]const u8) !void
}

defer file.close();
var buffered_writer = std.io.bufferedWriter(file.writer());
var buffered = buffered_writer.writer();
var write_buffer: [4096]u8 = undefined;
var file_writer = file.writer(&write_buffer);
var buffered = &file_writer.interface;

// Write column headers
var i: c_int = 1;
Expand Down Expand Up @@ -196,7 +197,7 @@ pub fn readTable(self: *Fits, hdu_number: c_int, output_path: ?[]const u8) !void
}
try buffered.writeAll("\n");
}
try buffered_writer.flush();
try file_writer.interface.flush();
std.log.debug("Finished processing all columns and rows. CSV file created: {any}\n", .{file});
}

Expand Down Expand Up @@ -266,15 +267,16 @@ fn applyStretch(self: *Fits, pixels: []f32, width: usize, height: usize, output_
const vmin = sorted_pixels[vmin_idx];
const vmax = sorted_pixels[vmax_idx];

var image = try zigimg.Image.create(self.allocator, width, height, .rgb24);
defer image.deinit();
var image: zignal.Image(zignal.Rgb) = try .initAlloc(self.allocator, height, width);
defer image.deinit(self.allocator);
std.debug.assert(image.data.len == pixels.len);

for (pixels, 0..) |pixel, i| {
for (pixels, image.data) |pixel, *i| {
const normalized = std.math.clamp((pixel - vmin) / (vmax - vmin), 0, 1);
const stretched = sineStretch(normalized, options);
const color = applyColorMap(stretched);

image.pixels.rgb24[i] = .{
i.* = .{
.r = @intFromFloat(color[0] * 255),
.g = @intFromFloat(color[1] * 255),
.b = @intFromFloat(color[2] * 255),
Expand All @@ -291,10 +293,10 @@ fn applyStretch(self: *Fits, pixels: []f32, width: usize, height: usize, output_
var output_path_buffer: [std.fs.max_path_bytes]u8 = undefined;
if (output_path) |op| {
const output = try std.fmt.bufPrint(&output_path_buffer, "{s}/{s}.png", .{ file_name, op });
try image.writeToFilePath(output, .{ .png = .{} });
try image.save(self.allocator, output);
} else {
const output = try std.fmt.bufPrint(&output_path_buffer, "{s}/{s}.png", .{ file_name, file_name });
try image.writeToFilePath(output, .{ .png = .{} });
try image.save(self.allocator, output);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const EquatorialCoordinateSystem = @import("EquatorialCoordinateSystem.zig")
pub const WorldCoordinateSystem = @import("WorldCoordinateSystem.zig");
pub const OrbitalMechanics = @import("OrbitalMechanics.zig");
pub const Mission = @import("Mission.zig");
// pub const Fits = @import("Fits.zig");
pub const Fits = @import("Fits.zig");

test {
std.testing.refAllDecls(@This());
Expand Down