Skip to content

Commit 7759cbc

Browse files
authored
Merge pull request #263 from boydm/bitmap_put_offset
Scenic.Assets.Stream.Bitmap.put_offset/3
2 parents ba9f0a0 + ee23c4d commit 7759cbc

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.11.0
4+
* Add the Scenic.Assets.Stream.Bitmap.put_offset/3 api
5+
* Various cleanup and documentation fixes
6+
* This is a __MAJOR__ update. As Connor Rigby put it in a call... "What hasn't changed?"
7+
* See the v.11 upgrade guide for an overview.
8+
39
## 0.11.0-beta.0
410
* This is a __MAJOR__ update. As Connor Rigby put it in a call... "What hasn't changed?"
511
* See the v.11 upgrade guide for an overview.

lib/scenic/assets/stream/bitmap.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,52 @@ defmodule Scenic.Assets.Stream.Bitmap do
265265
{@mutable, {w, h, :rgba}, p}
266266
end
267267

268+
# --------------------------------------------------------
269+
@doc """
270+
Set the color value of a single pixel in a bitmap using an offset from the start.
271+
272+
Only works with mutable bitmaps.
273+
274+
The color you provide can be any valid value from the `Scenic.Color` module.
275+
276+
Unlike the `put` function, which specifies the pixel by `x` and `y` position,
277+
`put_offset` takes an offset directly into the data.
278+
279+
The offset would be the same as y * width + x.
280+
"""
281+
282+
@spec put_offset(mutable :: m(), offset :: pos_integer, color :: Color.t()) ::
283+
mutable :: m()
284+
def put_offset(mutable, offset, color)
285+
286+
def put_offset({@mutable, {w, h, :g}, p}, offset, color) when is_integer(offset) do
287+
if offset > w * h, do: raise("Offset is out of bounds")
288+
{:color_g, g} = Color.to_g(color)
289+
nif_put(p, offset, g)
290+
{@mutable, {w, h, :g}, p}
291+
end
292+
293+
def put_offset({@mutable, {w, h, :ga}, p}, offset, color) when is_integer(offset) do
294+
if offset > w * h, do: raise("Offset is out of bounds")
295+
{:color_ga, {g, a}} = Color.to_ga(color)
296+
nif_put(p, offset, g, a)
297+
{@mutable, {w, h, :ga}, p}
298+
end
299+
300+
def put_offset({@mutable, {w, h, :rgb}, p}, offset, color) when is_integer(offset) do
301+
if offset > w * h, do: raise("Offset is out of bounds")
302+
{:color_rgb, {r, g, b}} = Color.to_rgb(color)
303+
nif_put(p, offset, r, g, b)
304+
{@mutable, {w, h, :rgb}, p}
305+
end
306+
307+
def put_offset({@mutable, {w, h, :rgba}, p}, offset, color) when is_integer(offset) do
308+
if offset > w * h, do: raise("Offset is out of bounds")
309+
{:color_rgba, {r, g, b, a}} = Color.to_rgba(color)
310+
nif_put(p, offset, r, g, b, a)
311+
{@mutable, {w, h, :rgba}, p}
312+
end
313+
268314
defp nif_put(_, _, _), do: :erlang.nif_error("Did not find nif_put_g")
269315
defp nif_put(_, _, _, _), do: :erlang.nif_error("Did not find nif_put_ga")
270316
defp nif_put(_, _, _, _, _), do: :erlang.nif_error("Did not find nif_put_rgb")

test/scenic/assets/stream/bitmap_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,43 @@ defmodule Scenic.Assets.Stream.BitmapTest do
181181
assert Bitmap.get(mut, 2, 2) == color
182182
end
183183

184+
# --------------------------------------------------------
185+
test "put_offset :g works" do
186+
color = Color.to_g(5)
187+
188+
mut = Bitmap.build(:g, @width, @height)
189+
assert Bitmap.get(mut, 2, 2) == {:color_g, 0}
190+
mut = Bitmap.put_offset(mut, @width * 2 + 2, color)
191+
assert Bitmap.get(mut, 2, 2) == color
192+
end
193+
194+
test "put_offset :ga works" do
195+
color = Color.to_ga({5, 100})
196+
197+
mut = Bitmap.build(:ga, @width, @height)
198+
assert Bitmap.get(mut, 2, 2) == {:color_ga, {0, 0}}
199+
mut = Bitmap.put_offset(mut, @width * 2 + 2, color)
200+
assert Bitmap.get(mut, 2, 2) == color
201+
end
202+
203+
test "put_offset :rgb works" do
204+
color = Color.to_rgb({1, 2, 3})
205+
206+
mut = Bitmap.build(:rgb, @width, @height)
207+
assert Bitmap.get(mut, 2, 2) == {:color_rgb, {0, 0, 0}}
208+
mut = Bitmap.put_offset(mut, @width * 2 + 2, color)
209+
assert Bitmap.get(mut, 2, 2) == color
210+
end
211+
212+
test "put_offset :rgba works" do
213+
color = Color.to_rgba({1, 2, 3, 100})
214+
215+
mut = Bitmap.build(:rgba, @width, @height)
216+
assert Bitmap.get(mut, 2, 2) == {:color_rgba, {0, 0, 0, 0}}
217+
mut = Bitmap.put_offset(mut, @width * 2 + 2, color)
218+
assert Bitmap.get(mut, 2, 2) == color
219+
end
220+
184221
# --------------------------------------------------------
185222
test "clear :g works" do
186223
color = Color.to_g(5)

0 commit comments

Comments
 (0)