-
Notifications
You must be signed in to change notification settings - Fork 117
feat(fits): add FITS image support #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * tev -- the EDR viewer | ||
| * | ||
| * Copyright (C) 2026 Thomas Müller <contact@tom94.net> | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <tev/Image.h> | ||
| #include <tev/imageio/ImageLoader.h> | ||
|
|
||
| #include <istream> | ||
|
|
||
| namespace tev { | ||
|
|
||
| class FitsImageLoader final : public ImageLoader { | ||
| public: | ||
| Task<std::vector<ImageData>> load( | ||
| std::istream& iStream, const fs::path& path, std::string_view channelSelector, const ImageLoaderSettings& settings, int priority | ||
| ) const override; | ||
|
|
||
| std::string name() const override { return "FITS"; } | ||
| }; | ||
|
|
||
| } // namespace tev |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * tev -- the EDR viewer | ||
| * | ||
| * Copyright (C) 2026 Thomas Müller <contact@tom94.net> | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include <tev/imageio/FitsImageLoader.h> | ||
|
|
||
| #define TINYFITS_IMPLEMENTATION | ||
| #include <tinyfits/tinyfits.h> | ||
|
|
||
| using namespace nanogui; | ||
| using namespace std; | ||
|
|
||
| namespace tev { | ||
|
|
||
| Task<vector<ImageData>> FitsImageLoader::load(istream& iStream, const fs::path&, string_view, const ImageLoaderSettings&, int priority) const { | ||
| char magic[9]; | ||
| iStream.read(magic, 9); | ||
|
|
||
| if (string_view{magic, 9} != "SIMPLE =") { | ||
| throw FormatNotSupported{"Not a FITS file."}; | ||
| } | ||
|
|
||
| iStream.clear(); | ||
| iStream.seekg(0, iStream.end); | ||
| const auto dataSize = iStream.tellg(); | ||
| iStream.seekg(0, iStream.beg); | ||
|
|
||
| HeapArray<char> data(dataSize); | ||
| iStream.read(data.data(), dataSize); | ||
|
|
||
| TinyFits fits = {}; | ||
| void* pixels = nullptr; | ||
| int err = tinyfits_load_from_memory(&fits, data.data(), (size_t)dataSize, &pixels); | ||
| if (err != TINYFITS_OK) { | ||
| tinyfits_free(&fits); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the explicit tinyfits_free and tinyfits_free_buffer calls should be replaced with a |
||
| throw ImageLoadError{fmt::format("Failed to load FITS: {}", tinyfits_error_string(err))}; | ||
| } | ||
|
|
||
| const Vector2i size{fits.width, fits.height}; | ||
| const auto numPixels = (size_t)fits.width * (size_t)fits.height; | ||
| if (numPixels == 0) { | ||
| tinyfits_free_buffer(pixels); | ||
| tinyfits_free(&fits); | ||
| throw ImageLoadError{"Image has zero pixels."}; | ||
| } | ||
|
|
||
| const auto numChannels = (size_t)fits.num_channels; | ||
|
|
||
| // Convert native pixel data to normalized float32 for display. | ||
| // tinyfits_to_float normalizes unsigned integer types to [0,1]; floats pass through as-is. | ||
| const auto totalSamples = numPixels * numChannels; | ||
| vector<float> floatPixels(totalSamples); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to create the channels upfront and then populate them directly via a switch over channel format w/ co_await toFloat32((pixel_t*)pixels + numPixels * c, 1, channels[c].view(), false, priority);Avoids the memcpy, extra allocation, and uses tev's thread pool to parallelize. |
||
| err = tinyfits_to_float(&fits, pixels, floatPixels.data()); | ||
| tinyfits_free_buffer(pixels); | ||
| if (err != TINYFITS_OK) { | ||
| tinyfits_free(&fits); | ||
| throw ImageLoadError{fmt::format("Failed to convert FITS pixels to float: {}", tinyfits_error_string(err))}; | ||
| } | ||
|
|
||
| vector<ImageData> result(1); | ||
| ImageData& resultData = result.front(); | ||
|
|
||
| for (size_t c = 0; c < numChannels; ++c) { | ||
| static const string rgbNames[] = {"R", "G", "B"}; | ||
| string name; | ||
| if (numChannels == 1) { | ||
| name = "L"; | ||
| } else if (numChannels == 3) { | ||
| name = rgbNames[c]; | ||
| } else { | ||
| name = to_string(c); | ||
| } | ||
| resultData.channels.emplace_back(name, size, EPixelFormat::F32, EPixelFormat::F32); | ||
| } | ||
|
|
||
| // tinyfits_to_float returns planar data (all of channel 0, then channel 1, ...) | ||
| for (size_t c = 0; c < numChannels; ++c) { | ||
| auto view = resultData.channels[c].view<float>(); | ||
| memcpy(view.data(), floatPixels.data() + c * numPixels, numPixels * sizeof(float)); | ||
| } | ||
|
|
||
| resultData.hasPremultipliedAlpha = false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the channel naming scheme above, it seems to me like FITS images can't contain an alpha channel at all? In that case, it'd be better to specify |
||
| resultData.nativeMetadata.transfer = ituth273::ETransfer::Linear; | ||
|
|
||
| AttributeNode root; | ||
| root.name = "FITS header"; | ||
| AttributeNode& section = root.children.emplace_back(); | ||
| section.name = ""; | ||
| for (int i = 0; i < fits.num_keywords; ++i) { | ||
| AttributeNode node; | ||
| node.name = fits.keywords[i].key; | ||
| node.value = fits.keywords[i].value; | ||
| section.children.push_back(std::move(node)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than Similarly for the root node. |
||
| } | ||
| resultData.attributes.push_back(std::move(root)); | ||
|
|
||
| tinyfits_free(&fits); | ||
|
|
||
| co_return result; | ||
| } | ||
|
|
||
| } // namespace tev | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should get zero-initialized via
= {};to avoid UB.Following two lines should use
sizeof(magic)instead of repeating the hardcoded size.