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 #include <poppler-document.h> |
| 10 #include <poppler-image.h> |
| 11 #include <poppler-page.h> |
| 12 #include <poppler-page-renderer.h> |
| 13 |
| 14 #include "SkPDFRasterizer.h" |
| 15 #include "SkColorPriv.h" |
| 16 |
| 17 bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { |
| 18 size_t size = pdf->getLength(); |
| 19 void* buffer = sk_malloc_throw(size); |
| 20 pdf->read(buffer, size); |
| 21 |
| 22 SkAutoTDelete<poppler::document> doc( |
| 23 poppler::document::load_from_raw_data((const char*)buffer, size)); |
| 24 if (!doc.get() || doc->is_locked()) { |
| 25 return false; |
| 26 } |
| 27 |
| 28 SkAutoTDelete<poppler::page> page(doc->create_page(0)); |
| 29 poppler::page_renderer renderer; |
| 30 poppler::image image = renderer.render_page(page.get()); |
| 31 |
| 32 if (!image.is_valid() || image.format() != poppler::image::format_argb32) { |
| 33 return false; |
| 34 } |
| 35 |
| 36 size_t width = image.width(), height = image.height(); |
| 37 size_t rowSize = image.bytes_per_row(); |
| 38 char *imgData = image.data(); |
| 39 |
| 40 SkBitmap bitmap; |
| 41 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 42 bitmap.allocPixels(); |
| 43 bitmap.eraseColor(SK_ColorWHITE); |
| 44 SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); |
| 45 |
| 46 // do pixel-by-pixel copy to deal with RGBA ordering conversions |
| 47 for (size_t y = 0; y < height; y++) { |
| 48 char *rowData = imgData; |
| 49 for (size_t x = 0; x < width; x++) { |
| 50 U8CPU r = (unsigned char)rowData[2]; |
| 51 U8CPU g = (unsigned char)rowData[1]; |
| 52 U8CPU b = (unsigned char)rowData[0]; |
| 53 |
| 54 SkPMColor out = SkPackARGB32( |
| 55 255, r, g, b); |
| 56 |
| 57 *bitmapPixels = out; |
| 58 |
| 59 bitmapPixels++; |
| 60 rowData += 4; |
| 61 } |
| 62 imgData += rowSize; |
| 63 } |
| 64 |
| 65 output->swap(bitmap); |
| 66 |
| 67 return true; |
| 68 } |
OLD | NEW |