vips.c 26 KB

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