vips.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. #include "vips.h"
  2. #include <string.h>
  3. #define VIPS_SCRGB_ALPHA_FIXED \
  4. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 15))
  5. #define VIPS_META_PALETTE_BITS_DEPTH "palette-bit-depth"
  6. #define IMGPROXY_META_ICC_NAME "imgproxy-icc-profile"
  7. int
  8. vips_initialize()
  9. {
  10. return vips_init("imgproxy");
  11. }
  12. void
  13. unref_image(VipsImage *in)
  14. {
  15. VIPS_UNREF(in);
  16. }
  17. void
  18. g_free_go(void **buf)
  19. {
  20. g_free(*buf);
  21. }
  22. int
  23. gif_resolution_limit()
  24. {
  25. return INT_MAX / 4;
  26. }
  27. // Just create and destroy a tiny image to ensure vips is operational
  28. int
  29. vips_health()
  30. {
  31. VipsImage *base = vips_image_new();
  32. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  33. int res = vips_black(&t[0], 4, 4, "bands", 4, NULL) ||
  34. !(t[1] = vips_image_copy_memory(t[0]));
  35. VIPS_UNREF(base);
  36. return res;
  37. }
  38. int
  39. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out)
  40. {
  41. if (shrink > 1)
  42. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink,
  43. NULL);
  44. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  45. }
  46. int
  47. vips_jxlload_go(void *buf, size_t len, int pages, VipsImage **out)
  48. {
  49. return vips_jxlload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  50. }
  51. int
  52. vips_pngload_go(void *buf, size_t len, VipsImage **out, int unlimited)
  53. {
  54. return vips_pngload_buffer(
  55. buf, len, out,
  56. "access", VIPS_ACCESS_SEQUENTIAL,
  57. "unlimited", unlimited,
  58. NULL);
  59. }
  60. int
  61. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out)
  62. {
  63. return vips_webpload_buffer(
  64. buf, len, out,
  65. "access", VIPS_ACCESS_SEQUENTIAL,
  66. "scale", scale,
  67. "n", pages,
  68. NULL);
  69. }
  70. int
  71. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out)
  72. {
  73. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  74. }
  75. int
  76. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out, int unlimited)
  77. {
  78. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  79. // for lower scale values
  80. double dpi = 72.0;
  81. if (scale < 0.001) {
  82. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  83. scale = 0.001;
  84. }
  85. return vips_svgload_buffer(
  86. buf, len, out,
  87. "access", VIPS_ACCESS_SEQUENTIAL,
  88. "scale", scale,
  89. "dpi", dpi,
  90. "unlimited", unlimited,
  91. NULL);
  92. }
  93. int
  94. vips_heifload_go(void *buf, size_t len, VipsImage **out, int thumbnail)
  95. {
  96. return vips_heifload_buffer(
  97. buf, len, out,
  98. "access", VIPS_ACCESS_SEQUENTIAL,
  99. "thumbnail", thumbnail,
  100. NULL);
  101. }
  102. int
  103. vips_tiffload_go(void *buf, size_t len, VipsImage **out)
  104. {
  105. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  106. }
  107. int
  108. vips_black_go(VipsImage **out, int width, int height, int bands)
  109. {
  110. VipsImage *tmp;
  111. int res = vips_black(&tmp, width, height, "bands", bands, NULL) ||
  112. vips_copy(tmp, out, "interpretation", VIPS_INTERPRETATION_sRGB, NULL);
  113. VIPS_UNREF(tmp);
  114. return res;
  115. }
  116. int
  117. vips_fix_scRGB_alpha_tiff(VipsImage *in, VipsImage **out)
  118. {
  119. #if VIPS_SCRGB_ALPHA_FIXED
  120. /* Vips 8.15+ uses 0.0-1.0 range for linear alpha, so we don't need a fix.
  121. */
  122. return vips_copy(in, out, NULL);
  123. #else
  124. /* Vips prior to 8.14 loads linear alpha in the 0.0-1.0 range but uses the 0.0-255.0 range.
  125. */
  126. VipsImage *base = vips_image_new();
  127. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  128. int res =
  129. vips_extract_band(in, &t[0], 0, "n", 3, NULL) ||
  130. vips_extract_band(in, &t[1], 3, "n", in->Bands - 3, NULL) ||
  131. vips_linear1(t[1], &t[2], 255.0, 0, NULL) ||
  132. vips_cast(t[2], &t[3], in->BandFmt, NULL) ||
  133. vips_bandjoin2(t[0], t[3], out, NULL);
  134. VIPS_UNREF(base);
  135. return res;
  136. #endif
  137. }
  138. /* Vips loads linear BW TIFFs as VIPS_INTERPRETATION_B_W or VIPS_INTERPRETATION_GREY16
  139. * but these colourspaces are not linear. We should properly convert them to
  140. * VIPS_INTERPRETATION_GREY16
  141. */
  142. int
  143. vips_fix_BW_float_tiff(VipsImage *in, VipsImage **out)
  144. {
  145. VipsImage *base = vips_image_new();
  146. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 8);
  147. VipsImage *color = in;
  148. VipsImage *alpha = NULL;
  149. /* Extract and fix alpha. Float WB TIFF uses the 0.0-1.0 range but we need
  150. * the 0.0-65535.0 range
  151. */
  152. if (in->Bands > 1) {
  153. if (
  154. vips_extract_band(in, &t[0], 0, NULL) ||
  155. vips_extract_band(in, &t[1], 1, "n", in->Bands - 1, NULL) ||
  156. vips_linear1(t[1], &t[2], 65535.0, 0, NULL) ||
  157. vips_cast_ushort(t[2], &t[3], NULL) ||
  158. vips_copy(t[3], &t[4], "interpretation", VIPS_INTERPRETATION_GREY16, NULL)) {
  159. VIPS_UNREF(base);
  160. return 1;
  161. }
  162. color = t[0];
  163. alpha = t[4];
  164. }
  165. /* Craft an scRGB image and convert it back to GREY16 to apply a gamma
  166. * correction
  167. */
  168. VipsImage *rgb[3] = { color, color, color };
  169. if (
  170. vips_bandjoin(rgb, &t[5], 3, NULL) ||
  171. vips_colourspace(t[5], &t[6], VIPS_INTERPRETATION_GREY16,
  172. "source_space", VIPS_INTERPRETATION_scRGB, NULL)) {
  173. VIPS_UNREF(base);
  174. return 1;
  175. }
  176. int res;
  177. if (alpha)
  178. res =
  179. vips_bandjoin2(t[6], alpha, &t[7], NULL) ||
  180. vips_icc_remove(t[7], out);
  181. else
  182. res = vips_icc_remove(t[6], out);
  183. VIPS_UNREF(base);
  184. return res;
  185. }
  186. int
  187. vips_fix_float_tiff(VipsImage *in, VipsImage **out)
  188. {
  189. /* Vips loads linear alpha in the 0.0-1.0 range but uses the 0.0-255.0 range.
  190. * https://github.com/libvips/libvips/pull/3627 fixes this behavior
  191. */
  192. if (in->Type == VIPS_INTERPRETATION_scRGB && in->Bands > 3)
  193. return vips_fix_scRGB_alpha_tiff(in, out);
  194. /* Vips loads linear BW TIFFs as VIPS_INTERPRETATION_B_W or VIPS_INTERPRETATION_GREY16
  195. * but these colourspaces are not linear. We should properly convert them to
  196. * VIPS_INTERPRETATION_GREY16
  197. */
  198. if (
  199. (in->Type == VIPS_INTERPRETATION_B_W || in->Type == VIPS_INTERPRETATION_GREY16) &&
  200. (in->BandFmt == VIPS_FORMAT_FLOAT || in->BandFmt == VIPS_FORMAT_DOUBLE))
  201. return vips_fix_BW_float_tiff(in, out);
  202. return vips_copy(in, out, NULL);
  203. }
  204. int
  205. vips_get_orientation(VipsImage *image)
  206. {
  207. int orientation;
  208. if (
  209. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  210. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0)
  211. return orientation;
  212. return 1;
  213. }
  214. int
  215. vips_get_palette_bit_depth(VipsImage *image)
  216. {
  217. int palette, palette_bit_depth;
  218. #ifdef VIPS_META_PALETTE
  219. if (vips_image_get_typeof(image, VIPS_META_PALETTE) == G_TYPE_INT &&
  220. vips_image_get_int(image, VIPS_META_PALETTE, &palette) == 0 &&
  221. palette) {
  222. if (vips_image_get_typeof(image, VIPS_META_BITS_PER_SAMPLE) == G_TYPE_INT &&
  223. vips_image_get_int(image, VIPS_META_BITS_PER_SAMPLE, &palette_bit_depth) == 0)
  224. return palette_bit_depth;
  225. else
  226. /* Image has palette but VIPS_META_BITS_PER_SAMPLE is not set.
  227. * It's very unlikely but we should handle this
  228. */
  229. return 8;
  230. }
  231. #else
  232. if (vips_image_get_typeof(image, VIPS_META_PALETTE_BITS_DEPTH) == G_TYPE_INT &&
  233. vips_image_get_int(image, VIPS_META_PALETTE_BITS_DEPTH, &palette_bit_depth) == 0)
  234. return palette_bit_depth;
  235. #endif
  236. return 0;
  237. }
  238. VipsBandFormat
  239. vips_band_format(VipsImage *in)
  240. {
  241. return in->BandFmt;
  242. }
  243. gboolean
  244. vips_is_animated(VipsImage *in)
  245. {
  246. int n_pages;
  247. return (vips_image_get_typeof(in, "delay") != G_TYPE_INVALID &&
  248. vips_image_get_typeof(in, "loop") != G_TYPE_INVALID &&
  249. vips_image_get_typeof(in, "page-height") == G_TYPE_INT &&
  250. vips_image_get_typeof(in, "n-pages") == G_TYPE_INT &&
  251. vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
  252. n_pages > 1);
  253. }
  254. int
  255. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n)
  256. {
  257. return vips_image_get_array_int(image, name, out, n);
  258. }
  259. void
  260. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n)
  261. {
  262. vips_image_set_array_int(image, name, array, n);
  263. }
  264. int
  265. vips_addalpha_go(VipsImage *in, VipsImage **out)
  266. {
  267. return vips_addalpha(in, out, NULL);
  268. }
  269. int
  270. vips_copy_go(VipsImage *in, VipsImage **out)
  271. {
  272. return vips_copy(in, out, NULL);
  273. }
  274. int
  275. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format)
  276. {
  277. return vips_cast(in, out, format, NULL);
  278. }
  279. int
  280. vips_rad2float_go(VipsImage *in, VipsImage **out)
  281. {
  282. return vips_rad2float(in, out, NULL);
  283. }
  284. int
  285. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale)
  286. {
  287. if (!vips_image_hasalpha(in))
  288. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  289. VipsBandFormat format = vips_band_format(in);
  290. VipsImage *base = vips_image_new();
  291. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  292. int res =
  293. vips_premultiply(in, &t[0], NULL) ||
  294. vips_cast(t[0], &t[1], format, NULL) ||
  295. vips_resize(t[1], &t[2], wscale, "vscale", hscale, NULL) ||
  296. vips_unpremultiply(t[2], &t[3], NULL) ||
  297. vips_cast(t[3], out, format, NULL);
  298. VIPS_UNREF(base);
  299. return res;
  300. }
  301. /* We don't really need to return the size since we check if the buffer is at least
  302. * the size of ICC header, and all we need is a header
  303. */
  304. static const void *
  305. vips_icc_get_header(VipsImage *in)
  306. {
  307. const void *data = NULL;
  308. size_t data_len = 0;
  309. if (!vips_image_get_typeof(in, VIPS_META_ICC_NAME) ||
  310. vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  311. return NULL;
  312. /* Less than header size
  313. */
  314. if (!data || data_len < 128)
  315. return NULL;
  316. return data;
  317. }
  318. int
  319. vips_icc_is_srgb_iec61966(VipsImage *in)
  320. {
  321. // 1998-12-01
  322. static char date[] = { 7, 206, 0, 2, 0, 9 };
  323. // 2.1
  324. static char version[] = { 2, 16, 0, 0 };
  325. const void *data = vips_icc_get_header(in);
  326. if (!data)
  327. return FALSE;
  328. /* Predict it is sRGB IEC61966 2.1 by checking some header fields
  329. */
  330. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  331. (memcmp(data + 16, "RGB ", 4) == 0) && // Colorspace
  332. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  333. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  334. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  335. (memcmp(data + 8, version, 4) == 0)); // Version
  336. }
  337. static VipsPCS
  338. vips_icc_get_pcs(VipsImage *in)
  339. {
  340. const void *data = vips_icc_get_header(in);
  341. if (!data)
  342. return VIPS_PCS_LAB;
  343. if (memcmp(data + 20, "XYZ ", 4) == 0)
  344. return VIPS_PCS_XYZ;
  345. return VIPS_PCS_LAB;
  346. }
  347. int
  348. vips_has_embedded_icc(VipsImage *in)
  349. {
  350. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  351. }
  352. int
  353. vips_icc_backup(VipsImage *in, VipsImage **out)
  354. {
  355. if (vips_copy(in, out, NULL))
  356. return 1;
  357. if (!vips_image_get_typeof(in, VIPS_META_ICC_NAME))
  358. return 0;
  359. const void *data = NULL;
  360. size_t data_len = 0;
  361. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  362. return 0;
  363. if (!data || data_len < 128)
  364. return 0;
  365. vips_image_remove(*out, IMGPROXY_META_ICC_NAME);
  366. vips_image_set_blob_copy(*out, IMGPROXY_META_ICC_NAME, data, data_len);
  367. return 0;
  368. }
  369. int
  370. vips_icc_restore(VipsImage *in, VipsImage **out)
  371. {
  372. if (vips_copy(in, out, NULL))
  373. return 1;
  374. if (vips_image_get_typeof(in, VIPS_META_ICC_NAME) ||
  375. !vips_image_get_typeof(in, IMGPROXY_META_ICC_NAME))
  376. return 0;
  377. const void *data = NULL;
  378. size_t data_len = 0;
  379. if (vips_image_get_blob(in, IMGPROXY_META_ICC_NAME, &data, &data_len))
  380. return 0;
  381. if (!data || data_len < 128)
  382. return 0;
  383. vips_image_remove(*out, VIPS_META_ICC_NAME);
  384. vips_image_set_blob_copy(*out, VIPS_META_ICC_NAME, data, data_len);
  385. return 0;
  386. }
  387. int
  388. vips_icc_import_go(VipsImage *in, VipsImage **out)
  389. {
  390. VipsImage *base = vips_image_new();
  391. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  392. int has_alpha_16 = FALSE;
  393. /* RGB16 and GREY16 images have max alpha 65535, but this is not handled by
  394. * vips_icc_import. We need to extract the alpha channel and convert it to 0-255
  395. */
  396. if ((in->Type == VIPS_INTERPRETATION_RGB16 && in->Bands > 3) ||
  397. (in->Type == VIPS_INTERPRETATION_GREY16 && in->Bands > 1)) {
  398. int bands = in->Type == VIPS_INTERPRETATION_RGB16 ? 3 : 1;
  399. if (vips_extract_band(in, &t[0], 0, "n", bands, NULL) ||
  400. vips_extract_band(in, &t[1], bands, "n", 1, NULL)) {
  401. VIPS_UNREF(base);
  402. return 1;
  403. }
  404. in = t[0];
  405. has_alpha_16 = TRUE;
  406. }
  407. if (vips_icc_import(in, out, "embedded", TRUE, "pcs", vips_icc_get_pcs(in), NULL)) {
  408. VIPS_UNREF(base);
  409. return 1;
  410. }
  411. /* Convert 16-bit alpha channel to 0-255 range and join it back to the image
  412. */
  413. if (has_alpha_16) {
  414. t[2] = *out;
  415. *out = NULL;
  416. if (vips_cast(t[1], &t[3], t[2]->BandFmt, NULL) ||
  417. vips_linear1(t[3], &t[4], 1.0 / 255.0, 0, NULL) ||
  418. vips_bandjoin2(t[2], t[4], out, NULL)) {
  419. VIPS_UNREF(base);
  420. return 1;
  421. }
  422. }
  423. vips_image_set_int(*out, "imgproxy-icc-imported", 1);
  424. VIPS_UNREF(base);
  425. return 0;
  426. }
  427. int
  428. vips_icc_export_go(VipsImage *in, VipsImage **out)
  429. {
  430. return vips_icc_export(in, out, "pcs", vips_icc_get_pcs(in), NULL);
  431. }
  432. int
  433. vips_icc_export_srgb(VipsImage *in, VipsImage **out)
  434. {
  435. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", vips_icc_get_pcs(in), NULL);
  436. }
  437. int
  438. vips_icc_transform_srgb(VipsImage *in, VipsImage **out)
  439. {
  440. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", vips_icc_get_pcs(in), NULL);
  441. }
  442. int
  443. vips_icc_remove(VipsImage *in, VipsImage **out)
  444. {
  445. if (vips_copy(in, out, NULL))
  446. return 1;
  447. vips_image_remove(*out, VIPS_META_ICC_NAME);
  448. vips_image_remove(*out, IMGPROXY_META_ICC_NAME);
  449. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  450. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  451. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  452. return 0;
  453. }
  454. int
  455. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs)
  456. {
  457. return vips_colourspace(in, out, cs, NULL);
  458. }
  459. int
  460. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle)
  461. {
  462. return vips_rot(in, out, angle, NULL);
  463. }
  464. int
  465. vips_flip_horizontal_go(VipsImage *in, VipsImage **out)
  466. {
  467. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  468. }
  469. int
  470. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height)
  471. {
  472. return vips_smartcrop(in, out, width, height, NULL);
  473. }
  474. int
  475. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  476. double sharp_sigma, int pixelate_pixels)
  477. {
  478. VipsImage *base = vips_image_new();
  479. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
  480. VipsInterpretation interpretation = in->Type;
  481. VipsBandFormat format = in->BandFmt;
  482. gboolean premultiplied = FALSE;
  483. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  484. if (
  485. vips_premultiply(in, &t[0], NULL) ||
  486. vips_cast(t[0], &t[1], format, NULL)) {
  487. VIPS_UNREF(base);
  488. return 1;
  489. }
  490. in = t[1];
  491. premultiplied = TRUE;
  492. }
  493. if (blur_sigma > 0.0) {
  494. if (vips_gaussblur(in, &t[2], blur_sigma, NULL)) {
  495. VIPS_UNREF(base);
  496. return 1;
  497. }
  498. in = t[2];
  499. }
  500. if (sharp_sigma > 0.0) {
  501. if (vips_sharpen(in, &t[3], "sigma", sharp_sigma, NULL)) {
  502. VIPS_UNREF(base);
  503. return 1;
  504. }
  505. in = t[3];
  506. }
  507. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  508. if (pixelate_pixels > 1) {
  509. int w, h, tw, th;
  510. w = in->Xsize;
  511. h = in->Ysize;
  512. tw = (int) ceil((double) w / pixelate_pixels) * pixelate_pixels;
  513. th = (int) ceil((double) h / pixelate_pixels) * pixelate_pixels;
  514. if (tw > w || th > h) {
  515. if (vips_embed(in, &t[4], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  516. VIPS_UNREF(base);
  517. return 1;
  518. }
  519. in = t[4];
  520. }
  521. if (
  522. vips_shrink(in, &t[5], pixelate_pixels, pixelate_pixels, NULL) ||
  523. vips_zoom(t[5], &t[6], pixelate_pixels, pixelate_pixels, NULL)) {
  524. VIPS_UNREF(base);
  525. return 1;
  526. }
  527. in = t[6];
  528. if (tw > w || th > h) {
  529. if (vips_extract_area(in, &t[7], 0, 0, w, h, NULL)) {
  530. VIPS_UNREF(base);
  531. return 1;
  532. }
  533. in = t[7];
  534. }
  535. }
  536. if (premultiplied) {
  537. if (vips_unpremultiply(in, &t[8], NULL)) {
  538. VIPS_UNREF(base);
  539. return 1;
  540. }
  541. in = t[8];
  542. }
  543. int res =
  544. vips_colourspace(in, &t[9], interpretation, NULL) ||
  545. vips_cast(t[9], out, format, NULL);
  546. VIPS_UNREF(base);
  547. return res;
  548. }
  549. int
  550. vips_flatten_go(VipsImage *in, VipsImage **out, RGB bg)
  551. {
  552. if (!vips_image_hasalpha(in))
  553. return vips_copy(in, out, NULL);
  554. VipsArrayDouble *bga = vips_array_double_newv(3, bg.r, bg.g, bg.b);
  555. int res = vips_flatten(in, out, "background", bga, NULL);
  556. vips_area_unref((VipsArea *) bga);
  557. return res;
  558. }
  559. int
  560. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height)
  561. {
  562. return vips_extract_area(in, out, left, top, width, height, NULL);
  563. }
  564. int
  565. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  566. gboolean smart, RGB bg, gboolean equal_hor, gboolean equal_ver)
  567. {
  568. VipsImage *base = vips_image_new();
  569. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  570. VipsImage *tmp = in;
  571. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  572. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  573. VIPS_UNREF(base);
  574. return 1;
  575. }
  576. tmp = t[0];
  577. }
  578. if (vips_image_hasalpha(tmp)) {
  579. RGB f_bg = { 255.0, 0, 255.0 };
  580. if (vips_flatten_go(tmp, &t[1], f_bg)) {
  581. VIPS_UNREF(base);
  582. return 1;
  583. }
  584. tmp = t[1];
  585. }
  586. double *img_bg = NULL;
  587. int img_bgn;
  588. VipsArrayDouble *bga;
  589. if (smart) {
  590. if (vips_getpoint(tmp, &img_bg, &img_bgn, 0, 0, NULL)) {
  591. VIPS_UNREF(base);
  592. return 1;
  593. }
  594. bga = vips_array_double_new(img_bg, img_bgn);
  595. }
  596. else {
  597. bga = vips_array_double_newv(3, bg.r, bg.g, bg.b);
  598. }
  599. int left, right, top, bot, width, height, diff;
  600. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  601. VIPS_UNREF(base);
  602. vips_area_unref((VipsArea *) bga);
  603. g_free(img_bg);
  604. if (res) {
  605. return 1;
  606. }
  607. if (equal_hor) {
  608. right = in->Xsize - left - width;
  609. diff = right - left;
  610. if (diff > 0) {
  611. width += diff;
  612. }
  613. else if (diff < 0) {
  614. left = right;
  615. width -= diff;
  616. }
  617. }
  618. if (equal_ver) {
  619. bot = in->Ysize - top - height;
  620. diff = bot - top;
  621. if (diff > 0) {
  622. height += diff;
  623. }
  624. else if (diff < 0) {
  625. top = bot;
  626. height -= diff;
  627. }
  628. }
  629. if (width == 0 || height == 0) {
  630. return vips_copy(in, out, NULL);
  631. }
  632. return vips_extract_area(in, out, left, top, width, height, NULL);
  633. }
  634. int
  635. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height, int centered)
  636. {
  637. VipsImage *tmp;
  638. int across = ceil((double) width / in->Xsize);
  639. int down = ceil((double) height / in->Ysize);
  640. if (centered) {
  641. if (across % 2 == 0)
  642. across++;
  643. if (down % 2 == 0)
  644. down++;
  645. }
  646. if (vips_replicate(in, &tmp, across, down, NULL))
  647. return 1;
  648. const int left = centered ? (tmp->Xsize - width) / 2 : 0;
  649. const int top = centered ? (tmp->Ysize - height) / 2 : 0;
  650. if (vips_extract_area(tmp, out, left, top, width, height, NULL)) {
  651. VIPS_UNREF(tmp);
  652. return 1;
  653. }
  654. VIPS_UNREF(tmp);
  655. return 0;
  656. }
  657. int
  658. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height)
  659. {
  660. VipsImage *tmp = NULL;
  661. if (!vips_image_hasalpha(in)) {
  662. if (vips_addalpha(in, &tmp, NULL))
  663. return 1;
  664. in = tmp;
  665. }
  666. int ret =
  667. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  668. if (tmp)
  669. VIPS_UNREF(tmp);
  670. return ret;
  671. }
  672. int
  673. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity)
  674. {
  675. VipsImage *base = vips_image_new();
  676. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  677. if (!vips_image_hasalpha(watermark)) {
  678. if (vips_addalpha(watermark, &t[0], NULL))
  679. return 1;
  680. watermark = t[0];
  681. }
  682. if (opacity < 1) {
  683. if (
  684. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  685. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  686. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  687. vips_bandjoin2(t[1], t[3], &t[4], NULL)) {
  688. VIPS_UNREF(base);
  689. return 1;
  690. }
  691. watermark = t[4];
  692. }
  693. int had_alpha = vips_image_hasalpha(in);
  694. if (
  695. vips_composite2(
  696. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  697. "x", left, "y", top, "compositing_space", in->Type,
  698. NULL) ||
  699. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)) {
  700. VIPS_UNREF(base);
  701. return 1;
  702. }
  703. int res;
  704. if (!had_alpha && vips_image_hasalpha(t[6])) {
  705. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  706. }
  707. else {
  708. res = vips_copy(t[6], out, NULL);
  709. }
  710. VIPS_UNREF(base);
  711. return res;
  712. }
  713. int
  714. vips_linecache_seq(VipsImage *in, VipsImage **out, int tile_height)
  715. {
  716. return vips_linecache(in, out, "tile_height", tile_height, "access", VIPS_ACCESS_SEQUENTIAL,
  717. NULL);
  718. }
  719. int
  720. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n)
  721. {
  722. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  723. }
  724. typedef struct {
  725. int strip_all;
  726. int keep_exif_copyright;
  727. int keep_animation;
  728. } VipsStripOptions;
  729. void *
  730. vips_strip_fn(VipsImage *in, const char *name, GValue *value, void *a)
  731. {
  732. VipsStripOptions *opts = (VipsStripOptions *) a;
  733. if (strcmp(name, "vips-sequential") == 0)
  734. return NULL;
  735. if (!opts->strip_all) {
  736. if ((strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  737. #ifdef VIPS_META_BITS_PER_SAMPLE
  738. (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) ||
  739. #endif
  740. #ifdef VIPS_META_PALETTE
  741. (strcmp(name, VIPS_META_PALETTE) == 0) ||
  742. #endif
  743. (strcmp(name, VIPS_META_PALETTE_BITS_DEPTH) == 0) ||
  744. (strcmp(name, "background") == 0) ||
  745. (strcmp(name, "vips-loader") == 0) ||
  746. (vips_isprefix("imgproxy-", name)))
  747. return NULL;
  748. if (opts->keep_exif_copyright)
  749. if ((strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  750. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  751. (strcmp(name, "exif-ifd0-Artist") == 0))
  752. return NULL;
  753. if (opts->keep_animation)
  754. if ((strcmp(name, "page-height") == 0) ||
  755. (strcmp(name, "delay") == 0) ||
  756. (strcmp(name, "loop") == 0) ||
  757. (strcmp(name, "n-pages") == 0))
  758. return NULL;
  759. }
  760. vips_image_remove(in, name);
  761. return NULL;
  762. }
  763. int
  764. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright)
  765. {
  766. static double default_resolution = 72.0 / 25.4;
  767. VipsStripOptions opts = {
  768. .strip_all = 0,
  769. .keep_exif_copyright = keep_exif_copyright,
  770. .keep_animation = FALSE,
  771. };
  772. if (vips_image_get_typeof(in, "imgproxy-is-animated") &&
  773. vips_image_get_int(in, "imgproxy-is-animated", &opts.keep_animation))
  774. opts.keep_animation = FALSE;
  775. if (vips_copy(
  776. in, out,
  777. "xres", default_resolution,
  778. "yres", default_resolution,
  779. NULL))
  780. return 1;
  781. vips_image_map(*out, vips_strip_fn, &opts);
  782. return 0;
  783. }
  784. int
  785. vips_strip_all(VipsImage *in, VipsImage **out)
  786. {
  787. VipsStripOptions opts = {
  788. .strip_all = TRUE,
  789. .keep_exif_copyright = FALSE,
  790. .keep_animation = FALSE,
  791. };
  792. if (vips_copy(in, out, NULL))
  793. return 1;
  794. vips_image_map(*out, vips_strip_fn, &opts);
  795. /* vips doesn't include "palette-bit-depth" to the map of fields
  796. */
  797. vips_image_remove(*out, VIPS_META_PALETTE_BITS_DEPTH);
  798. return 0;
  799. }
  800. int
  801. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace)
  802. {
  803. return vips_jpegsave_buffer(
  804. in, buf, len,
  805. "Q", quality,
  806. "optimize_coding", TRUE,
  807. "interlace", interlace,
  808. NULL);
  809. }
  810. int
  811. vips_jxlsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort)
  812. {
  813. return vips_jxlsave_buffer(
  814. in, buf, len,
  815. "Q", quality,
  816. "effort", effort,
  817. NULL);
  818. }
  819. int
  820. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors)
  821. {
  822. int bitdepth;
  823. if (quantize) {
  824. bitdepth = 1;
  825. if (colors > 16)
  826. bitdepth = 8;
  827. else if (colors > 4)
  828. bitdepth = 4;
  829. else if (colors > 2)
  830. bitdepth = 2;
  831. }
  832. else {
  833. bitdepth = vips_get_palette_bit_depth(in);
  834. if (bitdepth && bitdepth <= 8) {
  835. if (bitdepth > 4)
  836. bitdepth = 8;
  837. else if (bitdepth > 2)
  838. bitdepth = 4;
  839. quantize = 1;
  840. colors = 1 << bitdepth;
  841. }
  842. }
  843. if (!quantize)
  844. return vips_pngsave_buffer(
  845. in, buf, len,
  846. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  847. "interlace", interlace,
  848. NULL);
  849. return vips_pngsave_buffer(
  850. in, buf, len,
  851. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  852. "interlace", interlace,
  853. "palette", quantize,
  854. "bitdepth", bitdepth,
  855. NULL);
  856. }
  857. int
  858. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort, VipsForeignWebpPreset preset)
  859. {
  860. return vips_webpsave_buffer(
  861. in, buf, len,
  862. "Q", quality,
  863. "effort", effort,
  864. "preset", preset,
  865. NULL);
  866. }
  867. int
  868. vips_gifsave_go(VipsImage *in, void **buf, size_t *len)
  869. {
  870. int bitdepth = vips_get_palette_bit_depth(in);
  871. if (bitdepth <= 0 || bitdepth > 8)
  872. bitdepth = 8;
  873. return vips_gifsave_buffer(in, buf, len, "bitdepth", bitdepth, NULL);
  874. }
  875. int
  876. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  877. {
  878. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  879. }
  880. int
  881. vips_heifsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  882. {
  883. return vips_heifsave_buffer(
  884. in, buf, len,
  885. "Q", quality,
  886. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_HEVC,
  887. NULL);
  888. }
  889. int
  890. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed)
  891. {
  892. return vips_heifsave_buffer(
  893. in, buf, len,
  894. "Q", quality,
  895. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  896. "effort", 9 - speed,
  897. NULL);
  898. }
  899. void
  900. vips_cleanup()
  901. {
  902. vips_error_clear();
  903. vips_thread_shutdown();
  904. }