Add CFX_DIBBase::GetPaletteSpan(). Use it in place of GetPaletteData() in many places. Change-Id: Ie08e1df7e8a12bfce7900b515e29b9465f5fe0fa Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/75234 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp index 65c7eeb..bedfd4d 100644 --- a/core/fpdfapi/page/cpdf_dib.cpp +++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -1120,12 +1120,13 @@ uint8_t* pDestPixel = m_pMaskedLine.get(); const uint8_t* pSrcPixel = m_pLineBuf.get(); + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int col = 0; col < m_Width; col++) { uint8_t index = *pSrcPixel++; if (HasPalette()) { - *pDestPixel++ = FXARGB_B(GetPaletteData()[index]); - *pDestPixel++ = FXARGB_G(GetPaletteData()[index]); - *pDestPixel++ = FXARGB_R(GetPaletteData()[index]); + *pDestPixel++ = FXARGB_B(palette[index]); + *pDestPixel++ = FXARGB_G(palette[index]); + *pDestPixel++ = FXARGB_R(palette[index]); } else { *pDestPixel++ = index; *pDestPixel++ = index; @@ -1261,8 +1262,8 @@ reset_argb = 0xFFFFFFFF; } } else if (HasPalette() && dest_Bpp != 1) { - reset_argb = GetPaletteData()[0]; - set_argb = GetPaletteData()[1]; + reset_argb = GetPaletteSpan()[0]; + set_argb = GetPaletteSpan()[1]; } for (int i = 0; i < clip_width; i++) { uint32_t src_x = (clip_left + i) * src_width / dest_width; @@ -1306,6 +1307,7 @@ pSrcLine = m_pLineBuf.get(); } if (m_bColorKey) { + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int i = 0; i < clip_width; i++) { uint32_t src_x = (clip_left + i) * src_width / dest_width; if (bFlipX) { @@ -1315,9 +1317,9 @@ uint8_t* pDestPixel = dest_scan + i * 4; uint8_t index = pSrcLine[src_x]; if (HasPalette()) { - *pDestPixel++ = FXARGB_B(GetPaletteData()[index]); - *pDestPixel++ = FXARGB_G(GetPaletteData()[index]); - *pDestPixel++ = FXARGB_R(GetPaletteData()[index]); + *pDestPixel++ = FXARGB_B(palette[index]); + *pDestPixel++ = FXARGB_G(palette[index]); + *pDestPixel++ = FXARGB_R(palette[index]); } else { *pDestPixel++ = index; *pDestPixel++ = index; @@ -1330,6 +1332,8 @@ } return; } + + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int i = 0; i < clip_width; i++) { uint32_t src_x = (clip_left + i) * src_width / dest_width; if (bFlipX) @@ -1340,7 +1344,7 @@ dest_scan[i] = index; } else { int dest_pos = i * dest_Bpp; - FX_ARGB argb = GetPaletteData()[index]; + FX_ARGB argb = palette[index]; dest_scan[dest_pos] = FXARGB_B(argb); dest_scan[dest_pos + 1] = FXARGB_G(argb); dest_scan[dest_pos + 2] = FXARGB_R(argb); @@ -1461,11 +1465,11 @@ uint32_t CPDF_DIB::Get1BitSetValue() const { if (m_CompData[0].m_ColorKeyMax == 1) return 0x00000000; - return HasPalette() ? GetPaletteData()[1] : 0xFFFFFFFF; + return HasPalette() ? GetPaletteSpan()[1] : 0xFFFFFFFF; } uint32_t CPDF_DIB::Get1BitResetValue() const { if (m_CompData[0].m_ColorKeyMin == 0) return 0x00000000; - return HasPalette() ? GetPaletteData()[0] : 0xFF000000; + return HasPalette() ? GetPaletteSpan()[0] : 0xFF000000; }
diff --git a/core/fpdfapi/page/cpdf_transferfuncdib.cpp b/core/fpdfapi/page/cpdf_transferfuncdib.cpp index 4f03127..0bd1c59 100644 --- a/core/fpdfapi/page/cpdf_transferfuncdib.cpp +++ b/core/fpdfapi/page/cpdf_transferfuncdib.cpp
@@ -87,11 +87,11 @@ break; } case FXDIB_8bppRgb: { - const FX_ARGB* pPal = m_pSrc->GetPaletteData(); + pdfium::span<const uint32_t> src_palette = m_pSrc->GetPaletteSpan(); int index = 0; for (int i = 0; i < m_Width; i++) { - if (pPal) { - FX_ARGB src_argb = pPal[*src_buf]; + if (m_pSrc->HasPalette()) { + FX_ARGB src_argb = src_palette[*src_buf]; (*dest_buf)[index++] = m_RampB[FXARGB_R(src_argb)]; (*dest_buf)[index++] = m_RampG[FXARGB_G(src_argb)]; (*dest_buf)[index++] = m_RampR[FXARGB_B(src_argb)];
diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp index d4db32f..6bbf5ec 100644 --- a/core/fxge/agg/fx_agg_driver.cpp +++ b/core/fxge/agg/fx_agg_driver.cpp
@@ -980,7 +980,7 @@ int index = 0; if (m_pDevice->HasPalette()) { for (int i = 0; i < 2; i++) { - if (m_pDevice->GetPaletteData()[i] == m_Color) + if (m_pDevice->GetPaletteSpan()[i] == m_Color) index = i; } } else {
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp index b50ec24..bb31b80 100644 --- a/core/fxge/dib/cfx_dibbase.cpp +++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -143,7 +143,7 @@ const RetainPtr<CFX_DIBBase>& pSrcBitmap, int src_left, int src_top) { - const uint32_t* src_plt = pSrcBitmap->GetPaletteData(); + pdfium::span<const uint32_t> src_palette = pSrcBitmap->GetPaletteSpan(); uint8_t gray[2]; uint8_t reset_r; uint8_t reset_g; @@ -153,18 +153,18 @@ uint8_t set_b; if (pSrcBitmap->IsCmykImage()) { std::tie(reset_r, reset_g, reset_b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), - FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0])); + FXSYS_GetCValue(src_palette[0]), FXSYS_GetMValue(src_palette[0]), + FXSYS_GetYValue(src_palette[0]), FXSYS_GetKValue(src_palette[0])); std::tie(set_r, set_g, set_b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), - FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1])); + FXSYS_GetCValue(src_palette[1]), FXSYS_GetMValue(src_palette[1]), + FXSYS_GetYValue(src_palette[1]), FXSYS_GetKValue(src_palette[1])); } else { - reset_r = FXARGB_R(src_plt[0]); - reset_g = FXARGB_G(src_plt[0]); - reset_b = FXARGB_B(src_plt[0]); - set_r = FXARGB_R(src_plt[1]); - set_g = FXARGB_G(src_plt[1]); - set_b = FXARGB_B(src_plt[1]); + reset_r = FXARGB_R(src_palette[0]); + reset_g = FXARGB_G(src_palette[0]); + reset_b = FXARGB_B(src_palette[0]); + set_r = FXARGB_R(src_palette[1]); + set_g = FXARGB_G(src_palette[1]); + set_b = FXARGB_B(src_palette[1]); } gray[0] = FXRGB2GRAY(reset_r, reset_g, reset_b); gray[1] = FXRGB2GRAY(set_r, set_g, set_b); @@ -188,7 +188,7 @@ const RetainPtr<CFX_DIBBase>& pSrcBitmap, int src_left, int src_top) { - const uint32_t* src_plt = pSrcBitmap->GetPaletteData(); + pdfium::span<const uint32_t> src_palette = pSrcBitmap->GetPaletteSpan(); uint8_t gray[256]; if (pSrcBitmap->IsCmykImage()) { uint8_t r; @@ -196,14 +196,14 @@ uint8_t b; for (size_t i = 0; i < pdfium::size(gray); ++i) { std::tie(r, g, b) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), - FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); + FXSYS_GetCValue(src_palette[i]), FXSYS_GetMValue(src_palette[i]), + FXSYS_GetYValue(src_palette[i]), FXSYS_GetKValue(src_palette[i])); gray[i] = FXRGB2GRAY(r, g, b); } } else { for (size_t i = 0; i < pdfium::size(gray); ++i) { - gray[i] = FXRGB2GRAY(FXARGB_R(src_plt[i]), FXARGB_G(src_plt[i]), - FXARGB_B(src_plt[i])); + gray[i] = FXRGB2GRAY(FXARGB_R(src_palette[i]), FXARGB_G(src_palette[i]), + FXARGB_B(src_palette[i])); } } @@ -424,28 +424,28 @@ int src_left, int src_top) { int comps = GetCompsFromFormat(dest_format); - const uint32_t* src_plt = pSrcBitmap->GetPaletteData(); + pdfium::span<const uint32_t> src_palette = pSrcBitmap->GetPaletteSpan(); uint32_t plt[2]; uint8_t* bgr_ptr = reinterpret_cast<uint8_t*>(plt); if (pSrcBitmap->IsCmykImage()) { - plt[0] = FXCMYK_TODIB(src_plt[0]); - plt[1] = FXCMYK_TODIB(src_plt[1]); + plt[0] = FXCMYK_TODIB(src_palette[0]); + plt[1] = FXCMYK_TODIB(src_palette[1]); } else { - bgr_ptr[0] = FXARGB_B(src_plt[0]); - bgr_ptr[1] = FXARGB_G(src_plt[0]); - bgr_ptr[2] = FXARGB_R(src_plt[0]); - bgr_ptr[3] = FXARGB_B(src_plt[1]); - bgr_ptr[4] = FXARGB_G(src_plt[1]); - bgr_ptr[5] = FXARGB_R(src_plt[1]); + bgr_ptr[0] = FXARGB_B(src_palette[0]); + bgr_ptr[1] = FXARGB_G(src_palette[0]); + bgr_ptr[2] = FXARGB_R(src_palette[0]); + bgr_ptr[3] = FXARGB_B(src_palette[1]); + bgr_ptr[4] = FXARGB_G(src_palette[1]); + bgr_ptr[5] = FXARGB_R(src_palette[1]); } if (pSrcBitmap->IsCmykImage()) { std::tie(bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[0]), FXSYS_GetMValue(src_plt[0]), - FXSYS_GetYValue(src_plt[0]), FXSYS_GetKValue(src_plt[0])); + FXSYS_GetCValue(src_palette[0]), FXSYS_GetMValue(src_palette[0]), + FXSYS_GetYValue(src_palette[0]), FXSYS_GetKValue(src_palette[0])); std::tie(bgr_ptr[5], bgr_ptr[4], bgr_ptr[3]) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[1]), FXSYS_GetMValue(src_plt[1]), - FXSYS_GetYValue(src_plt[1]), FXSYS_GetKValue(src_plt[1])); + FXSYS_GetCValue(src_palette[1]), FXSYS_GetMValue(src_palette[1]), + FXSYS_GetYValue(src_palette[1]), FXSYS_GetKValue(src_palette[1])); } for (int row = 0; row < height; ++row) { @@ -468,14 +468,14 @@ int src_left, int src_top) { int comps = GetCompsFromFormat(dest_format); - const uint32_t* src_plt = pSrcBitmap->GetPaletteData(); + pdfium::span<const uint32_t> src_palette = pSrcBitmap->GetPaletteSpan(); uint32_t plt[256]; uint8_t* bgr_ptr = reinterpret_cast<uint8_t*>(plt); if (!pSrcBitmap->IsCmykImage()) { for (int i = 0; i < 256; ++i) { - *bgr_ptr++ = FXARGB_B(src_plt[i]); - *bgr_ptr++ = FXARGB_G(src_plt[i]); - *bgr_ptr++ = FXARGB_R(src_plt[i]); + *bgr_ptr++ = FXARGB_B(src_palette[i]); + *bgr_ptr++ = FXARGB_G(src_palette[i]); + *bgr_ptr++ = FXARGB_R(src_palette[i]); } bgr_ptr = reinterpret_cast<uint8_t*>(plt); } @@ -483,8 +483,8 @@ if (pSrcBitmap->IsCmykImage()) { for (int i = 0; i < 256; ++i) { std::tie(bgr_ptr[2], bgr_ptr[1], bgr_ptr[0]) = AdobeCMYK_to_sRGB1( - FXSYS_GetCValue(src_plt[i]), FXSYS_GetMValue(src_plt[i]), - FXSYS_GetYValue(src_plt[i]), FXSYS_GetKValue(src_plt[i])); + FXSYS_GetCValue(src_palette[i]), FXSYS_GetMValue(src_palette[i]), + FXSYS_GetYValue(src_palette[i]), FXSYS_GetKValue(src_palette[i])); bgr_ptr += 3; } bgr_ptr = reinterpret_cast<uint8_t*>(plt); @@ -805,7 +805,7 @@ uint32_t CFX_DIBBase::GetPaletteArgb(int index) const { ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); if (HasPalette()) - return GetPaletteData()[index]; + return GetPaletteSpan()[index]; if (IsCmykImage()) { if (GetBPP() == 1) @@ -829,8 +829,9 @@ ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask()); if (HasPalette()) { int palsize = (1 << GetBPP()); + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int i = 0; i < palsize; ++i) { - if (GetPaletteData()[i] == color) + if (palette[i] == color) return i; } return -1; @@ -916,15 +917,16 @@ ASSERT(!IsCmykImage()); if (GetBPP() == 1) { - pal[0] = ((HasPalette() ? GetPaletteData()[0] : 0xff000000) & 0xffffff) | + pal[0] = ((HasPalette() ? GetPaletteSpan()[0] : 0xff000000) & 0xffffff) | (alpha << 24); - pal[1] = ((HasPalette() ? GetPaletteData()[1] : 0xffffffff) & 0xffffff) | + pal[1] = ((HasPalette() ? GetPaletteSpan()[1] : 0xffffffff) & 0xffffff) | (alpha << 24); return; } if (HasPalette()) { + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int i = 0; i < 256; ++i) - pal[i] = (GetPaletteData()[i] & 0x00ffffff) | (alpha << 24); + pal[i] = (palette[i] & 0x00ffffff) | (alpha << 24); } else { for (int i = 0; i < 256; ++i) pal[i] = (i * 0x10101) | (alpha << 24);
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h index e8212b9..763f3d1 100644 --- a/core/fxge/dib/cfx_dibbase.h +++ b/core/fxge/dib/cfx_dibbase.h
@@ -13,6 +13,7 @@ #include "core/fxcrt/fx_memory_wrappers.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxge/fx_dib.h" +#include "third_party/base/span.h" enum FXDIB_Channel { FXDIB_Red = 1, @@ -56,6 +57,7 @@ } uint32_t GetPitch() const { return m_Pitch; } bool HasPalette() const { return !m_palette.empty(); } + pdfium::span<const uint32_t> GetPaletteSpan() const { return m_palette; } const uint32_t* GetPaletteData() const { return m_palette.data(); } int GetBPP() const { return m_bpp; }
diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp index f738edc..7d70927 100644 --- a/core/fxge/dib/cfx_dibitmap.cpp +++ b/core/fxge/dib/cfx_dibitmap.cpp
@@ -547,14 +547,14 @@ } case FXDIB_1bppRgb: { if ((*pos) & (1 << (7 - x % 8))) { - return HasPalette() ? GetPaletteData()[1] : 0xffffffff; + return HasPalette() ? GetPaletteSpan()[1] : 0xffffffff; } - return HasPalette() ? GetPaletteData()[0] : 0xff000000; + return HasPalette() ? GetPaletteSpan()[0] : 0xff000000; } case FXDIB_8bppMask: return (*pos) << 24; case FXDIB_8bppRgb: - return HasPalette() ? GetPaletteData()[*pos] + return HasPalette() ? GetPaletteSpan()[*pos] : (0xff000000 | ((*pos) * 0x10101)); case FXDIB_Rgb: case FXDIB_Rgba: @@ -588,7 +588,7 @@ break; case FXDIB_1bppRgb: if (HasPalette()) { - if (color == GetPaletteData()[1]) { + if (color == GetPaletteSpan()[1]) { *pos |= 1 << (7 - x % 8); } else { *pos &= ~(1 << (7 - x % 8)); @@ -606,8 +606,9 @@ break; case FXDIB_8bppRgb: { if (HasPalette()) { + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int i = 0; i < 256; i++) { - if (GetPaletteData()[i] == color) { + if (palette[i] == color) { *pos = (uint8_t)i; return; } @@ -664,6 +665,7 @@ dest_scan[i] = (scanline[src_x / 8] & (1 << (7 - src_x % 8))) ? 255 : 0; } } else if (src_Bpp == 1) { + pdfium::span<const uint32_t> palette = GetPaletteSpan(); for (int i = 0; i < clip_width; i++) { uint32_t dest_x = clip_left + i; uint32_t src_x = dest_x * m_Width / dest_width; @@ -675,13 +677,13 @@ if (HasPalette()) { if (!IsCmykImage()) { dest_pos *= 3; - FX_ARGB argb = GetPaletteData()[scanline[src_x]]; + FX_ARGB argb = palette[scanline[src_x]]; dest_scan[dest_pos] = FXARGB_B(argb); dest_scan[dest_pos + 1] = FXARGB_G(argb); dest_scan[dest_pos + 2] = FXARGB_R(argb); } else { dest_pos *= 4; - FX_CMYK cmyk = GetPaletteData()[scanline[src_x]]; + FX_CMYK cmyk = palette[scanline[src_x]]; dest_scan[dest_pos] = FXSYS_GetCValue(cmyk); dest_scan[dest_pos + 1] = FXSYS_GetMValue(cmyk); dest_scan[dest_pos + 2] = FXSYS_GetYValue(cmyk); @@ -1070,7 +1072,7 @@ int index = 0; if (HasPalette()) { for (int i = 0; i < 2; i++) { - if (GetPaletteData()[i] == color) + if (GetPaletteSpan()[i] == color) index = i; } } else {
diff --git a/core/fxge/dib/cfx_imagetransformer.cpp b/core/fxge/dib/cfx_imagetransformer.cpp index bb0b683..44c3ede 100644 --- a/core/fxge/dib/cfx_imagetransformer.cpp +++ b/core/fxge/dib/cfx_imagetransformer.cpp
@@ -513,10 +513,11 @@ void CFX_ImageTransformer::CalcMono(const CalcData& cdata, FXDIB_Format format) { uint32_t argb[256]; - const FX_ARGB* pPal = m_Storer.GetBitmap()->GetPaletteData(); - if (pPal) { + if (m_Storer.GetBitmap()->HasPalette()) { + pdfium::span<const uint32_t> palette = + m_Storer.GetBitmap()->GetPaletteSpan(); for (size_t i = 0; i < pdfium::size(argb); i++) - argb[i] = pPal[i]; + argb[i] = palette[i]; } else if (m_Storer.GetBitmap()->IsCmykImage()) { for (size_t i = 0; i < pdfium::size(argb); i++) argb[i] = 255 - i;
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp index 868360c..80a03b0 100644 --- a/core/fxge/skia/fx_skia_device.cpp +++ b/core/fxge/skia/fx_skia_device.cpp
@@ -667,18 +667,18 @@ if (pSource->HasPalette()) { dst32Storage.reset(FX_Alloc2D(uint32_t, width, height)); SkPMColor* dst32Pixels = dst32Storage.get(); - const SkPMColor* ctable = pSource->GetPaletteData(); - const unsigned ctableSize = pSource->GetPaletteSize(); + pdfium::span<const uint32_t> src_palette = pSource->GetPaletteSpan(); + const unsigned src_palette_size = pSource->GetPaletteSize(); for (int y = 0; y < height; ++y) { const uint8_t* srcRow = static_cast<const uint8_t*>(buffer) + y * rowBytes; uint32_t* dstRow = dst32Pixels + y * width; for (int x = 0; x < width; ++x) { unsigned index = srcRow[x]; - if (index >= ctableSize) { + if (index >= src_palette_size) { index = 0; } - dstRow[x] = ctable[index]; + dstRow[x] = src_palette[index]; } } buffer = dst32Storage.get();
diff --git a/core/fxge/win32/cfx_windowsdib.cpp b/core/fxge/win32/cfx_windowsdib.cpp index 08e8d81..4a6fd54 100644 --- a/core/fxge/win32/cfx_windowsdib.cpp +++ b/core/fxge/win32/cfx_windowsdib.cpp
@@ -48,8 +48,9 @@ if (pBitmap->GetBPP() == 8) { uint32_t* pPalette = (uint32_t*)(pbmih + 1); if (pBitmap->HasPalette()) { + pdfium::span<const uint32_t> palette = pBitmap->GetPaletteSpan(); for (int i = 0; i < 256; i++) { - pPalette[i] = pBitmap->GetPaletteData()[i]; + pPalette[i] = palette[i]; } } else { for (int i = 0; i < 256; i++) {