vips.c 27 KB

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