Skip to content

Commit

Permalink
refactor(pixel-io-netpbm): minor changes (rename types)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 4, 2021
1 parent 4fc248a commit 7bd491d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/pixel-io-netpbm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ pbm.parseHeader(src)
// }

const img = pbm.read(src);
// PackedBuffer {
// IntBuffer {
// width: 12,
// height: 8,
// format: [Object],
// pixels: Uint8Array(96) [
// data: Uint8Array(96) [
// 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
// 255, 255, 255, 0, 0, 255, 255, 255, 255, 255, 0, 255,
// 255, 255, 0, 0, 0, 0, 255, 255, 255, 0, 0, 255,
Expand Down
20 changes: 10 additions & 10 deletions packages/pixel-io-netpbm/src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { unsupported } from "@thi.ng/errors/unsupported";
import { GRAY16 } from "@thi.ng/pixel/format/gray16";
import { GRAY8 } from "@thi.ng/pixel/format/gray8";
import { RGB888 } from "@thi.ng/pixel/format/rgb888";
import { packedBuffer } from "@thi.ng/pixel/packed";
import { intBuffer } from "@thi.ng/pixel/int";

const isLinebreak = (c: number) => c === 0xa;

Expand Down Expand Up @@ -73,7 +73,7 @@ export const parseHeader = (src: Uint8Array) => {

/**
* Takes a PBM/PGM/PPM file as byte array and parses it into a
* {@link @thi.ng/pixel#PackedBuffer} of corresponding format.
* {@link @thi.ng/pixel#IntBuffer} of corresponding format.
*
* @remarks
* Depending on header information, the following rules apply:
Expand Down Expand Up @@ -106,7 +106,7 @@ export const read = (src: Uint8Array) => {

/**
* Reads pixels from given 1bit PBM file byte buffer, starting at index `i` and
* returns {@link @thi.ng/pixel#PackedBuffer} in `GRAY8` format (due to current
* returns {@link @thi.ng/pixel#IntBuffer} in `GRAY8` format (due to current
* lack of true 1bit format).
*
* @param src
Expand All @@ -120,7 +120,7 @@ export const readPBM = (
width: number,
height: number
) => {
const buf = packedBuffer(width, height, GRAY8);
const buf = intBuffer(width, height, GRAY8);
const data = buf.data;
const w1 = width - 1;
for (let y = 0, j = 0; y < height; y++) {
Expand All @@ -134,7 +134,7 @@ export const readPBM = (

/**
* Reads pixels from given 8bit PGM file byte buffer, starting at index `i` and
* returns {@link @thi.ng/pixel#PackedBuffer} in `GRAY8` format. If needed,
* returns {@link @thi.ng/pixel#IntBuffer} in `GRAY8` format. If needed,
* pixel values are rescaled given `max` value defined in PGM header (MUST
* be <= 0xff).
*
Expand All @@ -154,7 +154,7 @@ export const readPGM8 = (
height: number,
max = 0xff
) => {
const buf = packedBuffer(width, height, GRAY8);
const buf = intBuffer(width, height, GRAY8);
const data = buf.data;
if (max === 0xff) {
data.set(src.subarray(i));
Expand All @@ -169,7 +169,7 @@ export const readPGM8 = (

/**
* Reads pixels from given 16bit PGM file byte buffer, starting at index `i` and
* returns {@link @thi.ng/pixel#PackedBuffer} in `GRAY16` format. Pixel values
* returns {@link @thi.ng/pixel#IntBuffer} in `GRAY16` format. Pixel values
* are rescaled given `max` value defined in PGM header (MUST be <= 0xffff).
*
* @remarks
Expand All @@ -188,7 +188,7 @@ export const readPGM16 = (
height: number,
max = 0xffff
) => {
const buf = packedBuffer(width, height, GRAY16);
const buf = intBuffer(width, height, GRAY16);
const data = buf.data;
max = 0xffff / max;
for (let j = 0, n = data.length; j < n; i += 2, j++) {
Expand All @@ -199,7 +199,7 @@ export const readPGM16 = (

/**
* Reads pixels from given 24bit PPM file byte buffer, starting at index `i` and
* returns {@link @thi.ng/pixel#PackedBuffer} in `RGB888` format. Color channel
* returns {@link @thi.ng/pixel#IntBuffer} in `RGB888` format. Color channel
* values are rescaled given `max` value defined in PGM header (MUST be <=
* 0xff).
*
Expand All @@ -219,7 +219,7 @@ export const readPPM = (
height: number,
max = 0xff
) => {
const buf = packedBuffer(width, height, RGB888);
const buf = intBuffer(width, height, RGB888);
const data = buf.data;
assert(max <= 0xff, `unsupported max value: ${max}`);
if (max === 0xff) {
Expand Down
22 changes: 11 additions & 11 deletions packages/pixel-io-netpbm/src/write.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { PackedBuffer } from "@thi.ng/pixel";
import type { IntBuffer } from "@thi.ng/pixel";
import { GRAY16 } from "@thi.ng/pixel/format/gray16";

const formatComments = (
comments: string[] = ["generated by @thi.ng/pixel-io-netpbm"]
) => comments.map((x) => `# ${x}`).join("\n");

/**
* Initializes byte array & PBM header for given {@link PackedBuffer} and format
* Initializes byte array & PBM header for given {@link IntBuffer} and format
* details.
*
* @param magic
Expand All @@ -20,7 +20,7 @@ const initHeader = (
magic: string,
limits: number,
size: number,
buf: PackedBuffer,
buf: IntBuffer,
comments?: string[]
) => {
const { width, height } = buf;
Expand All @@ -35,15 +35,15 @@ const initHeader = (
};

/**
* Converts a {@link PackedBuffer} into a 1bit PBM byte array (binary format).
* Converts a {@link IntBuffer} into a 1bit PBM byte array (binary format).
*
* @remarks
* Reference: http://netpbm.sourceforge.net/doc/pbm.html
*
* @param buf
* @param comments
*/
export const asPBM = (buf: PackedBuffer, comments?: string[]) => {
export const asPBM = (buf: IntBuffer, comments?: string[]) => {
const { data, width, height } = buf;
const { dest, start, abgr } = initHeader(
"P4",
Expand All @@ -69,7 +69,7 @@ export const asPBM = (buf: PackedBuffer, comments?: string[]) => {
};

/**
* Converts a {@link PackedBuffer} into a 8bit grayscale PGM byte array (binary
* Converts a {@link IntBuffer} into a 8bit grayscale PGM byte array (binary
* format).
*
* @remarks
Expand All @@ -78,7 +78,7 @@ export const asPBM = (buf: PackedBuffer, comments?: string[]) => {
* @param buf
* @param comments
*/
export const asPGM = (buf: PackedBuffer, comments?: string[]) => {
export const asPGM = (buf: IntBuffer, comments?: string[]) => {
const { data, width, height } = buf;
const { dest, start, abgr } = initHeader(
"P5",
Expand All @@ -94,7 +94,7 @@ export const asPGM = (buf: PackedBuffer, comments?: string[]) => {
};

/**
* Converts a {@link PackedBuffer} into a 16bit grayscale PGM byte array (binary
* Converts a {@link IntBuffer} into a 16bit grayscale PGM byte array (binary
* format).
*
* @remarks
Expand All @@ -103,7 +103,7 @@ export const asPGM = (buf: PackedBuffer, comments?: string[]) => {
* @param buf
* @param comments
*/
export const asPGM16 = (buf: PackedBuffer, comments?: string[]) => {
export const asPGM16 = (buf: IntBuffer, comments?: string[]) => {
if (buf.format !== GRAY16) buf = buf.as(GRAY16);
const { data, width, height } = buf;
const { dest, start } = initHeader(
Expand All @@ -121,15 +121,15 @@ export const asPGM16 = (buf: PackedBuffer, comments?: string[]) => {
};

/**
* Converts a {@link PackedBuffer} into a 24bit PPM byte array (binary format).
* Converts a {@link IntBuffer} into a 24bit PPM byte array (binary format).
*
* @remarks
* Reference: http://netpbm.sourceforge.net/doc/ppm.html
*
* @param buf
* @param comments
*/
export const asPPM = (buf: PackedBuffer, comments?: string[]) => {
export const asPPM = (buf: IntBuffer, comments?: string[]) => {
const { data, width, height } = buf;
const { dest, start, abgr } = initHeader(
"P6",
Expand Down
12 changes: 6 additions & 6 deletions packages/pixel-io-netpbm/test/read.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { group } from "@thi.ng/testament";
import * as assert from "assert";
import { parseHeader, read } from "../src/index.js"
import { parseHeader, read } from "../src/index.js";

group("pixel-io-netpbm", {
"parse header": () => {
Expand Down Expand Up @@ -28,7 +28,7 @@ group("pixel-io-netpbm", {
"read 1bit": () => {
// prettier-ignore
assert.deepStrictEqual(
read(new Uint8Array([0x50, 0x34, 0x0a, 0x31, 0x32, 0x20, 0x32, 0x0a, 0xff, 0xff, 0xaa, 0x55])).pixels,
read(new Uint8Array([0x50, 0x34, 0x0a, 0x31, 0x32, 0x20, 0x32, 0x0a, 0xff, 0xff, 0xaa, 0x55])).data,
new Uint8Array([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00
Expand All @@ -43,7 +43,7 @@ group("pixel-io-netpbm", {
0x50, 0x35, 0x0a, 0x34, 0x20, 0x34, 0x0a, 0x31, 0x35, 0x0a,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
])).pixels,
])).data,
new Uint8Array([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
Expand All @@ -57,7 +57,7 @@ group("pixel-io-netpbm", {
read(new Uint8Array([
0x50, 0x35, 0x0a, 0x32, 0x20, 0x32, 0x0a, 0x32, 0x35, 0x35, 0x0a,
0x00, 0x44, 0x88, 0xff
])).pixels,
])).data,
new Uint8Array([0x00, 0x44, 0x88, 0xff])
);
},
Expand All @@ -68,7 +68,7 @@ group("pixel-io-netpbm", {
read(new Uint8Array([
0x50, 0x35, 0x0a, 0x32, 0x20, 0x32, 0x0a, 0x31, 0x30, 0x32, 0x33, 0x0a,
0x03, 0xff, 0x01, 0x00, 0x02, 0xff, 0x00, 0xff
])).pixels,
])).data,
new Uint16Array([0xffff, 0x400f, 0xbfef, 0x3fcf])
);
},
Expand All @@ -80,7 +80,7 @@ group("pixel-io-netpbm", {
0x50, 0x36, 0x0a, 0x32, 0x0a, 0x32, 0x0a, 0x32, 0x35, 0x35, 0x0a,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc
])).pixels,
])).data,
new Uint32Array([0x112233, 0x445566, 0x778899, 0xaabbcc])
);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/pixel-io-netpbm/tpl.readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ pbm.parseHeader(src)


const img = pbm.read(src);
// PackedBuffer {
// IntBuffer {
// width: 12,
// height: 8,
// format: [Object],
// pixels: Uint8Array(96) [
// data: Uint8Array(96) [
// 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
// 255, 255, 255, 0, 0, 255, 255, 255, 255, 255, 0, 255,
// 255, 255, 0, 0, 0, 0, 255, 255, 255, 0, 0, 255,
Expand Down

0 comments on commit 7bd491d

Please sign in to comment.