KItinerary

pdfimage.h
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
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 
16 class QImage;
17 class QTransform;
18 
19 namespace KItinerary {
20 class PdfImagePrivate;
21 class PdfImageRef;
22 }
23 
24 namespace std {
25 template<>
26 struct hash<KItinerary::PdfImageRef>
27 {
28  inline std::size_t operator()(const KItinerary::PdfImageRef &ref) const noexcept;
29 };
30 }
31 
32 namespace KItinerary {
33 
34 /** PDF image element type. */
35 enum 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 {
44 public:
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 
62 private:
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  */
72 class KITINERARY_EXPORT PdfImage
73 {
74  Q_GADGET
75  Q_PROPERTY(int width READ width)
76  Q_PROPERTY(int height READ height)
77 public:
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. */
99  enum LoadingHint {
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 with 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 private:
132  friend class PdfExtractorOutputDevice;
133  friend class PdfPagePrivate;
134  friend class PdfPage;
136 };
137 
138 Q_DECLARE_OPERATORS_FOR_FLAGS(PdfImage::LoadingHints)
139 
140 }
141 
142 std::size_t std::hash<KItinerary::PdfImageRef>::operator()(const KItinerary::PdfImageRef &ref) const noexcept
143 {
144  // first part is how poppler hashes its Ref type
145  return std::hash<int>{}(ref.m_refNum) ^ (std::hash<int>{}(ref.m_refGen) << 1) ^ std::hash<int>{}((int)ref.m_type);
146 }
void ref()
An image in a PDF document.
Definition: pdfimage.h:72
LoadingHint
Hints for loading image data.
Definition: pdfimage.h:99
PDF object reference for an image, with the ability to address attached masks as well.
Definition: pdfimage.h:42
PdfImageType
PDF image element type.
Definition: pdfimage.h:35
A page in a PDF document.
Definition: pdfdocument.h:28
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 03:58:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.