vips.c 28 KB

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