vips.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 = NULL;
  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 = NULL;
  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. VIPS_UNREF(tmp);
  669. return ret;
  670. }
  671. int
  672. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity)
  673. {
  674. VipsImage *base = vips_image_new();
  675. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  676. if (!vips_image_hasalpha(watermark)) {
  677. if (vips_addalpha(watermark, &t[0], NULL))
  678. return 1;
  679. watermark = t[0];
  680. }
  681. if (opacity < 1) {
  682. if (
  683. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  684. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  685. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  686. vips_bandjoin2(t[1], t[3], &t[4], NULL)) {
  687. VIPS_UNREF(base);
  688. return 1;
  689. }
  690. watermark = t[4];
  691. }
  692. int had_alpha = vips_image_hasalpha(in);
  693. if (
  694. vips_composite2(
  695. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  696. "x", left, "y", top, "compositing_space", in->Type,
  697. NULL) ||
  698. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)) {
  699. VIPS_UNREF(base);
  700. return 1;
  701. }
  702. int res;
  703. if (!had_alpha && vips_image_hasalpha(t[6])) {
  704. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  705. }
  706. else {
  707. res = vips_copy(t[6], out, NULL);
  708. }
  709. VIPS_UNREF(base);
  710. return res;
  711. }
  712. int
  713. vips_linecache_seq(VipsImage *in, VipsImage **out, int tile_height)
  714. {
  715. return vips_linecache(in, out, "tile_height", tile_height, "access", VIPS_ACCESS_SEQUENTIAL,
  716. NULL);
  717. }
  718. int
  719. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n)
  720. {
  721. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  722. }
  723. typedef struct {
  724. int strip_all;
  725. int keep_exif_copyright;
  726. int keep_animation;
  727. } VipsStripOptions;
  728. void *
  729. vips_strip_fn(VipsImage *in, const char *name, GValue *value, void *a)
  730. {
  731. VipsStripOptions *opts = (VipsStripOptions *) a;
  732. if (strcmp(name, "vips-sequential") == 0)
  733. return NULL;
  734. if (!opts->strip_all) {
  735. if ((strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  736. #ifdef VIPS_META_BITS_PER_SAMPLE
  737. (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) ||
  738. #endif
  739. #ifdef VIPS_META_PALETTE
  740. (strcmp(name, VIPS_META_PALETTE) == 0) ||
  741. #endif
  742. (strcmp(name, VIPS_META_PALETTE_BITS_DEPTH) == 0) ||
  743. (strcmp(name, "background") == 0) ||
  744. (strcmp(name, "vips-loader") == 0) ||
  745. (vips_isprefix("imgproxy-", name)))
  746. return NULL;
  747. if (opts->keep_exif_copyright)
  748. if ((strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  749. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  750. (strcmp(name, "exif-ifd0-Artist") == 0))
  751. return NULL;
  752. if (opts->keep_animation)
  753. if ((strcmp(name, "page-height") == 0) ||
  754. (strcmp(name, "delay") == 0) ||
  755. (strcmp(name, "loop") == 0) ||
  756. (strcmp(name, "n-pages") == 0))
  757. return NULL;
  758. }
  759. vips_image_remove(in, name);
  760. return NULL;
  761. }
  762. int
  763. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright)
  764. {
  765. static double default_resolution = 72.0 / 25.4;
  766. VipsStripOptions opts = {
  767. .strip_all = 0,
  768. .keep_exif_copyright = keep_exif_copyright,
  769. .keep_animation = FALSE,
  770. };
  771. if (vips_image_get_typeof(in, "imgproxy-is-animated") &&
  772. vips_image_get_int(in, "imgproxy-is-animated", &opts.keep_animation))
  773. opts.keep_animation = FALSE;
  774. if (vips_copy(
  775. in, out,
  776. "xres", default_resolution,
  777. "yres", default_resolution,
  778. NULL))
  779. return 1;
  780. vips_image_map(*out, vips_strip_fn, &opts);
  781. return 0;
  782. }
  783. int
  784. vips_strip_all(VipsImage *in, VipsImage **out)
  785. {
  786. VipsStripOptions opts = {
  787. .strip_all = TRUE,
  788. .keep_exif_copyright = FALSE,
  789. .keep_animation = FALSE,
  790. };
  791. if (vips_copy(in, out, NULL))
  792. return 1;
  793. vips_image_map(*out, vips_strip_fn, &opts);
  794. /* vips doesn't include "palette-bit-depth" to the map of fields
  795. */
  796. vips_image_remove(*out, VIPS_META_PALETTE_BITS_DEPTH);
  797. return 0;
  798. }
  799. int
  800. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace)
  801. {
  802. return vips_jpegsave_buffer(
  803. in, buf, len,
  804. "Q", quality,
  805. "optimize_coding", TRUE,
  806. "interlace", interlace,
  807. NULL);
  808. }
  809. int
  810. vips_jxlsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort)
  811. {
  812. return vips_jxlsave_buffer(
  813. in, buf, len,
  814. "Q", quality,
  815. "effort", effort,
  816. NULL);
  817. }
  818. int
  819. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors)
  820. {
  821. int bitdepth;
  822. if (quantize) {
  823. bitdepth = 1;
  824. if (colors > 16)
  825. bitdepth = 8;
  826. else if (colors > 4)
  827. bitdepth = 4;
  828. else if (colors > 2)
  829. bitdepth = 2;
  830. }
  831. else {
  832. bitdepth = vips_get_palette_bit_depth(in);
  833. if (bitdepth && bitdepth <= 8) {
  834. if (bitdepth > 4)
  835. bitdepth = 8;
  836. else if (bitdepth > 2)
  837. bitdepth = 4;
  838. quantize = 1;
  839. colors = 1 << bitdepth;
  840. }
  841. }
  842. if (!quantize)
  843. return vips_pngsave_buffer(
  844. in, buf, len,
  845. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  846. "interlace", interlace,
  847. NULL);
  848. return vips_pngsave_buffer(
  849. in, buf, len,
  850. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  851. "interlace", interlace,
  852. "palette", quantize,
  853. "bitdepth", bitdepth,
  854. NULL);
  855. }
  856. int
  857. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort, VipsForeignWebpPreset preset)
  858. {
  859. return vips_webpsave_buffer(
  860. in, buf, len,
  861. "Q", quality,
  862. "effort", effort,
  863. "preset", preset,
  864. NULL);
  865. }
  866. int
  867. vips_gifsave_go(VipsImage *in, void **buf, size_t *len)
  868. {
  869. int bitdepth = vips_get_palette_bit_depth(in);
  870. if (bitdepth <= 0 || bitdepth > 8)
  871. bitdepth = 8;
  872. return vips_gifsave_buffer(in, buf, len, "bitdepth", bitdepth, NULL);
  873. }
  874. int
  875. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  876. {
  877. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  878. }
  879. int
  880. vips_heifsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  881. {
  882. return vips_heifsave_buffer(
  883. in, buf, len,
  884. "Q", quality,
  885. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_HEVC,
  886. NULL);
  887. }
  888. int
  889. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed)
  890. {
  891. return vips_heifsave_buffer(
  892. in, buf, len,
  893. "Q", quality,
  894. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  895. "effort", 9 - speed,
  896. NULL);
  897. }
  898. void
  899. vips_cleanup()
  900. {
  901. vips_error_clear();
  902. vips_thread_shutdown();
  903. }