Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Jul 22, 2018
2 parents 3d04a35 + 6ee8801 commit c45d089
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 452 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ image.pixelate( size[, x, y, w, h ]); // apply a pixelation effect to the image
image.displace( map, offset ); // displaces the image pixels based on the provided displacement map. Useful for making stereoscopic 3D images.
```

Some of these methods are irreversable, so it can be useful to perform them on a clone of the original image:
Some of these methods are irreversible, so it can be useful to perform them on a clone of the original image:

```js
image.clone(); // returns a clone of the image
Expand Down Expand Up @@ -157,7 +157,7 @@ image.resize(250, 250, Jimp.RESIZE_BEZIER);

### Align modes ###

The following constants can be passed to image.cover and image.contain methods:
The following constants can be passed to the `image.cover` and `image.contain` methods:

```js
Jimp.HORIZONTAL_ALIGN_LEFT;
Expand All @@ -175,7 +175,7 @@ For example:
image.contain(250, 250, Jimp.HORIZONTAL_ALIGN_LEFT | Jimp.VERTICAL_ALIGN_TOP);
```

Default align modes are :
Default align modes are:

```js
Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE;
Expand Down Expand Up @@ -242,7 +242,7 @@ image.write(file)

### Writing to Buffers ###

A PNG, JPEG or BMP binary Buffer of an image (e.g. for storage in a database) can be obtained using:
A PNG, JPEG or BMP binary Buffer of an image (e.g. for storage in a database) can be generated using:

```js
image.getBuffer( mime, cb ); // Node-style callback will be fired with result
Expand All @@ -256,7 +256,7 @@ Jimp.MIME_JPEG; // "image/jpeg"
Jimp.MIME_BMP; // "image/bmp"
```

If `Jimp.AUTO` is passed as the MIME type then the original MIME type for the image (or "image/png") will be used. Alernatively, `image.getMIME()` will return the original MIME type of the image (or "image/png").
If `Jimp.AUTO` is passed as the MIME type then the original MIME type for the image (or "image/png") will be used. Alternatively, `image.getMIME()` will return the original MIME type of the image (or "image/png").

### Data URI ###

Expand Down Expand Up @@ -341,7 +341,7 @@ Implement emboss effect:
])
```

### Low-level manipulation ###
### Low-level manipulation

Jimp enables low-level manipulation of images in memory through the bitmap property of each Jimp object:

Expand All @@ -351,7 +351,7 @@ image.bitmap.width; // the width of the image
image.bitmap.height // the height of the image
```

This data can be manipulated directly but remember: garbage in, garbage out.
This data can be manipulated directly, but remember: garbage in, garbage out.

A helper method is available to scan a region of the bitmap:

Expand Down Expand Up @@ -382,10 +382,10 @@ If you need to do something with the image at the end of the scan:
```js
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
// do your stuff..
if(x == image.bitmap.width-1 &&

if(x == image.bitmap.width-1 &&
y == image.bitmap.height-1) {
// image scan finished, do your stuff
// image scan finished, do your stuff
}
});
```
Expand Down Expand Up @@ -466,7 +466,7 @@ diff.image; // a Jimp image showing differences
diff.percent; // the proportion of different pixels (0-1), where 0 means the images are pixel identical
```

Using a mix of hamming distance and pixel diffing to comare images, the following code has a 99% success rate of detecting the same image from a random sample (with 1% false positives). The test this figure is drawn from attempts to match each image from a sample of 120 PNGs against 120 corresponing JPEGs saved at a quality setting of 60.
Using a mix of hamming distance and pixel diffing to compare images, the following code has a 99% success rate of detecting the same image from a random sample (with 1% false positives). The test this figure is drawn from attempts to match each image from a sample of 120 PNGs against 120 corresponding JPEGs saved at a quality setting of 60.

```js
var distance = Jimp.distance(png, jpeg); // perceived distance
Expand Down Expand Up @@ -505,12 +505,13 @@ The Node-style callback pattern allows Jimp to be used with frameworks that expe

## Contributing ##

Basicaly clone, change, test, push and pull request.
Please read de [CONTRIBUTING documentation](CONTRIBUTING.md).
Basically clone, change, test, push and pull request.

Please read the [CONTRIBUTING documentation](CONTRIBUTING.md).

### Testing ###

The test framework runs at node.js and browsers environments. Just run `npm test` to test in node and browser environments.
The test framework runs in Node.js and browser environments. Just run `npm test` to test in Node and browser environments.
More information at ["How to Contribute" doc's "Testing" topic](CONTRIBUTING.md#testing).

## License ##
Expand Down
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ class Jimp extends EventEmitter {

var bitmap = new Buffer(original.bitmap.data.length);
original.scanQuiet(0, 0, original.bitmap.width, original.bitmap.height, function (x, y, idx) {
var data = original.bitmap.data.readUInt32BE(idx, true);
bitmap.writeUInt32BE(data, idx, true);
var data = original.bitmap.data.readUInt32BE(idx);
bitmap.writeUInt32BE(data, idx);
});

this.bitmap = {
Expand Down Expand Up @@ -929,7 +929,7 @@ Jimp.prototype.setPixelColor = Jimp.prototype.setPixelColour = function (hex, x,
y = Math.round(y);

var idx = this.getPixelIndex(x, y);
this.bitmap.data.writeUInt32BE(hex, idx, true);
this.bitmap.data.writeUInt32BE(hex, idx);

if (isNodePattern(cb)) return cb.call(this, null, this);
else return this;
Expand Down Expand Up @@ -994,8 +994,8 @@ JimpEvChange("crop", function crop (x, y, w, h, cb) {
var bitmap = new Buffer(this.bitmap.data.length);
var offset = 0;
this.scanQuiet(x, y, w, h, function (x, y, idx) {
var data = this.bitmap.data.readUInt32BE(idx, true);
bitmap.writeUInt32BE(data, offset, true);
var data = this.bitmap.data.readUInt32BE(idx);
bitmap.writeUInt32BE(data, offset);
offset += 4;
});

Expand Down Expand Up @@ -1520,8 +1520,8 @@ Jimp.prototype.mirror = Jimp.prototype.flip = function (horizontal, vertical, cb
var _y = (vertical) ? (this.bitmap.height - 1 - y) : y;
var _idx = (this.bitmap.width * _y + _x) << 2;

var data = this.bitmap.data.readUInt32BE(idx, true);
bitmap.writeUInt32BE(data, _idx, true);
var data = this.bitmap.data.readUInt32BE(idx);
bitmap.writeUInt32BE(data, _idx);
});

this.bitmap.data = new Buffer(bitmap);
Expand Down Expand Up @@ -1903,7 +1903,7 @@ Jimp.prototype.resize = function (w, h, mode, cb) {
}

if (w === Jimp.AUTO && h === Jimp.AUTO)
return throwError.call(this, "w and h cannot both the set to auto", cb);
return throwError.call(this, "w and h cannot both be set to auto", cb);

if (w === Jimp.AUTO) w = this.bitmap.width * (h / this.bitmap.height);
if (h === Jimp.AUTO) h = this.bitmap.height * (w / this.bitmap.width);
Expand Down Expand Up @@ -2221,8 +2221,8 @@ function simpleRotate (deg) {
for (let x = this.bitmap.width-1; x >= 0; x--) {
for (let y = 0; y < this.bitmap.height; y++) {
var srcOffset = (this.bitmap.width * y + x) << 2;
var data = this.bitmap.data.readUInt32BE(srcOffset, true);
dstBuffer.writeUInt32BE(data, dstOffset, true);
var data = this.bitmap.data.readUInt32BE(srcOffset);
dstBuffer.writeUInt32BE(data, dstOffset);
dstOffset += 4;
}
}
Expand Down Expand Up @@ -2303,7 +2303,7 @@ function advancedRotate (deg, mode) {
if (source.x >= 0 && source.x < bW &&
source.y >= 0 && source.y < bH) {
var srcIdx = (bW * (source.y | 0) + source.x | 0) << 2;
var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx, true);
var pixelRGBA = this.bitmap.data.readUInt32BE(srcIdx);
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
} else {
// reset off-image pixels
Expand Down
121 changes: 64 additions & 57 deletions jimp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
declare namespace Jimp {
type ImageCallback = (err: Error|null, image: Jimp) => any;

interface Bitmap {
data: Buffer, width: number, height: number
}
interface RGB {
r: number;
g: number;
Expand All @@ -14,8 +17,14 @@ declare namespace Jimp {
a: number;
}

interface Jimp {
bitmap: {data: Buffer, width: number, height: number};
class Jimp {
bitmap: Bitmap;

constructor(path: string, cb?: Jimp.ImageCallback);
constructor(image: Jimp, cb?: Jimp.ImageCallback);
constructor(data: Buffer, cb?: Jimp.ImageCallback);
constructor(w: number, h: number, cb?: Jimp.ImageCallback);
constructor(w: number, h: number, background?: number, cb?: Jimp.ImageCallback);

clone(cb?: Jimp.ImageCallback): Jimp;
quality(n: number, cb?: Jimp.ImageCallback): this;
Expand Down Expand Up @@ -62,6 +71,7 @@ declare namespace Jimp {
pixelate(size: number, x: number, y: number, w: number, h: number, cb?: Jimp.ImageCallback): this;
convolute(kernel: any, x: number | Jimp.ImageCallback, y?: number, w?: number, h?: number, cb?: Jimp.ImageCallback): this;
rotate(deg: number, mode?: number | boolean, cb?: Jimp.ImageCallback): this;
exifRotate(): this;
displace(map: Jimp, offset: number, cb?: Jimp.ImageCallback): this;
getBuffer(mime: string, cb:(err:Error, buffer:Buffer)=>any): this;
getBase64(mime: string, cb?: (err: Error, src: string) => any): this;
Expand All @@ -73,85 +83,82 @@ declare namespace Jimp {
print(font: any, x: number, y: number, text: string, maxWidth?: number | Jimp.ImageCallback, maxHeight?: number | Jimp.ImageCallback, cb?: Jimp.ImageCallback): this;
inspect(): string;
toString(): string;
}

var Jimp: {

// used to auto resizing etc.
AUTO: number;
static AUTO: number;

// supported mime types
MIME_PNG: string;
MIME_JPEG: string;
MIME_JGD: string;
MIME_BMP: string;
MIME_X_MS_BMP: string;
MIME_GIF: string;

static MIME_PNG: string;
static MIME_JPEG: string;
static MIME_JGD: string;
static MIME_BMP: string;
static MIME_X_MS_BMP: string;
static MIME_GIF: string;
// PNG filter types
PNG_FILTER_AUTO: number;
PNG_FILTER_NONE: number;
PNG_FILTER_SUB: number;
PNG_FILTER_UP: number;
PNG_FILTER_AVERAGE: number;
PNG_FILTER_PAETH: number;
static PNG_FILTER_AUTO: number;
static PNG_FILTER_NONE: number;
static PNG_FILTER_SUB: number;
static PNG_FILTER_UP: number;
static PNG_FILTER_AVERAGE: number;
static PNG_FILTER_PAETH: number;

// resize methods
RESIZE_NEAREST_NEIGHBOR: string;
RESIZE_BILINEAR: string;
RESIZE_BICUBIC: string;
RESIZE_HERMITE: string;
RESIZE_BEZIER: string;
static RESIZE_NEAREST_NEIGHBOR: string;
static RESIZE_BILINEAR: string;
static RESIZE_BICUBIC: string;
static RESIZE_HERMITE: string;
static RESIZE_BEZIER: string;

// Align modes for cover, contain, bit masks
HORIZONTAL_ALIGN_LEFT: number;
HORIZONTAL_ALIGN_CENTER: number;
HORIZONTAL_ALIGN_RIGHT: number;
static HORIZONTAL_ALIGN_LEFT: number;
static HORIZONTAL_ALIGN_CENTER: number;
static HORIZONTAL_ALIGN_RIGHT: number;

VERTICAL_ALIGN_TOP: number;
VERTICAL_ALIGN_MIDDLE: number;
VERTICAL_ALIGN_BOTTOM: number;
static VERTICAL_ALIGN_TOP: number;
static VERTICAL_ALIGN_MIDDLE: number;
static VERTICAL_ALIGN_BOTTOM: number;

// Font locations
FONT_SANS_8_BLACK: string;
FONT_SANS_10_BLACK: string;
FONT_SANS_12_BLACK: string;
FONT_SANS_14_BLACK: string;
FONT_SANS_16_BLACK: string;
FONT_SANS_32_BLACK: string;
FONT_SANS_64_BLACK: string;
FONT_SANS_128_BLACK: string;

FONT_SANS_8_WHITE: string;
FONT_SANS_16_WHITE: string;
FONT_SANS_32_WHITE: string;
FONT_SANS_64_WHITE: string;
FONT_SANS_128_WHITE: string;
static FONT_SANS_8_BLACK: string;
static FONT_SANS_10_BLACK: string;
static FONT_SANS_12_BLACK: string;
static FONT_SANS_14_BLACK: string;
static FONT_SANS_16_BLACK: string;
static FONT_SANS_32_BLACK: string;
static FONT_SANS_64_BLACK: string;
static FONT_SANS_128_BLACK: string;

static FONT_SANS_8_WHITE: string;
static FONT_SANS_16_WHITE: string;
static FONT_SANS_32_WHITE: string;
static FONT_SANS_64_WHITE: string;
static FONT_SANS_128_WHITE: string;

// Edge Handling
EDGE_EXTEND: number;
EDGE_WRAP: number;
EDGE_CROP: number;
static EDGE_EXTEND: number;
static EDGE_WRAP: number;
static EDGE_CROP: number;

/* These are constructors, have already moved up, TODO: remove it in the future
(path: string, cb?: Jimp.ImageCallback): void;
(image: Jimp, cb?: Jimp.ImageCallback): void;
(data: Buffer, cb?: Jimp.ImageCallback): void;
(w: number, h: number, cb?: Jimp.ImageCallback): void;
(w: number, h: number, background?: number, cb?: Jimp.ImageCallback): void;
*/

read(src: string | Buffer, cb?: Jimp.ImageCallback): Promise<Jimp>;
loadFont(file: string, cb?: Jimp.ImageCallback): Promise<any>;
static read(src: string | Buffer, cb?: Jimp.ImageCallback): Promise<Jimp>;
static loadFont(file: string, cb?: Jimp.ImageCallback): Promise<any>;

rgbaToInt(r: number, g: number, b: number, a: number, cb?: (err: Error, i: number)=>any): number;
intToRGBA(i: number, cb?: (err:Error, rgba: Jimp.RGBA)=>any): Jimp.RGBA;
limit255(n: number): number;
diff(img1: Jimp, img2: Jimp, threshold?: number): {percent: number, image: Jimp};
distance(img1: Jimp, img2: Jimp): number;
static rgbaToInt(r: number, g: number, b: number, a: number, cb?: (err: Error, i: number)=>any): number;
static intToRGBA(i: number, cb?: (err:Error, rgba: Jimp.RGBA)=>any): Jimp.RGBA;
static limit255(n: number): number;
static diff(img1: Jimp, img2: Jimp, threshold?: number): {percent: number, image: Jimp};
static distance(img1: Jimp, img2: Jimp): number;

colorDiff(rgba1: Jimp.RGB | Jimp.RGBA, rgba2: Jimp.RGB | Jimp.RGBA): number;
static colorDiff(rgba1: Jimp.RGB | Jimp.RGBA, rgba2: Jimp.RGB | Jimp.RGBA): number;
}

prototype: Jimp;
};
}

declare module "jimp" {
Expand Down

0 comments on commit c45d089

Please sign in to comment.