Path: blob/master/node_modules/@jimp/bmp/src/index.js
2593 views
import BMP from 'bmp-js';1import { scan } from '@jimp/utils';23const MIME_TYPE = 'image/bmp';4const MIME_TYPE_SECOND = 'image/x-ms-bmp';56function toAGBR(image) {7return scan(image, 0, 0, image.bitmap.width, image.bitmap.height, function(8x,9y,10index11) {12const red = this.bitmap.data[index + 0];13const green = this.bitmap.data[index + 1];14const blue = this.bitmap.data[index + 2];15const alpha = this.bitmap.data[index + 3];1617this.bitmap.data[index + 0] = alpha;18this.bitmap.data[index + 1] = blue;19this.bitmap.data[index + 2] = green;20this.bitmap.data[index + 3] = red;21}).bitmap;22}2324function fromAGBR(bitmap) {25return scan({ bitmap }, 0, 0, bitmap.width, bitmap.height, function(26x,27y,28index29) {30const alpha = this.bitmap.data[index + 0];31const blue = this.bitmap.data[index + 1];32const green = this.bitmap.data[index + 2];33const red = this.bitmap.data[index + 3];3435this.bitmap.data[index + 0] = red;36this.bitmap.data[index + 1] = green;37this.bitmap.data[index + 2] = blue;38this.bitmap.data[index + 3] = bitmap.is_with_alpha ? alpha : 0xff;39}).bitmap;40}4142const decode = data => fromAGBR(BMP.decode(data));43const encode = image => BMP.encode(toAGBR(image)).data;4445export default () => ({46mime: { [MIME_TYPE]: ['bmp'] },4748constants: {49MIME_BMP: MIME_TYPE,50MIME_X_MS_BMP: MIME_TYPE_SECOND51},5253decoders: {54[MIME_TYPE]: decode,55[MIME_TYPE_SECOND]: decode56},5758encoders: {59[MIME_TYPE]: encode,60[MIME_TYPE_SECOND]: encode61}62});636465