Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp

Issue 1441973002: Merge to M47: Fix extraction of colour components in CPDF_DIBSource::DownSampleScanline32Bit (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@2526
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <vector> 7 #include <vector>
8 8
9 #include "../../../../third_party/base/nonstd_unique_ptr.h" 9 #include "../../../../third_party/base/nonstd_unique_ptr.h"
10 #include "../../../include/fpdfapi/fpdf_module.h" 10 #include "../../../include/fpdfapi/fpdf_module.h"
11 #include "../../../include/fpdfapi/fpdf_pageobj.h" 11 #include "../../../include/fpdfapi/fpdf_pageobj.h"
12 #include "../../../include/fpdfapi/fpdf_render.h" 12 #include "../../../include/fpdfapi/fpdf_render.h"
13 #include "../../../include/fxcodec/fx_codec.h" 13 #include "../../../include/fxcodec/fx_codec.h"
14 #include "../../../include/fxcrt/fx_safe_types.h" 14 #include "../../../include/fxcrt/fx_safe_types.h"
15 #include "../../../include/fxge/fx_ge.h" 15 #include "../../../include/fxge/fx_ge.h"
16 #include "../fpdf_page/pageint.h" 16 #include "../fpdf_page/pageint.h"
17 #include "render_int.h" 17 #include "render_int.h"
18 18
19 namespace { 19 namespace {
20 20
21 unsigned int _GetBits8(const uint8_t* pData, int bitpos, int nbits) { 21 unsigned int GetBits8(const uint8_t* pData, uint64_t bitpos, size_t nbits) {
22 ASSERT(nbits == 1 || nbits == 2 || nbits == 4 || nbits == 8 || nbits == 16);
23 ASSERT((bitpos & (nbits - 1)) == 0);
22 unsigned int byte = pData[bitpos / 8]; 24 unsigned int byte = pData[bitpos / 8];
23 if (nbits == 8) { 25 if (nbits == 8) {
24 return byte; 26 return byte;
25 } 27 }
26 if (nbits == 4) { 28 if (nbits == 4) {
27 return (bitpos % 8) ? (byte & 0x0f) : (byte >> 4); 29 return (bitpos % 8) ? (byte & 0x0f) : (byte >> 4);
28 } 30 }
29 if (nbits == 2) { 31 if (nbits == 2) {
30 return (byte >> (6 - bitpos % 8)) & 0x03; 32 return (byte >> (6 - bitpos % 8)) & 0x03;
31 } 33 }
(...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 *dest_scan++ = src_pos[2]; 1004 *dest_scan++ = src_pos[2];
1003 *dest_scan++ = src_pos[1]; 1005 *dest_scan++ = src_pos[1];
1004 *dest_scan++ = *src_pos; 1006 *dest_scan++ = *src_pos;
1005 src_pos += 3; 1007 src_pos += 3;
1006 } 1008 }
1007 break; 1009 break;
1008 default: 1010 default:
1009 int src_bit_pos = 0; 1011 int src_bit_pos = 0;
1010 int dest_byte_pos = 0; 1012 int dest_byte_pos = 0;
1011 for (int column = 0; column < m_Width; column++) { 1013 for (int column = 0; column < m_Width; column++) {
1012 int R = _GetBits8(src_scan, src_bit_pos, m_bpc); 1014 int R = GetBits8(src_scan, src_bit_pos, m_bpc);
1013 src_bit_pos += m_bpc; 1015 src_bit_pos += m_bpc;
1014 int G = _GetBits8(src_scan, src_bit_pos, m_bpc); 1016 int G = GetBits8(src_scan, src_bit_pos, m_bpc);
1015 src_bit_pos += m_bpc; 1017 src_bit_pos += m_bpc;
1016 int B = _GetBits8(src_scan, src_bit_pos, m_bpc); 1018 int B = GetBits8(src_scan, src_bit_pos, m_bpc);
1017 src_bit_pos += m_bpc; 1019 src_bit_pos += m_bpc;
1018 R = NORMALCOLOR_MAX(R, max_data); 1020 R = NORMALCOLOR_MAX(R, max_data);
1019 G = NORMALCOLOR_MAX(G, max_data); 1021 G = NORMALCOLOR_MAX(G, max_data);
1020 B = NORMALCOLOR_MAX(B, max_data); 1022 B = NORMALCOLOR_MAX(B, max_data);
1021 dest_scan[dest_byte_pos] = B * 255 / max_data; 1023 dest_scan[dest_byte_pos] = B * 255 / max_data;
1022 dest_scan[dest_byte_pos + 1] = G * 255 / max_data; 1024 dest_scan[dest_byte_pos + 1] = G * 255 / max_data;
1023 dest_scan[dest_byte_pos + 2] = R * 255 / max_data; 1025 dest_scan[dest_byte_pos + 2] = R * 255 / max_data;
1024 dest_byte_pos += 3; 1026 dest_byte_pos += 3;
1025 } 1027 }
1026 break; 1028 break;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1060 dest_scan[dest_byte_pos] = (int32_t)(B * 255); 1062 dest_scan[dest_byte_pos] = (int32_t)(B * 255);
1061 dest_scan[dest_byte_pos + 1] = (int32_t)(G * 255); 1063 dest_scan[dest_byte_pos + 1] = (int32_t)(G * 255);
1062 dest_scan[dest_byte_pos + 2] = (int32_t)(R * 255); 1064 dest_scan[dest_byte_pos + 2] = (int32_t)(R * 255);
1063 dest_byte_pos += 3; 1065 dest_byte_pos += 3;
1064 } 1066 }
1065 } else { 1067 } else {
1066 int src_bit_pos = 0; 1068 int src_bit_pos = 0;
1067 int dest_byte_pos = 0; 1069 int dest_byte_pos = 0;
1068 for (int column = 0; column < m_Width; column++) { 1070 for (int column = 0; column < m_Width; column++) {
1069 for (FX_DWORD color = 0; color < m_nComponents; color++) { 1071 for (FX_DWORD color = 0; color < m_nComponents; color++) {
1070 int data = _GetBits8(src_scan, src_bit_pos, m_bpc); 1072 int data = GetBits8(src_scan, src_bit_pos, m_bpc);
1071 color_values[color] = m_pCompData[color].m_DecodeMin + 1073 color_values[color] = m_pCompData[color].m_DecodeMin +
1072 m_pCompData[color].m_DecodeStep * data; 1074 m_pCompData[color].m_DecodeStep * data;
1073 src_bit_pos += m_bpc; 1075 src_bit_pos += m_bpc;
1074 } 1076 }
1075 if (TransMask()) { 1077 if (TransMask()) {
1076 FX_FLOAT k = 1.0f - color_values[3]; 1078 FX_FLOAT k = 1.0f - color_values[3];
1077 R = (1.0f - color_values[0]) * k; 1079 R = (1.0f - color_values[0]) * k;
1078 G = (1.0f - color_values[1]) * k; 1080 G = (1.0f - color_values[1]) * k;
1079 B = (1.0f - color_values[2]) * k; 1081 B = (1.0f - color_values[2]) * k;
1080 } else { 1082 } else {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 return m_pLineBuf; 1157 return m_pLineBuf;
1156 } 1158 }
1157 if (m_bpc * m_nComponents <= 8) { 1159 if (m_bpc * m_nComponents <= 8) {
1158 if (m_bpc == 8) { 1160 if (m_bpc == 8) {
1159 FXSYS_memcpy(m_pLineBuf, pSrcLine, src_pitch_value); 1161 FXSYS_memcpy(m_pLineBuf, pSrcLine, src_pitch_value);
1160 } else { 1162 } else {
1161 int src_bit_pos = 0; 1163 int src_bit_pos = 0;
1162 for (int col = 0; col < m_Width; col++) { 1164 for (int col = 0; col < m_Width; col++) {
1163 int color_index = 0; 1165 int color_index = 0;
1164 for (FX_DWORD color = 0; color < m_nComponents; color++) { 1166 for (FX_DWORD color = 0; color < m_nComponents; color++) {
1165 int data = _GetBits8(pSrcLine, src_bit_pos, m_bpc); 1167 int data = GetBits8(pSrcLine, src_bit_pos, m_bpc);
1166 color_index |= data << (color * m_bpc); 1168 color_index |= data << (color * m_bpc);
1167 src_bit_pos += m_bpc; 1169 src_bit_pos += m_bpc;
1168 } 1170 }
1169 m_pLineBuf[col] = color_index; 1171 m_pLineBuf[col] = color_index;
1170 } 1172 }
1171 } 1173 }
1172 if (m_bColorKey) { 1174 if (m_bColorKey) {
1173 uint8_t* pDestPixel = m_pMaskedLine; 1175 uint8_t* pDestPixel = m_pMaskedLine;
1174 const uint8_t* pSrcPixel = m_pLineBuf; 1176 const uint8_t* pSrcPixel = m_pLineBuf;
1175 for (int col = 0; col < m_Width; col++) { 1177 for (int col = 0; col < m_Width; col++) {
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1377 uint8_t* dest_scan, 1379 uint8_t* dest_scan,
1378 int dest_width, 1380 int dest_width,
1379 FX_BOOL bFlipX, 1381 FX_BOOL bFlipX,
1380 int clip_left, 1382 int clip_left,
1381 int clip_width) const { 1383 int clip_width) const {
1382 if (m_bpc < 8) { 1384 if (m_bpc < 8) {
1383 int src_bit_pos = 0; 1385 int src_bit_pos = 0;
1384 for (FX_DWORD col = 0; col < src_width; col++) { 1386 for (FX_DWORD col = 0; col < src_width; col++) {
1385 int color_index = 0; 1387 int color_index = 0;
1386 for (FX_DWORD color = 0; color < m_nComponents; color++) { 1388 for (FX_DWORD color = 0; color < m_nComponents; color++) {
1387 int data = _GetBits8(pSrcLine, src_bit_pos, m_bpc); 1389 int data = GetBits8(pSrcLine, src_bit_pos, m_bpc);
1388 color_index |= data << (color * m_bpc); 1390 color_index |= data << (color * m_bpc);
1389 src_bit_pos += m_bpc; 1391 src_bit_pos += m_bpc;
1390 } 1392 }
1391 m_pLineBuf[col] = color_index; 1393 m_pLineBuf[col] = color_index;
1392 } 1394 }
1393 pSrcLine = m_pLineBuf; 1395 pSrcLine = m_pLineBuf;
1394 } 1396 }
1395 if (m_bColorKey) { 1397 if (m_bColorKey) {
1396 for (int i = 0; i < clip_width; i++) { 1398 for (int i = 0; i < clip_width; i++) {
1397 FX_DWORD src_x = (clip_left + i) * src_width / dest_width; 1399 FX_DWORD src_x = (clip_left + i) * src_width / dest_width;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1440 int dest_Bpp, 1442 int dest_Bpp,
1441 FX_DWORD src_width, 1443 FX_DWORD src_width,
1442 const uint8_t* pSrcLine, 1444 const uint8_t* pSrcLine,
1443 uint8_t* dest_scan, 1445 uint8_t* dest_scan,
1444 int dest_width, 1446 int dest_width,
1445 FX_BOOL bFlipX, 1447 FX_BOOL bFlipX,
1446 int clip_left, 1448 int clip_left,
1447 int clip_width) const { 1449 int clip_width) const {
1448 int last_src_x = -1; 1450 int last_src_x = -1;
1449 FX_ARGB last_argb = FXARGB_MAKE(0xFF, 0xFF, 0xFF, 0xFF); 1451 FX_ARGB last_argb = FXARGB_MAKE(0xFF, 0xFF, 0xFF, 0xFF);
1450 FX_FLOAT orig_Not8Bpp = (FX_FLOAT)m_bpc * (FX_FLOAT)m_nComponents / 8.0f;
1451 FX_FLOAT unit_To8Bpc = 255.0f / ((1 << m_bpc) - 1); 1452 FX_FLOAT unit_To8Bpc = 255.0f / ((1 << m_bpc) - 1);
1452 for (int i = 0; i < clip_width; i++) { 1453 for (int i = 0; i < clip_width; i++) {
1453 int dest_x = clip_left + i; 1454 int dest_x = clip_left + i;
1454 FX_DWORD src_x = (bFlipX ? (dest_width - dest_x - 1) : dest_x) * 1455 FX_DWORD src_x = (bFlipX ? (dest_width - dest_x - 1) : dest_x) *
1455 (int64_t)src_width / dest_width; 1456 (int64_t)src_width / dest_width;
1456 src_x %= src_width; 1457 src_x %= src_width;
1458
1459 // No need to check for 32-bit overflow, as |src_x| is bounded by
1460 // |src_width| and DownSampleScanline already checked for overflow with the
1461 // pitch calculation.
1457 const uint8_t* pSrcPixel = nullptr; 1462 const uint8_t* pSrcPixel = nullptr;
1463 size_t bit_offset = 0;
1458 if (m_bpc % 8 == 0) { 1464 if (m_bpc % 8 == 0) {
1459 pSrcPixel = pSrcLine + src_x * orig_Bpp; 1465 pSrcPixel = pSrcLine + src_x * orig_Bpp;
1460 } else { 1466 } else {
1461 pSrcPixel = pSrcLine + (int)(src_x * orig_Not8Bpp); 1467 size_t num_bits = src_x * m_bpc * m_nComponents;
1468 pSrcPixel = pSrcLine + num_bits / 8;
1469 bit_offset = num_bits % 8;
1462 } 1470 }
1471
1463 uint8_t* pDestPixel = dest_scan + i * dest_Bpp; 1472 uint8_t* pDestPixel = dest_scan + i * dest_Bpp;
1464 FX_ARGB argb; 1473 FX_ARGB argb;
1465 if (src_x == last_src_x) { 1474 if (src_x == last_src_x) {
1466 argb = last_argb; 1475 argb = last_argb;
1467 } else { 1476 } else {
1477 CFX_FixedBufGrow<uint8_t, 128> extracted_components(m_nComponents);
1478 if (m_bpc % 8 != 0) {
1479 uint64_t src_bit_pos = bit_offset;
1480 for (FX_DWORD j = 0; j < m_nComponents; ++j) {
Oliver Chang 2015/11/13 17:24:03 ++<<<<<<< HEAD + for (int j = 0; j < m_n
1481 extracted_components[j] = static_cast<uint8_t>(
1482 GetBits8(pSrcPixel, src_bit_pos, m_bpc) * unit_To8Bpc);
1483 src_bit_pos += m_bpc;
1484 }
1485 pSrcPixel = extracted_components;
1486 }
1487
1468 if (m_pColorSpace) { 1488 if (m_pColorSpace) {
1469 CFX_FixedBufGrow<uint8_t, 128> temp(orig_Bpp);
1470 uint8_t color[4]; 1489 uint8_t color[4];
1471 const FX_BOOL bTransMask = TransMask(); 1490 const FX_BOOL bTransMask = TransMask();
1472 if (m_bDefaultDecode) { 1491 if (m_bDefaultDecode) {
1473 if (m_bpc < 8) { 1492 m_pColorSpace->TranslateImageLine(color, pSrcPixel, 1, 0, 0,
1474 int src_bit_pos = 0; 1493 bTransMask);
1475 if (src_x % 2) {
1476 src_bit_pos = 4;
1477 }
1478 for (FX_DWORD j = 0; j < m_nComponents; ++j) {
1479 temp[j] = (uint8_t)(_GetBits8(pSrcPixel, src_bit_pos, m_bpc) *
1480 unit_To8Bpc);
1481 src_bit_pos += m_bpc;
1482 }
1483 m_pColorSpace->TranslateImageLine(color, temp, 1, 0, 0, bTransMask);
1484 } else {
1485 m_pColorSpace->TranslateImageLine(color, pSrcPixel, 1, 0, 0,
1486 bTransMask);
1487 }
1488 } else { 1494 } else {
1489 for (int j = 0; j < m_nComponents; ++j) { 1495 for (FX_DWORD j = 0; j < m_nComponents; ++j) {
1496 FX_FLOAT component_value =
1497 static_cast<FX_FLOAT>(extracted_components[j]);
1490 int color_value = 1498 int color_value =
1491 (int)((m_pCompData[j].m_DecodeMin + 1499 (int)((m_pCompData[j].m_DecodeMin +
1492 m_pCompData[j].m_DecodeStep * (FX_FLOAT)pSrcPixel[j]) * 1500 m_pCompData[j].m_DecodeStep * component_value) *
1493 255.0f + 1501 255.0f +
1494 0.5f); 1502 0.5f);
1495 temp[j] = 1503 extracted_components[j] =
1496 color_value > 255 ? 255 : (color_value < 0 ? 0 : color_value); 1504 color_value > 255 ? 255 : (color_value < 0 ? 0 : color_value);
1497 } 1505 }
1498 m_pColorSpace->TranslateImageLine(color, temp, 1, 0, 0, bTransMask); 1506 m_pColorSpace->TranslateImageLine(color, extracted_components, 1, 0,
1507 0, bTransMask);
1499 } 1508 }
1500 argb = FXARGB_MAKE(0xFF, color[2], color[1], color[0]); 1509 argb = FXARGB_MAKE(0xFF, color[2], color[1], color[0]);
1501 } else { 1510 } else {
1502 argb = FXARGB_MAKE(0xFf, pSrcPixel[2], pSrcPixel[1], pSrcPixel[0]); 1511 argb = FXARGB_MAKE(0xFF, pSrcPixel[2], pSrcPixel[1], pSrcPixel[0]);
1503 } 1512 }
1504 if (m_bColorKey) { 1513 if (m_bColorKey) {
1505 int alpha = 0xFF; 1514 int alpha = 0xFF;
1506 if (m_nComponents == 3 && m_bpc == 8) { 1515 if (m_nComponents == 3 && m_bpc == 8) {
1507 alpha = (pSrcPixel[0] < m_pCompData[0].m_ColorKeyMin || 1516 alpha = (pSrcPixel[0] < m_pCompData[0].m_ColorKeyMin ||
1508 pSrcPixel[0] > m_pCompData[0].m_ColorKeyMax || 1517 pSrcPixel[0] > m_pCompData[0].m_ColorKeyMax ||
1509 pSrcPixel[1] < m_pCompData[1].m_ColorKeyMin || 1518 pSrcPixel[1] < m_pCompData[1].m_ColorKeyMin ||
1510 pSrcPixel[1] > m_pCompData[1].m_ColorKeyMax || 1519 pSrcPixel[1] > m_pCompData[1].m_ColorKeyMax ||
1511 pSrcPixel[2] < m_pCompData[2].m_ColorKeyMin || 1520 pSrcPixel[2] < m_pCompData[2].m_ColorKeyMin ||
1512 pSrcPixel[2] > m_pCompData[2].m_ColorKeyMax) 1521 pSrcPixel[2] > m_pCompData[2].m_ColorKeyMax)
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1657 } 1666 }
1658 FX_BOOL CPDF_ImageLoader::Continue(void* LoadHandle, IFX_Pause* pPause) { 1667 FX_BOOL CPDF_ImageLoader::Continue(void* LoadHandle, IFX_Pause* pPause) {
1659 return ((CPDF_ProgressiveImageLoaderHandle*)LoadHandle)->Continue(pPause); 1668 return ((CPDF_ProgressiveImageLoaderHandle*)LoadHandle)->Continue(pPause);
1660 } 1669 }
1661 CPDF_ImageLoader::~CPDF_ImageLoader() { 1670 CPDF_ImageLoader::~CPDF_ImageLoader() {
1662 if (!m_bCached) { 1671 if (!m_bCached) {
1663 delete m_pBitmap; 1672 delete m_pBitmap;
1664 delete m_pMask; 1673 delete m_pMask;
1665 } 1674 }
1666 } 1675 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698