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/gifmodule.h"
8 
9 #include "core/fxcodec/cfx_codec_memory.h"
10 #include "core/fxcodec/fx_codec.h"
11 #include "core/fxcodec/gif/cfx_gif.h"
12 #include "core/fxcodec/gif/cfx_gifcontext.h"
13 #include "core/fxge/fx_dib.h"
14 #include "third_party/base/ptr_util.h"
15 
16 namespace fxcodec {
17 
18 GifModule::GifModule() = default;
19 
20 GifModule::~GifModule() = default;
21 
Start(Delegate * pDelegate)22 std::unique_ptr<ModuleIface::Context> GifModule::Start(Delegate* pDelegate) {
23   return pdfium::MakeUnique<CFX_GifContext>(this, pDelegate);
24 }
25 
ReadHeader(Context * pContext,int * width,int * height,int * pal_num,CFX_GifPalette ** pal_pp,int * bg_index)26 CFX_GifDecodeStatus GifModule::ReadHeader(Context* pContext,
27                                           int* width,
28                                           int* height,
29                                           int* pal_num,
30                                           CFX_GifPalette** pal_pp,
31                                           int* bg_index) {
32   auto* context = static_cast<CFX_GifContext*>(pContext);
33   CFX_GifDecodeStatus ret = context->ReadHeader();
34   if (ret != CFX_GifDecodeStatus::Success)
35     return ret;
36 
37   *width = context->width_;
38   *height = context->height_;
39   *pal_num = (2 << context->global_pal_exp_);
40   *pal_pp = context->global_palette_.empty() ? nullptr
41                                              : context->global_palette_.data();
42   *bg_index = context->bc_index_;
43   return CFX_GifDecodeStatus::Success;
44 }
45 
LoadFrameInfo(Context * pContext)46 std::pair<CFX_GifDecodeStatus, size_t> GifModule::LoadFrameInfo(
47     Context* pContext) {
48   auto* context = static_cast<CFX_GifContext*>(pContext);
49   CFX_GifDecodeStatus ret = context->GetFrame();
50   if (ret != CFX_GifDecodeStatus::Success)
51     return {ret, 0};
52   return {CFX_GifDecodeStatus::Success, context->GetFrameNum()};
53 }
54 
LoadFrame(Context * pContext,size_t frame_num)55 CFX_GifDecodeStatus GifModule::LoadFrame(Context* pContext, size_t frame_num) {
56   return static_cast<CFX_GifContext*>(pContext)->LoadFrame(frame_num);
57 }
58 
GetAvailInput(Context * pContext) const59 FX_FILESIZE GifModule::GetAvailInput(Context* pContext) const {
60   return static_cast<CFX_GifContext*>(pContext)->GetAvailInput();
61 }
62 
Input(Context * pContext,RetainPtr<CFX_CodecMemory> codec_memory,CFX_DIBAttribute *)63 bool GifModule::Input(Context* pContext,
64                       RetainPtr<CFX_CodecMemory> codec_memory,
65                       CFX_DIBAttribute*) {
66   auto* ctx = static_cast<CFX_GifContext*>(pContext);
67   ctx->SetInputBuffer(std::move(codec_memory));
68   return true;
69 }
70 
71 }  // namespace fxcodec
72