Fix/Mark unsafe buffer regions is CJPX_Decoder

Spanify components returned by library and use throughout.
Mark other regions unsafe, may be performance-sensitive.

Bug: 42271175
Change-Id: Ib79da15042fc7cb866b46d0459ea2f7810e1cee6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/120133
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fxcodec/jpx/cjpx_decoder.cpp b/core/fxcodec/jpx/cjpx_decoder.cpp
index 2c3cf98..f0607fd 100644
--- a/core/fxcodec/jpx/cjpx_decoder.cpp
+++ b/core/fxcodec/jpx/cjpx_decoder.cpp
@@ -4,11 +4,6 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#if defined(UNSAFE_BUFFERS_BUILD)
-// TODO(crbug.com/pdfium/2154): resolve buffer safety issues.
-#pragma allow_unsafe_buffers
-#endif
-
 #include "core/fxcodec/jpx/cjpx_decoder.h"
 
 #include <string.h>
@@ -22,7 +17,9 @@
 #include "core/fxcodec/jpx/jpx_decode_utils.h"
 #include "core/fxcrt/check.h"
 #include "core/fxcrt/fx_safe_types.h"
+#include "core/fxcrt/numerics/safe_conversions.h"
 #include "core/fxcrt/ptr_util.h"
+#include "core/fxcrt/span.h"
 #include "core/fxcrt/span_util.h"
 #include "core/fxge/calculate_pitch.h"
 
@@ -98,26 +95,32 @@
   *out_b = std::clamp(y + static_cast<int>(1.772 * cb), 0, upb);
 }
 
+pdfium::span<opj_image_comp_t> components_span(opj_image_t* img) {
+  // SAFETY: required from third-party library.
+  return UNSAFE_BUFFERS(pdfium::make_span(img->comps, img->numcomps));
+}
+
 void sycc444_to_rgb(opj_image_t* img) {
-  int prec = img->comps[0].prec;
+  auto components = components_span(img);
+  int prec = components[0].prec;
   // If we shift 31 we're going to go negative, then things go bad.
   if (prec > 30)
     return;
   int offset = 1 << (prec - 1);
   int upb = (1 << prec) - 1;
   OPJ_UINT32 maxw =
-      std::min({img->comps[0].w, img->comps[1].w, img->comps[2].w});
+      std::min({components[0].w, components[1].w, components[2].w});
   OPJ_UINT32 maxh =
-      std::min({img->comps[0].h, img->comps[1].h, img->comps[2].h});
+      std::min({components[0].h, components[1].h, components[2].h});
   FX_SAFE_SIZE_T max_size = maxw;
   max_size *= maxh;
   max_size *= sizeof(int);
   if (!max_size.IsValid())
     return;
 
-  const int* y = img->comps[0].data;
-  const int* cb = img->comps[1].data;
-  const int* cr = img->comps[2].data;
+  const int* y = components[0].data;
+  const int* cb = components[1].data;
+  const int* cr = components[2].data;
   if (!y || !cb || !cr)
     return;
 
@@ -129,28 +132,28 @@
   int* g = data.value().g.get();
   int* b = data.value().b.get();
   max_size /= sizeof(int);
-  for (size_t i = 0; i < max_size.ValueOrDie(); ++i)
-    sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
-
-  opj_image_data_free(img->comps[0].data);
-  opj_image_data_free(img->comps[1].data);
-  opj_image_data_free(img->comps[2].data);
-  img->comps[0].data = data.value().r.release();
-  img->comps[1].data = data.value().g.release();
-  img->comps[2].data = data.value().b.release();
+  for (size_t i = 0; i < max_size.ValueOrDie(); ++i) {
+    UNSAFE_TODO(sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++));
+  }
+  opj_image_data_free(components[0].data);
+  opj_image_data_free(components[1].data);
+  opj_image_data_free(components[2].data);
+  components[0].data = data.value().r.release();
+  components[1].data = data.value().g.release();
+  components[2].data = data.value().b.release();
 }
 
-bool sycc420_422_size_is_valid(opj_image_t* img) {
-  return img && img->comps[0].w != std::numeric_limits<OPJ_UINT32>::max() &&
-         (img->comps[0].w + 1) / 2 == img->comps[1].w &&
-         img->comps[1].w == img->comps[2].w &&
-         img->comps[1].h == img->comps[2].h;
+bool sycc420_422_size_is_valid(pdfium::span<opj_image_comp_t> components) {
+  return components[0].w != std::numeric_limits<OPJ_UINT32>::max() &&
+         (components[0].w + 1) / 2 == components[1].w &&
+         components[1].w == components[2].w &&
+         components[1].h == components[2].h;
 }
 
-bool sycc420_size_is_valid(opj_image_t* img) {
-  return sycc420_422_size_is_valid(img) &&
-         img->comps[0].h != std::numeric_limits<OPJ_UINT32>::max() &&
-         (img->comps[0].h + 1) / 2 == img->comps[1].h;
+bool sycc420_size_is_valid(pdfium::span<opj_image_comp_t> components) {
+  return sycc420_422_size_is_valid(components) &&
+         components[0].h != std::numeric_limits<OPJ_UINT32>::max() &&
+         (components[0].h + 1) / 2 == components[1].h;
 }
 
 bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) {
@@ -158,20 +161,24 @@
 }
 
 void sycc420_to_rgb(opj_image_t* img) {
-  if (!sycc420_size_is_valid(img))
+  if (!img) {
     return;
-
-  OPJ_UINT32 prec = img->comps[0].prec;
+  }
+  auto components = components_span(img);
+  if (!sycc420_size_is_valid(components)) {
+    return;
+  }
+  OPJ_UINT32 prec = components[0].prec;
   if (!prec)
     return;
 
   OPJ_UINT32 offset = 1 << (prec - 1);
   OPJ_UINT32 upb = (1 << prec) - 1;
-  OPJ_UINT32 yw = img->comps[0].w;
-  OPJ_UINT32 yh = img->comps[0].h;
-  OPJ_UINT32 cbw = img->comps[1].w;
-  OPJ_UINT32 cbh = img->comps[1].h;
-  OPJ_UINT32 crw = img->comps[2].w;
+  OPJ_UINT32 yw = components[0].w;
+  OPJ_UINT32 yh = components[0].h;
+  OPJ_UINT32 cbw = components[1].w;
+  OPJ_UINT32 cbh = components[1].h;
+  OPJ_UINT32 crw = components[2].w;
   bool extw = sycc420_must_extend_cbcr(yw, cbw);
   bool exth = sycc420_must_extend_cbcr(yh, cbh);
   FX_SAFE_UINT32 safe_size = yw;
@@ -180,9 +187,9 @@
   if (!safe_size.IsValid())
     return;
 
-  const int* y = img->comps[0].data;
-  const int* cb = img->comps[1].data;
-  const int* cr = img->comps[2].data;
+  const int* y = components[0].data;
+  const int* cb = components[1].data;
+  const int* cr = components[2].data;
   if (!y || !cb || !cr)
     return;
 
@@ -199,127 +206,133 @@
   int* nb = nullptr;
   OPJ_UINT32 i = 0;
   OPJ_UINT32 j = 0;
-  for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
-    ny = y + yw;
-    nr = r + yw;
-    ng = g + yw;
-    nb = b + yw;
-    for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
-      sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
-      ++y;
-      ++r;
-      ++g;
-      ++b;
-      sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
-      ++y;
-      ++r;
-      ++g;
-      ++b;
-      sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
-      ++ny;
-      ++nr;
-      ++ng;
-      ++nb;
-      sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
-      ++ny;
-      ++nr;
-      ++ng;
-      ++nb;
-      ++cb;
-      ++cr;
-    }
-    if (j < yw) {
-      if (extw) {
-        --cb;
-        --cr;
+  UNSAFE_TODO({
+    for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) {
+      ny = y + yw;
+      nr = r + yw;
+      ng = g + yw;
+      nb = b + yw;
+      for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
+        sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+        ++y;
+        ++r;
+        ++g;
+        ++b;
+        sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+        ++y;
+        ++r;
+        ++g;
+        ++b;
+        sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+        ++ny;
+        ++nr;
+        ++ng;
+        ++nb;
+        sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+        ++ny;
+        ++nr;
+        ++ng;
+        ++nb;
+        ++cb;
+        ++cr;
       }
-      sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
-      ++y;
-      ++r;
-      ++g;
-      ++b;
-      sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
-      ++ny;
-      ++nr;
-      ++ng;
-      ++nb;
-      ++cb;
-      ++cr;
-    }
-    y += yw;
-    r += yw;
-    g += yw;
-    b += yw;
-  }
-  if (i < yh) {
-    if (exth) {
-      cb -= cbw;
-      cr -= crw;
-    }
-    for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
-      sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
-      ++y;
-      ++r;
-      ++g;
-      ++b;
-      sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
-      ++y;
-      ++r;
-      ++g;
-      ++b;
-      ++cb;
-      ++cr;
-    }
-    if (j < yw) {
-      if (extw) {
-        --cb;
-        --cr;
+      if (j < yw) {
+        if (extw) {
+          --cb;
+          --cr;
+        }
+        sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+        ++y;
+        ++r;
+        ++g;
+        ++b;
+        sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+        ++ny;
+        ++nr;
+        ++ng;
+        ++nb;
+        ++cb;
+        ++cr;
       }
-      sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+      y += yw;
+      r += yw;
+      g += yw;
+      b += yw;
     }
-  }
-
-  opj_image_data_free(img->comps[0].data);
-  opj_image_data_free(img->comps[1].data);
-  opj_image_data_free(img->comps[2].data);
-  img->comps[0].data = data.value().r.release();
-  img->comps[1].data = data.value().g.release();
-  img->comps[2].data = data.value().b.release();
-  img->comps[1].w = yw;
-  img->comps[1].h = yh;
-  img->comps[2].w = yw;
-  img->comps[2].h = yh;
-  img->comps[1].dx = img->comps[0].dx;
-  img->comps[2].dx = img->comps[0].dx;
-  img->comps[1].dy = img->comps[0].dy;
-  img->comps[2].dy = img->comps[0].dy;
+    if (i < yh) {
+      if (exth) {
+        cb -= cbw;
+        cr -= crw;
+      }
+      for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) {
+        sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+        ++y;
+        ++r;
+        ++g;
+        ++b;
+        sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+        ++y;
+        ++r;
+        ++g;
+        ++b;
+        ++cb;
+        ++cr;
+      }
+      if (j < yw) {
+        if (extw) {
+          --cb;
+          --cr;
+        }
+        sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+      }
+    }
+  });
+  opj_image_data_free(components[0].data);
+  opj_image_data_free(components[1].data);
+  opj_image_data_free(components[2].data);
+  components[0].data = data.value().r.release();
+  components[1].data = data.value().g.release();
+  components[2].data = data.value().b.release();
+  components[1].w = yw;
+  components[1].h = yh;
+  components[2].w = yw;
+  components[2].h = yh;
+  components[1].dx = components[0].dx;
+  components[2].dx = components[0].dx;
+  components[1].dy = components[0].dy;
+  components[2].dy = components[0].dy;
 }
 
-bool sycc422_size_is_valid(opj_image_t* img) {
-  return sycc420_422_size_is_valid(img) && img->comps[0].h == img->comps[1].h;
+bool sycc422_size_is_valid(pdfium::span<opj_image_comp_t> components) {
+  return sycc420_422_size_is_valid(components) &&
+         components[0].h == components[1].h;
 }
 
 void sycc422_to_rgb(opj_image_t* img) {
-  if (!sycc422_size_is_valid(img))
+  if (!img) {
     return;
-
-  int prec = img->comps[0].prec;
+  }
+  auto components = components_span(img);
+  if (!sycc422_size_is_valid(components)) {
+    return;
+  }
+  int prec = components[0].prec;
   if (prec <= 0 || prec >= 32)
     return;
 
   int offset = 1 << (prec - 1);
   int upb = (1 << prec) - 1;
-  OPJ_UINT32 maxw = img->comps[0].w;
-  OPJ_UINT32 maxh = img->comps[0].h;
+  OPJ_UINT32 maxw = components[0].w;
+  OPJ_UINT32 maxh = components[0].h;
   FX_SAFE_SIZE_T max_size = maxw;
   max_size *= maxh;
   max_size *= sizeof(int);
   if (!max_size.IsValid())
     return;
 
-  const int* y = img->comps[0].data;
-  const int* cb = img->comps[1].data;
-  const int* cr = img->comps[2].data;
+  const int* y = components[0].data;
+  const int* cb = components[1].data;
+  const int* cr = components[2].data;
   if (!y || !cb || !cr)
     return;
 
@@ -330,65 +343,67 @@
   int* r = data.value().r.get();
   int* g = data.value().g.get();
   int* b = data.value().b.get();
-  for (uint32_t i = 0; i < maxh; ++i) {
-    OPJ_UINT32 j;
-    for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
-      sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
-      sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
+  UNSAFE_TODO({
+    for (uint32_t i = 0; i < maxh; ++i) {
+      OPJ_UINT32 j;
+      for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) {
+        sycc_to_rgb(offset, upb, *y++, *cb, *cr, r++, g++, b++);
+        sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
+      }
+      if (j < maxw) {
+        sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
+      }
     }
-    if (j < maxw) {
-      sycc_to_rgb(offset, upb, *y++, *cb++, *cr++, r++, g++, b++);
-    }
-  }
-
-  opj_image_data_free(img->comps[0].data);
-  opj_image_data_free(img->comps[1].data);
-  opj_image_data_free(img->comps[2].data);
-  img->comps[0].data = data.value().r.release();
-  img->comps[1].data = data.value().g.release();
-  img->comps[2].data = data.value().b.release();
-  img->comps[1].w = maxw;
-  img->comps[1].h = maxh;
-  img->comps[2].w = maxw;
-  img->comps[2].h = maxh;
-  img->comps[1].dx = img->comps[0].dx;
-  img->comps[2].dx = img->comps[0].dx;
-  img->comps[1].dy = img->comps[0].dy;
-  img->comps[2].dy = img->comps[0].dy;
+  });
+  opj_image_data_free(components[0].data);
+  opj_image_data_free(components[1].data);
+  opj_image_data_free(components[2].data);
+  components[0].data = data.value().r.release();
+  components[1].data = data.value().g.release();
+  components[2].data = data.value().b.release();
+  components[1].w = maxw;
+  components[1].h = maxh;
+  components[2].w = maxw;
+  components[2].h = maxh;
+  components[1].dx = components[0].dx;
+  components[2].dx = components[0].dx;
+  components[1].dy = components[0].dy;
+  components[2].dy = components[0].dy;
 }
 
-bool is_sycc420(const opj_image_t* img) {
-  return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
-         img->comps[1].dx == 2 && img->comps[1].dy == 2 &&
-         img->comps[2].dx == 2 && img->comps[2].dy == 2;
+bool is_sycc420(pdfium::span<opj_image_comp_t> components) {
+  return components[0].dx == 1 && components[0].dy == 1 &&
+         components[1].dx == 2 && components[1].dy == 2 &&
+         components[2].dx == 2 && components[2].dy == 2;
 }
 
-bool is_sycc422(const opj_image_t* img) {
-  return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
-         img->comps[1].dx == 2 && img->comps[1].dy == 1 &&
-         img->comps[2].dx == 2 && img->comps[2].dy == 1;
+bool is_sycc422(pdfium::span<opj_image_comp_t> components) {
+  return components[0].dx == 1 && components[0].dy == 1 &&
+         components[1].dx == 2 && components[1].dy == 1 &&
+         components[2].dx == 2 && components[2].dy == 1;
 }
 
-bool is_sycc444(const opj_image_t* img) {
-  return img->comps[0].dx == 1 && img->comps[0].dy == 1 &&
-         img->comps[1].dx == 1 && img->comps[1].dy == 1 &&
-         img->comps[2].dx == 1 && img->comps[2].dy == 1;
+bool is_sycc444(pdfium::span<opj_image_comp_t> components) {
+  return components[0].dx == 1 && components[0].dy == 1 &&
+         components[1].dx == 1 && components[1].dy == 1 &&
+         components[2].dx == 1 && components[2].dy == 1;
 }
 
 void color_sycc_to_rgb(opj_image_t* img) {
-  if (img->numcomps < 3) {
+  auto components = components_span(img);
+  if (components.size() < 3) {
     img->color_space = OPJ_CLRSPC_GRAY;
     return;
   }
-  if (is_sycc420(img))
+  if (is_sycc420(components)) {
     sycc420_to_rgb(img);
-  else if (is_sycc422(img))
+  } else if (is_sycc422(components)) {
     sycc422_to_rgb(img);
-  else if (is_sycc444(img))
+  } else if (is_sycc444(components)) {
     sycc444_to_rgb(img);
-  else
+  } else {
     return;
-
+  }
   img->color_space = OPJ_CLRSPC_SRGB;
 }
 
@@ -491,9 +506,9 @@
   }
 
   opj_stream_destroy(m_Stream.ExtractAsDangling());
-  if (m_Image->color_space != OPJ_CLRSPC_SYCC && m_Image->numcomps == 3 &&
-      m_Image->comps[0].dx == m_Image->comps[0].dy &&
-      m_Image->comps[1].dx != 1) {
+  auto components = components_span(m_Image);
+  if (m_Image->color_space != OPJ_CLRSPC_SYCC && components.size() == 3 &&
+      components[0].dx == components[0].dy && components[1].dx != 1) {
     m_Image->color_space = OPJ_CLRSPC_SYCC;
   } else if (m_Image->numcomps <= 2) {
     m_Image->color_space = OPJ_CLRSPC_GRAY;
@@ -517,7 +532,9 @@
 }
 
 CJPX_Decoder::JpxImageInfo CJPX_Decoder::GetInfo() const {
-  return {m_Image->comps[0].w, m_Image->comps[0].h, m_Image->numcomps,
+  const auto components = components_span(m_Image);
+  return {components[0].w, components[0].h,
+          pdfium::checked_cast<uint32_t>(components.size()),
           m_Image->color_space};
 }
 
@@ -551,13 +568,14 @@
   fxcrt::spanset(dest_buf.first(m_Image->comps[0].h * pitch), 0xff);
   std::vector<uint8_t*> channel_bufs(m_Image->numcomps);
   std::vector<int> adjust_comps(m_Image->numcomps);
-  for (uint32_t i = 0; i < m_Image->numcomps; i++) {
+  const pdfium::span<opj_image_comp_t> components = components_span(m_Image);
+  for (size_t i = 0; i < components.size(); i++) {
     channel_bufs[i] = dest_buf.subspan(i).data();
-    adjust_comps[i] = m_Image->comps[i].prec - 8;
+    adjust_comps[i] = components[i].prec - 8;
     if (i > 0) {
-      if (m_Image->comps[i].dx != m_Image->comps[i - 1].dx ||
-          m_Image->comps[i].dy != m_Image->comps[i - 1].dy ||
-          m_Image->comps[i].prec != m_Image->comps[i - 1].prec) {
+      if (components[i].dx != components[i - 1].dx ||
+          components[i].dy != components[i - 1].dy ||
+          components[i].prec != components[i - 1].prec) {
         return false;
       }
     }
@@ -565,48 +583,50 @@
   if (swap_rgb)
     std::swap(channel_bufs[0], channel_bufs[2]);
 
-  uint32_t width = m_Image->comps[0].w;
-  uint32_t height = m_Image->comps[0].h;
+  uint32_t width = components[0].w;
+  uint32_t height = components[0].h;
   for (uint32_t channel = 0; channel < channel_count; ++channel) {
     uint8_t* pChannel = channel_bufs[channel];
     const int adjust = adjust_comps[channel];
-    const opj_image_comp_t& comps = m_Image->comps[channel];
+    const opj_image_comp_t& comps = components[channel];
     if (!comps.data)
       continue;
 
     // Perfomance-sensitive code below. Combining these 3 for-loops below will
     // cause a slowdown.
-    const uint32_t src_offset = comps.sgnd ? 1 << (comps.prec - 1) : 0;
-    if (adjust < 0) {
-      for (uint32_t row = 0; row < height; ++row) {
-        uint8_t* pScanline = pChannel + row * pitch;
-        for (uint32_t col = 0; col < width; ++col) {
-          uint8_t* pPixel = pScanline + col * channel_count;
-          int src = comps.data[row * width + col] + src_offset;
-          *pPixel = static_cast<uint8_t>(src << -adjust);
+    UNSAFE_TODO({
+      const uint32_t src_offset = comps.sgnd ? 1 << (comps.prec - 1) : 0;
+      if (adjust < 0) {
+        for (uint32_t row = 0; row < height; ++row) {
+          uint8_t* pScanline = pChannel + row * pitch;
+          for (uint32_t col = 0; col < width; ++col) {
+            uint8_t* pPixel = pScanline + col * channel_count;
+            int src = comps.data[row * width + col] + src_offset;
+            *pPixel = static_cast<uint8_t>(src << -adjust);
+          }
+        }
+      } else if (adjust == 0) {
+        for (uint32_t row = 0; row < height; ++row) {
+          uint8_t* pScanline = pChannel + row * pitch;
+          for (uint32_t col = 0; col < width; ++col) {
+            uint8_t* pPixel = pScanline + col * channel_count;
+            int src = comps.data[row * width + col] + src_offset;
+            *pPixel = static_cast<uint8_t>(src);
+          }
+        }
+      } else {
+        for (uint32_t row = 0; row < height; ++row) {
+          uint8_t* pScanline = pChannel + row * pitch;
+          for (uint32_t col = 0; col < width; ++col) {
+            uint8_t* pPixel = pScanline + col * channel_count;
+            int src = comps.data[row * width + col] + src_offset;
+            int pixel = (src >> adjust) + ((src >> (adjust - 1)) % 2);
+            pixel = std::clamp(pixel, 0, 255);
+            *pPixel = static_cast<uint8_t>(pixel);
+          }
         }
       }
-    } else if (adjust == 0) {
-      for (uint32_t row = 0; row < height; ++row) {
-        uint8_t* pScanline = pChannel + row * pitch;
-        for (uint32_t col = 0; col < width; ++col) {
-          uint8_t* pPixel = pScanline + col * channel_count;
-          int src = comps.data[row * width + col] + src_offset;
-          *pPixel = static_cast<uint8_t>(src);
-        }
-      }
-    } else {
-      for (uint32_t row = 0; row < height; ++row) {
-        uint8_t* pScanline = pChannel + row * pitch;
-        for (uint32_t col = 0; col < width; ++col) {
-          uint8_t* pPixel = pScanline + col * channel_count;
-          int src = comps.data[row * width + col] + src_offset;
-          int pixel = (src >> adjust) + ((src >> (adjust - 1)) % 2);
-          pixel = std::clamp(pixel, 0, 255);
-          *pPixel = static_cast<uint8_t>(pixel);
-        }
-      }
-    }
+    });
   }
   return true;
 }