npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

png-to-json

v1.0.6

Published

Parse PNG files into JSON human readable structures

Downloads

40

Readme

Png to JSON Parser

NoDependencies Browser Node

Tiny library to parse Png files (or uint8Arrays containing Png data structures) into JSON human readable Objects. It extracts all the data from the IHDR chunk, and creates an array of chunks with it's relative byte blocks.

Accepts different data sources :

  • HTML Input Elements : Handles automatically the File change events (async)
  • File references : From HTML Input Element (async)
  • Uint8Array : Typed Array containing the data (sync)

Usage example (Node):

    let fileData = fs.readFileSync('./test.png');
    let pngJSON = pngToJSON(fileData);
    console.log(pngJSON);

Syntax

Async modes (only browser) :

pngToJSON( HTMLInputElement, callback );
  • HTMLInputElement: Reference to the DOM File Input Element
  • callback: Function to receive the results of the conversion. It accepts two arguments : JSON PNG Object and Source File Info.
pngToJSON( FileReference, callback )
  • FileReference: Reference to the file returned from the File Input Element ( InputElement.files[x] )
  • callback: Function to receive the results of the conversion. It accepts two arguments : JSON PNG Object and Source File Info.

Sync mode (Node+browser):

pngToJSON( Uint8ArrayData )
  • Uint8ArrayData:Uint8Array typed Array containing the png file data

Returns : JSON representation of the png file contents.

JSON Structure

The returned JSON Object i strutured in thw following way (most keys are self explanatory)

	pngJSON{
    	bitDepth: Integer,
        chunks: Integer, 				// total count
        colorType: Integer,
        colorTypeString: String, 		// string representation
        compressionMethod: Integer,
        data: Uint8Array[...], 			// png file data
        filterMethod: Integer,
        height: Integer,				// image height
        interlaceMethod: Integer,
        signature: Uint8Array[...],		// firat 8 bytes
        width: Integer,					// image width
        chunk: [
        	[0] : {
                data: Uint8Array[...], 	// chunk data block
                raw : Uint8Array[...],  // full chunk block
                id: Integer,
                isCritical: Boolean,
                isPublic: Boolean,
                chunkLength: Integer, 	// chunk length
                dataLength: Integer, 	// chunk data length
                offset: Integer, 		// chunk start offset
                type: String, 			// chunk type
            },
            [1] : {...},
            [2] : {...},
			(...)
        ]
    }

Note : The resulting <pngJSON>.chunk[x].data byteArrays, do not contain the the full chunk data. The bytes corresponding to the length block , type block, and CRC block, are stripped from it. It only contains the data block from each chunk.

CHUNK BYTE STRUCTURE : 

<------------------------ chunk length -------------------------->
Chunk Offset                   Chunk Offset+8
├──────────────────┬───────────┼──────────────────────┬──────────┐
│ *DataLength (4b) │ Type (4b) │ Data (*DataLength b) │ CRC (4b) │
└──────────────────┴───────────┴──────────────────────┴──────────┘
<--------- stripped ----------->                      <-stripped->

Use <pngJSON>.chunk[x].raw instead, to get all the chunk data.

Package distribution :

In browser enviroment you can include this library using the jsdelivr CDN ( window.pngToJSON() is created autmatically ) ...

<script src='https://cdn.jsdelivr.net/gh/colxi/png-to-json@latest/src/png-to-json.min.js'></script>

If you are in the NodeJs enviroment, can install the package via:

$ npm install png-to-json --save

Git repository available too :

$ git clone https://github.com/colxi/png-to-json.git

The package can be imported using ES6 import in Browser

import '<libraryPath>/src/png-to-json.js';
// window.pngToJSON() is created autmatically

Licence

GPL 3