diff options
Diffstat (limited to 'core/fpdfapi/render/cpdf_rendershading.cpp')
-rw-r--r-- | core/fpdfapi/render/cpdf_rendershading.cpp | 594 |
1 files changed, 314 insertions, 280 deletions
diff --git a/core/fpdfapi/render/cpdf_rendershading.cpp b/core/fpdfapi/render/cpdf_rendershading.cpp index 53fb79a66..ce7543b6e 100644 --- a/core/fpdfapi/render/cpdf_rendershading.cpp +++ b/core/fpdfapi/render/cpdf_rendershading.cpp @@ -1,4 +1,4 @@ -// Copyright 2019 PDFium Authors. All rights reserved. +// Copyright 2019 The PDFium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -6,9 +6,10 @@ #include "core/fpdfapi/render/cpdf_rendershading.h" +#include <math.h> + #include <algorithm> #include <array> -#include <cmath> #include <memory> #include <utility> #include <vector> @@ -25,9 +26,16 @@ #include "core/fpdfapi/render/cpdf_renderoptions.h" #include "core/fxcrt/fx_safe_types.h" #include "core/fxcrt/fx_system.h" +#include "core/fxcrt/span_util.h" #include "core/fxge/cfx_defaultrenderdevice.h" +#include "core/fxge/cfx_fillrenderoptions.h" +#include "core/fxge/cfx_path.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/fx_dib.h" +#include "core/fxge/dib/fx_dib.h" +#include "third_party/base/check.h" +#include "third_party/base/check_op.h" +#include "third_party/base/cxx17_backports.h" +#include "third_party/base/span.h" namespace { @@ -57,28 +65,28 @@ std::array<FX_ARGB, kShadingSteps> GetShadingSteps( const RetainPtr<CPDF_ColorSpace>& pCS, int alpha, size_t results_count) { - ASSERT(results_count >= CountOutputsFromFunctions(funcs)); - ASSERT(results_count >= pCS->CountComponents()); + DCHECK(results_count >= CountOutputsFromFunctions(funcs)); + DCHECK(results_count >= pCS->CountComponents()); std::array<FX_ARGB, kShadingSteps> shading_steps; std::vector<float> result_array(results_count); float diff = t_max - t_min; for (int i = 0; i < kShadingSteps; ++i) { float input = diff * i / kShadingSteps + t_min; - int offset = 0; + pdfium::span<float> result_span = pdfium::make_span(result_array); for (const auto& func : funcs) { - if (func) { - int nresults = 0; - if (func->Call(&input, 1, &result_array[offset], &nresults)) - offset += nresults; - } + if (!func) + continue; + absl::optional<uint32_t> nresults = + func->Call(pdfium::make_span(&input, 1), result_span); + if (nresults.has_value()) + result_span = result_span.subspan(nresults.value()); } float R = 0.0f; float G = 0.0f; float B = 0.0f; - pCS->GetRGB(result_array.data(), &R, &G, &B); - shading_steps[i] = - FXARGB_TODIB(ArgbEncode(alpha, FXSYS_roundf(R * 255), - FXSYS_roundf(G * 255), FXSYS_roundf(B * 255))); + pCS->GetRGB(result_array, &R, &G, &B); + shading_steps[i] = ArgbEncode(alpha, FXSYS_roundf(R * 255), + FXSYS_roundf(G * 255), FXSYS_roundf(B * 255)); } return shading_steps; } @@ -89,26 +97,26 @@ void DrawAxialShading(const RetainPtr<CFX_DIBitmap>& pBitmap, const std::vector<std::unique_ptr<CPDF_Function>>& funcs, const RetainPtr<CPDF_ColorSpace>& pCS, int alpha) { - ASSERT(pBitmap->GetFormat() == FXDIB_Argb); + DCHECK_EQ(pBitmap->GetFormat(), FXDIB_Format::kArgb); const uint32_t total_results = GetValidatedOutputsCount(funcs, pCS); if (total_results == 0) return; - const CPDF_Array* pCoords = pDict->GetArrayFor("Coords"); + RetainPtr<const CPDF_Array> pCoords = pDict->GetArrayFor("Coords"); if (!pCoords) return; - float start_x = pCoords->GetNumberAt(0); - float start_y = pCoords->GetNumberAt(1); - float end_x = pCoords->GetNumberAt(2); - float end_y = pCoords->GetNumberAt(3); + float start_x = pCoords->GetFloatAt(0); + float start_y = pCoords->GetFloatAt(1); + float end_x = pCoords->GetFloatAt(2); + float end_y = pCoords->GetFloatAt(3); float t_min = 0; float t_max = 1.0f; - const CPDF_Array* pArray = pDict->GetArrayFor("Domain"); + RetainPtr<const CPDF_Array> pArray = pDict->GetArrayFor("Domain"); if (pArray) { - t_min = pArray->GetNumberAt(0); - t_max = pArray->GetNumberAt(1); + t_min = pArray->GetFloatAt(0); + t_max = pArray->GetFloatAt(1); } pArray = pDict->GetArrayFor("Extend"); const bool bStartExtend = pArray && pArray->GetBooleanAt(0, false); @@ -123,18 +131,17 @@ void DrawAxialShading(const RetainPtr<CFX_DIBitmap>& pBitmap, std::array<FX_ARGB, kShadingSteps> shading_steps = GetShadingSteps(t_min, t_max, funcs, pCS, alpha, total_results); - int pitch = pBitmap->GetPitch(); CFX_Matrix matrix = mtObject2Bitmap.GetInverse(); for (int row = 0; row < height; row++) { uint32_t* dib_buf = - reinterpret_cast<uint32_t*>(pBitmap->GetBuffer() + row * pitch); + reinterpret_cast<uint32_t*>(pBitmap->GetWritableScanline(row).data()); for (int column = 0; column < width; column++) { CFX_PointF pos = matrix.Transform( CFX_PointF(static_cast<float>(column), static_cast<float>(row))); float scale = (((pos.x - start_x) * x_span) + ((pos.y - start_y) * y_span)) / axis_len_square; - int index = (int32_t)(scale * (kShadingSteps - 1)); + int index = static_cast<int32_t>(scale * (kShadingSteps - 1)); if (index < 0) { if (!bStartExtend) continue; @@ -157,28 +164,28 @@ void DrawRadialShading(const RetainPtr<CFX_DIBitmap>& pBitmap, const std::vector<std::unique_ptr<CPDF_Function>>& funcs, const RetainPtr<CPDF_ColorSpace>& pCS, int alpha) { - ASSERT(pBitmap->GetFormat() == FXDIB_Argb); + DCHECK_EQ(pBitmap->GetFormat(), FXDIB_Format::kArgb); const uint32_t total_results = GetValidatedOutputsCount(funcs, pCS); if (total_results == 0) return; - const CPDF_Array* pCoords = pDict->GetArrayFor("Coords"); + RetainPtr<const CPDF_Array> pCoords = pDict->GetArrayFor("Coords"); if (!pCoords) return; - float start_x = pCoords->GetNumberAt(0); - float start_y = pCoords->GetNumberAt(1); - float start_r = pCoords->GetNumberAt(2); - float end_x = pCoords->GetNumberAt(3); - float end_y = pCoords->GetNumberAt(4); - float end_r = pCoords->GetNumberAt(5); + float start_x = pCoords->GetFloatAt(0); + float start_y = pCoords->GetFloatAt(1); + float start_r = pCoords->GetFloatAt(2); + float end_x = pCoords->GetFloatAt(3); + float end_y = pCoords->GetFloatAt(4); + float end_r = pCoords->GetFloatAt(5); float t_min = 0; float t_max = 1.0f; - const CPDF_Array* pArray = pDict->GetArrayFor("Domain"); + RetainPtr<const CPDF_Array> pArray = pDict->GetArrayFor("Domain"); if (pArray) { - t_min = pArray->GetNumberAt(0); - t_max = pArray->GetNumberAt(1); + t_min = pArray->GetFloatAt(0); + t_max = pArray->GetFloatAt(1); } pArray = pDict->GetArrayFor("Extend"); const bool bStartExtend = pArray && pArray->GetBooleanAt(0, false); @@ -191,19 +198,16 @@ void DrawRadialShading(const RetainPtr<CFX_DIBitmap>& pBitmap, const float dy = end_y - start_y; const float dr = end_r - start_r; const float a = dx * dx + dy * dy - dr * dr; - const bool a_is_float_zero = IsFloatZero(a); + const bool a_is_float_zero = FXSYS_IsFloatZero(a); int width = pBitmap->GetWidth(); int height = pBitmap->GetHeight(); - int pitch = pBitmap->GetPitch(); - - bool bDecreasing = - (dr < 0 && static_cast<int>(sqrt(dx * dx + dy * dy)) < -dr); + bool bDecreasing = dr < 0 && static_cast<int>(FXSYS_sqrt2(dx, dy)) < -dr; CFX_Matrix matrix = mtObject2Bitmap.GetInverse(); for (int row = 0; row < height; row++) { uint32_t* dib_buf = - reinterpret_cast<uint32_t*>(pBitmap->GetBuffer() + row * pitch); + reinterpret_cast<uint32_t*>(pBitmap->GetWritableScanline(row).data()); for (int column = 0; column < width; column++) { CFX_PointF pos = matrix.Transform( CFX_PointF(static_cast<float>(column), static_cast<float>(row))); @@ -212,7 +216,7 @@ void DrawRadialShading(const RetainPtr<CFX_DIBitmap>& pBitmap, float b = -2 * (pos_dx * dx + pos_dy * dy + start_r * dr); float c = pos_dx * pos_dx + pos_dy * pos_dy - start_r * start_r; float s; - if (IsFloatZero(b)) { + if (FXSYS_IsFloatZero(b)) { s = sqrt(-c / a); } else if (a_is_float_zero) { s = -c / b; @@ -256,57 +260,57 @@ void DrawFuncShading(const RetainPtr<CFX_DIBitmap>& pBitmap, const std::vector<std::unique_ptr<CPDF_Function>>& funcs, const RetainPtr<CPDF_ColorSpace>& pCS, int alpha) { - ASSERT(pBitmap->GetFormat() == FXDIB_Argb); + DCHECK_EQ(pBitmap->GetFormat(), FXDIB_Format::kArgb); const uint32_t total_results = GetValidatedOutputsCount(funcs, pCS); if (total_results == 0) return; - const CPDF_Array* pDomain = pDict->GetArrayFor("Domain"); + RetainPtr<const CPDF_Array> pDomain = pDict->GetArrayFor("Domain"); float xmin = 0.0f; float ymin = 0.0f; float xmax = 1.0f; float ymax = 1.0f; if (pDomain) { - xmin = pDomain->GetNumberAt(0); - xmax = pDomain->GetNumberAt(1); - ymin = pDomain->GetNumberAt(2); - ymax = pDomain->GetNumberAt(3); + xmin = pDomain->GetFloatAt(0); + xmax = pDomain->GetFloatAt(1); + ymin = pDomain->GetFloatAt(2); + ymax = pDomain->GetFloatAt(3); } CFX_Matrix mtDomain2Target = pDict->GetMatrixFor("Matrix"); CFX_Matrix matrix = mtObject2Bitmap.GetInverse() * mtDomain2Target.GetInverse(); int width = pBitmap->GetWidth(); int height = pBitmap->GetHeight(); - int pitch = pBitmap->GetPitch(); - ASSERT(total_results >= CountOutputsFromFunctions(funcs)); - ASSERT(total_results >= pCS->CountComponents()); + DCHECK(total_results >= CountOutputsFromFunctions(funcs)); + DCHECK(total_results >= pCS->CountComponents()); std::vector<float> result_array(total_results); for (int row = 0; row < height; ++row) { - uint32_t* dib_buf = (uint32_t*)(pBitmap->GetBuffer() + row * pitch); + uint32_t* dib_buf = + reinterpret_cast<uint32_t*>(pBitmap->GetWritableScanline(row).data()); for (int column = 0; column < width; column++) { CFX_PointF pos = matrix.Transform( CFX_PointF(static_cast<float>(column), static_cast<float>(row))); if (pos.x < xmin || pos.x > xmax || pos.y < ymin || pos.y > ymax) continue; - float input[] = {pos.x, pos.y}; - int offset = 0; + float input[2] = {pos.x, pos.y}; + pdfium::span<float> result_span = pdfium::make_span(result_array); for (const auto& func : funcs) { - if (func) { - int nresults; - if (func->Call(input, 2, &result_array[offset], &nresults)) - offset += nresults; - } + if (!func) + continue; + absl::optional<uint32_t> nresults = func->Call(input, result_span); + if (nresults.has_value()) + result_span = result_span.subspan(nresults.value()); } - float R = 0.0f; float G = 0.0f; float B = 0.0f; - pCS->GetRGB(result_array.data(), &R, &G, &B); - dib_buf[column] = FXARGB_TODIB(ArgbEncode( - alpha, (int32_t)(R * 255), (int32_t)(G * 255), (int32_t)(B * 255))); + pCS->GetRGB(result_array, &R, &G, &B); + dib_buf[column] = ArgbEncode(alpha, static_cast<int32_t>(R * 255), + static_cast<int32_t>(G * 255), + static_cast<int32_t>(B * 255)); } } } @@ -340,9 +344,8 @@ void DrawGouraud(const RetainPtr<CFX_DIBitmap>& pBitmap, if (min_y == max_y) return; - int min_yi = std::max(static_cast<int>(floor(min_y)), 0); - int max_yi = static_cast<int>(ceil(max_y)); - + int min_yi = std::max(static_cast<int>(floorf(min_y)), 0); + int max_yi = static_cast<int>(ceilf(max_y)); if (max_yi >= pBitmap->GetHeight()) max_yi = pBitmap->GetHeight() - 1; @@ -371,40 +374,42 @@ void DrawGouraud(const RetainPtr<CFX_DIBitmap>& pBitmap, if (nIntersects != 2) continue; - int min_x, max_x, start_index, end_index; + int min_x; + int max_x; + int start_index; + int end_index; if (inter_x[0] < inter_x[1]) { - min_x = (int)floor(inter_x[0]); - max_x = (int)ceil(inter_x[1]); + min_x = static_cast<int>(floorf(inter_x[0])); + max_x = static_cast<int>(ceilf(inter_x[1])); start_index = 0; end_index = 1; } else { - min_x = (int)floor(inter_x[1]); - max_x = (int)ceil(inter_x[0]); + min_x = static_cast<int>(floorf(inter_x[1])); + max_x = static_cast<int>(ceilf(inter_x[0])); start_index = 1; end_index = 0; } - int start_x = std::max(min_x, 0); - int end_x = max_x; - if (end_x > pBitmap->GetWidth()) - end_x = pBitmap->GetWidth(); - - uint8_t* dib_buf = - pBitmap->GetBuffer() + y * pBitmap->GetPitch() + start_x * 4; + int start_x = pdfium::clamp(min_x, 0, pBitmap->GetWidth()); + int end_x = pdfium::clamp(max_x, 0, pBitmap->GetWidth()); float r_unit = (r[end_index] - r[start_index]) / (max_x - min_x); float g_unit = (g[end_index] - g[start_index]) / (max_x - min_x); float b_unit = (b[end_index] - b[start_index]) / (max_x - min_x); - float R = r[start_index] + (start_x - min_x) * r_unit; - float G = g[start_index] + (start_x - min_x) * g_unit; - float B = b[start_index] + (start_x - min_x) * b_unit; + float r_result = r[start_index] + (start_x - min_x) * r_unit; + float g_result = g[start_index] + (start_x - min_x) * g_unit; + float b_result = b[start_index] + (start_x - min_x) * b_unit; + pdfium::span<uint8_t> dib_span = + pBitmap->GetWritableScanline(y).subspan(start_x * 4); + for (int x = start_x; x < end_x; x++) { - R += r_unit; - G += g_unit; - B += b_unit; - FXARGB_SETDIB(dib_buf, - ArgbEncode(alpha, (int32_t)(R * 255), (int32_t)(G * 255), - (int32_t)(B * 255))); - dib_buf += 4; + uint8_t* dib_buf = dib_span.data(); + r_result += r_unit; + g_result += g_unit; + b_result += b_unit; + FXARGB_SETDIB(dib_buf, ArgbEncode(alpha, static_cast<int>(r_result * 255), + static_cast<int>(g_result * 255), + static_cast<int>(b_result * 255))); + dib_span = dib_span.subspan(4); } } } @@ -412,21 +417,19 @@ void DrawGouraud(const RetainPtr<CFX_DIBitmap>& pBitmap, void DrawFreeGouraudShading( const RetainPtr<CFX_DIBitmap>& pBitmap, const CFX_Matrix& mtObject2Bitmap, - const CPDF_Stream* pShadingStream, + RetainPtr<const CPDF_Stream> pShadingStream, const std::vector<std::unique_ptr<CPDF_Function>>& funcs, - const RetainPtr<CPDF_ColorSpace>& pCS, + RetainPtr<CPDF_ColorSpace> pCS, int alpha) { - ASSERT(pBitmap->GetFormat() == FXDIB_Argb); + DCHECK_EQ(pBitmap->GetFormat(), FXDIB_Format::kArgb); CPDF_MeshStream stream(kFreeFormGouraudTriangleMeshShading, funcs, - pShadingStream, pCS); + std::move(pShadingStream), std::move(pCS)); if (!stream.Load()) return; CPDF_MeshVertex triangle[3]; - memset(triangle, 0, sizeof(triangle)); - - while (!stream.BitStream()->IsEOF()) { + while (!stream.IsEOF()) { CPDF_MeshVertex vertex; uint32_t flag; if (!stream.ReadVertex(mtObject2Bitmap, &vertex, &flag)) @@ -434,9 +437,9 @@ void DrawFreeGouraudShading( if (flag == 0) { triangle[0] = vertex; - for (int j = 1; j < 3; j++) { - uint32_t tflag; - if (!stream.ReadVertex(mtObject2Bitmap, &triangle[j], &tflag)) + for (int i = 1; i < 3; ++i) { + uint32_t dummy_flag; + if (!stream.ReadVertex(mtObject2Bitmap, &triangle[i], &dummy_flag)) return; } } else { @@ -453,18 +456,18 @@ void DrawFreeGouraudShading( void DrawLatticeGouraudShading( const RetainPtr<CFX_DIBitmap>& pBitmap, const CFX_Matrix& mtObject2Bitmap, - const CPDF_Stream* pShadingStream, + RetainPtr<const CPDF_Stream> pShadingStream, const std::vector<std::unique_ptr<CPDF_Function>>& funcs, - const RetainPtr<CPDF_ColorSpace>& pCS, + RetainPtr<CPDF_ColorSpace> pCS, int alpha) { - ASSERT(pBitmap->GetFormat() == FXDIB_Argb); + DCHECK_EQ(pBitmap->GetFormat(), FXDIB_Format::kArgb); int row_verts = pShadingStream->GetDict()->GetIntegerFor("VerticesPerRow"); if (row_verts < 2) return; CPDF_MeshStream stream(kLatticeFormGouraudTriangleMeshShading, funcs, - pShadingStream, pCS); + std::move(pShadingStream), std::move(pCS)); if (!stream.Load()) return; @@ -474,7 +477,7 @@ void DrawLatticeGouraudShading( return; int last_index = 0; - while (1) { + while (true) { vertices[1 - last_index] = stream.ReadVertexRow(mtObject2Bitmap, row_verts); if (vertices[1 - last_index].empty()) return; @@ -492,123 +495,127 @@ void DrawLatticeGouraudShading( } } -struct Coon_BezierCoeff { - float a, b, c, d; - void FromPoints(float p0, float p1, float p2, float p3) { +struct CoonBezierCoeff { + void InitFromPoints(float p0, float p1, float p2, float p3) { a = -p0 + 3 * p1 - 3 * p2 + p3; b = 3 * p0 - 6 * p1 + 3 * p2; c = -3 * p0 + 3 * p1; d = p0; } - Coon_BezierCoeff first_half() { - Coon_BezierCoeff result; + + void InitFromBezierInterpolation(const CoonBezierCoeff& C1, + const CoonBezierCoeff& C2, + const CoonBezierCoeff& D1, + const CoonBezierCoeff& D2) { + a = (D1.a + D2.a) / 2; + b = (D1.b + D2.b) / 2; + c = (D1.c + D2.c) / 2 - (C1.a / 8 + C1.b / 4 + C1.c / 2) + + (C2.a / 8 + C2.b / 4) + (-C1.d + D2.d) / 2 - (C2.a + C2.b) / 2; + d = C1.a / 8 + C1.b / 4 + C1.c / 2 + C1.d; + } + + CoonBezierCoeff first_half() const { + CoonBezierCoeff result; result.a = a / 8; result.b = b / 4; result.c = c / 2; result.d = d; return result; } - Coon_BezierCoeff second_half() { - Coon_BezierCoeff result; + + CoonBezierCoeff second_half() const { + CoonBezierCoeff result; result.a = a / 8; result.b = 3 * a / 8 + b / 4; result.c = 3 * a / 8 + b / 2 + c / 2; result.d = a / 8 + b / 4 + c / 2 + d; return result; } - void GetPoints(float p[4]) { + + void GetPoints(float p[4]) const { p[0] = d; p[1] = c / 3 + p[0]; p[2] = b / 3 - p[0] + 2 * p[1]; p[3] = a + p[0] - 3 * p[1] + 3 * p[2]; } - void GetPointsReverse(float p[4]) { - p[3] = d; - p[2] = c / 3 + p[3]; - p[1] = b / 3 - p[3] + 2 * p[2]; - p[0] = a + p[3] - 3 * p[2] + 3 * p[1]; - } - void BezierInterpol(Coon_BezierCoeff& C1, - Coon_BezierCoeff& C2, - Coon_BezierCoeff& D1, - Coon_BezierCoeff& D2) { - a = (D1.a + D2.a) / 2; - b = (D1.b + D2.b) / 2; - c = (D1.c + D2.c) / 2 - (C1.a / 8 + C1.b / 4 + C1.c / 2) + - (C2.a / 8 + C2.b / 4) + (-C1.d + D2.d) / 2 - (C2.a + C2.b) / 2; - d = C1.a / 8 + C1.b / 4 + C1.c / 2 + C1.d; - } - float Distance() { + + float Distance() const { float dis = a + b + c; return dis < 0 ? -dis : dis; } + + float a; + float b; + float c; + float d; }; -struct Coon_Bezier { - Coon_BezierCoeff x, y; - void FromPoints(float x0, - float y0, - float x1, - float y1, - float x2, - float y2, - float x3, - float y3) { - x.FromPoints(x0, x1, x2, x3); - y.FromPoints(y0, y1, y2, y3); - } - - Coon_Bezier first_half() { - Coon_Bezier result; +struct CoonBezier { + void InitFromPoints(float x0, + float y0, + float x1, + float y1, + float x2, + float y2, + float x3, + float y3) { + x.InitFromPoints(x0, x1, x2, x3); + y.InitFromPoints(y0, y1, y2, y3); + } + + void InitFromBezierInterpolation(const CoonBezier& C1, + const CoonBezier& C2, + const CoonBezier& D1, + const CoonBezier& D2) { + x.InitFromBezierInterpolation(C1.x, C2.x, D1.x, D2.x); + y.InitFromBezierInterpolation(C1.y, C2.y, D1.y, D2.y); + } + + CoonBezier first_half() const { + CoonBezier result; result.x = x.first_half(); result.y = y.first_half(); return result; } - Coon_Bezier second_half() { - Coon_Bezier result; + CoonBezier second_half() const { + CoonBezier result; result.x = x.second_half(); result.y = y.second_half(); return result; } - void BezierInterpol(Coon_Bezier& C1, - Coon_Bezier& C2, - Coon_Bezier& D1, - Coon_Bezier& D2) { - x.BezierInterpol(C1.x, C2.x, D1.x, D2.x); - y.BezierInterpol(C1.y, C2.y, D1.y, D2.y); - } - - void GetPoints(std::vector<FX_PATHPOINT>& pPoints, size_t start_idx) { - float p[4]; - int i; - x.GetPoints(p); - for (i = 0; i < 4; i++) - pPoints[start_idx + i].m_Point.x = p[i]; - - y.GetPoints(p); - for (i = 0; i < 4; i++) - pPoints[start_idx + i].m_Point.y = p[i]; + void GetPoints(pdfium::span<CFX_Path::Point> path_points) const { + constexpr size_t kPointsCount = 4; + float points_x[kPointsCount]; + float points_y[kPointsCount]; + x.GetPoints(points_x); + y.GetPoints(points_y); + for (size_t i = 0; i < kPointsCount; ++i) + path_points[i].m_Point = {points_x[i], points_y[i]}; + } + + void GetPointsReverse(pdfium::span<CFX_Path::Point> path_points) const { + constexpr size_t kPointsCount = 4; + float points_x[kPointsCount]; + float points_y[kPointsCount]; + x.GetPoints(points_x); + y.GetPoints(points_y); + for (size_t i = 0; i < kPointsCount; ++i) { + size_t reverse_index = kPointsCount - i - 1; + path_points[i].m_Point = {points_x[reverse_index], + points_y[reverse_index]}; + } } - void GetPointsReverse(std::vector<FX_PATHPOINT>& pPoints, size_t start_idx) { - float p[4]; - int i; - x.GetPointsReverse(p); - for (i = 0; i < 4; i++) - pPoints[i + start_idx].m_Point.x = p[i]; + float Distance() const { return x.Distance() + y.Distance(); } - y.GetPointsReverse(p); - for (i = 0; i < 4; i++) - pPoints[i + start_idx].m_Point.y = p[i]; - } - - float Distance() { return x.Distance() + y.Distance(); } + CoonBezierCoeff x; + CoonBezierCoeff y; }; int Interpolate(int p1, int p2, int delta1, int delta2, bool* overflow) { - pdfium::base::CheckedNumeric<int> p = p2; + FX_SAFE_INT32 p = p2; p -= p1; p *= delta1; p /= delta2; @@ -632,15 +639,11 @@ int BiInterpolImpl(int c0, return Interpolate(x1, x2, y, y_scale, overflow); } -struct Coon_Color { - Coon_Color() { memset(comp, 0, sizeof(int) * 3); } +struct CoonColor { + CoonColor() = default; // Returns true if successful, false if overflow detected. - bool BiInterpol(Coon_Color colors[4], - int x, - int y, - int x_scale, - int y_scale) { + bool BiInterpol(CoonColor colors[4], int x, int y, int x_scale, int y_scale) { bool overflow = false; for (int i = 0; i < 3; i++) { comp[i] = BiInterpolImpl(colors[0].comp[i], colors[1].comp[i], @@ -650,27 +653,28 @@ struct Coon_Color { return !overflow; } - int Distance(Coon_Color& o) { + int Distance(const CoonColor& o) const { return std::max({abs(comp[0] - o.comp[0]), abs(comp[1] - o.comp[1]), abs(comp[2] - o.comp[2])}); } - int comp[3]; + int comp[3] = {}; }; -#define COONCOLOR_THRESHOLD 4 -struct CPDF_PatchDrawer { +struct PatchDrawer { + static constexpr int kCoonColorThreshold = 4; + void Draw(int x_scale, int y_scale, int left, int bottom, - Coon_Bezier C1, - Coon_Bezier C2, - Coon_Bezier D1, - Coon_Bezier D2) { + CoonBezier C1, + CoonBezier C2, + CoonBezier D1, + CoonBezier D2) { bool bSmall = C1.Distance() < 2 && C2.Distance() < 2 && D1.Distance() < 2 && D2.Distance() < 2; - Coon_Color div_colors[4]; + CoonColor div_colors[4]; int d_bottom = 0; int d_left = 0; int d_top = 0; @@ -699,35 +703,37 @@ struct CPDF_PatchDrawer { } if (bSmall || - (d_bottom < COONCOLOR_THRESHOLD && d_left < COONCOLOR_THRESHOLD && - d_top < COONCOLOR_THRESHOLD && d_right < COONCOLOR_THRESHOLD)) { - std::vector<FX_PATHPOINT>& pPoints = path.GetPoints(); - C1.GetPoints(pPoints, 0); - D2.GetPoints(pPoints, 3); - C2.GetPointsReverse(pPoints, 6); - D1.GetPointsReverse(pPoints, 9); - int fillFlags = FXFILL_WINDING | FXFILL_FULLCOVER; + (d_bottom < kCoonColorThreshold && d_left < kCoonColorThreshold && + d_top < kCoonColorThreshold && d_right < kCoonColorThreshold)) { + pdfium::span<CFX_Path::Point> points = path.GetPoints(); + C1.GetPoints(points.subspan(0, 4)); + D2.GetPoints(points.subspan(3, 4)); + C2.GetPointsReverse(points.subspan(6, 4)); + D1.GetPointsReverse(points.subspan(9, 4)); + CFX_FillRenderOptions fill_options( + CFX_FillRenderOptions::WindingOptions()); + fill_options.full_cover = true; if (bNoPathSmooth) - fillFlags |= FXFILL_NOPATHSMOOTH; + fill_options.aliased_path = true; pDevice->DrawPath( - &path, nullptr, nullptr, + path, nullptr, nullptr, ArgbEncode(alpha, div_colors[0].comp[0], div_colors[0].comp[1], div_colors[0].comp[2]), - 0, fillFlags); + 0, fill_options); } else { - if (d_bottom < COONCOLOR_THRESHOLD && d_top < COONCOLOR_THRESHOLD) { - Coon_Bezier m1; - m1.BezierInterpol(D1, D2, C1, C2); + if (d_bottom < kCoonColorThreshold && d_top < kCoonColorThreshold) { + CoonBezier m1; + m1.InitFromBezierInterpolation(D1, D2, C1, C2); y_scale *= 2; bottom *= 2; Draw(x_scale, y_scale, left, bottom, C1, m1, D1.first_half(), D2.first_half()); Draw(x_scale, y_scale, left, bottom + 1, m1, C2, D1.second_half(), D2.second_half()); - } else if (d_left < COONCOLOR_THRESHOLD && - d_right < COONCOLOR_THRESHOLD) { - Coon_Bezier m2; - m2.BezierInterpol(C1, C2, D1, D2); + } else if (d_left < kCoonColorThreshold && + d_right < kCoonColorThreshold) { + CoonBezier m2; + m2.InitFromBezierInterpolation(C1, C2, D1, D2); x_scale *= 2; left *= 2; Draw(x_scale, y_scale, left, bottom, C1.first_half(), C2.first_half(), @@ -735,13 +741,14 @@ struct CPDF_PatchDrawer { Draw(x_scale, y_scale, left + 1, bottom, C1.second_half(), C2.second_half(), m2, D2); } else { - Coon_Bezier m1, m2; - m1.BezierInterpol(D1, D2, C1, C2); - m2.BezierInterpol(C1, C2, D1, D2); - Coon_Bezier m1f = m1.first_half(); - Coon_Bezier m1s = m1.second_half(); - Coon_Bezier m2f = m2.first_half(); - Coon_Bezier m2s = m2.second_half(); + CoonBezier m1; + CoonBezier m2; + m1.InitFromBezierInterpolation(D1, D2, C1, C2); + m2.InitFromBezierInterpolation(C1, C2, D1, D2); + CoonBezier m1f = m1.first_half(); + CoonBezier m1s = m1.second_half(); + CoonBezier m2f = m2.first_half(); + CoonBezier m2s = m2.second_half(); x_scale *= 2; y_scale *= 2; left *= 2; @@ -759,49 +766,54 @@ struct CPDF_PatchDrawer { } int max_delta; - CFX_PathData path; + CFX_Path path; CFX_RenderDevice* pDevice; int bNoPathSmooth; int alpha; - Coon_Color patch_colors[4]; + CoonColor patch_colors[4]; }; void DrawCoonPatchMeshes( ShadingType type, const RetainPtr<CFX_DIBitmap>& pBitmap, const CFX_Matrix& mtObject2Bitmap, - const CPDF_Stream* pShadingStream, + RetainPtr<const CPDF_Stream> pShadingStream, const std::vector<std::unique_ptr<CPDF_Function>>& funcs, - const RetainPtr<CPDF_ColorSpace>& pCS, + RetainPtr<CPDF_ColorSpace> pCS, bool bNoPathSmooth, int alpha) { - ASSERT(pBitmap->GetFormat() == FXDIB_Argb); - ASSERT(type == kCoonsPatchMeshShading || + DCHECK_EQ(pBitmap->GetFormat(), FXDIB_Format::kArgb); + DCHECK(type == kCoonsPatchMeshShading || type == kTensorProductPatchMeshShading); CFX_DefaultRenderDevice device; - device.Attach(pBitmap, false, nullptr, false); - CPDF_MeshStream stream(type, funcs, pShadingStream, pCS); + device.Attach(pBitmap); + + CPDF_MeshStream stream(type, funcs, std::move(pShadingStream), + std::move(pCS)); if (!stream.Load()) return; - CPDF_PatchDrawer patch; + PatchDrawer patch; patch.alpha = alpha; patch.pDevice = &device; patch.bNoPathSmooth = bNoPathSmooth; for (int i = 0; i < 13; i++) { - patch.path.AppendPoint( - CFX_PointF(), i == 0 ? FXPT_TYPE::MoveTo : FXPT_TYPE::BezierTo, false); + patch.path.AppendPoint(CFX_PointF(), i == 0 + ? CFX_Path::Point::Type::kMove + : CFX_Path::Point::Type::kBezier); } CFX_PointF coords[16]; int point_count = type == kTensorProductPatchMeshShading ? 16 : 12; - while (!stream.BitStream()->IsEOF()) { + while (!stream.IsEOF()) { if (!stream.CanReadFlag()) break; uint32_t flag = stream.ReadFlag(); - int iStartPoint = 0, iStartColor = 0, i = 0; + int iStartPoint = 0; + int iStartColor = 0; + int i = 0; if (flag) { iStartPoint = 4; iStartColor = 2; @@ -809,11 +821,12 @@ void DrawCoonPatchMeshes( for (i = 0; i < 4; i++) { tempCoords[i] = coords[(flag * 3 + i) % 12]; } - memcpy(coords, tempCoords, sizeof(tempCoords)); - Coon_Color tempColors[2]; - tempColors[0] = patch.patch_colors[flag]; - tempColors[1] = patch.patch_colors[(flag + 1) % 4]; - memcpy(patch.patch_colors, tempColors, sizeof(Coon_Color) * 2); + fxcrt::spancpy(pdfium::make_span(coords), pdfium::make_span(tempCoords)); + CoonColor tempColors[2] = { + tempColors[0] = patch.patch_colors[flag], + tempColors[1] = patch.patch_colors[(flag + 1) % 4]}; + fxcrt::spancpy(pdfium::make_span(patch.patch_colors), + pdfium::make_span(tempColors)); } for (i = iStartPoint; i < point_count; i++) { if (!stream.CanReadCoords()) @@ -830,24 +843,28 @@ void DrawCoonPatchMeshes( float b; std::tie(r, g, b) = stream.ReadColor(); - patch.patch_colors[i].comp[0] = (int32_t)(r * 255); - patch.patch_colors[i].comp[1] = (int32_t)(g * 255); - patch.patch_colors[i].comp[2] = (int32_t)(b * 255); + patch.patch_colors[i].comp[0] = static_cast<int32_t>(r * 255); + patch.patch_colors[i].comp[1] = static_cast<int32_t>(g * 255); + patch.patch_colors[i].comp[2] = static_cast<int32_t>(b * 255); } - CFX_FloatRect bbox = CFX_FloatRect::GetBBox(coords, point_count); + CFX_FloatRect bbox = + CFX_FloatRect::GetBBox(pdfium::make_span(coords).first(point_count)); if (bbox.right <= 0 || bbox.left >= (float)pBitmap->GetWidth() || bbox.top <= 0 || bbox.bottom >= (float)pBitmap->GetHeight()) { continue; } - Coon_Bezier C1, C2, D1, D2; - C1.FromPoints(coords[0].x, coords[0].y, coords[11].x, coords[11].y, - coords[10].x, coords[10].y, coords[9].x, coords[9].y); - C2.FromPoints(coords[3].x, coords[3].y, coords[4].x, coords[4].y, - coords[5].x, coords[5].y, coords[6].x, coords[6].y); - D1.FromPoints(coords[0].x, coords[0].y, coords[1].x, coords[1].y, - coords[2].x, coords[2].y, coords[3].x, coords[3].y); - D2.FromPoints(coords[9].x, coords[9].y, coords[8].x, coords[8].y, - coords[7].x, coords[7].y, coords[6].x, coords[6].y); + CoonBezier C1; + CoonBezier C2; + CoonBezier D1; + CoonBezier D2; + C1.InitFromPoints(coords[0].x, coords[0].y, coords[11].x, coords[11].y, + coords[10].x, coords[10].y, coords[9].x, coords[9].y); + C2.InitFromPoints(coords[3].x, coords[3].y, coords[4].x, coords[4].y, + coords[5].x, coords[5].y, coords[6].x, coords[6].y); + D1.InitFromPoints(coords[0].x, coords[0].y, coords[1].x, coords[1].y, + coords[2].x, coords[2].y, coords[3].x, coords[3].y); + D2.InitFromPoints(coords[9].x, coords[9].y, coords[8].x, coords[8].y, + coords[7].x, coords[7].y, coords[6].x, coords[6].y); patch.Draw(1, 1, 0, 0, C1, C2, D1, D2); } } @@ -863,25 +880,26 @@ void CPDF_RenderShading::Draw(CFX_RenderDevice* pDevice, const FX_RECT& clip_rect, int alpha, const CPDF_RenderOptions& options) { - const auto& funcs = pPattern->GetFuncs(); - const CPDF_Dictionary* pDict = pPattern->GetShadingObject()->GetDict(); RetainPtr<CPDF_ColorSpace> pColorSpace = pPattern->GetCS(); if (!pColorSpace) return; FX_ARGB background = 0; + RetainPtr<const CPDF_Dictionary> pDict = + pPattern->GetShadingObject()->GetDict(); if (!pPattern->IsShadingObject() && pDict->KeyExist("Background")) { - const CPDF_Array* pBackColor = pDict->GetArrayFor("Background"); + RetainPtr<const CPDF_Array> pBackColor = pDict->GetArrayFor("Background"); if (pBackColor && pBackColor->size() >= pColorSpace->CountComponents()) { - std::vector<float> comps = - ReadArrayElementsToVector(pBackColor, pColorSpace->CountComponents()); + std::vector<float> comps = ReadArrayElementsToVector( + pBackColor.Get(), pColorSpace->CountComponents()); float R = 0.0f; float G = 0.0f; float B = 0.0f; - pColorSpace->GetRGB(comps.data(), &R, &G, &B); - background = ArgbEncode(255, (int32_t)(R * 255), (int32_t)(G * 255), - (int32_t)(B * 255)); + pColorSpace->GetRGB(comps, &R, &G, &B); + background = ArgbEncode(255, static_cast<int32_t>(R * 255), + static_cast<int32_t>(G * 255), + static_cast<int32_t>(B * 255)); } } FX_RECT clip_rect_bbox = clip_rect; @@ -891,64 +909,80 @@ void CPDF_RenderShading::Draw(CFX_RenderDevice* pDevice, } bool bAlphaMode = options.ColorModeIs(CPDF_RenderOptions::kAlpha); if (pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_SHADING && - pDevice->GetDeviceDriver()->DrawShading( - pPattern, &mtMatrix, clip_rect_bbox, alpha, bAlphaMode)) { + pDevice->DrawShading(pPattern, &mtMatrix, clip_rect_bbox, alpha, + bAlphaMode)) { return; } CPDF_DeviceBuffer buffer(pContext, pDevice, clip_rect_bbox, pCurObj, 150); if (!buffer.Initialize()) return; - CFX_Matrix FinalMatrix = mtMatrix * buffer.GetMatrix(); RetainPtr<CFX_DIBitmap> pBitmap = buffer.GetBitmap(); - if (!pBitmap->GetBuffer()) + if (pBitmap->GetBuffer().empty()) return; - pBitmap->Clear(background); + if (background != 0) { + pBitmap->Clear(background); + } + const CFX_Matrix final_matrix = mtMatrix * buffer.GetMatrix(); + const auto& funcs = pPattern->GetFuncs(); switch (pPattern->GetShadingType()) { case kInvalidShading: case kMaxShading: return; case kFunctionBasedShading: - DrawFuncShading(pBitmap, FinalMatrix, pDict, funcs, pColorSpace, alpha); + DrawFuncShading(pBitmap, final_matrix, pDict.Get(), funcs, pColorSpace, + alpha); break; case kAxialShading: - DrawAxialShading(pBitmap, FinalMatrix, pDict, funcs, pColorSpace, alpha); + DrawAxialShading(pBitmap, final_matrix, pDict.Get(), funcs, pColorSpace, + alpha); break; case kRadialShading: - DrawRadialShading(pBitmap, FinalMatrix, pDict, funcs, pColorSpace, alpha); + DrawRadialShading(pBitmap, final_matrix, pDict.Get(), funcs, pColorSpace, + alpha); break; case kFreeFormGouraudTriangleMeshShading: { // The shading object can be a stream or a dictionary. We do not handle // the case of dictionary at the moment. - if (const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) { - DrawFreeGouraudShading(pBitmap, FinalMatrix, pStream, funcs, + RetainPtr<const CPDF_Stream> pStream = + ToStream(pPattern->GetShadingObject()); + if (pStream) { + DrawFreeGouraudShading(pBitmap, final_matrix, std::move(pStream), funcs, pColorSpace, alpha); } - } break; + break; + } case kLatticeFormGouraudTriangleMeshShading: { // The shading object can be a stream or a dictionary. We do not handle // the case of dictionary at the moment. - if (const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) { - DrawLatticeGouraudShading(pBitmap, FinalMatrix, pStream, funcs, - pColorSpace, alpha); + RetainPtr<const CPDF_Stream> pStream = + ToStream(pPattern->GetShadingObject()); + if (pStream) { + DrawLatticeGouraudShading(pBitmap, final_matrix, std::move(pStream), + funcs, pColorSpace, alpha); } - } break; + break; + } case kCoonsPatchMeshShading: case kTensorProductPatchMeshShading: { // The shading object can be a stream or a dictionary. We do not handle // the case of dictionary at the moment. - if (const CPDF_Stream* pStream = ToStream(pPattern->GetShadingObject())) { - DrawCoonPatchMeshes(pPattern->GetShadingType(), pBitmap, FinalMatrix, - pStream, funcs, pColorSpace, + RetainPtr<const CPDF_Stream> pStream = + ToStream(pPattern->GetShadingObject()); + if (pStream) { + DrawCoonPatchMeshes(pPattern->GetShadingType(), pBitmap, final_matrix, + std::move(pStream), funcs, pColorSpace, options.GetOptions().bNoPathSmooth, alpha); } - } break; + break; + } } if (bAlphaMode) - pBitmap->LoadChannelFromAlpha(FXDIB_Red, pBitmap); + pBitmap->SetRedFromBitmap(pBitmap); if (options.ColorModeIs(CPDF_RenderOptions::kGray)) pBitmap->ConvertColorScale(0, 0xffffff); + buffer.OutputToDevice(); } |