]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
MdeModulePkg/HiiDatabase: Update HiiImage to support PNG/JPEG
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / Image.c
1 /** @file
2 Implementation for EFI_HII_IMAGE_PROTOCOL.
3
4
5 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16
17 #include "HiiDatabase.h"
18
19
20 /**
21 Get the imageid of last image block: EFI_HII_IIBT_END_BLOCK when input
22 ImageId is zero, otherwise return the address of the
23 corresponding image block with identifier specified by ImageId.
24
25 This is a internal function.
26
27 @param ImageBlocks Points to the beginning of a series of image blocks stored in order.
28 @param ImageId If input ImageId is 0, output the image id of the EFI_HII_IIBT_END_BLOCK;
29 else use this id to find its corresponding image block address.
30
31 @return The image block address when input ImageId is not zero; otherwise return NULL.
32
33 **/
34 EFI_HII_IMAGE_BLOCK *
35 GetImageIdOrAddress (
36 IN EFI_HII_IMAGE_BLOCK *ImageBlocks,
37 IN OUT EFI_IMAGE_ID *ImageId
38 )
39 {
40 EFI_IMAGE_ID ImageIdCurrent;
41 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;
42 UINTN Length;
43
44 ASSERT (ImageBlocks != NULL && ImageId != NULL);
45 CurrentImageBlock = ImageBlocks;
46 ImageIdCurrent = 1;
47
48 while (CurrentImageBlock->BlockType != EFI_HII_IIBT_END) {
49 if (*ImageId != 0) {
50 if (*ImageId == ImageIdCurrent) {
51 //
52 // If the found image block is a duplicate block, update the ImageId to
53 // find the previous defined image block.
54 //
55 if (CurrentImageBlock->BlockType == EFI_HII_IIBT_DUPLICATE) {
56 *ImageId = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_DUPLICATE_BLOCK *) CurrentImageBlock)->ImageId);
57 ASSERT (*ImageId != ImageIdCurrent);
58 ASSERT (*ImageId != 0);
59 CurrentImageBlock = ImageBlocks;
60 ImageIdCurrent = 1;
61 continue;
62 }
63
64 return CurrentImageBlock;
65 }
66 if (*ImageId < ImageIdCurrent) {
67 //
68 // Can not find the specified image block in this image.
69 //
70 return NULL;
71 }
72 }
73 switch (CurrentImageBlock->BlockType) {
74 case EFI_HII_IIBT_EXT1:
75 Length = ((EFI_HII_IIBT_EXT1_BLOCK *) CurrentImageBlock)->Length;
76 break;
77 case EFI_HII_IIBT_EXT2:
78 Length = ReadUnaligned16 (&((EFI_HII_IIBT_EXT2_BLOCK *) CurrentImageBlock)->Length);
79 break;
80 case EFI_HII_IIBT_EXT4:
81 Length = ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_EXT4_BLOCK *) CurrentImageBlock)->Length);
82 break;
83
84 case EFI_HII_IIBT_IMAGE_1BIT:
85 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:
86 Length = sizeof (EFI_HII_IIBT_IMAGE_1BIT_BLOCK) - sizeof (UINT8) +
87 BITMAP_LEN_1_BIT (
88 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
89 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
90 );
91 ImageIdCurrent++;
92 break;
93
94 case EFI_HII_IIBT_IMAGE_4BIT:
95 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:
96 Length = sizeof (EFI_HII_IIBT_IMAGE_4BIT_BLOCK) - sizeof (UINT8) +
97 BITMAP_LEN_4_BIT (
98 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
99 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
100 );
101 ImageIdCurrent++;
102 break;
103
104 case EFI_HII_IIBT_IMAGE_8BIT:
105 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:
106 Length = sizeof (EFI_HII_IIBT_IMAGE_8BIT_BLOCK) - sizeof (UINT8) +
107 BITMAP_LEN_8_BIT (
108 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
109 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
110 );
111 ImageIdCurrent++;
112 break;
113
114 case EFI_HII_IIBT_IMAGE_24BIT:
115 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:
116 Length = sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL) +
117 BITMAP_LEN_24_BIT (
118 ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
119 ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
120 );
121 ImageIdCurrent++;
122 break;
123
124 case EFI_HII_IIBT_DUPLICATE:
125 Length = sizeof (EFI_HII_IIBT_DUPLICATE_BLOCK);
126 ImageIdCurrent++;
127 break;
128
129 case EFI_HII_IIBT_IMAGE_JPEG:
130 Length = OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size);
131 ImageIdCurrent++;
132 break;
133
134 case EFI_HII_IIBT_IMAGE_PNG:
135 Length = OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Size);
136 ImageIdCurrent++;
137 break;
138
139 case EFI_HII_IIBT_SKIP1:
140 Length = sizeof (EFI_HII_IIBT_SKIP1_BLOCK);
141 ImageIdCurrent += ((EFI_HII_IIBT_SKIP1_BLOCK *) CurrentImageBlock)->SkipCount;
142 break;
143
144 case EFI_HII_IIBT_SKIP2:
145 Length = sizeof (EFI_HII_IIBT_SKIP2_BLOCK);
146 ImageIdCurrent += ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_SKIP2_BLOCK *) CurrentImageBlock)->SkipCount);
147 break;
148
149 default:
150 //
151 // Unknown image blocks can not be skipped, processing halts.
152 //
153 ASSERT (FALSE);
154 Length = 0;
155 break;
156 }
157
158 CurrentImageBlock = (EFI_HII_IMAGE_BLOCK *) ((UINT8 *) CurrentImageBlock + Length);
159
160 }
161
162 //
163 // When ImageId is zero, return the imageid of last image block: EFI_HII_IIBT_END_BLOCK.
164 //
165 if (*ImageId == 0) {
166 *ImageId = ImageIdCurrent;
167 return CurrentImageBlock;
168 }
169
170 return NULL;
171 }
172
173
174
175 /**
176 Convert pixels from EFI_GRAPHICS_OUTPUT_BLT_PIXEL to EFI_HII_RGB_PIXEL style.
177
178 This is a internal function.
179
180
181 @param BitMapOut Pixels in EFI_HII_RGB_PIXEL format.
182 @param BitMapIn Pixels in EFI_GRAPHICS_OUTPUT_BLT_PIXEL format.
183 @param PixelNum The number of pixels to be converted.
184
185
186 **/
187 VOID
188 CopyGopToRgbPixel (
189 OUT EFI_HII_RGB_PIXEL *BitMapOut,
190 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapIn,
191 IN UINTN PixelNum
192 )
193 {
194 UINTN Index;
195
196 ASSERT (BitMapOut != NULL && BitMapIn != NULL);
197
198 for (Index = 0; Index < PixelNum; Index++) {
199 CopyMem (BitMapOut + Index, BitMapIn + Index, sizeof (EFI_HII_RGB_PIXEL));
200 }
201 }
202
203
204 /**
205 Convert pixels from EFI_HII_RGB_PIXEL to EFI_GRAPHICS_OUTPUT_BLT_PIXEL style.
206
207 This is a internal function.
208
209
210 @param BitMapOut Pixels in EFI_GRAPHICS_OUTPUT_BLT_PIXEL format.
211 @param BitMapIn Pixels in EFI_HII_RGB_PIXEL format.
212 @param PixelNum The number of pixels to be converted.
213
214
215 **/
216 VOID
217 CopyRgbToGopPixel (
218 OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapOut,
219 IN EFI_HII_RGB_PIXEL *BitMapIn,
220 IN UINTN PixelNum
221 )
222 {
223 UINTN Index;
224
225 ASSERT (BitMapOut != NULL && BitMapIn != NULL);
226
227 for (Index = 0; Index < PixelNum; Index++) {
228 CopyMem (BitMapOut + Index, BitMapIn + Index, sizeof (EFI_HII_RGB_PIXEL));
229 }
230 }
231
232
233 /**
234 Output pixels in "1 bit per pixel" format to an image.
235
236 This is a internal function.
237
238
239 @param Image Points to the image which will store the pixels.
240 @param Data Stores the value of output pixels, 0 or 1.
241 @param PaletteInfo PaletteInfo which stores the color of the output
242 pixels. First entry corresponds to color 0 and
243 second one to color 1.
244
245
246 **/
247 VOID
248 Output1bitPixel (
249 IN OUT EFI_IMAGE_INPUT *Image,
250 IN UINT8 *Data,
251 IN EFI_HII_IMAGE_PALETTE_INFO *PaletteInfo
252 )
253 {
254 UINT16 Xpos;
255 UINT16 Ypos;
256 UINTN OffsetY;
257 UINT8 Index;
258 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;
259 EFI_GRAPHICS_OUTPUT_BLT_PIXEL PaletteValue[2];
260 EFI_HII_IMAGE_PALETTE_INFO *Palette;
261 UINTN PaletteSize;
262 UINT8 Byte;
263
264 ASSERT (Image != NULL && Data != NULL && PaletteInfo != NULL);
265
266 BitMapPtr = Image->Bitmap;
267
268 //
269 // First entry corresponds to color 0 and second entry corresponds to color 1.
270 //
271 PaletteSize = 0;
272 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));
273 PaletteSize += sizeof (UINT16);
274 Palette = AllocateZeroPool (PaletteSize);
275 ASSERT (Palette != NULL);
276 if (Palette == NULL) {
277 return;
278 }
279 CopyMem (Palette, PaletteInfo, PaletteSize);
280
281 ZeroMem (PaletteValue, sizeof (PaletteValue));
282 CopyRgbToGopPixel (&PaletteValue[0], &Palette->PaletteValue[0], 1);
283 CopyRgbToGopPixel (&PaletteValue[1], &Palette->PaletteValue[1], 1);
284 FreePool (Palette);
285
286 //
287 // Convert the pixel from one bit to corresponding color.
288 //
289 for (Ypos = 0; Ypos < Image->Height; Ypos++) {
290 OffsetY = BITMAP_LEN_1_BIT (Image->Width, Ypos);
291 //
292 // All bits in these bytes are meaningful
293 //
294 for (Xpos = 0; Xpos < Image->Width / 8; Xpos++) {
295 Byte = *(Data + OffsetY + Xpos);
296 for (Index = 0; Index < 8; Index++) {
297 if ((Byte & (1 << Index)) != 0) {
298 BitMapPtr[Ypos * Image->Width + Xpos * 8 + (8 - Index - 1)] = PaletteValue[1];
299 } else {
300 BitMapPtr[Ypos * Image->Width + Xpos * 8 + (8 - Index - 1)] = PaletteValue[0];
301 }
302 }
303 }
304
305 if (Image->Width % 8 != 0) {
306 //
307 // Padding bits in this byte should be ignored.
308 //
309 Byte = *(Data + OffsetY + Xpos);
310 for (Index = 0; Index < Image->Width % 8; Index++) {
311 if ((Byte & (1 << (8 - Index - 1))) != 0) {
312 BitMapPtr[Ypos * Image->Width + Xpos * 8 + Index] = PaletteValue[1];
313 } else {
314 BitMapPtr[Ypos * Image->Width + Xpos * 8 + Index] = PaletteValue[0];
315 }
316 }
317 }
318 }
319 }
320
321
322 /**
323 Output pixels in "4 bit per pixel" format to an image.
324
325 This is a internal function.
326
327
328 @param Image Points to the image which will store the pixels.
329 @param Data Stores the value of output pixels, 0 ~ 15.
330 @param[in] PaletteInfo PaletteInfo which stores the color of the output
331 pixels. Each entry corresponds to a color within
332 [0, 15].
333
334
335 **/
336 VOID
337 Output4bitPixel (
338 IN OUT EFI_IMAGE_INPUT *Image,
339 IN UINT8 *Data,
340 IN EFI_HII_IMAGE_PALETTE_INFO *PaletteInfo
341 )
342 {
343 UINT16 Xpos;
344 UINT16 Ypos;
345 UINTN OffsetY;
346 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;
347 EFI_GRAPHICS_OUTPUT_BLT_PIXEL PaletteValue[16];
348 EFI_HII_IMAGE_PALETTE_INFO *Palette;
349 UINTN PaletteSize;
350 UINT16 PaletteNum;
351 UINT8 Byte;
352
353 ASSERT (Image != NULL && Data != NULL && PaletteInfo != NULL);
354
355 BitMapPtr = Image->Bitmap;
356
357 //
358 // The bitmap should allocate each color index starting from 0.
359 //
360 PaletteSize = 0;
361 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));
362 PaletteSize += sizeof (UINT16);
363 Palette = AllocateZeroPool (PaletteSize);
364 ASSERT (Palette != NULL);
365 if (Palette == NULL) {
366 return;
367 }
368 CopyMem (Palette, PaletteInfo, PaletteSize);
369 PaletteNum = (UINT16)(Palette->PaletteSize / sizeof (EFI_HII_RGB_PIXEL));
370
371 ZeroMem (PaletteValue, sizeof (PaletteValue));
372 CopyRgbToGopPixel (PaletteValue, Palette->PaletteValue, PaletteNum);
373 FreePool (Palette);
374
375 //
376 // Convert the pixel from 4 bit to corresponding color.
377 //
378 for (Ypos = 0; Ypos < Image->Height; Ypos++) {
379 OffsetY = BITMAP_LEN_4_BIT (Image->Width, Ypos);
380 //
381 // All bits in these bytes are meaningful
382 //
383 for (Xpos = 0; Xpos < Image->Width / 2; Xpos++) {
384 Byte = *(Data + OffsetY + Xpos);
385 BitMapPtr[Ypos * Image->Width + Xpos * 2] = PaletteValue[Byte >> 4];
386 BitMapPtr[Ypos * Image->Width + Xpos * 2 + 1] = PaletteValue[Byte & 0x0F];
387 }
388
389 if (Image->Width % 2 != 0) {
390 //
391 // Padding bits in this byte should be ignored.
392 //
393 Byte = *(Data + OffsetY + Xpos);
394 BitMapPtr[Ypos * Image->Width + Xpos * 2] = PaletteValue[Byte >> 4];
395 }
396 }
397 }
398
399
400 /**
401 Output pixels in "8 bit per pixel" format to an image.
402
403 This is a internal function.
404
405
406 @param Image Points to the image which will store the pixels.
407 @param Data Stores the value of output pixels, 0 ~ 255.
408 @param[in] PaletteInfo PaletteInfo which stores the color of the output
409 pixels. Each entry corresponds to a color within
410 [0, 255].
411
412
413 **/
414 VOID
415 Output8bitPixel (
416 IN OUT EFI_IMAGE_INPUT *Image,
417 IN UINT8 *Data,
418 IN EFI_HII_IMAGE_PALETTE_INFO *PaletteInfo
419 )
420 {
421 UINT16 Xpos;
422 UINT16 Ypos;
423 UINTN OffsetY;
424 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;
425 EFI_GRAPHICS_OUTPUT_BLT_PIXEL PaletteValue[256];
426 EFI_HII_IMAGE_PALETTE_INFO *Palette;
427 UINTN PaletteSize;
428 UINT16 PaletteNum;
429 UINT8 Byte;
430
431 ASSERT (Image != NULL && Data != NULL && PaletteInfo != NULL);
432
433 BitMapPtr = Image->Bitmap;
434
435 //
436 // The bitmap should allocate each color index starting from 0.
437 //
438 PaletteSize = 0;
439 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));
440 PaletteSize += sizeof (UINT16);
441 Palette = AllocateZeroPool (PaletteSize);
442 ASSERT (Palette != NULL);
443 if (Palette == NULL) {
444 return;
445 }
446 CopyMem (Palette, PaletteInfo, PaletteSize);
447 PaletteNum = (UINT16)(Palette->PaletteSize / sizeof (EFI_HII_RGB_PIXEL));
448 ZeroMem (PaletteValue, sizeof (PaletteValue));
449 CopyRgbToGopPixel (PaletteValue, Palette->PaletteValue, PaletteNum);
450 FreePool (Palette);
451
452 //
453 // Convert the pixel from 8 bits to corresponding color.
454 //
455 for (Ypos = 0; Ypos < Image->Height; Ypos++) {
456 OffsetY = BITMAP_LEN_8_BIT (Image->Width, Ypos);
457 //
458 // All bits are meaningful since the bitmap is 8 bits per pixel.
459 //
460 for (Xpos = 0; Xpos < Image->Width; Xpos++) {
461 Byte = *(Data + OffsetY + Xpos);
462 BitMapPtr[OffsetY + Xpos] = PaletteValue[Byte];
463 }
464 }
465
466 }
467
468
469 /**
470 Output pixels in "24 bit per pixel" format to an image.
471
472 This is a internal function.
473
474
475 @param Image Points to the image which will store the pixels.
476 @param Data Stores the color of output pixels, allowing 16.8
477 millions colors.
478
479
480 **/
481 VOID
482 Output24bitPixel (
483 IN OUT EFI_IMAGE_INPUT *Image,
484 IN EFI_HII_RGB_PIXEL *Data
485 )
486 {
487 UINT16 Ypos;
488 UINTN OffsetY;
489 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BitMapPtr;
490
491 ASSERT (Image != NULL && Data != NULL);
492
493 BitMapPtr = Image->Bitmap;
494
495 for (Ypos = 0; Ypos < Image->Height; Ypos++) {
496 OffsetY = BITMAP_LEN_8_BIT (Image->Width, Ypos);
497 CopyRgbToGopPixel (&BitMapPtr[OffsetY], &Data[OffsetY], Image->Width);
498 }
499
500 }
501
502
503 /**
504 Convert the image from EFI_IMAGE_INPUT to EFI_IMAGE_OUTPUT format.
505
506 This is a internal function.
507
508
509 @param BltBuffer Buffer points to bitmap data of incoming image.
510 @param BltX Specifies the offset from the left and top edge of
511 the output image of the first pixel in the image.
512 @param BltY Specifies the offset from the left and top edge of
513 the output image of the first pixel in the image.
514 @param Width Width of the incoming image, in pixels.
515 @param Height Height of the incoming image, in pixels.
516 @param Transparent If TRUE, all "off" pixels in the image will be
517 drawn using the pixel value from blt and all other
518 pixels will be copied.
519 @param Blt Buffer points to bitmap data of output image.
520
521 @retval EFI_SUCCESS The image was successfully converted.
522 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
523
524 **/
525 EFI_STATUS
526 ImageToBlt (
527 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer,
528 IN UINTN BltX,
529 IN UINTN BltY,
530 IN UINTN Width,
531 IN UINTN Height,
532 IN BOOLEAN Transparent,
533 IN OUT EFI_IMAGE_OUTPUT **Blt
534 )
535 {
536 EFI_IMAGE_OUTPUT *ImageOut;
537 UINTN Xpos;
538 UINTN Ypos;
539 UINTN OffsetY1; // src buffer
540 UINTN OffsetY2; // dest buffer
541 EFI_GRAPHICS_OUTPUT_BLT_PIXEL SrcPixel;
542 EFI_GRAPHICS_OUTPUT_BLT_PIXEL ZeroPixel;
543
544 if (BltBuffer == NULL || Blt == NULL || *Blt == NULL) {
545 return EFI_INVALID_PARAMETER;
546 }
547
548 ImageOut = *Blt;
549
550 if (Width + BltX > ImageOut->Width) {
551 return EFI_INVALID_PARAMETER;
552 }
553 if (Height + BltY > ImageOut->Height) {
554 return EFI_INVALID_PARAMETER;
555 }
556
557 ZeroMem (&ZeroPixel, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
558
559 for (Ypos = 0; Ypos < Height; Ypos++) {
560 OffsetY1 = Width * Ypos;
561 OffsetY2 = ImageOut->Width * (BltY + Ypos);
562 for (Xpos = 0; Xpos < Width; Xpos++) {
563 SrcPixel = BltBuffer[OffsetY1 + Xpos];
564 if (Transparent) {
565 if (CompareMem (&SrcPixel, &ZeroPixel, 3) != 0) {
566 ImageOut->Image.Bitmap[OffsetY2 + BltX + Xpos] = SrcPixel;
567 }
568 } else {
569 ImageOut->Image.Bitmap[OffsetY2 + BltX + Xpos] = SrcPixel;
570 }
571 }
572 }
573
574 return EFI_SUCCESS;
575 }
576
577 /**
578 Return the HII package list identified by PackageList HII handle.
579
580 @param Database Pointer to HII database list header.
581 @param PackageList HII handle of the package list to locate.
582
583 @retval The HII package list instance.
584 **/
585 HII_DATABASE_PACKAGE_LIST_INSTANCE *
586 LocatePackageList (
587 IN LIST_ENTRY *Database,
588 IN EFI_HII_HANDLE PackageList
589 )
590 {
591 LIST_ENTRY *Link;
592 HII_DATABASE_RECORD *Record;
593
594 //
595 // Get the specified package list and image package.
596 //
597 for (Link = GetFirstNode (Database);
598 !IsNull (Database, Link);
599 Link = GetNextNode (Database, Link)
600 ) {
601 Record = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
602 if (Record->Handle == PackageList) {
603 return Record->PackageList;
604 }
605 }
606 return NULL;
607 }
608
609 /**
610 This function adds the image Image to the group of images owned by PackageList, and returns
611 a new image identifier (ImageId).
612
613 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.
614 @param PackageList Handle of the package list where this image will
615 be added.
616 @param ImageId On return, contains the new image id, which is
617 unique within PackageList.
618 @param Image Points to the image.
619
620 @retval EFI_SUCCESS The new image was added successfully.
621 @retval EFI_NOT_FOUND The specified PackageList could not be found in
622 database.
623 @retval EFI_OUT_OF_RESOURCES Could not add the image due to lack of resources.
624 @retval EFI_INVALID_PARAMETER Image is NULL or ImageId is NULL.
625
626 **/
627 EFI_STATUS
628 EFIAPI
629 HiiNewImage (
630 IN CONST EFI_HII_IMAGE_PROTOCOL *This,
631 IN EFI_HII_HANDLE PackageList,
632 OUT EFI_IMAGE_ID *ImageId,
633 IN CONST EFI_IMAGE_INPUT *Image
634 )
635 {
636 HII_DATABASE_PRIVATE_DATA *Private;
637 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
638 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;
639 EFI_HII_IMAGE_BLOCK *ImageBlocks;
640 UINT32 NewBlockSize;
641
642 if (This == NULL || ImageId == NULL || Image == NULL || Image->Bitmap == NULL) {
643 return EFI_INVALID_PARAMETER;
644 }
645
646 if (!IsHiiHandleValid (PackageList)) {
647 return EFI_NOT_FOUND;
648 }
649
650 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);
651 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);
652 if (PackageListNode == NULL) {
653 return EFI_NOT_FOUND;
654 }
655
656 NewBlockSize = sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL) +
657 BITMAP_LEN_24_BIT (Image->Width, Image->Height);
658
659 //
660 // Get the image package in the package list,
661 // or create a new image package if image package does not exist.
662 //
663 if (PackageListNode->ImagePkg != NULL) {
664 ImagePackage = PackageListNode->ImagePkg;
665
666 //
667 // Output the image id of the incoming image being inserted, which is the
668 // image id of the EFI_HII_IIBT_END block of old image package.
669 //
670 *ImageId = 0;
671 GetImageIdOrAddress (ImagePackage->ImageBlock, ImageId);
672
673 //
674 // Update the package's image block by appending the new block to the end.
675 //
676 ImageBlocks = AllocatePool (ImagePackage->ImageBlockSize + NewBlockSize);
677 if (ImageBlocks == NULL) {
678 return EFI_OUT_OF_RESOURCES;
679 }
680 //
681 // Copy the original content.
682 //
683 CopyMem (
684 ImageBlocks,
685 ImagePackage->ImageBlock,
686 ImagePackage->ImageBlockSize - sizeof (EFI_HII_IIBT_END_BLOCK)
687 );
688 FreePool (ImagePackage->ImageBlock);
689 ImagePackage->ImageBlock = ImageBlocks;
690
691 //
692 // Point to the very last block.
693 //
694 ImageBlocks = (EFI_HII_IMAGE_BLOCK *) (
695 (UINT8 *) ImageBlocks + ImagePackage->ImageBlockSize - sizeof (EFI_HII_IIBT_END_BLOCK)
696 );
697 //
698 // Update the length record.
699 //
700 ImagePackage->ImageBlockSize += NewBlockSize;
701 ImagePackage->ImagePkgHdr.Header.Length += NewBlockSize;
702 PackageListNode->PackageListHdr.PackageLength += NewBlockSize;
703
704 } else {
705 //
706 // The specified package list does not contain image package.
707 // Create one to add this image block.
708 //
709 ImagePackage = (HII_IMAGE_PACKAGE_INSTANCE *) AllocateZeroPool (sizeof (HII_IMAGE_PACKAGE_INSTANCE));
710 if (ImagePackage == NULL) {
711 return EFI_OUT_OF_RESOURCES;
712 }
713 //
714 // Output the image id of the incoming image being inserted, which is the
715 // first image block so that id is initially to one.
716 //
717 *ImageId = 1;
718 //
719 // Fill in image package header.
720 //
721 ImagePackage->ImagePkgHdr.Header.Length = sizeof (EFI_HII_IMAGE_PACKAGE_HDR) + NewBlockSize + sizeof (EFI_HII_IIBT_END_BLOCK);
722 ImagePackage->ImagePkgHdr.Header.Type = EFI_HII_PACKAGE_IMAGES;
723 ImagePackage->ImagePkgHdr.ImageInfoOffset = sizeof (EFI_HII_IMAGE_PACKAGE_HDR);
724 ImagePackage->ImagePkgHdr.PaletteInfoOffset = 0;
725
726 //
727 // Fill in palette info.
728 //
729 ImagePackage->PaletteBlock = NULL;
730 ImagePackage->PaletteInfoSize = 0;
731
732 //
733 // Fill in image blocks.
734 //
735 ImagePackage->ImageBlockSize = NewBlockSize + sizeof (EFI_HII_IIBT_END_BLOCK);
736 ImagePackage->ImageBlock = AllocateZeroPool (NewBlockSize + sizeof (EFI_HII_IIBT_END_BLOCK));
737 if (ImagePackage->ImageBlock == NULL) {
738 FreePool (ImagePackage);
739 return EFI_OUT_OF_RESOURCES;
740 }
741 ImageBlocks = ImagePackage->ImageBlock;
742
743 //
744 // Insert this image package.
745 //
746 PackageListNode->ImagePkg = ImagePackage;
747 PackageListNode->PackageListHdr.PackageLength += ImagePackage->ImagePkgHdr.Header.Length;
748 }
749
750 //
751 // Append the new block here
752 //
753 if (Image->Flags == EFI_IMAGE_TRANSPARENT) {
754 ImageBlocks->BlockType = EFI_HII_IIBT_IMAGE_24BIT_TRANS;
755 } else {
756 ImageBlocks->BlockType = EFI_HII_IIBT_IMAGE_24BIT;
757 }
758 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) ImageBlocks)->Bitmap.Width, Image->Width);
759 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) ImageBlocks)->Bitmap.Height, Image->Height);
760 CopyGopToRgbPixel (((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) ImageBlocks)->Bitmap.Bitmap, Image->Bitmap, Image->Width * Image->Height);
761
762 //
763 // Append the block end
764 //
765 ImageBlocks = (EFI_HII_IMAGE_BLOCK *) ((UINT8 *) ImageBlocks + NewBlockSize);
766 ImageBlocks->BlockType = EFI_HII_IIBT_END;
767
768 //
769 // Check whether need to get the contents of HiiDataBase.
770 // Only after ReadyToBoot to do the export.
771 //
772 if (gExportAfterReadyToBoot) {
773 HiiGetDatabaseInfo(&Private->HiiDatabase);
774 }
775
776 return EFI_SUCCESS;
777 }
778
779
780 /**
781 This function retrieves the image specified by ImageId which is associated with
782 the specified PackageList and copies it into the buffer specified by Image.
783
784 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.
785 @param PackageList Handle of the package list where this image will
786 be searched.
787 @param ImageId The image's id,, which is unique within
788 PackageList.
789 @param Image Points to the image.
790
791 @retval EFI_SUCCESS The new image was returned successfully.
792 @retval EFI_NOT_FOUND The image specified by ImageId is not in the
793 database. The specified PackageList is not in the database.
794 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to
795 hold the image.
796 @retval EFI_INVALID_PARAMETER The Image or ImageSize was NULL.
797 @retval EFI_OUT_OF_RESOURCES The bitmap could not be retrieved because there was not
798 enough memory.
799
800 **/
801 EFI_STATUS
802 EFIAPI
803 HiiGetImage (
804 IN CONST EFI_HII_IMAGE_PROTOCOL *This,
805 IN EFI_HII_HANDLE PackageList,
806 IN EFI_IMAGE_ID ImageId,
807 OUT EFI_IMAGE_INPUT *Image
808 )
809 {
810 HII_DATABASE_PRIVATE_DATA *Private;
811 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
812 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;
813 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;
814 EFI_HII_IIBT_IMAGE_1BIT_BLOCK Iibt1bit;
815 UINT16 Width;
816 UINT16 Height;
817 UINTN ImageLength;
818 UINT8 *PaletteInfo;
819 UINT8 PaletteIndex;
820 UINT16 PaletteSize;
821
822 if (This == NULL || Image == NULL || ImageId == 0) {
823 return EFI_INVALID_PARAMETER;
824 }
825
826 if (!IsHiiHandleValid (PackageList)) {
827 return EFI_NOT_FOUND;
828 }
829
830 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);
831 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);
832 if (PackageListNode == NULL) {
833 return EFI_NOT_FOUND;
834 }
835 ImagePackage = PackageListNode->ImagePkg;
836 if (ImagePackage == NULL) {
837 return EFI_NOT_FOUND;
838 }
839
840 //
841 // Find the image block specified by ImageId
842 //
843 CurrentImageBlock = GetImageIdOrAddress (ImagePackage->ImageBlock, &ImageId);
844 if (CurrentImageBlock == NULL) {
845 return EFI_NOT_FOUND;
846 }
847
848 switch (CurrentImageBlock->BlockType) {
849 case EFI_HII_IIBT_IMAGE_JPEG:
850 case EFI_HII_IIBT_IMAGE_PNG:
851 //
852 // HiiImage protocol doesn't support return JPEG/PNG.
853 // Use HiiImageEx instead.
854 //
855 return EFI_UNSUPPORTED;
856
857 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:
858 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:
859 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:
860 Image->Flags = EFI_IMAGE_TRANSPARENT;
861 //
862 // fall through
863 //
864 case EFI_HII_IIBT_IMAGE_1BIT:
865 case EFI_HII_IIBT_IMAGE_4BIT:
866 case EFI_HII_IIBT_IMAGE_8BIT:
867 //
868 // Use the common block code since the definition of these structures is the same.
869 //
870 CopyMem (&Iibt1bit, CurrentImageBlock, sizeof (EFI_HII_IIBT_IMAGE_1BIT_BLOCK));
871 ImageLength = sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) *
872 (Iibt1bit.Bitmap.Width * Iibt1bit.Bitmap.Height);
873 Image->Bitmap = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) AllocateZeroPool (ImageLength);
874 if (Image->Bitmap == NULL) {
875 return EFI_OUT_OF_RESOURCES;
876 }
877
878 Image->Width = Iibt1bit.Bitmap.Width;
879 Image->Height = Iibt1bit.Bitmap.Height;
880
881 PaletteInfo = ImagePackage->PaletteBlock + sizeof (EFI_HII_IMAGE_PALETTE_INFO_HEADER);
882 for (PaletteIndex = 1; PaletteIndex < Iibt1bit.PaletteIndex; PaletteIndex++) {
883 CopyMem (&PaletteSize, PaletteInfo, sizeof (UINT16));
884 PaletteInfo += PaletteSize + sizeof (UINT16);
885 }
886 ASSERT (PaletteIndex == Iibt1bit.PaletteIndex);
887
888 //
889 // Output bitmap data
890 //
891 if (CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_1BIT ||
892 CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_1BIT_TRANS) {
893 Output1bitPixel (
894 Image,
895 ((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Data,
896 (EFI_HII_IMAGE_PALETTE_INFO *) PaletteInfo
897 );
898 } else if (CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_4BIT ||
899 CurrentImageBlock->BlockType == EFI_HII_IIBT_IMAGE_4BIT_TRANS) {
900 Output4bitPixel (
901 Image,
902 ((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Data,
903 (EFI_HII_IMAGE_PALETTE_INFO *) PaletteInfo
904 );
905 } else {
906 Output8bitPixel (
907 Image,
908 ((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Data,
909 (EFI_HII_IMAGE_PALETTE_INFO *) PaletteInfo
910 );
911 }
912
913 return EFI_SUCCESS;
914
915 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:
916 Image->Flags = EFI_IMAGE_TRANSPARENT;
917 //
918 // fall through
919 //
920 case EFI_HII_IIBT_IMAGE_24BIT:
921 Width = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width);
922 Height = ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height);
923 ImageLength = sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * (Width * Height);
924 Image->Bitmap = AllocateZeroPool (ImageLength);
925 if (Image->Bitmap == NULL) {
926 return EFI_OUT_OF_RESOURCES;
927 }
928
929 Image->Width = Width;
930 Image->Height = Height;
931
932 //
933 // Output the bitmap data directly.
934 //
935 Output24bitPixel (
936 Image,
937 ((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Bitmap
938 );
939 return EFI_SUCCESS;
940
941 default:
942 return EFI_NOT_FOUND;
943 }
944 }
945
946
947 /**
948 This function updates the image specified by ImageId in the specified PackageListHandle to
949 the image specified by Image.
950
951 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.
952 @param PackageList The package list containing the images.
953 @param ImageId The image's id,, which is unique within
954 PackageList.
955 @param Image Points to the image.
956
957 @retval EFI_SUCCESS The new image was updated successfully.
958 @retval EFI_NOT_FOUND The image specified by ImageId is not in the
959 database. The specified PackageList is not in the database.
960 @retval EFI_INVALID_PARAMETER The Image was NULL.
961
962 **/
963 EFI_STATUS
964 EFIAPI
965 HiiSetImage (
966 IN CONST EFI_HII_IMAGE_PROTOCOL *This,
967 IN EFI_HII_HANDLE PackageList,
968 IN EFI_IMAGE_ID ImageId,
969 IN CONST EFI_IMAGE_INPUT *Image
970 )
971 {
972 HII_DATABASE_PRIVATE_DATA *Private;
973 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
974 HII_IMAGE_PACKAGE_INSTANCE *ImagePackage;
975 EFI_HII_IMAGE_BLOCK *CurrentImageBlock;
976 EFI_HII_IMAGE_BLOCK *ImageBlocks;
977 EFI_HII_IMAGE_BLOCK *NewImageBlock;
978 UINT32 NewBlockSize;
979 UINT32 OldBlockSize;
980 UINT32 Part1Size;
981 UINT32 Part2Size;
982
983 if (This == NULL || Image == NULL || ImageId == 0 || Image->Bitmap == NULL) {
984 return EFI_INVALID_PARAMETER;
985 }
986
987 if (!IsHiiHandleValid (PackageList)) {
988 return EFI_NOT_FOUND;
989 }
990
991 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);
992 PackageListNode = LocatePackageList (&Private->DatabaseList, PackageList);
993 if (PackageListNode == NULL) {
994 return EFI_NOT_FOUND;
995 }
996 ImagePackage = PackageListNode->ImagePkg;
997 if (ImagePackage == NULL) {
998 return EFI_NOT_FOUND;
999 }
1000
1001 //
1002 // Find the image block specified by ImageId
1003 //
1004 CurrentImageBlock = GetImageIdOrAddress (ImagePackage->ImageBlock, &ImageId);
1005 if (CurrentImageBlock == NULL) {
1006 return EFI_NOT_FOUND;
1007 }
1008
1009 //
1010 // Get the size of original image block. Use some common block code here
1011 // since the definition of some structures is the same.
1012 //
1013 switch (CurrentImageBlock->BlockType) {
1014 case EFI_HII_IIBT_IMAGE_JPEG:
1015 OldBlockSize = OFFSET_OF (EFI_HII_IIBT_JPEG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_JPEG_BLOCK *) CurrentImageBlock)->Size);
1016 break;
1017 case EFI_HII_IIBT_IMAGE_PNG:
1018 OldBlockSize = OFFSET_OF (EFI_HII_IIBT_PNG_BLOCK, Data) + ReadUnaligned32 ((VOID *) &((EFI_HII_IIBT_PNG_BLOCK *) CurrentImageBlock)->Size);
1019 break;
1020 case EFI_HII_IIBT_IMAGE_1BIT:
1021 case EFI_HII_IIBT_IMAGE_1BIT_TRANS:
1022 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_1BIT_BLOCK) - sizeof (UINT8) +
1023 BITMAP_LEN_1_BIT (
1024 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
1025 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_1BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
1026 );
1027 break;
1028 case EFI_HII_IIBT_IMAGE_4BIT:
1029 case EFI_HII_IIBT_IMAGE_4BIT_TRANS:
1030 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_4BIT_BLOCK) - sizeof (UINT8) +
1031 BITMAP_LEN_4_BIT (
1032 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
1033 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_4BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
1034 );
1035 break;
1036 case EFI_HII_IIBT_IMAGE_8BIT:
1037 case EFI_HII_IIBT_IMAGE_8BIT_TRANS:
1038 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_8BIT_BLOCK) - sizeof (UINT8) +
1039 BITMAP_LEN_8_BIT (
1040 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
1041 ReadUnaligned16 (&((EFI_HII_IIBT_IMAGE_8BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
1042 );
1043 break;
1044 case EFI_HII_IIBT_IMAGE_24BIT:
1045 case EFI_HII_IIBT_IMAGE_24BIT_TRANS:
1046 OldBlockSize = sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL) +
1047 BITMAP_LEN_24_BIT (
1048 ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Width),
1049 ReadUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) CurrentImageBlock)->Bitmap.Height)
1050 );
1051 break;
1052 default:
1053 return EFI_NOT_FOUND;
1054 }
1055
1056 //
1057 // Create the new image block according to input image.
1058 //
1059 NewBlockSize = sizeof (EFI_HII_IIBT_IMAGE_24BIT_BLOCK) - sizeof (EFI_HII_RGB_PIXEL) +
1060 BITMAP_LEN_24_BIT (Image->Width, Image->Height);
1061 //
1062 // Adjust the image package to remove the original block firstly then add the new block.
1063 //
1064 ImageBlocks = AllocateZeroPool (ImagePackage->ImageBlockSize + NewBlockSize - OldBlockSize);
1065 if (ImageBlocks == NULL) {
1066 return EFI_OUT_OF_RESOURCES;
1067 }
1068
1069 Part1Size = (UINT32) (UINTN) ((UINT8 *) CurrentImageBlock - (UINT8 *) ImagePackage->ImageBlock);
1070 Part2Size = ImagePackage->ImageBlockSize - Part1Size - OldBlockSize;
1071 CopyMem (ImageBlocks, ImagePackage->ImageBlock, Part1Size);
1072
1073 //
1074 // Set the new image block
1075 //
1076 NewImageBlock = (EFI_HII_IMAGE_BLOCK *) ((UINT8 *) ImageBlocks + Part1Size);
1077 if ((Image->Flags & EFI_IMAGE_TRANSPARENT) == EFI_IMAGE_TRANSPARENT) {
1078 NewImageBlock->BlockType= EFI_HII_IIBT_IMAGE_24BIT_TRANS;
1079 } else {
1080 NewImageBlock->BlockType = EFI_HII_IIBT_IMAGE_24BIT;
1081 }
1082 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) NewImageBlock)->Bitmap.Width, Image->Width);
1083 WriteUnaligned16 ((VOID *) &((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) NewImageBlock)->Bitmap.Height, Image->Height);
1084 CopyGopToRgbPixel (((EFI_HII_IIBT_IMAGE_24BIT_BLOCK *) NewImageBlock)->Bitmap.Bitmap,
1085 Image->Bitmap, Image->Width * Image->Height);
1086
1087 CopyMem ((UINT8 *) NewImageBlock + NewBlockSize, (UINT8 *) CurrentImageBlock + OldBlockSize, Part2Size);
1088
1089 FreePool (ImagePackage->ImageBlock);
1090 ImagePackage->ImageBlock = ImageBlocks;
1091 ImagePackage->ImageBlockSize += NewBlockSize - OldBlockSize;
1092 ImagePackage->ImagePkgHdr.Header.Length += NewBlockSize - OldBlockSize;
1093 PackageListNode->PackageListHdr.PackageLength += NewBlockSize - OldBlockSize;
1094
1095 //
1096 // Check whether need to get the contents of HiiDataBase.
1097 // Only after ReadyToBoot to do the export.
1098 //
1099 if (gExportAfterReadyToBoot) {
1100 HiiGetDatabaseInfo(&Private->HiiDatabase);
1101 }
1102
1103 return EFI_SUCCESS;
1104
1105 }
1106
1107
1108 /**
1109 This function renders an image to a bitmap or the screen using the specified
1110 color and options. It draws the image on an existing bitmap, allocates a new
1111 bitmap or uses the screen. The images can be clipped.
1112
1113 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.
1114 @param Flags Describes how the image is to be drawn.
1115 @param Image Points to the image to be displayed.
1116 @param Blt If this points to a non-NULL on entry, this points
1117 to the image, which is Width pixels wide and
1118 Height pixels high. The image will be drawn onto
1119 this image and EFI_HII_DRAW_FLAG_CLIP is implied.
1120 If this points to a NULL on entry, then a buffer
1121 will be allocated to hold the generated image and
1122 the pointer updated on exit. It is the caller's
1123 responsibility to free this buffer.
1124 @param BltX Specifies the offset from the left and top edge of
1125 the output image of the first pixel in the image.
1126 @param BltY Specifies the offset from the left and top edge of
1127 the output image of the first pixel in the image.
1128
1129 @retval EFI_SUCCESS The image was successfully drawn.
1130 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.
1131 @retval EFI_INVALID_PARAMETER The Image or Blt was NULL.
1132 @retval EFI_INVALID_PARAMETER Any combination of Flags is invalid.
1133
1134 **/
1135 EFI_STATUS
1136 EFIAPI
1137 HiiDrawImage (
1138 IN CONST EFI_HII_IMAGE_PROTOCOL *This,
1139 IN EFI_HII_DRAW_FLAGS Flags,
1140 IN CONST EFI_IMAGE_INPUT *Image,
1141 IN OUT EFI_IMAGE_OUTPUT **Blt,
1142 IN UINTN BltX,
1143 IN UINTN BltY
1144 )
1145 {
1146 EFI_STATUS Status;
1147 HII_DATABASE_PRIVATE_DATA *Private;
1148 BOOLEAN Transparent;
1149 EFI_IMAGE_OUTPUT *ImageOut;
1150 EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer;
1151 UINTN BufferLen;
1152 UINTN Width;
1153 UINTN Height;
1154 UINTN Xpos;
1155 UINTN Ypos;
1156 UINTN OffsetY1;
1157 UINTN OffsetY2;
1158 EFI_FONT_DISPLAY_INFO *FontInfo;
1159 UINTN Index;
1160
1161 if (This == NULL || Image == NULL || Blt == NULL) {
1162 return EFI_INVALID_PARAMETER;
1163 }
1164
1165 if ((Flags & EFI_HII_DRAW_FLAG_CLIP) == EFI_HII_DRAW_FLAG_CLIP && *Blt == NULL) {
1166 return EFI_INVALID_PARAMETER;
1167 }
1168
1169 if ((Flags & EFI_HII_DRAW_FLAG_TRANSPARENT) == EFI_HII_DRAW_FLAG_TRANSPARENT) {
1170 return EFI_INVALID_PARAMETER;
1171 }
1172
1173 FontInfo = NULL;
1174
1175 //
1176 // Check whether the image will be drawn transparently or opaquely.
1177 //
1178 Transparent = FALSE;
1179 if ((Flags & EFI_HII_DRAW_FLAG_TRANSPARENT) == EFI_HII_DRAW_FLAG_FORCE_TRANS) {
1180 Transparent = TRUE;
1181 } else if ((Flags & EFI_HII_DRAW_FLAG_TRANSPARENT) == EFI_HII_DRAW_FLAG_FORCE_OPAQUE){
1182 Transparent = FALSE;
1183 } else {
1184 //
1185 // Now EFI_HII_DRAW_FLAG_DEFAULT is set, whether image will be drawn depending
1186 // on the image's transparency setting.
1187 //
1188 if ((Image->Flags & EFI_IMAGE_TRANSPARENT) == EFI_IMAGE_TRANSPARENT) {
1189 Transparent = TRUE;
1190 }
1191 }
1192
1193 //
1194 // Image cannot be drawn transparently if Blt points to NULL on entry.
1195 // Currently output to Screen transparently is not supported, either.
1196 //
1197 if (Transparent) {
1198 if (*Blt == NULL) {
1199 return EFI_INVALID_PARAMETER;
1200 } else if ((Flags & EFI_HII_DIRECT_TO_SCREEN) == EFI_HII_DIRECT_TO_SCREEN) {
1201 return EFI_INVALID_PARAMETER;
1202 }
1203 }
1204
1205 Private = HII_IMAGE_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1206
1207 //
1208 // When Blt points to a non-NULL on entry, this image will be drawn onto
1209 // this bitmap or screen pointed by "*Blt" and EFI_HII_DRAW_FLAG_CLIP is implied.
1210 // Otherwise a new bitmap will be allocated to hold this image.
1211 //
1212 if (*Blt != NULL) {
1213 //
1214 // Clip the image by (Width, Height)
1215 //
1216
1217 Width = Image->Width;
1218 Height = Image->Height;
1219
1220 if (Width > (*Blt)->Width - BltX) {
1221 Width = (*Blt)->Width - BltX;
1222 }
1223 if (Height > (*Blt)->Height - BltY) {
1224 Height = (*Blt)->Height - BltY;
1225 }
1226
1227 BufferLen = Width * Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
1228 BltBuffer = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) AllocateZeroPool (BufferLen);
1229 if (BltBuffer == NULL) {
1230 return EFI_OUT_OF_RESOURCES;
1231 }
1232
1233 if (Width == Image->Width && Height == Image->Height) {
1234 CopyMem (BltBuffer, Image->Bitmap, BufferLen);
1235 } else {
1236 for (Ypos = 0; Ypos < Height; Ypos++) {
1237 OffsetY1 = Image->Width * Ypos;
1238 OffsetY2 = Width * Ypos;
1239 for (Xpos = 0; Xpos < Width; Xpos++) {
1240 BltBuffer[OffsetY2 + Xpos] = Image->Bitmap[OffsetY1 + Xpos];
1241 }
1242 }
1243 }
1244
1245 //
1246 // Draw the image to existing bitmap or screen depending on flag.
1247 //
1248 if ((Flags & EFI_HII_DIRECT_TO_SCREEN) == EFI_HII_DIRECT_TO_SCREEN) {
1249 //
1250 // Caller should make sure the current UGA console is grarphic mode.
1251 //
1252
1253 //
1254 // Write the image directly to the output device specified by Screen.
1255 //
1256 Status = (*Blt)->Image.Screen->Blt (
1257 (*Blt)->Image.Screen,
1258 BltBuffer,
1259 EfiBltBufferToVideo,
1260 0,
1261 0,
1262 BltX,
1263 BltY,
1264 Width,
1265 Height,
1266 0
1267 );
1268 } else {
1269 //
1270 // Draw the image onto the existing bitmap specified by Bitmap.
1271 //
1272 Status = ImageToBlt (
1273 BltBuffer,
1274 BltX,
1275 BltY,
1276 Width,
1277 Height,
1278 Transparent,
1279 Blt
1280 );
1281
1282 }
1283
1284 FreePool (BltBuffer);
1285 return Status;
1286
1287 } else {
1288 //
1289 // Allocate a new bitmap to hold the incoming image.
1290 //
1291 Width = Image->Width + BltX;
1292 Height = Image->Height + BltY;
1293
1294 BufferLen = Width * Height * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
1295 BltBuffer = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) AllocateZeroPool (BufferLen);
1296 if (BltBuffer == NULL) {
1297 return EFI_OUT_OF_RESOURCES;
1298 }
1299
1300 ImageOut = (EFI_IMAGE_OUTPUT *) AllocateZeroPool (sizeof (EFI_IMAGE_OUTPUT));
1301 if (ImageOut == NULL) {
1302 FreePool (BltBuffer);
1303 return EFI_OUT_OF_RESOURCES;
1304 }
1305 ImageOut->Width = (UINT16) Width;
1306 ImageOut->Height = (UINT16) Height;
1307 ImageOut->Image.Bitmap = BltBuffer;
1308
1309 //
1310 // BUGBUG: Now all the "blank" pixels are filled with system default background
1311 // color. Not sure if it need to be updated or not.
1312 //
1313 Status = GetSystemFont (Private, &FontInfo, NULL);
1314 if (EFI_ERROR (Status)) {
1315 FreePool (BltBuffer);
1316 FreePool (ImageOut);
1317 return Status;
1318 }
1319 ASSERT (FontInfo != NULL);
1320 for (Index = 0; Index < Width * Height; Index++) {
1321 BltBuffer[Index] = FontInfo->BackgroundColor;
1322 }
1323 FreePool (FontInfo);
1324
1325 //
1326 // Draw the incoming image to the new created image.
1327 //
1328 *Blt = ImageOut;
1329 return ImageToBlt (
1330 Image->Bitmap,
1331 BltX,
1332 BltY,
1333 Image->Width,
1334 Image->Height,
1335 Transparent,
1336 Blt
1337 );
1338
1339 }
1340 }
1341
1342
1343 /**
1344 This function renders an image to a bitmap or the screen using the specified
1345 color and options. It draws the image on an existing bitmap, allocates a new
1346 bitmap or uses the screen. The images can be clipped.
1347
1348 @param This A pointer to the EFI_HII_IMAGE_PROTOCOL instance.
1349 @param Flags Describes how the image is to be drawn.
1350 @param PackageList The package list in the HII database to search for
1351 the specified image.
1352 @param ImageId The image's id, which is unique within
1353 PackageList.
1354 @param Blt If this points to a non-NULL on entry, this points
1355 to the image, which is Width pixels wide and
1356 Height pixels high. The image will be drawn onto
1357 this image and
1358 EFI_HII_DRAW_FLAG_CLIP is implied. If this points
1359 to a NULL on entry, then a buffer will be
1360 allocated to hold the generated image and the
1361 pointer updated on exit. It is the caller's
1362 responsibility to free this buffer.
1363 @param BltX Specifies the offset from the left and top edge of
1364 the output image of the first pixel in the image.
1365 @param BltY Specifies the offset from the left and top edge of
1366 the output image of the first pixel in the image.
1367
1368 @retval EFI_SUCCESS The image was successfully drawn.
1369 @retval EFI_OUT_OF_RESOURCES Unable to allocate an output buffer for Blt.
1370 @retval EFI_INVALID_PARAMETER The Blt was NULL.
1371 @retval EFI_NOT_FOUND The image specified by ImageId is not in the database.
1372 The specified PackageList is not in the database.
1373
1374 **/
1375 EFI_STATUS
1376 EFIAPI
1377 HiiDrawImageId (
1378 IN CONST EFI_HII_IMAGE_PROTOCOL *This,
1379 IN EFI_HII_DRAW_FLAGS Flags,
1380 IN EFI_HII_HANDLE PackageList,
1381 IN EFI_IMAGE_ID ImageId,
1382 IN OUT EFI_IMAGE_OUTPUT **Blt,
1383 IN UINTN BltX,
1384 IN UINTN BltY
1385 )
1386 {
1387 EFI_STATUS Status;
1388 EFI_IMAGE_INPUT Image;
1389
1390 //
1391 // Check input parameter.
1392 //
1393 if (This == NULL || Blt == NULL) {
1394 return EFI_INVALID_PARAMETER;
1395 }
1396
1397 if (!IsHiiHandleValid (PackageList)) {
1398 return EFI_NOT_FOUND;
1399 }
1400
1401 //
1402 // Get the specified Image.
1403 //
1404 Status = HiiGetImage (This, PackageList, ImageId, &Image);
1405 if (EFI_ERROR (Status)) {
1406 return Status;
1407 }
1408
1409 //
1410 // Draw this image.
1411 //
1412 Status = HiiDrawImage (This, Flags, &Image, Blt, BltX, BltY);
1413 if (Image.Bitmap != NULL) {
1414 FreePool (Image.Bitmap);
1415 }
1416 return Status;
1417 }
1418