jj2000.j2k.codestream.writer
Class CodestreamWriter

java.lang.Object
  |
  +--jj2000.j2k.codestream.writer.CodestreamWriter
Direct Known Subclasses:
FileCodestreamWriter

public abstract class CodestreamWriter
extends java.lang.Object

This is the abstract class for writing to a codestream. A codestream corresponds to headers (main and tile-parts) and packets. Each packet has a head and a body. The codestream always has a maximum number of bytes that can be written to it. After that many number of bytes no more data is written to the codestream but the number of bytes is counted so that the value returned by getMaxAvailableBytes() is negative. If the number of bytes is unlimited a ridicoulosly large value, such as Integer.MAX_VALUE, is equivalent.

Data writting to the codestream can be simulated. In this case, no byto is effectively written to the codestream but the resulting number of bytes is calculated and returned (although it is not accounted in the bit stream). This can be used in rate control loops.

Implementing classes should write the header of the bit stream before writing any packets. The bit stream header can be written with the help of the HeaderEncoder class.

See Also:
HeaderEncoder

Field Summary
protected  int maxBytes
          The maximum number of bytes that can be written to the bit stream
protected  int ndata
          The number of bytes already written to the bit stream
 
Constructor Summary
protected CodestreamWriter(int mb)
          Allocates this object and initializes the maximum number of bytes.
 
Method Summary
abstract  void close()
          Closes the underlying resource (file, stream, network connection, etc.).
abstract  void commitBitstreamHeader(HeaderEncoder he)
          Writes the header data to the bit stream, if it has not been already done.
abstract  int getLength()
          Returns the current length of the entire codestream.
abstract  int getMaxAvailableBytes()
          Returns the number of bytes remaining available in the codestream.
abstract  int getOffLastROIPkt()
          Gives the offset of the end of last packet containing ROI information
abstract  int writePacketBody(byte[] body, int blen, boolean sim, boolean roiInPkt, int roiLen)
          Writes a packet body to the codestream and returns the number of bytes used by this body.
abstract  int writePacketHead(byte[] head, int hlen, boolean sim, boolean sop, boolean eph)
          Writes a packet head into the codestream and returns the number of bytes used by this header.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ndata

protected int ndata
The number of bytes already written to the bit stream


maxBytes

protected int maxBytes
The maximum number of bytes that can be written to the bit stream

Constructor Detail

CodestreamWriter

protected CodestreamWriter(int mb)
Allocates this object and initializes the maximum number of bytes.

Parameters:
mb - The maximum number of bytes that can be written to the codestream.
Method Detail

getMaxAvailableBytes

public abstract int getMaxAvailableBytes()
Returns the number of bytes remaining available in the codestream. This is the maximum allowed number of bytes minus the number of bytes that have already been written to the bit stream. If more bytes have been written to the bit stream than the maximum number of allowed bytes, then a negative value is returned.

Returns:
The number of bytes remaining available in the bit stream.

getLength

public abstract int getLength()
Returns the current length of the entire codestream.

Returns:
the current length of the codestream

writePacketHead

public abstract int writePacketHead(byte[] head,
                                    int hlen,
                                    boolean sim,
                                    boolean sop,
                                    boolean eph)
                             throws java.io.IOException
Writes a packet head into the codestream and returns the number of bytes used by this header. If in simulation mode then no data is effectively written to the codestream but the number of bytes is calculated. This can be used for iterative rate allocation.

If the number of bytes that has to be written to the codestream is more than the space left (as returned by getMaxAvailableBytes()), only the data that does not exceed the allowed length is effectively written and the rest is discarded. However the value returned by the method is the total length of the packet, as if all of it was written to the bit stream.

If the codestream header has not been commited yet and if 'sim' is false, then the bit stream header is automatically commited (see commitBitstreamHeader() method) before writting the packet.

Parameters:
head - The packet head data.
hlen - The number of bytes in the packet head.
sim - Simulation mode flag. If true nothing is written to the bit stream, but the number of bytes that would be written is returned.
sop - Start of packet header marker flag. This flag indicates whether or not SOP markers should be written. If true, SOP markers should be written, if false, they should not.
eph - End of Packet Header marker flag. This flag indicates whether or not EPH markers should be written. If true, EPH markers should be written, if false, they should not.
Returns:
The number of bytes spent by the packet head.
Throws:
java.io.IOException - If an I/O error occurs while writing to the output stream.
See Also:
commitBitstreamHeader(jj2000.j2k.codestream.writer.HeaderEncoder)

writePacketBody

public abstract int writePacketBody(byte[] body,
                                    int blen,
                                    boolean sim,
                                    boolean roiInPkt,
                                    int roiLen)
                             throws java.io.IOException
Writes a packet body to the codestream and returns the number of bytes used by this body. If in simulation mode then no data is written to the bit stream but the number of bytes is calculated. This can be used for iterative rate allocation.

If the number of bytes that has to be written to the codestream is more than the space left (as returned by getMaxAvailableBytes()), only the data that does not exceed the allowed length is effectively written and the rest is discarded. However the value returned by the method is the total length of the packet, as if all of it was written to the bit stream.

Parameters:
body - The packet body data.
blen - The number of bytes in the packet body.
sim - Simulation mode flag. If true nothing is written to the bit stream, but the number of bytes that would be written is returned.
roiInPkt - Whether or not there is ROI information in this packet
roiLen - Number of byte to read in packet body to get all the ROI information
Returns:
The number of bytes spent by the packet body.
Throws:
java.io.IOException - If an I/O error occurs while writing to the output stream.
See Also:
commitBitstreamHeader(jj2000.j2k.codestream.writer.HeaderEncoder)

close

public abstract void close()
                    throws java.io.IOException
Closes the underlying resource (file, stream, network connection, etc.). After a CodestreamWriter is closed no more data can be written to it.

Throws:
java.io.IOException - If an I/O error occurs while closing the resource.

commitBitstreamHeader

public abstract void commitBitstreamHeader(HeaderEncoder he)
                                    throws java.io.IOException
Writes the header data to the bit stream, if it has not been already done. In some implementations this method can be called only once, and an IllegalArgumentException is thrown if called more than once.

Throws:
java.io.IOException - If an I/O error occurs while writing the data.
java.lang.IllegalArgumentException - If this method has already been called.

getOffLastROIPkt

public abstract int getOffLastROIPkt()
Gives the offset of the end of last packet containing ROI information

Returns:
End of last ROI packet