Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed Dec 18, 2021
2 parents 2f5fe43 + 46a3beb commit cba7252
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .ci/utils/use-esy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# steps to install esy globally

steps:
- script: "npm install -g esy"
- script: "npm install -g @esy-nightly/esy"
displayName: "install esy"
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,18 @@ export type ODiffOptions = Partial<{
outputDiffMask: boolean;
/** Do not compare images and produce output if images layout is different. */
failOnLayoutDiff: boolean;
/** Return { match: false, reason: '...' } instead of throwing error if file is missing. */
noFailOnFsErrors: boolean;
/** Color difference threshold (from 0 to 1). Less more precise. */
threshold: number;
/** If this is true, antialiased pixels are not counted to the diff of an image */
antialiasing: boolean;
/** An array of regions to ignore in the diff. */
ignoreRegions: Array<{
x1: number,
y1: number,
x2: number,
y2: number,
x1: number;
y1: number;
x2: number;
y2: number;
}>;
}>;

Expand All @@ -117,6 +119,12 @@ declare function compare(
/** Percentage of different pixels in the whole image */
diffPercentage: number;
}
| {
match: false;
reason: "file-not-exists";
/** Errored file path */
file: string;
}
>;

export { compare };
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- template: .ci/build-platform.yml
parameters:
platform: MacOS
vmImage: macOS-10.14
vmImage: macOS-latest

# Need windows-2019 to do esy import/export-dependencies
# which assumes you have bsdtar (tar.exe) in your system
Expand All @@ -38,7 +38,7 @@ jobs:
- MacOS
- Windows_x64
pool:
vmImage: macOS-10.14
vmImage: macOS-latest
demands: node.js
steps:
- template: .ci/cross-release.yml
16 changes: 12 additions & 4 deletions bin/node-bindings/odiff.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ export type ODiffOptions = Partial<{
outputDiffMask: boolean;
/** Do not compare images and produce output if images layout is different. */
failOnLayoutDiff: boolean;
/** Return { match: false, reason: '...' } instead of throwing error if file is missing. */
noFailOnFsErrors: boolean;
/** Color difference threshold (from 0 to 1). Less more precise. */
threshold: number;
/** If this is true, antialiased pixels are not counted to the diff of an image */
antialiasing: boolean;
/** An array of regions to ignore in the diff. */
ignoreRegions: Array<{
x1: number,
y1: number,
x2: number,
y2: number,
x1: number;
y1: number;
x2: number;
y2: number;
}>;
}>;

Expand All @@ -34,6 +36,12 @@ declare function compare(
/** Percentage of different pixels in the whole image */
diffPercentage: number;
}
| {
match: false;
reason: "file-not-exists";
/** Errored file path */
file: string;
}
>;

export { compare };
37 changes: 25 additions & 12 deletions bin/node-bindings/odiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ function optionsToArgs(options) {

case "ignoreRegions": {
const regions = value
.map((region) => `${region.x1}:${region.y1}-${region.x2}:${region.y2}`)
.join(',');
.map(
(region) => `${region.x1}:${region.y1}-${region.x2}:${region.y2}`
)
.join(",");

setArgWithValue("ignore", regions);
break;
Expand Down Expand Up @@ -92,9 +94,10 @@ async function compare(basePath, comparePath, diffOutput, options = {}) {
return new Promise((resolve, reject) => {
let producedStdout, producedStdError;

const binaryPath = options && options.__binaryPath
? options.__binaryPath
: path.join(__dirname, "bin", "odiff");
const binaryPath =
options && options.__binaryPath
? options.__binaryPath
: path.join(__dirname, "bin", "odiff");

execFile(
binaryPath,
Expand All @@ -119,14 +122,24 @@ async function compare(basePath, comparePath, diffOutput, options = {}) {
});
break;
case 124:
reject(
new TypeError(
(producedStdError || "Invalid Argument Exception").replace(
CMD_BIN_HELPER_MSG,
""
)
)
/** @type string */
const originalErrorMessage = (
producedStdError || "Invalid Argument Exception"
).replace(CMD_BIN_HELPER_MSG, "");

const noFileOrDirectoryMatches = originalErrorMessage.match(
/no\n\s*`(.*)'\sfile or\n\s*directory/
);

if (options.noFailOnFsErrors && noFileOrDirectoryMatches[1]) {
resolve({
match: false,
reason: "file-not-exists",
file: noFileOrDirectoryMatches[1],
});
} else {
reject(new TypeError(originalErrorMessage));
}
break;

default:
Expand Down
19 changes: 12 additions & 7 deletions io/png/ReadPng.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ read_png_file(value file)
const char *filename = String_val(file);

png = fopen(filename, "rb");
if(png == NULL) {
if (png == NULL)
{
caml_failwith("error opening input file");
}

ctx = spng_ctx_new(0);

if(ctx == NULL) {
if (ctx == NULL)
{
caml_failwith("spng_ctx_new() failed");
spng_ctx_free(ctx);
free(out);
Expand All @@ -47,27 +49,30 @@ read_png_file(value file)
struct spng_ihdr ihdr;
result = spng_get_ihdr(ctx, &ihdr);

if(result) {
if (result)
{
caml_failwith("spng_get_ihdr() error!");
spng_ctx_free(ctx);
free(out);
}

size_t out_size;
result = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
if(result) {
if (result)
{
spng_ctx_free(ctx);
};


out = malloc(out_size);
if(out == NULL) {
if (out == NULL)
{
spng_ctx_free(ctx);
free(out);
};

result = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
if(result) {
if (result)
{
spng_ctx_free(ctx);
free(out);
caml_failwith(spng_strerror(result));
Expand Down
38 changes: 23 additions & 15 deletions io/png_write/WritePng.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ value write_png_bigarray(value filename_val, value bigarray, value width_val, va

FILE *fp;

if (( fp = fopen(filename, "wb")) == NULL ){
if ((fp = fopen(filename, "wb")) == NULL)
{
caml_failwith("Can not save the output :(");
}

Expand All @@ -35,51 +36,58 @@ value write_png_bigarray(value filename_val, value bigarray, value width_val, va

spng_ctx *ctx = spng_ctx_new(SPNG_CTX_ENCODER);
struct spng_ihdr ihdr = {
width,
height,
bit_depth,
color_type,
compression_method,
filter_method,
interlace_method,
width,
height,
bit_depth,
color_type,
compression_method,
filter_method,
interlace_method,
};

result = spng_set_ihdr(ctx, &ihdr);
if(result) {
if (result)
{
spng_ctx_free(ctx);
fclose(fp);
caml_failwith(spng_strerror(result));
}

result = spng_set_option(ctx, SPNG_FILTER_CHOICE, SPNG_DISABLE_FILTERING);
if(result) {
if (result)
{
spng_ctx_free(ctx);
fclose(fp);
caml_failwith(spng_strerror(result));
}

result = spng_set_png_file(ctx, fp);
if(result) {
if (result)
{
fclose(fp);
spng_ctx_free(ctx);
caml_failwith(spng_strerror(result));
}

result = spng_encode_image(ctx, 0, 0, SPNG_FMT_PNG, SPNG_ENCODE_PROGRESSIVE);

if(result) {
if (result)
{
fclose(fp);
spng_ctx_free(ctx);
caml_failwith(spng_strerror(result));
}

for(int i = 0; i < ihdr.height; i++) {
for (int i = 0; i < ihdr.height; i++)
{
const char *row = data + out_width * i;
result = spng_encode_scanline(ctx, row, out_width);
if(result) break;
if (result)
break;
}

if(result != SPNG_EOI) {
if (result != SPNG_EOI)
{
spng_ctx_free(ctx);
fclose(fp);
caml_failwith(spng_strerror(result));
Expand Down
15 changes: 7 additions & 8 deletions io/tiff/ReadTiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@ read_tiff_file_to_tuple(value file)

TIFF *image;



if (!(image = TIFFOpen(filename, "r"))) {
if (!(image = TIFFOpen(filename, "r")))
{
caml_failwith("opening input file failed!");
}

TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);


int buffer_size = width * height;
buffer = (uint32_t*)malloc(buffer_size * 4);
buffer = (uint32_t *)malloc(buffer_size * 4);

if (!buffer) {
if (!buffer)
{
TIFFClose(image);
caml_failwith("allocating TIFF buffer failed");
}


if (!(TIFFReadRGBAImageOriented(image, width, height, buffer, ORIENTATION_TOPLEFT, 0))) {
if (!(TIFFReadRGBAImageOriented(image, width, height, buffer, ORIENTATION_TOPLEFT, 0)))
{
TIFFClose(image);
caml_failwith("reading input file failed");
}
Expand Down
26 changes: 13 additions & 13 deletions src/Antialiasing.re
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ module MakeAntialiasing = (IO1: ImageIO.ImageIO, IO2: ImageIO.ImageIO) => {
let x1 = min(x + 1, baseImg.width - 1);
let y1 = min(y + 1, baseImg.height - 1);

let minAdjacientDelta = ref(0.0);
let maxAdjacientDelta = ref(0.0);
let minSiblingDelta = ref(0.0);
let maxSiblingDelta = ref(0.0);

let minAdjacientDeltaCoord = ref((0, 0));
let maxAdjacientDeltaCoord = ref((0, 0));
let minSiblingDeltaCoord = ref((0, 0));
let maxSiblingDeltaCoord = ref((0, 0));

let zeroes = ref(x == x0 || x == x1 || y == y0 || y == y1 ? 1 : 0);

Expand All @@ -65,12 +65,12 @@ module MakeAntialiasing = (IO1: ImageIO.ImageIO, IO2: ImageIO.ImageIO) => {
adjacentColor,
);

if (delta < minAdjacientDelta^) {
minAdjacientDelta := delta;
minAdjacientDeltaCoord := (adj_x, adj_y);
} else if (delta > maxAdjacientDelta^) {
maxAdjacientDelta := delta;
maxAdjacientDeltaCoord := (adj_x, adj_y);
if (delta < minSiblingDelta^) {
minSiblingDelta := delta;
minSiblingDeltaCoord := (adj_x, adj_y);
} else if (delta > maxSiblingDelta^) {
maxSiblingDelta := delta;
maxSiblingDeltaCoord := (adj_x, adj_y);
};
};
};
Expand All @@ -80,13 +80,13 @@ module MakeAntialiasing = (IO1: ImageIO.ImageIO, IO2: ImageIO.ImageIO) => {
// if we found more than 2 equal siblings or
// there are no darker pixels among the siblings or
// there are no brighter pixels among the siblings it's not anti-aliasing
if (zeroes^ >= 3 || minAdjacientDelta^ == 0.0 || maxAdjacientDelta^ == 0.0) {
if (zeroes^ >= 3 || minSiblingDelta^ == 0.0 || maxSiblingDelta^ == 0.0) {
false;
} else {
// if either the darkest or the brightest pixel has 3+ equal siblings in both images
// (definitely not anti-aliased), this pixel is anti-aliased
let (minX, minY) = minAdjacientDeltaCoord^;
let (maxX, maxY) = maxAdjacientDeltaCoord^;
let (minX, minY) = minSiblingDeltaCoord^;
let (maxX, maxY) = maxSiblingDeltaCoord^;
(
hasManySiblingsWithSameColor(
~x=minX,
Expand Down
Loading

0 comments on commit cba7252

Please sign in to comment.