1 |
|
2 |
|
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 |
|
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 |
|
33 | using vips::VImage;
|
34 |
|
35 | namespace sharp {
|
36 |
|
37 | struct InputDescriptor {
|
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 |
|
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 |
|
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 |
|
165 | extern std::atomic<int> counterQueue;
|
166 |
|
167 |
|
168 | extern std::atomic<int> counterProcess;
|
169 |
|
170 |
|
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 |
|
187 |
|
188 | std::string TrimEnd(std::string const &str);
|
189 |
|
190 | |
191 |
|
192 |
|
193 | std::string ImageTypeId(ImageType const imageType);
|
194 |
|
195 | |
196 |
|
197 |
|
198 | ImageType DetermineImageType(void *buffer, size_t const length);
|
199 |
|
200 | |
201 |
|
202 |
|
203 | ImageType DetermineImageType(char const *file);
|
204 |
|
205 | |
206 |
|
207 |
|
208 | bool ImageTypeSupportsPage(ImageType imageType);
|
209 |
|
210 | |
211 |
|
212 |
|
213 | bool ImageTypeSupportsUnlimited(ImageType imageType);
|
214 |
|
215 | |
216 |
|
217 |
|
218 | std::tuple<VImage, ImageType> OpenInput(InputDescriptor *descriptor);
|
219 |
|
220 | |
221 |
|
222 |
|
223 | bool HasProfile(VImage image);
|
224 |
|
225 | |
226 |
|
227 |
|
228 | std::pair<char*, size_t> GetProfile(VImage image);
|
229 |
|
230 | |
231 |
|
232 |
|
233 | VImage SetProfile(VImage image, std::pair<char*, size_t> icc);
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 | bool HasAlpha(VImage image);
|
240 |
|
241 | |
242 |
|
243 |
|
244 | VImage RemoveExif(VImage image);
|
245 |
|
246 | |
247 |
|
248 |
|
249 | int ExifOrientation(VImage image);
|
250 |
|
251 | |
252 |
|
253 |
|
254 | VImage SetExifOrientation(VImage image, int const orientation);
|
255 |
|
256 | |
257 |
|
258 |
|
259 | VImage RemoveExifOrientation(VImage image);
|
260 |
|
261 | |
262 |
|
263 |
|
264 | VImage SetAnimationProperties(VImage image, int nPages, int pageHeight, std::vector<int> delay, int loop);
|
265 |
|
266 | |
267 |
|
268 |
|
269 | VImage RemoveAnimationProperties(VImage image);
|
270 |
|
271 | |
272 |
|
273 |
|
274 | VImage RemoveGifPalette(VImage image);
|
275 |
|
276 | |
277 |
|
278 |
|
279 | bool HasDensity(VImage image);
|
280 |
|
281 | |
282 |
|
283 |
|
284 | int GetDensity(VImage image);
|
285 |
|
286 | |
287 |
|
288 |
|
289 | VImage SetDensity(VImage image, const double density);
|
290 |
|
291 | |
292 |
|
293 |
|
294 |
|
295 | int GetPageHeight(VImage image);
|
296 |
|
297 | |
298 |
|
299 |
|
300 | void AssertImageTypeDimensions(VImage image, ImageType const imageType);
|
301 |
|
302 | |
303 |
|
304 |
|
305 | extern std::function<void(void*, char*)> FreeCallback;
|
306 |
|
307 | |
308 |
|
309 |
|
310 | void VipsWarningCallback(char const* log_domain, GLogLevelFlags log_level, char const* message, void* ignore);
|
311 |
|
312 | |
313 |
|
314 |
|
315 | std::string VipsWarningPop();
|
316 |
|
317 | |
318 |
|
319 |
|
320 | void SetTimeout(VImage image, int const timeoutSeconds);
|
321 |
|
322 | |
323 |
|
324 |
|
325 | void VipsProgressCallBack(VipsImage *image, VipsProgress *progress, int *timeoutSeconds);
|
326 |
|
327 | |
328 |
|
329 |
|
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 |
|
336 |
|
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 |
|
343 |
|
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 |
|
350 |
|
351 | bool Is16Bit(VipsInterpretation const interpretation);
|
352 |
|
353 | |
354 |
|
355 |
|
356 |
|
357 | double MaximumImageAlpha(VipsInterpretation const interpretation);
|
358 |
|
359 | |
360 |
|
361 |
|
362 | std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba,
|
363 | VipsInterpretation const interpretation, bool premultiply);
|
364 |
|
365 | |
366 |
|
367 |
|
368 | std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour, bool premultiply);
|
369 |
|
370 | |
371 |
|
372 |
|
373 | VImage RemoveAlpha(VImage image);
|
374 |
|
375 | |
376 |
|
377 |
|
378 | VImage EnsureAlpha(VImage image, double const value);
|
379 |
|
380 | |
381 |
|
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 |
|
388 |
|
389 | VImage StaySequential(VImage image, bool condition = true);
|
390 |
|
391 | }
|
392 |
|
393 | #endif
|