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.
37 * Modifications copyright (c) 2012-2013 Roderick W. Smith
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
46 #include "../refind/global.h"
47 #include "../refind/lib.h"
48 #include "../refind/screen.h"
49 #include "../include/refit_call_wrapper.h"
52 #define MAX_FILE_SIZE (1024*1024*1024)
54 #ifndef __MAKEWITH_GNUEFI
55 #define LibLocateHandle gBS->LocateHandleBuffer
56 #define LibOpenRoot EfiLibOpenRoot
60 // Basic image handling
63 EG_IMAGE
* egCreateImage(IN UINTN Width
, IN UINTN Height
, IN BOOLEAN HasAlpha
)
67 NewImage
= (EG_IMAGE
*) AllocatePool(sizeof(EG_IMAGE
));
70 NewImage
->PixelData
= (EG_PIXEL
*) AllocatePool(Width
* Height
* sizeof(EG_PIXEL
));
71 if (NewImage
->PixelData
== NULL
) {
76 NewImage
->Width
= Width
;
77 NewImage
->Height
= Height
;
78 NewImage
->HasAlpha
= HasAlpha
;
82 EG_IMAGE
* egCreateFilledImage(IN UINTN Width
, IN UINTN Height
, IN BOOLEAN HasAlpha
, IN EG_PIXEL
*Color
)
86 NewImage
= egCreateImage(Width
, Height
, HasAlpha
);
90 egFillImage(NewImage
, Color
);
94 EG_IMAGE
* egCopyImage(IN EG_IMAGE
*Image
)
96 EG_IMAGE
*NewImage
= NULL
;
99 NewImage
= egCreateImage(Image
->Width
, Image
->Height
, Image
->HasAlpha
);
100 if (NewImage
== NULL
)
103 CopyMem(NewImage
->PixelData
, Image
->PixelData
, Image
->Width
* Image
->Height
* sizeof(EG_PIXEL
));
107 // Returns a smaller image composed of the specified crop area from the larger area.
108 // If the specified area is larger than is in the original, returns NULL.
109 EG_IMAGE
* egCropImage(IN EG_IMAGE
*Image
, IN UINTN StartX
, IN UINTN StartY
, IN UINTN Width
, IN UINTN Height
) {
110 EG_IMAGE
*NewImage
= NULL
;
113 if (((StartX
+ Width
) > Image
->Width
) || ((StartY
+ Height
) > Image
->Height
))
116 NewImage
= egCreateImage(Width
, Height
, Image
->HasAlpha
);
117 if (NewImage
== NULL
)
120 for (y
= 0; y
< Height
; y
++) {
121 for (x
= 0; x
< Width
; x
++) {
122 NewImage
->PixelData
[y
* NewImage
->Width
+ x
] = Image
->PixelData
[(y
+ StartY
) * Image
->Width
+ x
+ StartX
];
126 } // EG_IMAGE * egCropImage()
128 VOID
egFreeImage(IN EG_IMAGE
*Image
)
131 if (Image
->PixelData
!= NULL
)
132 FreePool(Image
->PixelData
);
138 // Basic file operations
141 EFI_STATUS
egLoadFile(IN EFI_FILE
* BaseDir
, IN CHAR16
*FileName
, OUT UINT8
**FileData
, OUT UINTN
*FileDataLength
)
144 EFI_FILE_HANDLE FileHandle
;
145 EFI_FILE_INFO
*FileInfo
;
150 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
, EFI_FILE_MODE_READ
, 0);
151 if (EFI_ERROR(Status
)) {
155 FileInfo
= LibFileInfo(FileHandle
);
156 if (FileInfo
== NULL
) {
157 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
158 return EFI_NOT_FOUND
;
160 ReadSize
= FileInfo
->FileSize
;
161 if (ReadSize
> MAX_FILE_SIZE
)
162 ReadSize
= MAX_FILE_SIZE
;
165 BufferSize
= (UINTN
)ReadSize
; // was limited to 1 GB above, so this is safe
166 Buffer
= (UINT8
*) AllocatePool(BufferSize
);
167 if (Buffer
== NULL
) {
168 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
169 return EFI_OUT_OF_RESOURCES
;
172 Status
= refit_call3_wrapper(FileHandle
->Read
, FileHandle
, &BufferSize
, Buffer
);
173 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
174 if (EFI_ERROR(Status
)) {
180 *FileDataLength
= BufferSize
;
184 static EFI_GUID ESPGuid
= { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
186 EFI_STATUS
egFindESP(OUT EFI_FILE_HANDLE
*RootDir
)
189 UINTN HandleCount
= 0;
192 Status
= LibLocateHandle(ByProtocol
, &ESPGuid
, NULL
, &HandleCount
, &Handles
);
193 if (!EFI_ERROR(Status
) && HandleCount
> 0) {
194 *RootDir
= LibOpenRoot(Handles
[0]);
195 if (*RootDir
== NULL
)
196 Status
= EFI_NOT_FOUND
;
202 EFI_STATUS
egSaveFile(IN EFI_FILE
* BaseDir OPTIONAL
, IN CHAR16
*FileName
,
203 IN UINT8
*FileData
, IN UINTN FileDataLength
)
206 EFI_FILE_HANDLE FileHandle
;
209 if (BaseDir
== NULL
) {
210 Status
= egFindESP(&BaseDir
);
211 if (EFI_ERROR(Status
))
215 Status
= refit_call5_wrapper(BaseDir
->Open
, BaseDir
, &FileHandle
, FileName
,
216 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
, 0);
217 if (EFI_ERROR(Status
))
220 BufferSize
= FileDataLength
;
221 Status
= refit_call3_wrapper(FileHandle
->Write
, FileHandle
, &BufferSize
, FileData
);
222 refit_call1_wrapper(FileHandle
->Close
, FileHandle
);
228 // Loading images from files and embedded data
231 // Decode the specified image data. The IconSize parameter is relevant only
232 // for ICNS, for which it selects which ICNS sub-image is decoded.
233 // Returns a pointer to the resulting EG_IMAGE or NULL if decoding failed.
234 static EG_IMAGE
* egDecodeAny(IN UINT8
*FileData
, IN UINTN FileDataLength
, IN UINTN IconSize
, IN BOOLEAN WantAlpha
)
236 EG_IMAGE
*NewImage
= NULL
;
238 NewImage
= egDecodeICNS(FileData
, FileDataLength
, IconSize
, WantAlpha
);
239 if (NewImage
== NULL
)
240 NewImage
= egDecodePNG(FileData
, FileDataLength
, IconSize
, WantAlpha
);
241 if (NewImage
== NULL
)
242 NewImage
= egDecodeBMP(FileData
, FileDataLength
, IconSize
, WantAlpha
);
247 EG_IMAGE
* egLoadImage(IN EFI_FILE
* BaseDir
, IN CHAR16
*FileName
, IN BOOLEAN WantAlpha
)
251 UINTN FileDataLength
;
254 if (BaseDir
== NULL
|| FileName
== NULL
)
258 Status
= egLoadFile(BaseDir
, FileName
, &FileData
, &FileDataLength
);
259 if (EFI_ERROR(Status
))
263 NewImage
= egDecodeAny(FileData
, FileDataLength
, 128, WantAlpha
);
269 // Load an icon from (BaseDir)/Path, extracting the icon of size IconSize x IconSize.
270 // Returns a pointer to the image data, or NULL if the icon could not be loaded.
271 EG_IMAGE
* egLoadIcon(IN EFI_FILE
* BaseDir
, IN CHAR16
*Path
, IN UINTN IconSize
)
275 UINTN FileDataLength
;
278 if (BaseDir
== NULL
|| Path
== NULL
)
282 Status
= egLoadFile(BaseDir
, Path
, &FileData
, &FileDataLength
);
283 if (EFI_ERROR(Status
))
287 NewImage
= egDecodeAny(FileData
, FileDataLength
, IconSize
, TRUE
);
289 if ((NewImage
->Width
!= IconSize
) || (NewImage
->Height
!= IconSize
)) {
290 Print(L
"Warning: Attempt to load icon of the wrong size from '%s'\n", Path
);
291 MyFreePool(NewImage
);
296 } // EG_IMAGE *egLoadIcon()
298 // Returns an icon of any type from the specified subdirectory using the specified
299 // base name. All directory references are relative to BaseDir. For instance, if
300 // SubdirName is "myicons" and BaseName is "os_linux", this function will return
301 // an image based on "myicons/os_linux.icns" or "myicons/os_linux.png", in that
302 // order of preference. Returns NULL if no such file is a valid icon file.
303 EG_IMAGE
* egLoadIconAnyType(IN EFI_FILE
*BaseDir
, IN CHAR16
*SubdirName
, IN CHAR16
*BaseName
, IN UINTN IconSize
) {
304 EG_IMAGE
*Image
= NULL
;
306 CHAR16 FileName
[256];
309 while (((Extension
= FindCommaDelimited(ICON_EXTENSIONS
, i
++)) != NULL
) && (Image
== NULL
)) {
310 SPrint(FileName
, 255, L
"%s\\%s.%s", SubdirName
, BaseName
, Extension
);
311 Image
= egLoadIcon(BaseDir
, FileName
, IconSize
);
312 MyFreePool(Extension
);
316 } // EG_IMAGE *egLoadIconAnyType()
318 // Returns an icon with any extension in ICON_EXTENSIONS from either the directory
319 // specified by GlobalConfig.IconsDir or DEFAULT_ICONS_DIR. The input BaseName
320 // should be the icon name without an extension. For instance, if BaseName is
321 // os_linux, GlobalConfig.IconsDir is myicons, DEFAULT_ICONS_DIR is icons, and
322 // ICON_EXTENSIONS is "icns,png", this function will return myicons/os_linux.icns,
323 // myicons/os_linux.png, icons/os_linux.icns, or icons/os_linux.png, in that
324 // order of preference. Returns NULL if no such icon can be found. All file
325 // references are relative to SelfDir.
326 EG_IMAGE
* egFindIcon(IN CHAR16
*BaseName
, IN UINTN IconSize
) {
327 EG_IMAGE
*Image
= NULL
;
329 if (GlobalConfig
.IconsDir
!= NULL
) {
330 Image
= egLoadIconAnyType(SelfDir
, GlobalConfig
.IconsDir
, BaseName
, IconSize
);
334 Image
= egLoadIconAnyType(SelfDir
, DEFAULT_ICONS_DIR
, BaseName
, IconSize
);
338 } // EG_IMAGE * egFindIcon()
340 EG_IMAGE
* egPrepareEmbeddedImage(IN EG_EMBEDDED_IMAGE
*EmbeddedImage
, IN BOOLEAN WantAlpha
)
348 if (EmbeddedImage
->PixelMode
> EG_MAX_EIPIXELMODE
||
349 (EmbeddedImage
->CompressMode
!= EG_EICOMPMODE_NONE
&& EmbeddedImage
->CompressMode
!= EG_EICOMPMODE_RLE
))
352 // allocate image structure and pixel buffer
353 NewImage
= egCreateImage(EmbeddedImage
->Width
, EmbeddedImage
->Height
, WantAlpha
);
354 if (NewImage
== NULL
)
357 CompData
= (UINT8
*)EmbeddedImage
->Data
; // drop const
358 CompLen
= EmbeddedImage
->DataLength
;
359 PixelCount
= EmbeddedImage
->Width
* EmbeddedImage
->Height
;
361 // FUTURE: for EG_EICOMPMODE_EFICOMPRESS, decompress whole data block here
363 if (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY
||
364 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY_ALPHA
) {
366 // copy grayscale plane and expand
367 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
368 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, r
), PixelCount
);
370 egInsertPlane(CompData
, PLPTR(NewImage
, r
), PixelCount
);
371 CompData
+= PixelCount
;
373 egCopyPlane(PLPTR(NewImage
, r
), PLPTR(NewImage
, g
), PixelCount
);
374 egCopyPlane(PLPTR(NewImage
, r
), PLPTR(NewImage
, b
), PixelCount
);
376 } else if (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR
||
377 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR_ALPHA
) {
380 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
381 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, r
), PixelCount
);
382 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, g
), PixelCount
);
383 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, b
), PixelCount
);
385 egInsertPlane(CompData
, PLPTR(NewImage
, r
), PixelCount
);
386 CompData
+= PixelCount
;
387 egInsertPlane(CompData
, PLPTR(NewImage
, g
), PixelCount
);
388 CompData
+= PixelCount
;
389 egInsertPlane(CompData
, PLPTR(NewImage
, b
), PixelCount
);
390 CompData
+= PixelCount
;
395 // set color planes to black
396 egSetPlane(PLPTR(NewImage
, r
), 0, PixelCount
);
397 egSetPlane(PLPTR(NewImage
, g
), 0, PixelCount
);
398 egSetPlane(PLPTR(NewImage
, b
), 0, PixelCount
);
402 if (WantAlpha
&& (EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_GRAY_ALPHA
||
403 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_COLOR_ALPHA
||
404 EmbeddedImage
->PixelMode
== EG_EIPIXELMODE_ALPHA
)) {
407 if (EmbeddedImage
->CompressMode
== EG_EICOMPMODE_RLE
) {
408 egDecompressIcnsRLE(&CompData
, &CompLen
, PLPTR(NewImage
, a
), PixelCount
);
410 egInsertPlane(CompData
, PLPTR(NewImage
, a
), PixelCount
);
411 CompData
+= PixelCount
;
415 egSetPlane(PLPTR(NewImage
, a
), WantAlpha
? 255 : 0, PixelCount
);
425 VOID
egRestrictImageArea(IN EG_IMAGE
*Image
,
426 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
427 IN OUT UINTN
*AreaWidth
, IN OUT UINTN
*AreaHeight
)
429 if (AreaPosX
>= Image
->Width
|| AreaPosY
>= Image
->Height
) {
430 // out of bounds, operation has no effect
434 // calculate affected area
435 if (*AreaWidth
> Image
->Width
- AreaPosX
)
436 *AreaWidth
= Image
->Width
- AreaPosX
;
437 if (*AreaHeight
> Image
->Height
- AreaPosY
)
438 *AreaHeight
= Image
->Height
- AreaPosY
;
442 VOID
egFillImage(IN OUT EG_IMAGE
*CompImage
, IN EG_PIXEL
*Color
)
449 if (!CompImage
->HasAlpha
)
452 PixelPtr
= CompImage
->PixelData
;
453 for (i
= 0; i
< CompImage
->Width
* CompImage
->Height
; i
++, PixelPtr
++)
454 *PixelPtr
= FillColor
;
457 VOID
egFillImageArea(IN OUT EG_IMAGE
*CompImage
,
458 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
459 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
465 EG_PIXEL
*PixelBasePtr
;
467 egRestrictImageArea(CompImage
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
471 if (!CompImage
->HasAlpha
)
474 PixelBasePtr
= CompImage
->PixelData
+ AreaPosY
* CompImage
->Width
+ AreaPosX
;
475 for (y
= 0; y
< AreaHeight
; y
++) {
476 PixelPtr
= PixelBasePtr
;
477 for (x
= 0; x
< AreaWidth
; x
++, PixelPtr
++)
478 *PixelPtr
= FillColor
;
479 PixelBasePtr
+= CompImage
->Width
;
484 VOID
egRawCopy(IN OUT EG_PIXEL
*CompBasePtr
, IN EG_PIXEL
*TopBasePtr
,
485 IN UINTN Width
, IN UINTN Height
,
486 IN UINTN CompLineOffset
, IN UINTN TopLineOffset
)
489 EG_PIXEL
*TopPtr
, *CompPtr
;
491 for (y
= 0; y
< Height
; y
++) {
493 CompPtr
= CompBasePtr
;
494 for (x
= 0; x
< Width
; x
++) {
498 TopBasePtr
+= TopLineOffset
;
499 CompBasePtr
+= CompLineOffset
;
503 VOID
egRawCompose(IN OUT EG_PIXEL
*CompBasePtr
, IN EG_PIXEL
*TopBasePtr
,
504 IN UINTN Width
, IN UINTN Height
,
505 IN UINTN CompLineOffset
, IN UINTN TopLineOffset
)
508 EG_PIXEL
*TopPtr
, *CompPtr
;
513 for (y
= 0; y
< Height
; y
++) {
515 CompPtr
= CompBasePtr
;
516 for (x
= 0; x
< Width
; x
++) {
518 RevAlpha
= 255 - Alpha
;
519 Temp
= (UINTN
)CompPtr
->b
* RevAlpha
+ (UINTN
)TopPtr
->b
* Alpha
+ 0x80;
520 CompPtr
->b
= (Temp
+ (Temp
>> 8)) >> 8;
521 Temp
= (UINTN
)CompPtr
->g
* RevAlpha
+ (UINTN
)TopPtr
->g
* Alpha
+ 0x80;
522 CompPtr
->g
= (Temp
+ (Temp
>> 8)) >> 8;
523 Temp
= (UINTN
)CompPtr
->r
* RevAlpha
+ (UINTN
)TopPtr
->r
* Alpha
+ 0x80;
524 CompPtr
->r
= (Temp
+ (Temp
>> 8)) >> 8;
526 CompPtr->b = ((UINTN)CompPtr->b * RevAlpha + (UINTN)TopPtr->b * Alpha) / 255;
527 CompPtr->g = ((UINTN)CompPtr->g * RevAlpha + (UINTN)TopPtr->g * Alpha) / 255;
528 CompPtr->r = ((UINTN)CompPtr->r * RevAlpha + (UINTN)TopPtr->r * Alpha) / 255;
532 TopBasePtr
+= TopLineOffset
;
533 CompBasePtr
+= CompLineOffset
;
537 VOID
egComposeImage(IN OUT EG_IMAGE
*CompImage
, IN EG_IMAGE
*TopImage
, IN UINTN PosX
, IN UINTN PosY
)
539 UINTN CompWidth
, CompHeight
;
541 CompWidth
= TopImage
->Width
;
542 CompHeight
= TopImage
->Height
;
543 egRestrictImageArea(CompImage
, PosX
, PosY
, &CompWidth
, &CompHeight
);
547 if (TopImage
->HasAlpha
) {
548 egRawCompose(CompImage
->PixelData
+ PosY
* CompImage
->Width
+ PosX
, TopImage
->PixelData
,
549 CompWidth
, CompHeight
, CompImage
->Width
, TopImage
->Width
);
551 egRawCopy(CompImage
->PixelData
+ PosY
* CompImage
->Width
+ PosX
, TopImage
->PixelData
,
552 CompWidth
, CompHeight
, CompImage
->Width
, TopImage
->Width
);
555 } /* VOID egComposeImage() */
557 EG_IMAGE
* egEnsureImageSize(IN EG_IMAGE
*Image
, IN UINTN Width
, IN UINTN Height
, IN EG_PIXEL
*Color
)
563 if (Image
->Width
== Width
&& Image
->Height
== Height
)
566 NewImage
= egCreateFilledImage(Width
, Height
, Image
->HasAlpha
, Color
);
567 if (NewImage
== NULL
) {
571 Image
->HasAlpha
= FALSE
;
572 egComposeImage(NewImage
, Image
, 0, 0);
579 // misc internal functions
582 VOID
egInsertPlane(IN UINT8
*SrcDataPtr
, IN UINT8
*DestPlanePtr
, IN UINTN PixelCount
)
586 for (i
= 0; i
< PixelCount
; i
++) {
587 *DestPlanePtr
= *SrcDataPtr
++;
592 VOID
egSetPlane(IN UINT8
*DestPlanePtr
, IN UINT8 Value
, IN UINTN PixelCount
)
596 for (i
= 0; i
< PixelCount
; i
++) {
597 *DestPlanePtr
= Value
;
602 VOID
egCopyPlane(IN UINT8
*SrcPlanePtr
, IN UINT8
*DestPlanePtr
, IN UINTN PixelCount
)
606 for (i
= 0; i
< PixelCount
; i
++) {
607 *DestPlanePtr
= *SrcPlanePtr
;
608 DestPlanePtr
+= 4, SrcPlanePtr
+= 4;