Class GIFCodec
- java.lang.Object
-
- net.sourceforge.jiu.ops.Operation
-
- net.sourceforge.jiu.codecs.ImageCodec
-
- net.sourceforge.jiu.codecs.GIFCodec
-
public class GIFCodec extends ImageCodec
A codec to write Compuserve GIF (Graphics Interchange Format) files.Only writing GIF files is supported right now. Reading GIF files with JIU can be done with the
ToolkitLoaderclass which uses the image reader built into the Java runtime library (Toolkitclass). That reader has supported GIF since Java 1.0.Supported image types
When saving, classes implementing the following image data interfaces are supported:BilevelImage,Gray8ImageandPaletted8Image. GIF only supports up to 256 colors in an image, so you will have to use one of the quantization classes to reduce a truecolor image to 256 or less colors before you can save it with this codec.Supported I/O classes
This codec supportsOutputStream,DataOutputandRandomAccessFile.Bounds
ImageCodec's bounds concept is supported. A user of this codec can specify a rectangular part of the input image that will be saved instead of the complete image.Comments
GIF - at least in its 89a version - allows for the inclusion of textual comments. When saving an image to a GIF file, each comment given to a codec will be stored in a comment extension block of its own.Usage example
Save an image using this codec:GIFCodec codec = new GIFCodec(); codec.appendComment("Bob and Susan at the Munich airport (2002-06-13)."); codec.setImage(image); // BilevelImage, Gray8Image or Paletted8Image codec.setInterlacing(true); codec.setFile("output.gif", CodecMode.SAVE); codec.process(); codec.close();Interlaced storage
This codec allows creating interlaced and non-interlaced GIF files. The default is non-interlaced storage. Non-interlaced files store the rows top-to-bottom.Interlaced files store the image in four passes, progressively adding rows until the complete image is stored. When decoding, the progressive display of interlaced files makes it supposedly quicker to find out what's displayed in the image.
On the other hand, transmission typically takes longer, because interlacing often leads to slightly larger files. When using interlaced mode, lines that get stored one after another have some room between them in the image, so there are less similarities between consecutive lines, which worsens compression ratio (compression works better with a lot of similarities in the data to be compressed).
GIF versions
There are two versions of GIF, 87a and 89a. In 89a, several things were added to the file format specification. From the 89a features this codec only uses the possibility of storing textual comments in GIF files. Thus, the version used for writing depends on the return value ofImageCodec.getNumComments(). If there is at least one comment to be written to the file, version 89a will be used, 87a otherwise.Licensing of the LZW algorithm
Unisys Corp. had a patent in several countries on the LZW algorithm used within GIF. However, this patent has expired (Japan being the last country where the patent expired, on July 7th 2004) so that LZW can be used freely.Licensing of the file format
GIF was defined by Compuserve. In a technical document file calledGif89a.txtthat I found somewhere on the Net they grant a royalty-free license for use of the format to anyone - in order to improve the popularity of the format, I guess. I don't think that it should be possible to put a file format under a copyright, but all that Compuserve asks for in exchange for freely using the format is the inclusion of a message. So, here is that message:"The Graphics Interchange Format(c) is the Copyright property of CompuServe Incorporated. GIF(sm) is a Service Mark property of CompuServe Incorporated."
Animated GIFs
GIF allows for animations to be stored. This codec only supports storing a single image, though.File format background
I've compiled a web page with technical information on GIF.- Author:
- Marco Schmidt
-
-
Field Summary
Fields Modifier and Type Field Description private intbackgroundColorprivate intbitOffsetprivate intbitsPerPixelprivate byte[]blockprivate intblockLengthprivate intclearCodeprivate static intCODE_ARRAY_LENGTHprivate intcodeSizeprivate int[]currentCodeprivate intcurrentColumnprivate intcurrentInterlacingPassprivate intcurrentRowprivate intendOfInformationCodeprivate intfreeCodeprivate intheightprivate IntegerImageimageToBeSavedprivate intinitialCodeSizeprivate booleaninterlacedprivate static int[]INTERLACING_FIRST_ROWprivate static int[]INTERLACING_INCREMENTprivate static byte[]MAGIC_GIF87Aprivate static byte[]MAGIC_GIF89Aprivate intmaxCodeprivate int[]newCodeprivate booleannotFinishedprivate static intNUM_INTERLACING_PASSESprivate int[]oldCodeprivate DataOutputoutprivate intprocessedRowsprivate intwidth
-
Constructor Summary
Constructors Constructor Description GIFCodec()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetBackgroundColor()Returns the index of the background color.String[]getFileExtensions()Returns all file extensions that are typical for this file format.StringgetFormatName()Returns the name of the file format supported by this codec.String[]getMimeTypes()Return the MIME (Multipurpose Internet Mail Extensions) type strings for this format, ornullif none are available.private intgetNextSample()private voidinitEncoding()booleanisInterlaced()Returns if the image will be stored in interlaced (true) or non-interlaced mode (false).booleanisLoadingSupported()Returns if this codec is able to load images in the file format supported by this codec.booleanisSavingSupported()Returns if this codec is able to save images in the file format supported by this codec.voidprocess()This method does the actual work of the operation.private voidresetBlock()private voidresetEncoder()private voidsave()voidsetBackgroundColor(int colorIndex)Specify the value of the background color.voidsetInterlacing(boolean useInterlacing)Specifies whether the image will be stored in interlaced mode (true) or non-interlaced mode (false).private voidwriteBlock()private voidwriteCode(int code)private voidwriteComments()private voidwriteHeader()Writes a global header, a global palette and an image descriptor to output.private voidwriteImage()private voidwritePalette()private voidwriteShort(int value)private voidwriteStream()private voidwriteTrailer()-
Methods inherited from class net.sourceforge.jiu.codecs.ImageCodec
appendComment, checkBounds, checkImageResolution, close, getBoundsHeight, getBoundsWidth, getBoundsX1, getBoundsX2, getBoundsY1, getBoundsY2, getComment, getDataInput, getDataOutput, getDpiX, getDpiY, getImage, getImageIndex, getInputAsDataInput, getInputStream, getMode, getNumComments, getOutputAsDataOutput, getOutputStream, getRandomAccessFile, hasBounds, initModeFromIOObjects, isRowRequired, isTileRequired, removeAllComments, removeBounds, setBounds, setBoundsIfNecessary, setDataInput, setDataOutput, setDpi, setFile, setFile, setImage, setImageIndex, setInputStream, setOutputStream, setRandomAccessFile, suggestFileExtension
-
Methods inherited from class net.sourceforge.jiu.ops.Operation
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
-
-
-
-
Field Detail
-
CODE_ARRAY_LENGTH
private static final int CODE_ARRAY_LENGTH
- See Also:
- Constant Field Values
-
INTERLACING_FIRST_ROW
private static final int[] INTERLACING_FIRST_ROW
-
INTERLACING_INCREMENT
private static final int[] INTERLACING_INCREMENT
-
NUM_INTERLACING_PASSES
private static final int NUM_INTERLACING_PASSES
- See Also:
- Constant Field Values
-
MAGIC_GIF87A
private static final byte[] MAGIC_GIF87A
-
MAGIC_GIF89A
private static final byte[] MAGIC_GIF89A
-
backgroundColor
private int backgroundColor
-
block
private byte[] block
-
bitOffset
private int bitOffset
-
bitsPerPixel
private int bitsPerPixel
-
blockLength
private int blockLength
-
clearCode
private int clearCode
-
codeSize
private int codeSize
-
currentCode
private int[] currentCode
-
currentColumn
private int currentColumn
-
currentInterlacingPass
private int currentInterlacingPass
-
currentRow
private int currentRow
-
endOfInformationCode
private int endOfInformationCode
-
notFinished
private boolean notFinished
-
freeCode
private int freeCode
-
imageToBeSaved
private IntegerImage imageToBeSaved
-
initialCodeSize
private int initialCodeSize
-
interlaced
private boolean interlaced
-
height
private int height
-
maxCode
private int maxCode
-
newCode
private int[] newCode
-
oldCode
private int[] oldCode
-
out
private DataOutput out
-
processedRows
private int processedRows
-
width
private int width
-
-
Method Detail
-
getBackgroundColor
public int getBackgroundColor()
Returns the index of the background color.- Returns:
- int value with the color (index into the palette) of the background color
- See Also:
setBackgroundColor(int)
-
getFileExtensions
public String[] getFileExtensions()
Description copied from class:ImageCodecReturns all file extensions that are typical for this file format. The default implementation in ImageCodec returnsnull. The file extension strings should include a leading dot and are supposed to be lower case (if that is allowed for the given file format). Example:{".jpg", ".jpeg"}for the JPEG file format.- Overrides:
getFileExtensionsin classImageCodec- Returns:
- String array with typical file extensions
-
getFormatName
public String getFormatName()
Description copied from class:ImageCodecReturns the name of the file format supported by this codec. All classes extendingImageCodecmust override this method. When overriding, leave out any words in a particular language so that this format name can be understood by everyone. Usually it is enough to return the format creator plus a typical abbreviation, e.g.Microsoft BMPorPortable Anymap (PNM).- Specified by:
getFormatNamein classImageCodec- Returns:
- name of the file format supported by this codec
-
getMimeTypes
public String[] getMimeTypes()
Description copied from class:ImageCodecReturn the MIME (Multipurpose Internet Mail Extensions) type strings for this format, ornullif none are available.- Specified by:
getMimeTypesin classImageCodec- Returns:
- MIME type strings or null
-
getNextSample
private int getNextSample()
-
initEncoding
private void initEncoding() throws IOException- Throws:
IOException
-
isInterlaced
public boolean isInterlaced()
Returns if the image will be stored in interlaced (true) or non-interlaced mode (false).- Returns:
- interlacing mode
- See Also:
setInterlacing(boolean)
-
isLoadingSupported
public boolean isLoadingSupported()
Description copied from class:ImageCodecReturns if this codec is able to load images in the file format supported by this codec. Iftrueis returned this does not necessarily mean that all files in this format can be read, but at least some.- Specified by:
isLoadingSupportedin classImageCodec- Returns:
- if loading is supported
-
isSavingSupported
public boolean isSavingSupported()
Description copied from class:ImageCodecReturns if this codec is able to save images in the file format supported by this codec. Iftrueis returned this does not necessarily mean that all types files in this format can be written, but at least some.- Specified by:
isSavingSupportedin classImageCodec- Returns:
- if saving is supported
-
process
public void process() throws MissingParameterException, OperationFailedExceptionDescription copied from class:OperationThis method does the actual work of the operation. It must be called after all parameters have been given to the operation object.- Overrides:
processin classOperation- Throws:
MissingParameterException- if any mandatory parameter was not given to the operationWrongParameterException- if at least one of the input parameters was not initialized appropriately (values out of the valid interval, etc.)OperationFailedException
-
resetBlock
private void resetBlock()
-
resetEncoder
private void resetEncoder()
-
save
private void save() throws MissingParameterException, OperationFailedException, UnsupportedTypeException, WrongParameterException
-
setBackgroundColor
public void setBackgroundColor(int colorIndex)
Specify the value of the background color. Default is0.- Parameters:
colorIndex- int value with the color (index into the palette) of the background color- See Also:
getBackgroundColor()
-
setInterlacing
public void setInterlacing(boolean useInterlacing)
Specifies whether the image will be stored in interlaced mode (true) or non-interlaced mode (false).- Parameters:
useInterlacing- boolean, if true interlaced mode, otherwise non-interlaced mode- See Also:
isInterlaced()
-
writeBlock
private void writeBlock() throws IOException- Throws:
IOException
-
writeCode
private void writeCode(int code) throws IOException- Throws:
IOException
-
writeComments
private void writeComments() throws IOException- Throws:
IOException
-
writeHeader
private void writeHeader() throws IOExceptionWrites a global header, a global palette and an image descriptor to output.- Throws:
IOException
-
writeImage
private void writeImage() throws IOException- Throws:
IOException
-
writePalette
private void writePalette() throws IOException- Throws:
IOException
-
writeShort
private void writeShort(int value) throws IOException- Throws:
IOException
-
writeStream
private void writeStream() throws IOException- Throws:
IOException
-
writeTrailer
private void writeTrailer() throws IOException- Throws:
IOException
-
-