1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcodec/gif/gif_decoder.h"
8 
9 #include "core/fxcodec/cfx_codec_memory.h"
10 #include "core/fxcodec/fx_codec.h"
11 #include "core/fxcodec/gif/cfx_gifcontext.h"
12 #include "core/fxge/dib/fx_dib.h"
13 
14 namespace fxcodec {
15 
16 // static
StartDecode(Delegate * pDelegate)17 std::unique_ptr<ProgressiveDecoderIface::Context> GifDecoder::StartDecode(
18     Delegate* pDelegate) {
19   return std::make_unique<CFX_GifContext>(pDelegate);
20 }
21 
22 // static
ReadHeader(ProgressiveDecoderIface::Context * pContext,int * width,int * height,int * pal_num,CFX_GifPalette ** pal_pp,int * bg_index)23 CFX_GifDecodeStatus GifDecoder::ReadHeader(
24     ProgressiveDecoderIface::Context* pContext,
25     int* width,
26     int* height,
27     int* pal_num,
28     CFX_GifPalette** pal_pp,
29     int* bg_index) {
30   auto* context = static_cast<CFX_GifContext*>(pContext);
31   CFX_GifDecodeStatus ret = context->ReadHeader();
32   if (ret != CFX_GifDecodeStatus::Success)
33     return ret;
34 
35   *width = context->width_;
36   *height = context->height_;
37   *pal_num = (2 << context->global_palette_exp_);
38   *pal_pp = context->global_palette_.empty() ? nullptr
39                                              : context->global_palette_.data();
40   *bg_index = context->bc_index_;
41   return CFX_GifDecodeStatus::Success;
42 }
43 
44 // static
LoadFrameInfo(ProgressiveDecoderIface::Context * pContext)45 std::pair<CFX_GifDecodeStatus, size_t> GifDecoder::LoadFrameInfo(
46     ProgressiveDecoderIface::Context* pContext) {
47   auto* context = static_cast<CFX_GifContext*>(pContext);
48   CFX_GifDecodeStatus ret = context->GetFrame();
49   if (ret != CFX_GifDecodeStatus::Success)
50     return {ret, 0};
51   return {CFX_GifDecodeStatus::Success, context->GetFrameNum()};
52 }
53 
54 // static
LoadFrame(ProgressiveDecoderIface::Context * pContext,size_t frame_num)55 CFX_GifDecodeStatus GifDecoder::LoadFrame(
56     ProgressiveDecoderIface::Context* pContext,
57     size_t frame_num) {
58   return static_cast<CFX_GifContext*>(pContext)->LoadFrame(frame_num);
59 }
60 
61 // static
GetAvailInput(ProgressiveDecoderIface::Context * pContext)62 FX_FILESIZE GifDecoder::GetAvailInput(
63     ProgressiveDecoderIface::Context* pContext) {
64   return static_cast<CFX_GifContext*>(pContext)->GetAvailInput();
65 }
66 
67 // static
Input(ProgressiveDecoderIface::Context * pContext,RetainPtr<CFX_CodecMemory> codec_memory,CFX_DIBAttribute *)68 bool GifDecoder::Input(ProgressiveDecoderIface::Context* pContext,
69                        RetainPtr<CFX_CodecMemory> codec_memory,
70                        CFX_DIBAttribute*) {
71   auto* ctx = static_cast<CFX_GifContext*>(pContext);
72   ctx->SetInputBuffer(std::move(codec_memory));
73   return true;
74 }
75 
76 }  // namespace fxcodec
77