]> code.delx.au - refind/blob - libeg/image.c
Improved submenu and information menu appearance over a full-screen
[refind] / libeg / image.c
1 /*
2 * libeg/image.c
3 * Image handling functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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.
23 *
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.
35 */
36
37 #include "libegint.h"
38 #include "../refind/global.h"
39 #include "../refind/lib.h"
40 #include "../refind/screen.h"
41 #include "../include/refit_call_wrapper.h"
42 #include "lodepng.h"
43
44 #define MAX_FILE_SIZE (1024*1024*1024)
45
46 #ifndef __MAKEWITH_GNUEFI
47 #define LibLocateHandle gBS->LocateHandleBuffer
48 #define LibOpenRoot EfiLibOpenRoot
49 #endif
50
51 //
52 // Basic image handling
53 //
54
55 EG_IMAGE * egCreateImage(IN UINTN Width, IN UINTN Height, IN BOOLEAN HasAlpha)
56 {
57 EG_IMAGE *NewImage;
58
59 NewImage = (EG_IMAGE *) AllocatePool(sizeof(EG_IMAGE));
60 if (NewImage == NULL)
61 return NULL;
62 NewImage->PixelData = (EG_PIXEL *) AllocatePool(Width * Height * sizeof(EG_PIXEL));
63 if (NewImage->PixelData == NULL) {
64 FreePool(NewImage);
65 return NULL;
66 }
67
68 NewImage->Width = Width;
69 NewImage->Height = Height;
70 NewImage->HasAlpha = HasAlpha;
71 return NewImage;
72 }
73
74 EG_IMAGE * egCreateFilledImage(IN UINTN Width, IN UINTN Height, IN BOOLEAN HasAlpha, IN EG_PIXEL *Color)
75 {
76 EG_IMAGE *NewImage;
77
78 NewImage = egCreateImage(Width, Height, HasAlpha);
79 if (NewImage == NULL)
80 return NULL;
81
82 egFillImage(NewImage, Color);
83 return NewImage;
84 }
85
86 EG_IMAGE * egCopyImage(IN EG_IMAGE *Image)
87 {
88 EG_IMAGE *NewImage;
89
90 NewImage = egCreateImage(Image->Width, Image->Height, Image->HasAlpha);
91 if (NewImage == NULL)
92 return NULL;
93
94 CopyMem(NewImage->PixelData, Image->PixelData, Image->Width * Image->Height * sizeof(EG_PIXEL));
95 return NewImage;
96 }
97
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;
102 UINTN x, y;
103
104 if (((StartX + Width) > Image->Width) || ((StartY + Height) > Image->Height))
105 return NULL;
106
107 NewImage = egCreateImage(Width, Height, Image->HasAlpha);
108 if (NewImage == NULL)
109 return NULL;
110
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];
114 }
115 }
116 return NewImage;
117 } // EG_IMAGE * egCropImage()
118
119 VOID egFreeImage(IN EG_IMAGE *Image)
120 {
121 if (Image != NULL) {
122 if (Image->PixelData != NULL)
123 FreePool(Image->PixelData);
124 FreePool(Image);
125 }
126 }
127
128 //
129 // Basic file operations
130 //
131
132 EFI_STATUS egLoadFile(IN EFI_FILE* BaseDir, IN CHAR16 *FileName,
133 OUT UINT8 **FileData, OUT UINTN *FileDataLength)
134 {
135 EFI_STATUS Status;
136 EFI_FILE_HANDLE FileHandle;
137 EFI_FILE_INFO *FileInfo;
138 UINT64 ReadSize;
139 UINTN BufferSize;
140 UINT8 *Buffer;
141
142 Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
143 if (EFI_ERROR(Status)) {
144 return Status;
145 }
146
147 FileInfo = LibFileInfo(FileHandle);
148 if (FileInfo == NULL) {
149 refit_call1_wrapper(FileHandle->Close, FileHandle);
150 return EFI_NOT_FOUND;
151 }
152 ReadSize = FileInfo->FileSize;
153 if (ReadSize > MAX_FILE_SIZE)
154 ReadSize = MAX_FILE_SIZE;
155 FreePool(FileInfo);
156
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;
162 }
163
164 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &BufferSize, Buffer);
165 refit_call1_wrapper(FileHandle->Close, FileHandle);
166 if (EFI_ERROR(Status)) {
167 FreePool(Buffer);
168 return Status;
169 }
170
171 *FileData = Buffer;
172 *FileDataLength = BufferSize;
173 return EFI_SUCCESS;
174 }
175
176 static EFI_GUID ESPGuid = { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
177
178 static EFI_STATUS egFindESP(OUT EFI_FILE_HANDLE *RootDir)
179 {
180 EFI_STATUS Status;
181 UINTN HandleCount = 0;
182 EFI_HANDLE *Handles;
183
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;
189 FreePool(Handles);
190 }
191 return Status;
192 }
193
194 EFI_STATUS egSaveFile(IN EFI_FILE* BaseDir OPTIONAL, IN CHAR16 *FileName,
195 IN UINT8 *FileData, IN UINTN FileDataLength)
196 {
197 EFI_STATUS Status;
198 EFI_FILE_HANDLE FileHandle;
199 UINTN BufferSize;
200
201 if (BaseDir == NULL) {
202 Status = egFindESP(&BaseDir);
203 if (EFI_ERROR(Status))
204 return Status;
205 }
206
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))
210 return Status;
211
212 BufferSize = FileDataLength;
213 Status = refit_call3_wrapper(FileHandle->Write, FileHandle, &BufferSize, FileData);
214 refit_call1_wrapper(FileHandle->Close, FileHandle);
215
216 return Status;
217 }
218
219 //
220 // Loading images from files and embedded data
221 //
222
223 static CHAR16 * egFindExtension(IN CHAR16 *FileName)
224 {
225 UINTN i;
226
227 for (i = StrLen(FileName); i >= 0; i--) {
228 if (FileName[i] == '.')
229 return FileName + i + 1;
230 if (FileName[i] == '/' || FileName[i] == '\\')
231 break;
232 }
233 return FileName + StrLen(FileName);
234 }
235
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)
241 {
242 EG_IMAGE *NewImage = NULL;
243
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);
255 } // if/else
256
257 return NewImage;
258 }
259
260 EG_IMAGE * egLoadImage(IN EFI_FILE* BaseDir, IN CHAR16 *FileName, IN BOOLEAN WantAlpha)
261 {
262 EFI_STATUS Status;
263 UINT8 *FileData;
264 UINTN FileDataLength;
265 EG_IMAGE *NewImage;
266
267 if (BaseDir == NULL || FileName == NULL)
268 return NULL;
269
270 // load file
271 Status = egLoadFile(BaseDir, FileName, &FileData, &FileDataLength);
272 if (EFI_ERROR(Status))
273 return NULL;
274
275 // decode it
276 NewImage = egDecodeAny(FileData, FileDataLength, egFindExtension(FileName), 128, WantAlpha);
277 FreePool(FileData);
278
279 return NewImage;
280 }
281
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)
290 {
291 EFI_STATUS Status;
292 UINT8 *FileData;
293 UINTN FileDataLength;
294 CHAR16 *FileName, FileName2[256];
295 EG_IMAGE *NewImage;
296
297 if (BaseDir == NULL || Path == NULL)
298 return NULL;
299
300 // load file
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))
307 return NULL;
308 }
309
310 // decode it
311 NewImage = egDecodeAny(FileData, FileDataLength, egFindExtension(Path), IconSize, TRUE);
312 FreePool(FileData);
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);
316 NewImage = NULL;
317 }
318
319 return NewImage;
320 } // EG_IMAGE *egLoadIcon()
321
322 EG_IMAGE * egDecodeImage(IN UINT8 *FileData, IN UINTN FileDataLength, IN CHAR16 *Format, IN BOOLEAN WantAlpha)
323 {
324 return egDecodeAny(FileData, FileDataLength, Format, 128, WantAlpha);
325 }
326
327 EG_IMAGE * egPrepareEmbeddedImage(IN EG_EMBEDDED_IMAGE *EmbeddedImage, IN BOOLEAN WantAlpha)
328 {
329 EG_IMAGE *NewImage;
330 UINT8 *CompData;
331 UINTN CompLen;
332 UINTN PixelCount;
333
334 // sanity check
335 if (EmbeddedImage->PixelMode > EG_MAX_EIPIXELMODE ||
336 (EmbeddedImage->CompressMode != EG_EICOMPMODE_NONE && EmbeddedImage->CompressMode != EG_EICOMPMODE_RLE))
337 return NULL;
338
339 // allocate image structure and pixel buffer
340 NewImage = egCreateImage(EmbeddedImage->Width, EmbeddedImage->Height, WantAlpha);
341 if (NewImage == NULL)
342 return NULL;
343
344 CompData = (UINT8 *)EmbeddedImage->Data; // drop const
345 CompLen = EmbeddedImage->DataLength;
346 PixelCount = EmbeddedImage->Width * EmbeddedImage->Height;
347
348 // FUTURE: for EG_EICOMPMODE_EFICOMPRESS, decompress whole data block here
349
350 if (EmbeddedImage->PixelMode == EG_EIPIXELMODE_GRAY ||
351 EmbeddedImage->PixelMode == EG_EIPIXELMODE_GRAY_ALPHA) {
352
353 // copy grayscale plane and expand
354 if (EmbeddedImage->CompressMode == EG_EICOMPMODE_RLE) {
355 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, r), PixelCount);
356 } else {
357 egInsertPlane(CompData, PLPTR(NewImage, r), PixelCount);
358 CompData += PixelCount;
359 }
360 egCopyPlane(PLPTR(NewImage, r), PLPTR(NewImage, g), PixelCount);
361 egCopyPlane(PLPTR(NewImage, r), PLPTR(NewImage, b), PixelCount);
362
363 } else if (EmbeddedImage->PixelMode == EG_EIPIXELMODE_COLOR ||
364 EmbeddedImage->PixelMode == EG_EIPIXELMODE_COLOR_ALPHA) {
365
366 // copy color planes
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);
371 } else {
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;
378 }
379
380 } else {
381
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);
386
387 }
388
389 if (WantAlpha && (EmbeddedImage->PixelMode == EG_EIPIXELMODE_GRAY_ALPHA ||
390 EmbeddedImage->PixelMode == EG_EIPIXELMODE_COLOR_ALPHA ||
391 EmbeddedImage->PixelMode == EG_EIPIXELMODE_ALPHA)) {
392
393 // copy alpha plane
394 if (EmbeddedImage->CompressMode == EG_EICOMPMODE_RLE) {
395 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, a), PixelCount);
396 } else {
397 egInsertPlane(CompData, PLPTR(NewImage, a), PixelCount);
398 CompData += PixelCount;
399 }
400
401 } else {
402 egSetPlane(PLPTR(NewImage, a), WantAlpha ? 255 : 0, PixelCount);
403 }
404
405 return NewImage;
406 }
407
408 //
409 // Compositing
410 //
411
412 VOID egRestrictImageArea(IN EG_IMAGE *Image,
413 IN UINTN AreaPosX, IN UINTN AreaPosY,
414 IN OUT UINTN *AreaWidth, IN OUT UINTN *AreaHeight)
415 {
416 if (AreaPosX >= Image->Width || AreaPosY >= Image->Height) {
417 // out of bounds, operation has no effect
418 *AreaWidth = 0;
419 *AreaHeight = 0;
420 } else {
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;
426 }
427 }
428
429 VOID egFillImage(IN OUT EG_IMAGE *CompImage, IN EG_PIXEL *Color)
430 {
431 UINTN i;
432 EG_PIXEL FillColor;
433 EG_PIXEL *PixelPtr;
434
435 FillColor = *Color;
436 if (!CompImage->HasAlpha)
437 FillColor.a = 0;
438
439 PixelPtr = CompImage->PixelData;
440 for (i = 0; i < CompImage->Width * CompImage->Height; i++, PixelPtr++)
441 *PixelPtr = FillColor;
442 }
443
444 VOID egFillImageArea(IN OUT EG_IMAGE *CompImage,
445 IN UINTN AreaPosX, IN UINTN AreaPosY,
446 IN UINTN AreaWidth, IN UINTN AreaHeight,
447 IN EG_PIXEL *Color)
448 {
449 UINTN x, y;
450 EG_PIXEL FillColor;
451 EG_PIXEL *PixelPtr;
452 EG_PIXEL *PixelBasePtr;
453
454 egRestrictImageArea(CompImage, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
455
456 if (AreaWidth > 0) {
457 FillColor = *Color;
458 if (!CompImage->HasAlpha)
459 FillColor.a = 0;
460
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;
467 }
468 }
469 }
470
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)
474 {
475 UINTN x, y;
476 EG_PIXEL *TopPtr, *CompPtr;
477
478 for (y = 0; y < Height; y++) {
479 TopPtr = TopBasePtr;
480 CompPtr = CompBasePtr;
481 for (x = 0; x < Width; x++) {
482 *CompPtr = *TopPtr;
483 TopPtr++, CompPtr++;
484 }
485 TopBasePtr += TopLineOffset;
486 CompBasePtr += CompLineOffset;
487 }
488 }
489
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)
493 {
494 UINTN x, y;
495 EG_PIXEL *TopPtr, *CompPtr;
496 UINTN Alpha;
497 UINTN RevAlpha;
498 UINTN Temp;
499
500 for (y = 0; y < Height; y++) {
501 TopPtr = TopBasePtr;
502 CompPtr = CompBasePtr;
503 for (x = 0; x < Width; x++) {
504 Alpha = TopPtr->a;
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;
512 /*
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;
516 */
517 TopPtr++, CompPtr++;
518 }
519 TopBasePtr += TopLineOffset;
520 CompBasePtr += CompLineOffset;
521 }
522 }
523
524 VOID egComposeImage(IN OUT EG_IMAGE *CompImage, IN EG_IMAGE *TopImage, IN UINTN PosX, IN UINTN PosY)
525 {
526 UINTN CompWidth, CompHeight;
527
528 CompWidth = TopImage->Width;
529 CompHeight = TopImage->Height;
530 egRestrictImageArea(CompImage, PosX, PosY, &CompWidth, &CompHeight);
531
532 // compose
533 if (CompWidth > 0) {
534 // if (CompImage->HasAlpha) {
535 // CompImage->HasAlpha = FALSE;
536 // egSetPlane(PLPTR(CompImage, a), 0, CompImage->Width * CompImage->Height);
537 // }
538
539 if (TopImage->HasAlpha) {
540 egRawCompose(CompImage->PixelData + PosY * CompImage->Width + PosX, TopImage->PixelData,
541 CompWidth, CompHeight, CompImage->Width, TopImage->Width);
542 } else {
543 egRawCopy(CompImage->PixelData + PosY * CompImage->Width + PosX, TopImage->PixelData,
544 CompWidth, CompHeight, CompImage->Width, TopImage->Width);
545 }
546 }
547 }
548
549 EG_IMAGE * egEnsureImageSize(IN EG_IMAGE *Image, IN UINTN Width, IN UINTN Height, IN EG_PIXEL *Color)
550 {
551 EG_IMAGE *NewImage;
552
553 if (Image == NULL)
554 return NULL;
555 if (Image->Width == Width && Image->Height == Height)
556 return Image;
557
558 NewImage = egCreateFilledImage(Width, Height, Image->HasAlpha, Color);
559 if (NewImage == NULL) {
560 egFreeImage(Image);
561 return NULL;
562 }
563 egComposeImage(NewImage, Image, 0, 0);
564 egFreeImage(Image);
565
566 return NewImage;
567 }
568
569 //
570 // misc internal functions
571 //
572
573 VOID egInsertPlane(IN UINT8 *SrcDataPtr, IN UINT8 *DestPlanePtr, IN UINTN PixelCount)
574 {
575 UINTN i;
576
577 for (i = 0; i < PixelCount; i++) {
578 *DestPlanePtr = *SrcDataPtr++;
579 DestPlanePtr += 4;
580 }
581 }
582
583 VOID egSetPlane(IN UINT8 *DestPlanePtr, IN UINT8 Value, IN UINTN PixelCount)
584 {
585 UINTN i;
586
587 for (i = 0; i < PixelCount; i++) {
588 *DestPlanePtr = Value;
589 DestPlanePtr += 4;
590 }
591 }
592
593 VOID egCopyPlane(IN UINT8 *SrcPlanePtr, IN UINT8 *DestPlanePtr, IN UINTN PixelCount)
594 {
595 UINTN i;
596
597 for (i = 0; i < PixelCount; i++) {
598 *DestPlanePtr = *SrcPlanePtr;
599 DestPlanePtr += 4, SrcPlanePtr += 4;
600 }
601 }
602
603 /* EOF */