File: lodepng_xtra.c

package info (click to toggle)
refind 0.14.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,432 kB
  • sloc: ansic: 52,757; sh: 2,086; python: 592; makefile: 351; perl: 5
file content (144 lines) | stat: -rw-r--r-- 4,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Additional functions to support LodePNG for use in rEFInd
 *
 * copyright (c) 2013 by by Roderick W. Smith, and distributed
 * under the terms of the GNU GPL v3, or (at your option) any
 * later version.
 *
 * See http://lodev.org/lodepng/ for the original LodePNG.
 *
 */
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "global.h"
#include "../refind/screen.h"
#include "../refind/lib.h"
#include "lodepng.h"

// EFI's equivalent of realloc requires the original buffer's size as an
// input parameter, which the standard libc realloc does not require. Thus,
// I've modified lodepng_malloc() to allocate more memory to store this data,
// and lodepng_realloc() can then read it when required. Because the size is
// stored at the start of the allocated area, these functions are NOT
// interchangeable with the standard EFI functions; memory allocated via
// lodepng_malloc() should be freed via lodepng_free(), and myfree() should
// NOT be used with memory allocated via AllocatePool() or AllocateZeroPool()!

void* lodepng_malloc(size_t size) {
   void *ptr;

   ptr = AllocateZeroPool(size + sizeof(size_t));
   if (ptr) {
      *(size_t *) ptr = size;
      return ((size_t *) ptr) + 1;
   } else {
      return NULL;
   }
} // void* lodepng_malloc()

void lodepng_free (void *ptr) {
   if (ptr) {
      ptr = (void *) (((size_t *) ptr) - 1);
      MyFreePool(ptr);
   }
} // void lodepng_free()

static size_t report_size(void *ptr) {
   if (ptr)
      return * (((size_t *) ptr) - 1);
   else
      return 0;
} // size_t report_size()

void* lodepng_realloc(void *ptr, size_t new_size) {
   size_t *new_pool;
   size_t old_size;

   new_pool = lodepng_malloc(new_size);
   if (new_pool && ptr) {
      old_size = report_size(ptr);
      MyCopyMem(new_pool, ptr, (old_size < new_size) ? old_size : new_size);
   }
   return new_pool;
} // lodepng_realloc()

// Finds length of ASCII string, which MUST be NULL-terminated.
int MyStrlen(const char *InString) {
   int Length = 0;

   if (InString) {
      while (InString[Length] != '\0')
         Length++;
   }
   return Length;
} // int MyStrlen()

VOID *MyMemSet(VOID *s, int c, size_t n) {
    SetMem(s, c, n);
    return s;
}

VOID *MyMemCpy(void *__restrict __dest, const void *__restrict __src, size_t __n) {
    MyCopyMem(__dest, __src, __n);
    return __dest;
}

typedef struct _lode_color {
   UINT8 red;
   UINT8 green;
   UINT8 blue;
   UINT8 alpha;
} lode_color;

EG_IMAGE * egDecodePNG(IN UINT8 *FileData, IN UINTN FileDataLength, IN UINTN IconSize, IN BOOLEAN WantAlpha) {
   EG_IMAGE *NewImage = NULL;
   unsigned Error, Width, Height;
   EG_PIXEL *PixelData;
   lode_color *LodeData;
   UINTN i;

   Error = lodepng_decode_memory((unsigned char **) &PixelData, &Width, &Height, (unsigned char*) FileData,
                                 (size_t) FileDataLength, LCT_RGBA, 8);

   if (Error) {
      return NULL;
   }

   // allocate image structure and buffer
   NewImage = egCreateImage(Width, Height, WantAlpha);
   if (NewImage == NULL)
       return NULL;
   // The following really should never happen; just being paranoid....
   if ((NewImage->Width != Width) || (NewImage->Height != Height)) {
      egFreeImage(NewImage);
      return NULL;
   }

   LodeData = (lode_color *) PixelData;

   // Annoyingly, EFI and LodePNG use different ordering of RGB values in
   // their pixel data representations, so we've got to adjust them....
   for (i = 0; i < (NewImage->Height * NewImage->Width); i++) {
      NewImage->PixelData[i].r = LodeData[i].red;
      NewImage->PixelData[i].g = LodeData[i].green;
      NewImage->PixelData[i].b = LodeData[i].blue;
      if (WantAlpha)
         NewImage->PixelData[i].a = LodeData[i].alpha;
   }
   lodepng_free(PixelData);

   return NewImage;
} // EG_IMAGE * egDecodePNG()