]> code.delx.au - refind/blob - libeg/image.c
Added OS check to mkrlconf.sh. Bypass checks for BIOS-mode boot
[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 * Modifications copyright (c) 2012-2015 Roderick W. Smith
38 *
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.
42 *
43 */
44
45 #include "libegint.h"
46 #include "../refind/global.h"
47 #include "../refind/lib.h"
48 #include "../refind/screen.h"
49 #include "../include/refit_call_wrapper.h"
50 #include "lodepng.h"
51
52 #define MAX_FILE_SIZE (1024*1024*1024)
53
54 #ifndef __MAKEWITH_GNUEFI
55 #define LibLocateHandle gBS->LocateHandleBuffer
56 #define LibOpenRoot EfiLibOpenRoot
57 #endif
58
59 //
60 // Basic image handling
61 //
62
63 EG_IMAGE * egCreateImage(IN UINTN Width, IN UINTN Height, IN BOOLEAN HasAlpha)
64 {
65 EG_IMAGE *NewImage;
66
67 NewImage = (EG_IMAGE *) AllocatePool(sizeof(EG_IMAGE));
68 if (NewImage == NULL)
69 return NULL;
70 NewImage->PixelData = (EG_PIXEL *) AllocatePool(Width * Height * sizeof(EG_PIXEL));
71 if (NewImage->PixelData == NULL) {
72 egFreeImage(NewImage);
73 return NULL;
74 }
75
76 NewImage->Width = Width;
77 NewImage->Height = Height;
78 NewImage->HasAlpha = HasAlpha;
79 return NewImage;
80 }
81
82 EG_IMAGE * egCreateFilledImage(IN UINTN Width, IN UINTN Height, IN BOOLEAN HasAlpha, IN EG_PIXEL *Color)
83 {
84 EG_IMAGE *NewImage;
85
86 NewImage = egCreateImage(Width, Height, HasAlpha);
87 if (NewImage == NULL)
88 return NULL;
89
90 egFillImage(NewImage, Color);
91 return NewImage;
92 }
93
94 EG_IMAGE * egCopyImage(IN EG_IMAGE *Image)
95 {
96 EG_IMAGE *NewImage = NULL;
97
98 if (Image != NULL)
99 NewImage = egCreateImage(Image->Width, Image->Height, Image->HasAlpha);
100 if (NewImage == NULL)
101 return NULL;
102
103 CopyMem(NewImage->PixelData, Image->PixelData, Image->Width * Image->Height * sizeof(EG_PIXEL));
104 return NewImage;
105 }
106
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;
111 UINTN x, y;
112
113 if (((StartX + Width) > Image->Width) || ((StartY + Height) > Image->Height))
114 return NULL;
115
116 NewImage = egCreateImage(Width, Height, Image->HasAlpha);
117 if (NewImage == NULL)
118 return NULL;
119
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];
123 }
124 }
125 return NewImage;
126 } // EG_IMAGE * egCropImage()
127
128 // The following function implements a bilinear image scaling algorithm, based on
129 // code presented at http://tech-algorithm.com/articles/bilinear-image-scaling/.
130 // Resize an image; returns pointer to resized image if successful, NULL otherwise.
131 // Calling function is responsible for freeing allocated memory.
132 EG_IMAGE * egScaleImage(IN EG_IMAGE *Image, IN UINTN NewWidth, IN UINTN NewHeight) {
133 EG_IMAGE *NewImage = NULL;
134 EG_PIXEL a, b, c, d;
135 UINTN x, y, Index ;
136 UINTN i, j;
137 UINTN Offset = 0;
138 float x_ratio, y_ratio, x_diff, y_diff;
139
140 if ((Image == NULL) || (Image->Height == 0) || (Image->Width == 0) || (NewWidth == 0) || (NewHeight == 0))
141 return NULL;
142
143 if ((Image->Width == NewWidth) && (Image->Height == NewHeight))
144 return (egCopyImage(Image));
145
146 NewImage = egCreateImage(NewWidth, NewHeight, Image->HasAlpha);
147 if (NewImage == NULL)
148 return NULL;
149
150 x_ratio = ((float)(Image->Width - 1)) / NewWidth;
151 y_ratio = ((float)(Image->Height - 1)) / NewHeight;
152
153 for (i = 0; i < NewHeight; i++) {
154 for (j = 0; j < NewWidth; j++) {
155 x = (UINTN)(x_ratio * j);
156 y = (UINTN)(y_ratio * i);
157 x_diff = (x_ratio * j) - x;
158 y_diff = (y_ratio * i) - y;
159 Index = ((y * Image->Width) + x);
160 a = Image->PixelData[Index];
161 b = Image->PixelData[Index + 1];
162 c = Image->PixelData[Index + Image->Width];
163 d = Image->PixelData[Index + Image->Width + 1];
164
165 // blue element
166 // Yb = Ab(1-Image->Width)(1-Image->Height) + Bb(Image->Width)(1-Image->Height) + Cb(Image->Height)(1-Image->Width) + Db(wh)
167 NewImage->PixelData[Offset].b = (a.b)*(1-x_diff)*(1-y_diff) + (b.b)*(x_diff)*(1-y_diff) +
168 (c.b)*(y_diff)*(1-x_diff) + (d.b)*(x_diff*y_diff);
169
170 // green element
171 // Yg = Ag(1-Image->Width)(1-Image->Height) + Bg(Image->Width)(1-Image->Height) + Cg(Image->Height)(1-Image->Width) + Dg(wh)
172 NewImage->PixelData[Offset].g = (a.g)*(1-x_diff)*(1-y_diff) + (b.g)*(x_diff)*(1-y_diff) +
173 (c.g)*(y_diff)*(1-x_diff) + (d.g)*(x_diff*y_diff);
174
175 // red element
176 // Yr = Ar(1-Image->Width)(1-Image->Height) + Br(Image->Width)(1-Image->Height) + Cr(Image->Height)(1-Image->Width) + Dr(wh)
177 NewImage->PixelData[Offset].r = (a.r)*(1-x_diff)*(1-y_diff) + (b.r)*(x_diff)*(1-y_diff) +
178 (c.r)*(y_diff)*(1-x_diff) + (d.r)*(x_diff*y_diff);
179
180 // alpha element
181 NewImage->PixelData[Offset++].a = (a.a)*(1-x_diff)*(1-y_diff) + (b.a)*(x_diff)*(1-y_diff) +
182 (c.a)*(y_diff)*(1-x_diff) + (d.a)*(x_diff*y_diff);
183
184 } // for (j...)
185 } // for (i...)
186 return NewImage;
187 } // EG_IMAGE * egScaleImage()
188
189 VOID egFreeImage(IN EG_IMAGE *Image)
190 {
191 if (Image != NULL) {
192 if (Image->PixelData != NULL)
193 FreePool(Image->PixelData);
194 FreePool(Image);
195 }
196 }
197
198 //
199 // Basic file operations
200 //
201
202 EFI_STATUS egLoadFile(IN EFI_FILE *BaseDir, IN CHAR16 *FileName, OUT UINT8 **FileData, OUT UINTN *FileDataLength)
203 {
204 EFI_STATUS Status;
205 EFI_FILE_HANDLE FileHandle;
206 EFI_FILE_INFO *FileInfo;
207 UINT64 ReadSize;
208 UINTN BufferSize;
209 UINT8 *Buffer;
210
211 if ((BaseDir == NULL) || (FileName == NULL))
212 return EFI_NOT_FOUND;
213
214 Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0);
215 if (EFI_ERROR(Status)) {
216 return Status;
217 }
218
219 FileInfo = LibFileInfo(FileHandle);
220 if (FileInfo == NULL) {
221 refit_call1_wrapper(FileHandle->Close, FileHandle);
222 return EFI_NOT_FOUND;
223 }
224 ReadSize = FileInfo->FileSize;
225 if (ReadSize > MAX_FILE_SIZE)
226 ReadSize = MAX_FILE_SIZE;
227 FreePool(FileInfo);
228
229 BufferSize = (UINTN)ReadSize; // was limited to 1 GB above, so this is safe
230 Buffer = (UINT8 *) AllocatePool(BufferSize);
231 if (Buffer == NULL) {
232 refit_call1_wrapper(FileHandle->Close, FileHandle);
233 return EFI_OUT_OF_RESOURCES;
234 }
235
236 Status = refit_call3_wrapper(FileHandle->Read, FileHandle, &BufferSize, Buffer);
237 refit_call1_wrapper(FileHandle->Close, FileHandle);
238 if (EFI_ERROR(Status)) {
239 FreePool(Buffer);
240 return Status;
241 }
242
243 *FileData = Buffer;
244 *FileDataLength = BufferSize;
245 return EFI_SUCCESS;
246 }
247
248 static EFI_GUID ESPGuid = { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
249
250 EFI_STATUS egFindESP(OUT EFI_FILE_HANDLE *RootDir)
251 {
252 EFI_STATUS Status;
253 UINTN HandleCount = 0;
254 EFI_HANDLE *Handles;
255
256 Status = LibLocateHandle(ByProtocol, &ESPGuid, NULL, &HandleCount, &Handles);
257 if (!EFI_ERROR(Status) && HandleCount > 0) {
258 *RootDir = LibOpenRoot(Handles[0]);
259 if (*RootDir == NULL)
260 Status = EFI_NOT_FOUND;
261 FreePool(Handles);
262 }
263 return Status;
264 }
265
266 EFI_STATUS egSaveFile(IN EFI_FILE* BaseDir OPTIONAL, IN CHAR16 *FileName,
267 IN UINT8 *FileData, IN UINTN FileDataLength)
268 {
269 EFI_STATUS Status;
270 EFI_FILE_HANDLE FileHandle;
271 UINTN BufferSize;
272
273 if (BaseDir == NULL) {
274 Status = egFindESP(&BaseDir);
275 if (EFI_ERROR(Status))
276 return Status;
277 }
278
279 Status = refit_call5_wrapper(BaseDir->Open, BaseDir, &FileHandle, FileName,
280 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
281 if (EFI_ERROR(Status))
282 return Status;
283
284 BufferSize = FileDataLength;
285 Status = refit_call3_wrapper(FileHandle->Write, FileHandle, &BufferSize, FileData);
286 refit_call1_wrapper(FileHandle->Close, FileHandle);
287
288 return Status;
289 }
290
291 //
292 // Loading images from files and embedded data
293 //
294
295 // Decode the specified image data. The IconSize parameter is relevant only
296 // for ICNS, for which it selects which ICNS sub-image is decoded.
297 // Returns a pointer to the resulting EG_IMAGE or NULL if decoding failed.
298 static EG_IMAGE * egDecodeAny(IN UINT8 *FileData, IN UINTN FileDataLength, IN UINTN IconSize, IN BOOLEAN WantAlpha)
299 {
300 EG_IMAGE *NewImage = NULL;
301
302 NewImage = egDecodeICNS(FileData, FileDataLength, IconSize, WantAlpha);
303 if (NewImage == NULL)
304 NewImage = egDecodePNG(FileData, FileDataLength, IconSize, WantAlpha);
305 if (NewImage == NULL)
306 NewImage = egDecodeBMP(FileData, FileDataLength, IconSize, WantAlpha);
307
308 return NewImage;
309 }
310
311 EG_IMAGE * egLoadImage(IN EFI_FILE* BaseDir, IN CHAR16 *FileName, IN BOOLEAN WantAlpha)
312 {
313 EFI_STATUS Status;
314 UINT8 *FileData;
315 UINTN FileDataLength;
316 EG_IMAGE *NewImage;
317
318 if (BaseDir == NULL || FileName == NULL)
319 return NULL;
320
321 // load file
322 Status = egLoadFile(BaseDir, FileName, &FileData, &FileDataLength);
323 if (EFI_ERROR(Status))
324 return NULL;
325
326 // decode it
327 NewImage = egDecodeAny(FileData, FileDataLength, 128 /* arbitrary value */, WantAlpha);
328 FreePool(FileData);
329
330 return NewImage;
331 }
332
333 // Load an icon from (BaseDir)/Path, extracting the icon of size IconSize x IconSize.
334 // Returns a pointer to the image data, or NULL if the icon could not be loaded.
335 EG_IMAGE * egLoadIcon(IN EFI_FILE* BaseDir, IN CHAR16 *Path, IN UINTN IconSize)
336 {
337 EFI_STATUS Status;
338 UINT8 *FileData;
339 UINTN FileDataLength;
340 EG_IMAGE *Image, *NewImage;
341
342 if (BaseDir == NULL || Path == NULL)
343 return NULL;
344
345 // load file
346 Status = egLoadFile(BaseDir, Path, &FileData, &FileDataLength);
347 if (EFI_ERROR(Status))
348 return NULL;
349
350 // decode it
351 Image = egDecodeAny(FileData, FileDataLength, IconSize, TRUE);
352 FreePool(FileData);
353 if ((Image->Width != IconSize) || (Image->Height != IconSize)) {
354 NewImage = egScaleImage(Image, IconSize, IconSize);
355 if (!NewImage)
356 Print(L"Warning: Unable to scale icon of the wrong size from '%s'\n", Path);
357 egFreeImage(Image);
358 Image = NewImage;
359 }
360
361 return Image;
362 } // EG_IMAGE *egLoadIcon()
363
364 // Returns an icon of any type from the specified subdirectory using the specified
365 // base name. All directory references are relative to BaseDir. For instance, if
366 // SubdirName is "myicons" and BaseName is "os_linux", this function will return
367 // an image based on "myicons/os_linux.icns" or "myicons/os_linux.png", in that
368 // order of preference. Returns NULL if no such file is a valid icon file.
369 EG_IMAGE * egLoadIconAnyType(IN EFI_FILE *BaseDir, IN CHAR16 *SubdirName, IN CHAR16 *BaseName, IN UINTN IconSize) {
370 EG_IMAGE *Image = NULL;
371 CHAR16 *Extension;
372 CHAR16 FileName[256];
373 UINTN i = 0;
374
375 while (((Extension = FindCommaDelimited(ICON_EXTENSIONS, i++)) != NULL) && (Image == NULL)) {
376 SPrint(FileName, 255, L"%s\\%s.%s", SubdirName, BaseName, Extension);
377 Image = egLoadIcon(BaseDir, FileName, IconSize);
378 MyFreePool(Extension);
379 } // while()
380
381 return Image;
382 } // EG_IMAGE *egLoadIconAnyType()
383
384 // Returns an icon with any extension in ICON_EXTENSIONS from either the directory
385 // specified by GlobalConfig.IconsDir or DEFAULT_ICONS_DIR. The input BaseName
386 // should be the icon name without an extension. For instance, if BaseName is
387 // os_linux, GlobalConfig.IconsDir is myicons, DEFAULT_ICONS_DIR is icons, and
388 // ICON_EXTENSIONS is "icns,png", this function will return myicons/os_linux.icns,
389 // myicons/os_linux.png, icons/os_linux.icns, or icons/os_linux.png, in that
390 // order of preference. Returns NULL if no such icon can be found. All file
391 // references are relative to SelfDir.
392 EG_IMAGE * egFindIcon(IN CHAR16 *BaseName, IN UINTN IconSize) {
393 EG_IMAGE *Image = NULL;
394
395 if (GlobalConfig.IconsDir != NULL) {
396 Image = egLoadIconAnyType(SelfDir, GlobalConfig.IconsDir, BaseName, IconSize);
397 }
398
399 if (Image == NULL) {
400 Image = egLoadIconAnyType(SelfDir, DEFAULT_ICONS_DIR, BaseName, IconSize);
401 }
402
403 return Image;
404 } // EG_IMAGE * egFindIcon()
405
406 EG_IMAGE * egPrepareEmbeddedImage(IN EG_EMBEDDED_IMAGE *EmbeddedImage, IN BOOLEAN WantAlpha)
407 {
408 EG_IMAGE *NewImage;
409 UINT8 *CompData;
410 UINTN CompLen;
411 UINTN PixelCount;
412
413 // sanity check
414 if (EmbeddedImage->PixelMode > EG_MAX_EIPIXELMODE ||
415 (EmbeddedImage->CompressMode != EG_EICOMPMODE_NONE && EmbeddedImage->CompressMode != EG_EICOMPMODE_RLE))
416 return NULL;
417
418 // allocate image structure and pixel buffer
419 NewImage = egCreateImage(EmbeddedImage->Width, EmbeddedImage->Height, WantAlpha);
420 if (NewImage == NULL)
421 return NULL;
422
423 CompData = (UINT8 *)EmbeddedImage->Data; // drop const
424 CompLen = EmbeddedImage->DataLength;
425 PixelCount = EmbeddedImage->Width * EmbeddedImage->Height;
426
427 // FUTURE: for EG_EICOMPMODE_EFICOMPRESS, decompress whole data block here
428
429 if (EmbeddedImage->PixelMode == EG_EIPIXELMODE_GRAY ||
430 EmbeddedImage->PixelMode == EG_EIPIXELMODE_GRAY_ALPHA) {
431
432 // copy grayscale plane and expand
433 if (EmbeddedImage->CompressMode == EG_EICOMPMODE_RLE) {
434 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, r), PixelCount);
435 } else {
436 egInsertPlane(CompData, PLPTR(NewImage, r), PixelCount);
437 CompData += PixelCount;
438 }
439 egCopyPlane(PLPTR(NewImage, r), PLPTR(NewImage, g), PixelCount);
440 egCopyPlane(PLPTR(NewImage, r), PLPTR(NewImage, b), PixelCount);
441
442 } else if (EmbeddedImage->PixelMode == EG_EIPIXELMODE_COLOR ||
443 EmbeddedImage->PixelMode == EG_EIPIXELMODE_COLOR_ALPHA) {
444
445 // copy color planes
446 if (EmbeddedImage->CompressMode == EG_EICOMPMODE_RLE) {
447 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, r), PixelCount);
448 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, g), PixelCount);
449 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, b), PixelCount);
450 } else {
451 egInsertPlane(CompData, PLPTR(NewImage, r), PixelCount);
452 CompData += PixelCount;
453 egInsertPlane(CompData, PLPTR(NewImage, g), PixelCount);
454 CompData += PixelCount;
455 egInsertPlane(CompData, PLPTR(NewImage, b), PixelCount);
456 CompData += PixelCount;
457 }
458
459 } else {
460
461 // set color planes to black
462 egSetPlane(PLPTR(NewImage, r), 0, PixelCount);
463 egSetPlane(PLPTR(NewImage, g), 0, PixelCount);
464 egSetPlane(PLPTR(NewImage, b), 0, PixelCount);
465
466 }
467
468 if (WantAlpha && (EmbeddedImage->PixelMode == EG_EIPIXELMODE_GRAY_ALPHA ||
469 EmbeddedImage->PixelMode == EG_EIPIXELMODE_COLOR_ALPHA ||
470 EmbeddedImage->PixelMode == EG_EIPIXELMODE_ALPHA)) {
471
472 // copy alpha plane
473 if (EmbeddedImage->CompressMode == EG_EICOMPMODE_RLE) {
474 egDecompressIcnsRLE(&CompData, &CompLen, PLPTR(NewImage, a), PixelCount);
475 } else {
476 egInsertPlane(CompData, PLPTR(NewImage, a), PixelCount);
477 CompData += PixelCount;
478 }
479
480 } else {
481 egSetPlane(PLPTR(NewImage, a), WantAlpha ? 255 : 0, PixelCount);
482 }
483
484 return NewImage;
485 }
486
487 //
488 // Compositing
489 //
490
491 VOID egRestrictImageArea(IN EG_IMAGE *Image,
492 IN UINTN AreaPosX, IN UINTN AreaPosY,
493 IN OUT UINTN *AreaWidth, IN OUT UINTN *AreaHeight)
494 {
495 if (AreaPosX >= Image->Width || AreaPosY >= Image->Height) {
496 // out of bounds, operation has no effect
497 *AreaWidth = 0;
498 *AreaHeight = 0;
499 } else {
500 // calculate affected area
501 if (*AreaWidth > Image->Width - AreaPosX)
502 *AreaWidth = Image->Width - AreaPosX;
503 if (*AreaHeight > Image->Height - AreaPosY)
504 *AreaHeight = Image->Height - AreaPosY;
505 }
506 }
507
508 VOID egFillImage(IN OUT EG_IMAGE *CompImage, IN EG_PIXEL *Color)
509 {
510 UINTN i;
511 EG_PIXEL FillColor;
512 EG_PIXEL *PixelPtr;
513
514 FillColor = *Color;
515 if (!CompImage->HasAlpha)
516 FillColor.a = 0;
517
518 PixelPtr = CompImage->PixelData;
519 for (i = 0; i < CompImage->Width * CompImage->Height; i++, PixelPtr++)
520 *PixelPtr = FillColor;
521 }
522
523 VOID egFillImageArea(IN OUT EG_IMAGE *CompImage,
524 IN UINTN AreaPosX, IN UINTN AreaPosY,
525 IN UINTN AreaWidth, IN UINTN AreaHeight,
526 IN EG_PIXEL *Color)
527 {
528 UINTN x, y;
529 EG_PIXEL FillColor;
530 EG_PIXEL *PixelPtr;
531 EG_PIXEL *PixelBasePtr;
532
533 egRestrictImageArea(CompImage, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
534
535 if (AreaWidth > 0) {
536 FillColor = *Color;
537 if (!CompImage->HasAlpha)
538 FillColor.a = 0;
539
540 PixelBasePtr = CompImage->PixelData + AreaPosY * CompImage->Width + AreaPosX;
541 for (y = 0; y < AreaHeight; y++) {
542 PixelPtr = PixelBasePtr;
543 for (x = 0; x < AreaWidth; x++, PixelPtr++)
544 *PixelPtr = FillColor;
545 PixelBasePtr += CompImage->Width;
546 }
547 }
548 }
549
550 VOID egRawCopy(IN OUT EG_PIXEL *CompBasePtr, IN EG_PIXEL *TopBasePtr,
551 IN UINTN Width, IN UINTN Height,
552 IN UINTN CompLineOffset, IN UINTN TopLineOffset)
553 {
554 UINTN x, y;
555 EG_PIXEL *TopPtr, *CompPtr;
556
557 for (y = 0; y < Height; y++) {
558 TopPtr = TopBasePtr;
559 CompPtr = CompBasePtr;
560 for (x = 0; x < Width; x++) {
561 *CompPtr = *TopPtr;
562 TopPtr++, CompPtr++;
563 }
564 TopBasePtr += TopLineOffset;
565 CompBasePtr += CompLineOffset;
566 }
567 }
568
569 VOID egRawCompose(IN OUT EG_PIXEL *CompBasePtr, IN EG_PIXEL *TopBasePtr,
570 IN UINTN Width, IN UINTN Height,
571 IN UINTN CompLineOffset, IN UINTN TopLineOffset)
572 {
573 UINTN x, y;
574 EG_PIXEL *TopPtr, *CompPtr;
575 UINTN Alpha;
576 UINTN RevAlpha;
577 UINTN Temp;
578
579 for (y = 0; y < Height; y++) {
580 TopPtr = TopBasePtr;
581 CompPtr = CompBasePtr;
582 for (x = 0; x < Width; x++) {
583 Alpha = TopPtr->a;
584 RevAlpha = 255 - Alpha;
585 Temp = (UINTN)CompPtr->b * RevAlpha + (UINTN)TopPtr->b * Alpha + 0x80;
586 CompPtr->b = (Temp + (Temp >> 8)) >> 8;
587 Temp = (UINTN)CompPtr->g * RevAlpha + (UINTN)TopPtr->g * Alpha + 0x80;
588 CompPtr->g = (Temp + (Temp >> 8)) >> 8;
589 Temp = (UINTN)CompPtr->r * RevAlpha + (UINTN)TopPtr->r * Alpha + 0x80;
590 CompPtr->r = (Temp + (Temp >> 8)) >> 8;
591 /*
592 CompPtr->b = ((UINTN)CompPtr->b * RevAlpha + (UINTN)TopPtr->b * Alpha) / 255;
593 CompPtr->g = ((UINTN)CompPtr->g * RevAlpha + (UINTN)TopPtr->g * Alpha) / 255;
594 CompPtr->r = ((UINTN)CompPtr->r * RevAlpha + (UINTN)TopPtr->r * Alpha) / 255;
595 */
596 TopPtr++, CompPtr++;
597 }
598 TopBasePtr += TopLineOffset;
599 CompBasePtr += CompLineOffset;
600 }
601 }
602
603 VOID egComposeImage(IN OUT EG_IMAGE *CompImage, IN EG_IMAGE *TopImage, IN UINTN PosX, IN UINTN PosY)
604 {
605 UINTN CompWidth, CompHeight;
606
607 CompWidth = TopImage->Width;
608 CompHeight = TopImage->Height;
609 egRestrictImageArea(CompImage, PosX, PosY, &CompWidth, &CompHeight);
610
611 // compose
612 if (CompWidth > 0) {
613 if (TopImage->HasAlpha) {
614 egRawCompose(CompImage->PixelData + PosY * CompImage->Width + PosX, TopImage->PixelData,
615 CompWidth, CompHeight, CompImage->Width, TopImage->Width);
616 } else {
617 egRawCopy(CompImage->PixelData + PosY * CompImage->Width + PosX, TopImage->PixelData,
618 CompWidth, CompHeight, CompImage->Width, TopImage->Width);
619 }
620 }
621 } /* VOID egComposeImage() */
622
623 //
624 // misc internal functions
625 //
626
627 VOID egInsertPlane(IN UINT8 *SrcDataPtr, IN UINT8 *DestPlanePtr, IN UINTN PixelCount)
628 {
629 UINTN i;
630
631 for (i = 0; i < PixelCount; i++) {
632 *DestPlanePtr = *SrcDataPtr++;
633 DestPlanePtr += 4;
634 }
635 }
636
637 VOID egSetPlane(IN UINT8 *DestPlanePtr, IN UINT8 Value, IN UINTN PixelCount)
638 {
639 UINTN i;
640
641 for (i = 0; i < PixelCount; i++) {
642 *DestPlanePtr = Value;
643 DestPlanePtr += 4;
644 }
645 }
646
647 VOID egCopyPlane(IN UINT8 *SrcPlanePtr, IN UINT8 *DestPlanePtr, IN UINTN PixelCount)
648 {
649 UINTN i;
650
651 for (i = 0; i < PixelCount; i++) {
652 *DestPlanePtr = *SrcPlanePtr;
653 DestPlanePtr += 4, SrcPlanePtr += 4;
654 }
655 }
656
657 /* EOF */