| OLD | NEW |
| (Empty) |
| 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 "SkPDFRasterizer.h" | |
| 15 #include "SkColorPriv.h" | |
| 16 | |
| 17 #ifdef SK_BUILD_NATIVE_PDF_RENDERER | |
| 18 #include "SkPdfRenderer.h" | |
| 19 #endif // SK_BUILD_NATIVE_PDF_RENDERER | |
| 20 | |
| 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 | |
| 27 | |
| 28 #ifdef SK_BUILD_POPPLER | |
| 29 bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { | |
| 30 SkAutoTDelete<SkStream> streamDeleter(pdf); | |
| 31 size_t size = pdf->getLength(); | |
| 32 SkAutoFree buffer(sk_malloc_throw(size)); | |
| 33 pdf->read(buffer.get(), size); | |
| 34 | |
| 35 SkAutoTDelete<poppler::document> doc( | |
| 36 poppler::document::load_from_raw_data((const char*)buffer.get(), size)); | |
| 37 if (!doc.get() || doc->is_locked()) { | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 SkAutoTDelete<poppler::page> page(doc->create_page(0)); | |
| 42 poppler::page_renderer renderer; | |
| 43 poppler::image image = renderer.render_page(page.get()); | |
| 44 | |
| 45 if (!image.is_valid() || image.format() != poppler::image::format_argb32) { | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 int width = image.width(), height = image.height(); | |
| 50 size_t rowSize = image.bytes_per_row(); | |
| 51 char *imgData = image.data(); | |
| 52 | |
| 53 SkBitmap bitmap; | |
| 54 if (!bitmap.tryAllocN32Pixels(width, height)) { | |
| 55 return false; | |
| 56 } | |
| 57 bitmap.eraseColor(SK_ColorWHITE); | |
| 58 SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); | |
| 59 | |
| 60 // do pixel-by-pixel copy to deal with RGBA ordering conversions | |
| 61 for (int y = 0; y < height; y++) { | |
| 62 char *rowData = imgData; | |
| 63 for (int x = 0; x < width; x++) { | |
| 64 uint8_t a = rowData[3]; | |
| 65 uint8_t r = rowData[2]; | |
| 66 uint8_t g = rowData[1]; | |
| 67 uint8_t b = rowData[0]; | |
| 68 | |
| 69 *bitmapPixels = SkPreMultiplyARGB(a, r, g, b); | |
| 70 | |
| 71 bitmapPixels++; | |
| 72 rowData += 4; | |
| 73 } | |
| 74 imgData += rowSize; | |
| 75 } | |
| 76 | |
| 77 output->swap(bitmap); | |
| 78 | |
| 79 return true; | |
| 80 } | |
| 81 #endif // SK_BUILD_POPPLER | |
| 82 | |
| 83 #ifdef SK_BUILD_NATIVE_PDF_RENDERER | |
| 84 bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) { | |
| 85 SkAutoTDelete<SkStream> streamDeleter(pdf); | |
| 86 return SkPDFNativeRenderToBitmap(pdf, output); | |
| 87 } | |
| 88 #endif // SK_BUILD_NATIVE_PDF_RENDERER | |
| OLD | NEW |