Skip to content

Commit fd5fc11

Browse files
committed
Fixed stride-issue introduced with rotation-copy methods
1 parent 466f844 commit fd5fc11

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

ScreenCapture.NET/DirectX/DX11ScreenCapture.cs

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,23 @@ private void UpdateZones()
186186
MappedSubresource mapSource = _context.Map(stagingTexture, 0, MapMode.Read, MapFlags.None);
187187
lock (captureZone.Buffer)
188188
{
189-
Span<byte> source = mapSource.AsSpan(captureZone.Stride * captureZone.Height);
189+
Span<byte> source = mapSource.AsSpan(mapSource.RowPitch * captureZone.Height);
190190
switch (Display.Rotation)
191191
{
192192
case Rotation.Rotation90:
193-
CopyRotate90(source, captureZone);
193+
CopyRotate90(source, mapSource.RowPitch, captureZone);
194194
break;
195195

196196
case Rotation.Rotation180:
197-
CopyRotate180(source, captureZone);
197+
CopyRotate180(source, mapSource.RowPitch, captureZone);
198198
break;
199199

200200
case Rotation.Rotation270:
201-
CopyRotate270(source, captureZone);
201+
CopyRotate270(source, mapSource.RowPitch, captureZone);
202202
break;
203203

204204
default:
205-
CopyRotate0(source, captureZone);
205+
CopyRotate0(source, mapSource.RowPitch, captureZone);
206206
break;
207207
}
208208
}
@@ -214,62 +214,79 @@ private void UpdateZones()
214214
}
215215

216216
[MethodImpl(MethodImplOptions.AggressiveInlining)]
217-
private static void CopyRotate0(in Span<byte> source, in CaptureZone captureZone) => source.CopyTo(captureZone.Buffer.AsSpan());
217+
private static void CopyRotate0(in Span<byte> source, int sourceStride, in CaptureZone captureZone)
218+
{
219+
int height = captureZone.Height;
220+
int stride = captureZone.Stride;
221+
Span<byte> target = captureZone.Buffer.AsSpan();
222+
223+
for (int y = 0; y < height; y++)
224+
{
225+
int sourceOffset = y * sourceStride;
226+
int targetOffset = y * stride;
227+
228+
source.Slice(sourceOffset, stride).CopyTo(target.Slice(targetOffset, stride));
229+
}
230+
}
218231

219232
[MethodImpl(MethodImplOptions.AggressiveInlining)]
220-
private static void CopyRotate90(in Span<byte> source, in CaptureZone captureZone)
233+
private static void CopyRotate90(in Span<byte> source, int sourceStride, in CaptureZone captureZone)
221234
{
222235
int width = captureZone.Width;
223236
int height = captureZone.Height;
224-
Span<byte> destination = captureZone.Buffer.AsSpan();
237+
Span<byte> target = captureZone.Buffer.AsSpan();
225238

226239
for (int y = 0; y < height; y++)
227240
for (int x = 0; x < width; x++)
228241
{
229-
int offset = ((y * width) + x) * BPP;
230-
int destinationOffset = ((x * height) + ((height - 1) - y)) * BPP;
231-
destination[destinationOffset] = source[offset];
232-
destination[destinationOffset + 1] = source[offset + 1];
233-
destination[destinationOffset + 2] = source[offset + 2];
234-
destination[destinationOffset + 3] = source[offset + 3];
242+
int sourceOffset = ((y * sourceStride) + (x * BPP));
243+
int targetOffset = ((x * height) + ((height - 1) - y)) * BPP;
244+
245+
target[targetOffset] = source[sourceOffset];
246+
target[targetOffset + 1] = source[sourceOffset + 1];
247+
target[targetOffset + 2] = source[sourceOffset + 2];
248+
target[targetOffset + 3] = source[sourceOffset + 3];
235249
}
236250
}
237251

238252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
239-
private static void CopyRotate180(in Span<byte> source, in CaptureZone captureZone)
253+
private static void CopyRotate180(in Span<byte> source, int sourceStride, in CaptureZone captureZone)
240254
{
241255
int width = captureZone.Width;
242256
int height = captureZone.Height;
243-
Span<byte> destination = captureZone.Buffer.AsSpan();
257+
int stride = captureZone.Stride;
258+
Span<byte> target = captureZone.Buffer.AsSpan();
244259

245260
for (int y = 0; y < height; y++)
246261
for (int x = 0; x < width; x++)
247262
{
248-
int offset = ((y * width) + x) * BPP;
249-
int destinationOffset = destination.Length - offset;
250-
destination[destinationOffset - 4] = source[offset];
251-
destination[destinationOffset - 3] = source[offset + 1];
252-
destination[destinationOffset - 2] = source[offset + 2];
253-
destination[destinationOffset - 1] = source[offset + 3];
263+
int sourceOffset = ((y * sourceStride) + (x * BPP));
264+
int targetOffset = target.Length - ((y * stride) + (x * BPP)) - 1;
265+
266+
target[targetOffset - 3] = source[sourceOffset];
267+
target[targetOffset - 2] = source[sourceOffset + 1];
268+
target[targetOffset - 1] = source[sourceOffset + 2];
269+
target[targetOffset] = source[sourceOffset + 3];
254270
}
255271
}
256272

257273
[MethodImpl(MethodImplOptions.AggressiveInlining)]
258-
private static void CopyRotate270(in Span<byte> source, in CaptureZone captureZone)
274+
private static void CopyRotate270(in Span<byte> source, int sourceStride, in CaptureZone captureZone)
259275
{
260276
int width = captureZone.Width;
261277
int height = captureZone.Height;
262-
Span<byte> destination = captureZone.Buffer.AsSpan();
278+
Span<byte> target = captureZone.Buffer.AsSpan();
263279

264280
for (int y = 0; y < height; y++)
265281
for (int x = 0; x < width; x++)
266282
{
267-
int offset = ((y * width) + x) * BPP;
268-
int destinationOffset = ((((width - 1) - x) * height) + y) * BPP;
269-
destination[destinationOffset] = source[offset];
270-
destination[destinationOffset + 1] = source[offset + 1];
271-
destination[destinationOffset + 2] = source[offset + 2];
272-
destination[destinationOffset + 3] = source[offset + 3];
283+
int sourceOffset = ((y * sourceStride) + (x * BPP));
284+
int targetOffset = ((((width - 1) - x) * height) + y) * BPP;
285+
286+
target[targetOffset] = source[sourceOffset];
287+
target[targetOffset + 1] = source[sourceOffset + 1];
288+
target[targetOffset + 2] = source[sourceOffset + 2];
289+
target[targetOffset + 3] = source[sourceOffset + 3];
273290
}
274291
}
275292

0 commit comments

Comments
 (0)