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/fx_codec.h"
8
9 #include <algorithm>
10 #include <cmath>
11 #include <memory>
12 #include <utility>
13
14 #include "core/fxcodec/jbig2/jbig2module.h"
15 #include "core/fxcodec/jpeg/jpegmodule.h"
16 #include "core/fxcrt/fx_extension.h"
17 #include "core/fxcrt/fx_safe_types.h"
18 #include "third_party/base/logging.h"
19 #include "third_party/base/ptr_util.h"
20
21 namespace fxcodec {
22
23 namespace {
24
25 ModuleMgr* g_ModuleMgr = nullptr;
26
27 } // namespace
28
29 // static
Create()30 void ModuleMgr::Create() {
31 ASSERT(!g_ModuleMgr);
32 g_ModuleMgr = new ModuleMgr();
33 }
34
35 // static
Destroy()36 void ModuleMgr::Destroy() {
37 ASSERT(g_ModuleMgr);
38 delete g_ModuleMgr;
39 g_ModuleMgr = nullptr;
40 }
41
42 // static
GetInstance()43 ModuleMgr* ModuleMgr::GetInstance() {
44 ASSERT(g_ModuleMgr);
45 return g_ModuleMgr;
46 }
47
ModuleMgr()48 ModuleMgr::ModuleMgr()
49 : m_pJpegModule(pdfium::MakeUnique<JpegModule>()),
50 m_pJbig2Module(pdfium::MakeUnique<Jbig2Module>()) {
51 #ifdef PDF_ENABLE_XFA_BMP
52 SetBmpModule(pdfium::MakeUnique<BmpModule>());
53 #endif
54
55 #ifdef PDF_ENABLE_XFA_GIF
56 SetGifModule(pdfium::MakeUnique<GifModule>());
57 #endif
58
59 #ifdef PDF_ENABLE_XFA_PNG
60 SetPngModule(pdfium::MakeUnique<PngModule>());
61 #endif
62
63 #ifdef PDF_ENABLE_XFA_TIFF
64 SetTiffModule(pdfium::MakeUnique<TiffModule>());
65 #endif
66 }
67
68 ModuleMgr::~ModuleMgr() = default;
69
70 #ifdef PDF_ENABLE_XFA
71 CFX_DIBAttribute::CFX_DIBAttribute() = default;
72
~CFX_DIBAttribute()73 CFX_DIBAttribute::~CFX_DIBAttribute() {
74 for (const auto& pair : m_Exif)
75 FX_Free(pair.second);
76 }
77 #endif // PDF_ENABLE_XFA
78
ReverseRGB(uint8_t * pDestBuf,const uint8_t * pSrcBuf,int pixels)79 void ReverseRGB(uint8_t* pDestBuf, const uint8_t* pSrcBuf, int pixels) {
80 if (pDestBuf == pSrcBuf) {
81 for (int i = 0; i < pixels; i++) {
82 uint8_t temp = pDestBuf[2];
83 pDestBuf[2] = pDestBuf[0];
84 pDestBuf[0] = temp;
85 pDestBuf += 3;
86 }
87 } else {
88 for (int i = 0; i < pixels; i++) {
89 *pDestBuf++ = pSrcBuf[2];
90 *pDestBuf++ = pSrcBuf[1];
91 *pDestBuf++ = pSrcBuf[0];
92 pSrcBuf += 3;
93 }
94 }
95 }
96
CalculatePitch8(uint32_t bpc,uint32_t components,int width)97 FX_SAFE_UINT32 CalculatePitch8(uint32_t bpc, uint32_t components, int width) {
98 FX_SAFE_UINT32 pitch = bpc;
99 pitch *= components;
100 pitch *= width;
101 pitch += 7;
102 pitch /= 8;
103 return pitch;
104 }
105
CalculatePitch32(int bpp,int width)106 FX_SAFE_UINT32 CalculatePitch32(int bpp, int width) {
107 FX_SAFE_UINT32 pitch = bpp;
108 pitch *= width;
109 pitch += 31;
110 pitch /= 32; // quantized to number of 32-bit words.
111 pitch *= 4; // and then back to bytes, (not just /8 in one step).
112 return pitch;
113 }
114
115 } // namespace fxcodec
116