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