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"
43 #define MAX_FILE_SIZE (1024*1024*1024)
45 #ifndef __MAKEWITH_GNUEFI
46 #define LibLocateHandle gBS->LocateHandleBuffer
47 #define LibOpenRoot EfiLibOpenRoot
51 // Basic image handling
54 EG_IMAGE
* egCreateImage(IN UINTN Width
, IN UINTN Height
, IN BOOLEAN HasAlpha
)
58 NewImage
= (EG_IMAGE
*) AllocatePool(sizeof(EG_IMAGE
));
61 NewImage
->PixelData
= (EG_PIXEL
*) AllocatePool(Width
* Height
* sizeof(EG_PIXEL
));
62 if (NewImage
->PixelData
== NULL
) {
67 NewImage
->Width
= Width
;
68 NewImage
->Height
= Height
;
69 NewImage
->HasAlpha
= HasAlpha
;
73 EG_IMAGE
* egCreateFilledImage(IN UINTN Width
, IN UINTN Height
, IN BOOLEAN HasAlpha
, IN EG_PIXEL
*Color
)
77 NewImage
= egCreateImage(Width
, Height
, HasAlpha
);
81 egFillImage(NewImage
, Color
);
85 EG_IMAGE
* egCopyImage(IN EG_IMAGE
*Image
)
89 NewImage
= egCreateImage(Image
->Width
, Image
->Height
, Image
->HasAlpha
);
93 CopyMem(NewImage
->PixelData
, Image
->PixelData
, Image
->Width
* Image
->Height
* sizeof(EG_PIXEL
));
97 // Returns a smaller image composed of the specified crop area from the larger area.
98 // If the specified area is larger than is in the original, returns NULL.
99 EG_IMAGE
* egCropImage(IN EG_IMAGE
*Image
, IN UINTN StartX
, IN UINTN StartY
, IN UINTN Width
, IN UINTN Height
) {
100 EG_IMAGE
*NewImage
= NULL
;
103 if (((StartX
+ Width
) > Image
->Width
) || ((StartY
+ Height
) > Image
->Height
))
106 NewImage
= egCreateImage(Width
, Height
, Image
->HasAlpha
);
107 if (NewImage
== NULL
)
110 for (y
= 0; y
< Height
; y
++) {
111 for (x
= 0; x
< Width
; x
++) {
112 NewImage
->PixelData
[y
* NewImage
->Width
+ x
] = Image
->PixelData
[(y
+ StartY
) * Image
->Width
+ x
+ StartX
];
116 } // EG_IMAGE * egCropImage()
118 VOID
egFreeImage(IN EG_IMAGE
*Image
)
121 if (Image
->PixelData
!= NULL
)
122 FreePool(Image
->PixelData
);
128 // Basic file operations
131 EFI_STATUS
egLoadFile(IN EFI_FILE
* BaseDir
, IN CHAR16
*FileName
,
132 OUT UINT8
**FileData
, OUT UINTN
*FileDataLength
)
135 EFI_FILE_HANDLE FileHandle
;
136 EFI_FILE_INFO
*FileInfo
;
141 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
142 if (EFI_ERROR(Status
)) {
146 FileInfo
= LibFileInfo(FileHandle
);
147 if (FileInfo
== NULL
) {
148 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
149 return EFI_NOT_FOUND
;
151 ReadSize
= FileInfo
->FileSize
;
152 if (ReadSize
> MAX_FILE_SIZE
)
153 ReadSize
= MAX_FILE_SIZE
;
156 BufferSize
= (UINTN
)ReadSize
; // was limited to 1 GB above, so this is safe
157 Buffer
= (UINT8
*) AllocatePool(BufferSize
);
158 if (Buffer
== NULL
) {
159 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
160 return EFI_OUT_OF_RESOURCES
;
163 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &BufferSize
, Buffer
);
164 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
165 if (EFI_ERROR(Status
)) {
171 *FileDataLength
= BufferSize
;
175 static EFI_GUID ESPGuid
= { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
177 static EFI_STATUS
egFindESP(OUT EFI_FILE_HANDLE
*RootDir
)
180 UINTN HandleCount
= 0;
183 Status
= LibLocateHandle(ByProtocol
, &ESPGuid
, NULL
, &HandleCount
, &Handles
);
184 if (!EFI_ERROR(Status
) && HandleCount
> 0) {
185 *RootDir
= LibOpenRoot(Handles
[0]);
186 if (*RootDir
== NULL
)
187 Status
= EFI_NOT_FOUND
;
193 EFI_STATUS
egSaveFile(IN EFI_FILE
* BaseDir OPTIONAL
, IN CHAR16
*FileName
,
194 IN UINT8
*FileData
, IN UINTN FileDataLength
)
197 EFI_FILE_HANDLE FileHandle
;
200 if (BaseDir
== NULL
) {
201 Status
= egFindESP(&BaseDir
);
202 if (EFI_ERROR(Status
))
206 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
,
207 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0);
208 if (EFI_ERROR(Status
))
211 BufferSize
= FileDataLength
;
212 Status
= refit_call3_wrapper(FileHandle
->Write
, FileHandle
, &BufferSize
, FileData
);
213 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
219 // Loading images from files and embedded data
222 static CHAR16
* egFindExtension(IN CHAR16
*FileName
)
226 for (i
= StrLen(FileName
); i
>= 0; i
--) {
227 if (FileName
[i
] == '.')
228 return FileName
+ i
+ 1;
229 if (FileName
[i
] == '/' || FileName
[i
] == '\\')
232 return FileName
+ StrLen(FileName
);
235 static EG_IMAGE
* egDecodeAny(IN UINT8
*FileData
, IN UINTN FileDataLength
,
236 IN CHAR16
*Format
, IN UINTN IconSize
, IN BOOLEAN WantAlpha
)
238 EG_IMAGE
*NewImage
= NULL
;
240 // Note: The UEFI implementation in Gigabyte's Hybrid EFI is buggy and does
241 // a case-sensitive comparison in StriCmp rather than the case-insensitive
242 // comparison that the spec says should be done. As a workaround, we repeat
243 // the comparison twice here.
244 // dispatch by extension
245 if ((StriCmp(Format
, L
"BMP") == 0) || (StriCmp(Format
, L
"bmp") == 0)) {
246 NewImage
= egDecodeBMP(FileData
, FileDataLength
, IconSize
, WantAlpha
);
247 } else if ((StriCmp(Format
, L
"ICNS") == 0) || (StriCmp(Format
, L
"icns") == 0)) {
248 NewImage
= egDecodeICNS(FileData
, FileDataLength
, IconSize
, WantAlpha
);
254 EG_IMAGE
* egLoadImage(IN EFI_FILE
* BaseDir
, IN CHAR16
*FileName
, IN BOOLEAN WantAlpha
)
258 UINTN FileDataLength
;
261 if (BaseDir
== NULL
|| FileName
== NULL
)
265 Status
= egLoadFile(BaseDir
, FileName
, &FileData
, &FileDataLength
);
266 if (EFI_ERROR(Status
))
270 NewImage
= egDecodeAny(FileData
, FileDataLength
, egFindExtension(FileName
), 128, WantAlpha
);
276 // Load an icon from (BaseDir)/Path, extracting the icon of size IconSize x IconSize.
277 // If the initial attempt is unsuccessful, try again, replacing the directory
278 // component of Path with DEFAULT_ICONS_DIR.
279 // Note: The assumption is that BaseDir points to rEFInd's home directory and Path
280 // includes one subdirectory level. If this changes in future revisions, it may be
281 // necessary to alter the code that tries again with DEFAULT_ICONS_DIR.
282 // Returns a pointer to the image data, or NULL if the icon could not be loaded.
283 EG_IMAGE
* egLoadIcon(IN EFI_FILE
* BaseDir
, IN CHAR16
*Path
, IN UINTN IconSize
)
287 UINTN FileDataLength
;
288 CHAR16
*FileName
, FileName2
[256];
291 if (BaseDir
== NULL
|| Path
== NULL
)
295 Status
= egLoadFile(BaseDir
, Path
, &FileData
, &FileDataLength
);
296 if (EFI_ERROR(Status
)) {
297 FileName
= Basename(Path
); // Note: FileName is a pointer within Path; DON'T FREE IT!
298 SPrint(FileName2
, 255, L
"%s\\%s", DEFAULT_ICONS_DIR
, FileName
);
299 Status
= egLoadFile(BaseDir
, FileName2
, &FileData
, &FileDataLength
);
300 if (EFI_ERROR(Status
))
305 NewImage
= egDecodeAny(FileData
, FileDataLength
, egFindExtension(Path
), IconSize
, TRUE
);
309 } // EG_IMAGE *egLoadIcon()
311 EG_IMAGE
* egDecodeImage(IN UINT8
*FileData
, IN UINTN FileDataLength
, IN CHAR16
*Format
, IN BOOLEAN WantAlpha
)
313 return egDecodeAny(FileData
, FileDataLength
, Format
, 128, WantAlpha
);
316 EG_IMAGE
* egPrepareEmbeddedImage(IN EG_EMBEDDED_IMAGE
*EmbeddedImage
, IN BOOLEAN WantAlpha
)
324 if (EmbeddedImage
->PixelMode
> EG_MAX_EIPIXELMODE
||
325 (EmbeddedImage
->CompressMode
!= EG_EICOMPMODE_NONE
&& EmbeddedImage
->CompressMode
!= EG_EICOMPMODE_RLE
))
328 // allocate image structure and pixel buffer
329 NewImage
= egCreateImage(EmbeddedImage
->Width
, EmbeddedImage
->Height
, WantAlpha
);
330 if (NewImage
== NULL
)
333 CompData
= (UINT8
*)EmbeddedImage
->Data
; // drop const
334 CompLen
= EmbeddedImage
->DataLength
;
335 PixelCount
= EmbeddedImage
->Width
* EmbeddedImage
->Height
;
337 // FUTURE: for EG_EICOMPMODE_EFICOMPRESS, decompress whole data block here
339 if (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY
||
340 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY_ALPHA
) {
342 // copy grayscale plane and expand
343 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
344 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, r
), PixelCount
);
346 egInsertPlane(CompData
, PLPTR(NewImage
, r
), PixelCount
);
347 CompData
+= PixelCount
;
349 egCopyPlane(PLPTR(NewImage
, r
), PLPTR(NewImage
, g
), PixelCount
);
350 egCopyPlane(PLPTR(NewImage
, r
), PLPTR(NewImage
, b
), PixelCount
);
352 } else if (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR
||
353 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR_ALPHA
) {
356 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
357 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, r
), PixelCount
);
358 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, g
), PixelCount
);
359 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, b
), PixelCount
);
361 egInsertPlane(CompData
, PLPTR(NewImage
, r
), PixelCount
);
362 CompData
+= PixelCount
;
363 egInsertPlane(CompData
, PLPTR(NewImage
, g
), PixelCount
);
364 CompData
+= PixelCount
;
365 egInsertPlane(CompData
, PLPTR(NewImage
, b
), PixelCount
);
366 CompData
+= PixelCount
;
371 // set color planes to black
372 egSetPlane(PLPTR(NewImage
, r
), 0, PixelCount
);
373 egSetPlane(PLPTR(NewImage
, g
), 0, PixelCount
);
374 egSetPlane(PLPTR(NewImage
, b
), 0, PixelCount
);
378 if (WantAlpha
&& (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY_ALPHA
||
379 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR_ALPHA
||
380 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_ALPHA
)) {
383 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
384 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, a
), PixelCount
);
386 egInsertPlane(CompData
, PLPTR(NewImage
, a
), PixelCount
);
387 CompData
+= PixelCount
;
391 egSetPlane(PLPTR(NewImage
, a
), WantAlpha
? 255 : 0, PixelCount
);
401 VOID
egRestrictImageArea(IN EG_IMAGE
*Image
,
402 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
403 IN OUT UINTN
*AreaWidth
, IN OUT UINTN
*AreaHeight
)
405 if (AreaPosX
>= Image
->Width
|| AreaPosY
>= Image
->Height
) {
406 // out of bounds, operation has no effect
410 // calculate affected area
411 if (*AreaWidth
> Image
->Width
- AreaPosX
)
412 *AreaWidth
= Image
->Width
- AreaPosX
;
413 if (*AreaHeight
> Image
->Height
- AreaPosY
)
414 *AreaHeight
= Image
->Height
- AreaPosY
;
418 VOID
egFillImage(IN OUT EG_IMAGE
*CompImage
, IN EG_PIXEL
*Color
)
425 if (!CompImage
->HasAlpha
)
428 PixelPtr
= CompImage
->PixelData
;
429 for (i
= 0; i
< CompImage
->Width
* CompImage
->Height
; i
++, PixelPtr
++)
430 *PixelPtr
= FillColor
;
433 VOID
egFillImageArea(IN OUT EG_IMAGE
*CompImage
,
434 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
435 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
441 EG_PIXEL
*PixelBasePtr
;
443 egRestrictImageArea(CompImage
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
447 if (!CompImage
->HasAlpha
)
450 PixelBasePtr
= CompImage
->PixelData
+ AreaPosY
* CompImage
->Width
+ AreaPosX
;
451 for (y
= 0; y
< AreaHeight
; y
++) {
452 PixelPtr
= PixelBasePtr
;
453 for (x
= 0; x
< AreaWidth
; x
++, PixelPtr
++)
454 *PixelPtr
= FillColor
;
455 PixelBasePtr
+= CompImage
->Width
;
460 VOID
egRawCopy(IN OUT EG_PIXEL
*CompBasePtr
, IN EG_PIXEL
*TopBasePtr
,
461 IN UINTN Width
, IN UINTN Height
,
462 IN UINTN CompLineOffset
, IN UINTN TopLineOffset
)
465 EG_PIXEL
*TopPtr
, *CompPtr
;
467 for (y
= 0; y
< Height
; y
++) {
469 CompPtr
= CompBasePtr
;
470 for (x
= 0; x
< Width
; x
++) {
474 TopBasePtr
+= TopLineOffset
;
475 CompBasePtr
+= CompLineOffset
;
479 VOID
egRawCompose(IN OUT EG_PIXEL
*CompBasePtr
, IN EG_PIXEL
*TopBasePtr
,
480 IN UINTN Width
, IN UINTN Height
,
481 IN UINTN CompLineOffset
, IN UINTN TopLineOffset
)
484 EG_PIXEL
*TopPtr
, *CompPtr
;
489 for (y
= 0; y
< Height
; y
++) {
491 CompPtr
= CompBasePtr
;
492 for (x
= 0; x
< Width
; x
++) {
494 RevAlpha
= 255 - Alpha
;
495 Temp
= (UINTN
)CompPtr
->b
* RevAlpha
+ (UINTN
)TopPtr
->b
* Alpha
+ 0x80;
496 CompPtr
->b
= (Temp
+ (Temp
>> 8)) >> 8;
497 Temp
= (UINTN
)CompPtr
->g
* RevAlpha
+ (UINTN
)TopPtr
->g
* Alpha
+ 0x80;
498 CompPtr
->g
= (Temp
+ (Temp
>> 8)) >> 8;
499 Temp
= (UINTN
)CompPtr
->r
* RevAlpha
+ (UINTN
)TopPtr
->r
* Alpha
+ 0x80;
500 CompPtr
->r
= (Temp
+ (Temp
>> 8)) >> 8;
502 CompPtr->b = ((UINTN)CompPtr->b * RevAlpha + (UINTN)TopPtr->b * Alpha) / 255;
503 CompPtr->g = ((UINTN)CompPtr->g * RevAlpha + (UINTN)TopPtr->g * Alpha) / 255;
504 CompPtr->r = ((UINTN)CompPtr->r * RevAlpha + (UINTN)TopPtr->r * Alpha) / 255;
508 TopBasePtr
+= TopLineOffset
;
509 CompBasePtr
+= CompLineOffset
;
513 VOID
egComposeImage(IN OUT EG_IMAGE
*CompImage
, IN EG_IMAGE
*TopImage
, IN UINTN PosX
, IN UINTN PosY
)
515 UINTN CompWidth
, CompHeight
;
517 CompWidth
= TopImage
->Width
;
518 CompHeight
= TopImage
->Height
;
519 egRestrictImageArea(CompImage
, PosX
, PosY
, &CompWidth
, &CompHeight
);
523 // if (CompImage->HasAlpha) {
524 // CompImage->HasAlpha = FALSE;
525 // egSetPlane(PLPTR(CompImage, a), 0, CompImage->Width * CompImage->Height);
528 if (TopImage
->HasAlpha
) {
529 egRawCompose(CompImage
->PixelData
+ PosY
* CompImage
->Width
+ PosX
, TopImage
->PixelData
,
530 CompWidth
, CompHeight
, CompImage
->Width
, TopImage
->Width
);
532 egRawCopy(CompImage
->PixelData
+ PosY
* CompImage
->Width
+ PosX
, TopImage
->PixelData
,
533 CompWidth
, CompHeight
, CompImage
->Width
, TopImage
->Width
);
538 EG_IMAGE
* egEnsureImageSize(IN EG_IMAGE
*Image
, IN UINTN Width
, IN UINTN Height
, IN EG_PIXEL
*Color
)
544 if (Image
->Width
== Width
&& Image
->Height
== Height
)
547 NewImage
= egCreateFilledImage(Width
, Height
, Image
->HasAlpha
, Color
);
548 if (NewImage
== NULL
) {
552 egComposeImage(NewImage
, Image
, 0, 0);
559 // misc internal functions
562 VOID
egInsertPlane(IN UINT8
*SrcDataPtr
, IN UINT8
*DestPlanePtr
, IN UINTN PixelCount
)
566 for (i
= 0; i
< PixelCount
; i
++) {
567 *DestPlanePtr
= *SrcDataPtr
++;
572 VOID
egSetPlane(IN UINT8
*DestPlanePtr
, IN UINT8 Value
, IN UINTN PixelCount
)
576 for (i
= 0; i
< PixelCount
; i
++) {
577 *DestPlanePtr
= Value
;
582 VOID
egCopyPlane(IN UINT8
*SrcPlanePtr
, IN UINT8
*DestPlanePtr
, IN UINTN PixelCount
)
586 for (i
= 0; i
< PixelCount
; i
++) {
587 *DestPlanePtr
= *SrcPlanePtr
;
588 DestPlanePtr
+= 4, SrcPlanePtr
+= 4;