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/codec/ccodec_gifmodule.h"
8
9#include "core/fxcodec/codec/codec_int.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
16CCodec_GifModule::CCodec_GifModule() {}
17
18CCodec_GifModule::~CCodec_GifModule() {}
19
20std::unique_ptr<CCodec_GifModule::Context> CCodec_GifModule::Start(
21    Delegate* pDelegate) {
22  return pdfium::MakeUnique<CFX_GifContext>(this, pDelegate);
23}
24
25CFX_GifDecodeStatus CCodec_GifModule::ReadHeader(Context* pContext,
26                                                 int* width,
27                                                 int* height,
28                                                 int* pal_num,
29                                                 void** pal_pp,
30                                                 int* bg_index,
31                                                 CFX_DIBAttribute* pAttribute) {
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
46std::pair<CFX_GifDecodeStatus, size_t> CCodec_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
55CFX_GifDecodeStatus CCodec_GifModule::LoadFrame(Context* pContext,
56                                                size_t frame_num,
57                                                CFX_DIBAttribute* pAttribute) {
58  auto* context = static_cast<CFX_GifContext*>(pContext);
59  CFX_GifDecodeStatus ret = context->LoadFrame(frame_num);
60  if (ret != CFX_GifDecodeStatus::Success || !pAttribute)
61    return ret;
62
63  pAttribute->m_nGifLeft = context->images_[frame_num]->image_info.left;
64  pAttribute->m_nGifTop = context->images_[frame_num]->image_info.top;
65  pAttribute->m_fAspectRatio = context->pixel_aspect_;
66  const uint8_t* buf =
67      reinterpret_cast<const uint8_t*>(context->cmt_data_.GetBuffer(0));
68  uint32_t len = context->cmt_data_.GetLength();
69  if (len > 21) {
70    uint8_t size = *buf++;
71    if (size != 0)
72      pAttribute->m_strAuthor = ByteString(buf, size);
73    else
74      pAttribute->m_strAuthor.clear();
75  }
76  return CFX_GifDecodeStatus::Success;
77}
78
79uint32_t CCodec_GifModule::GetAvailInput(Context* pContext,
80                                         uint8_t** avail_buf_ptr) {
81  auto* context = static_cast<CFX_GifContext*>(pContext);
82  return context->GetAvailInput(avail_buf_ptr);
83}
84
85void CCodec_GifModule::Input(Context* pContext,
86                             const uint8_t* src_buf,
87                             uint32_t src_size) {
88  auto* context = static_cast<CFX_GifContext*>(pContext);
89  context->SetInputBuffer((uint8_t*)src_buf, src_size);
90}
91