-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement physical pixel size management for PNG
This feature adds support for the 'pHYs' chunk in the PNG format specification, which governs the intended physical pixel size. The physical pixel size, often measured in DPI, affects the displayed size of the image in viewers that support this feature. A practical application of this functionality is encoding HiDPI images (such as 2x or 4x) for display on Apple Retina screens.
- Loading branch information
1 parent
18bd44f
commit 07deebf
Showing
7 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Class representing the physical pixel dimensions of a PNG image. | ||
* | ||
* @format | ||
*/ | ||
|
||
export class PngPhysicalPixelDimensions { | ||
/** | ||
* Conversion factor from meters to inches. | ||
*/ | ||
private static readonly _inchesPerM: number = 39.3701; | ||
|
||
/** | ||
* Unit specifier for unknown units. | ||
*/ | ||
public static readonly unitUnknown: number = 0; | ||
|
||
/** | ||
* Unit specifier for meters. | ||
*/ | ||
public static readonly unitMeter: number = 1; | ||
|
||
/** | ||
* Pixels per unit on the X axis. | ||
*/ | ||
private _xPxPerUnit: number; | ||
|
||
/** | ||
* Gets the pixels per unit on the X axis. | ||
* @returns {number} Pixels per unit on the X axis. | ||
*/ | ||
public get xPxPerUnit(): number { | ||
return this._xPxPerUnit; | ||
} | ||
|
||
/** | ||
* Pixels per unit on the Y axis. | ||
*/ | ||
private _yPxPerUnit: number; | ||
|
||
/** | ||
* Gets the pixels per unit on the Y axis. | ||
*/ | ||
public get yPxPerUnit(): number { | ||
return this._yPxPerUnit; | ||
} | ||
|
||
/** | ||
* Unit specifier, either `unitUnknown` or `unitMeter`. | ||
*/ | ||
private _unitSpecifier: number; | ||
|
||
/** | ||
* Gets the unit specifier. | ||
*/ | ||
public get unitSpecifier(): number { | ||
return this._unitSpecifier; | ||
} | ||
|
||
/** | ||
* Constructs a dimension descriptor with the given values. | ||
* @param {number} xPxPerUnit - Pixels per unit on the X axis. | ||
* @param {number} yPxPerUnit - Pixels per unit on the Y axis. | ||
* @param {number} unitSpecifier - Unit specifier, either `unitUnknown` or `unitMeter`. | ||
*/ | ||
constructor(xPxPerUnit: number, yPxPerUnit: number, unitSpecifier: number) { | ||
this._xPxPerUnit = xPxPerUnit; | ||
this._yPxPerUnit = yPxPerUnit; | ||
this._unitSpecifier = unitSpecifier; | ||
} | ||
|
||
/** | ||
* Constructs a dimension descriptor specifying x and y resolution in dots per inch (DPI). | ||
* If `dpiY` is unspecified, `dpiX` is used for both x and y axes. | ||
* @param {number} dpiX - Dots per inch on the X axis. | ||
* @param {number} [dpiY] - Dots per inch on the Y axis. | ||
* @returns {PngPhysicalPixelDimensions} A new instance of `PngPhysicalPixelDimensions`. | ||
*/ | ||
public static fromDPI( | ||
dpiX: number, | ||
dpiY?: number | ||
): PngPhysicalPixelDimensions { | ||
const xPxPerUnit = Math.round( | ||
dpiX * PngPhysicalPixelDimensions._inchesPerM | ||
); | ||
const yPxPerUnit = Math.round( | ||
(dpiY ?? dpiX) * PngPhysicalPixelDimensions._inchesPerM | ||
); | ||
const unitSpecifier = PngPhysicalPixelDimensions.unitMeter; | ||
return new PngPhysicalPixelDimensions( | ||
xPxPerUnit, | ||
yPxPerUnit, | ||
unitSpecifier | ||
); | ||
} | ||
|
||
/** | ||
* Checks if this instance is equal to another `PngPhysicalPixelDimensions` instance. | ||
* @param {PngPhysicalPixelDimensions} other - The other instance to compare with. | ||
* @returns {boolean} `true` if the instances are equal, `false` otherwise. | ||
*/ | ||
public equals(other: PngPhysicalPixelDimensions): boolean { | ||
return ( | ||
this._xPxPerUnit === other._xPxPerUnit && | ||
this._yPxPerUnit === other._yPxPerUnit && | ||
this._unitSpecifier === other._unitSpecifier | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters