From 60cc3baa05136bccf5d77e0b2cf3000817915bcd Mon Sep 17 00:00:00 2001 From: Nicolas Pena Date: Fri, 5 May 2017 17:11:35 -0400 Subject: Cleanup CStretchEngine and its CWeightTable This CL cleans up a bunch of nits and improves ownership in the CStretchEngine class. Change-Id: I6527f29c50dab329ef58d0724cd24b94fca50ee6 Reviewed-on: https://pdfium-review.googlesource.com/4970 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- core/fxge/dib/cfx_bitmapcomposer.cpp | 4 +- core/fxge/dib/cstretchengine.cpp | 620 +++++++++++++++-------------------- core/fxge/dib/cstretchengine.h | 15 +- 3 files changed, 282 insertions(+), 357 deletions(-) diff --git a/core/fxge/dib/cfx_bitmapcomposer.cpp b/core/fxge/dib/cfx_bitmapcomposer.cpp index bd634ad574..604c0c2ce0 100644 --- a/core/fxge/dib/cfx_bitmapcomposer.cpp +++ b/core/fxge/dib/cfx_bitmapcomposer.cpp @@ -45,6 +45,7 @@ void CFX_BitmapComposer::Compose(const CFX_RetainPtr& pDest, m_bRgbByteOrder = bRgbByteOrder; m_BlendType = blend_type; } + bool CFX_BitmapComposer::SetInfo(int width, int height, FXDIB_Format src_format, @@ -107,11 +108,12 @@ void CFX_BitmapComposer::ComposeScanline(int line, return; } const uint8_t* clip_scan = nullptr; - if (m_pClipMask) + if (m_pClipMask) { clip_scan = m_pClipMask->GetBuffer() + (m_DestTop + line - m_pClipRgn->GetBox().top) * m_pClipMask->GetPitch() + (m_DestLeft - m_pClipRgn->GetBox().left); + } uint8_t* dest_scan = const_cast(m_pBitmap->GetScanline(line + m_DestTop)) + m_DestLeft * m_pBitmap->GetBPP() / 8; diff --git a/core/fxge/dib/cstretchengine.cpp b/core/fxge/dib/cstretchengine.cpp index 9fdaa02f95..b5cf4a781d 100644 --- a/core/fxge/dib/cstretchengine.cpp +++ b/core/fxge/dib/cstretchengine.cpp @@ -6,20 +6,26 @@ #include "core/fxge/dib/cstretchengine.h" +#include +#include + #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/dib/cfx_dibsource.h" #include "core/fxge/dib/ifx_scanlinecomposer.h" #include "core/fxge/fx_dib.h" +namespace { + +const int kMaxDestValue = 16711680; + +} // namespace + CStretchEngine::CWeightTable::CWeightTable() : m_DestMin(0), m_ItemSize(0), - m_pWeightTables(nullptr), m_dwWeightTablesSize(0) {} -CStretchEngine::CWeightTable::~CWeightTable() { - FX_Free(m_pWeightTables); -} +CStretchEngine::CWeightTable::~CWeightTable() {} size_t CStretchEngine::CWeightTable::GetPixelWeightSize() const { return m_ItemSize / sizeof(int) - 2; @@ -32,64 +38,59 @@ bool CStretchEngine::CWeightTable::Calc(int dest_len, int src_min, int src_max, int flags) { - FX_Free(m_pWeightTables); - m_pWeightTables = nullptr; + m_WeightTables.clear(); m_dwWeightTablesSize = 0; - const double scale = (float)src_len / (float)dest_len; - const double base = dest_len < 0 ? (float)(src_len) : 0; + const double scale = static_cast(src_len) / dest_len; + const double base = dest_len < 0 ? src_len : 0; const int ext_size = flags & FXDIB_BICUBIC_INTERPOL ? 3 : 1; - m_ItemSize = sizeof(int) * 2 + - (int)(sizeof(int) * (ceil(fabs((float)scale)) + ext_size)); + m_ItemSize = + sizeof(int) * 2 + + static_cast(sizeof(int) * + (ceil(fabs(static_cast(scale))) + ext_size)); + m_DestMin = dest_min; - if ((dest_max - dest_min) > (int)((1U << 30) - 4) / m_ItemSize) + if (dest_max - dest_min > static_cast((1U << 30) - 4) / m_ItemSize) return false; m_dwWeightTablesSize = (dest_max - dest_min) * m_ItemSize + 4; - m_pWeightTables = FX_TryAlloc(uint8_t, m_dwWeightTablesSize); - if (!m_pWeightTables) - return false; - - if ((flags & FXDIB_NOSMOOTH) != 0 || fabs((float)scale) < 1.0f) { - for (int dest_pixel = dest_min; dest_pixel < dest_max; dest_pixel++) { + m_WeightTables.resize(m_dwWeightTablesSize); + if ((flags & FXDIB_NOSMOOTH) != 0 || fabs(static_cast(scale)) < 1.0f) { + for (int dest_pixel = dest_min; dest_pixel < dest_max; ++dest_pixel) { PixelWeight& pixel_weights = *GetPixelWeight(dest_pixel); double src_pos = dest_pixel * scale + scale / 2 + base; if (flags & FXDIB_INTERPOL) { - pixel_weights.m_SrcStart = (int)floor((float)src_pos - 1.0f / 2); - pixel_weights.m_SrcEnd = (int)floor((float)src_pos + 1.0f / 2); - if (pixel_weights.m_SrcStart < src_min) { - pixel_weights.m_SrcStart = src_min; - } - if (pixel_weights.m_SrcEnd >= src_max) { - pixel_weights.m_SrcEnd = src_max - 1; - } + pixel_weights.m_SrcStart = + static_cast(floor(static_cast(src_pos) - 1.0f / 2)); + pixel_weights.m_SrcEnd = + static_cast(floor(static_cast(src_pos) + 1.0f / 2)); + pixel_weights.m_SrcStart = std::max(pixel_weights.m_SrcStart, src_min); + pixel_weights.m_SrcEnd = std::min(pixel_weights.m_SrcEnd, src_max - 1); if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) { pixel_weights.m_Weights[0] = 65536; } else { - pixel_weights.m_Weights[1] = FXSYS_round( - (float)(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * 65536); + pixel_weights.m_Weights[1] = + FXSYS_round(static_cast( + src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * + 65536); pixel_weights.m_Weights[0] = 65536 - pixel_weights.m_Weights[1]; } } else if (flags & FXDIB_BICUBIC_INTERPOL) { - pixel_weights.m_SrcStart = (int)floor((float)src_pos - 1.0f / 2); - pixel_weights.m_SrcEnd = (int)floor((float)src_pos + 1.0f / 2); + pixel_weights.m_SrcStart = + static_cast(floor(static_cast(src_pos) - 1.0f / 2)); + pixel_weights.m_SrcEnd = + static_cast(floor(static_cast(src_pos) + 1.0f / 2)); int start = pixel_weights.m_SrcStart - 1; int end = pixel_weights.m_SrcEnd + 1; - if (start < src_min) { - start = src_min; - } - if (end >= src_max) { - end = src_max - 1; - } + start = std::max(start, src_min); + end = std::min(end, src_max - 1); if (pixel_weights.m_SrcStart < src_min) { src_pos += src_min - pixel_weights.m_SrcStart; pixel_weights.m_SrcStart = src_min; } - if (pixel_weights.m_SrcEnd >= src_max) { - pixel_weights.m_SrcEnd = src_max - 1; - } - int weight; - weight = FXSYS_round( - (float)(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * 256); + pixel_weights.m_SrcEnd = std::min(pixel_weights.m_SrcEnd, src_max - 1); + int weight = FXSYS_round( + static_cast(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * + 256); if (start == end) { pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight] + @@ -153,70 +154,48 @@ bool CStretchEngine::CWeightTable::Calc(int dest_len, pixel_weights.m_SrcEnd = end; } } else { - pixel_weights.m_SrcStart = pixel_weights.m_SrcEnd = - (int)floor((float)src_pos); - if (pixel_weights.m_SrcStart < src_min) { - pixel_weights.m_SrcStart = src_min; - } - if (pixel_weights.m_SrcEnd >= src_max) { - pixel_weights.m_SrcEnd = src_max - 1; - } + int pixel_pos = static_cast(floor(static_cast(src_pos))); + pixel_weights.m_SrcStart = std::max(pixel_pos, src_min); + pixel_weights.m_SrcEnd = std::min(pixel_pos, src_max - 1); pixel_weights.m_Weights[0] = 65536; } } return true; } - for (int dest_pixel = dest_min; dest_pixel < dest_max; dest_pixel++) { + for (int dest_pixel = dest_min; dest_pixel < dest_max; ++dest_pixel) { PixelWeight& pixel_weights = *GetPixelWeight(dest_pixel); double src_start = dest_pixel * scale + base; double src_end = src_start + scale; - int start_i, end_i; - if (src_start < src_end) { - start_i = (int)floor((float)src_start); - end_i = (int)ceil((float)src_end); - } else { - start_i = (int)floor((float)src_end); - end_i = (int)ceil((float)src_start); - } - if (start_i < src_min) { - start_i = src_min; - } - if (end_i >= src_max) { - end_i = src_max - 1; - } + int start_i = floor(std::min(src_start, src_end)); + int end_i = floor(std::max(src_start, src_end)); + start_i = std::max(start_i, src_min); + end_i = std::min(end_i, src_max - 1); if (start_i > end_i) { - if (start_i >= src_max) { - start_i = src_max - 1; - } + start_i = std::min(start_i, src_max - 1); pixel_weights.m_SrcStart = start_i; pixel_weights.m_SrcEnd = start_i; continue; } pixel_weights.m_SrcStart = start_i; pixel_weights.m_SrcEnd = end_i; - for (int j = start_i; j <= end_i; j++) { - double dest_start = ((float)j - base) / scale; - double dest_end = ((float)(j + 1) - base) / scale; - if (dest_start > dest_end) { - double temp = dest_start; - dest_start = dest_end; - dest_end = temp; - } - double area_start = - dest_start > (float)(dest_pixel) ? dest_start : (float)(dest_pixel); - double area_end = dest_end > (float)(dest_pixel + 1) - ? (float)(dest_pixel + 1) - : dest_end; - double weight = area_start >= area_end ? 0.0f : area_end - area_start; + for (int j = start_i; j <= end_i; ++j) { + double dest_start = (j - base) / scale; + double dest_end = (j + 1 - base) / scale; + if (dest_start > dest_end) + std::swap(dest_start, dest_end); + double area_start = std::max(dest_start, static_cast(dest_pixel)); + double area_end = std::min(dest_end, static_cast(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.m_SrcEnd; break; } size_t idx = j - start_i; if (idx >= GetPixelWeightSize()) return false; - pixel_weights.m_Weights[idx] = FXSYS_round((float)(weight * 65536)); + + pixel_weights.m_Weights[idx] = FXSYS_round(weight * 65536); } } return true; @@ -224,8 +203,8 @@ bool CStretchEngine::CWeightTable::Calc(int dest_len, PixelWeight* CStretchEngine::CWeightTable::GetPixelWeight(int pixel) const { ASSERT(pixel >= m_DestMin); - return reinterpret_cast(m_pWeightTables + - (pixel - m_DestMin) * m_ItemSize); + return reinterpret_cast(const_cast( + m_WeightTables.data() + (pixel - m_DestMin) * m_ItemSize)); } int* CStretchEngine::CWeightTable::GetValueFromPixelWeight(PixelWeight* pWeight, @@ -253,30 +232,22 @@ CStretchEngine::CStretchEngine(IFX_ScanlineComposer* pDestBitmap, m_pDestBitmap = pDestBitmap; m_DestWidth = dest_width; m_DestHeight = dest_height; - m_pInterBuf = nullptr; - m_pExtraAlphaBuf = nullptr; - m_pDestMaskScanline = nullptr; m_DestClip = clip_rect; uint32_t size = clip_rect.Width(); - if (size && m_DestBpp > (int)(INT_MAX / size)) { + if (size && m_DestBpp > static_cast(INT_MAX / size)) return; - } + size *= m_DestBpp; - if (size > INT_MAX - 31) { + if (size > INT_MAX - 31) return; - } + size += 31; size = size / 32 * 4; - m_pDestScanline = FX_TryAlloc(uint8_t, size); - if (!m_pDestScanline) { - return; - } - if (dest_format == FXDIB_Rgb32) { - memset(m_pDestScanline, 255, size); - } + m_DestScanline.resize(size); + if (dest_format == FXDIB_Rgb32) + std::fill(m_DestScanline.begin(), m_DestScanline.end(), 255); m_InterPitch = (m_DestClip.Width() * m_DestBpp + 31) / 32 * 4; m_ExtraMaskPitch = (m_DestClip.Width() * 8 + 31) / 32 * 4; - m_pInterBuf = nullptr; m_pSource = pSrcBitmap; m_SrcWidth = pSrcBitmap->GetWidth(); m_SrcHeight = pSrcBitmap->GetHeight(); @@ -291,69 +262,53 @@ CStretchEngine::CStretchEngine(IFX_ScanlineComposer* pDestBitmap, m_Flags = flags; } else { m_Flags = FXDIB_NOSMOOTH; - if (flags & FXDIB_DOWNSAMPLE) { + if (flags & FXDIB_DOWNSAMPLE) m_Flags |= FXDIB_DOWNSAMPLE; - } - } - double scale_x = (float)m_SrcWidth / (float)m_DestWidth; - double scale_y = (float)m_SrcHeight / (float)m_DestHeight; - double base_x = m_DestWidth > 0 ? 0.0f : (float)(m_DestWidth); - double base_y = m_DestHeight > 0 ? 0.0f : (float)(m_DestHeight); - double src_left = scale_x * ((float)(clip_rect.left) + base_x); - double src_right = scale_x * ((float)(clip_rect.right) + base_x); - double src_top = scale_y * ((float)(clip_rect.top) + base_y); - double src_bottom = scale_y * ((float)(clip_rect.bottom) + base_y); - if (src_left > src_right) { - double temp = src_left; - src_left = src_right; - src_right = temp; } - if (src_top > src_bottom) { - double temp = src_top; - src_top = src_bottom; - src_bottom = temp; - } - m_SrcClip.left = (int)floor((float)src_left); - m_SrcClip.right = (int)ceil((float)src_right); - m_SrcClip.top = (int)floor((float)src_top); - m_SrcClip.bottom = (int)ceil((float)src_bottom); + double scale_x = static_cast(m_SrcWidth) / m_DestWidth; + double scale_y = static_cast(m_SrcHeight) / m_DestHeight; + double base_x = m_DestWidth > 0 ? 0.0f : m_DestWidth; + double base_y = m_DestHeight > 0 ? 0.0f : m_DestHeight; + double src_left = scale_x * (clip_rect.left + base_x); + double src_right = scale_x * (clip_rect.right + base_x); + double src_top = scale_y * (clip_rect.top + base_y); + double src_bottom = scale_y * (clip_rect.bottom + base_y); + if (src_left > src_right) + std::swap(src_left, src_right); + if (src_top > src_bottom) + std::swap(src_top, src_bottom); + m_SrcClip.left = static_cast(floor(src_left)); + m_SrcClip.right = static_cast(ceil(src_right)); + m_SrcClip.top = static_cast(floor(src_top)); + m_SrcClip.bottom = static_cast(ceil(src_bottom)); FX_RECT src_rect(0, 0, m_SrcWidth, m_SrcHeight); m_SrcClip.Intersect(src_rect); if (m_SrcBpp == 1) { - if (m_DestBpp == 8) { + if (m_DestBpp == 8) m_TransMethod = 1; - } else { + else m_TransMethod = 2; - } } else if (m_SrcBpp == 8) { if (m_DestBpp == 8) { - if (!m_bHasAlpha) { + if (!m_bHasAlpha) m_TransMethod = 3; - } else { + else m_TransMethod = 4; - } } else { - if (!m_bHasAlpha) { + if (!m_bHasAlpha) m_TransMethod = 5; - } else { + else m_TransMethod = 6; - } } } else { - if (!m_bHasAlpha) { + if (!m_bHasAlpha) m_TransMethod = 7; - } else { + else m_TransMethod = 8; - } } } -CStretchEngine::~CStretchEngine() { - FX_Free(m_pDestScanline); - FX_Free(m_pInterBuf); - FX_Free(m_pExtraAlphaBuf); - FX_Free(m_pDestMaskScanline); -} +CStretchEngine::~CStretchEngine() {} bool CStretchEngine::Continue(IFX_Pause* pPause) { while (m_State == 1) { @@ -367,7 +322,7 @@ bool CStretchEngine::Continue(IFX_Pause* pPause) { } bool CStretchEngine::StartStretchHorz() { - if (m_DestWidth == 0 || m_InterPitch == 0 || !m_pDestScanline) + if (m_DestWidth == 0 || m_InterPitch == 0 || m_DestScanline.empty()) return false; if (m_SrcClip.Height() == 0 || @@ -375,17 +330,11 @@ bool CStretchEngine::StartStretchHorz() { return false; } - m_pInterBuf = FX_TryAlloc(unsigned char, m_SrcClip.Height() * m_InterPitch); - if (!m_pInterBuf) - return false; - + m_InterBuf.resize(m_SrcClip.Height() * m_InterPitch); if (m_pSource && m_bHasAlpha && m_pSource->m_pAlphaMask) { - m_pExtraAlphaBuf = - FX_Alloc2D(unsigned char, m_SrcClip.Height(), m_ExtraMaskPitch); + m_ExtraAlphaBuf.resize(m_SrcClip.Height(), m_ExtraMaskPitch); uint32_t size = (m_DestClip.Width() * 8 + 31) / 32 * 4; - m_pDestMaskScanline = FX_TryAlloc(unsigned char, size); - if (!m_pDestMaskScanline) - return false; + m_DestMaskScanline.resize(size); } bool ret = m_WeightTable.Calc(m_DestWidth, m_DestClip.left, m_DestClip.right, @@ -407,7 +356,7 @@ bool CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause) { int Bpp = m_DestBpp / 8; static const int kStrechPauseRows = 10; int rows_to_go = kStrechPauseRows; - for (; m_CurRow < m_SrcClip.bottom; m_CurRow++) { + for (; m_CurRow < m_SrcClip.bottom; ++m_CurRow) { if (rows_to_go == 0) { if (pPause && pPause->NeedToPauseNow()) return true; @@ -417,68 +366,61 @@ bool CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause) { const uint8_t* src_scan = m_pSource->GetScanline(m_CurRow); uint8_t* dest_scan = - m_pInterBuf + (m_CurRow - m_SrcClip.top) * m_InterPitch; + m_InterBuf.data() + (m_CurRow - m_SrcClip.top) * m_InterPitch; const uint8_t* src_scan_mask = nullptr; uint8_t* dest_scan_mask = nullptr; - if (m_pExtraAlphaBuf) { + if (!m_ExtraAlphaBuf.empty()) { src_scan_mask = m_pSource->m_pAlphaMask->GetScanline(m_CurRow); - dest_scan_mask = - m_pExtraAlphaBuf + (m_CurRow - m_SrcClip.top) * m_ExtraMaskPitch; + dest_scan_mask = m_ExtraAlphaBuf.data() + + (m_CurRow - m_SrcClip.top) * m_ExtraMaskPitch; } + // TODO(npm): reduce duplicated code here switch (m_TransMethod) { case 1: case 2: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); int dest_a = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; int pixel_weight = *pWeight; - if (src_scan[j / 8] & (1 << (7 - j % 8))) { + if (src_scan[j / 8] & (1 << (7 - j % 8))) dest_a += pixel_weight * 255; - } } - if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a; - } - *dest_scan++ = (uint8_t)(dest_a >> 16); + if (m_Flags & FXDIB_BICUBIC_INTERPOL) + dest_a = pdfium::clamp(dest_a, 0, kMaxDestValue); + *dest_scan++ = static_cast(dest_a >> 16); } break; } case 3: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); int dest_a = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; int pixel_weight = *pWeight; dest_a += pixel_weight * src_scan[j]; } - if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a; - } - *dest_scan++ = (uint8_t)(dest_a >> 16); + if (m_Flags & FXDIB_BICUBIC_INTERPOL) + dest_a = pdfium::clamp(dest_a, 0, kMaxDestValue); + *dest_scan++ = static_cast(dest_a >> 16); } break; } case 4: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); - int dest_a = 0, dest_r = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); + int dest_a = 0; + int dest_r = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; @@ -488,59 +430,57 @@ bool CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause) { dest_a += pixel_weight; } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_r = dest_r < 0 ? 0 : dest_r > 16711680 ? 16711680 : dest_r; - dest_a = dest_a < 0 ? 0 : dest_a > 65536 ? 65536 : dest_a; + dest_r = pdfium::clamp(dest_r, 0, kMaxDestValue); + dest_a = pdfium::clamp(dest_a, 0, 65536); } - *dest_scan++ = (uint8_t)(dest_r >> 16); - *dest_scan_mask++ = (uint8_t)((dest_a * 255) >> 16); + *dest_scan++ = static_cast(dest_r >> 16); + *dest_scan_mask++ = static_cast((dest_a * 255) >> 16); } break; } case 5: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); - int dest_r_y = 0, dest_g_m = 0, dest_b_c = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); + int dest_r_y = 0; + int dest_g_m = 0; + int dest_b_c = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; int pixel_weight = *pWeight; unsigned long argb_cmyk = m_pSrcPalette[src_scan[j]]; if (m_DestFormat == FXDIB_Rgb) { - dest_r_y += pixel_weight * (uint8_t)(argb_cmyk >> 16); - dest_g_m += pixel_weight * (uint8_t)(argb_cmyk >> 8); - dest_b_c += pixel_weight * (uint8_t)argb_cmyk; + dest_r_y += pixel_weight * static_cast(argb_cmyk >> 16); + dest_g_m += pixel_weight * static_cast(argb_cmyk >> 8); + dest_b_c += pixel_weight * static_cast(argb_cmyk); } else { - dest_b_c += pixel_weight * (uint8_t)(argb_cmyk >> 24); - dest_g_m += pixel_weight * (uint8_t)(argb_cmyk >> 16); - dest_r_y += pixel_weight * (uint8_t)(argb_cmyk >> 8); + dest_b_c += pixel_weight * static_cast(argb_cmyk >> 24); + dest_g_m += pixel_weight * static_cast(argb_cmyk >> 16); + dest_r_y += pixel_weight * static_cast(argb_cmyk >> 8); } } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_r_y = - dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y; - dest_g_m = - dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m; - dest_b_c = - dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c; + dest_r_y = pdfium::clamp(dest_r_y, 0, kMaxDestValue); + dest_g_m = pdfium::clamp(dest_g_m, 0, kMaxDestValue); + dest_b_c = pdfium::clamp(dest_b_c, 0, kMaxDestValue); } - *dest_scan++ = (uint8_t)(dest_b_c >> 16); - *dest_scan++ = (uint8_t)(dest_g_m >> 16); - *dest_scan++ = (uint8_t)(dest_r_y >> 16); + *dest_scan++ = static_cast(dest_b_c >> 16); + *dest_scan++ = static_cast(dest_g_m >> 16); + *dest_scan++ = static_cast(dest_r_y >> 16); } break; } case 6: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); - int dest_a = 0, dest_r_y = 0, dest_g_m = 0, dest_b_c = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); + int dest_a = 0; + int dest_r_y = 0; + int dest_g_m = 0; + int dest_b_c = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; @@ -548,40 +488,37 @@ bool CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause) { pixel_weight = pixel_weight * src_scan_mask[j] / 255; unsigned long argb_cmyk = m_pSrcPalette[src_scan[j]]; if (m_DestFormat == FXDIB_Rgba) { - dest_r_y += pixel_weight * (uint8_t)(argb_cmyk >> 16); - dest_g_m += pixel_weight * (uint8_t)(argb_cmyk >> 8); - dest_b_c += pixel_weight * (uint8_t)argb_cmyk; + dest_r_y += pixel_weight * static_cast(argb_cmyk >> 16); + dest_g_m += pixel_weight * static_cast(argb_cmyk >> 8); + dest_b_c += pixel_weight * static_cast(argb_cmyk); } else { - dest_b_c += pixel_weight * (uint8_t)(argb_cmyk >> 24); - dest_g_m += pixel_weight * (uint8_t)(argb_cmyk >> 16); - dest_r_y += pixel_weight * (uint8_t)(argb_cmyk >> 8); + dest_b_c += pixel_weight * static_cast(argb_cmyk >> 24); + dest_g_m += pixel_weight * static_cast(argb_cmyk >> 16); + dest_r_y += pixel_weight * static_cast(argb_cmyk >> 8); } dest_a += pixel_weight; } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_b_c = - dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c; - dest_g_m = - dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m; - dest_r_y = - dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y; - dest_a = dest_a < 0 ? 0 : dest_a > 65536 ? 65536 : dest_a; + dest_b_c = pdfium::clamp(dest_b_c, 0, kMaxDestValue); + dest_g_m = pdfium::clamp(dest_g_m, 0, kMaxDestValue); + dest_r_y = pdfium::clamp(dest_r_y, 0, kMaxDestValue); + dest_a = pdfium::clamp(dest_a, 0, 65536); } - *dest_scan++ = (uint8_t)(dest_b_c >> 16); - *dest_scan++ = (uint8_t)(dest_g_m >> 16); - *dest_scan++ = (uint8_t)(dest_r_y >> 16); - *dest_scan_mask++ = (uint8_t)((dest_a * 255) >> 16); + *dest_scan++ = static_cast(dest_b_c >> 16); + *dest_scan++ = static_cast(dest_g_m >> 16); + *dest_scan++ = static_cast(dest_r_y >> 16); + *dest_scan_mask++ = static_cast((dest_a * 255) >> 16); } break; } case 7: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); - int dest_r_y = 0, dest_g_m = 0, dest_b_c = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); + int dest_r_y = 0; + int dest_g_m = 0; + int dest_b_c = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; @@ -592,28 +529,26 @@ bool CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause) { dest_r_y += pixel_weight * (*src_pixel); } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_b_c = - dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c; - dest_g_m = - dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m; - dest_r_y = - dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y; + dest_b_c = pdfium::clamp(dest_b_c, 0, kMaxDestValue); + dest_g_m = pdfium::clamp(dest_g_m, 0, kMaxDestValue); + dest_r_y = pdfium::clamp(dest_r_y, 0, kMaxDestValue); } - *dest_scan++ = (uint8_t)((dest_b_c) >> 16); - *dest_scan++ = (uint8_t)((dest_g_m) >> 16); - *dest_scan++ = (uint8_t)((dest_r_y) >> 16); + *dest_scan++ = static_cast((dest_b_c) >> 16); + *dest_scan++ = static_cast((dest_g_m) >> 16); + *dest_scan++ = static_cast((dest_r_y) >> 16); dest_scan += Bpp - 3; } break; } case 8: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { - PixelWeight* pPixelWeights = m_WeightTable.GetPixelWeight(col); - int dest_a = 0, dest_r_y = 0, dest_g_m = 0, dest_b_c = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = - m_WeightTable.GetValueFromPixelWeight(pPixelWeights, j); + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { + PixelWeight* pWeights = m_WeightTable.GetPixelWeight(col); + int dest_a = 0; + int dest_r_y = 0; + int dest_g_m = 0; + int dest_b_c = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = m_WeightTable.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return false; @@ -630,23 +565,18 @@ bool CStretchEngine::ContinueStretchHorz(IFX_Pause* pPause) { dest_a += pixel_weight; } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_r_y = - dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y; - dest_g_m = - dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m; - dest_b_c = - dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c; - dest_a = dest_a < 0 ? 0 : dest_a > 65536 ? 65536 : dest_a; - } - *dest_scan++ = (uint8_t)((dest_b_c) >> 16); - *dest_scan++ = (uint8_t)((dest_g_m) >> 16); - *dest_scan++ = (uint8_t)((dest_r_y) >> 16); - if (m_DestFormat == FXDIB_Argb) { - *dest_scan = (uint8_t)((dest_a * 255) >> 16); - } - if (dest_scan_mask) { - *dest_scan_mask++ = (uint8_t)((dest_a * 255) >> 16); + dest_r_y = pdfium::clamp(dest_r_y, 0, kMaxDestValue); + dest_g_m = pdfium::clamp(dest_g_m, 0, kMaxDestValue); + dest_b_c = pdfium::clamp(dest_b_c, 0, kMaxDestValue); + dest_a = pdfium::clamp(dest_a, 0, 65536); } + *dest_scan++ = static_cast((dest_b_c) >> 16); + *dest_scan++ = static_cast((dest_g_m) >> 16); + *dest_scan++ = static_cast((dest_r_y) >> 16); + if (m_DestFormat == FXDIB_Argb) + *dest_scan = static_cast((dest_a * 255) >> 16); + if (dest_scan_mask) + *dest_scan_mask++ = static_cast((dest_a * 255) >> 16); dest_scan += Bpp - 3; } break; @@ -668,21 +598,20 @@ void CStretchEngine::StretchVert() { return; const int DestBpp = m_DestBpp / 8; - for (int row = m_DestClip.top; row < m_DestClip.bottom; row++) { - unsigned char* dest_scan = m_pDestScanline; - unsigned char* dest_scan_mask = m_pDestMaskScanline; - PixelWeight* pPixelWeights = table.GetPixelWeight(row); + for (int row = m_DestClip.top; row < m_DestClip.bottom; ++row) { + unsigned char* dest_scan = m_DestScanline.data(); + unsigned char* dest_scan_mask = m_DestMaskScanline.data(); + PixelWeight* pWeights = table.GetPixelWeight(row); switch (m_TransMethod) { case 1: case 2: case 3: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { unsigned char* src_scan = - m_pInterBuf + (col - m_DestClip.left) * DestBpp; + m_InterBuf.data() + (col - m_DestClip.left) * DestBpp; int dest_a = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = table.GetValueFromPixelWeight(pPixelWeights, j); + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = table.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return; @@ -690,24 +619,23 @@ void CStretchEngine::StretchVert() { dest_a += pixel_weight * src_scan[(j - m_SrcClip.top) * m_InterPitch]; } - if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a; - } - *dest_scan = (uint8_t)(dest_a >> 16); + if (m_Flags & FXDIB_BICUBIC_INTERPOL) + dest_a = pdfium::clamp(dest_a, 0, kMaxDestValue); + *dest_scan = static_cast(dest_a >> 16); dest_scan += DestBpp; } break; } case 4: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { unsigned char* src_scan = - m_pInterBuf + (col - m_DestClip.left) * DestBpp; + m_InterBuf.data() + (col - m_DestClip.left) * DestBpp; unsigned char* src_scan_mask = - m_pExtraAlphaBuf + (col - m_DestClip.left); - int dest_a = 0, dest_k = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = table.GetValueFromPixelWeight(pPixelWeights, j); + m_ExtraAlphaBuf.data() + (col - m_DestClip.left); + int dest_a = 0; + int dest_k = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = table.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return; @@ -718,24 +646,25 @@ void CStretchEngine::StretchVert() { src_scan_mask[(j - m_SrcClip.top) * m_ExtraMaskPitch]; } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_k = dest_k < 0 ? 0 : dest_k > 16711680 ? 16711680 : dest_k; - dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a; + dest_k = pdfium::clamp(dest_k, 0, kMaxDestValue); + dest_a = pdfium::clamp(dest_a, 0, kMaxDestValue); } - *dest_scan = (uint8_t)(dest_k >> 16); + *dest_scan = static_cast(dest_k >> 16); dest_scan += DestBpp; - *dest_scan_mask++ = (uint8_t)(dest_a >> 16); + *dest_scan_mask++ = static_cast(dest_a >> 16); } break; } case 5: case 7: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { unsigned char* src_scan = - m_pInterBuf + (col - m_DestClip.left) * DestBpp; - int dest_r_y = 0, dest_g_m = 0, dest_b_c = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = table.GetValueFromPixelWeight(pPixelWeights, j); + m_InterBuf.data() + (col - m_DestClip.left) * DestBpp; + int dest_r_y = 0; + int dest_g_m = 0; + int dest_b_c = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = table.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return; @@ -747,33 +676,31 @@ void CStretchEngine::StretchVert() { dest_r_y += pixel_weight * (*src_pixel); } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_r_y = - dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y; - dest_g_m = - dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m; - dest_b_c = - dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c; + dest_r_y = pdfium::clamp(dest_r_y, 0, kMaxDestValue); + dest_g_m = pdfium::clamp(dest_g_m, 0, kMaxDestValue); + dest_b_c = pdfium::clamp(dest_b_c, 0, kMaxDestValue); } - dest_scan[0] = (uint8_t)((dest_b_c) >> 16); - dest_scan[1] = (uint8_t)((dest_g_m) >> 16); - dest_scan[2] = (uint8_t)((dest_r_y) >> 16); + dest_scan[0] = static_cast((dest_b_c) >> 16); + dest_scan[1] = static_cast((dest_g_m) >> 16); + dest_scan[2] = static_cast((dest_r_y) >> 16); dest_scan += DestBpp; } break; } case 6: case 8: { - for (int col = m_DestClip.left; col < m_DestClip.right; col++) { + for (int col = m_DestClip.left; col < m_DestClip.right; ++col) { unsigned char* src_scan = - m_pInterBuf + (col - m_DestClip.left) * DestBpp; + m_InterBuf.data() + (col - m_DestClip.left) * DestBpp; unsigned char* src_scan_mask = nullptr; - if (m_DestFormat != FXDIB_Argb) { - src_scan_mask = m_pExtraAlphaBuf + (col - m_DestClip.left); - } - int dest_a = 0, dest_r_y = 0, dest_g_m = 0, dest_b_c = 0; - for (int j = pPixelWeights->m_SrcStart; j <= pPixelWeights->m_SrcEnd; - j++) { - int* pWeight = table.GetValueFromPixelWeight(pPixelWeights, j); + if (m_DestFormat != FXDIB_Argb) + src_scan_mask = m_ExtraAlphaBuf.data() + (col - m_DestClip.left); + int dest_a = 0; + int dest_r_y = 0; + int dest_g_m = 0; + int dest_b_c = 0; + for (int j = pWeights->m_SrcStart; j <= pWeights->m_SrcEnd; ++j) { + int* pWeight = table.GetValueFromPixelWeight(pWeights, j); if (!pWeight) return; @@ -781,49 +708,42 @@ void CStretchEngine::StretchVert() { const uint8_t* src_pixel = src_scan + (j - m_SrcClip.top) * m_InterPitch; int mask_v = 255; - if (src_scan_mask) { + if (src_scan_mask) mask_v = src_scan_mask[(j - m_SrcClip.top) * m_ExtraMaskPitch]; - } dest_b_c += pixel_weight * (*src_pixel++); dest_g_m += pixel_weight * (*src_pixel++); dest_r_y += pixel_weight * (*src_pixel); - if (m_DestFormat == FXDIB_Argb) { + if (m_DestFormat == FXDIB_Argb) dest_a += pixel_weight * (*(src_pixel + 1)); - } else { + else dest_a += pixel_weight * mask_v; - } } if (m_Flags & FXDIB_BICUBIC_INTERPOL) { - dest_r_y = - dest_r_y < 0 ? 0 : dest_r_y > 16711680 ? 16711680 : dest_r_y; - dest_g_m = - dest_g_m < 0 ? 0 : dest_g_m > 16711680 ? 16711680 : dest_g_m; - dest_b_c = - dest_b_c < 0 ? 0 : dest_b_c > 16711680 ? 16711680 : dest_b_c; - dest_a = dest_a < 0 ? 0 : dest_a > 16711680 ? 16711680 : dest_a; + dest_r_y = pdfium::clamp(dest_r_y, 0, kMaxDestValue); + dest_g_m = pdfium::clamp(dest_g_m, 0, kMaxDestValue); + dest_b_c = pdfium::clamp(dest_b_c, 0, kMaxDestValue); + dest_a = pdfium::clamp(dest_a, 0, kMaxDestValue); } if (dest_a) { - int r = ((uint32_t)dest_r_y) * 255 / dest_a; - int g = ((uint32_t)dest_g_m) * 255 / dest_a; - int b = ((uint32_t)dest_b_c) * 255 / dest_a; - dest_scan[0] = b > 255 ? 255 : b < 0 ? 0 : b; - dest_scan[1] = g > 255 ? 255 : g < 0 ? 0 : g; - dest_scan[2] = r > 255 ? 255 : r < 0 ? 0 : r; - } - if (m_DestFormat == FXDIB_Argb) { - dest_scan[3] = (uint8_t)((dest_a) >> 16); - } else { - *dest_scan_mask = (uint8_t)((dest_a) >> 16); + int r = static_cast(dest_r_y) * 255 / dest_a; + int g = static_cast(dest_g_m) * 255 / dest_a; + int b = static_cast(dest_b_c) * 255 / dest_a; + dest_scan[0] = pdfium::clamp(b, 0, 255); + dest_scan[1] = pdfium::clamp(g, 0, 255); + dest_scan[2] = pdfium::clamp(r, 0, 255); } + if (m_DestFormat == FXDIB_Argb) + dest_scan[3] = static_cast((dest_a) >> 16); + else + *dest_scan_mask = static_cast((dest_a) >> 16); dest_scan += DestBpp; - if (dest_scan_mask) { + if (dest_scan_mask) dest_scan_mask++; - } } break; } } - m_pDestBitmap->ComposeScanline(row - m_DestClip.top, m_pDestScanline, - m_pDestMaskScanline); + m_pDestBitmap->ComposeScanline(row - m_DestClip.top, m_DestScanline.data(), + m_DestMaskScanline.data()); } } diff --git a/core/fxge/dib/cstretchengine.h b/core/fxge/dib/cstretchengine.h index 0d9457fb9d..280641cb59 100644 --- a/core/fxge/dib/cstretchengine.h +++ b/core/fxge/dib/cstretchengine.h @@ -7,6 +7,8 @@ #ifndef CORE_FXGE_DIB_CSTRETCHENGINE_H_ #define CORE_FXGE_DIB_CSTRETCHENGINE_H_ +#include + #include "core/fxcrt/fx_coordinates.h" #include "core/fxge/fx_dib.h" @@ -48,7 +50,7 @@ class CStretchEngine { private: int m_DestMin; int m_ItemSize; - uint8_t* m_pWeightTables; + std::vector m_WeightTables; size_t m_dwWeightTablesSize; }; @@ -57,10 +59,11 @@ class CStretchEngine { int m_SrcBpp; int m_bHasAlpha; IFX_ScanlineComposer* m_pDestBitmap; - int m_DestWidth, m_DestHeight; + int m_DestWidth; + int m_DestHeight; FX_RECT m_DestClip; - uint8_t* m_pDestScanline; - uint8_t* m_pDestMaskScanline; + std::vector m_DestScanline; + std::vector m_DestMaskScanline; FX_RECT m_SrcClip; CFX_RetainPtr m_pSource; uint32_t* m_pSrcPalette; @@ -69,8 +72,8 @@ class CStretchEngine { int m_SrcPitch; int m_InterPitch; int m_ExtraMaskPitch; - uint8_t* m_pInterBuf; - uint8_t* m_pExtraAlphaBuf; + std::vector m_InterBuf; + std::vector m_ExtraAlphaBuf; int m_TransMethod; int m_Flags; CWeightTable m_WeightTable; -- cgit v1.2.3