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

[image] New image cache #1310

Merged
merged 28 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
77b5497
[image] utility to log OIIO shared img cache info
mugulmd Dec 6, 2022
c3973ff
[image] image caching mechanism + sample application + unit test
mugulmd Dec 6, 2022
43bdfe8
[image] add mutex for thread-safe code + option for multithread in sa…
mugulmd Dec 6, 2022
e0b2293
[imageCache] use mutable keyword for mutex
mugulmd Dec 7, 2022
110aa18
[imageCache] support all pixel types for cached images
mugulmd Dec 8, 2022
d5901d3
[imageCache] usage statistics
mugulmd Dec 8, 2022
b0f70a2
[imageCache] load new image: use cached image with higher scale when …
mugulmd Dec 8, 2022
23923db
[imageCache] include last write time of image on disk in cache key
mugulmd Dec 9, 2022
3d1ab02
[imageCache] add unit tests
mugulmd Dec 9, 2022
ad287ea
[samples] add command-line arguments to image caching sample app
mugulmd Dec 9, 2022
4f7f411
[imageCache] using resize from imageAlgo instead of downscaleImage fo…
mugulmd Dec 14, 2022
972e47f
[image] downscaling dichotomy: image::downscaleImage for custom sampl…
mugulmd Dec 14, 2022
9ebde4f
[imageCache] documented caching mechanism
mugulmd Dec 14, 2022
adc8e47
[imageCache] fix: use last write time when searching high res version…
mugulmd Dec 14, 2022
1fffb23
[image] typo fixes
fabiencastan Feb 8, 2023
25537c7
[image] add dedicated functions for rescaling in-place
mugulmd Feb 13, 2023
61e7b6d
[imageCache] cosmetic change in member variable initialization
mugulmd Feb 15, 2023
278e086
[image] linux build fix
fabiencastan Feb 15, 2023
201c96b
[image] rename caching to imageCaching
mugulmd Feb 27, 2023
fad85f0
[image] using unsigned long long int type for memory size
mugulmd Feb 27, 2023
d9217aa
[imageCaching] use trace logs instead of debug logs
mugulmd Feb 27, 2023
f7e476e
[imageCaching] use MiB (mebibytes) instead of MB (megabytes)
mugulmd Feb 27, 2023
ed8cdae
minor cosmetic changes
mugulmd Feb 27, 2023
5acb92a
[imageCaching] support all downscale levels instead of just powers of…
mugulmd Mar 2, 2023
2a2436d
[image] renamed imageCaching to ImageCache
mugulmd Mar 9, 2023
82c8c3f
[image] throw error when resizing image with downscale smaller than 1
mugulmd Mar 9, 2023
ef40139
[image] ImageCache: default downscale value
mugulmd Mar 13, 2023
1e1c7cd
[image] imageCaching: update unit tests
mugulmd Mar 13, 2023
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
13 changes: 8 additions & 5 deletions src/aliceVision/image/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(image_files_headers
Rgb.hpp
Sampler.hpp
cache.hpp
ImageCache.hpp
)

# Sources
Expand All @@ -32,6 +33,7 @@ set(image_files_sources
io.cpp
imageAlgo.cpp
cache.cpp
ImageCache.cpp
)

alicevision_add_library(aliceVision_image
Expand All @@ -52,8 +54,9 @@ alicevision_add_library(aliceVision_image
install(FILES ./share/aliceVision/config.ocio DESTINATION ${CMAKE_INSTALL_DATADIR}/aliceVision)

# Unit tests
alicevision_add_test(image_test.cpp NAME "image" LINKS aliceVision_image)
alicevision_add_test(io_test.cpp NAME "image_io" LINKS aliceVision_image)
alicevision_add_test(drawing_test.cpp NAME "image_drawing" LINKS aliceVision_image)
alicevision_add_test(filtering_test.cpp NAME "image_filtering" LINKS aliceVision_image)
alicevision_add_test(resampling_test.cpp NAME "image_resampling" LINKS aliceVision_image)
alicevision_add_test(image_test.cpp NAME "image" LINKS aliceVision_image)
alicevision_add_test(io_test.cpp NAME "image_io" LINKS aliceVision_image)
alicevision_add_test(drawing_test.cpp NAME "image_drawing" LINKS aliceVision_image)
alicevision_add_test(filtering_test.cpp NAME "image_filtering" LINKS aliceVision_image)
alicevision_add_test(resampling_test.cpp NAME "image_resampling" LINKS aliceVision_image)
alicevision_add_test(imageCaching_test.cpp NAME "image_caching" LINKS aliceVision_image)
12 changes: 12 additions & 0 deletions src/aliceVision/image/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ namespace aliceVision
return sizeof( Tpixel );
}

/**
* @brief Retrieve the size in byte of the image
* @return size of the image (in byte)
* @note We use unsigned long long integers to avoid issues with large images, which can exceed several GB.
*/
inline unsigned long long int MemorySize() const
{
return static_cast<unsigned long long int>(Width()) *
static_cast<unsigned long long int>(Height()) *
static_cast<unsigned long long int>(Depth());
}


/**
* @brief Return the number of channels
Expand Down
161 changes: 161 additions & 0 deletions src/aliceVision/image/ImageCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// This file is part of the AliceVision project.
// Copyright (c) 2022 AliceVision contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "ImageCache.hpp"

#include <aliceVision/system/Logger.hpp>


namespace aliceVision {
namespace image {

CacheValue::CacheValue()
{
}

CacheValue CacheValue::wrap(std::shared_ptr<Image<unsigned char>> img)
{
CacheValue value;
value.imgUChar = img;
return value;
}

CacheValue CacheValue::wrap(std::shared_ptr<Image<float>> img)
{
CacheValue value;
value.imgFloat = img;
return value;
}

CacheValue CacheValue::wrap(std::shared_ptr<Image<RGBColor>> img)
{
CacheValue value;
value.imgRGB = img;
return value;
}

CacheValue CacheValue::wrap(std::shared_ptr<Image<RGBfColor>> img)
{
CacheValue value;
value.imgRGBf = img;
return value;
}

CacheValue CacheValue::wrap(std::shared_ptr<Image<RGBAColor>> img)
{
CacheValue value;
value.imgRGBA = img;
return value;
}

CacheValue CacheValue::wrap(std::shared_ptr<Image<RGBAfColor>> img)
{
CacheValue value;
value.imgRGBAf = img;
return value;
}

int CacheValue::useCount() const
{
if (imgUChar)
{
return imgUChar.use_count();
}
if (imgFloat)
{
return imgFloat.use_count();
}
if (imgRGB)
{
return imgRGB.use_count();
}
if (imgRGBf)
{
return imgRGBf.use_count();
}
if (imgRGBA)
{
return imgRGBA.use_count();
}
if (imgRGBAf)
{
return imgRGBAf.use_count();
}
return 0;
}

unsigned long long int CacheValue::memorySize() const
{
if (imgUChar)
{
return imgUChar->MemorySize();
}
if (imgFloat)
{
return imgFloat->MemorySize();
}
if (imgRGB)
{
return imgRGB->MemorySize();
}
if (imgRGBf)
{
return imgRGBf->MemorySize();
}
if (imgRGBA)
{
return imgRGBA->MemorySize();
}
if (imgRGBAf)
{
return imgRGBAf->MemorySize();
}
return 0;
}

ImageCache::ImageCache(float capacity_MiB, float maxSize_MiB, const ImageReadOptions& options) :
_info(capacity_MiB, maxSize_MiB),
_options(options)
{
}

ImageCache::~ImageCache()
{
}

std::string ImageCache::toString() const
{
std::string description = "Image cache content (LRU to MRU): ";

for (const CacheKey& key : _keys)
{
std::string keyDesc = key.filename +
", nbChannels: " + std::to_string(key.nbChannels) +
", typeDesc: " + std::to_string(key.typeDesc) +
", downscaleLevel: " + std::to_string(key.downscaleLevel) +
", usages: " + std::to_string(_imagePtrs.at(key).useCount()) +
", size: " + std::to_string(_imagePtrs.at(key).memorySize());
description += "\n * " + keyDesc;
}

std::string memUsageDesc = "\nMemory usage: "
"\n * capacity: " + std::to_string(_info.capacity) +
Copy link
Member

Choose a reason for hiding this comment

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

Need a dedicated function to print in a human readable format, to print with MB, GB, etc depending on the values.

"\n * max size: " + std::to_string(_info.maxSize) +
"\n * nb images: " + std::to_string(_info.nbImages) +
"\n * content size: " + std::to_string(_info.contentSize);
description += memUsageDesc;

std::string statsDesc = "\nUsage statistics: "
"\n * nb load from disk: " + std::to_string(_info.nbLoadFromDisk) +
"\n * nb load from cache: " + std::to_string(_info.nbLoadFromCache) +
"\n * nb remove unused: " + std::to_string(_info.nbRemoveUnused);
description += statsDesc;

return description;
}

} // namespace image
} // namespace aliceVision
Loading