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 #ifndef CORE_FXCODEC_JBIG2_JBIG2_IMAGE_H_
8 #define CORE_FXCODEC_JBIG2_JBIG2_IMAGE_H_
9 
10 #include <memory>
11 
12 #include "core/fxcodec/jbig2/JBig2_Define.h"
13 #include "core/fxcrt/fx_memory_wrappers.h"
14 #include "core/fxcrt/maybe_owned.h"
15 
16 struct FX_RECT;
17 
18 enum JBig2ComposeOp {
19   JBIG2_COMPOSE_OR = 0,
20   JBIG2_COMPOSE_AND = 1,
21   JBIG2_COMPOSE_XOR = 2,
22   JBIG2_COMPOSE_XNOR = 3,
23   JBIG2_COMPOSE_REPLACE = 4
24 };
25 
26 class CJBig2_Image {
27  public:
28   CJBig2_Image(int32_t w, int32_t h);
29   CJBig2_Image(int32_t w, int32_t h, int32_t stride, uint8_t* pBuf);
30   CJBig2_Image(const CJBig2_Image& other);
31   ~CJBig2_Image();
32 
33   static bool IsValidImageSize(int32_t w, int32_t h);
34 
width()35   int32_t width() const { return m_nWidth; }
height()36   int32_t height() const { return m_nHeight; }
stride()37   int32_t stride() const { return m_nStride; }
38 
data()39   uint8_t* data() const { return m_pData.Get(); }
40 
41   int GetPixel(int32_t x, int32_t y) const;
42   void SetPixel(int32_t x, int32_t y, int v);
43 
GetLineUnsafe(int32_t y)44   uint8_t* GetLineUnsafe(int32_t y) const { return data() + y * m_nStride; }
GetLine(int32_t y)45   uint8_t* GetLine(int32_t y) const {
46     return (y >= 0 && y < m_nHeight) ? GetLineUnsafe(y) : nullptr;
47   }
48 
49   void CopyLine(int32_t hTo, int32_t hFrom);
50   void Fill(bool v);
51 
52   bool ComposeFrom(int32_t x, int32_t y, CJBig2_Image* pSrc, JBig2ComposeOp op);
53   bool ComposeFromWithRect(int32_t x,
54                            int32_t y,
55                            CJBig2_Image* pSrc,
56                            const FX_RECT& rtSrc,
57                            JBig2ComposeOp op);
58 
59   std::unique_ptr<CJBig2_Image> SubImage(int32_t x,
60                                          int32_t y,
61                                          int32_t w,
62                                          int32_t h);
63   void Expand(int32_t h, bool v);
64 
65   bool ComposeTo(CJBig2_Image* pDst, int32_t x, int32_t y, JBig2ComposeOp op);
66   bool ComposeToWithRect(CJBig2_Image* pDst,
67                          int32_t x,
68                          int32_t y,
69                          const FX_RECT& rtSrc,
70                          JBig2ComposeOp op);
71 
72  private:
73   void SubImageFast(int32_t x,
74                     int32_t y,
75                     int32_t w,
76                     int32_t h,
77                     CJBig2_Image* pImage);
78   void SubImageSlow(int32_t x,
79                     int32_t y,
80                     int32_t w,
81                     int32_t h,
82                     CJBig2_Image* pImage);
83   bool ComposeToInternal(CJBig2_Image* pDst,
84                          int32_t x,
85                          int32_t y,
86                          JBig2ComposeOp op,
87                          const FX_RECT& rtSrc);
88 
89   MaybeOwned<uint8_t, FxFreeDeleter> m_pData;
90   int32_t m_nWidth = 0;   // 1-bit pixels
91   int32_t m_nHeight = 0;  // lines
92   int32_t m_nStride = 0;  // bytes, must be multiple of 4.
93 };
94 
95 #endif  // CORE_FXCODEC_JBIG2_JBIG2_IMAGE_H_
96