* Create a Jimp instance from a bitmap. * The difference between this and just using the constructor is that this will * convert raw image data into the bitmap format that Jimp uses. * * @example * ```ts * import { Jimp } from "jimp"; * * const image = Jimp.fr
(bitmap: RawImageData)
| 284 | * ``` |
| 285 | */ |
| 286 | static fromBitmap(bitmap: RawImageData) { |
| 287 | let data: Buffer | undefined; |
| 288 | |
| 289 | if (bitmap.data instanceof Buffer) { |
| 290 | data = Buffer.from(bitmap.data); |
| 291 | } |
| 292 | |
| 293 | if ( |
| 294 | bitmap.data instanceof Uint8Array || |
| 295 | bitmap.data instanceof Uint8ClampedArray |
| 296 | ) { |
| 297 | data = Buffer.from(bitmap.data.buffer); |
| 298 | } |
| 299 | |
| 300 | if (Array.isArray(bitmap.data)) { |
| 301 | data = Buffer.concat( |
| 302 | bitmap.data.map((hex) => |
| 303 | Buffer.from(hex.toString(16).padStart(8, "0"), "hex") |
| 304 | ) |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | if (!data) { |
| 309 | throw new Error("data must be a Buffer"); |
| 310 | } |
| 311 | |
| 312 | if ( |
| 313 | typeof bitmap.height !== "number" || |
| 314 | typeof bitmap.width !== "number" |
| 315 | ) { |
| 316 | throw new Error("bitmap must have width and height"); |
| 317 | } |
| 318 | |
| 319 | return new CustomJimp({ |
| 320 | height: bitmap.height, |
| 321 | width: bitmap.width, |
| 322 | data, |
| 323 | }) as InstanceType<typeof CustomJimp> & ExtraMethodMap; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Parse a bitmap with the loaded image types. |
no test coverage detected