]>
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, or (at your option) any
8 * See http://lodev.org/lodepng/ for the original LodePNG.
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "../refind/screen.h"
30 // EFI's equivalent of realloc requires the original buffer's size as an
31 // input parameter, which the standard libc realloc does not require. Thus,
32 // I've modified lodepng_malloc() to allocate more memory to store this data,
33 // and lodepng_realloc() can then read it when required. Because the size is
34 // stored at the start of the allocated area, these functions are NOT
35 // interchangeable with the standard EFI functions; memory allocated via
36 // lodepng_malloc() should be freed via lodepng_free(), and myfree() should
37 // NOT be used with memory allocated via AllocatePool() or AllocateZeroPool()!
39 void* lodepng_malloc(size_t size
) {
42 ptr
= AllocateZeroPool(size
+ sizeof(size_t));
44 *(size_t *) ptr
= size
;
45 return ((size_t *) ptr
) + 1;
49 } // void* lodepng_malloc()
51 void lodepng_free (void *ptr
) {
53 ptr
= (void *) (((size_t *) ptr
) - 1);
56 } // void lodepng_free()
58 static size_t report_size(void *ptr
) {
60 return * (((size_t *) ptr
) - 1);
63 } // size_t report_size()
65 void* lodepng_realloc(void *ptr
, size_t new_size
) {
69 new_pool
= lodepng_malloc(new_size
);
70 if (new_pool
&& ptr
) {
71 old_size
= report_size(ptr
);
72 CopyMem(new_pool
, ptr
, (old_size
< new_size
) ? old_size
: new_size
);
75 } // lodepng_realloc()
77 // Finds length of ASCII string, which MUST be NULL-terminated.
78 int MyStrlen(const char *InString
) {
82 while (InString
[Length
] != '\0')
88 VOID
*memset (VOID
*s
, int c
, size_t n
) {
93 VOID
*memcpy (void *__restrict __dest
, const void *__restrict __src
, size_t __n
) {
94 CopyMem(__dest
, __src
, __n
);
98 typedef struct _lode_color
{
105 EG_IMAGE
* egDecodePNG(IN UINT8
*FileData
, IN UINTN FileDataLength
, IN UINTN IconSize
, IN BOOLEAN WantAlpha
) {
106 EG_IMAGE
*NewImage
= NULL
;
107 unsigned Error
, Width
, Height
;
109 lode_color
*LodeData
;
112 Error
= lodepng_decode_memory((unsigned char **) &PixelData
, &Width
, &Height
, (unsigned char*) FileData
,
113 (size_t) FileDataLength
, LCT_RGBA
, 8);
119 // allocate image structure and buffer
120 NewImage
= egCreateImage(Width
, Height
, WantAlpha
);
121 if ((NewImage
== NULL
) || (NewImage
->Width
!= Width
) || (NewImage
->Height
!= Height
))
124 LodeData
= (lode_color
*) PixelData
;
126 // Annoyingly, EFI and LodePNG use different ordering of RGB values in
127 // their pixel data representations, so we've got to adjust them....
128 for (i
= 0; i
< (NewImage
->Height
* NewImage
->Width
); i
++) {
129 NewImage
->PixelData
[i
].r
= LodeData
[i
].red
;
130 NewImage
->PixelData
[i
].g
= LodeData
[i
].green
;
131 NewImage
->PixelData
[i
].b
= LodeData
[i
].blue
;
133 NewImage
->PixelData
[i
].a
= LodeData
[i
].alpha
;
135 lodepng_free(PixelData
);
138 } // EG_IMAGE * egDecodePNG()