1 // Copyright 2014 The PDFium Authors
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/gif/cfx_gifcontext.h"
11 #include "core/fxge/dib/fx_dib.h"
12
13 namespace fxcodec {
14
15 // static
StartDecode(Delegate * pDelegate)16 std::unique_ptr<ProgressiveDecoderIface::Context> GifDecoder::StartDecode(
17 Delegate* pDelegate) {
18 return std::make_unique<CFX_GifContext>(pDelegate);
19 }
20
21 // static
ReadHeader(ProgressiveDecoderIface::Context * pContext,int * width,int * height,int * pal_num,CFX_GifPalette ** pal_pp,int * bg_index)22 GifDecoder::Status GifDecoder::ReadHeader(
23 ProgressiveDecoderIface::Context* pContext,
24 int* width,
25 int* height,
26 int* pal_num,
27 CFX_GifPalette** pal_pp,
28 int* bg_index) {
29 auto* context = static_cast<CFX_GifContext*>(pContext);
30 Status ret = context->ReadHeader();
31 if (ret != Status::kSuccess)
32 return ret;
33
34 *width = context->width_;
35 *height = context->height_;
36 *pal_num = (2 << context->global_palette_exp_);
37 *pal_pp = context->global_palette_.empty() ? nullptr
38 : context->global_palette_.data();
39 *bg_index = context->bc_index_;
40 return Status::kSuccess;
41 }
42
43 // static
LoadFrameInfo(ProgressiveDecoderIface::Context * pContext)44 std::pair<GifDecoder::Status, size_t> GifDecoder::LoadFrameInfo(
45 ProgressiveDecoderIface::Context* pContext) {
46 auto* context = static_cast<CFX_GifContext*>(pContext);
47 Status ret = context->GetFrame();
48 if (ret != Status::kSuccess)
49 return {ret, 0};
50 return {Status::kSuccess, context->GetFrameNum()};
51 }
52
53 // static
LoadFrame(ProgressiveDecoderIface::Context * pContext,size_t frame_num)54 GifDecoder::Status GifDecoder::LoadFrame(
55 ProgressiveDecoderIface::Context* pContext,
56 size_t frame_num) {
57 return static_cast<CFX_GifContext*>(pContext)->LoadFrame(frame_num);
58 }
59
60 // static
GetAvailInput(ProgressiveDecoderIface::Context * pContext)61 FX_FILESIZE GifDecoder::GetAvailInput(
62 ProgressiveDecoderIface::Context* pContext) {
63 return static_cast<CFX_GifContext*>(pContext)->GetAvailInput();
64 }
65
66 // static
Input(ProgressiveDecoderIface::Context * pContext,RetainPtr<CFX_CodecMemory> codec_memory)67 bool GifDecoder::Input(ProgressiveDecoderIface::Context* pContext,
68 RetainPtr<CFX_CodecMemory> codec_memory) {
69 auto* ctx = static_cast<CFX_GifContext*>(pContext);
70 ctx->SetInputBuffer(std::move(codec_memory));
71 return true;
72 }
73
74 } // namespace fxcodec
75