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/bmp/bmp_decoder.h"
8
9 #include <utility>
10
11 #include "core/fxcodec/bmp/cfx_bmpcontext.h"
12 #include "core/fxcodec/cfx_codec_memory.h"
13 #include "core/fxcodec/fx_codec.h"
14 #include "core/fxcodec/fx_codec_def.h"
15 #include "core/fxge/dib/fx_dib.h"
16 #include "third_party/base/check.h"
17
18 namespace fxcodec {
19
20 // static
StartDecode(Delegate * pDelegate)21 std::unique_ptr<ProgressiveDecoderIface::Context> BmpDecoder::StartDecode(
22 Delegate* pDelegate) {
23 return std::make_unique<CFX_BmpContext>(pDelegate);
24 }
25
26 // static
ReadHeader(ProgressiveDecoderIface::Context * pContext,int32_t * width,int32_t * height,bool * tb_flag,int32_t * components,int32_t * pal_num,const std::vector<uint32_t> ** palette,CFX_DIBAttribute * pAttribute)27 BmpDecoder::Status BmpDecoder::ReadHeader(
28 ProgressiveDecoderIface::Context* pContext,
29 int32_t* width,
30 int32_t* height,
31 bool* tb_flag,
32 int32_t* components,
33 int32_t* pal_num,
34 const std::vector<uint32_t>** palette,
35 CFX_DIBAttribute* pAttribute) {
36 DCHECK(pAttribute);
37
38 auto* ctx = static_cast<CFX_BmpContext*>(pContext);
39 Status status = ctx->m_Bmp.ReadHeader();
40 if (status != Status::kSuccess)
41 return status;
42
43 *width = ctx->m_Bmp.width();
44 *height = ctx->m_Bmp.height();
45 *tb_flag = ctx->m_Bmp.img_tb_flag();
46 *components = ctx->m_Bmp.components();
47 *pal_num = ctx->m_Bmp.pal_num();
48 *palette = ctx->m_Bmp.palette();
49 pAttribute->m_wDPIUnit = CFX_DIBAttribute::kResUnitMeter;
50 pAttribute->m_nXDPI = ctx->m_Bmp.dpi_x();
51 pAttribute->m_nYDPI = ctx->m_Bmp.dpi_y();
52 return Status::kSuccess;
53 }
54
55 // static
LoadImage(ProgressiveDecoderIface::Context * pContext)56 BmpDecoder::Status BmpDecoder::LoadImage(
57 ProgressiveDecoderIface::Context* pContext) {
58 return static_cast<CFX_BmpContext*>(pContext)->m_Bmp.DecodeImage();
59 }
60
61 // static
GetAvailInput(ProgressiveDecoderIface::Context * pContext)62 FX_FILESIZE BmpDecoder::GetAvailInput(
63 ProgressiveDecoderIface::Context* pContext) {
64 return static_cast<CFX_BmpContext*>(pContext)->m_Bmp.GetAvailInput();
65 }
66
67 // static
Input(ProgressiveDecoderIface::Context * pContext,RetainPtr<CFX_CodecMemory> codec_memory)68 bool BmpDecoder::Input(ProgressiveDecoderIface::Context* pContext,
69 RetainPtr<CFX_CodecMemory> codec_memory) {
70 auto* ctx = static_cast<CFX_BmpContext*>(pContext);
71 ctx->m_Bmp.SetInputBuffer(std::move(codec_memory));
72 return true;
73 }
74
75 } // namespace fxcodec
76