Path: blob/master/node_modules/@jimp/png/src/index.js
2593 views
import { PNG } from 'pngjs';1import { throwError, isNodePattern } from '@jimp/utils';23const MIME_TYPE = 'image/png';45// PNG filter types6const PNG_FILTER_AUTO = -1;7const PNG_FILTER_NONE = 0;8const PNG_FILTER_SUB = 1;9const PNG_FILTER_UP = 2;10const PNG_FILTER_AVERAGE = 3;11const PNG_FILTER_PATH = 4;1213export default () => ({14mime: { [MIME_TYPE]: ['png'] },1516constants: {17MIME_PNG: MIME_TYPE,18PNG_FILTER_AUTO,19PNG_FILTER_NONE,20PNG_FILTER_SUB,21PNG_FILTER_UP,22PNG_FILTER_AVERAGE,23PNG_FILTER_PATH24},2526hasAlpha: { [MIME_TYPE]: true },27decoders: { [MIME_TYPE]: PNG.sync.read },28encoders: {29[MIME_TYPE]: data => {30const png = new PNG({31width: data.bitmap.width,32height: data.bitmap.height33});3435png.data = data.bitmap.data;3637return PNG.sync.write(png, {38width: data.bitmap.width,39height: data.bitmap.height,40deflateLevel: data._deflateLevel,41deflateStrategy: data._deflateStrategy,42filterType: data._filterType,43colorType:44typeof data._colorType === 'number'45? data._colorType46: data._rgba47? 648: 2,49inputHasAlpha: data._rgba50});51}52},5354class: {55_deflateLevel: 9,56_deflateStrategy: 3,57_filterType: PNG_FILTER_AUTO,58_colorType: null,5960/**61* Sets the deflate level used when saving as PNG format (default is 9)62* @param {number} l Deflate level to use 0-9. 0 is no compression. 9 (default) is maximum compression.63* @param {function(Error, Jimp)} cb (optional) a callback for when complete64* @returns {Jimp} this for chaining of methods65*/66deflateLevel(l, cb) {67if (typeof l !== 'number') {68return throwError.call(this, 'l must be a number', cb);69}7071if (l < 0 || l > 9) {72return throwError.call(this, 'l must be a number 0 - 9', cb);73}7475this._deflateLevel = Math.round(l);7677if (isNodePattern(cb)) {78cb.call(this, null, this);79}8081return this;82},8384/**85* Sets the deflate strategy used when saving as PNG format (default is 3)86* @param {number} s Deflate strategy to use 0-3.87* @param {function(Error, Jimp)} cb (optional) a callback for when complete88* @returns {Jimp} this for chaining of methods89*/90deflateStrategy(s, cb) {91if (typeof s !== 'number') {92return throwError.call(this, 's must be a number', cb);93}9495if (s < 0 || s > 3) {96return throwError.call(this, 's must be a number 0 - 3', cb);97}9899this._deflateStrategy = Math.round(s);100101if (isNodePattern(cb)) {102cb.call(this, null, this);103}104105return this;106},107108/**109* Sets the filter type used when saving as PNG format (default is automatic filters)110* @param {number} f The quality to use -1-4.111* @param {function(Error, Jimp)} cb (optional) a callback for when complete112* @returns {Jimp} this for chaining of methods113*/114filterType(f, cb) {115if (typeof f !== 'number') {116return throwError.call(this, 'n must be a number', cb);117}118119if (f < -1 || f > 4) {120return throwError.call(121this,122'n must be -1 (auto) or a number 0 - 4',123cb124);125}126127this._filterType = Math.round(f);128129if (isNodePattern(cb)) {130cb.call(this, null, this);131}132133return this;134},135/**136* Sets the color type used when saving as PNG format137* @param {number} s color type to use 0, 2, 4, 6.138* @param {function(Error, Jimp)} cb (optional) a callback for when complete139* @returns {Jimp} this for chaining of methods140*/ colorType(s, cb) {141if (typeof s !== 'number') {142return throwError.call(this, 's must be a number', cb);143}144145if (s !== 0 && s !== 2 && s !== 4 && s !== 6) {146return throwError.call(this, 's must be a number 0, 2, 4, 6.', cb);147}148149this._colorType = Math.round(s);150151if (isNodePattern(cb)) {152cb.call(this, null, this);153}154155return this;156}157}158});159160161