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_bmpmodule.h"
8
9#include "core/fxcodec/codec/codec_int.h"
10#include "core/fxcodec/fx_codec.h"
11#include "core/fxcodec/lbmp/fx_bmp.h"
12#include "core/fxcrt/unowned_ptr.h"
13#include "core/fxge/fx_dib.h"
14#include "third_party/base/ptr_util.h"
15
16CBmpContext::CBmpContext(CCodec_BmpModule* pModule,
17                         CCodec_BmpModule::Delegate* pDelegate)
18    : m_pModule(pModule), m_pDelegate(pDelegate) {
19}
20
21CBmpContext::~CBmpContext() {}
22
23CCodec_BmpModule::CCodec_BmpModule() {}
24
25CCodec_BmpModule::~CCodec_BmpModule() {}
26
27std::unique_ptr<CCodec_BmpModule::Context> CCodec_BmpModule::Start(
28    Delegate* pDelegate) {
29  auto p = pdfium::MakeUnique<CBmpContext>(this, pDelegate);
30  p->m_Bmp.context_ptr = p.get();
31  return p;
32}
33
34int32_t CCodec_BmpModule::ReadHeader(Context* pContext,
35                                     int32_t* width,
36                                     int32_t* height,
37                                     bool* tb_flag,
38                                     int32_t* components,
39                                     int32_t* pal_num,
40                                     std::vector<uint32_t>* palette,
41                                     CFX_DIBAttribute* pAttribute) {
42  auto* ctx = static_cast<CBmpContext*>(pContext);
43  if (setjmp(ctx->m_Bmp.jmpbuf))
44    return 0;
45
46  int32_t ret = ctx->m_Bmp.ReadHeader();
47  if (ret != 1)
48    return ret;
49
50  *width = ctx->m_Bmp.width;
51  *height = ctx->m_Bmp.height;
52  *tb_flag = ctx->m_Bmp.imgTB_flag;
53  *components = ctx->m_Bmp.components;
54  *pal_num = ctx->m_Bmp.pal_num;
55  *palette = ctx->m_Bmp.palette;
56  if (pAttribute) {
57    pAttribute->m_wDPIUnit = FXCODEC_RESUNIT_METER;
58    pAttribute->m_nXDPI = ctx->m_Bmp.dpi_x;
59    pAttribute->m_nYDPI = ctx->m_Bmp.dpi_y;
60    pAttribute->m_nBmpCompressType = ctx->m_Bmp.compress_flag;
61  }
62  return 1;
63}
64
65int32_t CCodec_BmpModule::LoadImage(Context* pContext) {
66  auto* ctx = static_cast<CBmpContext*>(pContext);
67  if (setjmp(ctx->m_Bmp.jmpbuf))
68    return 0;
69
70  return ctx->m_Bmp.DecodeImage();
71}
72
73uint32_t CCodec_BmpModule::GetAvailInput(Context* pContext,
74                                         uint8_t** avail_buf_ptr) {
75  auto* ctx = static_cast<CBmpContext*>(pContext);
76  return ctx->m_Bmp.GetAvailInput(avail_buf_ptr);
77}
78
79void CCodec_BmpModule::Input(Context* pContext,
80                             const uint8_t* src_buf,
81                             uint32_t src_size) {
82  auto* ctx = static_cast<CBmpContext*>(pContext);
83  ctx->m_Bmp.SetInputBuffer(const_cast<uint8_t*>(src_buf), src_size);
84}
85