3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #ifdef SK_BUILD_FOR_WIN32
11 #pragma warning(disable : 4530)
14 #include "SkPDFRasterizer.h"
15 #include "SkColorPriv.h"
17 #ifdef SK_BUILD_NATIVE_PDF_RENDERER
18 #include "SkPdfRenderer.h"
19 #endif // SK_BUILD_NATIVE_PDF_RENDERER
21 #ifdef SK_BUILD_POPPLER
22 #include <poppler-document.h>
23 #include <poppler-image.h>
24 #include <poppler-page.h>
25 #include <poppler-page-renderer.h>
26 #endif // SK_BUILD_POPPLER
28 #ifdef SK_BUILD_POPPLER
29 bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) {
30 size_t size = pdf->getLength();
31 SkAutoFree buffer(sk_malloc_throw(size));
32 pdf->read(buffer.get(), size);
34 SkAutoTDelete<poppler::document> doc(
35 poppler::document::load_from_raw_data((const char*)buffer.get(), size));
36 if (!doc.get() || doc->is_locked()) {
40 SkAutoTDelete<poppler::page> page(doc->create_page(0));
41 poppler::page_renderer renderer;
42 poppler::image image = renderer.render_page(page.get());
44 if (!image.is_valid() || image.format() != poppler::image::format_argb32) {
48 int width = image.width(), height = image.height();
49 size_t rowSize = image.bytes_per_row();
50 char *imgData = image.data();
53 if (!bitmap.tryAllocN32Pixels(width, height)) {
56 bitmap.eraseColor(SK_ColorWHITE);
57 SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels();
59 // do pixel-by-pixel copy to deal with RGBA ordering conversions
60 for (int y = 0; y < height; y++) {
61 char *rowData = imgData;
62 for (int x = 0; x < width; x++) {
63 uint8_t a = rowData[3];
64 uint8_t r = rowData[2];
65 uint8_t g = rowData[1];
66 uint8_t b = rowData[0];
68 *bitmapPixels = SkPreMultiplyARGB(a, r, g, b);
80 #endif // SK_BUILD_POPPLER
82 #ifdef SK_BUILD_NATIVE_PDF_RENDERER
83 bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) {
84 return SkPDFNativeRenderToBitmap(pdf, output);
86 #endif // SK_BUILD_NATIVE_PDF_RENDERER