commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 14 | #include "SkPDFRasterizer.h" |
| 15 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 1066f08 | 2014-01-10 19:09:00 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | 23a9121 | 2013-11-12 17:44:28 +0000 | [diff] [blame] | 17 | #ifdef SK_BUILD_NATIVE_PDF_RENDERER |
commit-bot@chromium.org | ffd178c | 2013-11-11 15:10:47 +0000 | [diff] [blame] | 18 | #include "SkPdfRenderer.h" |
commit-bot@chromium.org | 23a9121 | 2013-11-12 17:44:28 +0000 | [diff] [blame] | 19 | #endif // SK_BUILD_NATIVE_PDF_RENDERER |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 20 | |
commit-bot@chromium.org | 1066f08 | 2014-01-10 19:09:00 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 29 | bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { |
| 30 | size_t size = pdf->getLength(); |
robertphillips@google.com | 25ae5ee | 2013-10-08 15:40:49 +0000 | [diff] [blame] | 31 | SkAutoFree buffer(sk_malloc_throw(size)); |
| 32 | pdf->read(buffer.get(), size); |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 33 | |
| 34 | SkAutoTDelete<poppler::document> doc( |
robertphillips@google.com | 25ae5ee | 2013-10-08 15:40:49 +0000 | [diff] [blame] | 35 | poppler::document::load_from_raw_data((const char*)buffer.get(), size)); |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 36 | if (!doc.get() || doc->is_locked()) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | SkAutoTDelete<poppler::page> page(doc->create_page(0)); |
| 41 | poppler::page_renderer renderer; |
| 42 | poppler::image image = renderer.render_page(page.get()); |
| 43 | |
| 44 | if (!image.is_valid() || image.format() != poppler::image::format_argb32) { |
| 45 | return false; |
| 46 | } |
| 47 | |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 48 | int width = image.width(), height = image.height(); |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 49 | size_t rowSize = image.bytes_per_row(); |
| 50 | char *imgData = image.data(); |
| 51 | |
| 52 | SkBitmap bitmap; |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 53 | if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height))) { |
robertphillips@google.com | 25ae5ee | 2013-10-08 15:40:49 +0000 | [diff] [blame] | 54 | return false; |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 55 | } |
| 56 | bitmap.eraseColor(SK_ColorWHITE); |
| 57 | SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); |
| 58 | |
| 59 | // do pixel-by-pixel copy to deal with RGBA ordering conversions |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 60 | for (int y = 0; y < height; y++) { |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 61 | char *rowData = imgData; |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 62 | for (int x = 0; x < width; x++) { |
commit-bot@chromium.org | 327c081 | 2013-08-20 18:57:01 +0000 | [diff] [blame] | 63 | uint8_t a = rowData[3]; |
| 64 | uint8_t r = rowData[2]; |
| 65 | uint8_t g = rowData[1]; |
| 66 | uint8_t b = rowData[0]; |
| 67 | |
| 68 | *bitmapPixels = SkPreMultiplyARGB(a, r, g, b); |
| 69 | |
| 70 | bitmapPixels++; |
| 71 | rowData += 4; |
| 72 | } |
| 73 | imgData += rowSize; |
| 74 | } |
| 75 | |
| 76 | output->swap(bitmap); |
| 77 | |
| 78 | return true; |
| 79 | } |
commit-bot@chromium.org | 1066f08 | 2014-01-10 19:09:00 +0000 | [diff] [blame] | 80 | #endif // SK_BUILD_POPPLER |
commit-bot@chromium.org | ffd178c | 2013-11-11 15:10:47 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 23a9121 | 2013-11-12 17:44:28 +0000 | [diff] [blame] | 82 | #ifdef SK_BUILD_NATIVE_PDF_RENDERER |
commit-bot@chromium.org | ffd178c | 2013-11-11 15:10:47 +0000 | [diff] [blame] | 83 | bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) { |
| 84 | return SkPDFNativeRenderToBitmap(pdf, output); |
| 85 | } |
commit-bot@chromium.org | 23a9121 | 2013-11-12 17:44:28 +0000 | [diff] [blame] | 86 | #endif // SK_BUILD_NATIVE_PDF_RENDERER |