diff --git a/c_src/vips_foreign.c b/c_src/vips_foreign.c index ac810cf..7603a78 100644 --- a/c_src/vips_foreign.c +++ b/c_src/vips_foreign.c @@ -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; +} diff --git a/c_src/vips_foreign.h b/c_src/vips_foreign.h index fc8abd2..38a148a 100644 --- a/c_src/vips_foreign.h +++ b/c_src/vips_foreign.h @@ -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 diff --git a/c_src/vix.c b/c_src/vix.c index 9ce90b2..d07cd56 100644 --- a/c_src/vix.c +++ b/c_src/vix.c @@ -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}, diff --git a/lib/vix/nif.ex b/lib/vix/nif.ex index 5f54090..69f645f 100644 --- a/lib/vix/nif.ex +++ b/lib/vix/nif.ex @@ -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) diff --git a/lib/vix/vips/foreign.ex b/lib/vix/vips/foreign.ex index f92ac33..1611f8a 100644 --- a/lib/vix/vips/foreign.ex +++ b/lib/vix/vips/foreign.ex @@ -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 diff --git a/lib/vix/vips/image.ex b/lib/vix/vips/image.ex index 8dd9118..686990e 100644 --- a/lib/vix/vips/image.ex +++ b/lib/vix/vips/image.ex @@ -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 @@ -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