Chromium Code Reviews| 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 <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 | |
| 22 bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { | |
|
edisonn
2013/08/08 20:14:17
instead of a function pointer, what about an abstr
ducky
2013/08/08 21:35:03
This was originally designed as a drop-in replacem
| |
| 23 size_t size = pdf->getLength(); | |
| 24 void* buffer = sk_malloc_throw(size); | |
| 25 pdf->read(buffer, size); | |
| 26 | |
| 27 SkAutoTDelete<poppler::document> doc( | |
| 28 poppler::document::load_from_raw_data((const char*)buffer, size)); | |
| 29 if (!doc.get() || doc->is_locked()) { | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 SkAutoTDelete<poppler::page> page(doc->create_page(0)); | |
| 34 poppler::page_renderer renderer; | |
| 35 poppler::image image = renderer.render_page(page.get()); | |
| 36 | |
| 37 if (!image.is_valid() || image.format() != poppler::image::format_argb32) { | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 size_t width = image.width(), height = image.height(); | |
| 42 size_t rowSize = image.bytes_per_row(); | |
| 43 char *imgData = image.data(); | |
| 44 | |
| 45 SkBitmap bitmap; | |
| 46 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 47 bitmap.allocPixels(); | |
|
edisonn
2013/08/08 20:14:17
check alloc pixels, we will crash here if we fail
ducky
2013/08/08 21:35:03
Done.
| |
| 48 bitmap.eraseColor(SK_ColorWHITE); | |
| 49 SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); | |
| 50 | |
| 51 // do pixel-by-pixel copy to deal with RGBA ordering conversions | |
| 52 for (size_t y = 0; y < height; y++) { | |
| 53 char *rowData = imgData; | |
| 54 for (size_t x = 0; x < width; x++) { | |
| 55 U8CPU r = (unsigned char)rowData[2]; | |
| 56 U8CPU g = (unsigned char)rowData[1]; | |
| 57 U8CPU b = (unsigned char)rowData[0]; | |
| 58 | |
| 59 SkPMColor out = SkPackARGB32( | |
| 60 255, r, g, b); | |
| 61 | |
| 62 *bitmapPixels = out; | |
| 63 | |
| 64 bitmapPixels++; | |
| 65 rowData += 4; | |
| 66 } | |
| 67 imgData += rowSize; | |
| 68 } | |
| 69 | |
| 70 output->swap(bitmap); | |
| 71 | |
| 72 return true; | |
| 73 } | |
| OLD | NEW |