@@ -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" )
0 commit comments