Avoid multiplying by 0x010101 for RGB value computation
Replaces CL at https://pdfium-review.googlesource.com/c/pdfium/+/75770
It is clearer to use the pre-existing ArgbEncode() function rather
than realizing that this multiplication results in an RGB value
where all the components are equal.
-- use unsigned shifts in ArgbEncode() to avoid undefined behavior.
Change-Id: Icd5003d0c85b78ef5d0020a74d73a3926fe22cd8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/75811
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index a58b84f..cff1d79 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -650,7 +650,7 @@
} else if (GetBPP() == 8) {
m_palette.resize(256);
for (int i = 0; i < 256; ++i)
- m_palette[i] = 0xff000000 | (i * 0x10101);
+ m_palette[i] = ArgbEncode(0xff, i, i, i);
}
}
@@ -690,7 +690,7 @@
if (GetBPP() == 1)
return index ? 0xffffffff : 0xff000000;
- return index * 0x10101 | 0xff000000;
+ return ArgbEncode(0xff, index, index, index);
}
void CFX_DIBBase::SetPaletteArgb(int index, uint32_t color) {
@@ -840,7 +840,7 @@
pal[i] = (palette[i] & 0x00ffffff) | (alpha << 24);
} else {
for (int i = 0; i < 256; ++i)
- pal[i] = (i * 0x10101) | (alpha << 24);
+ pal[i] = ArgbEncode(alpha, i, i, i);
}
}
diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp
index 4c85633..1052b35 100644
--- a/core/fxge/dib/cfx_dibitmap.cpp
+++ b/core/fxge/dib/cfx_dibitmap.cpp
@@ -515,7 +515,7 @@
return (*pos) << 24;
case FXDIB_Format::k8bppRgb:
return HasPalette() ? GetPaletteSpan()[*pos]
- : (0xff000000 | ((*pos) * 0x10101));
+ : ArgbEncode(0xff, *pos, *pos, *pos);
case FXDIB_Format::kRgb:
case FXDIB_Format::kRgb32:
return FXARGB_GETDIB(pos) | 0xff000000;
diff --git a/core/fxge/dib/cfx_imagetransformer.cpp b/core/fxge/dib/cfx_imagetransformer.cpp
index ffcc7d9..c5e1576 100644
--- a/core/fxge/dib/cfx_imagetransformer.cpp
+++ b/core/fxge/dib/cfx_imagetransformer.cpp
@@ -309,7 +309,7 @@
argb[i] = palette[i];
} else {
for (size_t i = 0; i < pdfium::size(argb); i++)
- argb[i] = 0xff000000 | (i * 0x010101);
+ argb[i] = ArgbEncode(0xff, i, i, i);
}
int destBpp = calc_data.bitmap->GetBPP() / 8;
auto func = [&calc_data, &argb](const BilinearData& data, uint8_t* dest) {
diff --git a/core/fxge/dib/cfx_scanlinecompositor.cpp b/core/fxge/dib/cfx_scanlinecompositor.cpp
index 47c8880..e8362d7 100644
--- a/core/fxge/dib/cfx_scanlinecompositor.cpp
+++ b/core/fxge/dib/cfx_scanlinecompositor.cpp
@@ -2253,7 +2253,8 @@
int DestBpp,
const uint8_t* clip_scan) {
for (int col = 0; col < pixel_count; col++) {
- FX_ARGB argb = pPalette ? pPalette[*src_scan] : (*src_scan) * 0x010101;
+ FX_ARGB argb = pPalette ? pPalette[*src_scan]
+ : ArgbEncode(0, *src_scan, *src_scan, *src_scan);
int src_r = FXARGB_R(argb);
int src_g = FXARGB_G(argb);
int src_b = FXARGB_B(argb);
@@ -2764,7 +2765,7 @@
pPalette[1] = 0xffffffff;
} else {
for (size_t i = 0; i < pal_count; ++i)
- pPalette[i] = i * 0x10101;
+ pPalette[i] = ArgbEncode(0, i, i, i);
}
}
diff --git a/core/fxge/dib/fx_dib.h b/core/fxge/dib/fx_dib.h
index 2cfdd49..a167214 100644
--- a/core/fxge/dib/fx_dib.h
+++ b/core/fxge/dib/fx_dib.h
@@ -122,7 +122,7 @@
// Returns FX_COLORREF.
FX_COLORREF ArgbToColorRef(FX_ARGB argb);
-constexpr FX_ARGB ArgbEncode(int a, int r, int g, int b) {
+constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b) {
return (a << 24) | (r << 16) | (g << 8) | b;
}
diff --git a/core/fxge/win32/cgdi_device_driver.cpp b/core/fxge/win32/cgdi_device_driver.cpp
index 5648c2d..57c127a 100644
--- a/core/fxge/win32/cgdi_device_driver.cpp
+++ b/core/fxge/win32/cgdi_device_driver.cpp
@@ -172,13 +172,11 @@
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++) {
+ for (int i = 0; i < 256; i++)
pPalette[i] = palette[i];
- }
} else {
- for (int i = 0; i < 256; i++) {
- pPalette[i] = i * 0x010101;
- }
+ for (int i = 0; i < 256; i++)
+ pPalette[i] = ArgbEncode(0, i, i, i);
}
}
if (pBitmap->GetBPP() == 1) {