Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash with invalid LCD pixel checks (fix included) #48

Open
freds72 opened this issue Nov 30, 2015 · 0 comments
Open

Crash with invalid LCD pixel checks (fix included) #48

freds72 opened this issue Nov 30, 2015 · 0 comments

Comments

@freds72
Copy link

freds72 commented Nov 30, 2015

LCD screen size is 178x128 pixels, valid values MUST be 0-177/0-127, not 0-178/0-128!
Drawing a line/rectangle beyond screen limits no longer crashes.
MonoBrickFirmware.Display.EV3Lcd.cs

protected bool IsPixelInLcd(Point pixel)
{
  return (pixel.X >= 0) && (pixel.Y >= 0) && (pixel.X < width) && (pixel.Y < height);
}
protected bool IsPixelInLcd(int x, int y)
{
 return (x >= 0) && (y >= 0) && (x < width) && (y < height);
}

Same EV3 simulator: EV3MonoBrickSimulator.Stub.LcdStub.cs

// new help method
bool IsPixelInLcd(int x, int y)
{
 return (x >= 0) && (y >= 0) && (x < Width) && (y < Height);
}
public bool IsPixelSet(int x, int y)
{
 if (!IsPixelInLcd(x, y)) return false; // boundary check
 int index = GetIndex(x, y);
 return (Marshal.ReadInt32( IntPtr.Add (lcdBuffer.Pixels, index)) & 0x00ffffff) == 0x000000;
}
public void SetPixel(int x, int y)
{
 if (!IsPixelInLcd(x, y)) return; // boundary check

 int index = GetIndex(x, y);
 Int32 oldValue = Marshal.ReadInt32( IntPtr.Add (lcdBuffer.Pixels, index));
 Int32 newValue = (int)(oldValue & 0xff000000);
 Marshal.WriteInt32 (IntPtr.Add (lcdBuffer.Pixels, index), newValue);
}
public void ClearPixel(int x, int y)
{
 if (!IsPixelInLcd(x, y)) return; // boundary check
 int index = GetIndex(x, y);
 int backGroundValue = Marshal.ReadInt32( IntPtr.Add (backGroundPixBuffer.Pixels, index));
 int oldValue = Marshal.ReadInt32( IntPtr.Add (lcdBuffer.Pixels, index));
 int newValue = (int)(oldValue & 0xff000000) | (int)(backGroundValue & 0x00ffffff);
 Marshal.WriteInt32 (IntPtr.Add (lcdBuffer.Pixels, index), newValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant