Skip to content

Commit 62db2d0

Browse files
committed
add overlay cleanup for 32bits
1 parent 5a2016f commit 62db2d0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx

+45
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,51 @@ bool ImageCodec::DoOverlayCleanup(std::istream &is, std::ostream &os)
581581
}
582582
}
583583
}
584+
else if (PF.GetBitsAllocated() == 32) {
585+
// pmask : to mask the 'unused bits' (may contain overlays)
586+
uint16_t pmask = 0xffff;
587+
pmask = (uint16_t)(pmask >> (PF.GetBitsAllocated() - PF.GetBitsStored()));
588+
589+
if (PF.GetPixelRepresentation()) {
590+
// smask : to check the 'sign' when BitsStored != BitsAllocated
591+
uint16_t smask = 0x0001;
592+
smask = (uint16_t)(smask << (16 - (PF.GetBitsAllocated() -
593+
PF.GetBitsStored() + 1)));
594+
// nmask : to propagate sign bit on negative values
595+
int16_t nmask = (int16_t)0x8000;
596+
nmask =
597+
(int16_t)(nmask >> (PF.GetBitsAllocated() - PF.GetBitsStored() - 1));
598+
599+
uint16_t c;
600+
while (is.read((char *)&c, 2)) {
601+
c = (uint16_t)(c >> (PF.GetBitsStored() - PF.GetHighBit() - 1));
602+
if (c & smask) {
603+
c = (uint16_t)(c | nmask);
604+
} else {
605+
c = c & pmask;
606+
}
607+
os.write((char *)&c, 2);
608+
}
609+
} else // Pixel are unsigned
610+
{
611+
// On Windows, is.read and os.write are expensive operations.
612+
// If we called it for each value then conversion would be extremely slow.
613+
// Therefore we read/mask/write 1024 values at once.
614+
const unsigned int bufferSize = 1024;
615+
std::vector<uint16_t> buffer(bufferSize);
616+
while (is) {
617+
is.read((char *)buffer.data(), bufferSize * sizeof(uint16_t));
618+
std::streamsize bytesRead = is.gcount();
619+
std::vector<uint16_t>::iterator validBufferEnd =
620+
buffer.begin() + bytesRead / sizeof(uint16_t);
621+
for (std::vector<uint16_t>::iterator it = buffer.begin();
622+
it != validBufferEnd; ++it) {
623+
*it = ((*it >> (PF.GetBitsStored() - PF.GetHighBit() - 1)) & pmask);
624+
}
625+
os.write((char *)buffer.data(), bytesRead);
626+
}
627+
}
628+
}
584629
else
585630
{
586631
assert(0); // TODO

0 commit comments

Comments
 (0)