1
0

vips.c 27 KB

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