0

Modernize codec usage in pdf/

Codec calls have new API that uses std::optional and spans. Switch usage
to those new APIs, and lightly modernize the calling code.

Bug: 370696612
Change-Id: Ic1c4effb2f426595f84fda1d0cad300989e33ecb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5932471
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Auto-Submit: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1369531}
This commit is contained in:
Avi Drissman
2024-10-16 18:20:33 +00:00
committed by Chromium LUCI CQ
parent acdfbc7f98
commit 32d2e0ab08
2 changed files with 15 additions and 14 deletions

@ -440,21 +440,20 @@ ScopedFPDFDocument PDFiumPrint::CreateSinglePageRasterPdf(
kBGRA_8888_SkColorType, kOpaque_SkAlphaType);
SkPixmap src(info, FPDFBitmap_GetBuffer(bitmap.get()),
FPDFBitmap_GetStride(bitmap.get()));
std::vector<uint8_t> compressed_bitmap_data;
bool encoded = gfx::JPEGCodec::Encode(src, kQuality, &compressed_bitmap_data);
std::optional<std::vector<uint8_t>> compressed_bitmap_data =
gfx::JPEGCodec::Encode(src, kQuality);
ScopedFPDFPage temp_page_holder(
FPDFPage_New(temp_doc.get(), 0, source_page_width, source_page_height));
FPDF_PAGE temp_page = temp_page_holder.get();
if (encoded) {
base::span<const uint8_t> compressed_bitmap_span =
base::make_span(compressed_bitmap_data);
if (compressed_bitmap_data) {
base::span<const uint8_t> compressed_bitmap_span(
compressed_bitmap_data.value());
FPDF_FILEACCESS file_access = {};
file_access.m_FileLen =
static_cast<unsigned long>(compressed_bitmap_data.size());
static_cast<unsigned long>(compressed_bitmap_span.size());
file_access.m_GetBlock = [](void* param, unsigned long pos,
unsigned char* buf, unsigned long size) {
const auto& compressed_bitmap_span =
base::span<const uint8_t>& compressed_bitmap_span =
*static_cast<base::span<const uint8_t>*>(param);
if (pos + size < pos || pos + size > compressed_bitmap_span.size()) {
return 0;
@ -464,7 +463,7 @@ ScopedFPDFDocument PDFiumPrint::CreateSinglePageRasterPdf(
buf_span.copy_from(compressed_bitmap_span.subspan(pos, size));
return 1;
};
file_access.m_Param = &compressed_bitmap_data;
file_access.m_Param = &compressed_bitmap_span;
FPDFImageObj_LoadJpegFileInline(&temp_page, 1, temp_img.get(),
&file_access);

@ -45,18 +45,20 @@ void PdfiumProgressiveSearchifier::AddPage(
CHECK(page);
ScopedFPDFPageObject image(FPDFPageObj_NewImageObj(doc_.get()));
CHECK(image);
std::vector<uint8_t> encoded;
CHECK(gfx::JPEGCodec::Encode(bitmap, 100, &encoded));
base::span<const uint8_t> encoded_span = base::make_span(encoded);
std::optional<std::vector<uint8_t>> encoded =
gfx::JPEGCodec::Encode(bitmap, /*quality=*/100);
CHECK(encoded.has_value());
base::span<const uint8_t> encoded_span(encoded.value());
FPDF_FILEACCESS file_access = {};
file_access.m_FileLen = static_cast<unsigned long>(encoded_span.size());
file_access.m_GetBlock = [](void* param, unsigned long pos,
unsigned char* buf, unsigned long size) {
const auto& encoded_span = *static_cast<base::span<const uint8_t>*>(param);
base::span<const uint8_t>& encoded_span =
*static_cast<base::span<const uint8_t>*>(param);
if (pos + size < pos || pos + size > encoded_span.size()) {
return 0;
}
// TODO(tsepez): spanify arguments to remove the error.
// TODO(thestig): spanify arguments to remove the error.
base::span<uint8_t> UNSAFE_TODO(buf_span(buf, size));
buf_span.copy_from(encoded_span.subspan(pos, size));
return 1;