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..e0c951a56533fa7850d1710e14ac2f667a1cbfcd |
| --- /dev/null |
| +++ b/src/utils/SkPDFRasterizer.cpp |
| @@ -0,0 +1,75 @@ |
| + |
| +/* |
| + * 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) |
|
vandebo (ex-Chrome)
2013/08/14 15:41:37
Don't indent these.
ducky
2013/08/14 20:43:18
Done.
|
| + #pragma warning(disable : 4530) |
| +#endif |
| + |
| +#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); |
| + if (!bitmap.allocPixels()) { |
| + return false; |
| + } |
| + 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); |
|
vandebo (ex-Chrome)
2013/08/14 15:41:37
Are you dropping the alpha component?
vandebo (ex-Chrome)
2013/08/14 15:41:37
nit: line wrap.
ducky
2013/08/14 20:43:18
Done.
ducky
2013/08/14 20:43:18
Yes. I tried adding back in the alpha, but the pic
vandebo (ex-Chrome)
2013/08/15 00:22:13
This concerns me. Can you try using this with a p
ducky
2013/08/15 01:19:12
Looks like I typo'd when unpacking my pixels...
Sk
|
| + |
| + *bitmapPixels = out; |
| + |
| + bitmapPixels++; |
| + rowData += 4; |
| + } |
| + imgData += rowSize; |
| + } |
| + |
| + output->swap(bitmap); |
| + |
| + return true; |
| +} |