KItinerary

pdfimage.h
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kitinerary_export.h"
10
11#include <QExplicitlySharedDataPointer>
12#include <QMetaType>
13
14#include <functional>
15
16class QImage;
17class QTransform;
18
19namespace KItinerary {
20class PdfImagePrivate;
21class PdfImageRef;
22}
23
24namespace std {
25template<>
26struct hash<KItinerary::PdfImageRef>
27{
28 inline std::size_t operator()(const KItinerary::PdfImageRef &ref) const noexcept;
29};
30}
31
32namespace KItinerary {
33
34/** PDF image element type. */
35enum class PdfImageType {
36 Image,
37 Mask,
38 SMask
39};
40
41/** PDF object reference for an image, with the ability to address attached masks as well. */
43{
44public:
45 constexpr PdfImageRef() = default;
46 constexpr inline PdfImageRef(int num, int gen, PdfImageType type = PdfImageType::Image)
47 : m_refNum(num)
48 , m_refGen(gen)
49 , m_type(type)
50 {}
51
52 [[nodiscard]] constexpr inline bool isNull() const
53 {
54 return m_refNum < 0;
55 }
56
57 [[nodiscard]] constexpr inline bool operator==(const PdfImageRef &other) const
58 {
59 return m_refNum == other.m_refNum && m_refGen == other.m_refGen && m_type == other.m_type;
60 }
61
62private:
63 int m_refNum = -1;
64 int m_refGen = -1;
65 PdfImageType m_type = PdfImageType::Image;
66 friend class PdfImage;
67 friend class PdfImagePrivate;
68 friend std::size_t std::hash<PdfImageRef>::operator ()(const PdfImageRef&) const noexcept;
69};
70
71/** An image in a PDF document.
72 */
73class KITINERARY_EXPORT PdfImage
74{
75 Q_GADGET
76 Q_PROPERTY(int width READ width)
77 Q_PROPERTY(int height READ height)
78public:
79 PdfImage();
80 PdfImage(const PdfImage&);
81 ~PdfImage();
82 PdfImage& operator=(const PdfImage&);
83
84 /** Width of the image in PDF 1/72 dpi coordinates. */
85 [[nodiscard]] int width() const;
86 /** Height of the image in PDF 1/72 dpi coordinates. */
87 [[nodiscard]] int height() const;
88
89 /** Height of the source image. */
90 [[nodiscard]] int sourceHeight() const;
91 /** Width of the source image. */
92 [[nodiscard]] int sourceWidth() const;
93
94 /** Transformation from source image to final size/position on the page.
95 * Values are 1/72 inch.
96 */
97 [[nodiscard]] QTransform transform() const;
98
99 /** Hints for loading image data. */
101 NoHint = 0, ///< Load image data as-is. The default.
102 AbortOnColorHint = 1, ///< Abort loading when encountering a non black/white pixel, as a shortcut for barcode detection.
103 ConvertToGrayscaleHint = 2, ///< Convert to QImage::Format_Grayscale8 during loading. More efficient than converting later if all you need is grayscale.
104 };
105 Q_DECLARE_FLAGS(LoadingHints, LoadingHint)
106
107 /** Sets image loading hints. */
108 void setLoadingHints(LoadingHints hints);
109
110 /** The source image without display transformations applied. */
111 [[nodiscard]] QImage image() const;
112
113 /** Returns whether this image has an object id.
114 * Vector graphic "images" don't have that.
115 */
116 [[nodiscard]] bool hasObjectId() const;
117
118 /** PDF-internal unique identifier of this image.
119 * Use this to detect multiple occurrences of the same image in different
120 * places, if that reduces e.g. computation cost.
121 */
122 [[nodiscard]] PdfImageRef objectId() const;
123
124 /** Returns whether this is a raster or vector image. */
125 [[nodiscard]] bool isVectorImage() const;
126
127 /** If this is a vector image, this returns the number
128 * of vector path elemets.
129 */
130 [[nodiscard]] int pathElementsCount() const;
131
132 /** Returns @c true if this image has an aspect-ratio changing transform.
133 * That might need to be applied before doing barcode decoding for example.
134 */
135 [[nodiscard]] bool hasAspectRatioTransform() const;
136
137 /** Applies the aspect ratio changing part of the transform
138 * to the given image (which typically should be the one returned
139 * by image()).
140 */
141 [[nodiscard]] QImage applyAspectRatioTransform(const QImage &image) const;
142
143 /** PDF image type. */
144 [[nodiscard]] PdfImageType type() const;
145
146private:
147 friend class PdfExtractorOutputDevice;
148 friend class PdfPagePrivate;
149 friend class PdfPage;
151};
152
153Q_DECLARE_OPERATORS_FOR_FLAGS(PdfImage::LoadingHints)
154
155}
156
157std::size_t std::hash<KItinerary::PdfImageRef>::operator()(const KItinerary::PdfImageRef &ref) const noexcept
158{
159 // first part is how poppler hashes its Ref type
160 return std::hash<int>{}(ref.m_refNum) ^ (std::hash<int>{}(ref.m_refGen) << 1) ^ std::hash<int>{}((int)ref.m_type);
161}
PDF object reference for an image, with the ability to address attached masks as well.
Definition pdfimage.h:43
An image in a PDF document.
Definition pdfimage.h:74
LoadingHint
Hints for loading image data.
Definition pdfimage.h:100
A page in a PDF document.
Definition pdfdocument.h:29
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
PdfImageType
PDF image element type.
Definition pdfimage.h:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.