Skip to content
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

Check compression method used in the image (#17) #19

Merged
merged 1 commit into from
May 5, 2021
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
45 changes: 20 additions & 25 deletions cpp/plugins/cucim.kit.cuslide/src/cuslide/cuslide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@

#include "cuslide.h"

#include "cucim/io/format/image_format.h"

#include "cucim/core/framework.h"
#include "cucim/core/plugin_util.h"

#include <memory>
//#include "tiffio.h"
//#include "tif_dir.h"
#include "cucim/io/format/image_format.h"
#include "tiff/tiff.h"
#include <fcntl.h>

#include <fmt/format.h>
#include <nlohmann/json.hpp>

#include <array>
#include <cassert>
#include <cstring>
#include <array>
#include <nlohmann/json.hpp>
// #include <rmm/mr/host/new_delete_resource.hpp>
#include <fcntl.h>
#include <memory>

using json = nlohmann::json;

Expand Down Expand Up @@ -121,7 +119,18 @@ static bool CUCIM_ABI parser_parse(CuCIMFileHandle* handle, cucim::io::format::I
}

// Assume that the image has only one main (high resolution) image.
assert(main_ifd_list.size() == 1);
if (main_ifd_list.size() != 1)
{
throw std::runtime_error(
fmt::format("This format has more than one image with Subfile Type 0 so cannot be loaded!"));
}

// Explicitly forbid loading SVS format (#17)
if (tif->ifd(0)->image_description().rfind("Aperio", 0) == 0)
{
throw std::runtime_error(
fmt::format("cuCIM doesn't support Aperio SVS for now (https://github.com/rapidsai/cucim/issues/17)."));
}

//
// Metadata Setup
Expand Down Expand Up @@ -284,17 +293,3 @@ void fill_interface(cucim::io::format::IImageFormat& iface)
};
// clang-format on
}

//
//
//#include <iostream>
//#include "fmt/format.h"
//
//
//
// CUCIM_API int foo()
//{
// std::cout << "Foo!" << std::endl;
//// std::string a = fmt::format(b.getName());
// return 0;
//}
35 changes: 32 additions & 3 deletions cpp/plugins/cucim.kit.cuslide/src/cuslide/tiff/ifd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ bool IFD::read(const TIFF* tiff,
"Cannot handle the out-of-boundary cases for a non-RGB image or a non-Jpeg/Deflate-compressed image."));
}

// Check if the image format is supported or not
if (!is_format_supported())
{
throw std::runtime_error(fmt::format(
"This format (compression: {}, sample_per_pixel: {}, planar_config: {}, photometric: {}) is not supported yet!.",
compression_, samples_per_pixel_, planar_config_, photometric_));
}

if (tif->tif_curdir != ifd_index)
{
TIFFSetDirectory(tif, ifd_index);
Expand Down Expand Up @@ -204,8 +212,20 @@ bool IFD::read(const TIFF* tiff,
}
}
}
else
{
throw std::runtime_error(fmt::format(
"This format (compression: {}, sample_per_pixel: {}, planar_config: {}, photometric: {}) is not supported yet!: {}",
compression_, samples_per_pixel_, planar_config_, photometric_, emsg));
}
TIFFRGBAImageEnd(&img);
}
else
{
throw std::runtime_error(fmt::format(
"This format (compression: {}, sample_per_pixel: {}, planar_config: {}, photometric: {}) is not supported yet!: {}",
compression_, samples_per_pixel_, planar_config_, photometric_, emsg));
}
}

int ndim = 3;
Expand Down Expand Up @@ -308,15 +328,24 @@ const std::vector<uint64_t>& IFD::image_piece_bytecounts() const
return image_piece_bytecounts_;
}

bool IFD::is_read_optimizable() const
bool IFD::is_compression_supported() const
{
return (compression_ == COMPRESSION_ADOBE_DEFLATE || compression_ == COMPRESSION_JPEG ||
compression_ == COMPRESSION_DEFLATE) &&
bits_per_sample_ == 8 && samples_per_pixel_ == 3 && planar_config_ == PLANARCONFIG_CONTIG &&
compression_ == COMPRESSION_DEFLATE);
}
bool IFD::is_read_optimizable() const
{
return is_compression_supported() && bits_per_sample_ == 8 && samples_per_pixel_ == 3 &&
planar_config_ == PLANARCONFIG_CONTIG &&
(photometric_ == PHOTOMETRIC_RGB || photometric_ == PHOTOMETRIC_YCBCR) &&
!tiff_->is_in_read_config(TIFF::kUseLibTiff);
}

bool IFD::is_format_supported() const
{
return is_compression_supported();
}

bool IFD::read_region_tiles(const TIFF* tiff,
const IFD* ifd,
const int64_t sx,
Expand Down
10 changes: 10 additions & 0 deletions cpp/plugins/cucim.kit.cuslide/src/cuslide/tiff/ifd.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ class EXPORT_VISIBLE IFD : public std::enable_shared_from_this<IFD>
std::vector<uint64_t> image_piece_offsets_;
std::vector<uint64_t> image_piece_bytecounts_;

/**
* @brief Check if the current compression method is supported or not.
*/
bool is_compression_supported() const;

/**
*
* Note: This method is called by the constructor of IFD and read() method so it is possible that the output of
Expand All @@ -129,6 +134,11 @@ class EXPORT_VISIBLE IFD : public std::enable_shared_from_this<IFD>
* @return
*/
bool is_read_optimizable() const;

/**
* @brief Check if the specified image format is supported or not.
*/
bool is_format_supported() const;
};
} // namespace cuslide::tiff

Expand Down