diff --git a/Frameworks/include/CGIWICBitmap.h b/Frameworks/include/CGIWICBitmap.h index c04d6f8569..4d75fdbeb3 100644 --- a/Frameworks/include/CGIWICBitmap.h +++ b/Frameworks/include/CGIWICBitmap.h @@ -152,7 +152,7 @@ class CGIWICBitmap : public Microsoft::WRL::RuntimeClass lock; - RETURN_IF_FAILED(Lock(copyRect, 0, &lock)); + // TODO #1379: Support sub-regional locking. + RETURN_IF_FAILED(Lock(&fullRect, 0, &lock)); + if (!copyRect) { + copyRect = &fullRect; + } + + UINT sourceDataStride; UINT sourceDataSize; BYTE* sourceData; + RETURN_IF_FAILED(lock->GetStride(&sourceDataStride)); RETURN_IF_FAILED(lock->GetDataPointer(&sourceDataSize, &sourceData)); - RETURN_HR_IF(E_INVALIDARG, sourceDataSize > bufferSize); - - // TODO #1379 - should support regional copying. - RETURN_HR_IF(E_UNEXPECTED, memcpy_s(buffer, bufferSize, sourceData, sourceDataSize) != 0); + if (copyRect->X == 0 && copyRect->Y == 0 && copyRect->Width == m_width && copyRect->Height == m_height) { + RETURN_HR_IF(E_INVALIDARG, sourceDataSize > bufferSize); + RETURN_HR_IF(E_UNEXPECTED, memcpy_s(buffer, bufferSize, sourceData, sourceDataSize) != 0); + } else { + // Once we support sub-regional locking we can fix this stride copier. + size_t bytesPerPixel = sourceDataStride / fullRect.Width; + ptrdiff_t xOffBytes = copyRect->X * bytesPerPixel; + for (off_t i = 0, j = copyRect->Y; i < copyRect->Height; ++i, ++j) { + RETURN_HR_IF(E_UNEXPECTED, + memcpy_s(buffer + (stride * i), + bufferSize - (stride * i), + sourceData + xOffBytes + (sourceDataStride * j), + stride)); + } + } return S_OK; }