vips.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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, "n-pages") == G_TYPE_INT &&
  269. vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
  270. n_pages > 1);
  271. }
  272. int
  273. vips_image_remove_animation(VipsImage *in, VipsImage **out)
  274. {
  275. if (vips_copy(in, out, NULL))
  276. return -1;
  277. vips_image_remove(*out, "delay");
  278. vips_image_remove(*out, "loop");
  279. vips_image_remove(*out, "page-height");
  280. vips_image_remove(*out, "n-pages");
  281. return 0;
  282. }
  283. int
  284. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n)
  285. {
  286. return vips_image_get_array_int(image, name, out, n);
  287. }
  288. void
  289. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n)
  290. {
  291. vips_image_set_array_int(image, name, array, n);
  292. }
  293. int
  294. vips_addalpha_go(VipsImage *in, VipsImage **out)
  295. {
  296. return vips_addalpha(in, out, NULL);
  297. }
  298. int
  299. vips_copy_go(VipsImage *in, VipsImage **out)
  300. {
  301. return vips_copy(in, out, NULL);
  302. }
  303. int
  304. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format)
  305. {
  306. return vips_cast(in, out, format, NULL);
  307. }
  308. int
  309. vips_rad2float_go(VipsImage *in, VipsImage **out)
  310. {
  311. return vips_rad2float(in, out, NULL);
  312. }
  313. int
  314. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale)
  315. {
  316. if (!vips_image_hasalpha(in))
  317. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  318. VipsBandFormat format = vips_band_format(in);
  319. VipsImage *base = vips_image_new();
  320. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  321. int res =
  322. vips_premultiply(in, &t[0], NULL) ||
  323. vips_cast(t[0], &t[1], format, NULL) ||
  324. vips_resize(t[1], &t[2], wscale, "vscale", hscale, NULL) ||
  325. vips_unpremultiply(t[2], &t[3], NULL) ||
  326. vips_cast(t[3], out, format, NULL);
  327. clear_image(&base);
  328. return res;
  329. }
  330. /* We don't really need to return the size since we check if the buffer is at least
  331. * the size of ICC header, and all we need is a header
  332. */
  333. static const void *
  334. vips_icc_get_header(VipsImage *in)
  335. {
  336. const void *data = NULL;
  337. size_t data_len = 0;
  338. if (!vips_image_get_typeof(in, VIPS_META_ICC_NAME) ||
  339. vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  340. return NULL;
  341. /* Less than header size
  342. */
  343. if (!data || data_len < 128)
  344. return NULL;
  345. return data;
  346. }
  347. int
  348. vips_icc_is_srgb_iec61966(VipsImage *in)
  349. {
  350. // 1998-12-01
  351. static char date[] = { 7, 206, 0, 2, 0, 9 };
  352. // 2.1
  353. static char version[] = { 2, 16, 0, 0 };
  354. const void *data = vips_icc_get_header(in);
  355. if (!data)
  356. return FALSE;
  357. /* Predict it is sRGB IEC61966 2.1 by checking some header fields
  358. */
  359. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  360. (memcmp(data + 16, "RGB ", 4) == 0) && // Colorspace
  361. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  362. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  363. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  364. (memcmp(data + 8, version, 4) == 0)); // Version
  365. }
  366. static VipsPCS
  367. vips_icc_get_pcs(VipsImage *in)
  368. {
  369. const void *data = vips_icc_get_header(in);
  370. if (!data)
  371. return VIPS_PCS_LAB;
  372. if (memcmp(data + 20, "XYZ ", 4) == 0)
  373. return VIPS_PCS_XYZ;
  374. return VIPS_PCS_LAB;
  375. }
  376. int
  377. vips_has_embedded_icc(VipsImage *in)
  378. {
  379. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  380. }
  381. int
  382. vips_icc_backup(VipsImage *in, VipsImage **out)
  383. {
  384. if (vips_copy(in, out, NULL))
  385. return 1;
  386. if (!vips_image_get_typeof(in, VIPS_META_ICC_NAME))
  387. return 0;
  388. const void *data = NULL;
  389. size_t data_len = 0;
  390. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  391. return 0;
  392. if (!data || data_len < 128)
  393. return 0;
  394. vips_image_remove(*out, IMGPROXY_META_ICC_NAME);
  395. vips_image_set_blob_copy(*out, IMGPROXY_META_ICC_NAME, data, data_len);
  396. return 0;
  397. }
  398. int
  399. vips_icc_restore(VipsImage *in, VipsImage **out)
  400. {
  401. if (vips_copy(in, out, NULL))
  402. return 1;
  403. if (vips_image_get_typeof(in, VIPS_META_ICC_NAME) ||
  404. !vips_image_get_typeof(in, IMGPROXY_META_ICC_NAME))
  405. return 0;
  406. const void *data = NULL;
  407. size_t data_len = 0;
  408. if (vips_image_get_blob(in, IMGPROXY_META_ICC_NAME, &data, &data_len))
  409. return 0;
  410. if (!data || data_len < 128)
  411. return 0;
  412. vips_image_remove(*out, VIPS_META_ICC_NAME);
  413. vips_image_set_blob_copy(*out, VIPS_META_ICC_NAME, data, data_len);
  414. return 0;
  415. }
  416. int
  417. vips_icc_import_go(VipsImage *in, VipsImage **out)
  418. {
  419. VipsImage *base = vips_image_new();
  420. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  421. int has_alpha_16 = FALSE;
  422. /* RGB16 and GREY16 images have max alpha 65535, but this is not handled by
  423. * vips_icc_import. We need to extract the alpha channel and convert it to 0-255
  424. */
  425. if ((in->Type == VIPS_INTERPRETATION_RGB16 && in->Bands > 3) ||
  426. (in->Type == VIPS_INTERPRETATION_GREY16 && in->Bands > 1)) {
  427. int bands = in->Type == VIPS_INTERPRETATION_RGB16 ? 3 : 1;
  428. if (vips_extract_band(in, &t[0], 0, "n", bands, NULL) ||
  429. vips_extract_band(in, &t[1], bands, "n", 1, NULL)) {
  430. clear_image(&base);
  431. return 1;
  432. }
  433. in = t[0];
  434. has_alpha_16 = TRUE;
  435. }
  436. if (vips_icc_import(in, out, "embedded", TRUE, "pcs", vips_icc_get_pcs(in), NULL)) {
  437. clear_image(&base);
  438. return 1;
  439. }
  440. /* Convert 16-bit alpha channel to 0-255 range and join it back to the image
  441. */
  442. if (has_alpha_16) {
  443. t[2] = *out;
  444. *out = NULL;
  445. if (vips_cast(t[1], &t[3], t[2]->BandFmt, NULL) ||
  446. vips_linear1(t[3], &t[4], 1.0 / 255.0, 0, NULL) ||
  447. vips_bandjoin2(t[2], t[4], out, NULL)) {
  448. clear_image(&base);
  449. return 1;
  450. }
  451. }
  452. vips_image_set_int(*out, "imgproxy-icc-imported", 1);
  453. clear_image(&base);
  454. return 0;
  455. }
  456. int
  457. vips_icc_export_go(VipsImage *in, VipsImage **out)
  458. {
  459. return vips_icc_export(in, out, "pcs", vips_icc_get_pcs(in), NULL);
  460. }
  461. int
  462. vips_icc_export_srgb(VipsImage *in, VipsImage **out)
  463. {
  464. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", vips_icc_get_pcs(in), NULL);
  465. }
  466. int
  467. vips_icc_transform_srgb(VipsImage *in, VipsImage **out)
  468. {
  469. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", vips_icc_get_pcs(in), NULL);
  470. }
  471. int
  472. vips_icc_remove(VipsImage *in, VipsImage **out)
  473. {
  474. if (vips_copy(in, out, NULL))
  475. return 1;
  476. vips_image_remove(*out, VIPS_META_ICC_NAME);
  477. vips_image_remove(*out, IMGPROXY_META_ICC_NAME);
  478. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  479. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  480. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  481. return 0;
  482. }
  483. int
  484. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs)
  485. {
  486. return vips_colourspace(in, out, cs, NULL);
  487. }
  488. int
  489. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle)
  490. {
  491. return vips_rot(in, out, angle, NULL);
  492. }
  493. int
  494. vips_flip_horizontal_go(VipsImage *in, VipsImage **out)
  495. {
  496. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  497. }
  498. int
  499. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height)
  500. {
  501. return vips_smartcrop(in, out, width, height, NULL);
  502. }
  503. int
  504. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  505. double sharp_sigma, int pixelate_pixels)
  506. {
  507. VipsImage *base = vips_image_new();
  508. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
  509. VipsInterpretation interpretation = in->Type;
  510. VipsBandFormat format = in->BandFmt;
  511. gboolean premultiplied = FALSE;
  512. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  513. if (
  514. vips_premultiply(in, &t[0], NULL) ||
  515. vips_cast(t[0], &t[1], format, NULL)) {
  516. clear_image(&base);
  517. return 1;
  518. }
  519. in = t[1];
  520. premultiplied = TRUE;
  521. }
  522. if (blur_sigma > 0.0) {
  523. if (vips_gaussblur(in, &t[2], blur_sigma, NULL)) {
  524. clear_image(&base);
  525. return 1;
  526. }
  527. in = t[2];
  528. }
  529. if (sharp_sigma > 0.0) {
  530. if (vips_sharpen(in, &t[3], "sigma", sharp_sigma, NULL)) {
  531. clear_image(&base);
  532. return 1;
  533. }
  534. in = t[3];
  535. }
  536. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  537. if (pixelate_pixels > 1) {
  538. int w, h, tw, th;
  539. w = in->Xsize;
  540. h = in->Ysize;
  541. tw = (int) ceil((double) w / pixelate_pixels) * pixelate_pixels;
  542. th = (int) ceil((double) h / pixelate_pixels) * pixelate_pixels;
  543. if (tw > w || th > h) {
  544. if (vips_embed(in, &t[4], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  545. clear_image(&base);
  546. return 1;
  547. }
  548. in = t[4];
  549. }
  550. if (
  551. vips_shrink(in, &t[5], pixelate_pixels, pixelate_pixels, NULL) ||
  552. vips_zoom(t[5], &t[6], pixelate_pixels, pixelate_pixels, NULL)) {
  553. clear_image(&base);
  554. return 1;
  555. }
  556. in = t[6];
  557. if (tw > w || th > h) {
  558. if (vips_extract_area(in, &t[7], 0, 0, w, h, NULL)) {
  559. clear_image(&base);
  560. return 1;
  561. }
  562. in = t[7];
  563. }
  564. }
  565. if (premultiplied) {
  566. if (vips_unpremultiply(in, &t[8], NULL)) {
  567. clear_image(&base);
  568. return 1;
  569. }
  570. in = t[8];
  571. }
  572. int res =
  573. vips_colourspace(in, &t[9], interpretation, NULL) ||
  574. vips_cast(t[9], out, format, NULL);
  575. clear_image(&base);
  576. return res;
  577. }
  578. int
  579. vips_flatten_go(VipsImage *in, VipsImage **out, RGB bg)
  580. {
  581. if (!vips_image_hasalpha(in))
  582. return vips_copy(in, out, NULL);
  583. VipsArrayDouble *bga = vips_array_double_newv(3, bg.r, bg.g, bg.b);
  584. int res = vips_flatten(in, out, "background", bga, NULL);
  585. vips_area_unref((VipsArea *) bga);
  586. return res;
  587. }
  588. int
  589. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height)
  590. {
  591. return vips_extract_area(in, out, left, top, width, height, NULL);
  592. }
  593. int
  594. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  595. gboolean smart, RGB bg, gboolean equal_hor, gboolean equal_ver)
  596. {
  597. VipsImage *base = vips_image_new();
  598. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  599. VipsImage *tmp = in;
  600. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  601. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  602. clear_image(&base);
  603. return 1;
  604. }
  605. tmp = t[0];
  606. }
  607. if (vips_image_hasalpha(tmp)) {
  608. RGB f_bg = { 255.0, 0, 255.0 };
  609. if (vips_flatten_go(tmp, &t[1], f_bg)) {
  610. clear_image(&base);
  611. return 1;
  612. }
  613. tmp = t[1];
  614. }
  615. double *img_bg = NULL;
  616. int img_bgn;
  617. VipsArrayDouble *bga;
  618. if (smart) {
  619. if (vips_getpoint(tmp, &img_bg, &img_bgn, 0, 0, NULL)) {
  620. clear_image(&base);
  621. return 1;
  622. }
  623. bga = vips_array_double_new(img_bg, img_bgn);
  624. }
  625. else {
  626. bga = vips_array_double_newv(3, bg.r, bg.g, bg.b);
  627. }
  628. int left, right, top, bot, width, height, diff;
  629. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  630. clear_image(&base);
  631. vips_area_unref((VipsArea *) bga);
  632. g_free(img_bg);
  633. if (res) {
  634. return 1;
  635. }
  636. if (equal_hor) {
  637. right = in->Xsize - left - width;
  638. diff = right - left;
  639. if (diff > 0) {
  640. width += diff;
  641. }
  642. else if (diff < 0) {
  643. left = right;
  644. width -= diff;
  645. }
  646. }
  647. if (equal_ver) {
  648. bot = in->Ysize - top - height;
  649. diff = bot - top;
  650. if (diff > 0) {
  651. height += diff;
  652. }
  653. else if (diff < 0) {
  654. top = bot;
  655. height -= diff;
  656. }
  657. }
  658. if (width == 0 || height == 0) {
  659. return vips_copy(in, out, NULL);
  660. }
  661. return vips_extract_area(in, out, left, top, width, height, NULL);
  662. }
  663. int
  664. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height, int centered)
  665. {
  666. VipsImage *tmp;
  667. int across = ceil((double) width / in->Xsize);
  668. int down = ceil((double) height / in->Ysize);
  669. if (centered) {
  670. if (across % 2 == 0)
  671. across++;
  672. if (down % 2 == 0)
  673. down++;
  674. }
  675. if (vips_replicate(in, &tmp, across, down, NULL))
  676. return 1;
  677. const int left = centered ? (tmp->Xsize - width) / 2 : 0;
  678. const int top = centered ? (tmp->Ysize - height) / 2 : 0;
  679. if (vips_extract_area(tmp, out, left, top, width, height, NULL)) {
  680. clear_image(&tmp);
  681. return 1;
  682. }
  683. clear_image(&tmp);
  684. return 0;
  685. }
  686. int
  687. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height)
  688. {
  689. VipsImage *tmp = NULL;
  690. if (!vips_image_hasalpha(in)) {
  691. if (vips_addalpha(in, &tmp, NULL))
  692. return 1;
  693. in = tmp;
  694. }
  695. int ret =
  696. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  697. if (tmp)
  698. clear_image(&tmp);
  699. return ret;
  700. }
  701. int
  702. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity)
  703. {
  704. VipsImage *base = vips_image_new();
  705. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  706. if (!vips_image_hasalpha(watermark)) {
  707. if (vips_addalpha(watermark, &t[0], NULL))
  708. return 1;
  709. watermark = t[0];
  710. }
  711. if (opacity < 1) {
  712. if (
  713. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  714. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  715. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  716. vips_bandjoin2(t[1], t[3], &t[4], NULL)) {
  717. clear_image(&base);
  718. return 1;
  719. }
  720. watermark = t[4];
  721. }
  722. int had_alpha = vips_image_hasalpha(in);
  723. if (
  724. vips_composite2(
  725. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  726. "x", left, "y", top, "compositing_space", in->Type,
  727. NULL) ||
  728. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)) {
  729. clear_image(&base);
  730. return 1;
  731. }
  732. int res;
  733. if (!had_alpha && vips_image_hasalpha(t[6])) {
  734. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  735. }
  736. else {
  737. res = vips_copy(t[6], out, NULL);
  738. }
  739. clear_image(&base);
  740. return res;
  741. }
  742. int
  743. vips_linecache_seq(VipsImage *in, VipsImage **out, int tile_height)
  744. {
  745. return vips_linecache(in, out, "tile_height", tile_height, "access", VIPS_ACCESS_SEQUENTIAL,
  746. NULL);
  747. }
  748. int
  749. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n)
  750. {
  751. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  752. }
  753. typedef struct {
  754. int strip_all;
  755. int keep_exif_copyright;
  756. int keep_animation;
  757. } VipsStripOptions;
  758. void *
  759. vips_strip_fn(VipsImage *in, const char *name, GValue *value, void *a)
  760. {
  761. VipsStripOptions *opts = (VipsStripOptions *) a;
  762. if (strcmp(name, "vips-sequential") == 0)
  763. return NULL;
  764. if (!opts->strip_all) {
  765. if ((strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  766. #ifdef VIPS_META_BITS_PER_SAMPLE
  767. (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) ||
  768. #endif
  769. #ifdef VIPS_META_PALETTE
  770. (strcmp(name, VIPS_META_PALETTE) == 0) ||
  771. #endif
  772. (strcmp(name, VIPS_META_PALETTE_BITS_DEPTH) == 0) ||
  773. (strcmp(name, "background") == 0) ||
  774. (strcmp(name, "vips-loader") == 0) ||
  775. (vips_isprefix("imgproxy-", name)))
  776. return NULL;
  777. if (opts->keep_exif_copyright)
  778. if ((strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  779. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  780. (strcmp(name, "exif-ifd0-Artist") == 0))
  781. return NULL;
  782. if (opts->keep_animation)
  783. if ((strcmp(name, "page-height") == 0) ||
  784. (strcmp(name, "delay") == 0) ||
  785. (strcmp(name, "loop") == 0) ||
  786. (strcmp(name, "n-pages") == 0))
  787. return NULL;
  788. }
  789. vips_image_remove(in, name);
  790. return NULL;
  791. }
  792. int
  793. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright)
  794. {
  795. static double default_resolution = 72.0 / 25.4;
  796. VipsStripOptions opts = {
  797. .strip_all = 0,
  798. .keep_exif_copyright = keep_exif_copyright,
  799. .keep_animation = FALSE,
  800. };
  801. if (vips_image_get_typeof(in, "imgproxy-is-animated") &&
  802. vips_image_get_int(in, "imgproxy-is-animated", &opts.keep_animation))
  803. opts.keep_animation = FALSE;
  804. if (vips_copy(
  805. in, out,
  806. "xres", default_resolution,
  807. "yres", default_resolution,
  808. NULL))
  809. return 1;
  810. vips_image_map(*out, vips_strip_fn, &opts);
  811. return 0;
  812. }
  813. int
  814. vips_strip_all(VipsImage *in, VipsImage **out)
  815. {
  816. VipsStripOptions opts = {
  817. .strip_all = TRUE,
  818. .keep_exif_copyright = FALSE,
  819. .keep_animation = FALSE,
  820. };
  821. if (vips_copy(in, out, NULL))
  822. return 1;
  823. vips_image_map(*out, vips_strip_fn, &opts);
  824. /* vips doesn't include "palette-bit-depth" to the map of fields
  825. */
  826. vips_image_remove(*out, VIPS_META_PALETTE_BITS_DEPTH);
  827. return 0;
  828. }
  829. int
  830. vips_jpegsave_go(VipsImage *in, VipsTarget *target, int quality, int interlace)
  831. {
  832. return vips_jpegsave_target(
  833. in, target,
  834. "Q", quality,
  835. "optimize_coding", TRUE,
  836. "interlace", interlace,
  837. NULL);
  838. }
  839. int
  840. vips_jxlsave_go(VipsImage *in, VipsTarget *target, int quality, int effort)
  841. {
  842. return vips_jxlsave_target(
  843. in, target,
  844. "Q", quality,
  845. "effort", effort,
  846. NULL);
  847. }
  848. int
  849. vips_pngsave_go(VipsImage *in, VipsTarget *target, int interlace, int quantize, int colors)
  850. {
  851. int bitdepth;
  852. if (quantize) {
  853. bitdepth = 1;
  854. if (colors > 16)
  855. bitdepth = 8;
  856. else if (colors > 4)
  857. bitdepth = 4;
  858. else if (colors > 2)
  859. bitdepth = 2;
  860. }
  861. else {
  862. bitdepth = vips_get_palette_bit_depth(in);
  863. if (bitdepth && bitdepth <= 8) {
  864. if (bitdepth > 4)
  865. bitdepth = 8;
  866. else if (bitdepth > 2)
  867. bitdepth = 4;
  868. quantize = 1;
  869. colors = 1 << bitdepth;
  870. }
  871. }
  872. if (!quantize)
  873. return vips_pngsave_target(
  874. in, target,
  875. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  876. "interlace", interlace,
  877. NULL);
  878. return vips_pngsave_target(
  879. in, target,
  880. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  881. "interlace", interlace,
  882. "palette", quantize,
  883. "bitdepth", bitdepth,
  884. NULL);
  885. }
  886. int
  887. vips_webpsave_go(VipsImage *in, VipsTarget *target, int quality, int effort, VipsForeignWebpPreset preset)
  888. {
  889. return vips_webpsave_target(
  890. in, target,
  891. "Q", quality,
  892. "effort", effort,
  893. "preset", preset,
  894. NULL);
  895. }
  896. int
  897. vips_gifsave_go(VipsImage *in, VipsTarget *target)
  898. {
  899. int bitdepth = vips_get_palette_bit_depth(in);
  900. if (bitdepth <= 0 || bitdepth > 8)
  901. bitdepth = 8;
  902. return vips_gifsave_target(in, target, "bitdepth", bitdepth, NULL);
  903. }
  904. int
  905. vips_tiffsave_go(VipsImage *in, VipsTarget *target, int quality)
  906. {
  907. return vips_tiffsave_target(in, target, "Q", quality, NULL);
  908. }
  909. int
  910. vips_heifsave_go(VipsImage *in, VipsTarget *target, int quality)
  911. {
  912. return vips_heifsave_target(
  913. in, target,
  914. "Q", quality,
  915. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_HEVC,
  916. NULL);
  917. }
  918. int
  919. vips_avifsave_go(VipsImage *in, VipsTarget *target, int quality, int speed)
  920. {
  921. return vips_heifsave_target(
  922. in, target,
  923. "Q", quality,
  924. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  925. "effort", 9 - speed,
  926. NULL);
  927. }
  928. void
  929. vips_cleanup()
  930. {
  931. vips_error_clear();
  932. vips_thread_shutdown();
  933. }
  934. void
  935. vips_error_go(const char *function, const char *message)
  936. {
  937. vips_error(function, "%s", message);
  938. }
  939. int
  940. vips_foreign_load_read_full(VipsSource *source, void *buf, size_t len)
  941. {
  942. while (len > 0) {
  943. ssize_t n = vips_source_read(source, buf, len);
  944. if (n <= 0)
  945. return n;
  946. buf = (uint8_t *) buf + n;
  947. len -= n;
  948. }
  949. return 1;
  950. }
  951. void
  952. vips_unref_target(VipsTarget *target)
  953. {
  954. VIPS_UNREF(target);
  955. }