1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/utils/pdf_conversion.h"
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "printing/units.h"
#include "third_party/skia/include/codec/SkCodec.h"
#include "third_party/skia/include/codec/SkJpegDecoder.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkTypes.h"
#include "third_party/skia/include/docs/SkPDFDocument.h"
#include "ui/gfx/image/buffer_w_stream.h"
namespace chromeos {
namespace {
// The number of degrees to rotate a PDF image.
constexpr int kRotationDegrees = 180;
// Creates a new page for the PDF document and adds `image_data` to the page.
// `rotate` indicates whether the page should be rotated 180 degrees.
// Returns whether the page was successfully created.
bool AddPdfPage(sk_sp<SkDocument> pdf_doc,
const sk_sp<SkData>& jpeg_image_data,
bool rotate,
std::optional<int> dpi) {
if (!SkJpegDecoder::IsJpeg(jpeg_image_data->data(),
jpeg_image_data->size())) {
LOG(ERROR) << "Not a valid JPEG image.";
return false;
}
CHECK(
SkJpegDecoder::IsJpeg(jpeg_image_data->data(), jpeg_image_data->size()));
const sk_sp<SkImage> image =
SkImages::DeferredFromEncodedData(jpeg_image_data);
if (!image) {
LOG(ERROR) << "Unable to generate image from encoded image data.";
return false;
}
// Convert from JPG dimensions in pixels (DPI) to PDF dimensions in points
// (1/72 in).
int page_width;
int page_height;
if (dpi.has_value() && dpi.value() > 0) {
page_width = printing::ConvertUnit(image->width(), dpi.value(),
printing::kPointsPerInch);
page_height = printing::ConvertUnit(image->height(), dpi.value(),
printing::kPointsPerInch);
} else {
page_width = image->width();
page_height = image->height();
}
SkCanvas* page_canvas = pdf_doc->beginPage(page_width, page_height);
if (!page_canvas) {
LOG(ERROR) << "Unable to access PDF page canvas.";
return false;
}
// Rotate pages that were flipped by an ADF scanner.
if (rotate) {
page_canvas->rotate(kRotationDegrees);
page_canvas->translate(-page_width, -page_height);
}
SkRect image_bounds = SkRect::MakeIWH(page_width, page_height);
page_canvas->drawImageRect(image, image_bounds, SkSamplingOptions());
pdf_doc->endPage();
return true;
}
} // namespace
bool ConvertJpgImagesToPdf(const std::vector<std::string>& jpg_images,
const base::FilePath& file_path,
bool rotate_alternate_pages,
std::optional<int> dpi) {
DCHECK(!file_path.empty());
// Register Jpeg Decoder for use by DeferredFromEncodedData in AddPdfPage.
SkCodecs::Register(SkJpegDecoder::Decoder());
SkFILEWStream pdf_outfile(file_path.value().c_str());
if (!pdf_outfile.isValid()) {
LOG(ERROR) << "Unable to open output file.";
return false;
}
sk_sp<SkDocument> pdf_doc = SkPDF::MakeDocument(&pdf_outfile);
DCHECK(pdf_doc);
// Never rotate first page of PDF.
bool rotate_current_page = false;
for (const auto& jpg_image : jpg_images) {
SkDynamicMemoryWStream img_stream;
if (!img_stream.write(jpg_image.c_str(), jpg_image.size())) {
LOG(ERROR) << "Unable to write image to dynamic memory stream.";
return false;
}
const sk_sp<SkData> img_data = img_stream.detachAsData();
if (img_data->isEmpty()) {
LOG(ERROR) << "Stream data is empty.";
return false;
}
if (!AddPdfPage(pdf_doc, img_data, rotate_current_page, dpi)) {
LOG(ERROR) << "Unable to add new PDF page.";
return false;
}
if (rotate_alternate_pages) {
rotate_current_page = !rotate_current_page;
}
}
pdf_doc->close();
return true;
}
bool ConvertJpgImagesToPdf(const std::vector<std::vector<uint8_t>>& jpg_images,
std::vector<uint8_t>* output) {
gfx::BufferWStream output_stream;
sk_sp<SkDocument> pdf_doc = SkPDF::MakeDocument(&output_stream);
DCHECK(pdf_doc);
// Register Jpeg Decoder for use by DeferredFromEncodedData in AddPdfPage.
SkCodecs::Register(SkJpegDecoder::Decoder());
for (const auto& jpg_image : jpg_images) {
SkDynamicMemoryWStream img_stream;
bool result = img_stream.write(jpg_image.data(), jpg_image.size());
CHECK(result);
const sk_sp<SkData> img_data = img_stream.detachAsData();
if (img_data->isEmpty()) {
LOG(ERROR) << "Stream data is empty.";
return false;
}
if (!AddPdfPage(pdf_doc, img_data, false, std::nullopt)) {
LOG(ERROR) << "Unable to add new PDF page.";
return false;
}
}
pdf_doc->close();
*output = output_stream.TakeBuffer();
return true;
}
} // namespace chromeos
|