# PNG implementation in Crystal

**URL:** <https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652>\
**Category:** Community\
**Created:** [February 28, 2024, 4:12pm UTC](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652 "2024-02-28T16:12:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SleepingInsomniac](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sleepinginsomniac/32/1442_2.png) [@SleepingInsomniac](https://forum.crystal-lang.org/u/SleepingInsomniac)\
**Post date:** [February 28, 2024, 4:12pm UTC](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652/1 "2024-02-28T16:12:54Z")

</div>

Hi everyone, I wanted to share my PNG implementation in crystal: [GitHub - SleepingInsomniac/png: PNG implementation in Crystal](https://github.com/SleepingInsomniac/png). I started working on it to support another project since I didn’t see an existing project with support for all the bit depths, color types, paletted images etc… It was a pretty fun learning experience, and I’m wondering what others think.

---

<div class="post-metadata">

**Author:** ![stakach](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stakach/32/58_2.png) [@stakach](https://forum.crystal-lang.org/u/stakach)\
**Post date:** [February 29, 2024, 11:23pm UTC](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652/2 "2024-02-29T23:23:10Z")

</div>

Looks like an awesome project!

I’ve been using [stumpy png](https://github.com/stumpycr/stumpy_png) - mostly for outputting the results of neural networks in specs  
I’d be interested in some performance comparisons. Stumpy feels quite slow to me (but still super useful)

I’ve also implemented a bunch of [resizing and cropping algorithms](https://github.com/spider-gazelle/stumpy_resize) you might find useful in your project

---

<div class="post-metadata">

**Author:** ![SleepingInsomniac](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sleepinginsomniac/32/1442_2.png) [@SleepingInsomniac](https://forum.crystal-lang.org/u/SleepingInsomniac)\
**Post date:** [March 1, 2024, 1:37am UTC](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652/3 "2024-03-01T01:37:12Z")

</div>

Thanks! I’ll take a look at those. I am planning on adding some additional methods like resizing and rotation. I have another project which is a simple SDL game engine with a some 2d drawing functions like Bézier curves, shape filling, etc. so I thought I might extract those to a separate shard and make it integrate nicely with the PNG library here.

---

<div class="post-metadata">

**Author:** ![tsornson](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/tsornson/32/1651_2.png) [@tsornson](https://forum.crystal-lang.org/u/tsornson)\
**Post date:** [March 2, 2024, 5:21am UTC](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652/4 "2024-03-02T05:21:12Z")

</div>

Nice!!! I just updated my [cr-image](https://github.com/Vici37/cr-image) shard to use your crystal native implementation rather than the previous `libspng` version for the png format 😁 Performance wise, it’s about the same in timeliness, but `libspng` has a better memory footprint (see [benchmark](https://github.com/Vici37/cr-image/blob/master/BENCHMARKS.md#file-writing)). Easily close enough that I prefer the crystal native implementation instead.

cr-image (like [Pluto](https://github.com/phenopolis/pluto), which it started as a fork from) tries to prioritize performance. If Stumpy is feeling sluggish, maybe take a look at cr-image (or Pluto) instead? Ideally I want cr-image to be as feature-full for image processing as I can, partly as a great utility, and partly because I’ve been having a ton of fun learning about, well, image processing 🙂 cr-image also contains some utility methods to convert from / to stumpy, if needed.

---

<div class="post-metadata">

**Author:** ![drmidnight](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/drmidnight/32/2266_2.png) [@drmidnight](https://forum.crystal-lang.org/u/drmidnight)\
**Post date:** [March 12, 2024, 2:24pm UTC](https://forum.crystal-lang.org/t/png-implementation-in-crystal/6652/5 "2024-03-12T14:24:24Z")

</div>

The bottleneck, especially in stumpypng, is the zlib call. There isn’t much you can do about speeding that up. I spent a day trying to eek more performance out of stumpy for a work project and while I was able to trim some fat, there is no getting around the zlib usage.

I noticed this does just about the same thing as stumpy in that regard. I’ll play around with it and see if there is any performance improvements to how it’s writing.
