• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2013 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifdef SK_BUILD_FOR_WIN32
10 #pragma warning(push)
11 #pragma warning(disable : 4530)
12 #endif
13 
14 #include <poppler-document.h>
15 #include <poppler-image.h>
16 #include <poppler-page.h>
17 #include <poppler-page-renderer.h>
18 
19 #include "SkPDFRasterizer.h"
20 #include "SkColorPriv.h"
21 #ifdef SK_BUILD_NATIVE_PDF_RENDERER
22 #include "SkPdfRenderer.h"
23 #endif  // SK_BUILD_NATIVE_PDF_RENDERER
24 
SkPopplerRasterizePDF(SkStream * pdf,SkBitmap * output)25 bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) {
26   size_t size = pdf->getLength();
27   SkAutoFree buffer(sk_malloc_throw(size));
28   pdf->read(buffer.get(), size);
29 
30   SkAutoTDelete<poppler::document> doc(
31       poppler::document::load_from_raw_data((const char*)buffer.get(), size));
32   if (!doc.get() || doc->is_locked()) {
33     return false;
34   }
35 
36   SkAutoTDelete<poppler::page> page(doc->create_page(0));
37   poppler::page_renderer renderer;
38   poppler::image image = renderer.render_page(page.get());
39 
40   if (!image.is_valid() || image.format() != poppler::image::format_argb32) {
41     return false;
42   }
43 
44   int width = image.width(), height = image.height();
45   size_t rowSize = image.bytes_per_row();
46   char *imgData = image.data();
47 
48   SkBitmap bitmap;
49   bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
50   if (!bitmap.allocPixels()) {
51     return false;
52   }
53   bitmap.eraseColor(SK_ColorWHITE);
54   SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels();
55 
56   // do pixel-by-pixel copy to deal with RGBA ordering conversions
57   for (int y = 0; y < height; y++) {
58     char *rowData = imgData;
59     for (int x = 0; x < width; x++) {
60       uint8_t a = rowData[3];
61       uint8_t r = rowData[2];
62       uint8_t g = rowData[1];
63       uint8_t b = rowData[0];
64 
65       *bitmapPixels = SkPreMultiplyARGB(a, r, g, b);
66 
67       bitmapPixels++;
68       rowData += 4;
69     }
70     imgData += rowSize;
71   }
72 
73   output->swap(bitmap);
74 
75   return true;
76 }
77 
78 #ifdef SK_BUILD_NATIVE_PDF_RENDERER
SkNativeRasterizePDF(SkStream * pdf,SkBitmap * output)79 bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) {
80     return SkPDFNativeRenderToBitmap(pdf, output);
81 }
82 #endif  // SK_BUILD_NATIVE_PDF_RENDERER
83