public class GIFCodec extends ImageCodec
Only writing GIF files is supported right now.
Reading GIF files with JIU can be done with the ToolkitLoader
class which uses the image reader built into the Java runtime library (Toolkit
class).
That reader has supported GIF since Java 1.0.
BilevelImage
,
Gray8Image
and
Paletted8Image
.
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.
OutputStream
, DataOutput
and RandomAccessFile
.
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.
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 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).
ImageCodec.getNumComments()
.
If there is at least one comment to be written to the file, version 89a
will be used, 87a otherwise.
Gif89a.txt
that 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."
Modifier and Type | Field | Description |
---|---|---|
private int |
backgroundColor |
|
private int |
bitOffset |
|
private int |
bitsPerPixel |
|
private byte[] |
block |
|
private int |
blockLength |
|
private int |
clearCode |
|
private static int |
CODE_ARRAY_LENGTH |
|
private int |
codeSize |
|
private int[] |
currentCode |
|
private int |
currentColumn |
|
private int |
currentInterlacingPass |
|
private int |
currentRow |
|
private int |
endOfInformationCode |
|
private int |
freeCode |
|
private int |
height |
|
private IntegerImage |
imageToBeSaved |
|
private int |
initialCodeSize |
|
private boolean |
interlaced |
|
private static int[] |
INTERLACING_FIRST_ROW |
|
private static int[] |
INTERLACING_INCREMENT |
|
private static byte[] |
MAGIC_GIF87A |
|
private static byte[] |
MAGIC_GIF89A |
|
private int |
maxCode |
|
private int[] |
newCode |
|
private boolean |
notFinished |
|
private static int |
NUM_INTERLACING_PASSES |
|
private int[] |
oldCode |
|
private DataOutput |
out |
|
private int |
processedRows |
|
private int |
width |
Constructor | Description |
---|---|
GIFCodec() |
Modifier and Type | Method | Description |
---|---|---|
int |
getBackgroundColor() |
Returns the index of the background color.
|
String[] |
getFileExtensions() |
Returns all file extensions that are typical for this file format.
|
String |
getFormatName() |
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, or
null
if none are available. |
private int |
getNextSample() |
|
private void |
initEncoding() |
|
boolean |
isInterlaced() |
Returns if the image will be stored in interlaced (
true )
or non-interlaced mode (false ). |
boolean |
isLoadingSupported() |
Returns if this codec is able to load images in the file format supported by this codec.
|
boolean |
isSavingSupported() |
Returns if this codec is able to save images in the file format supported by this codec.
|
void |
process() |
This method does the actual work of the operation.
|
private void |
resetBlock() |
|
private void |
resetEncoder() |
|
private void |
save() |
|
void |
setBackgroundColor(int colorIndex) |
Specify the value of the background color.
|
void |
setInterlacing(boolean useInterlacing) |
Specifies whether the image will be stored in interlaced mode
(
true ) or non-interlaced mode (false ). |
private void |
writeBlock() |
|
private void |
writeCode(int code) |
|
private void |
writeComments() |
|
private void |
writeHeader() |
Writes a global header, a global palette and
an image descriptor to output.
|
private void |
writeImage() |
|
private void |
writePalette() |
|
private void |
writeShort(int value) |
|
private void |
writeStream() |
|
private void |
writeTrailer() |
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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
private static final int CODE_ARRAY_LENGTH
private static final int[] INTERLACING_FIRST_ROW
private static final int[] INTERLACING_INCREMENT
private static final int NUM_INTERLACING_PASSES
private static final byte[] MAGIC_GIF87A
private static final byte[] MAGIC_GIF89A
private int backgroundColor
private byte[] block
private int bitOffset
private int bitsPerPixel
private int blockLength
private int clearCode
private int codeSize
private int[] currentCode
private int currentColumn
private int currentInterlacingPass
private int currentRow
private int endOfInformationCode
private boolean notFinished
private int freeCode
private IntegerImage imageToBeSaved
private int initialCodeSize
private boolean interlaced
private int height
private int maxCode
private int[] newCode
private int[] oldCode
private DataOutput out
private int processedRows
private int width
public int getBackgroundColor()
setBackgroundColor(int)
public String[] getFileExtensions()
ImageCodec
null
.
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.getFileExtensions
in class ImageCodec
public String getFormatName()
ImageCodec
ImageCodec
must 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 BMP
or Portable Anymap (PNM)
.getFormatName
in class ImageCodec
public String[] getMimeTypes()
ImageCodec
null
if none are available.getMimeTypes
in class ImageCodec
private int getNextSample()
private void initEncoding() throws IOException
IOException
public boolean isInterlaced()
true
)
or non-interlaced mode (false
).setInterlacing(boolean)
public boolean isLoadingSupported()
ImageCodec
true
is returned this does not necessarily mean that all files in this
format can be read, but at least some.isLoadingSupported
in class ImageCodec
public boolean isSavingSupported()
ImageCodec
true
is returned this does not necessarily mean that all types files in this
format can be written, but at least some.isSavingSupported
in class ImageCodec
public void process() throws MissingParameterException, OperationFailedException
Operation
process
in class Operation
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
private void resetBlock()
private void resetEncoder()
private void save() throws MissingParameterException, OperationFailedException, UnsupportedTypeException, WrongParameterException
public void setBackgroundColor(int colorIndex)
0
.colorIndex
- int value with the color (index into the palette) of the background colorgetBackgroundColor()
public void setInterlacing(boolean useInterlacing)
true
) or non-interlaced mode (false
).useInterlacing
- boolean, if true interlaced mode, otherwise non-interlaced modeisInterlaced()
private void writeBlock() throws IOException
IOException
private void writeCode(int code) throws IOException
IOException
private void writeComments() throws IOException
IOException
private void writeHeader() throws IOException
IOException
private void writeImage() throws IOException
IOException
private void writePalette() throws IOException
IOException
private void writeShort(int value) throws IOException
IOException
private void writeStream() throws IOException
IOException
private void writeTrailer() throws IOException
IOException