Commit 6ce9514e authored by Baptiste Esteban's avatar Baptiste Esteban
Browse files

Solve too much errors + start working on tests

parent cd21b3b6
Pipeline #30144 failed with stages
in 1 minute and 57 seconds
......@@ -107,13 +107,14 @@ add_library(Pylene-io-fits)
add_library(Pylene::IO-fits ALIAS Pylene-io-fits)
target_sources(Pylene-io-fits PRIVATE
src/io/cfitsio_plugin.cpp
src/io/io.cpp
)
target_compile_features(Pylene-io-fits PUBLIC cxx_std_20)
target_include_directories(Pylene-io-fits PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(Pylene-io-freeimage PUBLIC cfitsio::cfitsio Pylene-core)
target_link_libraries(Pylene-io-fits PUBLIC cfitsio::cfitsio Pylene-core)
# Compiler configurations
target_compile_features(Pylene-core PUBLIC cxx_std_20)
......
#include <mln/core/image/ndimage.hpp>
#include <mln/io/imread.hpp>
#include <mln/io/private/cfitsio_plugin.hpp>
#include <mln/io/private/io.hpp>
#include <fitsio.h>
#include <fmt/format.h>
......@@ -6,121 +10,138 @@
#include <stdexcept>
#include <tuple>
namespace mln::io::fits::internal
namespace mln::io::fits
{
namespace
namespace internal
{
std::pair<mln::sample_type_id, int> get_type_info(int type)
namespace
{
switch (type)
std::pair<mln::sample_type_id, int> get_type_info(int type)
{
case BYTE_IMG:
return {mln::sample_type_id::UINT8, TBYTE};
case SHORT_IMG:
return {mln::sample_type_id::INT16, TSHORT};
case LONG_IMG:
return {mln::sample_type_id::INT32, TINT};
case LONGLONG_IMG:
return {mln::sample_type_id::INT64, TLONGLONG};
case FLOAT_IMG:
return {mln::sample_type_id::FLOAT, TFLOAT};
case DOUBLE_IMG:
return {mln::sample_type_id::DOUBLE, TDOUBLE};
break;
default:
throw std::runtime_error("Unhandled data type");
switch (type)
{
case BYTE_IMG:
return {mln::sample_type_id::UINT8, TBYTE};
case SHORT_IMG:
return {mln::sample_type_id::INT16, TSHORT};
case LONG_IMG:
return {mln::sample_type_id::INT32, TINT};
case LONGLONG_IMG:
return {mln::sample_type_id::INT64, TLONGLONG};
case FLOAT_IMG:
return {mln::sample_type_id::FLOAT, TFLOAT};
case DOUBLE_IMG:
return {mln::sample_type_id::DOUBLE, TDOUBLE};
break;
default:
throw std::runtime_error("Unhandled data type");
}
return {};
}
return {};
}
struct impl_cfitsio_t : mln::io::internal::plugin_base::impl_t
{
fitsfile* file = nullptr;
int datatype;
void read_next_line(std::byte* __restrict buffer) final
struct impl_cfitsio_t : mln::io::internal::plugin_base::impl_t
{
int anynul;
int nullpix = 0;
int status;
fits_read_img(file, datatype, m_line * m_dims[0] + 1, m_dims[0], &nullpix, buffer, &anynul, &status);
if (status)
fitsfile* file = nullptr;
int datatype;
void read_next_line(std::byte* __restrict buffer) final
{
char msg[80];
fits_get_errstatus(status, msg);
throw std::runtime_error(fmt::format("Unable to write the image ({})", msg));
int anynul;
int nullpix = 0;
int status = 0;
fits_read_img(file, datatype, m_line * m_dims[0] + 1, m_dims[0], &nullpix, buffer, &anynul, &status);
if (status)
{
char msg[80];
fits_get_errstatus(status, msg);
throw std::runtime_error(fmt::format("Unable to read the image ({})", msg));
}
m_line++;
}
m_line++;
}
void write_next_line(const std::byte* /* buffer */) final { std::abort(); }
void write_next_line(const std::byte* /* buffer */) final { std::abort(); }
private:
int m_line = 0;
};
} // namespace
private:
int m_line = 0;
};
} // namespace
cfitsio_reader_plugin::cfitsio_reader_plugin(int ind)
: m_image_index(ind)
{
}
cfitsio_reader_plugin::cfitsio_reader_plugin(int ind)
: m_image_index(ind)
{
}
cfitsio_reader_plugin::~cfitsio_reader_plugin() { this->close(); }
void cfitsio_reader_plugin::open(const char* filename)
{
int status = 0;
// Open the file
fitsfile* file;
fits_open_file(&file, filename, READONLY, &status);
if (status)
throw std::runtime_error(fmt::format("Unable to read the file {}", filename));
// Go to the index of the image
fits_movrel_hdu(file, m_image_index, nullptr, &status);
if (status)
throw std::runtime_error(fmt::format("Could not find the image at index {}", m_image_index));
// Check if the HDU is an image
int hdu_type;
fits_get_hdu_type(file, &hdu_type, &status);
if (hdu_type != IMAGE_HDU)
throw std::runtime_error(fmt::format("HDU at index {} is not an image", m_image_index));
// Get the number of dimension
int ndim;
fits_get_img_dim(file, &ndim, &status);
if (ndim == 0 || ndim > 4)
throw std::runtime_error(fmt::format("Unhandled image number of dimension (Got {}, expected in [1 - 4]", ndim));
// Get the dimensions
long dims[4];
fits_get_img_size(file, ndim, dims, &status);
// Get the type info
int type;
fits_get_img_type(file, &type, &status);
const auto [sample_type, datatype] = get_type_info(type);
auto impl = std::make_unique<impl_cfitsio_t>();
impl->file = file;
impl->m_ndim = ndim;
for (int i = 0; i < ndim; i++)
impl->m_dims[i] = dims[i];
impl->m_sample_type_id = sample_type;
impl->datatype = datatype;
this->m_impl = std::move(impl);
}
cfitsio_reader_plugin::~cfitsio_reader_plugin() { this->close(); }
void cfitsio_reader_plugin::close()
{
auto* impl = static_cast<impl_cfitsio_t*>(this->m_impl.get());
if (impl && impl->file)
{
int status;
fits_close_file(impl->file, &status);
impl->file = nullptr;
}
}
} // namespace internal
void cfitsio_reader_plugin::open(const char* filename)
mln::ndbuffer_image imread(const std::string& filename, int ind)
{
int status;
// Open the file
fitsfile* file;
fits_open_file(&file, filename, READONLY, &status);
if (status)
throw std::runtime_error(fmt::format("Unable to read the file {}", filename));
// Go to the index of the image
fits_movrel_hdu(file, m_image_index, nullptr, &status);
if (!status)
throw std::runtime_error(fmt::format("Could not find the image at index {}", m_image_index));
// Check if the HDU is an image
int hdu_type;
fits_get_hdu_type(file, &hdu_type, &status);
if (hdu_type != IMAGE_HDU)
throw std::runtime_error(fmt::format("HDU at index {} is not an image", m_image_index));
// Get the number of dimension
int ndim;
fits_get_img_dim(file, &ndim, &status);
if (ndim == 0 || ndim > 4)
throw std::runtime_error(fmt::format("Unhandled image number of dimension (Got {}, expected in [1 - 4]", ndim));
// Get the dimensions
long dims[4];
fits_get_img_size(file, ndim, dims, &status);
// Get the type info
int type;
fits_get_img_type(file, &type, &status);
const auto [sample_type, datatype] = get_type_info(type);
auto impl = std::make_unique<impl_cfitsio_t>();
impl->file = file;
impl->m_ndim = ndim;
for (int i = 0; i < ndim; i++)
impl->m_dims[i] = dims[i];
impl->m_sample_type_id = sample_type;
this->m_impl = std::move(impl);
mln::ndbuffer_image out;
imread(filename, out, ind);
return out;
}
void cfitsio_reader_plugin::close()
void imread(const std::string& filename, mln::ndbuffer_image& out, int ind)
{
auto* impl = static_cast<impl_cfitsio_t*>(this->m_impl.get());
if (impl && impl->file)
{
int status;
fits_close_file(impl->file, &status);
impl->file = nullptr;
}
internal::cfitsio_reader_plugin p(ind);
mln::io::internal::load(&p, filename.c_str(), out);
}
} // namespace mln::io::fits::internal
\ No newline at end of file
} // namespace mln::io::fits
\ No newline at end of file
......@@ -19,20 +19,4 @@ namespace mln::io
internal::freeimage_reader_plugin p;
internal::load(&p, filename.c_str(), out);
}
namespace fits
{
mln::ndbuffer_image imread(const std::string& filename, int ind = 0)
{
mln::ndbuffer_image out;
imread(filename, out, ind);
return out;
}
void imread(const std::string& filename, mln::ndbuffer_image& out, int ind = 0)
{
internal::cfitsio_reader_plugin p(ind);
mln::io::internal::load(&p, filename.c_str(), out);
}
} // namespace fits
} // namespace mln::io
add_core_test(UTIo_freeimage freeimage.cpp)
add_core_test(UTIo_imprint imprint.cpp)
add_core_test(UTIo_cfitsio cfitsio.cpp)
target_link_libraries(UTIo_cfitsio PUBLIC Pylene::IO-fits)
\ No newline at end of file
#include <mln/io/imread.hpp>
#include <fixtures/ImageCompare/image_compare.hpp>
#include <fixtures/ImagePath/image_path.hpp>
#include <gtest/gtest.h>
static const auto filename = fixtures::ImagePath::concat_with_filename("test.fit");
TEST(IO, cfitsio_not_an_image)
{
bool has_raised = false;
try
{
auto img = mln::io::fits::imread(filename, 0);
(void)img;
}
catch (std::runtime_error&)
{
has_raised = true;
}
ASSERT_TRUE(has_raised);
}
TEST(IO, cfitsio_2D_uint8)
{
auto img = mln::io::fits::imread(filename, 1);
ASSERT_TRUE(img.sample_type() == mln::sample_type_id::UINT8);
}
TEST(IO, cfitsio_2D_int16)
{
auto img = mln::io::fits::imread(filename, 2);
ASSERT_TRUE(img.sample_type() == mln::sample_type_id::INT16);
}
TEST(IO, cfitsio_2D_int32)
{
auto img = mln::io::fits::imread(filename, 3);
ASSERT_TRUE(img.sample_type() == mln::sample_type_id::INT32);
}
TEST(IO, cfitsio_2D_int64)
{
auto img = mln::io::fits::imread(filename, 4);
ASSERT_TRUE(img.sample_type() == mln::sample_type_id::INT64);
}
TEST(IO, cfitsio_2D_float)
{
auto img = mln::io::fits::imread(filename, 5);
ASSERT_TRUE(img.sample_type() == mln::sample_type_id::FLOAT);
}
TEST(IO, cfitsio_2D_double)
{
auto img = mln::io::fits::imread(filename, 6);
ASSERT_TRUE(img.sample_type() == mln::sample_type_id::DOUBLE);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment