3 * Loader for .icns icon files
5 * Copyright (c) 2006-2007 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.
52 BUILTIN_ICON BuiltinIconTable
[BUILTIN_ICON_COUNT
] = {
53 { NULL
, L
"icons\\func_about.icns", 48 },
54 { NULL
, L
"icons\\func_reset.icns", 48 },
55 { NULL
, L
"icons\\func_shutdown.icns", 48 },
56 { NULL
, L
"icons\\tool_shell.icns", 48 },
57 { NULL
, L
"icons\\tool_part.icns", 48 },
58 { NULL
, L
"icons\\tool_rescue.icns", 48 },
59 { NULL
, L
"icons\\vol_internal.icns", 32 },
60 { NULL
, L
"icons\\vol_external.icns", 32 },
61 { NULL
, L
"icons\\vol_optical.icns", 32 },
64 EG_IMAGE
* BuiltinIcon(IN UINTN Id
)
66 if (Id
>= BUILTIN_ICON_COUNT
)
69 if (BuiltinIconTable
[Id
].Image
== NULL
)
70 BuiltinIconTable
[Id
].Image
= LoadIcnsFallback(SelfDir
, BuiltinIconTable
[Id
].Path
, BuiltinIconTable
[Id
].PixelSize
);
72 return BuiltinIconTable
[Id
].Image
;
76 // Load an icon for an operating system
79 EG_IMAGE
* LoadOSIcon(IN CHAR16
*OSIconName OPTIONAL
, IN CHAR16
*FallbackIconName
, BOOLEAN BootLogo
)
82 CHAR16 CutoutName
[16];
84 UINTN StartIndex
, Index
, NextIndex
;
86 if (GlobalConfig
.TextOnly
) // skip loading if it's not used anyway
90 // try the names from OSIconName
91 for (StartIndex
= 0; OSIconName
!= NULL
&& OSIconName
[StartIndex
]; StartIndex
= NextIndex
) {
92 // find the next name in the list
94 for (Index
= StartIndex
; OSIconName
[Index
]; Index
++) {
95 if (OSIconName
[Index
] == ',') {
96 NextIndex
= Index
+ 1;
100 if (OSIconName
[Index
] == 0)
103 // construct full path
104 if (Index
> StartIndex
+ 15) // prevent buffer overflow
106 CopyMem(CutoutName
, OSIconName
+ StartIndex
, (Index
- StartIndex
) * sizeof(CHAR16
));
107 CutoutName
[Index
- StartIndex
] = 0;
108 SPrint(FileName
, 255, L
"icons\\%s_%s.icns",
109 BootLogo
? L
"boot" : L
"os", CutoutName
);
112 Image
= egLoadIcon(SelfDir
, FileName
, 128);
117 // try the fallback name
118 SPrint(FileName
, 255, L
"icons\\%s_%s.icns",
119 BootLogo
? L
"boot" : L
"os", FallbackIconName
);
120 Image
= egLoadIcon(SelfDir
, FileName
, 128);
124 // try the fallback name with os_ instead of boot_
126 return LoadOSIcon(NULL
, FallbackIconName
, FALSE
);
128 return DummyImage(128);
132 // Load an image from a .icns file
135 EG_IMAGE
* LoadIcns(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN UINTN PixelSize
)
137 if (GlobalConfig
.TextOnly
) // skip loading if it's not used anyway
139 return egLoadIcon(BaseDir
, FileName
, PixelSize
);
142 static EG_PIXEL BlackPixel
= { 0x00, 0x00, 0x00, 0 };
143 //static EG_PIXEL YellowPixel = { 0x00, 0xff, 0xff, 0 };
145 EG_IMAGE
* DummyImage(IN UINTN PixelSize
)
148 UINTN x
, y
, LineOffset
;
151 Image
= egCreateFilledImage(PixelSize
, PixelSize
, TRUE
, &BlackPixel
);
153 LineOffset
= PixelSize
* 4;
155 YPtr
= (CHAR8
*)Image
->PixelData
+ ((PixelSize
- 32) >> 1) * (LineOffset
+ 4);
156 for (y
= 0; y
< 32; y
++) {
158 for (x
= 0; x
< 32; x
++) {
159 if (((x
+ y
) % 12) < 6) {
176 EG_IMAGE
* LoadIcnsFallback(IN EFI_FILE_HANDLE BaseDir
, IN CHAR16
*FileName
, IN UINTN PixelSize
)
179 if (GlobalConfig
.TextOnly
) // skip loading if it's not used anyway
182 Image
= LoadIcns(BaseDir
, FileName
, PixelSize
);
184 Image
= DummyImage(PixelSize
);