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