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

Fa b/dev #614

Open
wants to merge 13 commits into
base: legacy
Choose a base branch
from
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"eslint": "eslint src test/common.js test/nodeScripts --cache",
"eslint-fix": "npm run eslint -- --fix",
"prepack": "npm run compile",
"prepare": "npm run compile",
"prettier": "prettier --check .",
"prettier-write": "prettier --write .",
"test": "npm run test-only && npm run eslint && npm run prettier",
Expand All @@ -71,7 +72,7 @@
"eslint": "^8.1.0",
"eslint-config-cheminfo": "^7.1.2",
"esm": "^3.2.25",
"jest": "^27.3.1",
"jest": "^27.5.1",
"jest-matcher-deep-close-to": "^3.0.2",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/test-binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ test('test-binary', () => {
new Uint8Array([0b11111111, 0b00000000, 0b00001111]),
);
}

{
const result = binary`
1111
0000
1111
`;
expect(result.data).toStrictEqual(new Uint8Array([0b11110000, 0b11110000]));
}
});
6 changes: 6 additions & 0 deletions src/image/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ export default class Image {
`incorrect data size: ${data.length}. Should be ${expectedLength}`,
);
}

if (bitDepth === 1) {
const exceedingBits = data.length * 8 - size * channels;
data[data.length - 1] =
data[data.length - 1] & ~(0xff >> (8 - exceedingBits));
}
}

/**
Expand Down
24 changes: 18 additions & 6 deletions src/image/internal/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ export default function copyImage(fromImage, toImage, x, y) {
let fromHeight = fromImage.height;
let toWidth = toImage.width;
let channels = fromImage.channels;
for (let i = 0; i < fromWidth; i++) {
for (let j = 0; j < fromHeight; j++) {
for (let k = 0; k < channels; k++) {
let source = (j * fromWidth + i) * channels + k;
let target = ((y + j) * toWidth + x + i) * channels + k;
toImage.data[target] = fromImage.data[source];
if (fromImage.bitDepth === 1 && toImage.bitDepth === 1) {
for (let i = 0; i < fromHeight; i++) {
for (let j = 0; j < fromWidth; j++) {
let source = i * fromWidth + j;
let target = (i + y) * toWidth + (j + x);
if (fromImage.getBit(source)) {
toImage.setBit(target);
}
}
}
} else {
for (let i = 0; i < fromWidth; i++) {
for (let j = 0; j < fromHeight; j++) {
for (let k = 0; k < channels; k++) {
let source = (j * fromWidth + i) * channels + k;
let target = ((y + j) * toWidth + x + i) * channels + k;
toImage.data[target] = fromImage.data[source];
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/image/operator/__tests__/paintPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ describe('we check paintPolygon', function () {

expect(Array.from(image.data)).toStrictEqual(Array.from(painted.data));
});

it('should yield the filled painted binary image with a cut triangle', function () {
let size = 5;
let data = new Uint8Array(Math.ceil((size * size) / 8)).fill(0xff);
let image = new Image(size, size, data, { kind: 'BINARY' });

let points = [
[2, 0],
[4, 2],
[0, 2],
];
image.paintPolygon(points, { filled: true, color: 0 });

let painted = binary`
11011
10001
00000
11111
11111
`;

expect(Array.from(image.data)).toStrictEqual(Array.from(painted.data));
});
});

function getExpected(painted, color) {
Expand Down
23 changes: 23 additions & 0 deletions src/image/operator/__tests__/paintPolyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ describe('we check paintPolyline', function () {

expect(Array.from(image.data)).toStrictEqual(Array.from(painted.data));
});

it('should yield the painted image binary image with a cut triangle', function () {
let size = 5;
let data = new Uint8Array(Math.ceil((size * size) / 8)).fill(0xff);
let image = new Image(size, size, data, { kind: 'BINARY' });

let points = [
[2, 0],
[4, 2],
[0, 2],
];
image.paintPolyline(points, { color: 0 });

let painted = binary`
11011
11101
00000
11111
11111
`;

expect(Array.from(image.data)).toStrictEqual(Array.from(painted.data));
});
});

function getExpected(painted, color) {
Expand Down
5 changes: 4 additions & 1 deletion src/image/operator/paintPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function paintPolygon(points, options = {}) {
bitDepth: [1, 8, 16],
});

color = color.length ? color : [color];

options.closed = true;

let filteredPoints = deleteDouble(points);
Expand Down Expand Up @@ -45,7 +47,8 @@ export default function paintPolygon(points, options = {}) {
for (let x = 0; x < this.width; x++) {
if (matrixBinary[y][x] === 1) {
if (this.bitDepth === 1) {
this.setBitXY(x, y);
if (color[0] === 1) this.setBitXY(x, y);
else this.clearBitXY(x, y);
} else {
let numberChannels = Math.min(this.channels, color.length);
let position = (x + y * this.width) * this.channels;
Expand Down
5 changes: 4 additions & 1 deletion src/image/operator/paintPolyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function paintPolyline(points, options = {}) {
bitDepth: [1, 8, 16],
});

color = color.length ? color : [color];

let numberChannels = Math.min(this.channels, color.length);

for (let i = 0; i < points.length - 1 + closed; i++) {
Expand All @@ -41,7 +43,8 @@ export default function paintPolyline(points, options = {}) {
yPoint < this.height
) {
if (this.bitDepth === 1) {
this.setBitXY(xPoint, yPoint);
if (color[0] === 1) this.setBitXY(xPoint, yPoint);
else this.clearBitXY(xPoint, yPoint);
} else {
let position = (xPoint + yPoint * this.width) * this.channels;
for (let channel = 0; channel < numberChannels; channel++) {
Expand Down
73 changes: 72 additions & 1 deletion src/image/transform/__tests__/pad.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
import binary from 'test/binary';
import { Image } from 'test/common';

describe('check the pad transform', function () {
it('check the right pad extract for a BINARY image', function () {
let image = new Image(5, 5, [0x03, 0x9c, 0xe0, 0x00], { kind: 'BINARY' });

let expected = binary`
00000
01110
01110
01110
00000
`;

expect(image.pad().data).toStrictEqual(expected.data);

expected = binary`
0000000
0000000
0011100
0011100
0011100
0000000
0000000
`;
expect(image.pad({ size: 1 }).data).toStrictEqual(expected.data);

expected = binary`
0000000
0000000
0000000
0011100
0011100
0011100
0000000
0000000
0000000
`;
expect(image.pad({ size: [1, 2] }).data).toStrictEqual(expected.data);
});

it('check the right pad extract for GREY image', function () {
let image = new Image(2, 2, [1, 2, 3, 4], { kind: 'GREY' });

Expand All @@ -10,6 +49,10 @@ describe('check the pad transform', function () {
1, 1, 2, 2, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 4, 4,
]);

expect(
Array.from(image.pad({ size: 1, algorithm: 'set', color: 0 }).data),
).toStrictEqual([0, 0, 0, 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 0]);

expect(Array.from(image.pad({ size: 2 }).data)).toStrictEqual([
1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 3,
3, 3, 4, 4, 4, 3, 3, 3, 4, 4, 4,
Expand All @@ -18,9 +61,37 @@ describe('check the pad transform', function () {
expect(
Array.from(image.pad({ algorithm: 'set', size: 1, color: [9] }).data),
).toStrictEqual([9, 9, 9, 9, 9, 1, 2, 9, 9, 3, 4, 9, 9, 9, 9, 9]);
});

it('check the right pad extract for RGB image', function () {
let image = new Image(2, 2, [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], {
kind: 'RGB',
});

expect(Array.from(image.pad().data)).toStrictEqual([
1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4,
]);

expect(Array.from(image.pad({ size: 1 }).data)).toStrictEqual([
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
]);

expect(
Array.from(
image.pad({ algorithm: 'set', size: 2, color: [15, 15, 15] }).data,
),
).toStrictEqual([
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 1, 1, 1, 2, 2, 2, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 3, 3, 3, 4, 4, 4, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
]);

expect(function () {
image.pad({ algorithm: 'set', size: 1, color: [0, 1] });
}).toThrow(/the color array must have the same/);
}).toThrow(/the color array must at least have the same/);
});
});
1 change: 1 addition & 0 deletions src/image/transform/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function crop(options = {}) {
const newImage = new Image(width, height, {
kind: 'BINARY',
parent: this,
position: [x, y],
});
result = cropBinary(this, newImage, x, y, width, height);
} else {
Expand Down
Loading