summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Library/ImageDecoderLib/ImageDecoderLib.c
blob: 4a6219bbb138389da7d96048f9c973dfb3ed362f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/** @file
  This library provides image decoding service by managing the different
  image decoding libraries.

Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Uefi.h>
#include <Protocol/GraphicsOutput.h>
#include <Library/ImageDecoderLib.h>
#include <Library/BaseLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>

typedef struct {
  UINT32       Signature;
  DECODE_IMAGE Decoder;
  LIST_ENTRY   Link;
} IMAGE_DECODER_ENTRY;
#define IMAGE_DECODER_ENTRY_SIGNATURE  SIGNATURE_32 ('i', 'm', 'g', 'd')
#define IMAGE_DECODER_ENTRY_FROM_LINK(Link) \
        CR (Link, IMAGE_DECODER_ENTRY, Link, IMAGE_DECODER_ENTRY_SIGNATURE)

LIST_ENTRY mImageDecoderLibDecoders = INITIALIZE_LIST_HEAD_VARIABLE (mImageDecoderLibDecoders);

/**
  Convert a graphics image to a callee allocated GOP blt buffer.

  @param  ImageFormat   Format of the image file.
  @param  Image         Pointer to image file.
  @param  ImageSize     Number of bytes in Image.
  @param  GopBlt        Buffer containing GOP version of Image.
  @param  GopBltSize    Size of GopBlt in bytes.
  @param  PixelWidth    Width of GopBlt/Image in pixels.
  @param  PixelHeight   Height of GopBlt/Image in pixels.

  @retval EFI_SUCCESS           GopBlt and GopBltSize are returned.
  @retval EFI_INVALID_PARAMETER GopBlt or GopBltSize is NULL.
  @retval EFI_INVALID_PARAMETER Image is NULL or ImageSize is 0.
  @retval EFI_UNSUPPORTED       Image is not supported.
  @retval EFI_OUT_OF_RESOURCES  No enough buffer to allocate.

**/
EFI_STATUS
EFIAPI
DecodeImage (
  IN  IMAGE_FORMAT                  ImageFormat,
  IN  UINT8                         *Image,
  IN  UINTN                         ImageSize,
  OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL **GopBlt,
  OUT UINTN                         *GopBltSize,
  OUT UINTN                         *PixelWidth,
  OUT UINTN                         *PixelHeight
  )
{
  IMAGE_DECODER_ENTRY               *Entry;
  LIST_ENTRY                        *Link;
  EFI_STATUS                        Status;

  if ((GopBlt == NULL) || (GopBltSize == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  if ((Image == NULL) || (ImageSize == 0)) {
    return EFI_INVALID_PARAMETER;
  }

  for ( Link = GetFirstNode (&mImageDecoderLibDecoders)
      ; !IsNull (&mImageDecoderLibDecoders, Link)
      ; Link = GetNextNode (&mImageDecoderLibDecoders, Link)
      ) {
    Entry = IMAGE_DECODER_ENTRY_FROM_LINK (Link);
    Status = Entry->Decoder (ImageFormat, Image, ImageSize, GopBlt, GopBltSize, PixelWidth, PixelHeight);
    if (!EFI_ERROR (Status)) {
      break;
    }
  }

  if (IsNull (&mImageDecoderLibDecoders, Link)) {
    return EFI_UNSUPPORTED;
  } else {
    return EFI_SUCCESS;
  }
}

/**
  Register an image decoder.

  @param Decoder  An image decoder.

  @retval EFI_SUCCESS          The decoder was successfully registered.
  @retval EFI_OUT_OF_RESOURCES No enough resource to register the decoder.

**/
EFI_STATUS
EFIAPI
RegisterImageDecoder (
  IN  DECODE_IMAGE     Decoder
  )
{
  IMAGE_DECODER_ENTRY  *Entry;

  Entry = AllocatePool (sizeof (IMAGE_DECODER_ENTRY));
  if (Entry == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Entry->Signature = IMAGE_DECODER_ENTRY_SIGNATURE;
  Entry->Decoder   = Decoder;
  InsertTailList (&mImageDecoderLibDecoders, &Entry->Link);

  return EFI_SUCCESS;
}