Add PixelWeight::SetWeightForPixel() and RemoveLastWeight().

Replace an "unreachable" (in theory) branch with a hard check.

-- Avoid the use of the term "pixel" while at it, since these
   aren't luminescence values like 0..255 but positions within
   the scanline.

Change-Id: I27d413ca3b2e8c2ee7420ed9fab42140c430a738
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82130
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/dib/cstretchengine.cpp b/core/fxge/dib/cstretchengine.cpp
index 01a5ff1..f486338 100644
--- a/core/fxge/dib/cstretchengine.cpp
+++ b/core/fxge/dib/cstretchengine.cpp
@@ -126,14 +126,10 @@
       double area_end = std::min(dest_end, static_cast<double>(dest_pixel + 1));
       double weight = std::max(0.0, area_end - area_start);
       if (weight == 0 && j == end_i) {
-        --pixel_weights.m_SrcEnd;
+        pixel_weights.RemoveLastWeight();
         break;
       }
-      size_t idx = j - start_i;
-      if (idx >= weight_count)
-        return false;
-
-      pixel_weights.m_Weights[idx] = FixedFromFloat(weight);
+      pixel_weights.SetWeightForPosition(j, FixedFromFloat(weight));
     }
   }
   return true;
@@ -301,7 +297,7 @@
           PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col);
           uint32_t dest_a = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             if (src_scan[j / 8] & (1 << (7 - j % 8)))
               dest_a += pixel_weight * 255;
           }
@@ -314,7 +310,7 @@
           PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col);
           uint32_t dest_a = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             dest_a += pixel_weight * src_scan[j];
           }
           *dest_scan++ = PixelFromFixed(dest_a);
@@ -327,7 +323,7 @@
           uint32_t dest_a = 0;
           uint32_t dest_r = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             pixel_weight = pixel_weight * src_scan_mask[j] / 255;
             dest_r += pixel_weight * src_scan[j];
             dest_a += pixel_weight;
@@ -344,7 +340,7 @@
           uint32_t dest_g = 0;
           uint32_t dest_b = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             unsigned long argb = m_pSrcPalette[src_scan[j]];
             if (m_DestFormat == FXDIB_Format::kRgb) {
               dest_r += pixel_weight * static_cast<uint8_t>(argb >> 16);
@@ -370,7 +366,7 @@
           uint32_t dest_g = 0;
           uint32_t dest_b = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             pixel_weight = pixel_weight * src_scan_mask[j] / 255;
             unsigned long argb = m_pSrcPalette[src_scan[j]];
             dest_b += pixel_weight * static_cast<uint8_t>(argb >> 24);
@@ -392,7 +388,7 @@
           uint32_t dest_g = 0;
           uint32_t dest_b = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             const uint8_t* src_pixel = src_scan + j * Bpp;
             dest_b += pixel_weight * (*src_pixel++);
             dest_g += pixel_weight * (*src_pixel++);
@@ -413,7 +409,7 @@
           uint32_t dest_g = 0;
           uint32_t dest_b = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             const uint8_t* src_pixel = src_scan + j * Bpp;
             if (m_DestFormat == FXDIB_Format::kArgb) {
               pixel_weight = pixel_weight * src_pixel[3] / 255;
@@ -467,7 +463,7 @@
               m_InterBuf.data() + (col - m_DestClip.left) * DestBpp;
           uint32_t dest_a = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             dest_a +=
                 pixel_weight * src_scan[(j - m_SrcClip.top) * m_InterPitch];
           }
@@ -485,7 +481,7 @@
           uint32_t dest_a = 0;
           uint32_t dest_k = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             dest_k +=
                 pixel_weight * src_scan[(j - m_SrcClip.top) * m_InterPitch];
             dest_a += pixel_weight *
@@ -506,7 +502,7 @@
           uint32_t dest_g = 0;
           uint32_t dest_b = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             const uint8_t* src_pixel =
                 src_scan + (j - m_SrcClip.top) * m_InterPitch;
             dest_b += pixel_weight * (*src_pixel++);
@@ -533,7 +529,7 @@
           uint32_t dest_g = 0;
           uint32_t dest_b = 0;
           for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) {
-            uint32_t pixel_weight = pWeights->GetWeight(j);
+            uint32_t pixel_weight = pWeights->GetWeightForPosition(j);
             const uint8_t* src_pixel =
                 src_scan + (j - m_SrcClip.top) * m_InterPitch;
             int mask_v = 255;
diff --git a/core/fxge/dib/fx_dib.h b/core/fxge/dib/fx_dib.h
index fe05b98..5cb271e 100644
--- a/core/fxge/dib/fx_dib.h
+++ b/core/fxge/dib/fx_dib.h
@@ -39,10 +39,21 @@
     m_SrcEnd = src_end;
   }
 
-  uint32_t GetWeight(int pixel) const {
-    CHECK_GE(pixel, m_SrcStart);
-    CHECK_LE(pixel, m_SrcEnd);
-    return m_Weights[pixel - m_SrcStart];
+  uint32_t GetWeightForPosition(int position) const {
+    CHECK_GE(position, m_SrcStart);
+    CHECK_LE(position, m_SrcEnd);
+    return m_Weights[position - m_SrcStart];
+  }
+
+  void SetWeightForPosition(int position, uint32_t weight) {
+    CHECK_GE(position, m_SrcStart);
+    CHECK_LE(position, m_SrcEnd);
+    m_Weights[position - m_SrcStart] = weight;
+  }
+
+  void RemoveLastWeight() {
+    CHECK_GT(m_SrcEnd, m_SrcStart);
+    --m_SrcEnd;
   }
 
   int m_SrcStart;