Chromium Code Reviews| Index: src/utils/SkPDFRasterizer.cpp |
| diff --git a/src/utils/SkPDFRasterizer.cpp b/src/utils/SkPDFRasterizer.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..93c3154357b0e3dbc52a77517e6a403476a896e9 |
| --- /dev/null |
| +++ b/src/utils/SkPDFRasterizer.cpp |
| @@ -0,0 +1,73 @@ |
| + |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#ifdef SK_BUILD_FOR_WIN32 |
| + #pragma warning(push) |
| + #pragma warning(disable : 4530) |
| +#endif |
|
ducky
2013/08/01 20:40:23
I have no clue what I'm doing here, but I saw this
vandebo (ex-Chrome)
2013/08/02 15:48:55
What code is using exceptions?
ducky
2013/08/02 22:32:50
I think it's something internal to poppler. The MS
vandebo (ex-Chrome)
2013/08/05 17:46:30
You should look at the code to understand how exce
ducky
2013/08/05 20:43:48
The error message is super-unintuitive, but it loo
|
| + |
| +#include <poppler-document.h> |
| +#include <poppler-image.h> |
| +#include <poppler-page.h> |
| +#include <poppler-page-renderer.h> |
| + |
| +#include "SkPDFRasterizer.h" |
| +#include "SkColorPriv.h" |
| + |
| +bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { |
| + size_t size = pdf->getLength(); |
| + void* buffer = sk_malloc_throw(size); |
| + pdf->read(buffer, size); |
| + |
| + SkAutoTDelete<poppler::document> doc( |
| + poppler::document::load_from_raw_data((const char*)buffer, size)); |
| + if (!doc.get() || doc->is_locked()) { |
| + return false; |
| + } |
| + |
| + SkAutoTDelete<poppler::page> page(doc->create_page(0)); |
| + poppler::page_renderer renderer; |
| + poppler::image image = renderer.render_page(page.get()); |
| + |
| + if (!image.is_valid() || image.format() != poppler::image::format_argb32) { |
| + return false; |
| + } |
| + |
| + size_t width = image.width(), height = image.height(); |
| + size_t rowSize = image.bytes_per_row(); |
| + char *imgData = image.data(); |
| + |
| + SkBitmap bitmap; |
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| + bitmap.allocPixels(); |
| + bitmap.eraseColor(SK_ColorWHITE); |
| + SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); |
| + |
| + // do pixel-by-pixel copy to deal with RGBA ordering conversions |
| + for (size_t y = 0; y < height; y++) { |
| + char *rowData = imgData; |
| + for (size_t x = 0; x < width; x++) { |
| + U8CPU r = (unsigned char)rowData[2]; |
| + U8CPU g = (unsigned char)rowData[1]; |
| + U8CPU b = (unsigned char)rowData[0]; |
| + |
| + SkPMColor out = SkPackARGB32( |
| + 255, r, g, b); |
| + |
| + *bitmapPixels = out; |
| + |
| + bitmapPixels++; |
| + rowData += 4; |
| + } |
| + imgData += rowSize; |
| + } |
| + |
| + output->swap(bitmap); |
| + |
| + return true; |
| +} |