image.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { describe, expect, it } from "vitest";
  2. import { Jimp, JimpMime, rgbaToInt } from "jimp";
  3. import { createJimpImageProcessorAdapter } from "../src/image/index.js";
  4. describe("Jimp image processor adapter", () => {
  5. it("resizes images so the longest edge is within the requested dimension", async () => {
  6. const input = await createPng(3000, 1000, 0x2060a0ff);
  7. const result = await createJimpImageProcessorAdapter().resizeToFit({
  8. data: input,
  9. maxDimension: 2048,
  10. mediaType: "image/png",
  11. });
  12. const metadata = await Jimp.read(Buffer.from(result.data));
  13. expect(result.resized).toBe(true);
  14. expect(result.mediaType).toBe("image/png");
  15. expect(result.originalWidth).toBe(3000);
  16. expect(result.originalHeight).toBe(1000);
  17. expect(result.width).toBeLessThanOrEqual(2048);
  18. expect(result.height).toBeLessThanOrEqual(2048);
  19. expect(metadata.bitmap.width).toBeLessThanOrEqual(2048);
  20. expect(metadata.bitmap.height).toBeLessThanOrEqual(2048);
  21. });
  22. it("returns the original bytes when an image already fits", async () => {
  23. const input = await createPng(64, 32, 0x0c2238ff);
  24. const result = await createJimpImageProcessorAdapter().resizeToFit({
  25. data: input,
  26. maxDimension: 2048,
  27. mediaType: "image/png",
  28. });
  29. expect(result.resized).toBe(false);
  30. expect(Buffer.from(result.data).equals(input)).toBe(true);
  31. expect(result.width).toBe(64);
  32. expect(result.height).toBe(32);
  33. });
  34. it("passes WebP through because the Jimp adapter does not encode WebP", async () => {
  35. const input = Buffer.from("webp-bytes");
  36. const result = await createJimpImageProcessorAdapter().resizeToFit({
  37. data: input,
  38. maxDimension: 2048,
  39. mediaType: "image/webp",
  40. });
  41. expect(result.resized).toBe(false);
  42. expect(result.mediaType).toBe("image/webp");
  43. expect(Buffer.from(result.data).equals(input)).toBe(true);
  44. expect(result.width).toBeUndefined();
  45. expect(result.height).toBeUndefined();
  46. });
  47. it("keeps model images original when dimensions and budgets already fit", async () => {
  48. const input = await createPng(64, 32, 0x0c2238ff);
  49. const result = await createJimpImageProcessorAdapter().prepareForModel({
  50. data: input,
  51. maxBase64Bytes: 1024 * 1024,
  52. maxDimension: 2048,
  53. maxRawBytes: 1024 * 1024,
  54. mediaType: "image/png",
  55. });
  56. expect(result.strategy).toBe("original");
  57. expect(result.compressed).toBe(false);
  58. expect(result.resized).toBe(false);
  59. expect(result.transformedSizeBytes).toBe(input.byteLength);
  60. expect(Buffer.from(result.data).equals(input)).toBe(true);
  61. });
  62. it("resizes model images before returning them when the longest edge is too large", async () => {
  63. const input = await createPng(3000, 1000, 0x2060a0ff);
  64. const result = await createJimpImageProcessorAdapter().prepareForModel({
  65. data: input,
  66. maxBase64Bytes: 10 * 1024 * 1024,
  67. maxDimension: 2048,
  68. maxRawBytes: 10 * 1024 * 1024,
  69. mediaType: "image/png",
  70. });
  71. expect(result.resized).toBe(true);
  72. expect(result.originalWidth).toBe(3000);
  73. expect(result.originalHeight).toBe(1000);
  74. expect(result.width).toBeLessThanOrEqual(2048);
  75. expect(result.height).toBeLessThanOrEqual(2048);
  76. expect(result.originalSizeBytes).toBe(input.byteLength);
  77. expect(result.transformedSizeBytes).toBe(
  78. Buffer.from(result.data).byteLength,
  79. );
  80. });
  81. it("tries compression candidates until model image byte budgets fit", async () => {
  82. const input = await createNoisyPng(512, 512);
  83. const maxRawBytes = 100_000;
  84. const result = await createJimpImageProcessorAdapter().prepareForModel({
  85. data: input,
  86. maxBase64Bytes: 140_000,
  87. maxDimension: 2048,
  88. maxRawBytes,
  89. mediaType: "image/png",
  90. });
  91. expect(result.compressed).toBe(true);
  92. expect(result.resized).toBe(false);
  93. expect(result.originalSizeBytes).toBe(input.byteLength);
  94. expect(result.transformedSizeBytes).toBeLessThanOrEqual(maxRawBytes);
  95. expect(Buffer.from(result.data).byteLength).toBe(
  96. result.transformedSizeBytes,
  97. );
  98. });
  99. it("switches an over-budget PNG to JPEG without reducing dimensions when JPEG fits", async () => {
  100. const width = 256;
  101. const height = 256;
  102. const input = await createNoisyPng(width, height);
  103. const image = await Jimp.read(input);
  104. const optimizedPng = await image.getBuffer(JimpMime.png, {
  105. deflateLevel: 9,
  106. deflateStrategy: 3,
  107. });
  108. const jpegCandidates = await Promise.all(
  109. [80, 60, 40, 20].map((quality) =>
  110. image.getBuffer(JimpMime.jpeg, { quality }),
  111. ),
  112. );
  113. const firstFittingJpeg = jpegCandidates.find(
  114. (candidate) => candidate.byteLength < optimizedPng.byteLength,
  115. );
  116. expect(firstFittingJpeg).toBeDefined();
  117. const maxRawBytes = firstFittingJpeg?.byteLength ?? 0;
  118. const result = await createJimpImageProcessorAdapter().prepareForModel({
  119. data: input,
  120. maxBase64Bytes: base64LengthForRawBytes(maxRawBytes),
  121. maxDimension: 2048,
  122. maxRawBytes,
  123. mediaType: "image/png",
  124. });
  125. expect(result.mediaType).toBe("image/jpeg");
  126. expect(result.strategy).toBe("jpeg-quality");
  127. expect(result.width).toBe(width);
  128. expect(result.height).toBe(height);
  129. expect(result.resized).toBe(false);
  130. });
  131. it("uses only resized JPEG candidates after an original-size PNG exceeds the budget", async () => {
  132. const width = 512;
  133. const height = 512;
  134. const input = await createNoisyPng(width, height);
  135. const image = await Jimp.read(input);
  136. const smallestOriginalJpeg = await image.getBuffer(JimpMime.jpeg, {
  137. quality: 20,
  138. });
  139. const maxRawBytes = smallestOriginalJpeg.byteLength - 1;
  140. const result = await createJimpImageProcessorAdapter().prepareForModel({
  141. data: input,
  142. maxBase64Bytes: base64LengthForRawBytes(maxRawBytes),
  143. maxDimension: 2048,
  144. maxRawBytes,
  145. mediaType: "image/png",
  146. });
  147. expect(result.mediaType).toBe("image/jpeg");
  148. expect(result.strategy).toMatch(/^jpeg-/u);
  149. expect(result.width).toBeLessThan(width);
  150. expect(result.height).toBeLessThan(height);
  151. expect(result.resized).toBe(true);
  152. });
  153. it("compresses a tall browser screenshot PNG below the 200 KiB base64 budget", async () => {
  154. const input = await createNoisyPng(320, 2400);
  155. const maxBase64Bytes = 200 * 1024;
  156. const maxDimension = 2000;
  157. const maxRawBytes = Math.floor((maxBase64Bytes * 3) / 4);
  158. const result = await createJimpImageProcessorAdapter().prepareForModel({
  159. data: input,
  160. maxBase64Bytes,
  161. maxDimension,
  162. maxRawBytes,
  163. mediaType: "image/png",
  164. });
  165. const transformed = Buffer.from(result.data);
  166. expect(input.toString("base64").length).toBeGreaterThan(maxBase64Bytes);
  167. expect(transformed.toString("base64").length).toBeLessThanOrEqual(
  168. maxBase64Bytes,
  169. );
  170. expect(transformed.byteLength).toBeLessThanOrEqual(maxRawBytes);
  171. expect(Math.max(result.width ?? 0, result.height ?? 0)).toBeLessThanOrEqual(
  172. maxDimension,
  173. );
  174. expect(result.compressed).toBe(true);
  175. expect(result.mediaType).toBe("image/jpeg");
  176. });
  177. it("rejects empty model image inputs before decoding", async () => {
  178. await expect(
  179. createJimpImageProcessorAdapter().prepareForModel({
  180. data: Buffer.alloc(0),
  181. maxBase64Bytes: 1024,
  182. maxDimension: 2048,
  183. maxRawBytes: 1024,
  184. mediaType: "image/png",
  185. }),
  186. ).rejects.toMatchObject({
  187. code: "empty",
  188. });
  189. });
  190. it("rejects over-budget WebP inputs because this adapter cannot transcode WebP", async () => {
  191. const webpBytes = Buffer.concat([
  192. Buffer.from([0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50]),
  193. Buffer.alloc(32),
  194. ]);
  195. await expect(
  196. createJimpImageProcessorAdapter().prepareForModel({
  197. data: webpBytes,
  198. maxBase64Bytes: 12,
  199. maxDimension: 2048,
  200. maxRawBytes: 8,
  201. mediaType: "image/webp",
  202. }),
  203. ).rejects.toMatchObject({
  204. code: "unsupported",
  205. });
  206. });
  207. });
  208. async function createPng(
  209. width: number,
  210. height: number,
  211. color: number,
  212. ): Promise<Buffer> {
  213. const image = new Jimp({ color, height, width });
  214. return await image.getBuffer(JimpMime.png);
  215. }
  216. async function createNoisyPng(width: number, height: number): Promise<Buffer> {
  217. const image = new Jimp({ color: 0xffffffff, height, width });
  218. for (let y = 0; y < height; y++) {
  219. for (let x = 0; x < width; x++) {
  220. const red = (x * 31 + y * 17) & 0xff;
  221. const green = (x * 13 + y * 47) & 0xff;
  222. const blue = (x * 53 + y * 7) & 0xff;
  223. image.setPixelColor(rgbaToInt(red, green, blue, 255), x, y);
  224. }
  225. }
  226. return await image.getBuffer(JimpMime.png);
  227. }
  228. function base64LengthForRawBytes(rawBytes: number): number {
  229. return Math.ceil(rawBytes / 3) * 4;
  230. }