PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
thumbnail_service.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
19#pragma once
20
21#include <chrono>
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <mutex>
26#include <optional>
27#include <shared_mutex>
28#include <string>
29#include <string_view>
30#include <unordered_map>
31#include <vector>
32
33namespace kcenon::pacs::storage {
34class index_database;
35} // namespace kcenon::pacs::storage
36
37namespace kcenon::pacs::web {
38
44 uint16_t size{128};
45
47 std::string format{"jpeg"};
48
50 int quality{60};
51
53 uint32_t frame{1};
54};
55
61 std::vector<uint8_t> data;
62
64 std::string content_type;
65
67 std::chrono::system_clock::time_point created_at;
68
70 std::chrono::system_clock::time_point last_accessed;
71};
72
78 bool success{false};
79
81 std::string error_message;
82
85
89 r.success = true;
90 r.entry = std::move(entry);
91 return r;
92 }
93
95 static thumbnail_result error(std::string message) {
97 r.success = false;
98 r.error_message = std::move(message);
99 return r;
100 }
101};
102
126public:
131 explicit thumbnail_service(
132 std::shared_ptr<storage::index_database> database);
133
136
142
143 // =========================================================================
144 // Thumbnail Generation
145 // =========================================================================
146
153 [[nodiscard]] thumbnail_result get_thumbnail(
154 std::string_view sop_instance_uid,
155 const thumbnail_params& params);
156
166 std::string_view series_uid,
167 const thumbnail_params& params);
168
178 std::string_view study_uid,
179 const thumbnail_params& params);
180
181 // =========================================================================
182 // Cache Management
183 // =========================================================================
184
188 void clear_cache();
189
194 void clear_cache(std::string_view sop_instance_uid);
195
200 [[nodiscard]] size_t cache_size() const;
201
206 [[nodiscard]] size_t cache_entry_count() const;
207
212 void set_max_cache_size(size_t max_bytes);
213
218 [[nodiscard]] size_t max_cache_size() const;
219
220private:
222 struct cache_key {
223 std::string uid;
224 uint16_t size;
225 std::string format;
227 uint32_t frame;
228
229 bool operator==(const cache_key&) const = default;
230 };
231
234 size_t operator()(const cache_key& k) const {
235 size_t h = std::hash<std::string>{}(k.uid);
236 h ^= std::hash<uint16_t>{}(k.size) << 1;
237 h ^= std::hash<std::string>{}(k.format) << 2;
238 h ^= std::hash<int>{}(k.quality) << 3;
239 h ^= std::hash<uint32_t>{}(k.frame) << 4;
240 return h;
241 }
242 };
243
245 std::shared_ptr<storage::index_database> database_;
246
248 std::unordered_map<cache_key, thumbnail_cache_entry, cache_key_hash> cache_;
249
251 mutable std::shared_mutex cache_mutex_;
252
255
257 size_t max_cache_size_{64 * 1024 * 1024};
258
259 // =========================================================================
260 // Internal Methods
261 // =========================================================================
262
269 [[nodiscard]] std::vector<uint8_t> generate_thumbnail(
270 std::string_view file_path,
271 const thumbnail_params& params);
272
278 [[nodiscard]] std::optional<std::string> select_representative_instance(
279 std::string_view series_uid);
280
286 [[nodiscard]] std::optional<std::string> select_representative_series(
287 std::string_view study_uid);
288
292 void evict_lru();
293
299 [[nodiscard]] static std::string get_content_type(std::string_view format);
300};
301
302} // namespace kcenon::pacs::web
Thumbnail generation and caching service.
thumbnail_service(const thumbnail_service &)=delete
Non-copyable, non-movable (due to shared_mutex member)
thumbnail_service(thumbnail_service &&)=delete
std::shared_ptr< storage::index_database > database_
Database for instance lookups.
thumbnail_service & operator=(const thumbnail_service &)=delete
thumbnail_result get_thumbnail(std::string_view sop_instance_uid, const thumbnail_params &params)
Get or generate thumbnail for a specific instance.
void evict_lru()
Evict least recently used entries to make room.
thumbnail_service(std::shared_ptr< storage::index_database > database)
Construct thumbnail service with database.
std::vector< uint8_t > generate_thumbnail(std::string_view file_path, const thumbnail_params &params)
Generate thumbnail from DICOM file.
thumbnail_result get_series_thumbnail(std::string_view series_uid, const thumbnail_params &params)
Get thumbnail for a series (representative image)
void set_max_cache_size(size_t max_bytes)
Set maximum cache size.
size_t cache_entry_count() const
Get number of cached entries.
static std::string get_content_type(std::string_view format)
Get MIME type for format.
size_t current_cache_size_
Current cache size in bytes.
size_t max_cache_size_
Maximum cache size (default: 64MB)
size_t cache_size() const
Get current cache size in bytes.
thumbnail_result get_study_thumbnail(std::string_view study_uid, const thumbnail_params &params)
Get thumbnail for a study (representative image)
size_t max_cache_size() const
Get maximum cache size.
std::optional< std::string > select_representative_series(std::string_view study_uid)
Select representative series from study.
std::optional< std::string > select_representative_instance(std::string_view series_uid)
Select representative instance from series.
thumbnail_service & operator=(thumbnail_service &&)=delete
std::unordered_map< cache_key, thumbnail_cache_entry, cache_key_hash > cache_
Thumbnail cache.
void clear_cache()
Clear all cached thumbnails.
std::shared_mutex cache_mutex_
Cache mutex.
Cached thumbnail entry.
std::vector< uint8_t > data
Compressed image data.
std::chrono::system_clock::time_point created_at
When the entry was created.
std::chrono::system_clock::time_point last_accessed
When the entry was last accessed.
std::string content_type
MIME content type.
Parameters for thumbnail generation.
uint16_t size
Output size in pixels (64, 128, 256, 512)
uint32_t frame
Frame number for multi-frame images (1-indexed)
int quality
Quality for lossy compression (1-100)
Result type for thumbnail operations.
thumbnail_cache_entry entry
Thumbnail data if succeeded.
std::string error_message
Error message if failed.
bool success
Whether the operation succeeded.
static thumbnail_result ok(thumbnail_cache_entry entry)
Create a success result.
static thumbnail_result error(std::string message)
Create an error result.
bool operator==(const cache_key &) const =default