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 "SkPDFRasterizer.h" 15 #include "SkColorPriv.h" 16 17 #ifdef SK_BUILD_NATIVE_PDF_RENDERER 18 #include "SkPdfRenderer.h" 19 #endif // SK_BUILD_NATIVE_PDF_RENDERER 20 21 #ifdef SK_BUILD_POPPLER 22 #include <poppler-document.h> 23 #include <poppler-image.h> 24 #include <poppler-page.h> 25 #include <poppler-page-renderer.h> 26 #endif // SK_BUILD_POPPLER 27 28 #ifdef SK_BUILD_POPPLER 29 bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) { 30 size_t size = pdf->getLength(); 31 SkAutoFree buffer(sk_malloc_throw(size)); 32 pdf->read(buffer.get(), size); 33 34 SkAutoTDelete<poppler::document> doc( 35 poppler::document::load_from_raw_data((const char*)buffer.get(), size)); 36 if (!doc.get() || doc->is_locked()) { 37 return false; 38 } 39 40 SkAutoTDelete<poppler::page> page(doc->create_page(0)); 41 poppler::page_renderer renderer; 42 poppler::image image = renderer.render_page(page.get()); 43 44 if (!image.is_valid() || image.format() != poppler::image::format_argb32) { 45 return false; 46 } 47 48 int width = image.width(), height = image.height(); 49 size_t rowSize = image.bytes_per_row(); 50 char *imgData = image.data(); 51 52 SkBitmap bitmap; 53 if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height))) { 54 return false; 55 } 56 bitmap.eraseColor(SK_ColorWHITE); 57 SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels(); 58 59 // do pixel-by-pixel copy to deal with RGBA ordering conversions 60 for (int y = 0; y < height; y++) { 61 char *rowData = imgData; 62 for (int x = 0; x < width; x++) { 63 uint8_t a = rowData[3]; 64 uint8_t r = rowData[2]; 65 uint8_t g = rowData[1]; 66 uint8_t b = rowData[0]; 67 68 *bitmapPixels = SkPreMultiplyARGB(a, r, g, b); 69 70 bitmapPixels++; 71 rowData += 4; 72 } 73 imgData += rowSize; 74 } 75 76 output->swap(bitmap); 77 78 return true; 79 } 80 #endif // SK_BUILD_POPPLER 81 82 #ifdef SK_BUILD_NATIVE_PDF_RENDERER 83 bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) { 84 return SkPDFNativeRenderToBitmap(pdf, output); 85 } 86 #endif // SK_BUILD_NATIVE_PDF_RENDERER 87