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