Skip to content

Commit

Permalink
Merge pull request #67 from clojerl/54-image-related
Browse files Browse the repository at this point in the history
[#54] image related
  • Loading branch information
jfacorro authored Oct 23, 2023
2 parents 1510f41 + f9c674f commit cb5bb26
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ _build
.idea
*.iml
rebar3.crashdump
*~
*~
.clj-kondo/
.lsp/
Binary file added resources/pacman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 47 additions & 1 deletion src/doodler/core.clje
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,60 @@
`width` and `height` parameters.

In clj the `format` parameter defines how the pixels are stored.
See the PImage reference for more information.
See the wxImage reference for more information.
Possible formats: `:rgb`, `:argb`, `:alpha` (grayscale alpha channel)

Prefer using `create-image` over initialising new `wxImage` (or `Image`)
instances directly."
[w h]
(p/create-image *canvas* w h))

(defn load-image
"Loads an image into a variable of type wxImage. Four types of
images ( .gif, .jpg, .tga, .png) images may be loaded. To load
correctly, images must be located in the data directory of the
current sketch. In most cases, load all images in setup to preload
them at the start of the program. Loading images inside draw will
reduce the speed of a program.

The filename parameter can also be a URL to a file found online.

If an image is not loaded successfully, the null value is returned
and an error message will be printed to the console. The error
message does not halt the program, however the null value may cause
a NullPointerException if your code does not check whether the value
returned from load-image is nil.

Depending on the type of error, a wxImage object may still be
returned, but the width and height of the image will be set to
-1. This happens if bad image data is returned or cannot be decoded
properly. Sometimes this happens with image URLs that produce a 403
error or that redirect to a password prompt, because load-image
will attempt to interpret the HTML as image data."
[filename]
(p/load-image *canvas* (str filename)))


(defn image
"Displays images to the screen. Processing currently works with GIF,
JPEG, and Targa images. The color of an image may be modified with
the tint function and if a GIF has transparency, it will maintain
its transparency. The img parameter specifies the image to display
and the x and y parameters define the location of the image from its
upper-left corner. The image is displayed at its original size
unless the width and height parameters specify a different size. The
image-mode fn changes the way the parameters work. A call to
(image-mode :corners) will change the width and height parameters to
define the x and y values of the opposite corner of the image.

Starting with release 0124, when using the default (JAVA2D)
renderer, smooth will also improve image quality of resized
images."
([img x y]
(p/image *canvas* img x y))
([img x y w h]
(p/image *canvas* img x y w h)))

;; Stroke & fill

(defn- save-current-stroke
Expand Down
21 changes: 21 additions & 0 deletions src/doodler/examples/image.clje
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
;; Source: https://github.com/quil/quil/blob/master/README.md
(ns doodler.examples.image
(:require [doodler.core :as d]))

(defn setup
[]
(d/frame-rate 60))

(defn draw []
(d/background 200)
(d/text (str (d/current-frame-rate)) 0 10)
(let [img (d/load-image "resources/pacman.png")]
(d/image img 10 10 50 50)))

(d/defsketch sketch
:title "Load image"
:size [323 200]
:setup setup
:draw draw
:features [:resizable :keep-on-top]
:bgcolor [200])
4 changes: 3 additions & 1 deletion src/doodler/protocols.clje
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
(height [this]))

(defprotocol IBitmap
(create-image [this w h]))
(create-image [this w h])
(load-image [this filename])
(image [this img x y] [this img x y w h]))

(defprotocol IPrimitives
(arc [this x y w h start end])
Expand Down
24 changes: 18 additions & 6 deletions src/doodler/wx/panel.clje
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@
matrix
current-shape]
p/ICanvas
(canvas [this] panel)
(width [this] width)
(height [this] height)
(canvas [_this] panel)
(width [_this] width)
(height [_this] height)

p/IBitmap
(create-image [this w h]
(create-image [_this w h]
(wxBitmap/new w h))
(load-image [_this filename]
(let [img (wxImage/new)]
(if (wxImage/loadFile img filename)
img
(throw (str "Could not load " filename)))))
(image [_this img x y]
(let [bitmap (wxBitmap/new img)]
(wxDC/drawBitmap bitmap-dc bitmap #erl[x y])))
(image [_this img x y w h]
(let [img (wxImage/scale img w h)
bitmap (wxBitmap/new img)]
(wxDC/drawBitmap bitmap-dc bitmap #erl[x y])))

p/IPrimitives
(arc [this x y w h start end]
(arc [_this x y w h start end]
(let [path (wxGraphicsContext/createPath @gc)
r (/ (math/sqrt (+ (* w w) (* h h))) 2)]
(wxGraphicsPath/addArc path
Expand Down Expand Up @@ -144,7 +156,7 @@
(p/show-unsupported-warning! "print-camera"))
(print-matrix [this]
(let [m (wxGraphicsContext/getTransform @gc)
[a b c d tx ty](wxGraphicsMatrix/get m)]
[a b c d tx ty] (wxGraphicsMatrix/get m)]
(println "Matrix:" :a a :b b :c c :d d :tx tx :ty ty)))
(print-projection [this]
(p/show-unsupported-warning! "print-projection"))
Expand Down

0 comments on commit cb5bb26

Please sign in to comment.