jingwood / jingwood/d2dlib

Using PushTransform/PopTransform on D2DBitmapGraphics object causes Access Violation

Open
#13 1 comment 2 reactions 0 assignees View on GitHub
enhancement todo
Dominant language
C#
Stars
261
Forks
48
PR merge metrics
No merged PRs in 30d

Description

Using PushTransform/PopTransform on D2DBitmapGraphics object causes Access Violation

Fix for D2DBitmapGraphics (PushTransform/PopTransform)

```
public class D2DBitmapGraphics : D2DGraphics, IDisposable
{
internal enum D2DTransformType
{
Rotate1,
Rotate2,
Translate,
Scale,
Skew
}

internal class D2DTransform
{
public D2DTransformType Type { get; set; }
public float Angle { get; set; }
public D2DPoint Center { get; set; }
public float X { get; set; }
public float Y { get; set; }
}

internal Stack> TransStack {get; set; }

internal D2DBitmapGraphics(HANDLE handle)
: base(handle)
{
TransStack = new Stack>();
TransStack.Push(new List());
}

public void Dispose()
{
D2D.DestoryBitmapRenderTarget(DeviceHandle);

foreach (var ts in TransStack)
ts.Clear();

TransStack.Clear();
}

public D2DBitmap GetBitmap()
{
var bitmapHandle = D2D.GetBitmapRenderTargetBitmap(DeviceHandle);
return bitmapHandle == HANDLE.Zero ? null : new D2DBitmap(bitmapHandle);
}

public override void RotateTransform(float angle)
{
TransStack.Peek().Add(new D2DTransform() { Type = D2DTransformType.Rotate1, Angle = angle});
base.RotateTransform(angle);
}

public override void RotateTransform(float angle, D2DPoint center)
{
TransStack.Peek().Add(new D2DTransform() { Type = D2DTransformType.Rotate2, Angle = angle, Center = center});
base.RotateTransform(angle, center);
}

public override void TranslateTransform(float x, float y)
{
TransStack.Peek().Add(new D2DTransform() { Type = D2DTransformType.Translate, X = x, Y = y});
base.TranslateTransform(x, y);
}

public override void ScaleTransform(float sx, float sy, D2DPoint center = new D2DPoint())
{
TransStack.Peek().Add(new D2DTransform() { Type = D2DTransformType.Scale, X = sx, Y = sy, Center = center});
base.ScaleTransform(sx, sy, center);
}

public override void SkewTransform(float angleX, float angleY, D2DPoint center = new D2DPoint())
{
TransStack.Peek().Add(new D2DTransform() { Type = D2DTransformType.Skew, X = angleX, Y = angleY, Center = center});

base.SkewTransform(angleX, angleY, center);
}

public override void ResetTransform()
{
TransStack.Peek().Clear();
base.ResetTransform();
}

public override void PushTransform()
{
var c = TransStack.Peek().ToList();
TransStack.Push(c);
}

public override void PopTransform()
{
if (TransStack.Count == 1)
throw new InvalidOperationException();

TransStack.Pop();

base.ResetTransform();

foreach (var t in TransStack.Peek())
{
switch (t.Type)
{
case D2DTransformType.Rotate1:
base.RotateTransform(t.Angle);
break;

case D2DTransformType.Rotate2:
base.RotateTransform(t.Angle, t.Center);
break;

case D2DTransformType.Translate:
base.TranslateTransform(t.X, t.Y);
break;

case D2DTransformType.Scale:
base.ScaleTransform(t.X, t.Y, t.Center);
break;

case D2DTransformType.Skew:
base.SkewTransform(t.X, t.Y, t.Center);
break;

default:
throw new ArgumentOutOfRangeException();
}
}
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Review D2DBitmapGraphics and its PushTransform/PopTransform methods, then reproduce the access violation described in the issue. Compare the transform-stack behavior with the supplied implementation and verify that pushing and popping transforms no longer crashes and restores the expected transform state.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.