Skip to content

Use only 32-bit ARGB or RGB565 formats #2822

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

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/nanoFramework.Graphics/Graphics/Core/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ bool CLR_GFX_BitmapDescription::BitmapDescription_Initialize(int width, int heig
m_flags = 0;
m_type = CLR_GFX_BitmapDescription::c_Type_nanoCLRBitmap;

if (GetTotalSize() < 0)
return false;

return true;
}

Expand Down Expand Up @@ -162,7 +159,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstance(

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance(refUncompressed, bmUncompressed));

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(refUncompressed, bitmap));
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(refUncompressed, bitmap));

bitmap->Decompress(data, size);

Expand All @@ -182,7 +179,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstance(

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance(ref, bmNative));

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(ref, bitmapNative));
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(ref, bitmapNative));

bitmapNative->ConvertToNative(bm, (CLR_UINT32 *)data);
}
Expand All @@ -201,7 +198,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstance(
NANOCLR_CLEANUP_END();
}

HRESULT CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap)
HRESULT CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap)
{
HRESULT hr;
CLR_RT_HeapBlock *blob;
Expand Down Expand Up @@ -254,7 +251,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstanceBmp(CLR_RT_HeapBlock &ref, const CLR_UINT8
// Allocate the memory that the decoded bitmap would need
NANOCLR_CHECK_HRESULT(CreateInstance(ref, bm));

NANOCLR_CHECK_HRESULT(GetInstanceFromGraphicsHeapBlock(ref, bitmap));
NANOCLR_CHECK_HRESULT(GetInstanceFromManagedCSharpReference(ref, bitmap));

NANOCLR_CHECK_HRESULT(decoder.BmpStartOutput(bitmap));

Expand Down Expand Up @@ -421,7 +418,7 @@ CLR_UINT32 CLR_GFX_Bitmap::ConvertToNative16BppHelper(int x, int y, CLR_UINT32 f
myParam->srcCur16BppPixel++;
opacity = PAL_GFX_Bitmap::c_OpacityOpaque;

return (r << 16 | g << 8 | b);
return (r << 16) | (g << 8) | b;
}

CLR_UINT32 CLR_GFX_Bitmap::GetPixel(int xPos, int yPos) const
Expand Down Expand Up @@ -466,7 +463,7 @@ void CLR_GFX_Bitmap::Bitmap_Initialize()
m_palBitmap.width = m_bm.m_width;
m_palBitmap.height = m_bm.m_height;
m_palBitmap.data = (CLR_UINT32 *)&this[1];
m_palBitmap.transparentColor = PAL_GFX_Bitmap::c_InvalidColor;
m_palBitmap.transparentColorSet = PAL_GFX_Bitmap::c_TransparentColorNotSet;

PAL_GFX_Bitmap::ResetClipping(m_palBitmap);
}
Expand Down Expand Up @@ -735,7 +732,7 @@ HRESULT CLR_GFX_Bitmap::GetBitmap(CLR_RT_HeapBlock *pThis, bool fForWrite, CLR_G
FAULT_ON_NULL(pThis);

NANOCLR_CHECK_HRESULT(
CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));
CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));

if ((bitmap->m_bm.m_flags & CLR_GFX_BitmapDescription::c_ReadOnly) && fForWrite)
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
Expand Down
16 changes: 9 additions & 7 deletions src/nanoFramework.Graphics/Graphics/Core/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
#include "nf_errors_exceptions.h"

#include "Display.h"

#define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */

#include "Gestures.h"
// ???? Redefined below but gets past the compile error of not defined
// CLR_GFX_Font needs CLR_GFX_Bitmap and CLR_GFX_Bitmap needs CLR_GFX_Font
struct CLR_GFX_Bitmap;
Expand Down Expand Up @@ -422,8 +420,11 @@ struct PAL_GFX_Bitmap
CLR_UINT32 *data;
GFX_Rect clipping;
CLR_UINT32 transparentColor;
CLR_UINT16 transparentColorSet;

static const CLR_UINT32 c_InvalidColor = 0xFF000000;
static const CLR_UINT16 c_TransparentColorNotSet = 0x00000000;
static const CLR_UINT16 c_TransparentColorSet = 0x00000001;
static const CLR_UINT16 c_OpacityTransparent = 0;
static const CLR_UINT16 c_OpacityOpaque = 256;
static const CLR_UINT32 c_SetPixels_None = 0x00000000;
Expand Down Expand Up @@ -481,7 +482,7 @@ struct CLR_GFX_Bitmap
static HRESULT CreateInstanceGif(CLR_RT_HeapBlock &ref, const CLR_UINT8 *data, const CLR_UINT32 size);
static HRESULT CreateInstanceBmp(CLR_RT_HeapBlock &ref, const CLR_UINT8 *data, const CLR_UINT32 size);

static HRESULT GetInstanceFromGraphicsHeapBlock(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap);
static HRESULT GetInstanceFromManagedCSharpReference(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap);
static HRESULT DeleteInstance(CLR_RT_HeapBlock &ref);

static CLR_UINT32 CreateInstanceJpegHelper(int x, int y, CLR_UINT32 flags, CLR_UINT16 &opacity, void *param);
Expand Down Expand Up @@ -776,10 +777,10 @@ struct GraphicsDriver
}
__inline static CLR_UINT32 ColorFromRGB(CLR_UINT8 r, CLR_UINT8 g, CLR_UINT8 b)
{
return (b << 16) | (g << 8) | r;
return (r << 16) | (g << 8) | b;
}

__inline static CLR_UINT32 ConvertNativeToColor(CLR_UINT32 nativeColor)
__inline static CLR_UINT32 ConvertNativeToARGB(CLR_UINT32 nativeColor)
{
int r = NativeColorRValue(nativeColor) << 3;
if ((r & 0x8) != 0)
Expand All @@ -790,7 +791,8 @@ struct GraphicsDriver
int b = NativeColorBValue(nativeColor) << 3;
if ((b & 0x8) != 0)
b |= 0x7; // Copy LSB
return ColorFromRGB(r, g, b);
CLR_UINT32 opaque = 0xff000000;
return ColorFromRGB(r, g, b) | opaque;
}
__inline static CLR_UINT32 ConvertColorToNative(CLR_UINT32 color)
{
Expand Down
16 changes: 10 additions & 6 deletions src/nanoFramework.Graphics/Graphics/Core/GraphicsDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool GraphicsDriver::ChangeOrientation(DisplayOrientation newOrientation)

CLR_UINT32 GraphicsDriver::GetPixel(const PAL_GFX_Bitmap &bitmap, int x, int y)
{
return ConvertNativeToColor(GetPixelNative(bitmap, x, y));
return ConvertNativeToARGB(GetPixelNative(bitmap, x, y));
}

void GraphicsDriver::SetPixel(const PAL_GFX_Bitmap &bitmap, int x, int y, CLR_UINT32 color)
Expand Down Expand Up @@ -777,6 +777,9 @@ void GraphicsDriver::RotateImage(
if (degree < 0)
degree = 360 + degree;

CLR_UINT16 nativeTransparentColor = ConvertColorToNative(src.transparentColor);
bool srcHasTransparentColor = (src.transparentColorSet == PAL_GFX_Bitmap::c_TransparentColorSet);

// If it's just a translation, do the BitBlt instead
if (degree == 0)
{
Expand Down Expand Up @@ -849,7 +852,10 @@ void GraphicsDriver::RotateImage(
{
CLR_UINT32 mask, shift;
CLR_UINT32 *pbyteSrc = ComputePosition(src, xSrc, ySrc, mask, shift);
if ((CLR_UINT32)pbyteSrc >= sourceMemoryBlockStart && (CLR_UINT32)pbyteSrc <= sourceMemoryBlockEnd)
CLR_UINT16 rgb565Color = (*pbyteSrc & mask) >> shift;
bool transparentSrcColour = srcHasTransparentColor && (nativeTransparentColor == rgb565Color);
if ((CLR_UINT32)pbyteSrc >= sourceMemoryBlockStart &&
(CLR_UINT32)pbyteSrc <= sourceMemoryBlockEnd && !transparentSrcColour)
{
*pbyteDst &= ~maskDst;
*pbyteDst |= ((*pbyteSrc & mask) >> shift) << shiftDst;
Expand Down Expand Up @@ -898,7 +904,7 @@ void GraphicsDriver::DrawImage(

int type = 0x0;
CLR_UINT32 nativeTransparentColor = 0;
if (bitmapSrc.transparentColor != PAL_GFX_Bitmap::c_InvalidColor)
if (bitmapSrc.transparentColorSet == PAL_GFX_Bitmap::c_TransparentColorSet)
{
type |= Transparent;
nativeTransparentColor = g_GraphicsDriver.ConvertColorToNative(bitmapSrc.transparentColor);
Expand Down Expand Up @@ -1033,7 +1039,7 @@ void GraphicsDriver::DrawImage(

CLR_UINT16 color = *ps;
CLR_UINT32 nativeTransparentColor = g_GraphicsDriver.ConvertColorToNative(bitmapSrc.transparentColor);
bool noTransparent = bitmapSrc.transparentColor == PAL_GFX_Bitmap::c_InvalidColor;
bool noTransparent = bitmapSrc.transparentColorSet == PAL_GFX_Bitmap::c_TransparentColorNotSet;
bool transparent = (noTransparent == false) && (color == nativeTransparentColor);

for (int x = 0; x < croppedWidth; x++, pd++)
Expand Down Expand Up @@ -1587,8 +1593,6 @@ void GraphicsDriver::Screen_Flush(
return;
if (bitmap.m_bm.m_bitsPerPixel != CLR_GFX_BitmapDescription::c_NativeBpp)
return;
if (bitmap.m_palBitmap.transparentColor != PAL_GFX_Bitmap::c_InvalidColor)
return;

g_DisplayDriver.BitBlt(srcX, srcY, width, height, bitmap.m_bm.m_width, screenX, screenY, bitmap.m_palBitmap.data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ HRESULT BmpDecoder::BmpInitOutput(const CLR_UINT8 *src, CLR_UINT32 srcSize)
if (pbmih->biClrUsed != 0)
{
palette = source.source;
paletteDepth =
(CLR_UINT8)((pbmfh->bfOffBits - sizeof(BITMAPINFOHEADER) - sizeof(BITMAPFILEHEADER)) / pbmih->biClrUsed); // the rest is the palette
paletteDepth = (CLR_UINT8)((pbmfh->bfOffBits - sizeof(BITMAPINFOHEADER) - sizeof(BITMAPFILEHEADER)) /
pbmih->biClrUsed); // the rest is the palette
encodingType = Bmp8Bit_Indexed;
}
break;
Expand Down Expand Up @@ -291,5 +291,5 @@ CLR_UINT32 BmpDecoder::BmpOutputHelper(int x, int y, CLR_UINT32 flags, CLR_UINT1
break;
}

return r | (g << 8) | (b << 16);
return (r << 16) | (g << 8) | b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstanceGif(CLR_RT_HeapBlock &ref, const CLR_UINT8
// Allocate the memory that the decompressed bitmap would need
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance(ref, bm));

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(ref, bitmap));
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(ref, bitmap));

NANOCLR_CHECK_HRESULT(decoder->GifStartDecompress(bitmap));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#include "gif.h"
#include "lzwread.h"

// Reverse 32bit RGB Color to 24bit BGR Color
#define REVERSECOLOR(n) ((n << 24) | (((n >> 16) << 24) >> 16) | (((n << 16) >> 24) << 16)) >> 8

// Initialization routine for GifDecoder struct. When it's finished,
// the header field would be loaded already.
HRESULT GifDecoder::GifInitDecompress(const CLR_UINT8 *src, CLR_UINT32 srcSize)
Expand Down Expand Up @@ -331,7 +328,7 @@ CLR_UINT32 GifDecoder::ProcessImageChunkHelper(int x, int y, CLR_UINT32 flags, C
myParam->flushing = false;
}

return REVERSECOLOR(color);
return color;
}

#pragma GCC diagnostic pop
Expand Down Expand Up @@ -414,7 +411,7 @@ HRESULT GifDecoder::ReadColorTable()
for (int i = 0; i < colorTableSize; i++)
{
colorTable[i] =
((CLR_UINT32)curEntry->red) | (((CLR_UINT32)curEntry->green) << 8) | (((CLR_UINT32)curEntry->blue) << 16);
((CLR_UINT32)curEntry->red) << 16 | ((CLR_UINT32)curEntry->green) << 8 | ((CLR_UINT32)curEntry->blue);
if (colorTable[i] == transparentColor)
{
isTransparentColorUnique = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstanceJpeg(CLR_RT_HeapBlock &ref, const CLR_UINT
// Allocate the memory that the decompressed bitmap would need
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance(ref, bm));

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(ref, bitmap));
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(ref, bitmap));

// Do the actual decompression
rect.left = 0;
Expand Down
13 changes: 13 additions & 0 deletions src/nanoFramework.Graphics/Graphics/Displays/DisplayInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ struct DisplayInterfaceConfig
CLR_INT8 address;
CLR_INT8 fastMode;
} I2c;
struct
{
CLR_INT16 enable;
CLR_INT16 control;
CLR_INT16 backlight;
CLR_INT16 Horizontal_synchronization;
CLR_INT16 Horizontal_back_porch;
CLR_INT16 Horizontal_front_porch;
CLR_INT16 Vertical_synchronization;
CLR_INT16 Vertical_back_porch;
CLR_INT16 Vertical_front_porch;
CLR_INT16 Frequency_Divider;
} VideoDisplay;
};
struct
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,10 @@ HRESULT Library_nanoFramework_Graphics_nanoFramework_UI_Bitmap::MakeTransparent_
NANOCLR_HEADER();

CLR_GFX_Bitmap *bitmap;
CLR_UINT32 color;

NANOCLR_CHECK_HRESULT(GetBitmap(stack, true, bitmap));

color = stack.Arg1().NumericByRef().u4;

bitmap->m_palBitmap.transparentColor = (color & 0xFF000000) ? PAL_GFX_Bitmap::c_InvalidColor : color;
bitmap->m_palBitmap.transparentColor = stack.Arg1().NumericByRef().u4;
bitmap->m_palBitmap.transparentColorSet = PAL_GFX_Bitmap::c_TransparentColorSet;

NANOCLR_NOCLEANUP();
}
Expand Down Expand Up @@ -1177,7 +1174,7 @@ HRESULT GetBitmap(CLR_RT_HeapBlock *pThis, bool fForWrite, CLR_GFX_Bitmap *&bitm
FAULT_ON_NULL(pThis);

NANOCLR_CHECK_HRESULT(
CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));
CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));

if ((bitmap->m_bm.m_flags & CLR_GFX_BitmapDescription::c_ReadOnly) && fForWrite)
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ HRESULT Library_nanoFramework_Graphics_nanoFramework_UI_Ink::
// we are drawing on the object in the PAL therefore it should not move
m_InkPinnedBitmap->Pin();

NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(
m_InkPinnedBitmap[CLR_GFX_Bitmap::FIELD__m_bitmap],
bitmap));

Expand Down
2 changes: 1 addition & 1 deletion src/nanoFramework.Graphics/TouchPanel/Core/Ink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ HRESULT InkDriver::Initialize()
m_ScreenBmp.width = g_DisplayDriver.Attributes.Width;
m_ScreenBmp.height = g_DisplayDriver.Attributes.Height;
// m_ScreenBmp.data = Display::GetFrameBuffer();
m_ScreenBmp.transparentColor = PAL_GFX_Bitmap::c_InvalidColor;
m_ScreenBmp.transparentColorSet = PAL_GFX_Bitmap::c_TransparentColorNotSet;
}

return S_OK;
Expand Down
6 changes: 3 additions & 3 deletions src/nanoFramework.Graphics/TouchPanel/Core/Ink.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// See LICENSE file in the project root for full license information.
//

#ifndef GESTURES_H
#define GESTURES_H
#ifndef INK_H
#define INK_H

#include "Graphics.h"

Expand Down Expand Up @@ -42,4 +42,4 @@ class InkDriver
static void InkContinuationRoutine(void *arg);
};

#endif // GESTURES_H
#endif // INK_H