From 8727ae6f54d0b4b7a4d9ba364c020449c93147fa Mon Sep 17 00:00:00 2001 From: jief Date: Wed, 26 Feb 2020 00:28:36 +0300 Subject: [PATCH] Refactor of XImage to use XArray and create accessor for pixel. --- rEFIt_UEFI/libeg/XImage.cpp | 46 ++++++++++++++++++++----------------- rEFIt_UEFI/libeg/XImage.h | 8 +++++-- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/rEFIt_UEFI/libeg/XImage.cpp b/rEFIt_UEFI/libeg/XImage.cpp index be5f5014a..a75aaa90d 100644 --- a/rEFIt_UEFI/libeg/XImage.cpp +++ b/rEFIt_UEFI/libeg/XImage.cpp @@ -4,7 +4,6 @@ XImage::XImage() { Width = 0; Height = 0; - PixelData = nullptr; HasAlpha = true; } @@ -13,19 +12,29 @@ XImage::XImage(UINTN W, UINTN H, bool A) Width = W; Height = H; HasAlpha = A; - PixelData = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL*)Xalloc(GetSize()); + PixelData.CheckSize(GetWidth()*GetHeight()); } XImage::~XImage() { - Xfree(PixelData); } -const EFI_GRAPHICS_OUTPUT_BLT_PIXEL* XImage::GetData() const +const XArray& XImage::GetData() const { return PixelData; } +EFI_GRAPHICS_OUTPUT_BLT_PIXEL* XImage::GetPixelPtr(UINTN x, UINTN y) +{ + return &PixelData[x + y * Width]; +} + +const EFI_GRAPHICS_OUTPUT_BLT_PIXEL& XImage::GetPixel(UINTN x, UINTN y) const +{ + return PixelData[x + y * Width]; +} + + UINTN XImage::GetWidth() const { return Width; @@ -45,7 +54,7 @@ void XImage::Fill(EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color) { for (UINTN i = 0; i < Height; ++i) for (UINTN j = 0; j < Width; ++j) - *PixelData++ = Color; + PixelData[i*j] = Color; } void XImage::FillArea(EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color, const EgRect& Rect) @@ -65,35 +74,30 @@ void XImage::Compose(int PosX, int PosY, const XImage& TopImage, bool Lowest) // UINT32 RevAlpha; UINT32 FinalAlpha; UINT32 Temp; - const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *TopPtr; - EFI_GRAPHICS_OUTPUT_BLT_PIXEL *CompPtr; for (UINTN y = PosY; y < Height && (y - PosY) < TopImage.GetHeight(); y++) { - TopPtr = TopImage.GetData(); - CompPtr = PixelData + y * Width + PosX; + EFI_GRAPHICS_OUTPUT_BLT_PIXEL& CompPtr = *GetPixelPtr(PosX, y); // I assign a ref to avoid the operator ->. Compiler will produce the same anyway. for (UINTN x = PosX; x < Width && (x - PosX) < TopImage.GetWidth(); x++) { - TopAlpha = TopPtr->Reserved; + TopAlpha = TopImage.GetPixel(x-PosX, y-PosY).Reserved; RevAlpha = 255 - TopAlpha; - FinalAlpha = (255*255 - RevAlpha*(255 - CompPtr->Reserved)) / 255; + FinalAlpha = (255*255 - RevAlpha*(255 - CompPtr.Reserved)) / 255; //final alpha =(1-(1-x)*(1-y)) =(255*255-(255-topA)*(255-compA))/255 - Temp = ((UINT8)CompPtr->Blue * RevAlpha) + ((UINT8)TopPtr->Blue * TopAlpha); - CompPtr->Blue = (UINT8)(Temp / 255); + Temp = (CompPtr.Blue * RevAlpha) + (TopImage.GetPixel(x-PosX, y-PosY).Blue * TopAlpha); + CompPtr.Blue = (UINT8)(Temp / 255); - Temp = ((UINT8)CompPtr->Green * RevAlpha) + ((UINT8)TopPtr->Green * TopAlpha); - CompPtr->Green = (UINT8)(Temp / 255); + Temp = (CompPtr.Green * RevAlpha) + (TopImage.GetPixel(x-PosX, y-PosY).Green * TopAlpha); + CompPtr.Green = (UINT8)(Temp / 255); - Temp = ((UINT8)CompPtr->Red * RevAlpha) + ((UINT8)TopPtr->Red * TopAlpha); - CompPtr->Red = (UINT8)(Temp / 255); + Temp = (CompPtr.Red * RevAlpha) + (TopImage.GetPixel(x-PosX, y-PosY).Red * TopAlpha); + CompPtr.Red = (UINT8)(Temp / 255); if (Lowest) { - CompPtr->Reserved = (UINT8)(255); + CompPtr.Reserved = 255; } else { - CompPtr->Reserved = (UINT8)FinalAlpha; + CompPtr.Reserved = (UINT8)FinalAlpha; } - TopPtr++, CompPtr++; - } } } diff --git a/rEFIt_UEFI/libeg/XImage.h b/rEFIt_UEFI/libeg/XImage.h index d82c98ab2..5c7b4406e 100644 --- a/rEFIt_UEFI/libeg/XImage.h +++ b/rEFIt_UEFI/libeg/XImage.h @@ -6,6 +6,7 @@ This class will replace EG_IMAGE structure and methods #define __XSTRINGW_H__ #include "../cpp_foundation/XToolsCommon.h" +#include "../cpp_foundation/XArray.h" #include /* @@ -34,7 +35,7 @@ class XImage protected: UINTN Width; UINTN Height; - EFI_GRAPHICS_OUTPUT_BLT_PIXEL *PixelData; + XArray PixelData; bool HasAlpha; public: XImage(); @@ -48,7 +49,10 @@ protected: public: - const EFI_GRAPHICS_OUTPUT_BLT_PIXEL* GetData() const; + const XArray& GetData() const; + + const EFI_GRAPHICS_OUTPUT_BLT_PIXEL& GetPixel(UINTN x, UINTN y) const; + EFI_GRAPHICS_OUTPUT_BLT_PIXEL* GetPixelPtr(UINTN x, UINTN y); UINTN GetWidth() const; UINTN GetHeight() const;