PixelImage image = ...; // the image to be saved ImageCodec codec = new BMPCodec(); // BMPCodec is just an example codec.setFile("image.bmp", CodecMode.SAVE); codec.setImage(image); codec.process();
Codecs have different requirements concerning I/O objects. If an image is to be loaded, it's enough for some formats to linearly read from an {@link java.io.InputStream} to load the image.However, some formats (like TIFF) require random access.
When implementing a codec, take care that as many I/O classes as possible can be used. If possible, call {@link #getInputAsDataInput} when loading and {@link #getOutputAsDataOutput}when saving. That way, input / output streams, RandomAccessFiles and arbitrary DataInput / DataOutput objects can be used.
null
.Mode only has two possible values, {@link CodecMode#LOAD} and{@link CodecMode#SAVE}. In some cases, the codec can find out whether to load or save from the I/O objects that were given to it; if it has an input stream, something must be loaded, if it has an output stream, something is to be saved. If a codec demands a {@link RandomAccessFile}, there is no way to find out the mode automatically, that is why {@link #setRandomAccessFile} also has an argument of type {@link CodecMode}. Bounds; to load or save only part of an image. Defining bounds is optional; by default, the complete image is loaded or saved (no bounds). Using {@link #setBounds(int,int,int,int)}, one can specify the rectangle which will be loaded or saved.
PixelImage object; get and set methods for the image which is to be loaded or saved. If an image is to be loaded, a PixelImage object can optionally be specified so that the image will be written to that object; image type and resolution must of course match the image from input. Normally, the codec will create the appropriate image object itself. If an image is to be saved, an image object must be provided, otherwise there is nothing to do.
Image index; the index of the image that is to be loaded (int value, default is 0). For image formats that support more than one image in one stream, the index of the image to be loaded (zero-based) can be specified using {@link #setImageIndex(int)}.
Each file format must be able to return its name ( {@link #getFormatName()}) and file extensions that are typical for it ( {@link #getFileExtensions()}).
A related method suggests a file extension for a given PixelImage object ( {@link #suggestFileExtension(PixelImage)}). That method need not be implemented, the default version returns simply null
. However, it is encouraged that codec implementors provide this method as well. Most file formats only have one typical extension (e. g. .bmp
). However, for a file format like PNM, the extension depends on the image type (a grayscale image would end in .pgm
, a color image in .ppm
).
@author Marco Schmidt
|
|