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_imagestretcher.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_imagestretcher.h"
8
9#include <utility>
10
11#include "core/fxcrt/data_vector.h"
12#include "core/fxcrt/fx_safe_types.h"
13#include "core/fxge/dib/cfx_dibbase.h"
14#include "core/fxge/dib/cfx_dibitmap.h"
15#include "core/fxge/dib/cstretchengine.h"
16#include "core/fxge/dib/fx_dib.h"
17#include "third_party/base/check.h"
18#include "third_party/base/check_op.h"
19#include "third_party/base/containers/span.h"
20
21namespace {
22
23const int kMaxProgressiveStretchPixels = 1000000;
24
25bool SourceSizeWithinLimit(int width, int height) {
26 return !height || width < kMaxProgressiveStretchPixels / height;
27}
28
29FXDIB_Format GetStretchedFormat(const CFX_DIBBase& src) {
30 FXDIB_Format format = src.GetFormat();
31 if (format == FXDIB_Format::k1bppMask)
33 if (format == FXDIB_Format::k1bppRgb)
35 if (format == FXDIB_Format::k8bppRgb && src.HasPalette())
36 return FXDIB_Format::kRgb;
37 return format;
38}
39
40// Builds a new palette with a size of `CFX_DIBBase::kPaletteSize` from the
41// existing palette in `source`.
42DataVector<uint32_t> BuildPaletteFrom1BppSource(
43 const RetainPtr<const CFX_DIBBase>& source) {
44 DCHECK_EQ(FXDIB_Format::k1bppRgb, source->GetFormat());
45 DCHECK(source->HasPalette());
46
47 int a0;
48 int r0;
49 int g0;
50 int b0;
51 std::tie(a0, r0, g0, b0) = ArgbDecode(source->GetPaletteArgb(0));
52 int a1;
53 int r1;
54 int g1;
55 int b1;
56 std::tie(a1, r1, g1, b1) = ArgbDecode(source->GetPaletteArgb(1));
57 DCHECK_EQ(255, a0);
58 DCHECK_EQ(255, a1);
59
60 DataVector<uint32_t> palette(CFX_DIBBase::kPaletteSize);
61 for (int i = 0; i < static_cast<int>(CFX_DIBBase::kPaletteSize); ++i) {
62 int r = r0 + (r1 - r0) * i / 255;
63 int g = g0 + (g1 - g0) * i / 255;
64 int b = b0 + (b1 - b0) * i / 255;
65 palette[i] = ArgbEncode(255, r, g, b);
66 }
67 return palette;
68}
69
70} // namespace
71
73 RetainPtr<const CFX_DIBBase> source,
74 int dest_width,
75 int dest_height,
76 const FX_RECT& bitmap_rect,
77 const FXDIB_ResampleOptions& options)
78 : m_pDest(pDest),
80 m_ResampleOptions(options),
81 m_DestWidth(dest_width),
82 m_DestHeight(dest_height),
83 m_ClipRect(bitmap_rect),
85 DCHECK(m_ClipRect.Valid());
86}
87
89
91 if (m_DestWidth == 0 || m_DestHeight == 0)
92 return false;
93
94 if (m_pSource->GetFormat() == FXDIB_Format::k1bppRgb &&
95 m_pSource->HasPalette()) {
96 if (!m_pDest->SetInfo(m_ClipRect.Width(), m_ClipRect.Height(), m_DestFormat,
97 BuildPaletteFrom1BppSource(m_pSource))) {
98 return false;
99 }
100 } else if (!m_pDest->SetInfo(m_ClipRect.Width(), m_ClipRect.Height(),
101 m_DestFormat, {})) {
102 return false;
103 }
104 return StartStretch();
105}
106
108 return ContinueStretch(pPause);
109}
110
112 return m_pSource;
113}
114
115bool CFX_ImageStretcher::StartStretch() {
116 m_pStretchEngine = std::make_unique<CStretchEngine>(
117 m_pDest, m_DestFormat, m_DestWidth, m_DestHeight, m_ClipRect, m_pSource,
118 m_ResampleOptions);
119 m_pStretchEngine->StartStretchHorz();
120 if (SourceSizeWithinLimit(m_pSource->GetWidth(), m_pSource->GetHeight())) {
121 m_pStretchEngine->Continue(nullptr);
122 return false;
123 }
124 return true;
125}
126
127bool CFX_ImageStretcher::ContinueStretch(PauseIndicatorIface* pPause) {
128 return m_pStretchEngine && m_pStretchEngine->Continue(pPause);
129}
bool HasPalette() const
Definition cfx_dibbase.h:62
FXDIB_Format GetFormat() const
Definition cfx_dibbase.h:56
static constexpr uint32_t kPaletteSize
Definition cfx_dibbase.h:42
CFX_ImageStretcher(ScanlineComposerIface *pDest, RetainPtr< const CFX_DIBBase > source, int dest_width, int dest_height, const FX_RECT &bitmap_rect, const FXDIB_ResampleOptions &options)
RetainPtr< const CFX_DIBBase > source()
bool Continue(PauseIndicatorIface *pPause)
FXDIB_Format
Definition fx_dib.h:19
FX_RECT(const FX_RECT &that)=default