michael@0: michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifdef SK_BUILD_FOR_WIN32 michael@0: #pragma warning(push) michael@0: #pragma warning(disable : 4530) michael@0: #endif michael@0: michael@0: #include "SkPDFRasterizer.h" michael@0: #include "SkColorPriv.h" michael@0: michael@0: #ifdef SK_BUILD_NATIVE_PDF_RENDERER michael@0: #include "SkPdfRenderer.h" michael@0: #endif // SK_BUILD_NATIVE_PDF_RENDERER michael@0: michael@0: #ifdef SK_BUILD_POPPLER michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif // SK_BUILD_POPPLER michael@0: michael@0: #ifdef SK_BUILD_POPPLER michael@0: bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { michael@0: size_t size = pdf->getLength(); michael@0: SkAutoFree buffer(sk_malloc_throw(size)); michael@0: pdf->read(buffer.get(), size); michael@0: michael@0: SkAutoTDelete doc( michael@0: poppler::document::load_from_raw_data((const char*)buffer.get(), size)); michael@0: if (!doc.get() || doc->is_locked()) { michael@0: return false; michael@0: } michael@0: michael@0: SkAutoTDelete page(doc->create_page(0)); michael@0: poppler::page_renderer renderer; michael@0: poppler::image image = renderer.render_page(page.get()); michael@0: michael@0: if (!image.is_valid() || image.format() != poppler::image::format_argb32) { michael@0: return false; michael@0: } michael@0: michael@0: int width = image.width(), height = image.height(); michael@0: size_t rowSize = image.bytes_per_row(); michael@0: char *imgData = image.data(); michael@0: michael@0: SkBitmap bitmap; michael@0: if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height))) { michael@0: return false; michael@0: } michael@0: bitmap.eraseColor(SK_ColorWHITE); michael@0: SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); michael@0: michael@0: // do pixel-by-pixel copy to deal with RGBA ordering conversions michael@0: for (int y = 0; y < height; y++) { michael@0: char *rowData = imgData; michael@0: for (int x = 0; x < width; x++) { michael@0: uint8_t a = rowData[3]; michael@0: uint8_t r = rowData[2]; michael@0: uint8_t g = rowData[1]; michael@0: uint8_t b = rowData[0]; michael@0: michael@0: *bitmapPixels = SkPreMultiplyARGB(a, r, g, b); michael@0: michael@0: bitmapPixels++; michael@0: rowData += 4; michael@0: } michael@0: imgData += rowSize; michael@0: } michael@0: michael@0: output->swap(bitmap); michael@0: michael@0: return true; michael@0: } michael@0: #endif // SK_BUILD_POPPLER michael@0: michael@0: #ifdef SK_BUILD_NATIVE_PDF_RENDERER michael@0: bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) { michael@0: return SkPDFNativeRenderToBitmap(pdf, output); michael@0: } michael@0: #endif // SK_BUILD_NATIVE_PDF_RENDERER