]>
code.delx.au - refind/blob - libeg/lodepng_xtra.c
2 * Additional functions to support LodePNG for use in rEFInd
4 * copyright (c) 2013 by by Roderick W. Smith, and distributed
5 * under the terms of the GNU GPL v3.
7 * See http://lodev.org/lodepng/ for the original LodePNG.
12 #include "../refind/screen.h"
15 // EFI's equivalent of realloc requires the original buffer's size as an
16 // input parameter, which the standard libc realloc does not require. Thus,
17 // I've modified lodepng_malloc() to allocate more memory to store this data,
18 // and lodepng_realloc() can then read it when required. Because the size is
19 // stored at the start of the allocated area, these functions are NOT
20 // interchangeable with the standard EFI functions; memory allocated via
21 // lodepng_malloc() should be freed via lodepng_free(), and myfree() should
22 // NOT be used with memory allocated via AllocatePool() or AllocateZeroPool()!
24 void* lodepng_malloc(size_t size
) {
27 ptr
= AllocateZeroPool(size
+ sizeof(size_t));
29 *(size_t *) ptr
= size
;
30 return ((size_t *) ptr
) + 1;
34 } // void* lodepng_malloc()
36 void lodepng_free (void *ptr
) {
38 ptr
= (void *) (((size_t *) ptr
) - 1);
41 } // void lodepng_free()
43 static size_t report_size(void *ptr
) {
45 return * (((size_t *) ptr
) - 1);
48 } // size_t report_size()
50 void* lodepng_realloc(void *ptr
, size_t new_size
) {
54 new_pool
= lodepng_malloc(new_size
);
55 if (new_pool
&& ptr
) {
56 old_size
= report_size(ptr
);
57 CopyMem(new_pool
, ptr
, (old_size
< new_size
) ? old_size
: new_size
);
60 } // lodepng_realloc()
62 // Finds length of ASCII string, which MUST be NULL-terminated.
63 int MyStrlen(const char *InString
) {
67 while (InString
[Length
] != '\0')
73 typedef struct _lode_color
{
80 EG_IMAGE
* egDecodePNG(IN UINT8
*FileData
, IN UINTN FileDataLength
, IN UINTN IconSize
, IN BOOLEAN WantAlpha
) {
81 EG_IMAGE
*NewImage
= NULL
;
82 unsigned Error
, Width
, Height
;
87 Error
= lodepng_decode_memory((unsigned char **) &PixelData
, &Width
, &Height
, (unsigned char*) FileData
,
88 (size_t) FileDataLength
, LCT_RGBA
, 8);
94 // allocate image structure and buffer
95 NewImage
= egCreateImage(Width
, Height
, WantAlpha
);
96 if ((NewImage
== NULL
) || (NewImage
->Width
!= Width
) || (NewImage
->Height
!= Height
))
99 LodeData
= (lode_color
*) PixelData
;
101 // Annoyingly, EFI and LodePNG use different ordering of RGB values in
102 // their pixel data representations, so we've got to adjust them....
103 for (i
= 0; i
< (NewImage
->Height
* NewImage
->Width
); i
++) {
104 NewImage
->PixelData
[i
].r
= LodeData
[i
].red
;
105 NewImage
->PixelData
[i
].g
= LodeData
[i
].green
;
106 NewImage
->PixelData
[i
].b
= LodeData
[i
].blue
;
108 NewImage
->PixelData
[i
].a
= LodeData
[i
].alpha
;
110 lodepng_free(PixelData
);
113 } // EG_IMAGE * egDecodePNG()