Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_thumbnail.cpp
Go to the documentation of this file.
1// Copyright 2019 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "public/fpdf_thumbnail.h"
6
7#include <utility>
8
9#include "core/fpdfapi/page/cpdf_dib.h"
10#include "core/fpdfapi/page/cpdf_page.h"
11#include "core/fpdfapi/parser/cpdf_dictionary.h"
12#include "core/fpdfapi/parser/cpdf_stream.h"
13#include "core/fpdfapi/parser/cpdf_stream_acc.h"
14#include "core/fxcrt/compiler_specific.h"
15#include "core/fxcrt/span.h"
16#include "core/fxge/dib/cfx_dibitmap.h"
17#include "fpdfsdk/cpdfsdk_helpers.h"
18#include "public/fpdfview.h"
19
20namespace {
21
22RetainPtr<const CPDF_Stream> CPDFStreamForThumbnailFromPage(FPDF_PAGE page) {
23 const CPDF_Page* pdf_page = CPDFPageFromFPDFPage(page);
24 if (!pdf_page)
25 return nullptr;
26
27 RetainPtr<const CPDF_Dictionary> page_dict = pdf_page->GetDict();
28 if (!page_dict->KeyExist("Type"))
29 return nullptr;
30
31 return page_dict->GetStreamFor("Thumb");
32}
33
34} // namespace
35
36FPDF_EXPORT unsigned long FPDF_CALLCONV
38 void* buffer,
39 unsigned long buflen) {
40 RetainPtr<const CPDF_Stream> thumb_stream =
41 CPDFStreamForThumbnailFromPage(page);
42 if (!thumb_stream)
43 return 0u;
44
45 // SAFETY: caller ensures `buffer` points to at least `buflen` bytes.
46 return DecodeStreamMaybeCopyAndReturnLength(
47 std::move(thumb_stream),
48 UNSAFE_BUFFERS(pdfium::make_span(static_cast<uint8_t*>(buffer),
49 static_cast<size_t>(buflen))));
50}
51
52FPDF_EXPORT unsigned long FPDF_CALLCONV
54 void* buffer,
55 unsigned long buflen) {
56 RetainPtr<const CPDF_Stream> thumb_stream =
57 CPDFStreamForThumbnailFromPage(page);
58 if (!thumb_stream)
59 return 0u;
60
61 // SAFETY: caller ensures `buffer` points to at least `buflen` bytes.
62 return GetRawStreamMaybeCopyAndReturnLength(
63 std::move(thumb_stream),
64 UNSAFE_BUFFERS(pdfium::make_span(static_cast<uint8_t*>(buffer),
65 static_cast<size_t>(buflen))));
66}
67
68FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV
70 RetainPtr<const CPDF_Stream> thumb_stream =
71 CPDFStreamForThumbnailFromPage(page);
72 if (!thumb_stream)
73 return nullptr;
74
75 const CPDF_Page* pdf_page = CPDFPageFromFPDFPage(page);
76 auto dib_source = pdfium::MakeRetain<CPDF_DIB>(pdf_page->GetDocument(),
77 std::move(thumb_stream));
78 const CPDF_DIB::LoadState start_status = dib_source->StartLoadDIBBase(
79 false, nullptr, pdf_page->GetPageResources().Get(), false,
81 if (start_status == CPDF_DIB::LoadState::kFail)
82 return nullptr;
83
84 auto thumb_bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
85 if (!thumb_bitmap->Copy(std::move(dib_source))) {
86 return nullptr;
87 }
88
89 CHECK(!thumb_bitmap->IsPremultiplied());
90 return FPDFBitmapFromCFXDIBitmap(thumb_bitmap.Leak());
91}
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
CPDF_Document * GetDocument() const override
Definition cpdf_page.cpp:51
#define UNSAFE_BUFFERS(...)
CPDF_Page * CPDFPageFromFPDFPage(FPDF_PAGE page)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFPage_GetRawThumbnailData(FPDF_PAGE page, void *buffer, unsigned long buflen)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFPage_GetDecodedThumbnailData(FPDF_PAGE page, void *buffer, unsigned long buflen)
FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFPage_GetThumbnailAsBitmap(FPDF_PAGE page)
#define FPDF_CALLCONV
Definition fpdfview.h:229
#define FPDF_EXPORT
Definition fpdfview.h:223
#define CHECK(cvref)