Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Has alpha #585

Merged
merged 10 commits into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ An image processing library for Node written entirely in JavaScript, with zero n

Installation: `npm install --save jimp`

API documentation can be found in the main [jimp package](./packages/jimp)

## Supported Image Types

- [bmp](./packages/type-bmp)
Expand Down Expand Up @@ -38,6 +40,13 @@ Installation: `npm install --save jimp`
- [rotate](./packages/plugin-rotate)
- [scale](./packages/plugin-scale)

## Custom Jimp

If you want to extend jimp or omit types or functions visit [@jimp/custom](./packages).

- Add file-types or switch encoder/decoders
- Add add/remove plugins (image manipulation methods)

## Contributing

Basically clone, change, test, push and pull request.
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,25 @@ class Jimp extends EventEmitter {
}

setPixelColour = this.setPixelColor;

/**
* Determine if the image contains opaque pixels.
* @return {boolean} hasAlpha whether the image contains opaque pixels
*/
hasAlpha() {
for (let yIndex = 0; yIndex < this.bitmap.height; yIndex++) {
for (let xIndex = 0; xIndex < this.bitmap.width; xIndex++) {
const idx = (this.bitmap.width * yIndex + xIndex) << 2;
const alpha = this.bitmap.data[idx + 3];

if (alpha !== 0xff) {
return true;
}
}
}

return false;
}
}

export function addConstants(constants) {
Expand Down
1 change: 1 addition & 0 deletions packages/jimp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ image.invert(); // invert the image colours
image.normalize(); // normalize the channels in an image

/* Alpha channel */
image.hasAlpha(); // determines if an image contains opaque pixels
image.fade( f ); // an alternative to opacity, fades the image by a factor 0 - 1. 0 will haven no effect. 1 will turn the image
image.opacity( f ); // multiply the alpha channel by each pixel by the factor f, 0 - 1
image.opaque(); // set the alpha channel on every pixel to fully opaque
Expand Down
1 change: 1 addition & 0 deletions packages/jimp/jimp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ declare namespace Jimp {
event: T,
cb: (data: ListenerData<T>) => any
): any;
hasAlpha(): boolean;
getHeight(): number;
getWidth(): number;
inspect(): string;
Expand Down
18 changes: 16 additions & 2 deletions packages/jimp/test/filetypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import fs from 'fs';
import should from 'should';
import { Jimp, getTestDir } from './test-helper';

describe('FileType', () => {
const imagesDir = getTestDir() + '/samples';
const imagesDir = getTestDir() + '/samples';

describe('FileType', () => {
it('write uses original MIME type', async () => {
if (process.env.ENV === 'browser') {
return;
Expand Down Expand Up @@ -135,3 +135,17 @@ describe('FileType', () => {
image.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
});
});

describe('hasAlpha', () => {
it('image with no alpha', async () => {
const image = await Jimp.read(imagesDir + '/cops.jpg');

image.hasAlpha().should.be.equal(false);
});

it('image with alpha', async () => {
const image = await Jimp.read(imagesDir + '/dice.png');

image.hasAlpha().should.be.equal(true);
});
});