xref: /aosp_15_r20/external/pdfium/xfa/fgas/graphics/cfgas_geshading.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 "xfa/fgas/graphics/cfgas_geshading.h"
8 
CFGAS_GEShading(const CFX_PointF & beginPoint,const CFX_PointF & endPoint,bool isExtendedBegin,bool isExtendedEnd,FX_ARGB beginArgb,FX_ARGB endArgb)9 CFGAS_GEShading::CFGAS_GEShading(const CFX_PointF& beginPoint,
10                                  const CFX_PointF& endPoint,
11                                  bool isExtendedBegin,
12                                  bool isExtendedEnd,
13                                  FX_ARGB beginArgb,
14                                  FX_ARGB endArgb)
15     : m_type(Type::kAxial),
16       m_beginPoint(beginPoint),
17       m_endPoint(endPoint),
18       m_beginRadius(0),
19       m_endRadius(0),
20       m_isExtendedBegin(isExtendedBegin),
21       m_isExtendedEnd(isExtendedEnd) {
22   InitArgbArray(beginArgb, endArgb);
23 }
24 
CFGAS_GEShading(const CFX_PointF & beginPoint,const CFX_PointF & endPoint,float beginRadius,float endRadius,bool isExtendedBegin,bool isExtendedEnd,FX_ARGB beginArgb,FX_ARGB endArgb)25 CFGAS_GEShading::CFGAS_GEShading(const CFX_PointF& beginPoint,
26                                  const CFX_PointF& endPoint,
27                                  float beginRadius,
28                                  float endRadius,
29                                  bool isExtendedBegin,
30                                  bool isExtendedEnd,
31                                  FX_ARGB beginArgb,
32                                  FX_ARGB endArgb)
33     : m_type(Type::kRadial),
34       m_beginPoint(beginPoint),
35       m_endPoint(endPoint),
36       m_beginRadius(beginRadius),
37       m_endRadius(endRadius),
38       m_isExtendedBegin(isExtendedBegin),
39       m_isExtendedEnd(isExtendedEnd) {
40   InitArgbArray(beginArgb, endArgb);
41 }
42 
43 CFGAS_GEShading::~CFGAS_GEShading() = default;
44 
InitArgbArray(FX_ARGB beginArgb,FX_ARGB endArgb)45 void CFGAS_GEShading::InitArgbArray(FX_ARGB beginArgb, FX_ARGB endArgb) {
46   int32_t a1;
47   int32_t r1;
48   int32_t g1;
49   int32_t b1;
50   std::tie(a1, r1, g1, b1) = ArgbDecode(beginArgb);
51 
52   int32_t a2;
53   int32_t r2;
54   int32_t g2;
55   int32_t b2;
56   std::tie(a2, r2, g2, b2) = ArgbDecode(endArgb);
57 
58   float f = static_cast<float>(kSteps - 1);
59   float aScale = 1.0 * (a2 - a1) / f;
60   float rScale = 1.0 * (r2 - r1) / f;
61   float gScale = 1.0 * (g2 - g1) / f;
62   float bScale = 1.0 * (b2 - b1) / f;
63 
64   for (size_t i = 0; i < kSteps; i++) {
65     int32_t a3 = static_cast<int32_t>(i * aScale);
66     int32_t r3 = static_cast<int32_t>(i * rScale);
67     int32_t g3 = static_cast<int32_t>(i * gScale);
68     int32_t b3 = static_cast<int32_t>(i * bScale);
69 
70     // TODO(dsinclair): Add overloads for FX_ARGB. pdfium:437
71     m_argbArray[i] = ArgbEncode(a1 + a3, r1 + r3, g1 + g3, b1 + b3);
72   }
73 }
74