DxChain Network

DxChain Network is still in working process with the MVP currently set to launch sometime in July. According to the CTO, Wei Wang, a test chain could be launched in the next few months and its…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Understanding and decoding PNGs

With an example in TypeScript

While working on a game project in TS, we found ourselves needing a PNG decoder to create our objects metadata from pictures in a generic way.

We are coding using TS/JS in the browser, so the obvious solution is to use Canvas, but that wouldn’t be fun wouldn't it? So I decided to lookup the PNG format to create a decoder that will fit our needs.

Other than the official documentation, which can be heavy to read at first, i didn’t find much examples of how to implement that quickly, so I decided to share my experience and I hope it will help some of you getting started 😊

In my project, I used a class Color to store the file’s pixel, but for our example, i’m going to output the results as an array of Uint8Array. Each array will store the information about one pixel (RGBA). Below is an example of the result we want implemented using canvas so you can see where we’re going. The index.html file is only there to have a visual representation of what we’re doing, it may seems useless for the first example (using canvas) but it will help us later.

All of the usefull code is located in src/index.ts. This implementation isn’t the subject of our article so I won’t go into the details, however, note that the code simply loads the picture into a canvas, then uses the canvas API to extract the image data.

But I get it, they can be confusing, there is a lot of content, with each section redirecting towards another, so let’s find out where to start !

Add a comment

Related posts:

What Animals Can Teach Us About Effective Teams

Since the earliest humans started forming groups to become more effective hunters, the human species has had a keen aptitude for forming teams to complete tasks too difficult for one individual. Many…

Herbst Produkt

You have probably used one of Scot Herbst’s products before. Not just admired it in a gallery but really used it. Reclining on garden furniture, riding a bike, cutting open a box, perhaps you even…

Friend and Box.

An echoing heartbeat, pulsates at the tips of my fingers, as I carry you up the dirt-laden hill. I do not expect that the memory will ever dissipate. It may, perhaps, leave in intervals — evaporating…