implement PNG coding in ximage

Signed-off-by: Sergey Isakov <isakov-sl@bk.ru>
This commit is contained in:
Sergey Isakov 2020-02-26 09:05:14 +03:00
parent 8727ae6f54
commit 1946bf5838
7 changed files with 69 additions and 25 deletions

View File

@ -13,12 +13,6 @@
//#define fabsf(x) ((x >= 0.0f)?x:(-x))
#define fabsf(x) FabsF(x)
float FabsF(float x)
{
if (x < 0.f) return -x;
return x;
}
//we will assume sqrt(abs(x))
float SqrtF(float X)
{

View File

@ -39,6 +39,14 @@ float rndf(void); //random number from 0 to 1.0f
int dither(float x, int level);
float nsvg__vmag(float x, float y); //sqrt(x*x+y*y)
inline float FabsF(float x) {
if (x < 0.f) return -x;
return x;
}
inline float SqrF(float x) { return x*x; }
RETURN_STATUS
AsciiStrToFloat(IN CONST CHAR8 *String,
OUT CHAR8 **EndPointer, OPTIONAL

View File

@ -52,17 +52,18 @@ UINTN XImage::GetSize() const
void XImage::Fill(EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color)
{
for (UINTN i = 0; i < Height; ++i)
for (UINTN j = 0; j < Width; ++j)
PixelData[i*j] = Color;
for (UINTN y = 0; y < Height; ++y)
for (UINTN x = 0; x < Width; ++x)
PixelData[y * Width + x] = Color;
}
void XImage::FillArea(EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color, const EgRect& Rect)
{
for (UINTN y = Rect.Ypos; y < Rect.Height && y < Height; ++y) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL* Ptr = PixelData + y * Width + Rect.Xpos;
for (UINTN x = Rect.Xpos; x < Rect.Width && x < Width; ++x)
*Ptr++ = Color;
for (UINTN y = Rect.Ypos; y < Height && (y - Rect.Ypos) < Rect.Height; ++y) {
// EFI_GRAPHICS_OUTPUT_BLT_PIXEL* Ptr = PixelData + y * Width + Rect.Xpos;
for (UINTN x = Rect.Xpos; x < Width && (x - Rect.Xpos) < Rect.Width; ++x)
// *Ptr++ = Color;
PixelData[y * Width + x] = Color;
}
}
@ -75,9 +76,9 @@ void XImage::Compose(int PosX, int PosY, const XImage& TopImage, bool Lowest) //
UINT32 FinalAlpha;
UINT32 Temp;
for (UINTN y = PosY; y < Height && (y - PosY) < TopImage.GetHeight(); y++) {
for (UINTN y = PosY; y < Height && (y - PosY) < TopImage.GetHeight(); ++y) {
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++) {
for (UINTN x = PosX; x < Width && (x - PosX) < TopImage.GetWidth(); ++x) {
TopAlpha = TopImage.GetPixel(x-PosX, y-PosY).Reserved;
RevAlpha = 255 - TopAlpha;
FinalAlpha = (255*255 - RevAlpha*(255 - CompPtr.Reserved)) / 255;
@ -102,4 +103,37 @@ void XImage::Compose(int PosX, int PosY, const XImage& TopImage, bool Lowest) //
}
}
void XImage::FlipRB(bool WantAlpha)
{
UINTN ImageSize = (Width * Height);
EFI_GRAPHICS_OUTPUT_BLT_PIXEL* Pixel = GetPixelPtr(0,0);
for (UINTN i = 0; i < ImageSize; ++i) {
UINT8 Temp = Pixel->Blue;
Pixel->Blue = Pixel->Red;
Pixel->Red = Temp;
if (!WantAlpha) Pixel->Reserved = 0xFF;
Pixel++;
}
}
unsigned XImage::FromPNG(const uint8_t * Data, UINTN Length, bool WantAlpha)
{
unsigned Error = 0;
uint8_t * PixelPtr = (uint8_t *)&PixelData[0];
Error = eglodepng_decode(&PixelPtr, &Width, &Height, Data, Length);
FlipRB(WantAlpha);
return Error;
}
unsigned XImage::ToPNG(uint8_t** Data, UINTN& OutSize)
{
size_t FileDataLength = 0;
FlipRB(false);
uint8_t * PixelPtr = (uint8_t *)&PixelData[0];
unsigned lode_return = eglodepng_encode(Data, &FileDataLength, PixelPtr, Width, Height);
OutSize = FileDataLength;
return lode_return;
}

View File

@ -7,16 +7,18 @@ This class will replace EG_IMAGE structure and methods
#include "../cpp_foundation/XToolsCommon.h"
#include "../cpp_foundation/XArray.h"
#include "lodepng.h"
#include <Platform.h>
/*
#if 0 //ndef EFI_GRAPHICS_OUTPUT_BLT_PIXEL
typedef struct {
UINT8 Blue;
UINT8 Green;
UINT8 Red;
UINT8 Reserved; //this is Alpha. 0 means full transparent, 0xFF means opaque
} EFI_GRAPHICS_OUTPUT_BLT_PIXEL;
#endif
/*
typedef union {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL Pixel;
UINT32 Raw;
@ -59,6 +61,9 @@ public:
void Fill(EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color = { 0, 0, 0, 0 });
void FillArea(EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color, const EgRect& Rect);
void Compose(int PosX, int PosY, const XImage& TopImage, bool Lowest);
void FlipRB(bool WantAlpha);
unsigned FromPNG(const uint8_t * Data, UINTN Lenght, bool WantAlpha); //WantAlpha always true?
unsigned ToPNG(uint8_t** Data, UINTN& OutSize);
};
#endif //__XSTRINGW_H__

View File

@ -844,7 +844,7 @@ EG_IMAGE * egEnsureImageSize(IN EG_IMAGE *Image, IN INTN Width, IN INTN Height,
//
// misc internal functions
//
//these functions used for icns, not with png
VOID egInsertPlane(IN UINT8 *SrcDataPtr, IN UINT8 *DestPlanePtr, IN UINTN PixelCount)
{
UINTN i;

View File

@ -6312,8 +6312,8 @@ unsigned eglodepng_encode(unsigned char** out, size_t* outsize, const unsigned c
#ifdef LODEPNG_COMPILE_DECODER
unsigned eglodepng_decode(unsigned char** out, size_t* w, size_t* h, const unsigned char* in, size_t insize)
{
unsigned _w, _h, _r;
_r = lodepng_decode32(out, &_w, &_h, in, insize);
unsigned _w = 0, _h = 0, _r;
_r = lodepng_decode_memory(out, &_w, &_h, in, insize, LCT_RGBA, 8);
if (!_r) {
if (w) *w = (size_t) _w;
if (h) *h = (size_t) _h;

View File

@ -64,7 +64,6 @@
//#define fabsf(x) ((x >= 0.0f)?x:(-x))
#define fabsf(x) FabsF(x)
#define MALCOLM 1
static void renderShape(NSVGrasterizer* r,
NSVGshape* shape, float *xform, float min_scale);
@ -199,7 +198,7 @@ static int nsvg__ptEquals(NSVGpoint* pt1, NSVGpoint* pt2, float tol)
{
float dx = pt2->x - pt1->x;
float dy = pt2->y - pt1->y;
return dx*dx + dy*dy < tol*tol;
return SqrF(dx) + SqrF(dy) < SqrF(tol);
}
// t is a matrix xform
@ -313,7 +312,8 @@ static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float
static float nsvg__normalize(float *x, float* y)
{
float d = sqrtf((*x)*(*x) + (*y)*(*y));
// float d = sqrtf((*x)*(*x) + (*y)*(*y));
float d = SqrtF(SqrF(*x) + SqrF(*x));
if (d > 1e-6f) {
float id = 1.0f / d;
*x *= id;
@ -322,8 +322,11 @@ static float nsvg__normalize(float *x, float* y)
return d;
}
static float nsvg__absf(float x) { return x < 0 ? -x : x; }
static float nsvg__sqr(float x) { return x*x; }
//static float nsvg__absf(float x) { return x < 0 ? -x : x; }
#define nsvg__absf(x) FabsF(x)
//static float nsvg__sqr(float x) { return x*x; }
#define nsvg__sqr(x) SqrF(x)
// 0 1 2 3 4 5 6 7
static float nsvg__controlPathLength(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
{