Clojure(Script) library for reading and writing Netpbm format. Supports plain-text formats: p1 (.pbm), p2 (.pgm) and p3 (.ppm).
Notes on the state of the library:
Good explanation of the format and the resources that are used as guides for this library can be found here and here.
clj-pnm is built using Leiningen:
lein clean
lein jar
Install clj-kondo and create the lint cache:
mkdir -p .clj-kondo
clj-kondo --lint "$(lein classpath)" --dependencies --parallel --copy-configs
clj-pnm is written as a cljc file so it is compatible with some Clojure dialects.
clj-pnm can be used as a Babashka dependency.
To test the Babashka compatibility, install it and run:
bb clean
bb test
See bb.edn for details on how the tests are run.
clj-pnm can be used as a ClojureScript dependency, and shadow-cljs is used to test the compatibility. First, install NodeJS and NPM (fnm is recommended).
Prepare the test environment:
bb clean # this is the fastest
npm install
To test the compatibility with Node:
npx shadow-cljs compile test-node
To test the compatibility with a (default) browser:
npx shadow-cljs watch test-browser
This will start a server, and to check the test results go to http://localhost:8021/.
Karma is used to run the tests in a headless browser:
npx shadow-cljs compile test-karma
npx shadow-cljs start karma-test.conf.js --single-run
See the test configurations here.
Since the library is light, it can be used from Squint. Custom test runner is needed, but the library source is the same.
The test the Squint compatibility:
npx squint-cljs compile src/clj_pnm/core.cljc
npx squint-cljs run test/squint/clj_pnm/clj_pnm.squint_read_test.cljs
Create the jar:
lein clean
lein jar
Install/deploy locally:
lein install
To deploy the library to Clojars:
lein deploy clojars
These actions are unified in Leiningen release process:
lein release :patch
The library is available on Clojars. Add it to your project as a dependency.
deps.edn:
org.theparanoidtimes/clj-pnm {:mvn/version "0.3.0"}
project.clj:
[org.theparanoidtimes/clj-pnm "0.3.0"]
require the library:
(require '[clj-pnm.core :as pnm])
=> nil
Read the pbm format:
(def pbm
"P1
2
2
1 1
1 1")
=> #'user/pbm
(pnm/read-pnm pbm)
=> {:type :p1, :width 2, :height 2, :map [[1 1] [1 1]]}
Read the pgm format:
(def pgm
"P2
3
2
4
# pgm-comment
1 2 3
4 3 2")
=> #'user/pgm
(pnm/read-pnm pgm)
=> {:type :p2, :width 3, :height 2, :max-value 4, :map [[1 2 3] [4 3 2]] :comments ("pgm-comment")}
Read the ppm format:
(def ppm
"P3
2
2
255
# ppm-comment
255 0 255 128 52 123
0 0 0 45 45 45")
=> #'user/ppm
(pnm/read-pnm ppm)
=> {:type :p3
:width 2
:height 2
:max-value 255
:map [[(255 0 255) (128 52 123)]
[(0 0 0) (45 45 45)]]
:comments ("ppm-comment")} ; prettified
You can extract comments separately if necessary:
(def pbm-with-comments
"P1
1
1
# A comment
# Another comment
1")
=> #'user/pbm-with-comments
(pnm/get-comments (pnm/read-lines pbm-with-comments))
=> ("A comment" "Another comment")
Some files have in-line comments in the header part. They are handled in the same way:
(def pbm-with-in-line-comments
"P1
1 # this is the width
1
# A comment
# Another comment
1")
=> #'user/pbm-with-in-line-comments
(pnm/read-pnm pbm-with-in-line-comments)
=> {:type :p1, :width 1, :height 1, :map [[1]] :comments ("1 this is the width" "A comment" "Another comment")}
(def pbm-with-in-line-comments
"P1
1
1 # this is the height
# A comment
# Another comment
1")
=> #'user/pbm-with-in-line-comments
(pnm/get-comments (pnm/read-lines pbm-with-in-line-comments))
=> ("1 this is the width" "A comment" "Another comment")
Clojure version of clj-pnm can work with files. To read a pnm file
pass it to the read-pnm function:
(require '[clojure.java.io :as io])
=> nil
(slurp (io/file "test.pgm"))
=> P2
3
2
4
1 2 3
4 5 6 ; prettified
(pnm/read-pnm (io/file "test.pgm"))
=> {:type :p2, :width 3, :height 2, :max-value 4, :map [[1 2 3] [4 5 6]]}
Writing a file is just a matter of passing a map like the ones explained above. It can include comments which are always written after the header part.
(pnm/write-pnm {:type :p1 :width 1 :height 1 :map [[1]]} "out.pbm")
=> ...out.pbm file...
(slurp *1)
=> "P1
1
1
1" ;prettified
(pnm/write-pnm
{:type :p3 :width 1 :height 1 :max-value 255 :map [[1]] :comments '("A comment" "Another comment")}
"out.ppm")
=> ...out.ppm file...
(slurp *1)
=> "P3
1
1
255
# A comment
# Another comment
255 255 255" ; prettified
See the API documentation here.
For any suggestions, bug reports, ideas and patches please send a plain-text e-mail to ~tpt/tpt-dev@lists.sr.ht.
Copyright (C) 2018-2026. Dejan Josifović, The Paranoid Times
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Can you improve this documentation? These fine people already did:
Dejan Josifović, Дејан Јосифовић & JosifovicEdit on sourcehut
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |