Skip to content

Commit

Permalink
PSD - use byte counts for scan lines
Browse files Browse the repository at this point in the history
Some PSD files rely on the byte counts for each scan line to decode file properly

Merge PR #312 by https://github.com/qbnu
  • Loading branch information
sylikc committed Jun 21, 2024
2 parents 37479d8 + eab4a2b commit b1763e5
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/JPEGView/PSDWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ CJPEGImage* PsdReader::ReadImage(LPCTSTR strFileName, bool& bOutOfMemory)
if ((double)nHeight * nWidth > MAX_IMAGE_PIXELS) {
bOutOfMemory = true;
}
ThrowIf(bOutOfMemory || nHeight > MAX_IMAGE_DIMENSION || nWidth > MAX_IMAGE_DIMENSION);
ThrowIf(bOutOfMemory || max(nHeight, nWidth) > MAX_IMAGE_DIMENSION || !min(nHeight, nWidth));

// PSD can have bit depths of 1, 2, 4, 8, 16, 32
unsigned short nBitDepth = ReadUShortFromFile(hFile);
Expand Down Expand Up @@ -301,6 +301,7 @@ CJPEGImage* PsdReader::ReadImage(LPCTSTR strFileName, bool& bOutOfMemory)
if (nCompressionMethod == COMPRESSION_RLE) {
// Skip byte counts for scanlines
p += nHeight * nRealChannels * 2;
unsigned char* pOffset = p;
for (unsigned channel = 0; channel < nChannels; channel++) {
unsigned rchannel;
if (nColorMode == MODE_Lab) {
Expand All @@ -309,6 +310,8 @@ CJPEGImage* PsdReader::ReadImage(LPCTSTR strFileName, bool& bOutOfMemory)
rchannel = (-channel - 2) % nChannels;
}
for (unsigned row = 0; row < nHeight; row++) {
p = pOffset;

for (unsigned count = 0; count < nWidth; ) {
unsigned char c;
ThrowIf(p >= (unsigned char*)pBuffer + nImageDataSize);
Expand Down Expand Up @@ -345,6 +348,15 @@ CJPEGImage* PsdReader::ReadImage(LPCTSTR strFileName, bool& bOutOfMemory)

count += c;
}

pOffset += _byteswap_ushort(*(unsigned short*)(pBuffer + (channel * nHeight + row) * 2));
#ifdef DEBUG
if (p != pOffset) {
WCHAR buf[100];
swprintf(buf, _T("Misaligned scan line bytes (%+d) for channel %d row %d\n"), p - pOffset, channel, row);
::OutputDebugString(buf);
}
#endif
}
}
} else { // No compression
Expand Down

0 comments on commit b1763e5

Please sign in to comment.