Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@
path = dependencies/small_vector
url = https://github.com/gharveymn/small_vector
shallow = true
[submodule "dependencies/tinyfits"]
path = dependencies/tinyfits
url = https://github.com/cek/tinyfits.git
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ set(TEV_SOURCES
include/tev/imageio/Exif.h src/imageio/Exif.cpp
include/tev/imageio/ExrImageLoader.h src/imageio/ExrImageLoader.cpp
include/tev/imageio/ExrImageSaver.h src/imageio/ExrImageSaver.cpp
include/tev/imageio/FitsImageLoader.h src/imageio/FitsImageLoader.cpp
include/tev/imageio/GainMap.h src/imageio/GainMap.cpp
include/tev/imageio/IcoImageLoader.h src/imageio/IcoImageLoader.cpp
include/tev/imageio/ImageLoader.h src/imageio/ImageLoader.cpp
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ $ cpack --config build/CPackConfig.cmake
## File Formats

- __EXR__ (via [OpenEXR](https://github.com/AcademySoftwareFoundation/openexr))
- __FITS__ (via [tinyfits](https://github.com/cek/tinyfits))
- __JPEG XL__ (including gain maps; via [libjxl](https://github.com/libjxl/libjxl))
- __JPEG__ (including gain maps, e.g. HDR pictures from Android; via [libjpeg-turbo](https://github.com/libjpeg-turbo/libjpeg-turbo))
- __JPEG 2000__ (via [openjpeg](https://github.com/uclouvain/openjpeg))
Expand Down
1 change: 1 addition & 0 deletions dependencies/tinyfits
Submodule tinyfits added at 598173
37 changes: 37 additions & 0 deletions include/tev/imageio/FitsImageLoader.h
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
1 change: 1 addition & 0 deletions src/HelpWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ HelpWindow::HelpWindow(Widget* parent, weak_ptr<Ipc> weakIpc, function<void()> c
addLibrary(about, "qoi", "QOI image format library");
addLibrary(about, "small_vector", "Vector with small buffer optimization");
addLibrary(about, "stb_image(_write)", "Single-header library for loading and writing images");
addLibrary(about, "tinyfits", "Single-header FITS image format library");
addLibrary(about, "tinylogger", "Minimal pretty-logging library");
addLibrary(about, "UTF8-CPP", "Lightweight UTF-8 string manipulation library");
addLibrary(about, "XMP-Toolkit-SDK", "XMP metadata parsing library");
Expand Down
116 changes: 116 additions & 0 deletions src/imageio/FitsImageLoader.cpp
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];
Copy link
Copy Markdown
Owner

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.

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);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 ScopeGuard. (See e.g. STBI image loader for a example usage.)

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);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.hasPremultipliedAlpha = true to avoid some tev-internal conversion checks.

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));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than std::move, prefer direct emplace AttributeNode& node = section.children.emplace_back();

Similarly for the root node.

}
resultData.attributes.push_back(std::move(root));

tinyfits_free(&fits);

co_return result;
}

} // namespace tev
3 changes: 3 additions & 0 deletions src/imageio/ImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <tev/imageio/ClipboardImageLoader.h>
#include <tev/imageio/EmptyImageLoader.h>
#include <tev/imageio/ExrImageLoader.h>
#include <tev/imageio/FitsImageLoader.h>
#include <tev/imageio/IcoImageLoader.h>
#include <tev/imageio/ImageLoader.h>
#include <tev/imageio/Jpeg2000ImageLoader.h>
Expand Down Expand Up @@ -65,6 +66,7 @@ const vector<unique_ptr<ImageLoader>>& ImageLoader::getLoaders() {
#ifdef TEV_SUPPORT_JXL
imageLoaders.emplace_back(new JxlImageLoader());
#endif
imageLoaders.emplace_back(new FitsImageLoader());
imageLoaders.emplace_back(new QoiImageLoader());
imageLoaders.emplace_back(new WebpImageLoader());
imageLoaders.emplace_back(new Jpeg2000ImageLoader());
Expand All @@ -89,6 +91,7 @@ const vector<string_view>& ImageLoader::supportedMimeTypes() {
#endif
"image/apng",
"image/bmp",
"image/fits",
"image/gif",
#ifdef TEV_SUPPORT_HEIC
"image/heic",
Expand Down
Loading