3 * Image handling functions
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "../refind/global.h"
39 #include "../refind/lib.h"
40 #include "../refind/screen.h"
41 #include "../include/refit_call_wrapper.h"
44 #define MAX_FILE_SIZE (1024*1024*1024)
46 #ifndef __MAKEWITH_GNUEFI
47 #define LibLocateHandle gBS->LocateHandleBuffer
48 #define LibOpenRoot EfiLibOpenRoot
52 // Basic image handling
55 EG_IMAGE
* egCreateImage(IN UINTN Width
, IN UINTN Height
, IN BOOLEAN HasAlpha
)
59 NewImage
= (EG_IMAGE
*) AllocatePool(sizeof(EG_IMAGE
));
62 NewImage
->PixelData
= (EG_PIXEL
*) AllocatePool(Width
* Height
* sizeof(EG_PIXEL
));
63 if (NewImage
->PixelData
== NULL
) {
68 NewImage
->Width
= Width
;
69 NewImage
->Height
= Height
;
70 NewImage
->HasAlpha
= HasAlpha
;
74 EG_IMAGE
* egCreateFilledImage(IN UINTN Width
, IN UINTN Height
, IN BOOLEAN HasAlpha
, IN EG_PIXEL
*Color
)
78 NewImage
= egCreateImage(Width
, Height
, HasAlpha
);
82 egFillImage(NewImage
, Color
);
86 EG_IMAGE
* egCopyImage(IN EG_IMAGE
*Image
)
90 NewImage
= egCreateImage(Image
->Width
, Image
->Height
, Image
->HasAlpha
);
94 CopyMem(NewImage
->PixelData
, Image
->PixelData
, Image
->Width
* Image
->Height
* sizeof(EG_PIXEL
));
98 // Returns a smaller image composed of the specified crop area from the larger area.
99 // If the specified area is larger than is in the original, returns NULL.
100 EG_IMAGE
* egCropImage(IN EG_IMAGE
*Image
, IN UINTN StartX
, IN UINTN StartY
, IN UINTN Width
, IN UINTN Height
) {
101 EG_IMAGE
*NewImage
= NULL
;
104 if (((StartX
+ Width
) > Image
->Width
) || ((StartY
+ Height
) > Image
->Height
))
107 NewImage
= egCreateImage(Width
, Height
, Image
->HasAlpha
);
108 if (NewImage
== NULL
)
111 for (y
= 0; y
< Height
; y
++) {
112 for (x
= 0; x
< Width
; x
++) {
113 NewImage
->PixelData
[y
* NewImage
->Width
+ x
] = Image
->PixelData
[(y
+ StartY
) * Image
->Width
+ x
+ StartX
];
117 } // EG_IMAGE * egCropImage()
119 VOID
egFreeImage(IN EG_IMAGE
*Image
)
122 if (Image
->PixelData
!= NULL
)
123 FreePool(Image
->PixelData
);
129 // Basic file operations
132 EFI_STATUS
egLoadFile(IN EFI_FILE
* BaseDir
, IN CHAR16
*FileName
,
133 OUT UINT8
**FileData
, OUT UINTN
*FileDataLength
)
136 EFI_FILE_HANDLE FileHandle
;
137 EFI_FILE_INFO
*FileInfo
;
142 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
143 if (EFI_ERROR(Status
)) {
147 FileInfo
= LibFileInfo(FileHandle
);
148 if (FileInfo
== NULL
) {
149 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
150 return EFI_NOT_FOUND
;
152 ReadSize
= FileInfo
->FileSize
;
153 if (ReadSize
> MAX_FILE_SIZE
)
154 ReadSize
= MAX_FILE_SIZE
;
157 BufferSize
= (UINTN
)ReadSize
; // was limited to 1 GB above, so this is safe
158 Buffer
= (UINT8
*) AllocatePool(BufferSize
);
159 if (Buffer
== NULL
) {
160 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
161 return EFI_OUT_OF_RESOURCES
;
164 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &BufferSize
, Buffer
);
165 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
166 if (EFI_ERROR(Status
)) {
172 *FileDataLength
= BufferSize
;
176 static EFI_GUID ESPGuid
= { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
178 static EFI_STATUS
egFindESP(OUT EFI_FILE_HANDLE
*RootDir
)
181 UINTN HandleCount
= 0;
184 Status
= LibLocateHandle(ByProtocol
, &ESPGuid
, NULL
, &HandleCount
, &Handles
);
185 if (!EFI_ERROR(Status
) && HandleCount
> 0) {
186 *RootDir
= LibOpenRoot(Handles
[0]);
187 if (*RootDir
== NULL
)
188 Status
= EFI_NOT_FOUND
;
194 EFI_STATUS
egSaveFile(IN EFI_FILE
* BaseDir OPTIONAL
, IN CHAR16
*FileName
,
195 IN UINT8
*FileData
, IN UINTN FileDataLength
)
198 EFI_FILE_HANDLE FileHandle
;
201 if (BaseDir
== NULL
) {
202 Status
= egFindESP(&BaseDir
);
203 if (EFI_ERROR(Status
))
207 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
,
208 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0);
209 if (EFI_ERROR(Status
))
212 BufferSize
= FileDataLength
;
213 Status
= refit_call3_wrapper(FileHandle
->Write
, FileHandle
, &BufferSize
, FileData
);
214 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
220 // Loading images from files and embedded data
223 static CHAR16
* egFindExtension(IN CHAR16
*FileName
)
227 for (i
= StrLen(FileName
); i
>= 0; i
--) {
228 if (FileName
[i
] == '.')
229 return FileName
+ i
+ 1;
230 if (FileName
[i
] == '/' || FileName
[i
] == '\\')
233 return FileName
+ StrLen(FileName
);
236 // Decode the specified data as the specified format. The IconSize parameter is
237 // relevant only for ICNS, for which it selects which ICNS sub-image is decoded.
238 // Returns a pointer to the resulting EG_IMAGE or NULL if decoding failed.
239 static EG_IMAGE
* egDecodeAny(IN UINT8
*FileData
, IN UINTN FileDataLength
,
240 IN CHAR16
*Format
, IN UINTN IconSize
, IN BOOLEAN WantAlpha
)
242 EG_IMAGE
*NewImage
= NULL
;
244 // Note: The UEFI implementation in Gigabyte's Hybrid EFI is buggy and does
245 // a case-sensitive comparison in StriCmp rather than the case-insensitive
246 // comparison that the spec says should be done. As a workaround, we repeat
247 // the comparison twice here.
248 // dispatch by extension
249 if ((StriCmp(Format
, L
"BMP") == 0) || (StriCmp(Format
, L
"bmp") == 0)) {
250 NewImage
= egDecodeBMP(FileData
, FileDataLength
, IconSize
, WantAlpha
);
251 } else if ((StriCmp(Format
, L
"ICNS") == 0) || (StriCmp(Format
, L
"icns") == 0)) {
252 NewImage
= egDecodeICNS(FileData
, FileDataLength
, IconSize
, WantAlpha
);
253 } else if ((StriCmp(Format
, L
"PNG") == 0) || (StriCmp(Format
, L
"png") == 0)) {
254 NewImage
= egDecodePNG(FileData
, FileDataLength
, IconSize
, WantAlpha
);
260 EG_IMAGE
* egLoadImage(IN EFI_FILE
* BaseDir
, IN CHAR16
*FileName
, IN BOOLEAN WantAlpha
)
264 UINTN FileDataLength
;
267 if (BaseDir
== NULL
|| FileName
== NULL
)
271 Status
= egLoadFile(BaseDir
, FileName
, &FileData
, &FileDataLength
);
272 if (EFI_ERROR(Status
))
276 NewImage
= egDecodeAny(FileData
, FileDataLength
, egFindExtension(FileName
), 128, WantAlpha
);
282 // Load an icon from (BaseDir)/Path, extracting the icon of size IconSize x IconSize.
283 // If the initial attempt is unsuccessful, try again, replacing the directory
284 // component of Path with DEFAULT_ICONS_DIR.
285 // Note: The assumption is that BaseDir points to rEFInd's home directory and Path
286 // includes one subdirectory level. If this changes in future revisions, it may be
287 // necessary to alter the code that tries again with DEFAULT_ICONS_DIR.
288 // Returns a pointer to the image data, or NULL if the icon could not be loaded.
289 EG_IMAGE
* egLoadIcon(IN EFI_FILE
* BaseDir
, IN CHAR16
*Path
, IN UINTN IconSize
)
293 UINTN FileDataLength
;
294 CHAR16
*FileName
, FileName2
[256];
297 if (BaseDir
== NULL
|| Path
== NULL
)
301 Status
= egLoadFile(BaseDir
, Path
, &FileData
, &FileDataLength
);
302 if (EFI_ERROR(Status
)) {
303 FileName
= Basename(Path
); // Note: FileName is a pointer within Path; DON'T FREE IT!
304 SPrint(FileName2
, 255, L
"%s\\%s", DEFAULT_ICONS_DIR
, FileName
);
305 Status
= egLoadFile(BaseDir
, FileName2
, &FileData
, &FileDataLength
);
306 if (EFI_ERROR(Status
))
311 NewImage
= egDecodeAny(FileData
, FileDataLength
, egFindExtension(Path
), IconSize
, TRUE
);
313 if ((NewImage
->Width
!= IconSize
) || (NewImage
->Height
!= IconSize
)) {
314 Print(L
"Warning: Attempt to load icon of the wrong size from '%s'\n", Path
);
315 MyFreePool(NewImage
);
320 } // EG_IMAGE *egLoadIcon()
322 EG_IMAGE
* egDecodeImage(IN UINT8
*FileData
, IN UINTN FileDataLength
, IN CHAR16
*Format
, IN BOOLEAN WantAlpha
)
324 return egDecodeAny(FileData
, FileDataLength
, Format
, 128, WantAlpha
);
327 EG_IMAGE
* egPrepareEmbeddedImage(IN EG_EMBEDDED_IMAGE
*EmbeddedImage
, IN BOOLEAN WantAlpha
)
335 if (EmbeddedImage
->PixelMode
> EG_MAX_EIPIXELMODE
||
336 (EmbeddedImage
->CompressMode
!= EG_EICOMPMODE_NONE
&& EmbeddedImage
->CompressMode
!= EG_EICOMPMODE_RLE
))
339 // allocate image structure and pixel buffer
340 NewImage
= egCreateImage(EmbeddedImage
->Width
, EmbeddedImage
->Height
, WantAlpha
);
341 if (NewImage
== NULL
)
344 CompData
= (UINT8
*)EmbeddedImage
->Data
; // drop const
345 CompLen
= EmbeddedImage
->DataLength
;
346 PixelCount
= EmbeddedImage
->Width
* EmbeddedImage
->Height
;
348 // FUTURE: for EG_EICOMPMODE_EFICOMPRESS, decompress whole data block here
350 if (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY
||
351 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY_ALPHA
) {
353 // copy grayscale plane and expand
354 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
355 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, r
), PixelCount
);
357 egInsertPlane(CompData
, PLPTR(NewImage
, r
), PixelCount
);
358 CompData
+= PixelCount
;
360 egCopyPlane(PLPTR(NewImage
, r
), PLPTR(NewImage
, g
), PixelCount
);
361 egCopyPlane(PLPTR(NewImage
, r
), PLPTR(NewImage
, b
), PixelCount
);
363 } else if (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR
||
364 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR_ALPHA
) {
367 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
368 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, r
), PixelCount
);
369 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, g
), PixelCount
);
370 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, b
), PixelCount
);
372 egInsertPlane(CompData
, PLPTR(NewImage
, r
), PixelCount
);
373 CompData
+= PixelCount
;
374 egInsertPlane(CompData
, PLPTR(NewImage
, g
), PixelCount
);
375 CompData
+= PixelCount
;
376 egInsertPlane(CompData
, PLPTR(NewImage
, b
), PixelCount
);
377 CompData
+= PixelCount
;
382 // set color planes to black
383 egSetPlane(PLPTR(NewImage
, r
), 0, PixelCount
);
384 egSetPlane(PLPTR(NewImage
, g
), 0, PixelCount
);
385 egSetPlane(PLPTR(NewImage
, b
), 0, PixelCount
);
389 if (WantAlpha
&& (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY_ALPHA
||
390 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR_ALPHA
||
391 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_ALPHA
)) {
394 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
395 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, a
), PixelCount
);
397 egInsertPlane(CompData
, PLPTR(NewImage
, a
), PixelCount
);
398 CompData
+= PixelCount
;
402 egSetPlane(PLPTR(NewImage
, a
), WantAlpha
? 255 : 0, PixelCount
);
412 VOID
egRestrictImageArea(IN EG_IMAGE
*Image
,
413 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
414 IN OUT UINTN
*AreaWidth
, IN OUT UINTN
*AreaHeight
)
416 if (AreaPosX
>= Image
->Width
|| AreaPosY
>= Image
->Height
) {
417 // out of bounds, operation has no effect
421 // calculate affected area
422 if (*AreaWidth
> Image
->Width
- AreaPosX
)
423 *AreaWidth
= Image
->Width
- AreaPosX
;
424 if (*AreaHeight
> Image
->Height
- AreaPosY
)
425 *AreaHeight
= Image
->Height
- AreaPosY
;
429 VOID
egFillImage(IN OUT EG_IMAGE
*CompImage
, IN EG_PIXEL
*Color
)
436 if (!CompImage
->HasAlpha
)
439 PixelPtr
= CompImage
->PixelData
;
440 for (i
= 0; i
< CompImage
->Width
* CompImage
->Height
; i
++, PixelPtr
++)
441 *PixelPtr
= FillColor
;
444 VOID
egFillImageArea(IN OUT EG_IMAGE
*CompImage
,
445 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
446 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
452 EG_PIXEL
*PixelBasePtr
;
454 egRestrictImageArea(CompImage
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
458 if (!CompImage
->HasAlpha
)
461 PixelBasePtr
= CompImage
->PixelData
+ AreaPosY
* CompImage
->Width
+ AreaPosX
;
462 for (y
= 0; y
< AreaHeight
; y
++) {
463 PixelPtr
= PixelBasePtr
;
464 for (x
= 0; x
< AreaWidth
; x
++, PixelPtr
++)
465 *PixelPtr
= FillColor
;
466 PixelBasePtr
+= CompImage
->Width
;
471 VOID
egRawCopy(IN OUT EG_PIXEL
*CompBasePtr
, IN EG_PIXEL
*TopBasePtr
,
472 IN UINTN Width
, IN UINTN Height
,
473 IN UINTN CompLineOffset
, IN UINTN TopLineOffset
)
476 EG_PIXEL
*TopPtr
, *CompPtr
;
478 for (y
= 0; y
< Height
; y
++) {
480 CompPtr
= CompBasePtr
;
481 for (x
= 0; x
< Width
; x
++) {
485 TopBasePtr
+= TopLineOffset
;
486 CompBasePtr
+= CompLineOffset
;
490 VOID
egRawCompose(IN OUT EG_PIXEL
*CompBasePtr
, IN EG_PIXEL
*TopBasePtr
,
491 IN UINTN Width
, IN UINTN Height
,
492 IN UINTN CompLineOffset
, IN UINTN TopLineOffset
)
495 EG_PIXEL
*TopPtr
, *CompPtr
;
500 for (y
= 0; y
< Height
; y
++) {
502 CompPtr
= CompBasePtr
;
503 for (x
= 0; x
< Width
; x
++) {
505 RevAlpha
= 255 - Alpha
;
506 Temp
= (UINTN
)CompPtr
->b
* RevAlpha
+ (UINTN
)TopPtr
->b
* Alpha
+ 0x80;
507 CompPtr
->b
= (Temp
+ (Temp
>> 8)) >> 8;
508 Temp
= (UINTN
)CompPtr
->g
* RevAlpha
+ (UINTN
)TopPtr
->g
* Alpha
+ 0x80;
509 CompPtr
->g
= (Temp
+ (Temp
>> 8)) >> 8;
510 Temp
= (UINTN
)CompPtr
->r
* RevAlpha
+ (UINTN
)TopPtr
->r
* Alpha
+ 0x80;
511 CompPtr
->r
= (Temp
+ (Temp
>> 8)) >> 8;
513 CompPtr->b = ((UINTN)CompPtr->b * RevAlpha + (UINTN)TopPtr->b * Alpha) / 255;
514 CompPtr->g = ((UINTN)CompPtr->g * RevAlpha + (UINTN)TopPtr->g * Alpha) / 255;
515 CompPtr->r = ((UINTN)CompPtr->r * RevAlpha + (UINTN)TopPtr->r * Alpha) / 255;
519 TopBasePtr
+= TopLineOffset
;
520 CompBasePtr
+= CompLineOffset
;
524 VOID
egComposeImage(IN OUT EG_IMAGE
*CompImage
, IN EG_IMAGE
*TopImage
, IN UINTN PosX
, IN UINTN PosY
)
526 UINTN CompWidth
, CompHeight
;
528 CompWidth
= TopImage
->Width
;
529 CompHeight
= TopImage
->Height
;
530 egRestrictImageArea(CompImage
, PosX
, PosY
, &CompWidth
, &CompHeight
);
534 // if (CompImage->HasAlpha) {
535 // CompImage->HasAlpha = FALSE;
536 // egSetPlane(PLPTR(CompImage, a), 0, CompImage->Width * CompImage->Height);
539 if (TopImage
->HasAlpha
) {
540 egRawCompose(CompImage
->PixelData
+ PosY
* CompImage
->Width
+ PosX
, TopImage
->PixelData
,
541 CompWidth
, CompHeight
, CompImage
->Width
, TopImage
->Width
);
543 egRawCopy(CompImage
->PixelData
+ PosY
* CompImage
->Width
+ PosX
, TopImage
->PixelData
,
544 CompWidth
, CompHeight
, CompImage
->Width
, TopImage
->Width
);
549 EG_IMAGE
* egEnsureImageSize(IN EG_IMAGE
*Image
, IN UINTN Width
, IN UINTN Height
, IN EG_PIXEL
*Color
)
555 if (Image
->Width
== Width
&& Image
->Height
== Height
)
558 NewImage
= egCreateFilledImage(Width
, Height
, Image
->HasAlpha
, Color
);
559 if (NewImage
== NULL
) {
563 egComposeImage(NewImage
, Image
, 0, 0);
570 // misc internal functions
573 VOID
egInsertPlane(IN UINT8
*SrcDataPtr
, IN UINT8
*DestPlanePtr
, IN UINTN PixelCount
)
577 for (i
= 0; i
< PixelCount
; i
++) {
578 *DestPlanePtr
= *SrcDataPtr
++;
583 VOID
egSetPlane(IN UINT8
*DestPlanePtr
, IN UINT8 Value
, IN UINTN PixelCount
)
587 for (i
= 0; i
< PixelCount
; i
++) {
588 *DestPlanePtr
= Value
;
593 VOID
egCopyPlane(IN UINT8
*SrcPlanePtr
, IN UINT8
*DestPlanePtr
, IN UINTN PixelCount
)
597 for (i
= 0; i
< PixelCount
; i
++) {
598 *DestPlanePtr
= *SrcPlanePtr
;
599 DestPlanePtr
+= 4, SrcPlanePtr
+= 4;