Skip to content

Commit

Permalink
Added MagickImageInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Mar 16, 2024
1 parent 8263de8 commit 39d611a
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export * from './magick-colors';
export * from './magick-error';
export * from './magick-format-info';
export * from './magick-image-collection';
export * from './magick-image-info';
export * from './magick-image';
export * from './magick';
export * from './native-instance';
Expand Down
121 changes: 121 additions & 0 deletions src/magick-image-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { ByteArray } from "./byte-array";
import { ColorSpace } from "./enums/color-space";
import { CompressionMethod } from "./enums/compression-method";
import { Density } from "./types/density";
import { Interlace } from "./enums/interlace";
import { MagickFormat } from "./enums/magick-format";
import { MagickReadSettings } from "./settings/magick-read-settings";
import { MagickImage } from "./magick-image";

export interface IMagickImageInfo {
/**
* Gets the color space of the image
*/
readonly colorSpace: ColorSpace;

/**
* Gets the compression method of the image
*/
readonly compression: CompressionMethod;

/**
* Gets the density of the image
*/
readonly density: Density;

/**
* Gets the format of the image.
*/
readonly format: MagickFormat;

/**
* Gets the height of the image.
*/
readonly height: number;

/**
* Gets the type of interlacing.
*/
readonly interlace: Interlace;

/**
* Gets the JPEG/MIFF/PNG compression level.
*/
readonly quality: number;

/**
* Gets the width of the image.
*/
readonly width: number;

/**
* Read single image frame.
* @param array - The byte array to read the image from.
* @param settings - The settings to use when reading the image.
*/
read(array: ByteArray, settings?: MagickReadSettings): void
}

export class MagickImageInfo implements IMagickImageInfo {
private _colorSpace: ColorSpace = ColorSpace.Undefined;
private _compression: CompressionMethod = CompressionMethod.Undefined;
private _density: Density = new Density(0, 0);
private _format: MagickFormat = MagickFormat.Unknown;
private _height: number = 0;
private _interlace: Interlace = Interlace.Undefined;
private _quality: number = 0;
private _width: number = 0;

get colorSpace(): ColorSpace {
return this._colorSpace;
}

get compression(): CompressionMethod {
return this._compression;
}

get density(): Density {
return this._density;
}

get format(): MagickFormat {
return this._format;
}

get height(): number {
return this._height;
}

get interlace(): Interlace {
return this._interlace;
}

get quality(): number {
return this._quality;
}

get width(): number {
return this._width;
}

constructor() {
}

read(array: ByteArray, settings?: MagickReadSettings): void {
MagickImage._create(image => {
image.ping(array, settings);

this._colorSpace = image.colorSpace;
this._compression = image.compression;
this._density = image.density;
this._format = image.format;
this._height = image.height;
this._interlace = image.interlace;
this._quality = image.quality;
this._width = image.width;
});
}
}
25 changes: 25 additions & 0 deletions tests/magick-image-info/constructor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { ColorSpace } from '@src/enums/color-space';
import { CompressionMethod } from '@src/enums/compression-method';
import { DensityUnit } from '@src/enums/density-unit';
import { Interlace } from '@src/enums/interlace';
import { MagickFormat } from '@src/enums/magick-format';
import { MagickImageInfo } from '@src/magick-image-info';

describe('MagickImageInfo#constructor', () => {
it('should create empty uninitialized instance', () => {
const magickImageInfo = new MagickImageInfo();
expect(magickImageInfo.colorSpace).toBe(ColorSpace.Undefined);
expect(magickImageInfo.compression).toBe(CompressionMethod.Undefined);
expect(magickImageInfo.density.x).toBe(0);
expect(magickImageInfo.density.y).toBe(0);
expect(magickImageInfo.density.units).toBe(DensityUnit.Undefined);
expect(magickImageInfo.format).toBe(MagickFormat.Unknown);
expect(magickImageInfo.height).toBe(0);
expect(magickImageInfo.interlace).toBe(Interlace.Undefined);
expect(magickImageInfo.quality).toBe(0);
expect(magickImageInfo.width).toBe(0);
});
});
27 changes: 27 additions & 0 deletions tests/magick-image-info/read.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { ColorSpace } from '@src/enums/color-space';
import { CompressionMethod } from '@src/enums/compression-method';
import { DensityUnit } from '@src/enums/density-unit';
import { Interlace } from '@src/enums/interlace';
import { MagickFormat } from '@src/enums/magick-format';
import { MagickImageInfo } from '@src/magick-image-info';
import { TestImages } from '@test/test-images';

describe('MagickImageInfo#constructor', () => {
it('should read the information of the image', () => {
const magickImageInfo = new MagickImageInfo();
magickImageInfo.read(TestImages.fujiFilmFinePixS1ProJpg.data);
expect(magickImageInfo.colorSpace).toBe(ColorSpace.sRGB);
expect(magickImageInfo.compression).toBe(CompressionMethod.JPEG);
expect(magickImageInfo.density.x).toBe(300);
expect(magickImageInfo.density.y).toBe(300);
expect(magickImageInfo.density.units).toBe(DensityUnit.PixelsPerInch);
expect(magickImageInfo.format).toBe(MagickFormat.Jpeg);
expect(magickImageInfo.height).toBe(400);
expect(magickImageInfo.interlace).toBe(Interlace.NoInterlace);
expect(magickImageInfo.quality).toBe(70);
expect(magickImageInfo.width).toBe(600);
});
});

0 comments on commit 39d611a

Please sign in to comment.