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