From 58854942e06d96972bf4722ab0a90249650d07e0 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 15 May 2017 17:38:19 -0700 Subject: Simplify ContrastAdjust(). Add a few constants in the process. Change-Id: Id69b939e4ea6a3de879e0a1f29d1453e95c838db Reviewed-on: https://pdfium-review.googlesource.com/5552 Commit-Queue: dsinclair Reviewed-by: dsinclair --- core/fxge/ge/cfx_facecache.cpp | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'core/fxge') diff --git a/core/fxge/ge/cfx_facecache.cpp b/core/fxge/ge/cfx_facecache.cpp index 6d5e3d4355..9c79444dd3 100644 --- a/core/fxge/ge/cfx_facecache.cpp +++ b/core/fxge/ge/cfx_facecache.cpp @@ -35,44 +35,48 @@ namespace { constexpr uint32_t kInvalidGlyphIndex = static_cast(-1); +constexpr int kMinPixel = 0; +constexpr int kMaxPixel = 255; + +constexpr int kMaxGlyphDimension = 2048; + void ContrastAdjust(uint8_t* pDataIn, uint8_t* pDataOut, int nWidth, int nHeight, int nSrcRowBytes, int nDstRowBytes) { - int col, row, temp; - int max = 0, min = 255; - float rate; - for (row = 0; row < nHeight; row++) { + int max = kMinPixel; + int min = kMaxPixel; + for (int row = 0; row < nHeight; row++) { uint8_t* pRow = pDataIn + row * nSrcRowBytes; - for (col = 0; col < nWidth; col++) { - temp = *pRow++; - max = std::max(temp, max); - min = std::min(temp, min); + for (int col = 0; col < nWidth; col++) { + int val = pRow[col]; + max = std::max(val, max); + min = std::min(val, min); } } - temp = max - min; - if (temp == 0 || temp == 255) { + int diff = max - min; + if (diff == kMinPixel || diff == kMaxPixel) { int rowbytes = std::min(abs(nSrcRowBytes), nDstRowBytes); - for (row = 0; row < nHeight; row++) { + for (int row = 0; row < nHeight; row++) { memcpy(pDataOut + row * nDstRowBytes, pDataIn + row * nSrcRowBytes, rowbytes); } return; } - rate = 255.f / temp; - for (row = 0; row < nHeight; row++) { + float rate = 255.f / diff; + for (int row = 0; row < nHeight; row++) { uint8_t* pSrcRow = pDataIn + row * nSrcRowBytes; uint8_t* pDstRow = pDataOut + row * nDstRowBytes; - for (col = 0; col < nWidth; col++) { - temp = static_cast((*(pSrcRow++) - min) * rate + 0.5); - temp = std::min(temp, 255); - temp = std::max(temp, 0); - *pDstRow++ = (uint8_t)temp; + for (int col = 0; col < nWidth; col++) { + int val = static_cast((pSrcRow[col] - min) * rate + 0.5); + pDstRow[col] = + static_cast(pdfium::clamp(val, kMinPixel, kMaxPixel)); } } } + } // namespace CFX_FaceCache::CFX_FaceCache(FXFT_Face face) @@ -179,7 +183,7 @@ std::unique_ptr CFX_FaceCache::RenderGlyph( return nullptr; int bmwidth = FXFT_Get_Bitmap_Width(FXFT_Get_Glyph_Bitmap(m_Face)); int bmheight = FXFT_Get_Bitmap_Rows(FXFT_Get_Glyph_Bitmap(m_Face)); - if (bmwidth > 2048 || bmheight > 2048) + if (bmwidth > kMaxGlyphDimension || bmheight > kMaxGlyphDimension) return nullptr; int dib_width = bmwidth; auto pGlyphBitmap = pdfium::MakeUnique(); -- cgit v1.2.3