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 "core/fpdfapi/fpdf_render/render_int.h" | 7 #include "core/fpdfapi/fpdf_render/render_int.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 20 matching lines...) Expand all Loading... |
31 unsigned int byte = pData[bitpos / 8]; | 31 unsigned int byte = pData[bitpos / 8]; |
32 if (nbits == 8) { | 32 if (nbits == 8) { |
33 return byte; | 33 return byte; |
34 } else if (nbits == 16) { | 34 } else if (nbits == 16) { |
35 return byte * 256 + pData[bitpos / 8 + 1]; | 35 return byte * 256 + pData[bitpos / 8 + 1]; |
36 } | 36 } |
37 | 37 |
38 return (byte >> (8 - nbits - (bitpos % 8))) & ((1 << nbits) - 1); | 38 return (byte >> (8 - nbits - (bitpos % 8))) & ((1 << nbits) - 1); |
39 } | 39 } |
40 | 40 |
41 FX_SAFE_DWORD CalculatePitch8(uint32_t bpc, uint32_t components, int width) { | 41 FX_SAFE_UINT32 CalculatePitch8(uint32_t bpc, uint32_t components, int width) { |
42 FX_SAFE_DWORD pitch = bpc; | 42 FX_SAFE_UINT32 pitch = bpc; |
43 pitch *= components; | 43 pitch *= components; |
44 pitch *= width; | 44 pitch *= width; |
45 pitch += 7; | 45 pitch += 7; |
46 pitch /= 8; | 46 pitch /= 8; |
47 return pitch; | 47 return pitch; |
48 } | 48 } |
49 | 49 |
50 FX_SAFE_DWORD CalculatePitch32(int bpp, int width) { | 50 FX_SAFE_UINT32 CalculatePitch32(int bpp, int width) { |
51 FX_SAFE_DWORD pitch = bpp; | 51 FX_SAFE_UINT32 pitch = bpp; |
52 pitch *= width; | 52 pitch *= width; |
53 pitch += 31; | 53 pitch += 31; |
54 pitch /= 32; // quantized to number of 32-bit words. | 54 pitch /= 32; // quantized to number of 32-bit words. |
55 pitch *= 4; // and then back to bytes, (not just /8 in one step). | 55 pitch *= 4; // and then back to bytes, (not just /8 in one step). |
56 return pitch; | 56 return pitch; |
57 } | 57 } |
58 | 58 |
59 bool IsAllowedBPCValue(int bpc) { | 59 bool IsAllowedBPCValue(int bpc) { |
60 return bpc == 1 || bpc == 2 || bpc == 4 || bpc == 8 || bpc == 16; | 60 return bpc == 1 || bpc == 2 || bpc == 4 || bpc == 8 || bpc == 16; |
61 } | 61 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 170 } |
171 m_GroupFamily = GroupFamily; | 171 m_GroupFamily = GroupFamily; |
172 m_bLoadMask = bLoadMask; | 172 m_bLoadMask = bLoadMask; |
173 if (!LoadColorInfo(m_pStream->GetObjNum() != 0 ? nullptr : pFormResources, | 173 if (!LoadColorInfo(m_pStream->GetObjNum() != 0 ? nullptr : pFormResources, |
174 pPageResources)) { | 174 pPageResources)) { |
175 return FALSE; | 175 return FALSE; |
176 } | 176 } |
177 if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) { | 177 if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) { |
178 return FALSE; | 178 return FALSE; |
179 } | 179 } |
180 FX_SAFE_DWORD src_size = | 180 FX_SAFE_UINT32 src_size = |
181 CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; | 181 CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; |
182 if (!src_size.IsValid()) { | 182 if (!src_size.IsValid()) { |
183 return FALSE; | 183 return FALSE; |
184 } | 184 } |
185 m_pStreamAcc.reset(new CPDF_StreamAcc); | 185 m_pStreamAcc.reset(new CPDF_StreamAcc); |
186 m_pStreamAcc->LoadAllData(pStream, FALSE, src_size.ValueOrDie(), TRUE); | 186 m_pStreamAcc->LoadAllData(pStream, FALSE, src_size.ValueOrDie(), TRUE); |
187 if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) { | 187 if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) { |
188 return FALSE; | 188 return FALSE; |
189 } | 189 } |
190 if (!CreateDecoder()) { | 190 if (!CreateDecoder()) { |
191 return FALSE; | 191 return FALSE; |
192 } | 192 } |
193 if (m_bImageMask) { | 193 if (m_bImageMask) { |
194 m_bpp = 1; | 194 m_bpp = 1; |
195 m_bpc = 1; | 195 m_bpc = 1; |
196 m_nComponents = 1; | 196 m_nComponents = 1; |
197 m_AlphaFlag = 1; | 197 m_AlphaFlag = 1; |
198 } else if (m_bpc * m_nComponents == 1) { | 198 } else if (m_bpc * m_nComponents == 1) { |
199 m_bpp = 1; | 199 m_bpp = 1; |
200 } else if (m_bpc * m_nComponents <= 8) { | 200 } else if (m_bpc * m_nComponents <= 8) { |
201 m_bpp = 8; | 201 m_bpp = 8; |
202 } else { | 202 } else { |
203 m_bpp = 24; | 203 m_bpp = 24; |
204 } | 204 } |
205 FX_SAFE_DWORD pitch = CalculatePitch32(m_bpp, m_Width); | 205 FX_SAFE_UINT32 pitch = CalculatePitch32(m_bpp, m_Width); |
206 if (!pitch.IsValid()) { | 206 if (!pitch.IsValid()) { |
207 return FALSE; | 207 return FALSE; |
208 } | 208 } |
209 m_pLineBuf = FX_Alloc(uint8_t, pitch.ValueOrDie()); | 209 m_pLineBuf = FX_Alloc(uint8_t, pitch.ValueOrDie()); |
210 if (m_pColorSpace && bStdCS) { | 210 if (m_pColorSpace && bStdCS) { |
211 m_pColorSpace->EnableStdConversion(TRUE); | 211 m_pColorSpace->EnableStdConversion(TRUE); |
212 } | 212 } |
213 LoadPalette(); | 213 LoadPalette(); |
214 if (m_bColorKey) { | 214 if (m_bColorKey) { |
215 m_bpp = 32; | 215 m_bpp = 32; |
(...skipping 23 matching lines...) Expand all Loading... |
239 } else if (m_bpc * m_nComponents == 1) { | 239 } else if (m_bpc * m_nComponents == 1) { |
240 m_bpp = 1; | 240 m_bpp = 1; |
241 } else if (m_bpc * m_nComponents <= 8) { | 241 } else if (m_bpc * m_nComponents <= 8) { |
242 m_bpp = 8; | 242 m_bpp = 8; |
243 } else { | 243 } else { |
244 m_bpp = 24; | 244 m_bpp = 24; |
245 } | 245 } |
246 if (!m_bpc || !m_nComponents) { | 246 if (!m_bpc || !m_nComponents) { |
247 return 0; | 247 return 0; |
248 } | 248 } |
249 FX_SAFE_DWORD pitch = CalculatePitch32(m_bpp, m_Width); | 249 FX_SAFE_UINT32 pitch = CalculatePitch32(m_bpp, m_Width); |
250 if (!pitch.IsValid()) { | 250 if (!pitch.IsValid()) { |
251 return 0; | 251 return 0; |
252 } | 252 } |
253 m_pLineBuf = FX_Alloc(uint8_t, pitch.ValueOrDie()); | 253 m_pLineBuf = FX_Alloc(uint8_t, pitch.ValueOrDie()); |
254 if (m_pColorSpace && m_bStdCS) { | 254 if (m_pColorSpace && m_bStdCS) { |
255 m_pColorSpace->EnableStdConversion(TRUE); | 255 m_pColorSpace->EnableStdConversion(TRUE); |
256 } | 256 } |
257 LoadPalette(); | 257 LoadPalette(); |
258 if (m_bColorKey) { | 258 if (m_bColorKey) { |
259 m_bpp = 32; | 259 m_bpp = 32; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 } | 292 } |
293 m_GroupFamily = GroupFamily; | 293 m_GroupFamily = GroupFamily; |
294 m_bLoadMask = bLoadMask; | 294 m_bLoadMask = bLoadMask; |
295 if (!LoadColorInfo(m_pStream->GetObjNum() != 0 ? nullptr : pFormResources, | 295 if (!LoadColorInfo(m_pStream->GetObjNum() != 0 ? nullptr : pFormResources, |
296 pPageResources)) { | 296 pPageResources)) { |
297 return 0; | 297 return 0; |
298 } | 298 } |
299 if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) { | 299 if (m_bDoBpcCheck && (m_bpc == 0 || m_nComponents == 0)) { |
300 return 0; | 300 return 0; |
301 } | 301 } |
302 FX_SAFE_DWORD src_size = | 302 FX_SAFE_UINT32 src_size = |
303 CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; | 303 CalculatePitch8(m_bpc, m_nComponents, m_Width) * m_Height; |
304 if (!src_size.IsValid()) { | 304 if (!src_size.IsValid()) { |
305 return 0; | 305 return 0; |
306 } | 306 } |
307 m_pStreamAcc.reset(new CPDF_StreamAcc); | 307 m_pStreamAcc.reset(new CPDF_StreamAcc); |
308 m_pStreamAcc->LoadAllData(pStream, FALSE, src_size.ValueOrDie(), TRUE); | 308 m_pStreamAcc->LoadAllData(pStream, FALSE, src_size.ValueOrDie(), TRUE); |
309 if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) { | 309 if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) { |
310 return 0; | 310 return 0; |
311 } | 311 } |
312 int ret = CreateDecoder(); | 312 int ret = CreateDecoder(); |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 m_pDecoder.reset(CPDF_ModuleMgr::Get() | 604 m_pDecoder.reset(CPDF_ModuleMgr::Get() |
605 ->GetCodecModule() | 605 ->GetCodecModule() |
606 ->GetBasicModule() | 606 ->GetBasicModule() |
607 ->CreateRunLengthDecoder(src_data, src_size, m_Width, | 607 ->CreateRunLengthDecoder(src_data, src_size, m_Width, |
608 m_Height, m_nComponents, | 608 m_Height, m_nComponents, |
609 m_bpc)); | 609 m_bpc)); |
610 } | 610 } |
611 if (!m_pDecoder) | 611 if (!m_pDecoder) |
612 return 0; | 612 return 0; |
613 | 613 |
614 FX_SAFE_DWORD requested_pitch = | 614 FX_SAFE_UINT32 requested_pitch = |
615 CalculatePitch8(m_bpc, m_nComponents, m_Width); | 615 CalculatePitch8(m_bpc, m_nComponents, m_Width); |
616 if (!requested_pitch.IsValid()) { | 616 if (!requested_pitch.IsValid()) { |
617 return 0; | 617 return 0; |
618 } | 618 } |
619 FX_SAFE_DWORD provided_pitch = CalculatePitch8( | 619 FX_SAFE_UINT32 provided_pitch = CalculatePitch8( |
620 m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth()); | 620 m_pDecoder->GetBPC(), m_pDecoder->CountComps(), m_pDecoder->GetWidth()); |
621 if (!provided_pitch.IsValid()) { | 621 if (!provided_pitch.IsValid()) { |
622 return 0; | 622 return 0; |
623 } | 623 } |
624 if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) { | 624 if (provided_pitch.ValueOrDie() < requested_pitch.ValueOrDie()) { |
625 return 0; | 625 return 0; |
626 } | 626 } |
627 return 1; | 627 return 1; |
628 } | 628 } |
629 | 629 |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 } | 1023 } |
1024 | 1024 |
1025 uint8_t* CPDF_DIBSource::GetBuffer() const { | 1025 uint8_t* CPDF_DIBSource::GetBuffer() const { |
1026 return m_pCachedBitmap ? m_pCachedBitmap->GetBuffer() : nullptr; | 1026 return m_pCachedBitmap ? m_pCachedBitmap->GetBuffer() : nullptr; |
1027 } | 1027 } |
1028 | 1028 |
1029 const uint8_t* CPDF_DIBSource::GetScanline(int line) const { | 1029 const uint8_t* CPDF_DIBSource::GetScanline(int line) const { |
1030 if (m_bpc == 0) { | 1030 if (m_bpc == 0) { |
1031 return nullptr; | 1031 return nullptr; |
1032 } | 1032 } |
1033 FX_SAFE_DWORD src_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); | 1033 FX_SAFE_UINT32 src_pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); |
1034 if (!src_pitch.IsValid()) | 1034 if (!src_pitch.IsValid()) |
1035 return nullptr; | 1035 return nullptr; |
1036 uint32_t src_pitch_value = src_pitch.ValueOrDie(); | 1036 uint32_t src_pitch_value = src_pitch.ValueOrDie(); |
1037 const uint8_t* pSrcLine = nullptr; | 1037 const uint8_t* pSrcLine = nullptr; |
1038 if (m_pCachedBitmap && src_pitch_value <= m_pCachedBitmap->GetPitch()) { | 1038 if (m_pCachedBitmap && src_pitch_value <= m_pCachedBitmap->GetPitch()) { |
1039 if (line >= m_pCachedBitmap->GetHeight()) { | 1039 if (line >= m_pCachedBitmap->GetHeight()) { |
1040 line = m_pCachedBitmap->GetHeight() - 1; | 1040 line = m_pCachedBitmap->GetHeight() - 1; |
1041 } | 1041 } |
1042 pSrcLine = m_pCachedBitmap->GetScanline(line); | 1042 pSrcLine = m_pCachedBitmap->GetScanline(line); |
1043 } else if (m_pDecoder) { | 1043 } else if (m_pDecoder) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 int dest_width, | 1169 int dest_width, |
1170 FX_BOOL bFlipX, | 1170 FX_BOOL bFlipX, |
1171 int clip_left, | 1171 int clip_left, |
1172 int clip_width) const { | 1172 int clip_width) const { |
1173 if (line < 0 || !dest_scan || dest_bpp <= 0 || dest_width <= 0 || | 1173 if (line < 0 || !dest_scan || dest_bpp <= 0 || dest_width <= 0 || |
1174 clip_left < 0 || clip_width <= 0) { | 1174 clip_left < 0 || clip_width <= 0) { |
1175 return; | 1175 return; |
1176 } | 1176 } |
1177 | 1177 |
1178 uint32_t src_width = m_Width; | 1178 uint32_t src_width = m_Width; |
1179 FX_SAFE_DWORD pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); | 1179 FX_SAFE_UINT32 pitch = CalculatePitch8(m_bpc, m_nComponents, m_Width); |
1180 if (!pitch.IsValid()) | 1180 if (!pitch.IsValid()) |
1181 return; | 1181 return; |
1182 | 1182 |
1183 const uint8_t* pSrcLine = nullptr; | 1183 const uint8_t* pSrcLine = nullptr; |
1184 if (m_pCachedBitmap) { | 1184 if (m_pCachedBitmap) { |
1185 pSrcLine = m_pCachedBitmap->GetScanline(line); | 1185 pSrcLine = m_pCachedBitmap->GetScanline(line); |
1186 } else if (m_pDecoder) { | 1186 } else if (m_pDecoder) { |
1187 pSrcLine = m_pDecoder->GetScanline(line); | 1187 pSrcLine = m_pDecoder->GetScanline(line); |
1188 } else { | 1188 } else { |
1189 uint32_t src_pitch = pitch.ValueOrDie(); | 1189 uint32_t src_pitch = pitch.ValueOrDie(); |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1568 IFX_Pause* pPause) { | 1568 IFX_Pause* pPause) { |
1569 return LoadHandle->Continue(pPause); | 1569 return LoadHandle->Continue(pPause); |
1570 } | 1570 } |
1571 | 1571 |
1572 CPDF_ImageLoader::~CPDF_ImageLoader() { | 1572 CPDF_ImageLoader::~CPDF_ImageLoader() { |
1573 if (!m_bCached) { | 1573 if (!m_bCached) { |
1574 delete m_pBitmap; | 1574 delete m_pBitmap; |
1575 delete m_pMask; | 1575 delete m_pMask; |
1576 } | 1576 } |
1577 } | 1577 } |
OLD | NEW |