Skip to content

Commit

Permalink
Refactor access impl
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Jul 26, 2024
1 parent 28a7635 commit fc220c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
49 changes: 30 additions & 19 deletions lib/vix/vips/image.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
defmodule Vix.Vips.Image do
defstruct [:ref]

alias __MODULE__

@moduledoc """
Functions for reading and writing images as well as
accessing and updating image metadata.
Expand Down Expand Up @@ -99,6 +95,9 @@ defmodule Vix.Vips.Image do
%Vix.Vips.Image{ref: #Reference<0.2448791511.2685009949.153742>}
"""

defstruct [:ref]

alias __MODULE__
alias Vix.Nif
alias Vix.Type
alias Vix.Vips.MutableImage
Expand Down Expand Up @@ -152,7 +151,7 @@ defmodule Vix.Vips.Image do

# Extract band when the band number is positive or zero
def fetch(image, band) when is_integer(band) and band >= 0 do
case Vix.Vips.Operation.extract_band(image, band) do
case Operation.extract_band(image, band) do
{:ok, band} -> {:ok, band}
{:error, _reason} -> raise ArgumentError, "Invalid band requested. Found #{inspect(band)}"
end
Expand All @@ -179,9 +178,13 @@ defmodule Vix.Vips.Image do
with {:ok, args} <- normalize_access_args(args),
{:ok, left, width} <- validate_dimension(args[:width], width(image)),
{:ok, top, height} <- validate_dimension(args[:height], height(image)),
{:ok, first_band, bands} <- validate_dimension(args[:band], bands(image)),
{:ok, area} <- extract_area(image, left, top, width, height) do
extract_band(area, first_band, n: bands)
{:ok, first_band, bands} <- validate_dimension(args[:band], bands(image)) do
extracted_image =
image
|> extract_area!(left, top, width, height)
|> extract_band!(first_band, n: bands)

{:ok, extracted_image}
else
{:error, _} ->
raise ArgumentError, "Argument must be list of integers or ranges or keyword list"
Expand All @@ -190,19 +193,19 @@ defmodule Vix.Vips.Image do

@impl Access
def get_and_update(_image, _key, _fun) do
raise "get_and_update/3 for Vix.Vips.Image is not supported."
raise ArgumentError, "get_and_update/3 for Vix.Vips.Image is not supported."
end

@impl Access
def pop(_image, _band, _default \\ nil) do
raise "pop/3 for Vix.Vips.Image is not supported."
raise ArgumentError, "pop/3 for Vix.Vips.Image is not supported."
end

# Extract a range of bands
defp fetch_range(image, %Range{first: first, last: last}) when first >= 0 and last >= first do
case Vix.Vips.Operation.extract_band(image, first, n: last - first + 1) do
case Operation.extract_band(image, first, n: last - first + 1) do
{:ok, band} -> {:ok, band}
{:error, _reason} -> raise "Invalid band range #{inspect(first..last)}"
{:error, _reason} -> raise Error, "Invalid band range #{inspect(first..last)}"
end
end

Expand Down Expand Up @@ -281,17 +284,25 @@ defmodule Vix.Vips.Image do
Map.get(range, :step) == 1 || !Map.has_key?(range, :step)
end

defp extract_area(image, left, top, width, height) do
@spec extract_area!(
Image.t(),
non_neg_integer,
non_neg_integer,
non_neg_integer,
non_neg_integer
) :: Image.t() | no_return
defp extract_area!(image, left, top, width, height) do
case Operation.extract_area(image, left, top, width, height) do
{:ok, image} -> {:ok, image}
_other -> raise "Requested area could not be extracted"
{:ok, image} -> image
{:error, _} = _error -> raise Error, "Requested area could not be extracted"
end
end

defp extract_band(area, first_band, options) do
@spec extract_band!(Image.t(), non_neg_integer, keyword) :: Image.t() | no_return
defp extract_band!(area, first_band, options) do
case Operation.extract_band(area, first_band, options) do
{:ok, image} -> {:ok, image}
_other -> raise "Requested band(s) could not be extracted"
{:ok, image} -> image
{:error, _} = _error -> raise Error, "Requested band(s) could not be extracted"
end
end

Expand Down Expand Up @@ -380,7 +391,7 @@ defmodule Vix.Vips.Image do
@spec new_from_buffer(binary(), keyword()) :: {:ok, t()} | {:error, term()}
def new_from_buffer(bin, opts \\ []) do
with {:ok, loader} <- Vix.Vips.Foreign.find_load_buffer(bin),
{:ok, {ref, _optional}} <- Vix.Vips.Operation.Helper.operation_call(loader, [bin], opts) do
{:ok, {ref, _optional}} <- Operation.Helper.operation_call(loader, [bin], opts) do
{:ok, wrap_type(ref)}
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/vix/vips/access_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Vix.Vips.AccessTest do
test "Access behaviour for Vix.Vips.Image with invalid range band" do
{:ok, im} = Image.new_from_file(img_path("puppies.jpg"))

assert_raise RuntimeError, "Invalid band range 1..5", fn ->
assert_raise Image.Error, "Invalid band range 1..5", fn ->
im[1..5]
end

Expand Down

0 comments on commit fc220c5

Please sign in to comment.