| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include "core/fxcodec/jbig2/JBig2_Image.h" | 9 #include "core/fxcodec/jbig2/JBig2_Image.h" |
| 10 #include "core/fxcrt/include/fx_coordinates.h" | 10 #include "core/fxcrt/include/fx_coordinates.h" |
| 11 #include "core/fxcrt/include/fx_safe_types.h" | 11 #include "core/fxcrt/include/fx_safe_types.h" |
| 12 | 12 |
| 13 CJBig2_Image::CJBig2_Image(int32_t w, int32_t h) { | 13 CJBig2_Image::CJBig2_Image(int32_t w, int32_t h) { |
| 14 m_nWidth = w; | 14 m_nWidth = w; |
| 15 m_nHeight = h; | 15 m_nHeight = h; |
| 16 if (m_nWidth <= 0 || m_nHeight <= 0 || m_nWidth > INT_MAX - 31) { | 16 if (m_nWidth <= 0 || m_nHeight <= 0 || m_nWidth > INT_MAX - 31) { |
| 17 m_pData = NULL; | 17 m_pData = nullptr; |
| 18 m_bNeedFree = FALSE; | 18 m_bNeedFree = FALSE; |
| 19 return; | 19 return; |
| 20 } | 20 } |
| 21 m_nStride = ((w + 31) >> 5) << 2; | 21 m_nStride = ((w + 31) >> 5) << 2; |
| 22 if (m_nStride * m_nHeight > 0 && 104857600 / (int)m_nStride > m_nHeight) { | 22 if (m_nStride * m_nHeight > 0 && 104857600 / (int)m_nStride > m_nHeight) { |
| 23 m_pData = FX_Alloc2D(uint8_t, m_nStride, m_nHeight); | 23 m_pData = FX_Alloc2D(uint8_t, m_nStride, m_nHeight); |
| 24 } else { | 24 } else { |
| 25 m_pData = NULL; | 25 m_pData = nullptr; |
| 26 } | 26 } |
| 27 m_bNeedFree = TRUE; | 27 m_bNeedFree = TRUE; |
| 28 } | 28 } |
| 29 CJBig2_Image::CJBig2_Image(int32_t w, | 29 CJBig2_Image::CJBig2_Image(int32_t w, |
| 30 int32_t h, | 30 int32_t h, |
| 31 int32_t stride, | 31 int32_t stride, |
| 32 uint8_t* pBuf) { | 32 uint8_t* pBuf) { |
| 33 m_nWidth = w; | 33 m_nWidth = w; |
| 34 m_nHeight = h; | 34 m_nHeight = h; |
| 35 m_nStride = stride; | 35 m_nStride = stride; |
| 36 m_pData = pBuf; | 36 m_pData = pBuf; |
| 37 m_bNeedFree = FALSE; | 37 m_bNeedFree = FALSE; |
| 38 } | 38 } |
| 39 CJBig2_Image::CJBig2_Image(const CJBig2_Image& im) { | 39 CJBig2_Image::CJBig2_Image(const CJBig2_Image& im) { |
| 40 m_nWidth = im.m_nWidth; | 40 m_nWidth = im.m_nWidth; |
| 41 m_nHeight = im.m_nHeight; | 41 m_nHeight = im.m_nHeight; |
| 42 m_nStride = im.m_nStride; | 42 m_nStride = im.m_nStride; |
| 43 if (im.m_pData) { | 43 if (im.m_pData) { |
| 44 m_pData = FX_Alloc2D(uint8_t, m_nStride, m_nHeight); | 44 m_pData = FX_Alloc2D(uint8_t, m_nStride, m_nHeight); |
| 45 JBIG2_memcpy(m_pData, im.m_pData, m_nStride * m_nHeight); | 45 JBIG2_memcpy(m_pData, im.m_pData, m_nStride * m_nHeight); |
| 46 } else { | 46 } else { |
| 47 m_pData = NULL; | 47 m_pData = nullptr; |
| 48 } | 48 } |
| 49 m_bNeedFree = TRUE; | 49 m_bNeedFree = TRUE; |
| 50 } | 50 } |
| 51 CJBig2_Image::~CJBig2_Image() { | 51 CJBig2_Image::~CJBig2_Image() { |
| 52 if (m_bNeedFree) { | 52 if (m_bNeedFree) { |
| 53 FX_Free(m_pData); | 53 FX_Free(m_pData); |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 FX_BOOL CJBig2_Image::getPixel(int32_t x, int32_t y) { | 56 FX_BOOL CJBig2_Image::getPixel(int32_t x, int32_t y) { |
| 57 if (!m_pData) { | 57 if (!m_pData) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 if (!m_pData) { | 113 if (!m_pData) { |
| 114 return FALSE; | 114 return FALSE; |
| 115 } | 115 } |
| 116 return composeTo_opt2(pDst, x, y, op); | 116 return composeTo_opt2(pDst, x, y, op); |
| 117 } | 117 } |
| 118 FX_BOOL CJBig2_Image::composeTo(CJBig2_Image* pDst, | 118 FX_BOOL CJBig2_Image::composeTo(CJBig2_Image* pDst, |
| 119 int32_t x, | 119 int32_t x, |
| 120 int32_t y, | 120 int32_t y, |
| 121 JBig2ComposeOp op, | 121 JBig2ComposeOp op, |
| 122 const FX_RECT* pSrcRect) { | 122 const FX_RECT* pSrcRect) { |
| 123 if (!m_pData) { | 123 if (!m_pData) |
| 124 return FALSE; | 124 return FALSE; |
| 125 } | 125 |
| 126 if (NULL == pSrcRect || *pSrcRect == FX_RECT(0, 0, m_nWidth, m_nHeight)) { | 126 if (!pSrcRect || *pSrcRect == FX_RECT(0, 0, m_nWidth, m_nHeight)) |
| 127 return composeTo_opt2(pDst, x, y, op); | 127 return composeTo_opt2(pDst, x, y, op); |
| 128 } | 128 |
| 129 return composeTo_opt2(pDst, x, y, op, pSrcRect); | 129 return composeTo_opt2(pDst, x, y, op, pSrcRect); |
| 130 } | 130 } |
| 131 | 131 |
| 132 FX_BOOL CJBig2_Image::composeFrom(int32_t x, | 132 FX_BOOL CJBig2_Image::composeFrom(int32_t x, |
| 133 int32_t y, | 133 int32_t y, |
| 134 CJBig2_Image* pSrc, | 134 CJBig2_Image* pSrc, |
| 135 JBig2ComposeOp op) { | 135 JBig2ComposeOp op) { |
| 136 if (!m_pData) { | 136 if (!m_pData) { |
| 137 return FALSE; | 137 return FALSE; |
| 138 } | 138 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 ((uint32_t)(((buf)[0] << 24) | ((buf)[1] << 16) | ((buf)[2] << 8) | (buf)[3])) | 152 ((uint32_t)(((buf)[0] << 24) | ((buf)[1] << 16) | ((buf)[2] << 8) | (buf)[3])) |
| 153 CJBig2_Image* CJBig2_Image::subImage(int32_t x, | 153 CJBig2_Image* CJBig2_Image::subImage(int32_t x, |
| 154 int32_t y, | 154 int32_t y, |
| 155 int32_t w, | 155 int32_t w, |
| 156 int32_t h) { | 156 int32_t h) { |
| 157 int32_t m, n, j; | 157 int32_t m, n, j; |
| 158 uint8_t *pLineSrc, *pLineDst; | 158 uint8_t *pLineSrc, *pLineDst; |
| 159 uint32_t wTmp; | 159 uint32_t wTmp; |
| 160 uint8_t *pSrc, *pSrcEnd, *pDst, *pDstEnd; | 160 uint8_t *pSrc, *pSrcEnd, *pDst, *pDstEnd; |
| 161 if (w == 0 || h == 0) { | 161 if (w == 0 || h == 0) { |
| 162 return NULL; | 162 return nullptr; |
| 163 } | 163 } |
| 164 CJBig2_Image* pImage = new CJBig2_Image(w, h); | 164 CJBig2_Image* pImage = new CJBig2_Image(w, h); |
| 165 if (!m_pData) { | 165 if (!m_pData) { |
| 166 pImage->fill(0); | 166 pImage->fill(0); |
| 167 return pImage; | 167 return pImage; |
| 168 } | 168 } |
| 169 if (!pImage->m_pData) { | 169 if (!pImage->m_pData) { |
| 170 return pImage; | 170 return pImage; |
| 171 } | 171 } |
| 172 pLineSrc = m_pData + m_nStride * y; | 172 pLineSrc = m_pData + m_nStride * y; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image* pDst, | 236 FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image* pDst, |
| 237 int32_t x, | 237 int32_t x, |
| 238 int32_t y, | 238 int32_t y, |
| 239 JBig2ComposeOp op) { | 239 JBig2ComposeOp op) { |
| 240 int32_t xs0 = 0, ys0 = 0, xs1 = 0, ys1 = 0, xd0 = 0, yd0 = 0, xd1 = 0, | 240 int32_t xs0 = 0, ys0 = 0, xs1 = 0, ys1 = 0, xd0 = 0, yd0 = 0, xd1 = 0, |
| 241 yd1 = 0, xx = 0, yy = 0, w = 0, h = 0, middleDwords = 0, lineLeft = 0; | 241 yd1 = 0, xx = 0, yy = 0, w = 0, h = 0, middleDwords = 0, lineLeft = 0; |
| 242 | 242 |
| 243 uint32_t s1 = 0, d1 = 0, d2 = 0, shift = 0, shift1 = 0, shift2 = 0, tmp = 0, | 243 uint32_t s1 = 0, d1 = 0, d2 = 0, shift = 0, shift1 = 0, shift2 = 0, tmp = 0, |
| 244 tmp1 = 0, tmp2 = 0, maskL = 0, maskR = 0, maskM = 0; | 244 tmp1 = 0, tmp2 = 0, maskL = 0, maskR = 0, maskM = 0; |
| 245 | 245 |
| 246 uint8_t *lineSrc = NULL, *lineDst = NULL, *sp = NULL, *dp = NULL; | 246 if (!m_pData) |
| 247 return FALSE; |
| 247 | 248 |
| 248 if (!m_pData) { | 249 if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) |
| 249 return FALSE; | 250 return FALSE; |
| 250 } | 251 |
| 251 if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) { | |
| 252 return FALSE; | |
| 253 } | |
| 254 if (y < 0) { | 252 if (y < 0) { |
| 255 ys0 = -y; | 253 ys0 = -y; |
| 256 } | 254 } |
| 257 if (y + m_nHeight > pDst->m_nHeight) { | 255 if (y + m_nHeight > pDst->m_nHeight) { |
| 258 ys1 = pDst->m_nHeight - y; | 256 ys1 = pDst->m_nHeight - y; |
| 259 } else { | 257 } else { |
| 260 ys1 = m_nHeight; | 258 ys1 = m_nHeight; |
| 261 } | 259 } |
| 262 if (x < 0) { | 260 if (x < 0) { |
| 263 xs0 = -x; | 261 xs0 = -x; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 279 xd0 = x; | 277 xd0 = x; |
| 280 } | 278 } |
| 281 xd1 = xd0 + w; | 279 xd1 = xd0 + w; |
| 282 yd1 = yd0 + h; | 280 yd1 = yd0 + h; |
| 283 d1 = xd0 & 31; | 281 d1 = xd0 & 31; |
| 284 d2 = xd1 & 31; | 282 d2 = xd1 & 31; |
| 285 s1 = xs0 & 31; | 283 s1 = xs0 & 31; |
| 286 maskL = 0xffffffff >> d1; | 284 maskL = 0xffffffff >> d1; |
| 287 maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32); | 285 maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32); |
| 288 maskM = maskL & maskR; | 286 maskM = maskL & maskR; |
| 289 lineSrc = m_pData + ys0 * m_nStride + ((xs0 >> 5) << 2); | 287 uint8_t* lineSrc = m_pData + ys0 * m_nStride + ((xs0 >> 5) << 2); |
| 290 lineLeft = m_nStride - ((xs0 >> 5) << 2); | 288 lineLeft = m_nStride - ((xs0 >> 5) << 2); |
| 291 lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2); | 289 uint8_t* lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2); |
| 292 if ((xd0 & ~31) == ((xd1 - 1) & ~31)) { | 290 if ((xd0 & ~31) == ((xd1 - 1) & ~31)) { |
| 293 if ((xs0 & ~31) == ((xs1 - 1) & ~31)) { | 291 if ((xs0 & ~31) == ((xs1 - 1) & ~31)) { |
| 294 if (s1 > d1) { | 292 if (s1 > d1) { |
| 295 shift = s1 - d1; | 293 shift = s1 - d1; |
| 296 for (yy = yd0; yy < yd1; yy++) { | 294 for (yy = yd0; yy < yd1; yy++) { |
| 297 tmp1 = JBIG2_GETDWORD(lineSrc) << shift; | 295 tmp1 = JBIG2_GETDWORD(lineSrc) << shift; |
| 298 tmp2 = JBIG2_GETDWORD(lineDst); | 296 tmp2 = JBIG2_GETDWORD(lineDst); |
| 299 switch (op) { | 297 switch (op) { |
| 300 case JBIG2_COMPOSE_OR: | 298 case JBIG2_COMPOSE_OR: |
| 301 tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM); | 299 tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 374 } |
| 377 lineDst[0] = (uint8_t)(tmp >> 24); | 375 lineDst[0] = (uint8_t)(tmp >> 24); |
| 378 lineDst[1] = (uint8_t)(tmp >> 16); | 376 lineDst[1] = (uint8_t)(tmp >> 16); |
| 379 lineDst[2] = (uint8_t)(tmp >> 8); | 377 lineDst[2] = (uint8_t)(tmp >> 8); |
| 380 lineDst[3] = (uint8_t)tmp; | 378 lineDst[3] = (uint8_t)tmp; |
| 381 lineSrc += m_nStride; | 379 lineSrc += m_nStride; |
| 382 lineDst += pDst->m_nStride; | 380 lineDst += pDst->m_nStride; |
| 383 } | 381 } |
| 384 } | 382 } |
| 385 } else { | 383 } else { |
| 384 uint8_t* sp = nullptr; |
| 385 uint8_t* dp = nullptr; |
| 386 |
| 386 if (s1 > d1) { | 387 if (s1 > d1) { |
| 387 shift1 = s1 - d1; | 388 shift1 = s1 - d1; |
| 388 shift2 = 32 - shift1; | 389 shift2 = 32 - shift1; |
| 389 middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5); | 390 middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5); |
| 390 for (yy = yd0; yy < yd1; yy++) { | 391 for (yy = yd0; yy < yd1; yy++) { |
| 391 sp = lineSrc; | 392 sp = lineSrc; |
| 392 dp = lineDst; | 393 dp = lineDst; |
| 393 if (d1 != 0) { | 394 if (d1 != 0) { |
| 394 tmp1 = (JBIG2_GETDWORD(sp) << shift1) | | 395 tmp1 = (JBIG2_GETDWORD(sp) << shift1) | |
| 395 (JBIG2_GETDWORD(sp + 4) >> shift2); | 396 (JBIG2_GETDWORD(sp + 4) >> shift2); |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1073 dp[2] = (uint8_t)(tmp >> 8); | 1074 dp[2] = (uint8_t)(tmp >> 8); |
| 1074 dp[3] = (uint8_t)tmp; | 1075 dp[3] = (uint8_t)tmp; |
| 1075 } | 1076 } |
| 1076 lineSrc += m_nStride; | 1077 lineSrc += m_nStride; |
| 1077 lineDst += pDst->m_nStride; | 1078 lineDst += pDst->m_nStride; |
| 1078 } | 1079 } |
| 1079 } | 1080 } |
| 1080 } | 1081 } |
| 1081 return 1; | 1082 return 1; |
| 1082 } | 1083 } |
| OLD | NEW |