Search code examples
encodingimagemagickwebassembly

How to get a list of the input and output formats in ImageMagick API?


I am tinkering with the browser / wasm ImageMagick API, and it has a MagickFormat module here which looks like this essentially:

export declare enum MagickFormat {
    Unknown = "UNKNOWN",
    ThreeFr = "3FR",
    ThreeG2 = "3G2",
    ThreeGp = "3GP",
    A = "A",
    Aai = "AAI",
    Ai = "AI",
    Apng = "APNG",
    Art = "ART",
    Arw = "ARW",
    Ashlar = "ASHLAR",
    Avi = "AVI",
    Avif = "AVIF",
    Avs = "AVS",
    B = "B",
    Bayer = "BAYER",
    Bayera = "BAYERA",
    Bgr = "BGR",
    Bgra = "BGRA",
    Bgro = "BGRO",
    Bmp = "BMP",
    Bmp2 = "BMP2",
    Bmp3 = "BMP3",
    Brf = "BRF",
    C = "C",
    Cal = "CAL",
    Cals = "CALS",
    Canvas = "CANVAS",
    Caption = "CAPTION",
    Cin = "CIN",
    Cip = "CIP",
    Clip = "CLIP",
    Cmyk = "CMYK",
    Cmyka = "CMYKA",
    Cr2 = "CR2",
    Cr3 = "CR3",
    ...

Essentially it lists about ~260 formats, but it doesn't say which ones are allowed as input and which ones are allowed as output. I tried to convert a PNG to CR2 but the wasm API failed with:

Error: NoEncodeDelegateForThisImageFormat `CR2' @ error/constitute.c/WriteImage/1409

I knew this would fail because I read it on some random website I can't remember, that CR2 is the Canon camera raw format, and they can only take it as input and use it to generate more simple outputs like JPG/PNG/etc.. But my question is, what exactly are the allowed input and output formats for ImageMagick? How can I figure that out? Is there a way to list them somehow with the API (which I can then figure out to translate to the wasm API)? Ideally not a CLI example, but a source code API example I guess.

Do all those in MagickFormat get allowed as input formats? And only a subset as output formats? Or where can I find which are allowed for which? Oh and related, does it depend on the input what output is allowed? (Like, is it a matrix of "JPG can convert into X and Y but not Z, but PNG can convert into X and Z but not Y" sort of thing, or is it more simple than that?) I'm partially curious in this process if you have to create a list of one-to-many mappings for each input type to the possible outputs for that input, or of it's more generic than that.

Main goal: figure out what operations I am allowed to make.

I also saw the supportedFormats property, but I'm not sure that fully solves my problem.


Solution

  • The simplest method I know is in Terminal:

    identify -list format
    

    and look for r for read and w for write in the second column:

    Output

    Format  Mode  Description
    -------------------------------------------------------------------------------
       3FR  r--   Hasselblad CFV/H3D39II (0.19.5-Release)
       3G2  r--   Media Container
       3GP  r--   Media Container
         A* rw+   Raw alpha samples
       AAI* rw+   AAI Dune image
        AI  rw-   Adobe Illustrator CS2
      APNG  rw+   Animated Portable Network Graphics
       ART* rw-   PFS: 1st Publisher Clip Art
       ARW  r--   Sony Alpha Raw Image Format
       ...
       ...
    

    You can go from any format to any other - obviously providing the input source is readable and the output destination is writable. The "all-encompassing, super format" which can represent everything ImageMagick knows (e.g. 8/16/32/64-bit integer/float/complex samples, transparency, tens of channels, EXIF/XMP/IPTC data, GIF delays) is MIFF - so it essentially goes via that format from any input to any output.


    Likewise, if you want to know what "compositing/blending modes" ImageMagick supports, you can use:

    identify -list compose
    

    Output

    top
    Blend
    Blur
    Bumpmap 
    ChangeMask
    Clear
    ColorBurn
    ColorDodge
    ...
    ...
    

    The "super command" that tells you everything you could put after identify -list, i.e. "give me a list of lists", is:

    identify -list list
    

    Output

    Align
    Alpha
    AutoThreshold
    Boolean
    Cache
    Channel
    Class
    CLI
    ClipPath
    Coder
    Color
    Colorspace
    Command
    ....
    ...
    

    So you can get a list of all the available channel names, all the compression types, all the colourspaces, all the interpolation options, all gravity options, all the interlace options, all the possible virtual pixel types...