23inline uint32_t read_le32(
const uint8_t* data) {
24 return static_cast<uint32_t
>(data[0]) |
25 (
static_cast<uint32_t
>(data[1]) << 8) |
26 (
static_cast<uint32_t
>(data[2]) << 16) |
27 (
static_cast<uint32_t
>(data[3]) << 24);
33inline void write_le32(uint8_t* data, uint32_t value) {
34 data[0] =
static_cast<uint8_t
>(value & 0xFF);
35 data[1] =
static_cast<uint8_t
>((value >> 8) & 0xFF);
36 data[2] =
static_cast<uint8_t
>((value >> 16) & 0xFF);
37 data[3] =
static_cast<uint8_t
>((value >> 24) & 0xFF);
52void encode_rle_segment(std::span<const uint8_t> input, std::vector<uint8_t>& output) {
58 const size_t size = input.size();
62 uint8_t current = input[pos];
63 size_t run_length = 1;
65 while (pos + run_length < size &&
66 input[pos + run_length] == current &&
71 if (run_length >= 3) {
74 output.push_back(
static_cast<uint8_t
>(1 -
static_cast<int>(run_length)));
75 output.push_back(current);
79 std::vector<uint8_t> literal;
82 while (pos < size && literal.size() < 128) {
85 while (pos + ahead_run < size &&
86 input[pos + ahead_run] == input[pos] &&
96 literal.push_back(input[pos]);
100 if (!literal.empty()) {
102 output.push_back(
static_cast<uint8_t
>(literal.size() - 1));
103 output.insert(output.end(), literal.begin(), literal.end());
117std::vector<uint8_t> decode_rle_segment(std::span<const uint8_t> input,
size_t expected_size) {
118 std::vector<uint8_t> output;
119 output.reserve(expected_size);
122 const size_t size = input.size();
124 while (pos < size && output.size() < expected_size) {
125 auto control =
static_cast<int8_t
>(input[pos]);
130 size_t count =
static_cast<size_t>(
control) + 1;
131 if (pos + count > size) {
132 throw std::runtime_error(
"RLE decode: insufficient literal data");
134 output.insert(output.end(), input.begin() + pos, input.begin() + pos + count);
136 }
else if (control != -128) {
139 throw std::runtime_error(
"RLE decode: missing replicate byte");
141 size_t count =
static_cast<size_t>(1 -
control);
142 uint8_t value = input[pos];
144 for (
size_t i = 0; i < count && output.size() < expected_size; ++i) {
145 output.push_back(value);
163int calculate_segment_count(
const image_params& params) {
164 int bytes_per_sample = (params.bits_allocated + 7) / 8;
165 return params.samples_per_pixel * bytes_per_sample;
178 std::span<const uint8_t> pixel_data,
182 if (pixel_data.empty()) {
188 "Invalid parameters for RLE: requires 8/16-bit, 1-3 samples per pixel");
192 if (pixel_data.size() != expected_size) {
194 "Pixel data size mismatch: expected " + std::to_string(expected_size) +
195 ", got " + std::to_string(pixel_data.size()));
200 }
catch (
const std::exception& e) {
206 std::span<const uint8_t> compressed_data,
209 if (compressed_data.empty()) {
219 }
catch (
const std::exception& e) {
227 if (params.bits_allocated != 8 && params.bits_allocated != 16) {
231 if (params.samples_per_pixel < 1 || params.samples_per_pixel > 3) {
235 int num_segments = calculate_segment_count(params);
240 if (params.width == 0 || params.height == 0) {
247 std::span<const uint8_t> pixel_data,
250 int num_segments = calculate_segment_count(params);
251 size_t pixels_per_frame =
static_cast<size_t>(params.
width) * params.
height;
255 std::vector<std::vector<uint8_t>> segments(num_segments);
256 for (
auto& seg : segments) {
257 seg.reserve(pixels_per_frame);
265 if (bytes_per_sample == 1) {
269 segments[0].assign(pixel_data.begin(), pixel_data.end());
272 segments[0].resize(pixels_per_frame);
273 segments[1].resize(pixels_per_frame);
274 segments[2].resize(pixels_per_frame);
283 for (
size_t i = 0; i < pixels_per_frame; ++i) {
294 segments[0].resize(pixels_per_frame);
295 segments[1].resize(pixels_per_frame);
304 std::vector<uint8_t> r_plane(pixels_per_frame * 2);
305 std::vector<uint8_t> g_plane(pixels_per_frame * 2);
306 std::vector<uint8_t> b_plane(pixels_per_frame * 2);
309 for (
size_t i = 0; i < pixels_per_frame; ++i) {
310 size_t src_idx = i * 6;
311 size_t dst_idx = i * 2;
312 r_plane[dst_idx] = pixel_data[src_idx];
313 r_plane[dst_idx + 1] = pixel_data[src_idx + 1];
314 g_plane[dst_idx] = pixel_data[src_idx + 2];
315 g_plane[dst_idx + 1] = pixel_data[src_idx + 3];
316 b_plane[dst_idx] = pixel_data[src_idx + 4];
317 b_plane[dst_idx + 1] = pixel_data[src_idx + 5];
321 segments[0].resize(pixels_per_frame);
322 segments[1].resize(pixels_per_frame);
323 segments[2].resize(pixels_per_frame);
324 segments[3].resize(pixels_per_frame);
325 segments[4].resize(pixels_per_frame);
326 segments[5].resize(pixels_per_frame);
329 segments[1].data(), pixels_per_frame);
331 segments[3].data(), pixels_per_frame);
333 segments[5].data(), pixels_per_frame);
336 for (
size_t i = 0; i < pixels_per_frame; ++i) {
339 segments[s * 2].push_back(pixel_data[idx + 1]);
340 segments[s * 2 + 1].push_back(pixel_data[idx]);
347 std::vector<std::vector<uint8_t>> encoded_segments(num_segments);
348 for (
int i = 0; i < num_segments; ++i) {
349 encode_rle_segment(segments[i], encoded_segments[i]);
353 std::vector<uint8_t> output;
357 for (
const auto& seg : encoded_segments) {
358 total_size += seg.size();
359 if (seg.size() % 2 != 0) {
363 output.reserve(total_size);
369 write_le32(output.data(),
static_cast<uint32_t
>(num_segments));
373 for (
int i = 0; i < num_segments; ++i) {
374 write_le32(output.data() + 4 + i * 4, current_offset);
375 current_offset +=
static_cast<uint32_t
>(encoded_segments[i].size());
376 if (encoded_segments[i].size() % 2 != 0) {
382 for (
const auto& seg : encoded_segments) {
383 output.insert(output.end(), seg.begin(), seg.end());
384 if (seg.size() % 2 != 0) {
390 return kcenon::pacs::ok<compression_result>(
compression_result{std::move(output), output_params});
394 std::span<const uint8_t> compressed_data,
397 const uint8_t* header = compressed_data.data();
400 uint32_t num_segments = read_le32(header);
403 "Invalid RLE segment count: " + std::to_string(num_segments));
406 int expected_segments = calculate_segment_count(params);
407 if (
static_cast<int>(num_segments) != expected_segments) {
409 "Segment count mismatch: expected " + std::to_string(expected_segments) +
410 ", got " + std::to_string(num_segments));
414 std::vector<uint32_t> offsets(num_segments);
415 for (uint32_t i = 0; i < num_segments; ++i) {
416 offsets[i] = read_le32(header + 4 + i * 4);
417 if (offsets[i] >= compressed_data.size()) {
419 "Invalid segment offset: " + std::to_string(offsets[i]));
424 std::vector<size_t> sizes(num_segments);
425 for (uint32_t i = 0; i < num_segments; ++i) {
426 if (i + 1 < num_segments) {
427 sizes[i] = offsets[i + 1] - offsets[i];
429 sizes[i] = compressed_data.size() - offsets[i];
433 size_t pixels_per_frame =
static_cast<size_t>(params.
width) * params.
height;
437 std::vector<std::vector<uint8_t>> decoded_segments(num_segments);
438 for (uint32_t i = 0; i < num_segments; ++i) {
439 std::span<const uint8_t> segment_data(
440 compressed_data.data() + offsets[i], sizes[i]);
441 decoded_segments[i] = decode_rle_segment(segment_data, pixels_per_frame);
443 if (decoded_segments[i].size() != pixels_per_frame) {
445 "Segment " + std::to_string(i) +
" decoded size mismatch: expected " +
446 std::to_string(pixels_per_frame) +
", got " +
447 std::to_string(decoded_segments[i].size()));
452 size_t output_size = pixels_per_frame * params.
samples_per_pixel * bytes_per_sample;
453 std::vector<uint8_t> output(output_size);
455 if (bytes_per_sample == 1) {
459 output = std::move(decoded_segments[0]);
463 decoded_segments[0].data(),
464 decoded_segments[1].data(),
465 decoded_segments[2].data(),
470 for (
size_t i = 0; i < pixels_per_frame; ++i) {
481 decoded_segments[0].data(),
482 decoded_segments[1].data(),
487 std::vector<uint8_t> r_plane(pixels_per_frame * 2);
488 std::vector<uint8_t> g_plane(pixels_per_frame * 2);
489 std::vector<uint8_t> b_plane(pixels_per_frame * 2);
493 decoded_segments[1].data(),
494 r_plane.data(), pixels_per_frame);
496 decoded_segments[3].data(),
497 g_plane.data(), pixels_per_frame);
499 decoded_segments[5].data(),
500 b_plane.data(), pixels_per_frame);
503 for (
size_t i = 0; i < pixels_per_frame; ++i) {
504 size_t src_idx = i * 2;
505 size_t dst_idx = i * 6;
506 output[dst_idx] = r_plane[src_idx];
507 output[dst_idx + 1] = r_plane[src_idx + 1];
508 output[dst_idx + 2] = g_plane[src_idx];
509 output[dst_idx + 3] = g_plane[src_idx + 1];
510 output[dst_idx + 4] = b_plane[src_idx];
511 output[dst_idx + 5] = b_plane[src_idx + 1];
515 for (
size_t i = 0; i < pixels_per_frame; ++i) {
518 output[idx] = decoded_segments[s * 2 + 1][i];
519 output[idx + 1] = decoded_segments[s * 2][i];
537 return kcenon::pacs::ok<compression_result>(
compression_result{std::move(output), output_params});
551std::string_view
rle_codec::transfer_syntax_uid() const noexcept {
552 return kTransferSyntaxUID;
556 return "RLE Lossless";
565 if (params.bits_allocated != 8 && params.bits_allocated != 16) {
569 if (params.samples_per_pixel < 1 || params.samples_per_pixel > 3) {
573 int bytes_per_sample = (params.bits_allocated + 7) / 8;
574 int num_segments = params.samples_per_pixel * bytes_per_sample;
575 if (num_segments > kMaxSegments) {
579 if (params.width == 0 || params.height == 0) {
588 if (params.bits_allocated != 0 &&
589 params.bits_allocated != 8 &&
590 params.bits_allocated != 16) {
593 if (params.samples_per_pixel != 0 &&
594 (params.samples_per_pixel < 1 || params.samples_per_pixel > 3)) {
601 std::span<const uint8_t> pixel_data,
608 std::span<const uint8_t> compressed_data,
PIMPL implementation for rle_codec.
codec_result decode_frame(std::span< const uint8_t > compressed_data, const image_params ¶ms) const
codec_result encode(std::span< const uint8_t > pixel_data, const image_params ¶ms, const compression_options &options) const
codec_result decode(std::span< const uint8_t > compressed_data, const image_params ¶ms) const
bool valid_for_rle(const image_params ¶ms) const noexcept
codec_result encode_frame(std::span< const uint8_t > pixel_data, const image_params ¶ms) const
DICOM RLE Lossless codec implementation.
std::string_view name() const noexcept override
Returns a human-readable name for the codec.
bool can_encode(const image_params ¶ms) const noexcept override
Checks if this codec supports the given image parameters.
rle_codec()
Constructs an RLE codec instance.
codec_result decode(std::span< const uint8_t > compressed_data, const image_params ¶ms) const override
Decompresses RLE data.
static constexpr size_t kRLEHeaderSize
RLE header size (64 bytes: 16 x 4-byte offsets)
codec_result encode(std::span< const uint8_t > pixel_data, const image_params ¶ms, const compression_options &options={}) const override
Compresses pixel data to RLE format.
std::unique_ptr< impl > impl_
bool can_decode(const image_params ¶ms) const noexcept override
Checks if this codec can decode data with given parameters.
static constexpr int kMaxSegments
Maximum number of RLE segments allowed by DICOM specification.
bool is_lossy() const noexcept override
Checks if this codec produces lossy compression.
void merge_planes_to_16bit(const uint8_t *high, const uint8_t *low, uint8_t *dst, size_t pixel_count) noexcept
Merge high and low byte planes into 16-bit data.
void interleaved_to_planar_rgb8(const uint8_t *src, uint8_t *r, uint8_t *g, uint8_t *b, size_t pixel_count) noexcept
Convert interleaved RGB to planar format using best available SIMD.
void planar_to_interleaved_rgb8(const uint8_t *r, const uint8_t *g, const uint8_t *b, uint8_t *dst, size_t pixel_count) noexcept
Convert planar RGB to interleaved format using best available SIMD.
void split_16bit_to_planes(const uint8_t *src, uint8_t *high, uint8_t *low, size_t pixel_count) noexcept
Split 16-bit data into high and low byte planes.
constexpr int decompression_error
@ control
Internal pipeline control messages.
Result< T > pacs_error(int code, const std::string &message, const std::string &details="")
Create a PACS error result with module context.
Result<T> type aliases and helpers for PACS system.
SIMD optimizations for RLE codec operations.
Compression quality settings for lossy codecs.
Successful result of a compression/decompression operation.
Parameters describing image pixel data.
uint16_t samples_per_pixel
Number of samples per pixel (0028,0002) 1 for grayscale, 3 for color.
uint16_t bits_allocated
Bits allocated per pixel sample (0028,0100) Valid values: 8, 16.
uint16_t height
Image height in pixels (Rows - 0028,0010)
size_t frame_size_bytes() const noexcept
Calculates the size of uncompressed pixel data in bytes.
photometric_interpretation photometric
Photometric interpretation (0028,0004)
uint16_t planar_configuration
Planar configuration (0028,0006) 0 = interleaved (R1G1B1R2G2B2...), 1 = separate planes (RRR....
uint16_t pixel_representation
Pixel representation (0028,0103) 0 = unsigned, 1 = signed.
uint16_t width
Image width in pixels (Columns - 0028,0011)
uint16_t bits_stored
Bits stored per pixel sample (0028,0101) Must be <= bits_allocated.
uint16_t high_bit
High bit position (0028,0102) Typically bits_stored - 1.