Skip to content

Commit ad6216f

Browse files
authored
Use only 32-bit ARGB or RGB565 formats (#2822)
***NO_CI***
1 parent dfd3f5d commit ad6216f

File tree

12 files changed

+54
-44
lines changed

12 files changed

+54
-44
lines changed

src/nanoFramework.Graphics/Graphics/Core/Graphics.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ bool CLR_GFX_BitmapDescription::BitmapDescription_Initialize(int width, int heig
3131
m_flags = 0;
3232
m_type = CLR_GFX_BitmapDescription::c_Type_nanoCLRBitmap;
3333

34-
if (GetTotalSize() < 0)
35-
return false;
36-
3734
return true;
3835
}
3936

@@ -162,7 +159,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstance(
162159

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

165-
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(refUncompressed, bitmap));
162+
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(refUncompressed, bitmap));
166163

167164
bitmap->Decompress(data, size);
168165

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

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

185-
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(ref, bitmapNative));
182+
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(ref, bitmapNative));
186183

187184
bitmapNative->ConvertToNative(bm, (CLR_UINT32 *)data);
188185
}
@@ -201,7 +198,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstance(
201198
NANOCLR_CLEANUP_END();
202199
}
203200

204-
HRESULT CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap)
201+
HRESULT CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap)
205202
{
206203
HRESULT hr;
207204
CLR_RT_HeapBlock *blob;
@@ -254,7 +251,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstanceBmp(CLR_RT_HeapBlock &ref, const CLR_UINT8
254251
// Allocate the memory that the decoded bitmap would need
255252
NANOCLR_CHECK_HRESULT(CreateInstance(ref, bm));
256253

257-
NANOCLR_CHECK_HRESULT(GetInstanceFromGraphicsHeapBlock(ref, bitmap));
254+
NANOCLR_CHECK_HRESULT(GetInstanceFromManagedCSharpReference(ref, bitmap));
258255

259256
NANOCLR_CHECK_HRESULT(decoder.BmpStartOutput(bitmap));
260257

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

424-
return (r << 16 | g << 8 | b);
421+
return (r << 16) | (g << 8) | b;
425422
}
426423

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

471468
PAL_GFX_Bitmap::ResetClipping(m_palBitmap);
472469
}
@@ -735,7 +732,7 @@ HRESULT CLR_GFX_Bitmap::GetBitmap(CLR_RT_HeapBlock *pThis, bool fForWrite, CLR_G
735732
FAULT_ON_NULL(pThis);
736733

737734
NANOCLR_CHECK_HRESULT(
738-
CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));
735+
CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));
739736

740737
if ((bitmap->m_bm.m_flags & CLR_GFX_BitmapDescription::c_ReadOnly) && fForWrite)
741738
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);

src/nanoFramework.Graphics/Graphics/Core/Graphics.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
#include "nf_errors_exceptions.h"
2929

3030
#include "Display.h"
31-
32-
#define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */
33-
31+
#include "Gestures.h"
3432
// ???? Redefined below but gets past the compile error of not defined
3533
// CLR_GFX_Font needs CLR_GFX_Bitmap and CLR_GFX_Bitmap needs CLR_GFX_Font
3634
struct CLR_GFX_Bitmap;
@@ -422,8 +420,11 @@ struct PAL_GFX_Bitmap
422420
CLR_UINT32 *data;
423421
GFX_Rect clipping;
424422
CLR_UINT32 transparentColor;
423+
CLR_UINT16 transparentColorSet;
425424

426425
static const CLR_UINT32 c_InvalidColor = 0xFF000000;
426+
static const CLR_UINT16 c_TransparentColorNotSet = 0x00000000;
427+
static const CLR_UINT16 c_TransparentColorSet = 0x00000001;
427428
static const CLR_UINT16 c_OpacityTransparent = 0;
428429
static const CLR_UINT16 c_OpacityOpaque = 256;
429430
static const CLR_UINT32 c_SetPixels_None = 0x00000000;
@@ -481,7 +482,7 @@ struct CLR_GFX_Bitmap
481482
static HRESULT CreateInstanceGif(CLR_RT_HeapBlock &ref, const CLR_UINT8 *data, const CLR_UINT32 size);
482483
static HRESULT CreateInstanceBmp(CLR_RT_HeapBlock &ref, const CLR_UINT8 *data, const CLR_UINT32 size);
483484

484-
static HRESULT GetInstanceFromGraphicsHeapBlock(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap);
485+
static HRESULT GetInstanceFromManagedCSharpReference(const CLR_RT_HeapBlock &ref, CLR_GFX_Bitmap *&bitmap);
485486
static HRESULT DeleteInstance(CLR_RT_HeapBlock &ref);
486487

487488
static CLR_UINT32 CreateInstanceJpegHelper(int x, int y, CLR_UINT32 flags, CLR_UINT16 &opacity, void *param);
@@ -776,10 +777,10 @@ struct GraphicsDriver
776777
}
777778
__inline static CLR_UINT32 ColorFromRGB(CLR_UINT8 r, CLR_UINT8 g, CLR_UINT8 b)
778779
{
779-
return (b << 16) | (g << 8) | r;
780+
return (r << 16) | (g << 8) | b;
780781
}
781782

782-
__inline static CLR_UINT32 ConvertNativeToColor(CLR_UINT32 nativeColor)
783+
__inline static CLR_UINT32 ConvertNativeToARGB(CLR_UINT32 nativeColor)
783784
{
784785
int r = NativeColorRValue(nativeColor) << 3;
785786
if ((r & 0x8) != 0)
@@ -790,7 +791,8 @@ struct GraphicsDriver
790791
int b = NativeColorBValue(nativeColor) << 3;
791792
if ((b & 0x8) != 0)
792793
b |= 0x7; // Copy LSB
793-
return ColorFromRGB(r, g, b);
794+
CLR_UINT32 opaque = 0xff000000;
795+
return ColorFromRGB(r, g, b) | opaque;
794796
}
795797
__inline static CLR_UINT32 ConvertColorToNative(CLR_UINT32 color)
796798
{

src/nanoFramework.Graphics/Graphics/Core/GraphicsDriver.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool GraphicsDriver::ChangeOrientation(DisplayOrientation newOrientation)
7777

7878
CLR_UINT32 GraphicsDriver::GetPixel(const PAL_GFX_Bitmap &bitmap, int x, int y)
7979
{
80-
return ConvertNativeToColor(GetPixelNative(bitmap, x, y));
80+
return ConvertNativeToARGB(GetPixelNative(bitmap, x, y));
8181
}
8282

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

780+
CLR_UINT16 nativeTransparentColor = ConvertColorToNative(src.transparentColor);
781+
bool srcHasTransparentColor = (src.transparentColorSet == PAL_GFX_Bitmap::c_TransparentColorSet);
782+
780783
// If it's just a translation, do the BitBlt instead
781784
if (degree == 0)
782785
{
@@ -849,7 +852,10 @@ void GraphicsDriver::RotateImage(
849852
{
850853
CLR_UINT32 mask, shift;
851854
CLR_UINT32 *pbyteSrc = ComputePosition(src, xSrc, ySrc, mask, shift);
852-
if ((CLR_UINT32)pbyteSrc >= sourceMemoryBlockStart && (CLR_UINT32)pbyteSrc <= sourceMemoryBlockEnd)
855+
CLR_UINT16 rgb565Color = (*pbyteSrc & mask) >> shift;
856+
bool transparentSrcColour = srcHasTransparentColor && (nativeTransparentColor == rgb565Color);
857+
if ((CLR_UINT32)pbyteSrc >= sourceMemoryBlockStart &&
858+
(CLR_UINT32)pbyteSrc <= sourceMemoryBlockEnd && !transparentSrcColour)
853859
{
854860
*pbyteDst &= ~maskDst;
855861
*pbyteDst |= ((*pbyteSrc & mask) >> shift) << shiftDst;
@@ -898,7 +904,7 @@ void GraphicsDriver::DrawImage(
898904

899905
int type = 0x0;
900906
CLR_UINT32 nativeTransparentColor = 0;
901-
if (bitmapSrc.transparentColor != PAL_GFX_Bitmap::c_InvalidColor)
907+
if (bitmapSrc.transparentColorSet == PAL_GFX_Bitmap::c_TransparentColorSet)
902908
{
903909
type |= Transparent;
904910
nativeTransparentColor = g_GraphicsDriver.ConvertColorToNative(bitmapSrc.transparentColor);
@@ -1033,7 +1039,7 @@ void GraphicsDriver::DrawImage(
10331039

10341040
CLR_UINT16 color = *ps;
10351041
CLR_UINT32 nativeTransparentColor = g_GraphicsDriver.ConvertColorToNative(bitmapSrc.transparentColor);
1036-
bool noTransparent = bitmapSrc.transparentColor == PAL_GFX_Bitmap::c_InvalidColor;
1042+
bool noTransparent = bitmapSrc.transparentColorSet == PAL_GFX_Bitmap::c_TransparentColorNotSet;
10371043
bool transparent = (noTransparent == false) && (color == nativeTransparentColor);
10381044

10391045
for (int x = 0; x < croppedWidth; x++, pd++)
@@ -1587,8 +1593,6 @@ void GraphicsDriver::Screen_Flush(
15871593
return;
15881594
if (bitmap.m_bm.m_bitsPerPixel != CLR_GFX_BitmapDescription::c_NativeBpp)
15891595
return;
1590-
if (bitmap.m_palBitmap.transparentColor != PAL_GFX_Bitmap::c_InvalidColor)
1591-
return;
15921596

15931597
g_DisplayDriver.BitBlt(srcX, srcY, width, height, bitmap.m_bm.m_width, screenX, screenY, bitmap.m_palBitmap.data);
15941598
}

src/nanoFramework.Graphics/Graphics/Core/Support/Bmp/Bitmap_Decoder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ HRESULT BmpDecoder::BmpInitOutput(const CLR_UINT8 *src, CLR_UINT32 srcSize)
104104
if (pbmih->biClrUsed != 0)
105105
{
106106
palette = source.source;
107-
paletteDepth =
108-
(CLR_UINT8)((pbmfh->bfOffBits - sizeof(BITMAPINFOHEADER) - sizeof(BITMAPFILEHEADER)) / pbmih->biClrUsed); // the rest is the palette
107+
paletteDepth = (CLR_UINT8)((pbmfh->bfOffBits - sizeof(BITMAPINFOHEADER) - sizeof(BITMAPFILEHEADER)) /
108+
pbmih->biClrUsed); // the rest is the palette
109109
encodingType = Bmp8Bit_Indexed;
110110
}
111111
break;
@@ -291,5 +291,5 @@ CLR_UINT32 BmpDecoder::BmpOutputHelper(int x, int y, CLR_UINT32 flags, CLR_UINT1
291291
break;
292292
}
293293

294-
return r | (g << 8) | (b << 16);
294+
return (r << 16) | (g << 8) | b;
295295
}

src/nanoFramework.Graphics/Graphics/Core/Support/Gif/Gif.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstanceGif(CLR_RT_HeapBlock &ref, const CLR_UINT8
3636
// Allocate the memory that the decompressed bitmap would need
3737
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance(ref, bm));
3838

39-
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(ref, bitmap));
39+
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(ref, bitmap));
4040

4141
NANOCLR_CHECK_HRESULT(decoder->GifStartDecompress(bitmap));
4242

src/nanoFramework.Graphics/Graphics/Core/Support/Gif/GifDecoder.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#include "gif.h"
99
#include "lzwread.h"
1010

11-
// Reverse 32bit RGB Color to 24bit BGR Color
12-
#define REVERSECOLOR(n) ((n << 24) | (((n >> 16) << 24) >> 16) | (((n << 16) >> 24) << 16)) >> 8
13-
1411
// Initialization routine for GifDecoder struct. When it's finished,
1512
// the header field would be loaded already.
1613
HRESULT GifDecoder::GifInitDecompress(const CLR_UINT8 *src, CLR_UINT32 srcSize)
@@ -331,7 +328,7 @@ CLR_UINT32 GifDecoder::ProcessImageChunkHelper(int x, int y, CLR_UINT32 flags, C
331328
myParam->flushing = false;
332329
}
333330

334-
return REVERSECOLOR(color);
331+
return color;
335332
}
336333

337334
#pragma GCC diagnostic pop
@@ -414,7 +411,7 @@ HRESULT GifDecoder::ReadColorTable()
414411
for (int i = 0; i < colorTableSize; i++)
415412
{
416413
colorTable[i] =
417-
((CLR_UINT32)curEntry->red) | (((CLR_UINT32)curEntry->green) << 8) | (((CLR_UINT32)curEntry->blue) << 16);
414+
((CLR_UINT32)curEntry->red) << 16 | ((CLR_UINT32)curEntry->green) << 8 | ((CLR_UINT32)curEntry->blue);
418415
if (colorTable[i] == transparentColor)
419416
{
420417
isTransparentColorUnique = false;

src/nanoFramework.Graphics/Graphics/Core/Support/Jpeg/Jpeg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ HRESULT CLR_GFX_Bitmap::CreateInstanceJpeg(CLR_RT_HeapBlock &ref, const CLR_UINT
9393
// Allocate the memory that the decompressed bitmap would need
9494
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance(ref, bm));
9595

96-
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(ref, bitmap));
96+
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(ref, bitmap));
9797

9898
// Do the actual decompression
9999
rect.left = 0;

src/nanoFramework.Graphics/Graphics/Displays/DisplayInterface.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ struct DisplayInterfaceConfig
2828
CLR_INT8 address;
2929
CLR_INT8 fastMode;
3030
} I2c;
31+
struct
32+
{
33+
CLR_INT16 enable;
34+
CLR_INT16 control;
35+
CLR_INT16 backlight;
36+
CLR_INT16 Horizontal_synchronization;
37+
CLR_INT16 Horizontal_back_porch;
38+
CLR_INT16 Horizontal_front_porch;
39+
CLR_INT16 Vertical_synchronization;
40+
CLR_INT16 Vertical_back_porch;
41+
CLR_INT16 Vertical_front_porch;
42+
CLR_INT16 Frequency_Divider;
43+
} VideoDisplay;
3144
};
3245
struct
3346
{

src/nanoFramework.Graphics/Graphics/Native/nanoFramework_Graphics_nanoFramework_UI_Bitmap.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,10 @@ HRESULT Library_nanoFramework_Graphics_nanoFramework_UI_Bitmap::MakeTransparent_
460460
NANOCLR_HEADER();
461461

462462
CLR_GFX_Bitmap *bitmap;
463-
CLR_UINT32 color;
464463

465464
NANOCLR_CHECK_HRESULT(GetBitmap(stack, true, bitmap));
466-
467-
color = stack.Arg1().NumericByRef().u4;
468-
469-
bitmap->m_palBitmap.transparentColor = (color & 0xFF000000) ? PAL_GFX_Bitmap::c_InvalidColor : color;
465+
bitmap->m_palBitmap.transparentColor = stack.Arg1().NumericByRef().u4;
466+
bitmap->m_palBitmap.transparentColorSet = PAL_GFX_Bitmap::c_TransparentColorSet;
470467

471468
NANOCLR_NOCLEANUP();
472469
}
@@ -1177,7 +1174,7 @@ HRESULT GetBitmap(CLR_RT_HeapBlock *pThis, bool fForWrite, CLR_GFX_Bitmap *&bitm
11771174
FAULT_ON_NULL(pThis);
11781175

11791176
NANOCLR_CHECK_HRESULT(
1180-
CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));
1177+
CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(pThis[CLR_GFX_Bitmap::FIELD__m_bitmap], bitmap));
11811178

11821179
if ((bitmap->m_bm.m_flags & CLR_GFX_BitmapDescription::c_ReadOnly) && fForWrite)
11831180
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);

src/nanoFramework.Graphics/Graphics/Native/nanoFramework_Graphics_nanoFramework_UI_Ink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ HRESULT Library_nanoFramework_Graphics_nanoFramework_UI_Ink::
4747
// we are drawing on the object in the PAL therefore it should not move
4848
m_InkPinnedBitmap->Pin();
4949

50-
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromGraphicsHeapBlock(
50+
NANOCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromManagedCSharpReference(
5151
m_InkPinnedBitmap[CLR_GFX_Bitmap::FIELD__m_bitmap],
5252
bitmap));
5353

0 commit comments

Comments
 (0)