gfx/skia/trunk/src/utils/SkPDFRasterizer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/utils/SkPDFRasterizer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2013 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +#ifdef SK_BUILD_FOR_WIN32
    1.13 +#pragma warning(push)
    1.14 +#pragma warning(disable : 4530)
    1.15 +#endif
    1.16 +
    1.17 +#include "SkPDFRasterizer.h"
    1.18 +#include "SkColorPriv.h"
    1.19 +
    1.20 +#ifdef SK_BUILD_NATIVE_PDF_RENDERER
    1.21 +#include "SkPdfRenderer.h"
    1.22 +#endif  // SK_BUILD_NATIVE_PDF_RENDERER
    1.23 +
    1.24 +#ifdef SK_BUILD_POPPLER
    1.25 +#include <poppler-document.h>
    1.26 +#include <poppler-image.h>
    1.27 +#include <poppler-page.h>
    1.28 +#include <poppler-page-renderer.h>
    1.29 +#endif  // SK_BUILD_POPPLER
    1.30 +
    1.31 +#ifdef SK_BUILD_POPPLER
    1.32 +bool SkPopplerRasterizePDF(SkStream* pdf, SkBitmap* output) {
    1.33 +  size_t size = pdf->getLength();
    1.34 +  SkAutoFree buffer(sk_malloc_throw(size));
    1.35 +  pdf->read(buffer.get(), size);
    1.36 +
    1.37 +  SkAutoTDelete<poppler::document> doc(
    1.38 +      poppler::document::load_from_raw_data((const char*)buffer.get(), size));
    1.39 +  if (!doc.get() || doc->is_locked()) {
    1.40 +    return false;
    1.41 +  }
    1.42 +
    1.43 +  SkAutoTDelete<poppler::page> page(doc->create_page(0));
    1.44 +  poppler::page_renderer renderer;
    1.45 +  poppler::image image = renderer.render_page(page.get());
    1.46 +
    1.47 +  if (!image.is_valid() || image.format() != poppler::image::format_argb32) {
    1.48 +    return false;
    1.49 +  }
    1.50 +
    1.51 +  int width = image.width(), height = image.height();
    1.52 +  size_t rowSize = image.bytes_per_row();
    1.53 +  char *imgData = image.data();
    1.54 +
    1.55 +  SkBitmap bitmap;
    1.56 +  if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height))) {
    1.57 +    return false;
    1.58 +  }
    1.59 +  bitmap.eraseColor(SK_ColorWHITE);
    1.60 +  SkPMColor* bitmapPixels = (SkPMColor*)bitmap.getPixels();
    1.61 +
    1.62 +  // do pixel-by-pixel copy to deal with RGBA ordering conversions
    1.63 +  for (int y = 0; y < height; y++) {
    1.64 +    char *rowData = imgData;
    1.65 +    for (int x = 0; x < width; x++) {
    1.66 +      uint8_t a = rowData[3];
    1.67 +      uint8_t r = rowData[2];
    1.68 +      uint8_t g = rowData[1];
    1.69 +      uint8_t b = rowData[0];
    1.70 +
    1.71 +      *bitmapPixels = SkPreMultiplyARGB(a, r, g, b);
    1.72 +
    1.73 +      bitmapPixels++;
    1.74 +      rowData += 4;
    1.75 +    }
    1.76 +    imgData += rowSize;
    1.77 +  }
    1.78 +
    1.79 +  output->swap(bitmap);
    1.80 +
    1.81 +  return true;
    1.82 +}
    1.83 +#endif  // SK_BUILD_POPPLER
    1.84 +
    1.85 +#ifdef SK_BUILD_NATIVE_PDF_RENDERER
    1.86 +bool SkNativeRasterizePDF(SkStream* pdf, SkBitmap* output) {
    1.87 +    return SkPDFNativeRenderToBitmap(pdf, output);
    1.88 +}
    1.89 +#endif  // SK_BUILD_NATIVE_PDF_RENDERER

mercurial