Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_bitmapcomposer.cpp
Go to the documentation of this file.
1// Copyright 2017 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/dib/cfx_bitmapcomposer.h"
8
9#include <string.h>
10
11#include "core/fxcrt/fx_2d_size.h"
12#include "core/fxcrt/fx_coordinates.h"
13#include "core/fxcrt/fx_safe_types.h"
14#include "core/fxcrt/fx_system.h"
15#include "core/fxcrt/span_util.h"
16#include "core/fxge/cfx_cliprgn.h"
17#include "core/fxge/dib/cfx_dibitmap.h"
18#include "third_party/base/check_op.h"
19
20CFX_BitmapComposer::CFX_BitmapComposer() = default;
21
22CFX_BitmapComposer::~CFX_BitmapComposer() = default;
23
24void CFX_BitmapComposer::Compose(const RetainPtr<CFX_DIBitmap>& pDest,
25 const CFX_ClipRgn* pClipRgn,
26 float alpha,
27 uint32_t mask_color,
28 const FX_RECT& dest_rect,
29 bool bVertical,
30 bool bFlipX,
31 bool bFlipY,
32 bool bRgbByteOrder,
33 BlendMode blend_mode) {
34 m_pBitmap = pDest;
35 m_pClipRgn = pClipRgn;
36 m_DestLeft = dest_rect.left;
37 m_DestTop = dest_rect.top;
38 m_DestWidth = dest_rect.Width();
39 m_DestHeight = dest_rect.Height();
40 m_Alpha = alpha;
41 m_MaskColor = mask_color;
42 m_pClipMask = nullptr;
43 if (pClipRgn && pClipRgn->GetType() != CFX_ClipRgn::kRectI)
44 m_pClipMask = pClipRgn->GetMask();
45 m_bVertical = bVertical;
46 m_bFlipX = bFlipX;
47 m_bFlipY = bFlipY;
48 m_bRgbByteOrder = bRgbByteOrder;
49 m_BlendMode = blend_mode;
50}
51
52bool CFX_BitmapComposer::SetInfo(int width,
53 int height,
54 FXDIB_Format src_format,
55 DataVector<uint32_t> src_palette) {
56 DCHECK_NE(src_format, FXDIB_Format::k1bppMask);
57 DCHECK_NE(src_format, FXDIB_Format::k1bppRgb);
58 m_SrcFormat = src_format;
59 if (!m_Compositor.Init(m_pBitmap->GetFormat(), src_format, src_palette,
60 m_MaskColor, m_BlendMode,
61 m_pClipMask || m_Alpha != 1.0f, m_bRgbByteOrder)) {
62 return false;
63 }
64 if (m_bVertical) {
65 m_pScanlineV.resize(m_pBitmap->GetBPP() / 8 * width + 4);
66 m_pClipScanV.resize(m_pBitmap->GetHeight());
67 }
68 if (m_Alpha != 1.0f) {
69 m_pAddClipScan.resize(m_bVertical ? m_pBitmap->GetHeight()
70 : m_pBitmap->GetWidth());
71 }
72 return true;
73}
74
75void CFX_BitmapComposer::DoCompose(pdfium::span<uint8_t> dest_scan,
76 pdfium::span<const uint8_t> src_scan,
77 int dest_width,
78 pdfium::span<const uint8_t> clip_scan) {
79 if (m_Alpha != 1.0f) {
80 if (!clip_scan.empty()) {
81 for (int i = 0; i < dest_width; ++i) {
82 m_pAddClipScan[i] = clip_scan[i] * m_Alpha;
83 }
84 } else {
85 fxcrt::spanset(pdfium::make_span(m_pAddClipScan).first(dest_width),
86 FXSYS_roundf(m_Alpha * 255));
87 }
88 clip_scan = m_pAddClipScan;
89 }
90 if (m_SrcFormat == FXDIB_Format::k8bppMask) {
91 m_Compositor.CompositeByteMaskLine(dest_scan, src_scan, dest_width,
92 clip_scan);
93 } else if (m_SrcFormat == FXDIB_Format::k8bppRgb) {
94 m_Compositor.CompositePalBitmapLine(dest_scan, src_scan, 0, dest_width,
95 clip_scan);
96 } else {
97 m_Compositor.CompositeRgbBitmapLine(dest_scan, src_scan, dest_width,
98 clip_scan);
99 }
100}
101
102void CFX_BitmapComposer::ComposeScanline(int line,
103 pdfium::span<const uint8_t> scanline) {
104 if (m_bVertical) {
105 ComposeScanlineV(line, scanline);
106 return;
107 }
108 pdfium::span<const uint8_t> clip_scan;
109 if (m_pClipMask) {
110 clip_scan =
111 m_pClipMask
112 ->GetWritableScanline(m_DestTop + line - m_pClipRgn->GetBox().top)
113 .subspan(m_DestLeft - m_pClipRgn->GetBox().left);
114 }
115 pdfium::span<uint8_t> dest_scan =
116 m_pBitmap->GetWritableScanline(line + m_DestTop);
117 if (!dest_scan.empty()) {
118 FX_SAFE_UINT32 offset = m_DestLeft;
119 offset *= m_pBitmap->GetBPP();
120 offset /= 8;
121 if (!offset.IsValid())
122 return;
123
124 dest_scan = dest_scan.subspan(offset.ValueOrDie());
125 }
126 DoCompose(dest_scan, scanline, m_DestWidth, clip_scan);
127}
128
129void CFX_BitmapComposer::ComposeScanlineV(
130 int line,
131 pdfium::span<const uint8_t> scanline) {
132 int Bpp = m_pBitmap->GetBPP() / 8;
133 int dest_pitch = m_pBitmap->GetPitch();
134 int dest_x = m_DestLeft + (m_bFlipX ? (m_DestWidth - line - 1) : line);
135 pdfium::span<uint8_t> dest_span = m_pBitmap->GetWritableBuffer();
136 if (!dest_span.empty()) {
137 const size_t dest_x_offset = Fx2DSizeOrDie(dest_x, Bpp);
138 const size_t dest_y_offset = Fx2DSizeOrDie(m_DestTop, dest_pitch);
139 dest_span = dest_span.subspan(dest_y_offset).subspan(dest_x_offset);
140 if (m_bFlipY) {
141 const size_t dest_flip_offset =
142 Fx2DSizeOrDie(dest_pitch, m_DestHeight - 1);
143 dest_span = dest_span.subspan(dest_flip_offset);
144 }
145 }
146 uint8_t* dest_buf = dest_span.data();
147 const int y_step = m_bFlipY ? -dest_pitch : dest_pitch;
148 uint8_t* src_scan = m_pScanlineV.data();
149 uint8_t* dest_scan = dest_buf;
150 for (int i = 0; i < m_DestHeight; ++i) {
151 for (int j = 0; j < Bpp; ++j)
152 *src_scan++ = dest_scan[j];
153 dest_scan += y_step;
154 }
155 pdfium::span<uint8_t> clip_scan;
156 if (m_pClipMask) {
157 clip_scan = m_pClipScanV;
158 int clip_pitch = m_pClipMask->GetPitch();
159 const uint8_t* src_clip =
160 m_pClipMask->GetScanline(m_DestTop - m_pClipRgn->GetBox().top)
161 .subspan(dest_x - m_pClipRgn->GetBox().left)
162 .data();
163 if (m_bFlipY) {
164 src_clip += Fx2DSizeOrDie(clip_pitch, m_DestHeight - 1);
165 clip_pitch = -clip_pitch;
166 }
167 for (int i = 0; i < m_DestHeight; ++i) {
168 clip_scan[i] = *src_clip;
169 src_clip += clip_pitch;
170 }
171 }
172 DoCompose(m_pScanlineV, scanline, m_DestHeight, clip_scan);
173 src_scan = m_pScanlineV.data();
174 dest_scan = dest_buf;
175 for (int i = 0; i < m_DestHeight; ++i) {
176 for (int j = 0; j < Bpp; ++j)
177 dest_scan[j] = *src_scan++;
178 dest_scan += y_step;
179 }
180}
~CFX_BitmapComposer() override
void Compose(const RetainPtr< CFX_DIBitmap > &pDest, const CFX_ClipRgn *pClipRgn, float alpha, uint32_t mask_color, const FX_RECT &dest_rect, bool bVertical, bool bFlipX, bool bFlipY, bool bRgbByteOrder, BlendMode blend_mode)
void ComposeScanline(int line, pdfium::span< const uint8_t > scanline) override
bool SetInfo(int width, int height, FXDIB_Format src_format, DataVector< uint32_t > src_palette) override
BlendMode
Definition fx_dib.h:49
FXDIB_Format
Definition fx_dib.h:19
int Height() const
int Width() const
int32_t top
int32_t left