Change CFX_BitmapComposer to use float alpha values

Change a helper class to use float alpha values when possible, and
convert to integer alpha values as-needed.

Change-Id: Ia5b4dbaee22af874524e64000d15fab00091fb2d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/115416
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp
index 6edad47..ef90113 100644
--- a/core/fxge/agg/fx_agg_driver.cpp
+++ b/core/fxge/agg/fx_agg_driver.cpp
@@ -1349,8 +1349,9 @@
   FX_RECT dest_clip = dest_rect;
   dest_clip.Intersect(*pClipRect);
   CFX_BitmapComposer composer;
-  composer.Compose(m_pBitmap, m_pClipRgn.get(), 255, argb, dest_clip, false,
-                   false, false, m_bRgbByteOrder, blend_type);
+  composer.Compose(m_pBitmap, m_pClipRgn.get(), /*alpha=*/1.0f, argb, dest_clip,
+                   /*bVertical=*/false, /*bFlipX=*/false, /*bFlipY=*/false,
+                   m_bRgbByteOrder, blend_type);
   dest_clip.Offset(-dest_rect.left, -dest_rect.top);
   CFX_ImageStretcher stretcher(&composer, pSource, dest_width, dest_height,
                                dest_clip, options);
diff --git a/core/fxge/dib/cfx_bitmapcomposer.cpp b/core/fxge/dib/cfx_bitmapcomposer.cpp
index e924034..30c57c3 100644
--- a/core/fxge/dib/cfx_bitmapcomposer.cpp
+++ b/core/fxge/dib/cfx_bitmapcomposer.cpp
@@ -11,6 +11,7 @@
 #include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_safe_types.h"
+#include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/span_util.h"
 #include "core/fxge/cfx_cliprgn.h"
 #include "core/fxge/dib/cfx_dibitmap.h"
@@ -22,7 +23,7 @@
 
 void CFX_BitmapComposer::Compose(const RetainPtr<CFX_DIBitmap>& pDest,
                                  const CFX_ClipRgn* pClipRgn,
-                                 int bitmap_alpha,
+                                 float alpha,
                                  uint32_t mask_color,
                                  const FX_RECT& dest_rect,
                                  bool bVertical,
@@ -36,7 +37,7 @@
   m_DestTop = dest_rect.top;
   m_DestWidth = dest_rect.Width();
   m_DestHeight = dest_rect.Height();
-  m_BitmapAlpha = bitmap_alpha;
+  m_Alpha = alpha;
   m_MaskColor = mask_color;
   m_pClipMask = nullptr;
   if (pClipRgn && pClipRgn->GetType() != CFX_ClipRgn::kRectI)
@@ -57,15 +58,14 @@
   m_SrcFormat = src_format;
   if (!m_Compositor.Init(m_pBitmap->GetFormat(), src_format, src_palette,
                          m_MaskColor, m_BlendMode,
-                         m_pClipMask != nullptr || (m_BitmapAlpha < 255),
-                         m_bRgbByteOrder)) {
+                         m_pClipMask || m_Alpha != 1.0f, m_bRgbByteOrder)) {
     return false;
   }
   if (m_bVertical) {
     m_pScanlineV.resize(m_pBitmap->GetBPP() / 8 * width + 4);
     m_pClipScanV.resize(m_pBitmap->GetHeight());
   }
-  if (m_BitmapAlpha < 255) {
+  if (m_Alpha != 1.0f) {
     m_pAddClipScan.resize(m_bVertical ? m_pBitmap->GetHeight()
                                       : m_pBitmap->GetWidth());
   }
@@ -76,13 +76,14 @@
                                    pdfium::span<const uint8_t> src_scan,
                                    int dest_width,
                                    pdfium::span<const uint8_t> clip_scan) {
-  if (m_BitmapAlpha < 255) {
+  if (m_Alpha != 1.0f) {
     if (!clip_scan.empty()) {
-      for (int i = 0; i < dest_width; ++i)
-        m_pAddClipScan[i] = clip_scan[i] * m_BitmapAlpha / 255;
+      for (int i = 0; i < dest_width; ++i) {
+        m_pAddClipScan[i] = clip_scan[i] * m_Alpha;
+      }
     } else {
       fxcrt::spanset(pdfium::make_span(m_pAddClipScan).first(dest_width),
-                     m_BitmapAlpha);
+                     FXSYS_roundf(m_Alpha * 255));
     }
     clip_scan = m_pAddClipScan;
   }
diff --git a/core/fxge/dib/cfx_bitmapcomposer.h b/core/fxge/dib/cfx_bitmapcomposer.h
index c4cc07d..f63c8c5 100644
--- a/core/fxge/dib/cfx_bitmapcomposer.h
+++ b/core/fxge/dib/cfx_bitmapcomposer.h
@@ -27,7 +27,7 @@
 
   void Compose(const RetainPtr<CFX_DIBitmap>& pDest,
                const CFX_ClipRgn* pClipRgn,
-               int bitmap_alpha,
+               float alpha,
                uint32_t mask_color,
                const FX_RECT& dest_rect,
                bool bVertical,
@@ -57,7 +57,7 @@
   int m_DestTop;
   int m_DestWidth;
   int m_DestHeight;
-  int m_BitmapAlpha;
+  float m_Alpha;
   uint32_t m_MaskColor;
   RetainPtr<CFX_DIBitmap> m_pClipMask;
   CFX_ScanlineCompositor m_Compositor;
diff --git a/core/fxge/dib/cfx_imagerenderer.cpp b/core/fxge/dib/cfx_imagerenderer.cpp
index 592124d..0631018 100644
--- a/core/fxge/dib/cfx_imagerenderer.cpp
+++ b/core/fxge/dib/cfx_imagerenderer.cpp
@@ -49,9 +49,11 @@
       bitmap_clip.Offset(-image_rect.left, -image_rect.top);
       bitmap_clip = bitmap_clip.SwappedClipBox(dest_width, dest_height,
                                                m_Matrix.c > 0, m_Matrix.b < 0);
-      m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha, mask_color, m_ClipBox,
-                         true, m_Matrix.c > 0, m_Matrix.b < 0, m_bRgbByteOrder,
-                         BlendMode::kNormal);
+      const bool flip_x = m_Matrix.c > 0;
+      const bool flip_y = m_Matrix.b < 0;
+      m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha / 255.0f, mask_color,
+                         m_ClipBox, /*bVertical=*/true, flip_x, flip_y,
+                         m_bRgbByteOrder, BlendMode::kNormal);
       m_Stretcher = std::make_unique<CFX_ImageStretcher>(
           &m_Composer, pSource, dest_height, dest_width, bitmap_clip, options);
       if (m_Stretcher->Start())
@@ -77,8 +79,10 @@
 
   FX_RECT bitmap_clip = m_ClipBox;
   bitmap_clip.Offset(-image_rect.left, -image_rect.top);
-  m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha, mask_color, m_ClipBox,
-                     false, false, false, m_bRgbByteOrder, BlendMode::kNormal);
+  m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha / 255.0f, mask_color,
+                     m_ClipBox,
+                     /*bVertical=*/false, /*bFlipX=*/false, /*bFlipY=*/false,
+                     m_bRgbByteOrder, BlendMode::kNormal);
   m_State = State::kStretching;
   m_Stretcher = std::make_unique<CFX_ImageStretcher>(
       &m_Composer, pSource, dest_width, dest_height, bitmap_clip, options);