Options
All
  • Public
  • Public/Protected
  • All
Menu

@thi.ng/pixel-io-netpbm

pixel-io-netpbm

npm versionnpm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Multi-format NetPBM reader & writer support for @thi.ng/pixel.

This package can read & write binary NetPBM image formats from byte arrays/buffers to @thi.ng/pixel pixel buffers (aka PackedBuffer).

Source formatDestination formatRec. file extension(1)
1 bitGRAY8(2).pbm
2-8 bit grayscaleGRAY8.pgm
9-16 bit grayscaleGRAY16.pgm
24 bit RGBARGB8888.ppm

(1) no relevance to actual parse/export logic (2) currently no support for actual 1-bit pixel buffers

Furthermore the parseHeader() function can be used to just extract image type, size and other meta data (from comments), without parsing the full image.

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

Installation

yarn add @thi.ng/pixel-io-netpbm
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/pixel-io-netpbm?module" crossorigin></script>

// UMD
<script src="https://unpkg.com/@thi.ng/pixel-io-netpbm/lib/index.umd.js" crossorigin></script>

Package sizes (gzipped, pre-treeshake): ESM: 1.31 KB / CJS: 1.39 KB / UMD: 1.49 KB

Dependencies

API

Generated API docs

import * as pbm from "@thi.ng/pixel-io-netpbm";
import * as fs from "fs";

const src = fs.readFileSync("a.pbm");
// <Buffer 50 34 0a 23 20 67 65 6e 65 72 61 74 65 64 20 62 79...>

// parse image header data
// P4 type => 1bit bitmap
pbm.parseHeader(src)
// {
//     type: 'P4',
//     width: 12,
//     height: 8,
//     max: undefined,
//     start: 47,
//     comments: [ 'generated by @thi.ng/pixel-io-netpbm' ]
// }

const img = pbm.read(src);
// PackedBuffer {
//   width: 12,
//   height: 8,
//   format: [Object],
//   pixels: 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,
//     255,   0,   0, 255, 255,   0,   0, 255,   0,   0,   0, 255,
//     255,   0,   0,   0,   0,   0,   0, 255,   0,   0,   0, 255,
//     255,   0,   0, 255, 255,   0,   0, 255, 255,   0,   0, 255,
//     255,   0,   0, 255, 255,   0,   0, 255, 255, 255,   0, 255,
//     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
//   ]
// }

// convert to RGB image and export w/ additional meta data
// (will be stored in PBM header comments)
fs.writeFileSync(
    "a-rgb.ppm",
    pbm.asPPM(
        img.as(RGB888),
        [
            "@prefix dc: http://purl.org/dc/terms/",
            "dc:created 2021-02-08",
            "dc:creator toxi"
        ]
    )
);

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-pixel-io-netpbm,
  title = "@thi.ng/pixel-io-netpbm",
  author = "Karsten Schmidt",
  note = "https://thi.ng/pixel-io-netpbm",
  year = 2021
}

License

© 2021 Karsten Schmidt // Apache Software License 2.0

Generated using TypeDoc