UNPKG

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