vips.c 28 KB

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