* Crops the image at a given point to a give size. * * @example * ```ts * import { Jimp } from "jimp"; * * const image = await Jimp.read("test/image.png"); * const cropped = image.crop(150, 100); * ```
(image: I, options: CropOptions)
| 52 | * ``` |
| 53 | */ |
| 54 | crop<I extends JimpClass>(image: I, options: CropOptions) { |
| 55 | let { x, y, w, h } = CropOptionsSchema.parse(options); |
| 56 | // round input |
| 57 | x = Math.round(x); |
| 58 | y = Math.round(y); |
| 59 | w = Math.round(w); |
| 60 | h = Math.round(h); |
| 61 | |
| 62 | if (x === 0 && w === image.bitmap.width) { |
| 63 | // shortcut |
| 64 | const start = (w * y + x) << 2; |
| 65 | const end = start + ((h * w) << 2); |
| 66 | |
| 67 | image.bitmap.data = image.bitmap.data.slice(start, end); |
| 68 | } else { |
| 69 | const bitmap = Buffer.allocUnsafe(w * h * 4); |
| 70 | let offset = 0; |
| 71 | |
| 72 | scan(image, x, y, w, h, function (_, __, idx) { |
| 73 | const data = image.bitmap.data.readUInt32BE(idx); |
| 74 | bitmap.writeUInt32BE(data, offset); |
| 75 | offset += 4; |
| 76 | }); |
| 77 | |
| 78 | image.bitmap.data = bitmap; |
| 79 | } |
| 80 | |
| 81 | image.bitmap.width = w; |
| 82 | image.bitmap.height = h; |
| 83 | |
| 84 | return image; |
| 85 | }, |
| 86 | |
| 87 | /** |
| 88 | * Autocrop same color borders from this image. |
nothing calls this directly
no test coverage detected
searching dependent graphs…