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/agg/cfx_agg_cliprgn.h"
8
9 #include <stdint.h>
10
11 #include <utility>
12
13 #include "core/fxcrt/check_op.h"
14 #include "core/fxcrt/notreached.h"
15 #include "core/fxcrt/stl_util.h"
16 #include "core/fxge/dib/cfx_dibitmap.h"
17
CFX_AggClipRgn(int width,int height)18 CFX_AggClipRgn::CFX_AggClipRgn(int width, int height)
19 : m_Box(0, 0, width, height) {}
20
21 CFX_AggClipRgn::CFX_AggClipRgn(const CFX_AggClipRgn& src) = default;
22
23 CFX_AggClipRgn::~CFX_AggClipRgn() = default;
24
IntersectRect(const FX_RECT & rect)25 void CFX_AggClipRgn::IntersectRect(const FX_RECT& rect) {
26 if (m_Type == kRectI) {
27 m_Box.Intersect(rect);
28 return;
29 }
30 IntersectMaskRect(rect, m_Box, m_Mask);
31 }
32
IntersectMaskRect(FX_RECT rect,FX_RECT mask_rect,RetainPtr<CFX_DIBitmap> pOldMask)33 void CFX_AggClipRgn::IntersectMaskRect(FX_RECT rect,
34 FX_RECT mask_rect,
35 RetainPtr<CFX_DIBitmap> pOldMask) {
36 m_Type = kMaskF;
37 m_Box = rect;
38 m_Box.Intersect(mask_rect);
39 if (m_Box.IsEmpty()) {
40 m_Type = kRectI;
41 return;
42 }
43 if (m_Box == mask_rect) {
44 m_Mask = std::move(pOldMask);
45 return;
46 }
47 m_Mask = pdfium::MakeRetain<CFX_DIBitmap>();
48 CHECK(m_Mask->Create(m_Box.Width(), m_Box.Height(), FXDIB_Format::k8bppMask));
49 const int offset = m_Box.left - mask_rect.left;
50 for (int row = m_Box.top; row < m_Box.bottom; row++) {
51 pdfium::span<uint8_t> dest_scan =
52 m_Mask->GetWritableScanline(row - m_Box.top);
53 pdfium::span<const uint8_t> src_scan =
54 pOldMask->GetScanline(row - mask_rect.top);
55 fxcrt::Copy(src_scan.subspan(offset, m_Box.Width()), dest_scan);
56 }
57 }
58
IntersectMaskF(int left,int top,RetainPtr<CFX_DIBitmap> pMask)59 void CFX_AggClipRgn::IntersectMaskF(int left,
60 int top,
61 RetainPtr<CFX_DIBitmap> pMask) {
62 FX_RECT mask_box(left, top, left + pMask->GetWidth(),
63 top + pMask->GetHeight());
64 if (!mask_box.IsEmpty()) {
65 // Make sure non-empty masks have the right format. If the mask is empty,
66 // then the format does not matter as it will not get used.
67 CHECK_EQ(pMask->GetFormat(), FXDIB_Format::k8bppMask);
68 }
69 if (m_Type == kRectI) {
70 IntersectMaskRect(m_Box, mask_box, std::move(pMask));
71 return;
72 }
73
74 FX_RECT new_box = m_Box;
75 new_box.Intersect(mask_box);
76 if (new_box.IsEmpty()) {
77 m_Type = kRectI;
78 m_Mask = nullptr;
79 m_Box = new_box;
80 return;
81 }
82 auto new_dib = pdfium::MakeRetain<CFX_DIBitmap>();
83 CHECK(new_dib->Create(new_box.Width(), new_box.Height(),
84 FXDIB_Format::k8bppMask));
85 for (int row = new_box.top; row < new_box.bottom; row++) {
86 pdfium::span<const uint8_t> old_scan = m_Mask->GetScanline(row - m_Box.top);
87 pdfium::span<const uint8_t> mask_scan = pMask->GetScanline(row - top);
88 auto new_scan = new_dib->GetWritableScanline(row - new_box.top);
89 for (int col = new_box.left; col < new_box.right; col++) {
90 new_scan[col - new_box.left] =
91 old_scan[col - m_Box.left] * mask_scan[col - left] / 255;
92 }
93 }
94 m_Box = new_box;
95 m_Mask = std::move(new_dib);
96 }
97