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 constexpr inline bool isNull() const
53 {
54 return m_refNum < 0;
55 }
56
57 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 PdfImagePrivate;
67 friend std::size_t std::hash<PdfImageRef>::operator ()(const PdfImageRef&) const noexcept;
68};
69
70/** An image in a PDF document.
71 */
72class KITINERARY_EXPORT PdfImage
73{
74 Q_GADGET
75 Q_PROPERTY(int width READ width)
76 Q_PROPERTY(int height READ height)
77public:
78 PdfImage();
79 PdfImage(const PdfImage&);
80 ~PdfImage();
81 PdfImage& operator=(const PdfImage&);
82
83 /** Width of the image in PDF 1/72 dpi coordinates. */
84 int width() const;
85 /** Height of the image in PDF 1/72 dpi coordinates. */
86 int height() const;
87
88 /** Height of the source image. */
89 int sourceHeight() const;
90 /** Width of the source image. */
91 int sourceWidth() const;
92
93 /** Transformation from source image to final size/position on the page.
94 * Values are 1/72 inch.
95 */
96 QTransform transform() const;
97
98 /** Hints for loading image data. */
100 NoHint = 0, ///< Load image data as-is. The default.
101 AbortOnColorHint = 1, ///< Abort loading when encountering a non black/white pixel, as a shortcut for barcode detection.
102 ConvertToGrayscaleHint = 2, ///< Convert to QImage::Format_Grayscale8 during loading. More efficient than converting later if all you need is grayscale.
103 };
104 Q_DECLARE_FLAGS(LoadingHints, LoadingHint)
105
106 /** Sets image loading hints. */
107 void setLoadingHints(LoadingHints hints);
108
109 /** The source image without display transformations applied. */
110 QImage image() const;
111
112 /** Returns whether this image has an object id.
113 * Vector graphic "images" don't have that.
114 */
115 bool hasObjectId() const;
116
117 /** PDF-internal unique identifier of this image.
118 * Use this to detect multiple occurrences of the same image in different
119 * places, if that reduces e.g. computation cost.
120 */
121 PdfImageRef objectId() const;
122
123 /** Returns whether this is a raster or vector image. */
124 bool isVectorImage() const;
125
126 /** If this is a vector image, this returns the number
127 * of vector path elemets.
128 */
129 int pathElementsCount() const;
130
131 /** Returns @c true if this image has an aspect-ratio changing transform.
132 * That might need to be applied before doing barcode decoding for example.
133 */
134 [[nodiscard]] bool hasAspectRatioTransform() const;
135
136 /** Applies the aspect ratio changing part of the transform
137 * to the given image (which typically should be the one returned
138 * by image()).
139 */
140 [[nodiscard]] QImage applyAspectRatioTransform(const QImage &image) const;
141
142private:
143 friend class PdfExtractorOutputDevice;
144 friend class PdfPagePrivate;
145 friend class PdfPage;
147};
148
149Q_DECLARE_OPERATORS_FOR_FLAGS(PdfImage::LoadingHints)
150
151}
152
153std::size_t std::hash<KItinerary::PdfImageRef>::operator()(const KItinerary::PdfImageRef &ref) const noexcept
154{
155 // first part is how poppler hashes its Ref type
156 return std::hash<int>{}(ref.m_refNum) ^ (std::hash<int>{}(ref.m_refGen) << 1) ^ std::hash<int>{}((int)ref.m_type);
157}
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:73
LoadingHint
Hints for loading image data.
Definition pdfimage.h:99
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 Tue Mar 26 2024 11:14:49 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.