1 // Copyright 2016 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/fxge/cfx_cliprgn.h"
8
9 #include <string.h>
10
11 #include <utility>
12
13 #include "core/fxcrt/span_util.h"
14 #include "core/fxge/dib/cfx_dibitmap.h"
15 #include "third_party/base/check_op.h"
16 #include "third_party/base/notreached.h"
17
CFX_ClipRgn(int width,int height)18 CFX_ClipRgn::CFX_ClipRgn(int width, int height) : m_Box(0, 0, width, height) {}
19
20 CFX_ClipRgn::CFX_ClipRgn(const CFX_ClipRgn& src) = default;
21
22 CFX_ClipRgn::~CFX_ClipRgn() = default;
23
IntersectRect(const FX_RECT & rect)24 void CFX_ClipRgn::IntersectRect(const FX_RECT& rect) {
25 if (m_Type == kRectI) {
26 m_Box.Intersect(rect);
27 return;
28 }
29 IntersectMaskRect(rect, m_Box, m_Mask);
30 }
31
IntersectMaskRect(FX_RECT rect,FX_RECT mask_rect,RetainPtr<CFX_DIBitmap> pOldMask)32 void CFX_ClipRgn::IntersectMaskRect(FX_RECT rect,
33 FX_RECT mask_rect,
34 RetainPtr<CFX_DIBitmap> pOldMask) {
35 m_Type = kMaskF;
36 m_Box = rect;
37 m_Box.Intersect(mask_rect);
38 if (m_Box.IsEmpty()) {
39 m_Type = kRectI;
40 return;
41 }
42 if (m_Box == mask_rect) {
43 m_Mask = std::move(pOldMask);
44 return;
45 }
46 m_Mask = pdfium::MakeRetain<CFX_DIBitmap>();
47 m_Mask->Create(m_Box.Width(), m_Box.Height(), FXDIB_Format::k8bppMask);
48 const int offset = m_Box.left - mask_rect.left;
49 for (int row = m_Box.top; row < m_Box.bottom; row++) {
50 pdfium::span<uint8_t> dest_scan =
51 m_Mask->GetWritableScanline(row - m_Box.top);
52 pdfium::span<const uint8_t> src_scan =
53 pOldMask->GetScanline(row - mask_rect.top);
54 fxcrt::spancpy(dest_scan, src_scan.subspan(offset, m_Box.Width()));
55 }
56 }
57
IntersectMaskF(int left,int top,RetainPtr<CFX_DIBitmap> pMask)58 void CFX_ClipRgn::IntersectMaskF(int left,
59 int top,
60 RetainPtr<CFX_DIBitmap> pMask) {
61 DCHECK_EQ(pMask->GetFormat(), FXDIB_Format::k8bppMask);
62 FX_RECT mask_box(left, top, left + pMask->GetWidth(),
63 top + pMask->GetHeight());
64 if (m_Type == kRectI) {
65 IntersectMaskRect(m_Box, mask_box, std::move(pMask));
66 return;
67 }
68
69 FX_RECT new_box = m_Box;
70 new_box.Intersect(mask_box);
71 if (new_box.IsEmpty()) {
72 m_Type = kRectI;
73 m_Mask = nullptr;
74 m_Box = new_box;
75 return;
76 }
77 auto new_dib = pdfium::MakeRetain<CFX_DIBitmap>();
78 new_dib->Create(new_box.Width(), new_box.Height(), FXDIB_Format::k8bppMask);
79 for (int row = new_box.top; row < new_box.bottom; row++) {
80 pdfium::span<const uint8_t> old_scan = m_Mask->GetScanline(row - m_Box.top);
81 pdfium::span<const uint8_t> mask_scan = pMask->GetScanline(row - top);
82 uint8_t* new_scan = new_dib->GetWritableScanline(row - new_box.top).data();
83 for (int col = new_box.left; col < new_box.right; col++) {
84 new_scan[col - new_box.left] =
85 old_scan[col - m_Box.left] * mask_scan[col - left] / 255;
86 }
87 }
88 m_Box = new_box;
89 m_Mask = std::move(new_dib);
90 }
91