Home | History | Annotate | Download | only in ImageDecoderLib
      1 /** @file
      2   This library provides image decoding service by managing the different
      3   image decoding libraries.
      4 
      5 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
      6 This program and the accompanying materials are licensed and made available under
      7 the terms and conditions of the BSD License that accompanies this distribution.
      8 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 #include <Uefi.h>
     17 #include <Protocol/GraphicsOutput.h>
     18 #include <Library/ImageDecoderLib.h>
     19 #include <Library/BaseLib.h>
     20 #include <Library/MemoryAllocationLib.h>
     21 #include <Library/DebugLib.h>
     22 
     23 typedef struct {
     24   UINT32       Signature;
     25   DECODE_IMAGE Decoder;
     26   LIST_ENTRY   Link;
     27 } IMAGE_DECODER_ENTRY;
     28 #define IMAGE_DECODER_ENTRY_SIGNATURE  SIGNATURE_32 ('i', 'm', 'g', 'd')
     29 #define IMAGE_DECODER_ENTRY_FROM_LINK(Link) \
     30         CR (Link, IMAGE_DECODER_ENTRY, Link, IMAGE_DECODER_ENTRY_SIGNATURE)
     31 
     32 LIST_ENTRY mImageDecoderLibDecoders = INITIALIZE_LIST_HEAD_VARIABLE (mImageDecoderLibDecoders);
     33 
     34 /**
     35   Convert a graphics image to a callee allocated GOP blt buffer.
     36 
     37   @param  ImageFormat   Format of the image file.
     38   @param  Image         Pointer to image file.
     39   @param  ImageSize     Number of bytes in Image.
     40   @param  GopBlt        Buffer containing GOP version of Image.
     41   @param  GopBltSize    Size of GopBlt in bytes.
     42   @param  PixelWidth    Width of GopBlt/Image in pixels.
     43   @param  PixelHeight   Height of GopBlt/Image in pixels.
     44 
     45   @retval EFI_SUCCESS           GopBlt and GopBltSize are returned.
     46   @retval EFI_INVALID_PARAMETER GopBlt or GopBltSize is NULL.
     47   @retval EFI_INVALID_PARAMETER Image is NULL or ImageSize is 0.
     48   @retval EFI_UNSUPPORTED       Image is not supported.
     49   @retval EFI_OUT_OF_RESOURCES  No enough buffer to allocate.
     50 
     51 **/
     52 EFI_STATUS
     53 EFIAPI
     54 DecodeImage (
     55   IN  IMAGE_FORMAT                  ImageFormat,
     56   IN  UINT8                         *Image,
     57   IN  UINTN                         ImageSize,
     58   OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL **GopBlt,
     59   OUT UINTN                         *GopBltSize,
     60   OUT UINTN                         *PixelWidth,
     61   OUT UINTN                         *PixelHeight
     62   )
     63 {
     64   IMAGE_DECODER_ENTRY               *Entry;
     65   LIST_ENTRY                        *Link;
     66   EFI_STATUS                        Status;
     67 
     68   if ((GopBlt == NULL) || (GopBltSize == NULL)) {
     69     return EFI_INVALID_PARAMETER;
     70   }
     71 
     72   if ((Image == NULL) || (ImageSize == 0)) {
     73     return EFI_INVALID_PARAMETER;
     74   }
     75 
     76   for ( Link = GetFirstNode (&mImageDecoderLibDecoders)
     77       ; !IsNull (&mImageDecoderLibDecoders, Link)
     78       ; Link = GetNextNode (&mImageDecoderLibDecoders, Link)
     79       ) {
     80     Entry = IMAGE_DECODER_ENTRY_FROM_LINK (Link);
     81     Status = Entry->Decoder (ImageFormat, Image, ImageSize, GopBlt, GopBltSize, PixelWidth, PixelHeight);
     82     if (!EFI_ERROR (Status)) {
     83       break;
     84     }
     85   }
     86 
     87   if (IsNull (&mImageDecoderLibDecoders, Link)) {
     88     return EFI_UNSUPPORTED;
     89   } else {
     90     return EFI_SUCCESS;
     91   }
     92 }
     93 
     94 /**
     95   Register an image decoder.
     96 
     97   @param Decoder  An image decoder.
     98 
     99   @retval EFI_SUCCESS          The decoder was successfully registered.
    100   @retval EFI_OUT_OF_RESOURCES No enough resource to register the decoder.
    101 
    102 **/
    103 EFI_STATUS
    104 EFIAPI
    105 RegisterImageDecoder (
    106   IN  DECODE_IMAGE     Decoder
    107   )
    108 {
    109   IMAGE_DECODER_ENTRY  *Entry;
    110 
    111   Entry = AllocatePool (sizeof (IMAGE_DECODER_ENTRY));
    112   if (Entry == NULL) {
    113     return EFI_OUT_OF_RESOURCES;
    114   }
    115 
    116   Entry->Signature = IMAGE_DECODER_ENTRY_SIGNATURE;
    117   Entry->Decoder   = Decoder;
    118   InsertTailList (&mImageDecoderLibDecoders, &Entry->Link);
    119 
    120   return EFI_SUCCESS;
    121 }