Skip to content

Commit

Permalink
refactor!: use camelcase for acronyms
Browse files Browse the repository at this point in the history
close #17
  • Loading branch information
opatiny authored and targos committed Oct 8, 2021
1 parent 4ba778d commit 3f485b3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 60 deletions.
42 changes: 21 additions & 21 deletions src/PNGDecoder.ts → src/PngDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
InterlaceMethod,
} from './internalTypes';
import {
IDecodedPNG,
DecodedPng,
DecoderInputType,
IPNGDecoderOptions,
PNGDataArray,
PngDecoderOptions,
PngDataArray,
IndexedColors,
BitDepth,
} from './types';
Expand All @@ -24,10 +24,10 @@ const uint16 = new Uint16Array([0x00ff]);
const uint8 = new Uint8Array(uint16.buffer);
const osIsLittleEndian = uint8[0] === 0xff;

export default class PNGDecoder extends IOBuffer {
export default class PngDecoder extends IOBuffer {
private _checkCrc: boolean;
private _inflator: Inflator;
private _png: IDecodedPNG;
private _png: DecodedPng;
private _end: boolean;
private _hasPalette: boolean;
private _palette: IndexedColors;
Expand All @@ -36,7 +36,7 @@ export default class PNGDecoder extends IOBuffer {
private _interlaceMethod: InterlaceMethod;
private _colorType: number;

public constructor(data: DecoderInputType, options: IPNGDecoderOptions = {}) {
public constructor(data: DecoderInputType, options: PngDecoderOptions = {}) {
super(data);
const { checkCrc = false } = options;
this._checkCrc = checkCrc;
Expand All @@ -61,7 +61,7 @@ export default class PNGDecoder extends IOBuffer {
this.setBigEndian();
}

public decode(): IDecodedPNG {
public decode(): DecodedPng {
this.decodeSignature();
while (!this._end) {
this.decodeChunk();
Expand Down Expand Up @@ -263,7 +263,7 @@ export default class PNGDecoder extends IOBuffer {
}
}

private decodeInterlaceNull(data: PNGDataArray): void {
private decodeInterlaceNull(data: PngDataArray): void {
const height = this._png.height;
const bytesPerPixel = (this._png.channels * this._png.depth) / 8;
const bytesPerLine = this._png.width * bytesPerPixel;
Expand Down Expand Up @@ -332,8 +332,8 @@ export default class PNGDecoder extends IOBuffer {
}

function unfilterNone(
currentLine: PNGDataArray,
newLine: PNGDataArray,
currentLine: PngDataArray,
newLine: PngDataArray,
bytesPerLine: number,
): void {
for (let i = 0; i < bytesPerLine; i++) {
Expand All @@ -342,8 +342,8 @@ function unfilterNone(
}

function unfilterSub(
currentLine: PNGDataArray,
newLine: PNGDataArray,
currentLine: PngDataArray,
newLine: PngDataArray,
bytesPerLine: number,
bytesPerPixel: number,
): void {
Expand All @@ -358,9 +358,9 @@ function unfilterSub(
}

function unfilterUp(
currentLine: PNGDataArray,
newLine: PNGDataArray,
prevLine: PNGDataArray,
currentLine: PngDataArray,
newLine: PngDataArray,
prevLine: PngDataArray,
bytesPerLine: number,
): void {
let i = 0;
Expand All @@ -377,9 +377,9 @@ function unfilterUp(
}

function unfilterAverage(
currentLine: PNGDataArray,
newLine: PNGDataArray,
prevLine: PNGDataArray,
currentLine: PngDataArray,
newLine: PngDataArray,
prevLine: PngDataArray,
bytesPerLine: number,
bytesPerPixel: number,
): void {
Expand All @@ -404,9 +404,9 @@ function unfilterAverage(
}

function unfilterPaeth(
currentLine: PNGDataArray,
newLine: PNGDataArray,
prevLine: PNGDataArray,
currentLine: PngDataArray,
newLine: PngDataArray,
prevLine: PngDataArray,
bytesPerLine: number,
bytesPerPixel: number,
): void {
Expand Down
32 changes: 17 additions & 15 deletions src/PNGEncoder.ts → src/PngEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import {
} from './internalTypes';
import {
DeflateFunctionOptions,
IPNGEncoderOptions,
IImageData,
IDecodedPNG,
PNGDataArray,
PngEncoderOptions,
ImageData,
DecodedPng,
PngDataArray,
BitDepth,
} from './types';

const defaultZlibOptions: DeflateFunctionOptions = {
level: 3,
};

export default class PNGEncoder extends IOBuffer {
private _png: IDecodedPNG;
export default class PngEncoder extends IOBuffer {
private _png: DecodedPng;
private _zlibOptions: DeflateFunctionOptions;
private _colorType: ColorType;

public constructor(data: IImageData, options: IPNGEncoderOptions = {}) {
public constructor(data: ImageData, options: PngEncoderOptions = {}) {
super();
this._colorType = ColorType.UNKNOWN;
this._zlibOptions = Object.assign({}, defaultZlibOptions, options.zlib);
Expand Down Expand Up @@ -74,7 +74,7 @@ export default class PNGEncoder extends IOBuffer {
}

// https://www.w3.org/TR/PNG/#11IDAT
private encodeIDAT(data: PNGDataArray): void {
private encodeIDAT(data: PngDataArray): void {
this.writeUint32(data.length);

this.writeChars('IDAT');
Expand Down Expand Up @@ -105,9 +105,9 @@ export default class PNGEncoder extends IOBuffer {
this.encodeIDAT(compressed);
}

private _checkData(data: IImageData): IDecodedPNG {
private _checkData(data: ImageData): DecodedPng {
const { colorType, channels, depth } = getColorType(data);
const png: IDecodedPNG = {
const png: DecodedPng = {
width: checkInteger(data.width, 'width'),
height: checkInteger(data.height, 'height'),
channels: channels,
Expand Down Expand Up @@ -146,9 +146,11 @@ function checkInteger(value: number, name: string): number {
throw new TypeError(`${name} must be a positive integer`);
}

function getColorType(
data: IImageData,
): { channels: number; depth: BitDepth; colorType: ColorType } {
function getColorType(data: ImageData): {
channels: number;
depth: BitDepth;
colorType: ColorType;
} {
const { channels = 4, depth = 8 } = data;
if (channels !== 4 && channels !== 3 && channels !== 2 && channels !== 1) {
throw new RangeError(`unsupported number of channels: ${channels}`);
Expand Down Expand Up @@ -178,7 +180,7 @@ function getColorType(
}

function writeDataBytes(
data: PNGDataArray,
data: PngDataArray,
newData: IOBuffer,
slotsPerLine: number,
offset: number,
Expand All @@ -190,7 +192,7 @@ function writeDataBytes(
}

function writeDataUint16(
data: PNGDataArray,
data: PngDataArray,
newData: IOBuffer,
slotsPerLine: number,
offset: number,
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/decode.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'fs';
import { join } from 'path';

import { decode, IPNGDecoderOptions, IDecodedPNG } from '../index';
import { decode, PngDecoderOptions, DecodedPng } from '../index';

describe('decode', () => {
it('BW2x2', () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('decode', () => {
});
});

function loadAndDecode(img: string, options?: IPNGDecoderOptions): IDecodedPNG {
function loadAndDecode(img: string, options?: PngDecoderOptions): DecodedPng {
return decode(readFileSync(join(__dirname, '../../img', img)), options);
}

Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import PNGDecoder from './PNGDecoder';
import PNGEncoder from './PNGEncoder';
import PngDecoder from './PngDecoder';
import PngEncoder from './PngEncoder';
import {
DecoderInputType,
IPNGDecoderOptions,
IDecodedPNG,
IImageData,
IPNGEncoderOptions,
PngDecoderOptions,
DecodedPng,
ImageData,
PngEncoderOptions,
} from './types';

export * from './types';

function decodePNG(
function decodePng(
data: DecoderInputType,
options?: IPNGDecoderOptions,
): IDecodedPNG {
const decoder = new PNGDecoder(data, options);
options?: PngDecoderOptions,
): DecodedPng {
const decoder = new PngDecoder(data, options);
return decoder.decode();
}

function encodePNG(png: IImageData, options?: IPNGEncoderOptions): Uint8Array {
const encoder = new PNGEncoder(png, options);
function encodePng(png: ImageData, options?: PngEncoderOptions): Uint8Array {
const encoder = new PngEncoder(png, options);
return encoder.encode();
}

export { decodePNG as decode, encodePNG as encode };
export { decodePng as decode, encodePng as encode };
18 changes: 9 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { DeflateFunctionOptions } from 'pako';

export { DeflateFunctionOptions };

export type PNGDataArray = Uint8Array | Uint8ClampedArray | Uint16Array;
export type PngDataArray = Uint8Array | Uint8ClampedArray | Uint16Array;

export type DecoderInputType = IOBuffer | ArrayBufferLike | ArrayBufferView;

export type BitDepth = 1 | 2 | 4 | 8 | 16;

export interface IPNGResolution {
export interface PngResolution {
/**
* Pixels per unit, X axis
*/
Expand All @@ -35,30 +35,30 @@ export enum ResolutionUnitSpecifier {
METRE = 1,
}

export interface IImageData {
export interface ImageData {
width: number;
height: number;
data: PNGDataArray;
data: PngDataArray;
depth?: BitDepth;
channels?: number;
}

export interface IDecodedPNG {
export interface DecodedPng {
width: number;
height: number;
data: PNGDataArray;
data: PngDataArray;
depth: BitDepth;
channels: number;
text: { [key: string]: string };
resolution?: IPNGResolution;
resolution?: PngResolution;
palette?: IndexedColors;
}

export interface IPNGDecoderOptions {
export interface PngDecoderOptions {
checkCrc?: boolean;
}

export interface IPNGEncoderOptions {
export interface PngEncoderOptions {
zlib?: DeflateFunctionOptions;
}

Expand Down

0 comments on commit 3f485b3

Please sign in to comment.