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

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

Issue 1800523005: Move core/src/ up to core/. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 9 months 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
(Empty)
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/src/fpdfapi/fpdf_render/render_int.h"
8
9 #include <utility>
10 #include <vector>
11
12 #include "core/include/fpdfapi/cpdf_array.h"
13 #include "core/include/fpdfapi/cpdf_dictionary.h"
14 #include "core/include/fpdfapi/cpdf_document.h"
15 #include "core/include/fpdfapi/fpdf_module.h"
16 #include "core/include/fpdfapi/fpdf_pageobj.h"
17 #include "core/include/fpdfapi/fpdf_render.h"
18 #include "core/include/fxcodec/fx_codec.h"
19 #include "core/include/fxcrt/fx_safe_types.h"
20 #include "core/include/fxge/fx_ge.h"
21 #include "core/src/fpdfapi/fpdf_page/pageint.h"
22
23 FX_BOOL CPDF_RenderStatus::ProcessImage(const CPDF_ImageObject* pImageObj,
24 const CFX_Matrix* pObj2Device) {
25 CPDF_ImageRenderer render;
26 if (render.Start(this, pImageObj, pObj2Device, m_bStdCS, m_curBlend)) {
27 render.Continue(NULL);
28 }
29 return render.m_Result;
30 }
31 void CPDF_RenderStatus::CompositeDIBitmap(CFX_DIBitmap* pDIBitmap,
32 int left,
33 int top,
34 FX_ARGB mask_argb,
35 int bitmap_alpha,
36 int blend_mode,
37 int Transparency) {
38 if (!pDIBitmap) {
39 return;
40 }
41 FX_BOOL bIsolated = Transparency & PDFTRANS_ISOLATED;
42 FX_BOOL bGroup = Transparency & PDFTRANS_GROUP;
43 if (blend_mode == FXDIB_BLEND_NORMAL) {
44 if (!pDIBitmap->IsAlphaMask()) {
45 if (bitmap_alpha < 255) {
46 pDIBitmap->MultiplyAlpha(bitmap_alpha);
47 }
48 if (m_pDevice->SetDIBits(pDIBitmap, left, top)) {
49 return;
50 }
51 } else {
52 FX_DWORD fill_argb = m_Options.TranslateColor(mask_argb);
53 if (bitmap_alpha < 255) {
54 ((uint8_t*)&fill_argb)[3] =
55 ((uint8_t*)&fill_argb)[3] * bitmap_alpha / 255;
56 }
57 if (m_pDevice->SetBitMask(pDIBitmap, left, top, fill_argb)) {
58 return;
59 }
60 }
61 }
62 FX_BOOL bBackAlphaRequired = blend_mode && bIsolated && !m_bDropObjects;
63 FX_BOOL bGetBackGround =
64 ((m_pDevice->GetRenderCaps() & FXRC_ALPHA_OUTPUT)) ||
65 (!(m_pDevice->GetRenderCaps() & FXRC_ALPHA_OUTPUT) &&
66 (m_pDevice->GetRenderCaps() & FXRC_GET_BITS) && !bBackAlphaRequired);
67 if (bGetBackGround) {
68 if (bIsolated || !bGroup) {
69 if (pDIBitmap->IsAlphaMask()) {
70 return;
71 }
72 m_pDevice->SetDIBits(pDIBitmap, left, top, blend_mode);
73 } else {
74 FX_RECT rect(left, top, left + pDIBitmap->GetWidth(),
75 top + pDIBitmap->GetHeight());
76 rect.Intersect(m_pDevice->GetClipBox());
77 CFX_DIBitmap* pClone = NULL;
78 FX_BOOL bClone = FALSE;
79 if (m_pDevice->GetBackDrop() && m_pDevice->GetBitmap()) {
80 bClone = TRUE;
81 pClone = m_pDevice->GetBackDrop()->Clone(&rect);
82 CFX_DIBitmap* pForeBitmap = m_pDevice->GetBitmap();
83 pClone->CompositeBitmap(0, 0, pClone->GetWidth(), pClone->GetHeight(),
84 pForeBitmap, rect.left, rect.top);
85 left = left >= 0 ? 0 : left;
86 top = top >= 0 ? 0 : top;
87 if (!pDIBitmap->IsAlphaMask())
88 pClone->CompositeBitmap(0, 0, pClone->GetWidth(), pClone->GetHeight(),
89 pDIBitmap, left, top, blend_mode);
90 else
91 pClone->CompositeMask(0, 0, pClone->GetWidth(), pClone->GetHeight(),
92 pDIBitmap, mask_argb, left, top, blend_mode);
93 } else {
94 pClone = pDIBitmap;
95 }
96 if (m_pDevice->GetBackDrop()) {
97 m_pDevice->SetDIBits(pClone, rect.left, rect.top);
98 } else {
99 if (pDIBitmap->IsAlphaMask()) {
100 return;
101 }
102 m_pDevice->SetDIBits(pDIBitmap, rect.left, rect.top, blend_mode);
103 }
104 if (bClone) {
105 delete pClone;
106 }
107 }
108 return;
109 }
110 int back_left, back_top;
111 FX_RECT rect(left, top, left + pDIBitmap->GetWidth(),
112 top + pDIBitmap->GetHeight());
113 std::unique_ptr<CFX_DIBitmap> pBackdrop(
114 GetBackdrop(m_pCurObj, rect, back_left, back_top,
115 blend_mode > FXDIB_BLEND_NORMAL && bIsolated));
116 if (!pBackdrop)
117 return;
118
119 if (!pDIBitmap->IsAlphaMask()) {
120 pBackdrop->CompositeBitmap(left - back_left, top - back_top,
121 pDIBitmap->GetWidth(), pDIBitmap->GetHeight(),
122 pDIBitmap, 0, 0, blend_mode);
123 } else {
124 pBackdrop->CompositeMask(left - back_left, top - back_top,
125 pDIBitmap->GetWidth(), pDIBitmap->GetHeight(),
126 pDIBitmap, mask_argb, 0, 0, blend_mode);
127 }
128
129 std::unique_ptr<CFX_DIBitmap> pBackdrop1(new CFX_DIBitmap);
130 pBackdrop1->Create(pBackdrop->GetWidth(), pBackdrop->GetHeight(),
131 FXDIB_Rgb32);
132 pBackdrop1->Clear((FX_DWORD)-1);
133 pBackdrop1->CompositeBitmap(0, 0, pBackdrop->GetWidth(),
134 pBackdrop->GetHeight(), pBackdrop.get(), 0, 0);
135 pBackdrop = std::move(pBackdrop1);
136 m_pDevice->SetDIBits(pBackdrop.get(), back_left, back_top);
137 }
138
139 CPDF_TransferFunc::CPDF_TransferFunc(CPDF_Document* pDoc) : m_pPDFDoc(pDoc) {}
140
141 FX_COLORREF CPDF_TransferFunc::TranslateColor(FX_COLORREF rgb) const {
142 return FXSYS_RGB(m_Samples[FXSYS_GetRValue(rgb)],
143 m_Samples[256 + FXSYS_GetGValue(rgb)],
144 m_Samples[512 + FXSYS_GetBValue(rgb)]);
145 }
146
147 CFX_DIBSource* CPDF_TransferFunc::TranslateImage(const CFX_DIBSource* pSrc,
148 FX_BOOL bAutoDropSrc) {
149 CPDF_DIBTransferFunc* pDest = new CPDF_DIBTransferFunc(this);
150 pDest->LoadSrc(pSrc, bAutoDropSrc);
151 return pDest;
152 }
153
154 CPDF_DIBTransferFunc::~CPDF_DIBTransferFunc() {
155 }
156
157 FXDIB_Format CPDF_DIBTransferFunc::GetDestFormat() {
158 if (m_pSrc->IsAlphaMask()) {
159 return FXDIB_8bppMask;
160 }
161 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
162 return (m_pSrc->HasAlpha()) ? FXDIB_Argb : FXDIB_Rgb32;
163 #else
164 return (m_pSrc->HasAlpha()) ? FXDIB_Argb : FXDIB_Rgb;
165 #endif
166 }
167 CPDF_DIBTransferFunc::CPDF_DIBTransferFunc(
168 const CPDF_TransferFunc* pTransferFunc) {
169 m_RampR = pTransferFunc->m_Samples;
170 m_RampG = &pTransferFunc->m_Samples[256];
171 m_RampB = &pTransferFunc->m_Samples[512];
172 }
173 void CPDF_DIBTransferFunc::TranslateScanline(uint8_t* dest_buf,
174 const uint8_t* src_buf) const {
175 int i;
176 FX_BOOL bSkip = FALSE;
177 switch (m_pSrc->GetFormat()) {
178 case FXDIB_1bppRgb: {
179 int r0 = m_RampR[0], g0 = m_RampG[0], b0 = m_RampB[0];
180 int r1 = m_RampR[255], g1 = m_RampG[255], b1 = m_RampB[255];
181 for (i = 0; i < m_Width; i++) {
182 if (src_buf[i / 8] & (1 << (7 - i % 8))) {
183 *dest_buf++ = b1;
184 *dest_buf++ = g1;
185 *dest_buf++ = r1;
186 } else {
187 *dest_buf++ = b0;
188 *dest_buf++ = g0;
189 *dest_buf++ = r0;
190 }
191 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
192 dest_buf++;
193 #endif
194 }
195 break;
196 }
197 case FXDIB_1bppMask: {
198 int m0 = m_RampR[0], m1 = m_RampR[255];
199 for (i = 0; i < m_Width; i++) {
200 if (src_buf[i / 8] & (1 << (7 - i % 8))) {
201 *dest_buf++ = m1;
202 } else {
203 *dest_buf++ = m0;
204 }
205 }
206 break;
207 }
208 case FXDIB_8bppRgb: {
209 FX_ARGB* pPal = m_pSrc->GetPalette();
210 for (i = 0; i < m_Width; i++) {
211 if (pPal) {
212 FX_ARGB src_argb = pPal[*src_buf];
213 *dest_buf++ = m_RampB[FXARGB_R(src_argb)];
214 *dest_buf++ = m_RampG[FXARGB_G(src_argb)];
215 *dest_buf++ = m_RampR[FXARGB_B(src_argb)];
216 } else {
217 FX_DWORD src_byte = *src_buf;
218 *dest_buf++ = m_RampB[src_byte];
219 *dest_buf++ = m_RampG[src_byte];
220 *dest_buf++ = m_RampR[src_byte];
221 }
222 src_buf++;
223 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
224 dest_buf++;
225 #endif
226 }
227 break;
228 }
229 case FXDIB_8bppMask:
230 for (i = 0; i < m_Width; i++) {
231 *dest_buf++ = m_RampR[*(src_buf++)];
232 }
233 break;
234 case FXDIB_Rgb:
235 for (i = 0; i < m_Width; i++) {
236 *dest_buf++ = m_RampB[*(src_buf++)];
237 *dest_buf++ = m_RampG[*(src_buf++)];
238 *dest_buf++ = m_RampR[*(src_buf++)];
239 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
240 dest_buf++;
241 #endif
242 }
243 break;
244 case FXDIB_Rgb32:
245 bSkip = TRUE;
246 case FXDIB_Argb:
247 for (i = 0; i < m_Width; i++) {
248 *dest_buf++ = m_RampB[*(src_buf++)];
249 *dest_buf++ = m_RampG[*(src_buf++)];
250 *dest_buf++ = m_RampR[*(src_buf++)];
251 if (!bSkip) {
252 *dest_buf++ = *src_buf;
253 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
254 } else {
255 dest_buf++;
256 #endif
257 }
258 src_buf++;
259 }
260 break;
261 default:
262 break;
263 }
264 }
265 void CPDF_DIBTransferFunc::TranslateDownSamples(uint8_t* dest_buf,
266 const uint8_t* src_buf,
267 int pixels,
268 int Bpp) const {
269 if (Bpp == 8) {
270 for (int i = 0; i < pixels; i++) {
271 *dest_buf++ = m_RampR[*(src_buf++)];
272 }
273 } else if (Bpp == 24) {
274 for (int i = 0; i < pixels; i++) {
275 *dest_buf++ = m_RampB[*(src_buf++)];
276 *dest_buf++ = m_RampG[*(src_buf++)];
277 *dest_buf++ = m_RampR[*(src_buf++)];
278 }
279 } else {
280 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
281 if (!m_pSrc->HasAlpha()) {
282 for (int i = 0; i < pixels; i++) {
283 *dest_buf++ = m_RampB[*(src_buf++)];
284 *dest_buf++ = m_RampG[*(src_buf++)];
285 *dest_buf++ = m_RampR[*(src_buf++)];
286 dest_buf++;
287 src_buf++;
288 }
289 } else {
290 #endif
291 for (int i = 0; i < pixels; i++) {
292 *dest_buf++ = m_RampB[*(src_buf++)];
293 *dest_buf++ = m_RampG[*(src_buf++)];
294 *dest_buf++ = m_RampR[*(src_buf++)];
295 *dest_buf++ = *(src_buf++);
296 }
297 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
298 }
299 #endif
300 }
301 }
302 CPDF_ImageRenderer::CPDF_ImageRenderer() {
303 m_pRenderStatus = NULL;
304 m_pImageObject = NULL;
305 m_Result = TRUE;
306 m_Status = 0;
307 m_pTransformer = NULL;
308 m_DeviceHandle = NULL;
309 m_LoadHandle = NULL;
310 m_pClone = NULL;
311 m_bStdCS = FALSE;
312 m_bPatternColor = FALSE;
313 m_BlendType = FXDIB_BLEND_NORMAL;
314 m_pPattern = NULL;
315 m_pObj2Device = NULL;
316 }
317 CPDF_ImageRenderer::~CPDF_ImageRenderer() {
318 delete m_pTransformer;
319 if (m_DeviceHandle) {
320 m_pRenderStatus->m_pDevice->CancelDIBits(m_DeviceHandle);
321 }
322 delete m_LoadHandle;
323 delete m_pClone;
324 }
325 FX_BOOL CPDF_ImageRenderer::StartLoadDIBSource() {
326 CFX_FloatRect image_rect_f = m_ImageMatrix.GetUnitRect();
327 FX_RECT image_rect = image_rect_f.GetOutterRect();
328 int dest_width = image_rect.Width();
329 int dest_height = image_rect.Height();
330 if (m_ImageMatrix.a < 0) {
331 dest_width = -dest_width;
332 }
333 if (m_ImageMatrix.d > 0) {
334 dest_height = -dest_height;
335 }
336 if (m_Loader.Start(m_pImageObject,
337 m_pRenderStatus->m_pContext->GetPageCache(), m_LoadHandle,
338 m_bStdCS, m_pRenderStatus->m_GroupFamily,
339 m_pRenderStatus->m_bLoadMask, m_pRenderStatus, dest_width,
340 dest_height)) {
341 if (m_LoadHandle) {
342 m_Status = 4;
343 return TRUE;
344 }
345 }
346 return FALSE;
347 }
348 FX_BOOL CPDF_ImageRenderer::StartRenderDIBSource() {
349 if (!m_Loader.m_pBitmap) {
350 return FALSE;
351 }
352 m_BitmapAlpha = 255;
353 const CPDF_GeneralStateData* pGeneralState = m_pImageObject->m_GeneralState;
354 if (pGeneralState) {
355 m_BitmapAlpha = FXSYS_round(pGeneralState->m_FillAlpha * 255);
356 }
357 m_pDIBSource = m_Loader.m_pBitmap;
358 if (m_pRenderStatus->m_Options.m_ColorMode == RENDER_COLOR_ALPHA &&
359 !m_Loader.m_pMask) {
360 return StartBitmapAlpha();
361 }
362 if (pGeneralState && pGeneralState->m_pTR) {
363 if (!pGeneralState->m_pTransferFunc) {
364 ((CPDF_GeneralStateData*)pGeneralState)->m_pTransferFunc =
365 m_pRenderStatus->GetTransferFunc(pGeneralState->m_pTR);
366 }
367 if (pGeneralState->m_pTransferFunc &&
368 !pGeneralState->m_pTransferFunc->m_bIdentity) {
369 m_pDIBSource = m_Loader.m_pBitmap =
370 pGeneralState->m_pTransferFunc->TranslateImage(m_Loader.m_pBitmap,
371 !m_Loader.m_bCached);
372 if (m_Loader.m_bCached && m_Loader.m_pMask) {
373 m_Loader.m_pMask = m_Loader.m_pMask->Clone();
374 }
375 m_Loader.m_bCached = FALSE;
376 }
377 }
378 m_FillArgb = 0;
379 m_bPatternColor = FALSE;
380 m_pPattern = NULL;
381 if (m_pDIBSource->IsAlphaMask()) {
382 CPDF_Color* pColor = m_pImageObject->m_ColorState.GetFillColor();
383 if (pColor && pColor->IsPattern()) {
384 m_pPattern = pColor->GetPattern();
385 if (m_pPattern) {
386 m_bPatternColor = TRUE;
387 }
388 }
389 m_FillArgb = m_pRenderStatus->GetFillArgb(m_pImageObject);
390 } else if (m_pRenderStatus->m_Options.m_ColorMode == RENDER_COLOR_GRAY) {
391 m_pClone = m_pDIBSource->Clone();
392 m_pClone->ConvertColorScale(m_pRenderStatus->m_Options.m_BackColor,
393 m_pRenderStatus->m_Options.m_ForeColor);
394 m_pDIBSource = m_pClone;
395 }
396 m_Flags = 0;
397 if (m_pRenderStatus->m_Options.m_Flags & RENDER_FORCE_DOWNSAMPLE) {
398 m_Flags |= RENDER_FORCE_DOWNSAMPLE;
399 } else if (m_pRenderStatus->m_Options.m_Flags & RENDER_FORCE_HALFTONE) {
400 m_Flags |= RENDER_FORCE_HALFTONE;
401 }
402 if (m_pRenderStatus->m_pDevice->GetDeviceClass() != FXDC_DISPLAY) {
403 CPDF_Object* pFilters =
404 m_pImageObject->m_pImage->GetStream()->GetDict()->GetElementValue(
405 "Filter");
406 if (pFilters) {
407 if (pFilters->IsName()) {
408 CFX_ByteStringC bsDecodeType = pFilters->GetConstString();
409 if (bsDecodeType == "DCTDecode" || bsDecodeType == "JPXDecode") {
410 m_Flags |= FXRENDER_IMAGE_LOSSY;
411 }
412 } else if (CPDF_Array* pArray = pFilters->AsArray()) {
413 for (FX_DWORD i = 0; i < pArray->GetCount(); i++) {
414 CFX_ByteStringC bsDecodeType = pArray->GetConstStringAt(i);
415 if (bsDecodeType == "DCTDecode" || bsDecodeType == "JPXDecode") {
416 m_Flags |= FXRENDER_IMAGE_LOSSY;
417 break;
418 }
419 }
420 }
421 }
422 }
423 if (m_pRenderStatus->m_Options.m_Flags & RENDER_NOIMAGESMOOTH) {
424 m_Flags |= FXDIB_NOSMOOTH;
425 } else if (m_pImageObject->m_pImage->IsInterpol()) {
426 m_Flags |= FXDIB_INTERPOL;
427 }
428 if (m_Loader.m_pMask) {
429 return DrawMaskedImage();
430 }
431 if (m_bPatternColor) {
432 return DrawPatternImage(m_pObj2Device);
433 }
434 if (m_BitmapAlpha == 255 && pGeneralState && pGeneralState->m_FillOP &&
435 pGeneralState->m_OPMode == 0 &&
436 pGeneralState->m_BlendType == FXDIB_BLEND_NORMAL &&
437 pGeneralState->m_StrokeAlpha == 1 && pGeneralState->m_FillAlpha == 1) {
438 CPDF_Document* pDocument = NULL;
439 CPDF_Page* pPage = NULL;
440 if (m_pRenderStatus->m_pContext->GetPageCache()) {
441 pPage = m_pRenderStatus->m_pContext->GetPageCache()->GetPage();
442 pDocument = pPage->m_pDocument;
443 } else {
444 pDocument = m_pImageObject->m_pImage->GetDocument();
445 }
446 CPDF_Dictionary* pPageResources = pPage ? pPage->m_pPageResources : NULL;
447 CPDF_Object* pCSObj =
448 m_pImageObject->m_pImage->GetStream()->GetDict()->GetElementValue(
449 "ColorSpace");
450 CPDF_ColorSpace* pColorSpace =
451 pDocument->LoadColorSpace(pCSObj, pPageResources);
452 if (pColorSpace) {
453 int format = pColorSpace->GetFamily();
454 if (format == PDFCS_DEVICECMYK || format == PDFCS_SEPARATION ||
455 format == PDFCS_DEVICEN) {
456 m_BlendType = FXDIB_BLEND_DARKEN;
457 }
458 pDocument->GetPageData()->ReleaseColorSpace(pCSObj);
459 }
460 }
461 return StartDIBSource();
462 }
463 FX_BOOL CPDF_ImageRenderer::Start(CPDF_RenderStatus* pStatus,
464 const CPDF_PageObject* pObj,
465 const CFX_Matrix* pObj2Device,
466 FX_BOOL bStdCS,
467 int blendType) {
468 m_pRenderStatus = pStatus;
469 m_bStdCS = bStdCS;
470 m_pImageObject = pObj->AsImage();
471 m_BlendType = blendType;
472 m_pObj2Device = pObj2Device;
473 CPDF_Dictionary* pOC = m_pImageObject->m_pImage->GetOC();
474 if (pOC && m_pRenderStatus->m_Options.m_pOCContext &&
475 !m_pRenderStatus->m_Options.m_pOCContext->CheckOCGVisible(pOC)) {
476 return FALSE;
477 }
478 m_ImageMatrix = m_pImageObject->m_Matrix;
479 m_ImageMatrix.Concat(*pObj2Device);
480 if (StartLoadDIBSource()) {
481 return TRUE;
482 }
483 return StartRenderDIBSource();
484 }
485 FX_BOOL CPDF_ImageRenderer::Start(CPDF_RenderStatus* pStatus,
486 const CFX_DIBSource* pDIBSource,
487 FX_ARGB bitmap_argb,
488 int bitmap_alpha,
489 const CFX_Matrix* pImage2Device,
490 FX_DWORD flags,
491 FX_BOOL bStdCS,
492 int blendType) {
493 m_pRenderStatus = pStatus;
494 m_pDIBSource = pDIBSource;
495 m_FillArgb = bitmap_argb;
496 m_BitmapAlpha = bitmap_alpha;
497 m_ImageMatrix = *pImage2Device;
498 m_Flags = flags;
499 m_bStdCS = bStdCS;
500 m_BlendType = blendType;
501 return StartDIBSource();
502 }
503 FX_BOOL CPDF_ImageRenderer::DrawPatternImage(const CFX_Matrix* pObj2Device) {
504 if (m_pRenderStatus->m_bPrint &&
505 !(m_pRenderStatus->m_pDevice->GetRenderCaps() & FXRC_BLEND_MODE)) {
506 m_Result = FALSE;
507 return FALSE;
508 }
509 FX_RECT rect = m_ImageMatrix.GetUnitRect().GetOutterRect();
510 rect.Intersect(m_pRenderStatus->m_pDevice->GetClipBox());
511 if (rect.IsEmpty()) {
512 return FALSE;
513 }
514 CFX_Matrix new_matrix = m_ImageMatrix;
515 new_matrix.TranslateI(-rect.left, -rect.top);
516 int width = rect.Width();
517 int height = rect.Height();
518 CFX_FxgeDevice bitmap_device1;
519 if (!bitmap_device1.Create(rect.Width(), rect.Height(), FXDIB_Rgb32)) {
520 return TRUE;
521 }
522 bitmap_device1.GetBitmap()->Clear(0xffffff);
523 {
524 CPDF_RenderStatus bitmap_render;
525 bitmap_render.Initialize(m_pRenderStatus->m_pContext, &bitmap_device1, NULL,
526 NULL, NULL, NULL, &m_pRenderStatus->m_Options, 0,
527 m_pRenderStatus->m_bDropObjects, NULL, TRUE);
528 CFX_Matrix patternDevice = *pObj2Device;
529 patternDevice.Translate((FX_FLOAT)-rect.left, (FX_FLOAT)-rect.top);
530 if (m_pPattern->m_PatternType == CPDF_Pattern::TILING) {
531 bitmap_render.DrawTilingPattern(
532 static_cast<CPDF_TilingPattern*>(m_pPattern), m_pImageObject,
533 &patternDevice, FALSE);
534 } else {
535 bitmap_render.DrawShadingPattern(
536 static_cast<CPDF_ShadingPattern*>(m_pPattern), m_pImageObject,
537 &patternDevice, FALSE);
538 }
539 }
540 {
541 CFX_FxgeDevice bitmap_device2;
542 if (!bitmap_device2.Create(rect.Width(), rect.Height(), FXDIB_8bppRgb)) {
543 return TRUE;
544 }
545 bitmap_device2.GetBitmap()->Clear(0);
546 CPDF_RenderStatus bitmap_render;
547 bitmap_render.Initialize(m_pRenderStatus->m_pContext, &bitmap_device2, NULL,
548 NULL, NULL, NULL, NULL, 0,
549 m_pRenderStatus->m_bDropObjects, NULL, TRUE);
550 CPDF_ImageRenderer image_render;
551 if (image_render.Start(&bitmap_render, m_pDIBSource, 0xffffffff, 255,
552 &new_matrix, m_Flags, TRUE)) {
553 image_render.Continue(NULL);
554 }
555 if (m_Loader.m_MatteColor != 0xffffffff) {
556 int matte_r = FXARGB_R(m_Loader.m_MatteColor);
557 int matte_g = FXARGB_G(m_Loader.m_MatteColor);
558 int matte_b = FXARGB_B(m_Loader.m_MatteColor);
559 for (int row = 0; row < height; row++) {
560 uint8_t* dest_scan =
561 (uint8_t*)bitmap_device1.GetBitmap()->GetScanline(row);
562 const uint8_t* mask_scan = bitmap_device2.GetBitmap()->GetScanline(row);
563 for (int col = 0; col < width; col++) {
564 int alpha = *mask_scan++;
565 if (alpha) {
566 int orig = (*dest_scan - matte_b) * 255 / alpha + matte_b;
567 if (orig < 0) {
568 orig = 0;
569 } else if (orig > 255) {
570 orig = 255;
571 }
572 *dest_scan++ = orig;
573 orig = (*dest_scan - matte_g) * 255 / alpha + matte_g;
574 if (orig < 0) {
575 orig = 0;
576 } else if (orig > 255) {
577 orig = 255;
578 }
579 *dest_scan++ = orig;
580 orig = (*dest_scan - matte_r) * 255 / alpha + matte_r;
581 if (orig < 0) {
582 orig = 0;
583 } else if (orig > 255) {
584 orig = 255;
585 }
586 *dest_scan++ = orig;
587 dest_scan++;
588 } else {
589 dest_scan += 4;
590 }
591 }
592 }
593 }
594 bitmap_device2.GetBitmap()->ConvertFormat(FXDIB_8bppMask);
595 bitmap_device1.GetBitmap()->MultiplyAlpha(bitmap_device2.GetBitmap());
596 bitmap_device1.GetBitmap()->MultiplyAlpha(255);
597 }
598 m_pRenderStatus->m_pDevice->SetDIBits(bitmap_device1.GetBitmap(), rect.left,
599 rect.top, m_BlendType);
600 return FALSE;
601 }
602 FX_BOOL CPDF_ImageRenderer::DrawMaskedImage() {
603 if (m_pRenderStatus->m_bPrint &&
604 !(m_pRenderStatus->m_pDevice->GetRenderCaps() & FXRC_BLEND_MODE)) {
605 m_Result = FALSE;
606 return FALSE;
607 }
608 FX_RECT rect = m_ImageMatrix.GetUnitRect().GetOutterRect();
609 rect.Intersect(m_pRenderStatus->m_pDevice->GetClipBox());
610 if (rect.IsEmpty()) {
611 return FALSE;
612 }
613 CFX_Matrix new_matrix = m_ImageMatrix;
614 new_matrix.TranslateI(-rect.left, -rect.top);
615 int width = rect.Width();
616 int height = rect.Height();
617 CFX_FxgeDevice bitmap_device1;
618 if (!bitmap_device1.Create(width, height, FXDIB_Rgb32)) {
619 return TRUE;
620 }
621 bitmap_device1.GetBitmap()->Clear(0xffffff);
622 {
623 CPDF_RenderStatus bitmap_render;
624 bitmap_render.Initialize(m_pRenderStatus->m_pContext, &bitmap_device1, NULL,
625 NULL, NULL, NULL, NULL, 0,
626 m_pRenderStatus->m_bDropObjects, NULL, TRUE);
627 CPDF_ImageRenderer image_render;
628 if (image_render.Start(&bitmap_render, m_pDIBSource, 0, 255, &new_matrix,
629 m_Flags, TRUE)) {
630 image_render.Continue(NULL);
631 }
632 }
633 {
634 CFX_FxgeDevice bitmap_device2;
635 if (!bitmap_device2.Create(width, height, FXDIB_8bppRgb)) {
636 return TRUE;
637 }
638 bitmap_device2.GetBitmap()->Clear(0);
639 CPDF_RenderStatus bitmap_render;
640 bitmap_render.Initialize(m_pRenderStatus->m_pContext, &bitmap_device2, NULL,
641 NULL, NULL, NULL, NULL, 0,
642 m_pRenderStatus->m_bDropObjects, NULL, TRUE);
643 CPDF_ImageRenderer image_render;
644 if (image_render.Start(&bitmap_render, m_Loader.m_pMask, 0xffffffff, 255,
645 &new_matrix, m_Flags, TRUE)) {
646 image_render.Continue(NULL);
647 }
648 if (m_Loader.m_MatteColor != 0xffffffff) {
649 int matte_r = FXARGB_R(m_Loader.m_MatteColor);
650 int matte_g = FXARGB_G(m_Loader.m_MatteColor);
651 int matte_b = FXARGB_B(m_Loader.m_MatteColor);
652 for (int row = 0; row < height; row++) {
653 uint8_t* dest_scan =
654 (uint8_t*)bitmap_device1.GetBitmap()->GetScanline(row);
655 const uint8_t* mask_scan = bitmap_device2.GetBitmap()->GetScanline(row);
656 for (int col = 0; col < width; col++) {
657 int alpha = *mask_scan++;
658 if (alpha) {
659 int orig = (*dest_scan - matte_b) * 255 / alpha + matte_b;
660 if (orig < 0) {
661 orig = 0;
662 } else if (orig > 255) {
663 orig = 255;
664 }
665 *dest_scan++ = orig;
666 orig = (*dest_scan - matte_g) * 255 / alpha + matte_g;
667 if (orig < 0) {
668 orig = 0;
669 } else if (orig > 255) {
670 orig = 255;
671 }
672 *dest_scan++ = orig;
673 orig = (*dest_scan - matte_r) * 255 / alpha + matte_r;
674 if (orig < 0) {
675 orig = 0;
676 } else if (orig > 255) {
677 orig = 255;
678 }
679 *dest_scan++ = orig;
680 dest_scan++;
681 } else {
682 dest_scan += 4;
683 }
684 }
685 }
686 }
687 bitmap_device2.GetBitmap()->ConvertFormat(FXDIB_8bppMask);
688 bitmap_device1.GetBitmap()->MultiplyAlpha(bitmap_device2.GetBitmap());
689 if (m_BitmapAlpha < 255) {
690 bitmap_device1.GetBitmap()->MultiplyAlpha(m_BitmapAlpha);
691 }
692 }
693 m_pRenderStatus->m_pDevice->SetDIBits(bitmap_device1.GetBitmap(), rect.left,
694 rect.top, m_BlendType);
695 return FALSE;
696 }
697 FX_BOOL CPDF_ImageRenderer::StartDIBSource() {
698 if (!(m_Flags & RENDER_FORCE_DOWNSAMPLE) && m_pDIBSource->GetBPP() > 1) {
699 int image_size = m_pDIBSource->GetBPP() / 8 * m_pDIBSource->GetWidth() *
700 m_pDIBSource->GetHeight();
701 if (image_size > FPDF_HUGE_IMAGE_SIZE &&
702 !(m_Flags & RENDER_FORCE_HALFTONE)) {
703 m_Flags |= RENDER_FORCE_DOWNSAMPLE;
704 }
705 }
706 if (m_pRenderStatus->m_pDevice->StartDIBits(
707 m_pDIBSource, m_BitmapAlpha, m_FillArgb, &m_ImageMatrix, m_Flags,
708 m_DeviceHandle, 0, NULL, m_BlendType)) {
709 if (m_DeviceHandle) {
710 m_Status = 3;
711 return TRUE;
712 }
713 return FALSE;
714 }
715 CFX_FloatRect image_rect_f = m_ImageMatrix.GetUnitRect();
716 FX_RECT image_rect = image_rect_f.GetOutterRect();
717 int dest_width = image_rect.Width();
718 int dest_height = image_rect.Height();
719 if ((FXSYS_fabs(m_ImageMatrix.b) >= 0.5f || m_ImageMatrix.a == 0) ||
720 (FXSYS_fabs(m_ImageMatrix.c) >= 0.5f || m_ImageMatrix.d == 0)) {
721 if (m_pRenderStatus->m_bPrint &&
722 !(m_pRenderStatus->m_pDevice->GetRenderCaps() & FXRC_BLEND_MODE)) {
723 m_Result = FALSE;
724 return FALSE;
725 }
726 FX_RECT clip_box = m_pRenderStatus->m_pDevice->GetClipBox();
727 clip_box.Intersect(image_rect);
728 m_Status = 2;
729 m_pTransformer = new CFX_ImageTransformer;
730 m_pTransformer->Start(m_pDIBSource, &m_ImageMatrix, m_Flags, &clip_box);
731 return TRUE;
732 }
733 if (m_ImageMatrix.a < 0) {
734 dest_width = -dest_width;
735 }
736 if (m_ImageMatrix.d > 0) {
737 dest_height = -dest_height;
738 }
739 int dest_left, dest_top;
740 dest_left = dest_width > 0 ? image_rect.left : image_rect.right;
741 dest_top = dest_height > 0 ? image_rect.top : image_rect.bottom;
742 if (m_pDIBSource->IsOpaqueImage() && m_BitmapAlpha == 255) {
743 if (m_pRenderStatus->m_pDevice->StretchDIBits(
744 m_pDIBSource, dest_left, dest_top, dest_width, dest_height, m_Flags,
745 NULL, m_BlendType)) {
746 return FALSE;
747 }
748 }
749 if (m_pDIBSource->IsAlphaMask()) {
750 if (m_BitmapAlpha != 255) {
751 m_FillArgb = FXARGB_MUL_ALPHA(m_FillArgb, m_BitmapAlpha);
752 }
753 if (m_pRenderStatus->m_pDevice->StretchBitMask(
754 m_pDIBSource, dest_left, dest_top, dest_width, dest_height,
755 m_FillArgb, m_Flags)) {
756 return FALSE;
757 }
758 }
759 if (m_pRenderStatus->m_bPrint &&
760 !(m_pRenderStatus->m_pDevice->GetRenderCaps() & FXRC_BLEND_MODE)) {
761 m_Result = FALSE;
762 return TRUE;
763 }
764 FX_RECT clip_box = m_pRenderStatus->m_pDevice->GetClipBox();
765 FX_RECT dest_rect = clip_box;
766 dest_rect.Intersect(image_rect);
767 FX_RECT dest_clip(
768 dest_rect.left - image_rect.left, dest_rect.top - image_rect.top,
769 dest_rect.right - image_rect.left, dest_rect.bottom - image_rect.top);
770 std::unique_ptr<CFX_DIBitmap> pStretched(
771 m_pDIBSource->StretchTo(dest_width, dest_height, m_Flags, &dest_clip));
772 if (pStretched) {
773 m_pRenderStatus->CompositeDIBitmap(pStretched.get(), dest_rect.left,
774 dest_rect.top, m_FillArgb, m_BitmapAlpha,
775 m_BlendType, FALSE);
776 }
777 return FALSE;
778 }
779 FX_BOOL CPDF_ImageRenderer::StartBitmapAlpha() {
780 if (m_pDIBSource->IsOpaqueImage()) {
781 CFX_PathData path;
782 path.AppendRect(0, 0, 1, 1);
783 path.Transform(&m_ImageMatrix);
784 FX_DWORD fill_color =
785 ArgbEncode(0xff, m_BitmapAlpha, m_BitmapAlpha, m_BitmapAlpha);
786 m_pRenderStatus->m_pDevice->DrawPath(&path, NULL, NULL, fill_color, 0,
787 FXFILL_WINDING);
788 } else {
789 const CFX_DIBSource* pAlphaMask = m_pDIBSource->IsAlphaMask()
790 ? m_pDIBSource
791 : m_pDIBSource->GetAlphaMask();
792 if (FXSYS_fabs(m_ImageMatrix.b) >= 0.5f ||
793 FXSYS_fabs(m_ImageMatrix.c) >= 0.5f) {
794 int left, top;
795 std::unique_ptr<CFX_DIBitmap> pTransformed(
796 pAlphaMask->TransformTo(&m_ImageMatrix, left, top));
797 if (!pTransformed)
798 return TRUE;
799
800 m_pRenderStatus->m_pDevice->SetBitMask(
801 pTransformed.get(), left, top,
802 ArgbEncode(0xff, m_BitmapAlpha, m_BitmapAlpha, m_BitmapAlpha));
803 } else {
804 CFX_FloatRect image_rect_f = m_ImageMatrix.GetUnitRect();
805 FX_RECT image_rect = image_rect_f.GetOutterRect();
806 int dest_width =
807 m_ImageMatrix.a > 0 ? image_rect.Width() : -image_rect.Width();
808 int dest_height =
809 m_ImageMatrix.d > 0 ? -image_rect.Height() : image_rect.Height();
810 int left = dest_width > 0 ? image_rect.left : image_rect.right;
811 int top = dest_height > 0 ? image_rect.top : image_rect.bottom;
812 m_pRenderStatus->m_pDevice->StretchBitMask(
813 pAlphaMask, left, top, dest_width, dest_height,
814 ArgbEncode(0xff, m_BitmapAlpha, m_BitmapAlpha, m_BitmapAlpha));
815 }
816 if (m_pDIBSource != pAlphaMask) {
817 delete pAlphaMask;
818 }
819 }
820 return FALSE;
821 }
822 FX_BOOL CPDF_ImageRenderer::Continue(IFX_Pause* pPause) {
823 if (m_Status == 2) {
824 if (m_pTransformer->Continue(pPause)) {
825 return TRUE;
826 }
827 CFX_DIBitmap* pBitmap = m_pTransformer->m_Storer.Detach();
828 if (!pBitmap) {
829 return FALSE;
830 }
831 if (pBitmap->IsAlphaMask()) {
832 if (m_BitmapAlpha != 255) {
833 m_FillArgb = FXARGB_MUL_ALPHA(m_FillArgb, m_BitmapAlpha);
834 }
835 m_Result = m_pRenderStatus->m_pDevice->SetBitMask(
836 pBitmap, m_pTransformer->m_ResultLeft, m_pTransformer->m_ResultTop,
837 m_FillArgb);
838 } else {
839 if (m_BitmapAlpha != 255) {
840 pBitmap->MultiplyAlpha(m_BitmapAlpha);
841 }
842 m_Result = m_pRenderStatus->m_pDevice->SetDIBits(
843 pBitmap, m_pTransformer->m_ResultLeft, m_pTransformer->m_ResultTop,
844 m_BlendType);
845 }
846 delete pBitmap;
847 return FALSE;
848 }
849 if (m_Status == 3) {
850 return m_pRenderStatus->m_pDevice->ContinueDIBits(m_DeviceHandle, pPause);
851 }
852 if (m_Status == 4) {
853 if (m_Loader.Continue(m_LoadHandle, pPause)) {
854 return TRUE;
855 }
856 if (StartRenderDIBSource()) {
857 return Continue(pPause);
858 }
859 }
860 return FALSE;
861 }
862 ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder(
863 const uint8_t* src_buf,
864 FX_DWORD src_size,
865 int width,
866 int height,
867 int nComps,
868 int bpc,
869 const CPDF_Dictionary* pParams);
870 CFX_DIBitmap* CPDF_RenderStatus::LoadSMask(CPDF_Dictionary* pSMaskDict,
871 FX_RECT* pClipRect,
872 const CFX_Matrix* pMatrix) {
873 if (!pSMaskDict) {
874 return NULL;
875 }
876 int width = pClipRect->right - pClipRect->left;
877 int height = pClipRect->bottom - pClipRect->top;
878 FX_BOOL bLuminosity = FALSE;
879 bLuminosity = pSMaskDict->GetConstStringBy("S") != "Alpha";
880 CPDF_Stream* pGroup = pSMaskDict->GetStreamBy("G");
881 if (!pGroup) {
882 return NULL;
883 }
884 std::unique_ptr<CPDF_Function> pFunc;
885 CPDF_Object* pFuncObj = pSMaskDict->GetElementValue("TR");
886 if (pFuncObj && (pFuncObj->IsDictionary() || pFuncObj->IsStream()))
887 pFunc.reset(CPDF_Function::Load(pFuncObj));
888
889 CFX_Matrix matrix = *pMatrix;
890 matrix.TranslateI(-pClipRect->left, -pClipRect->top);
891 CPDF_Form form(m_pContext->GetDocument(), m_pContext->GetPageResources(),
892 pGroup);
893 form.ParseContent(NULL, NULL, NULL, NULL);
894 CFX_FxgeDevice bitmap_device;
895 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
896 if (!bitmap_device.Create(width, height,
897 bLuminosity ? FXDIB_Rgb32 : FXDIB_8bppMask)) {
898 return NULL;
899 }
900 #else
901 if (!bitmap_device.Create(width, height,
902 bLuminosity ? FXDIB_Rgb : FXDIB_8bppMask)) {
903 return NULL;
904 }
905 #endif
906 CFX_DIBitmap& bitmap = *bitmap_device.GetBitmap();
907 CPDF_Object* pCSObj = NULL;
908 CPDF_ColorSpace* pCS = NULL;
909 if (bLuminosity) {
910 CPDF_Array* pBC = pSMaskDict->GetArrayBy("BC");
911 FX_ARGB back_color = 0xff000000;
912 if (pBC) {
913 CPDF_Dictionary* pDict = pGroup->GetDict();
914 if (pDict && pDict->GetDictBy("Group"))
915 pCSObj = pDict->GetDictBy("Group")->GetElementValue("CS");
916 else
917 pCSObj = NULL;
918 pCS = m_pContext->GetDocument()->LoadColorSpace(pCSObj);
919 if (pCS) {
920 FX_FLOAT R, G, B;
921 FX_DWORD comps = 8;
922 if (pCS->CountComponents() > comps) {
923 comps = pCS->CountComponents();
924 }
925 CFX_FixedBufGrow<FX_FLOAT, 8> float_array(comps);
926 FX_FLOAT* pFloats = float_array;
927 FX_SAFE_DWORD num_floats = comps;
928 num_floats *= sizeof(FX_FLOAT);
929 if (!num_floats.IsValid()) {
930 return NULL;
931 }
932 FXSYS_memset(pFloats, 0, num_floats.ValueOrDie());
933 int count = pBC->GetCount() > 8 ? 8 : pBC->GetCount();
934 for (int i = 0; i < count; i++) {
935 pFloats[i] = pBC->GetNumberAt(i);
936 }
937 pCS->GetRGB(pFloats, R, G, B);
938 back_color = 0xff000000 | ((int32_t)(R * 255) << 16) |
939 ((int32_t)(G * 255) << 8) | (int32_t)(B * 255);
940 m_pContext->GetDocument()->GetPageData()->ReleaseColorSpace(pCSObj);
941 }
942 }
943 bitmap.Clear(back_color);
944 } else {
945 bitmap.Clear(0);
946 }
947 CPDF_Dictionary* pFormResource = NULL;
948 if (form.m_pFormDict) {
949 pFormResource = form.m_pFormDict->GetDictBy("Resources");
950 }
951 CPDF_RenderOptions options;
952 options.m_ColorMode = bLuminosity ? RENDER_COLOR_NORMAL : RENDER_COLOR_ALPHA;
953 CPDF_RenderStatus status;
954 status.Initialize(m_pContext, &bitmap_device, NULL, NULL, NULL, NULL,
955 &options, 0, m_bDropObjects, pFormResource, TRUE, NULL, 0,
956 pCS ? pCS->GetFamily() : 0, bLuminosity);
957 status.RenderObjectList(&form, &matrix);
958 std::unique_ptr<CFX_DIBitmap> pMask(new CFX_DIBitmap);
959 if (!pMask->Create(width, height, FXDIB_8bppMask))
960 return nullptr;
961
962 uint8_t* dest_buf = pMask->GetBuffer();
963 int dest_pitch = pMask->GetPitch();
964 uint8_t* src_buf = bitmap.GetBuffer();
965 int src_pitch = bitmap.GetPitch();
966 std::vector<uint8_t> transfers(256);
967 if (pFunc) {
968 CFX_FixedBufGrow<FX_FLOAT, 16> results(pFunc->CountOutputs());
969 for (int i = 0; i < 256; i++) {
970 FX_FLOAT input = (FX_FLOAT)i / 255.0f;
971 int nresult;
972 pFunc->Call(&input, 1, results, nresult);
973 transfers[i] = FXSYS_round(results[0] * 255);
974 }
975 } else {
976 for (int i = 0; i < 256; i++) {
977 transfers[i] = i;
978 }
979 }
980 if (bLuminosity) {
981 int Bpp = bitmap.GetBPP() / 8;
982 for (int row = 0; row < height; row++) {
983 uint8_t* dest_pos = dest_buf + row * dest_pitch;
984 uint8_t* src_pos = src_buf + row * src_pitch;
985 for (int col = 0; col < width; col++) {
986 *dest_pos++ = transfers[FXRGB2GRAY(src_pos[2], src_pos[1], *src_pos)];
987 src_pos += Bpp;
988 }
989 }
990 } else if (pFunc) {
991 int size = dest_pitch * height;
992 for (int i = 0; i < size; i++) {
993 dest_buf[i] = transfers[src_buf[i]];
994 }
995 } else {
996 FXSYS_memcpy(dest_buf, src_buf, dest_pitch * height);
997 }
998 return pMask.release();
999 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_render/fpdf_render_cache.cpp ('k') | core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698