Skip to content

Commit

Permalink
[#54] Implement resize and pixel
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Oct 23, 2023
1 parent cb5bb26 commit 5fff2d4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/doodler/core.clje
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,27 @@
([img x y w h]
(p/image *canvas* img x y w h)))


(defn pixels
"Binary containing the values for all the pixels in the display
window or image. This array is therefore the size of the display window.

The return value is a binary with values representing in RGBRGBRGB... format
in the top-to-bottom, left-to-right order, that is the first RGB triplet
corresponds to the first pixel of the first row, the second one - to the
second pixel of the first row and so on until the end of the first row,
with second row following after it and so on."
([] (p/pixels *canvas*))
([img] (p/pixels *canvas* img)))

(defn resize
"Resize the image to a new width and height.
To make the image scale proportionally, use 0 as the value for the wide or
high parameter. For instance, to make the width of an image 150 pixels,
and change the height using the same proportion, use resize(150, 0)."
[img w h]
(p/resize *canvas* img w h))

;; Stroke & fill

(defn- save-current-stroke
Expand Down
5 changes: 3 additions & 2 deletions src/doodler/examples/image.clje
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
(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)))
(let [img (d/load-image "resources/pacman.png")
img (d/resize img 10 10)]
(d/image img 50 50)))

(d/defsketch sketch
:title "Load image"
Expand Down
6 changes: 4 additions & 2 deletions src/doodler/protocols.clje
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
(defprotocol IBitmap
(create-image [this w h])
(load-image [this filename])
(image [this img x y] [this img x y w h]))
(image [this img x y] [this img x y w h])
(resize [this img w h])
(pixels [this] [this img]))

(defprotocol IPrimitives
(arc [this x y w h start end])
Expand Down Expand Up @@ -56,7 +58,7 @@
(post-draw [this])
(paint [this])
(refresh [this])
(resize [this]))
(resize-canvas [this]))

(defprotocol ITransform
(push-matrix [this])
Expand Down
2 changes: 1 addition & 1 deletion src/doodler/sketch.clje
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

(defn on-resize
[sketch]
(update sketch :canvas p/resize))
(update sketch :canvas p/resize-canvas))

(def ^{:private true}
supported-features
Expand Down
14 changes: 10 additions & 4 deletions src/doodler/wx/panel.clje
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
(let [img (wxImage/scale img w h)
bitmap (wxBitmap/new img)]
(wxDC/drawBitmap bitmap-dc bitmap #erl[x y])))
(resize [_this img w h]
(wxImage/scale img w h))
(pixels [_this]
(wxImage/getData (wxBitmap/convertToImage bitmap)))
(pixels [_this img]
(wxImage/getData img))

p/IPrimitives
(arc [_this x y w h start end]
Expand Down Expand Up @@ -115,20 +121,20 @@
(wxDC/drawBitmap bitmap-dc img #erl[x y]))

p/IEvents
(pre-draw [this]
(pre-draw [_this]
(vreset! gc (wxGraphicsContext/create bitmap-dc)))
(post-draw [this]
(wxGraphicsContext/destroy @gc))
(paint [this]
(paint [_this]
(let [dc (wxPaintDC/new panel)]
(try
(wxDC/drawBitmap dc bitmap #erl[0 0])
(finally
(wxPaintDC/destroy dc)))))
(refresh [this]
(refresh [_this]
(wxWindow/refresh panel
#erl(#erl[:eraseBackground false])))
(resize [this]
(resize-canvas [_this]
(let [bgcolor (wxBrush/getColour bg-brush)
_ (wxMemoryDC/destroy bitmap-dc)
_ (wxBitmap/destroy bitmap)
Expand Down

0 comments on commit 5fff2d4

Please sign in to comment.