Skip to content

Commit

Permalink
remove short cut to getting scene xy size which can be inconsistent b…
Browse files Browse the repository at this point in the history
…etween boundingBox and logicalRect
  • Loading branch information
toloudis committed Nov 8, 2024
1 parent 58cedaa commit c58b470
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
6 changes: 4 additions & 2 deletions _aicspylibczi/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Image : public SubblockSortable
, m_shape(std::move(shape_))
, m_pixelType(pixel_type_)
, m_xywh(box_)
{}
{
}

size_t calculateIdx(const std::vector<size_t>& indexes_);

Expand Down Expand Up @@ -144,8 +145,9 @@ class ImageVector : public std::vector<std::shared_ptr<Image>>
size_t hByWsize = heightByWidth.size();
charSizes.emplace_back('Y', heightByWidth[0]); // H: 0
charSizes.emplace_back('X', heightByWidth[1]); // W: 1
if (hByWsize > 2)
if (hByWsize > 2) {
charSizes.emplace_back('A', heightByWidth[2]); // A: 3
}
// sort them into decending DimensionIndex Order
std::sort(charSizes.begin(), charSizes.end(), [&](std::pair<char, size_t> a_, std::pair<char, size_t> b_) {
return libCZI::Utils::CharToDimension(a_.first) > libCZI::Utils::CharToDimension(b_.first);
Expand Down
20 changes: 8 additions & 12 deletions _aicspylibczi/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,21 @@ Reader::sceneShape(int scene_index_)
definedDims[dimensionIndexToDimIndex(di_)].emplace(val_);
return true;
});
if (isMosaic())
if (isMosaic()) {
definedDims[DimIndex::M].emplace(x.first.mIndex());
}
}
for (auto x : definedDims)
for (auto x : definedDims) {
tbl.emplace(x.first, std::make_pair(*x.second.begin(), *x.second.rbegin() + 1));
}

auto xySize = getSceneYXSize(scene_index_);
tbl.emplace(DimIndex::Y, std::make_pair(0, xySize.h));
tbl.emplace(DimIndex::X, std::make_pair(0, xySize.w));
}
if (ImageFactory::numberOfSamples(m_pixelType) > 1)
if (ImageFactory::numberOfSamples(m_pixelType) > 1) {
tbl.emplace(charToDimIndex('A'), std::make_pair(0, ImageFactory::numberOfSamples(m_pixelType)));
}

return tbl;
}
Expand All @@ -206,18 +209,11 @@ Reader::getAllSceneYXSize(int scene_index_, bool get_all_matches_)
{
std::vector<libCZI::IntRect> result;
bool hasScene = m_statistics.dimBounds.IsValid(libCZI::DimensionIndex::S);
if (!isMosaic() && hasScene) {
int sStart(0), sSize(0);
m_statistics.dimBounds.TryGetInterval(libCZI::DimensionIndex::S, &sStart, &sSize);
if (scene_index_ >= sStart && (sStart + sSize - 1) >= scene_index_ && !m_statistics.sceneBoundingBoxes.empty()) {
result.emplace_back(m_statistics.sceneBoundingBoxes[scene_index_].boundingBoxLayer0);
return result;
}
}

libCZI::CDimCoordinate scene_coord; // default constructor
if (hasScene && scene_index_ >= 0)
if (hasScene && scene_index_ >= 0) {
scene_coord = libCZI::CDimCoordinate({ { libCZI::DimensionIndex::S, scene_index_ } });
}
SubblockSortable subblocksToFind(&scene_coord, -1, false);
SubblockIndexVec matches = getMatches(subblocksToFind);

Expand Down
16 changes: 7 additions & 9 deletions _aicspylibczi/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class Reader
ImagesContainerBase::ImagesContainerBasePtr readMosaic(libCZI::CDimCoordinate plane_coord_,
float scale_factor_ = 1.0,
libCZI::IntRect im_box_ = { 0, 0, -1, -1 },
libCZI::RgbFloatColor backGroundColor_ = { 0.0, 0.0, 0.0 });
libCZI::RgbFloatColor backGroundColor_ = { 0.0, 0.0, 0.0 });

/*!
* Convert the libCZI::DimensionIndex to a character
Expand All @@ -261,7 +261,6 @@ class Reader

virtual ~Reader() { m_czireader->Close(); }


/*!
* @brief get the shape of the loaded images
* @param images_ the ImageVector of images to get the shape of
Expand Down Expand Up @@ -347,15 +346,15 @@ class Reader
void checkSceneShapes();

/*!
* @brief get the pyramid 0 (acquired data) shape
* @param scene_index_ specifies scene but defaults to the first scene,
* Scenes can have different sizes
* @return std::vector<libCZI::IntRect> containing (x0, y0, w, h)
*/
* @brief get the pyramid 0 (acquired data) shape
* @param scene_index_ specifies scene but defaults to the first scene,
* Scenes can have different sizes
* @return std::vector<libCZI::IntRect> containing (x0, y0, w, h)
*/
libCZI::IntRect getSceneYXSize(int scene_index_ = -1)
{
std::vector<libCZI::IntRect> matches = getAllSceneYXSize(scene_index_);
return matches.empty() ? libCZI::IntRect{ 0,0,0,0 } : matches.front();
return matches.empty() ? libCZI::IntRect{ 0, 0, 0, 0 } : matches.front();
}

/*!
Expand All @@ -368,7 +367,6 @@ class Reader
std::vector<libCZI::IntRect> getAllSceneYXSize(int scene_index_ = -1, bool get_all_matches_ = false);

libCZI::PixelType getFirstPixelType();

};

}
Expand Down
12 changes: 10 additions & 2 deletions aicspylibczi/CziFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,13 @@ def read_image(self, **kwargs):
image, shape = self.reader.read_selected(plane_constraints, m_index, cores)
return image, shape

def read_mosaic(self, region: Tuple = None, scale_factor: float = 1.0, background_color: Tuple = None, **kwargs):
def read_mosaic(
self,
region: Tuple = None,
scale_factor: float = 1.0,
background_color: Tuple = None,
**kwargs,
):
"""
Reads a mosaic file and returns an image corresponding to the specified dimensions. If the file is more than
a 2D sheet of pixels, meaning only one channel, z-slice, time-index, etc then the kwargs must specify the
Expand Down Expand Up @@ -619,7 +625,9 @@ def read_mosaic(self, region: Tuple = None, scale_factor: float = 1.0, backgroun
tmp.b = background_color[2]
background_color = tmp

img = self.reader.read_mosaic(plane_constraints, scale_factor, region, background_color)
img = self.reader.read_mosaic(
plane_constraints, scale_factor, region, background_color
)

return img

Expand Down

0 comments on commit c58b470

Please sign in to comment.