stumpy_png
Interface
StumpyPNG.read(path) : Canvas
read a PNG image fileStumpyPNG.write(canvas, path, bit_depth: 16, color_type: :grayscale)
saves a canvas as a PNG image filebit_depth
is optional, valid values are8
and16
(default)color_type
is optional, valid values are:grayscale
,:grayscale_alpha
,:rgb
and:rgb_alpha
(default)StumpyPNG::PNG
, helper class to store some state while parsing PNG filesCanvas
andRGBA
from stumpy_core
Usage
Install the stumpy_png
shard
shards init
- Add the dependency to the
shard.yml
file` yaml ... dependencies: stumpy_png:
github: l3kn/stumpy_png
version: "~> 4.1.1"
...
`
shards install
Reading
require "stumpy_png"
canvas = StumpyPNG.read("foo.png")
r, g, b = canvas[0, 0].to_rgb8
puts "red=#{r}, green=#{g}, blue=#{b}"
Writing
include StumpyPNG
canvas = Canvas.new(256, 256)
(0...255).each do |x|
(0...255).each do |y|
# RGBA.from_rgb_n(values, bit_depth) is an internal helper method
# that creates an RGBA object from a rgb triplet with a given bit depth
color = RGBA.from_rgb_n(x, y, 255, 8)
canvas[x, y] = color
end
endIf you get
StumpyPNG.write(canvas, "rainbow.png")
(See examples/
for more examples)
Reading PNG files
Color Types
- [x] Grayscale
- [x] Grayscale + Alpha
- [x] RGB
- [x] RGB + Alpha
- [x] Palette
Filter Types
- [x] None
- [x] Sub
- [x] Up
- [x] Average
- [x] Paeth
Interlacing Methods
- [x] None
- [x] Adam7
Ancillary Chunks
- [ ] tRNS
- [ ] cHRM
- [ ] gAMA
- [ ] iCCP
- [ ] sBIT
- [ ] sRGB
- [ ] tEXt
- [ ] zTXt
- [ ] iTXt
- [ ] bKGD
- [ ] hIST
- [ ] pHYs
- [ ] sPLT
- [ ] tIME
Writing
- RGB with 8 or 16 bits
- RGB + Alpha with 8 or 16 bits
- Grayscale with 8 or 16 bits
- Grayscale + Alpha with 8 or 16 bits
Troubleshooting
If you run into errors like
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
make sure zlib
is installed
(Installing zlib under ubuntu).
Contributors
Thanks goes to these wonderful people (emoji key):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | <img src="https://avatars.githubusercontent.com/u/2788811?v=3" width="100px;"/><br /><sub>Chris Hobbs</sub><br /> | <img src="https://avatars.githubusercontent.com/u/209371?v=3" width="100px;"/><br /><sub>Ary Borenszweig</sub><br /> | <img src="https://avatars.githubusercontent.com/u/90345?v=3" width="100px;"/><br /><sub>Alex Muscar</sub><br /> | | :---: | :---: | :---: | <!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the all-contributors specification. Contributions of any kind welcome!