Skip to content

Commit

Permalink
Add Image.supported_save_suffixes/0
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Nov 10, 2023
1 parent c9ecc63 commit f93db94
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
40 changes: 40 additions & 0 deletions c_src/vips_foreign.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,43 @@ ERL_NIF_TERM nif_foreign_find_save(ErlNifEnv *env, int argc,
notify_consumed_timeslice(env, start, enif_monotonic_time(ERL_NIF_USEC));
return ret;
}

ERL_NIF_TERM nif_foreign_get_suffixes(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]) {
ASSERT_ARGC(argc, 0);

ErlNifTime start;
ERL_NIF_TERM ret;
gchar **suffixes;
ERL_NIF_TERM list;
ERL_NIF_TERM bin;
ssize_t length;
unsigned char *temp;

start = enif_monotonic_time(ERL_NIF_USEC);

suffixes = vips_foreign_get_suffixes();

if (!suffixes) {
error("Failed to fetch suffixes. error: %s", vips_error_buffer());
vips_error_clear();
ret = make_error(env, "Failed to fetch suffixes");
goto exit;
}

list = enif_make_list(env, 0);
for (int i = 0; suffixes[i] != NULL; i++) {
length = strlen(suffixes[i]);
temp = enif_make_new_binary(env, length, &bin);
memcpy(temp, suffixes[i], length);

list = enif_make_list_cell(env, bin, list);
}
g_strfreev(suffixes);

ret = make_ok(env, list);

exit:
notify_consumed_timeslice(env, start, enif_monotonic_time(ERL_NIF_USEC));
return ret;
}
3 changes: 3 additions & 0 deletions c_src/vips_foreign.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ ERL_NIF_TERM nif_foreign_find_load(ErlNifEnv *env, int argc,
ERL_NIF_TERM nif_foreign_find_save(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]);

ERL_NIF_TERM nif_foreign_get_suffixes(ErlNifEnv *env, int argc,
const ERL_NIF_TERM argv[]);

#endif
1 change: 1 addition & 0 deletions c_src/vix.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static ErlNifFunc nif_funcs[] = {
{"nif_foreign_find_save", 1, nif_foreign_find_save, 0},
{"nif_foreign_find_load_buffer", 1, nif_foreign_find_load_buffer, 0},
{"nif_foreign_find_save_buffer", 1, nif_foreign_find_save_buffer, 0},
{"nif_foreign_get_suffixes", 0, nif_foreign_get_suffixes, 0},

/* Syscalls */
{"nif_pipe_open", 1, nif_pipe_open, 0},
Expand Down
3 changes: 3 additions & 0 deletions lib/vix/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ defmodule Vix.Nif do
def nif_foreign_find_save(_filename),
do: :erlang.nif_error(:nif_library_not_loaded)

def nif_foreign_get_suffixes,
do: :erlang.nif_error(:nif_library_not_loaded)

# OS Specific
def nif_pipe_open(_mode),
do: :erlang.nif_error(:nif_library_not_loaded)
Expand Down
4 changes: 4 additions & 0 deletions lib/vix/vips/foreign.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ defmodule Vix.Vips.Foreign do
def find_save(filename) do
Nif.nif_foreign_find_save(filename)
end

def get_suffixes do
Nif.nif_foreign_get_suffixes()
end
end
26 changes: 20 additions & 6 deletions lib/vix/vips/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ defmodule Vix.Vips.Image do
end
end

@doc """
Returns list of supported extension for *saving* the image.
Supported suffix can be used to save image in a particular format.
See `write_to_file/2`.
Note that the image format supported for saving the image and the
format supported for loading image might be different. For example
SVG format can be loaded but can not be saved.
"""
@spec supported_save_suffixes :: {:ok, [String.t()]} | {:error, term}
def supported_save_suffixes, do: Vix.Vips.Foreign.get_suffixes()

# Copy an image to a memory area.
# If image is already a memory buffer, just ref and return. If it's
# a file on disc or a partial, allocate memory and copy the image to
Expand All @@ -625,19 +638,20 @@ defmodule Vix.Vips.Image do
@doc """
Write `vips_image` to a file.
A saver is selected based on image extension in `path`. You can
get list of supported extensions by `supported_save_suffixes/0`.
Save options may be encoded in the filename. For example:
```elixir
Image.write_to_file(vips_image, "fred.jpg[Q=90]")
```
A saver is selected based on image extension in `path`. The full set
of save options depend on the selected saver. Try something like:
The full set of save options depend on the selected saver.
You can check the supported options for a saver by checking
docs for the particular format save function in `Operation` module.
For example, for you jpeg, `Vix.Vips.Operation.jpegsave/2`.
```shell
$ vips jpegsave
```
at the command-line to see all the available options for JPEG save.
If you want more control over the saver, Use specific format saver
from `Vix.Vips.Operation`. For example for jpeg use
Expand Down

0 comments on commit f93db94

Please sign in to comment.