Skip to content

Commit

Permalink
Add read image function
Browse files Browse the repository at this point in the history
  • Loading branch information
jrstrunk committed Oct 1, 2024
1 parent d4ef83b commit 568897e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 4 deletions.
Binary file removed out.jpg
Binary file not shown.
Binary file removed out2.jpg
Binary file not shown.
4 changes: 4 additions & 0 deletions src/ansel.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule Ansel do
alias Vix.Vips.{Image, Operation}

def read(path) do
Path.expand(path) |> Image.new_from_file
end

def write_to_file(img, path) do
save_path = Path.expand(path)

Expand Down
17 changes: 13 additions & 4 deletions src/ansel.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ fn composite_over_ffi(
y: Int,
) -> Result(Image, String)

@external(erlang, "Elixir.Vix.Vips.Image", "width")
pub fn get_width(image: Image) -> Int

@external(erlang, "Elixir.Vix.Vips.Image", "height")
pub fn get_height(image: Image) -> Int

pub fn resize_width_to(img: Image, res width: Int) -> Result(Image, snag.Snag) {
resize_by(img, scale: int.to_float(width) /. int.to_float(get_width(img)))
}
Expand Down Expand Up @@ -124,8 +130,11 @@ pub fn write(
@external(erlang, "Elixir.Ansel", "write_to_file")
fn write_ffi(img: Image, to path: String) -> Result(Nil, String)

@external(erlang, "Elixir.Vix.Vips.Image", "width")
pub fn get_width(image: Image) -> Int
pub fn read(from path: String) -> Result(Image, snag.Snag) {
read_ffi(path)
|> result.map_error(snag.new)
|> snag.context("Failed to read image from file")
}

@external(erlang, "Elixir.Vix.Vips.Image", "height")
pub fn get_height(image: Image) -> Int
@external(erlang, "Elixir.Ansel", "read")
fn read_ffi(from path: String) -> Result(Image, String)
Binary file removed ss.png
Binary file not shown.
Binary file removed test.jpg
Binary file not shown.
7 changes: 7 additions & 0 deletions test/ansel_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ pub fn main() {
gleeunit.main()
}

pub fn read_test() {
let assert Ok(img) = ansel.read("test/resources/gleam_lucy_6x6.avif")

ansel.get_width(img)
|> should.equal(6)
}

pub fn new_image_solid_grey_test() {
let assert Ok(bin) =
simplifile.read_bits("test/resources/solid_grey_6x6.avif")
Expand Down

0 comments on commit 568897e

Please sign in to comment.