UNPKG

10.4 kBtext/x-cView Raw
1// Copyright 2013 Lovell Fuller and others.
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef SRC_COMMON_H_
5#define SRC_COMMON_H_
6
7#include <string>
8#include <tuple>
9#include <vector>
10
11#include <napi.h>
12#include <vips/vips8>
13
14// Verify platform and compiler compatibility
15
16#if (VIPS_MAJOR_VERSION < 8) || \
17 (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 14) || \
18 (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 14 && VIPS_MICRO_VERSION < 2)
19#error "libvips version 8.14.2+ is required - please see https://sharp.pixelplumbing.com/install"
20#endif
21
22#if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6)))
23#error "GCC version 4.6+ is required for C++11 features - please see https://sharp.pixelplumbing.com/install"
24#endif
25
26#if (defined(__clang__) && defined(__has_feature))
27#if (!__has_feature(cxx_range_for))
28#error "clang version 3.0+ is required for C++11 features - please see https://sharp.pixelplumbing.com/install"
29#endif
30#endif
31
32using vips::VImage;
33
34namespace sharp {
35
36 struct InputDescriptor { // NOLINT(runtime/indentation_namespace)
37 std::string name;
38 std::string file;
39 char *buffer;
40 VipsFailOn failOn;
41 uint64_t limitInputPixels;
42 bool unlimited;
43 VipsAccess access;
44 size_t bufferLength;
45 bool isBuffer;
46 double density;
47 bool ignoreIcc;
48 VipsBandFormat rawDepth;
49 int rawChannels;
50 int rawWidth;
51 int rawHeight;
52 bool rawPremultiplied;
53 int pages;
54 int page;
55 int level;
56 int subifd;
57 int createChannels;
58 int createWidth;
59 int createHeight;
60 std::vector<double> createBackground;
61 std::string createNoiseType;
62 double createNoiseMean;
63 double createNoiseSigma;
64 std::string textValue;
65 std::string textFont;
66 std::string textFontfile;
67 int textWidth;
68 int textHeight;
69 VipsAlign textAlign;
70 bool textJustify;
71 int textDpi;
72 bool textRgba;
73 int textSpacing;
74 VipsTextWrap textWrap;
75 int textAutofitDpi;
76
77 InputDescriptor():
78 buffer(nullptr),
79 failOn(VIPS_FAIL_ON_WARNING),
80 limitInputPixels(0x3FFF * 0x3FFF),
81 unlimited(FALSE),
82 access(VIPS_ACCESS_RANDOM),
83 bufferLength(0),
84 isBuffer(FALSE),
85 density(72.0),
86 ignoreIcc(FALSE),
87 rawDepth(VIPS_FORMAT_UCHAR),
88 rawChannels(0),
89 rawWidth(0),
90 rawHeight(0),
91 rawPremultiplied(false),
92 pages(1),
93 page(0),
94 level(0),
95 subifd(-1),
96 createChannels(0),
97 createWidth(0),
98 createHeight(0),
99 createBackground{ 0.0, 0.0, 0.0, 255.0 },
100 createNoiseMean(0.0),
101 createNoiseSigma(0.0),
102 textWidth(0),
103 textHeight(0),
104 textAlign(VIPS_ALIGN_LOW),
105 textJustify(FALSE),
106 textDpi(72),
107 textRgba(FALSE),
108 textSpacing(0),
109 textWrap(VIPS_TEXT_WRAP_WORD),
110 textAutofitDpi(0) {}
111 };
112
113 // Convenience methods to access the attributes of a Napi::Object
114 bool HasAttr(Napi::Object obj, std::string attr);
115 std::string AttrAsStr(Napi::Object obj, std::string attr);
116 std::string AttrAsStr(Napi::Object obj, unsigned int const attr);
117 uint32_t AttrAsUint32(Napi::Object obj, std::string attr);
118 int32_t AttrAsInt32(Napi::Object obj, std::string attr);
119 int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr);
120 double AttrAsDouble(Napi::Object obj, std::string attr);
121 double AttrAsDouble(Napi::Object obj, unsigned int const attr);
122 bool AttrAsBool(Napi::Object obj, std::string attr);
123 std::vector<double> AttrAsVectorOfDouble(Napi::Object obj, std::string attr);
124 std::vector<int32_t> AttrAsInt32Vector(Napi::Object obj, std::string attr);
125 template <class T> T AttrAsEnum(Napi::Object obj, std::string attr, GType type) {
126 return static_cast<T>(
127 vips_enum_from_nick(nullptr, type, AttrAsStr(obj, attr).data()));
128 }
129
130 // Create an InputDescriptor instance from a Napi::Object describing an input image
131 InputDescriptor* CreateInputDescriptor(Napi::Object input);
132
133 enum class ImageType {
134 JPEG,
135 PNG,
136 WEBP,
137 JP2,
138 TIFF,
139 GIF,
140 SVG,
141 HEIF,
142 PDF,
143 MAGICK,
144 OPENSLIDE,
145 PPM,
146 FITS,
147 EXR,
148 JXL,
149 VIPS,
150 RAW,
151 UNKNOWN,
152 MISSING
153 };
154
155 enum class Canvas {
156 CROP,
157 EMBED,
158 MAX,
159 MIN,
160 IGNORE_ASPECT
161 };
162
163 // How many tasks are in the queue?
164 extern volatile int counterQueue;
165
166 // How many tasks are being processed?
167 extern volatile int counterProcess;
168
169 // Filename extension checkers
170 bool IsJpeg(std::string const &str);
171 bool IsPng(std::string const &str);
172 bool IsWebp(std::string const &str);
173 bool IsJp2(std::string const &str);
174 bool IsGif(std::string const &str);
175 bool IsTiff(std::string const &str);
176 bool IsHeic(std::string const &str);
177 bool IsHeif(std::string const &str);
178 bool IsAvif(std::string const &str);
179 bool IsJxl(std::string const &str);
180 bool IsDz(std::string const &str);
181 bool IsDzZip(std::string const &str);
182 bool IsV(std::string const &str);
183
184 /*
185 Trim space from end of string.
186 */
187 std::string TrimEnd(std::string const &str);
188
189 /*
190 Provide a string identifier for the given image type.
191 */
192 std::string ImageTypeId(ImageType const imageType);
193
194 /*
195 Determine image format of a buffer.
196 */
197 ImageType DetermineImageType(void *buffer, size_t const length);
198
199 /*
200 Determine image format of a file.
201 */
202 ImageType DetermineImageType(char const *file);
203
204 /*
205 Does this image type support multiple pages?
206 */
207 bool ImageTypeSupportsPage(ImageType imageType);
208
209 /*
210 Does this image type support removal of safety limits?
211 */
212 bool ImageTypeSupportsUnlimited(ImageType imageType);
213
214 /*
215 Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
216 */
217 std::tuple<VImage, ImageType> OpenInput(InputDescriptor *descriptor);
218
219 /*
220 Does this image have an embedded profile?
221 */
222 bool HasProfile(VImage image);
223
224 /*
225 Does this image have an alpha channel?
226 Uses colour space interpretation with number of channels to guess this.
227 */
228 bool HasAlpha(VImage image);
229
230 /*
231 Get EXIF Orientation of image, if any.
232 */
233 int ExifOrientation(VImage image);
234
235 /*
236 Set EXIF Orientation of image.
237 */
238 VImage SetExifOrientation(VImage image, int const orientation);
239
240 /*
241 Remove EXIF Orientation from image.
242 */
243 VImage RemoveExifOrientation(VImage image);
244
245 /*
246 Set animation properties if necessary.
247 */
248 VImage SetAnimationProperties(VImage image, int nPages, int pageHeight, std::vector<int> delay, int loop);
249
250 /*
251 Remove animation properties from image.
252 */
253 VImage RemoveAnimationProperties(VImage image);
254
255 /*
256 Remove GIF palette from image.
257 */
258 VImage RemoveGifPalette(VImage image);
259
260 /*
261 Does this image have a non-default density?
262 */
263 bool HasDensity(VImage image);
264
265 /*
266 Get pixels/mm resolution as pixels/inch density.
267 */
268 int GetDensity(VImage image);
269
270 /*
271 Set pixels/mm resolution based on a pixels/inch density.
272 */
273 VImage SetDensity(VImage image, const double density);
274
275 /*
276 Multi-page images can have a page height. Fetch it, and sanity check it.
277 If page-height is not set, it defaults to the image height
278 */
279 int GetPageHeight(VImage image);
280
281 /*
282 Check the proposed format supports the current dimensions.
283 */
284 void AssertImageTypeDimensions(VImage image, ImageType const imageType);
285
286 /*
287 Called when a Buffer undergoes GC, required to support mixed runtime libraries in Windows
288 */
289 extern std::function<void(void*, char*)> FreeCallback;
290
291 /*
292 Called with warnings from the glib-registered "VIPS" domain
293 */
294 void VipsWarningCallback(char const* log_domain, GLogLevelFlags log_level, char const* message, void* ignore);
295
296 /*
297 Pop the oldest warning message from the queue
298 */
299 std::string VipsWarningPop();
300
301 /*
302 Attach an event listener for progress updates, used to detect timeout
303 */
304 void SetTimeout(VImage image, int const timeoutSeconds);
305
306 /*
307 Event listener for progress updates, used to detect timeout
308 */
309 void VipsProgressCallBack(VipsImage *image, VipsProgress *progress, int *timeoutSeconds);
310
311 /*
312 Calculate the (left, top) coordinates of the output image
313 within the input image, applying the given gravity during an embed.
314 */
315 std::tuple<int, int> CalculateEmbedPosition(int const inWidth, int const inHeight,
316 int const outWidth, int const outHeight, int const gravity);
317
318 /*
319 Calculate the (left, top) coordinates of the output image
320 within the input image, applying the given gravity.
321 */
322 std::tuple<int, int> CalculateCrop(int const inWidth, int const inHeight,
323 int const outWidth, int const outHeight, int const gravity);
324
325 /*
326 Calculate the (left, top) coordinates of the output image
327 within the input image, applying the given x and y offsets of the output image.
328 */
329 std::tuple<int, int> CalculateCrop(int const inWidth, int const inHeight,
330 int const outWidth, int const outHeight, int const x, int const y);
331
332 /*
333 Are pixel values in this image 16-bit integer?
334 */
335 bool Is16Bit(VipsInterpretation const interpretation);
336
337 /*
338 Return the image alpha maximum. Useful for combining alpha bands. scRGB
339 images are 0 - 1 for image data, but the alpha is 0 - 255.
340 */
341 double MaximumImageAlpha(VipsInterpretation const interpretation);
342
343 /*
344 Convert RGBA value to another colourspace
345 */
346 std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba,
347 VipsInterpretation const interpretation, bool premultiply);
348
349 /*
350 Apply the alpha channel to a given colour
351 */
352 std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour, bool premultiply);
353
354 /*
355 Removes alpha channel, if any.
356 */
357 VImage RemoveAlpha(VImage image);
358
359 /*
360 Ensures alpha channel, if missing.
361 */
362 VImage EnsureAlpha(VImage image, double const value);
363
364 /*
365 Calculate the shrink factor, taking into account auto-rotate, the canvas
366 mode, and so on. The hshrink/vshrink are the amount to shrink the input
367 image axes by in order for the output axes (ie. after rotation) to match
368 the required thumbnail width/height and canvas mode.
369 */
370 std::pair<double, double> ResolveShrink(int width, int height, int targetWidth, int targetHeight,
371 Canvas canvas, bool swap, bool withoutEnlargement, bool withoutReduction);
372
373} // namespace sharp
374
375#endif // SRC_COMMON_H_