Package bdsup2sub.tools
Class PngEncoder
java.lang.Object
bdsup2sub.tools.PngEncoder
- Direct Known Subclasses:
EnhancedPngEncoder
PngEncoder takes a Java Image object and creates a byte string which can be saved as a PNG file.
The Image is presumed to use the DirectColorModel.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
The byte position.protected int
The bytes-per-pixel.protected int
The compression level.protected CRC32
CRC.protected long
The CRC value.protected boolean
Encode alpha?protected int
The filter type.static final int
Constants for filter (LAST)static final int
Constants for filter (NONE)static final int
Constants for filter (SUB)static final int
Constants for filter (UP)protected int
The width.protected static final byte[]
IDAT tag.protected static final byte[]
IEND tag.protected static final byte[]
IHDR tag.protected Image
The image.protected byte[]
The left bytes.protected int
The byte position.protected byte[]
The png bytes.protected byte[]
The prior row.protected int
The width. -
Constructor Summary
ConstructorsConstructorDescriptionClass constructorPngEncoder
(Image image) Class constructor specifying Image to encode, with no alpha channel encoding.PngEncoder
(Image image, boolean encodeAlpha) Class constructor specifying Image to encode, and whether to encode alpha.PngEncoder
(Image image, boolean encodeAlpha, int whichFilter) Class constructor specifying Image to encode, whether to encode alpha, and filter to use.PngEncoder
(Image image, boolean encodeAlpha, int whichFilter, int compLevel) Class constructor specifying Image source to encode, whether to encode alpha, filter to use, and compression level. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
filterSub
(byte[] pixels, int startPos, int width) Perform "sub" filtering on the given row.protected void
filterUp
(byte[] pixels, int startPos, int width) Perform "up" filtering on the given row.int
Retrieve compression levelboolean
Retrieve alpha encoding status.int
Retrieve filtering schemebyte[]
Creates an array of bytes that is the PNG equivalent of the current image.protected byte[]
resizeByteArray
(byte[] array, int newLength) Increase or decrease the length of a byte array.void
setCompressionLevel
(int level) Set the compression level to usevoid
setEncodeAlpha
(boolean encodeAlpha) Set the alpha encoding on or off.void
setFilter
(int whichFilter) Set the filter to usevoid
Set the image to be encodedprotected int
writeByte
(int b, int offset) Write a single byte into the pngBytes array at a given position.protected int
writeBytes
(byte[] data, int offset) Write an array of bytes into the pngBytes array.protected int
writeBytes
(byte[] data, int nBytes, int offset) Write an array of bytes into the pngBytes array, specifying number of bytes to write.protected void
writeEnd()
Write a PNG "IEND" chunk into the pngBytes array.protected void
Write a PNG "IHDR" chunk into the pngBytes array.protected boolean
Write the image data into the pngBytes array.protected int
writeInt2
(int n, int offset) Write a two-byte integer into the pngBytes array at a given position.protected int
writeInt4
(int n, int offset) Write a four-byte integer into the pngBytes array at a given position.
-
Field Details
-
FILTER_NONE
public static final int FILTER_NONEConstants for filter (NONE)- See Also:
-
FILTER_SUB
public static final int FILTER_SUBConstants for filter (SUB)- See Also:
-
FILTER_UP
public static final int FILTER_UPConstants for filter (UP)- See Also:
-
FILTER_LAST
public static final int FILTER_LASTConstants for filter (LAST)- See Also:
-
IHDR
protected static final byte[] IHDRIHDR tag. -
IDAT
protected static final byte[] IDATIDAT tag. -
IEND
protected static final byte[] IENDIEND tag. -
pngBytes
protected byte[] pngBytesThe png bytes. -
priorRow
protected byte[] priorRowThe prior row. -
leftBytes
protected byte[] leftBytesThe left bytes. -
image
The image. -
width
protected int widthThe width. -
height
protected int heightThe width. -
bytePos
protected int bytePosThe byte position. -
maxPos
protected int maxPosThe byte position. -
crc
CRC. -
crcValue
protected long crcValueThe CRC value. -
encodeAlpha
protected boolean encodeAlphaEncode alpha? -
filter
protected int filterThe filter type. -
bytesPerPixel
protected int bytesPerPixelThe bytes-per-pixel. -
compressionLevel
protected int compressionLevelThe compression level.
-
-
Constructor Details
-
PngEncoder
public PngEncoder()Class constructor -
PngEncoder
Class constructor specifying Image to encode, with no alpha channel encoding.- Parameters:
image
- A Java Image object which uses the DirectColorModel- See Also:
-
PngEncoder
Class constructor specifying Image to encode, and whether to encode alpha.- Parameters:
image
- A Java Image object which uses the DirectColorModelencodeAlpha
- Encode the alpha channel? false=no; true=yes- See Also:
-
PngEncoder
Class constructor specifying Image to encode, whether to encode alpha, and filter to use.- Parameters:
image
- A Java Image object which uses the DirectColorModelencodeAlpha
- Encode the alpha channel? false=no; true=yeswhichFilter
- 0=none, 1=sub, 2=up- See Also:
-
PngEncoder
Class constructor specifying Image source to encode, whether to encode alpha, filter to use, and compression level.- Parameters:
image
- A Java Image objectencodeAlpha
- Encode the alpha channel? false=no; true=yeswhichFilter
- 0=none, 1=sub, 2=upcompLevel
- 0..9- See Also:
-
-
Method Details
-
setImage
Set the image to be encoded- Parameters:
image
- A Java Image object which uses the DirectColorModel- See Also:
-
pngEncode
public byte[] pngEncode()Creates an array of bytes that is the PNG equivalent of the current image.- Returns:
- an array of bytes, or null if there was a problem
-
setEncodeAlpha
public void setEncodeAlpha(boolean encodeAlpha) Set the alpha encoding on or off.- Parameters:
encodeAlpha
- false=no, true=yes
-
getEncodeAlpha
public boolean getEncodeAlpha()Retrieve alpha encoding status.- Returns:
- boolean false=no, true=yes
-
setFilter
public void setFilter(int whichFilter) Set the filter to use- Parameters:
whichFilter
- from constant list
-
getFilter
public int getFilter()Retrieve filtering scheme- Returns:
- int (see constant list)
-
setCompressionLevel
public void setCompressionLevel(int level) Set the compression level to use- Parameters:
level
- 0 through 9
-
getCompressionLevel
public int getCompressionLevel()Retrieve compression level- Returns:
- int in range 0-9
-
resizeByteArray
protected byte[] resizeByteArray(byte[] array, int newLength) Increase or decrease the length of a byte array.- Parameters:
array
- The original array.newLength
- The length you wish the new array to have.- Returns:
- Array of newly desired length. If shorter than the original, the trailing elements are truncated.
-
writeBytes
protected int writeBytes(byte[] data, int offset) Write an array of bytes into the pngBytes array. Note: This routine has the side effect of updating maxPos, the largest element written in the array. The array is resized by 1000 bytes or the length of the data to be written, whichever is larger.- Parameters:
data
- The data to be written into pngBytes.offset
- The starting point to write to.- Returns:
- The next place to be written to in the pngBytes array.
-
writeBytes
protected int writeBytes(byte[] data, int nBytes, int offset) Write an array of bytes into the pngBytes array, specifying number of bytes to write. Note: This routine has the side effect of updating maxPos, the largest element written in the array. The array is resized by 1000 bytes or the length of the data to be written, whichever is larger.- Parameters:
data
- The data to be written into pngBytes.nBytes
- The number of bytes to be written.offset
- The starting point to write to.- Returns:
- The next place to be written to in the pngBytes array.
-
writeInt2
protected int writeInt2(int n, int offset) Write a two-byte integer into the pngBytes array at a given position.- Parameters:
n
- The integer to be written into pngBytes.offset
- The starting point to write to.- Returns:
- The next place to be written to in the pngBytes array.
-
writeInt4
protected int writeInt4(int n, int offset) Write a four-byte integer into the pngBytes array at a given position.- Parameters:
n
- The integer to be written into pngBytes.offset
- The starting point to write to.- Returns:
- The next place to be written to in the pngBytes array.
-
writeByte
protected int writeByte(int b, int offset) Write a single byte into the pngBytes array at a given position.- Parameters:
b
- The integer to be written into pngBytes.offset
- The starting point to write to.- Returns:
- The next place to be written to in the pngBytes array.
-
writeHeader
protected void writeHeader()Write a PNG "IHDR" chunk into the pngBytes array. -
filterSub
protected void filterSub(byte[] pixels, int startPos, int width) Perform "sub" filtering on the given row. Uses temporary array leftBytes to store the original values of the previous pixels. The array is 16 bytes long, which will easily hold two-byte samples plus two-byte alpha.- Parameters:
pixels
- The array holding the scan lines being builtstartPos
- Starting position within pixels of bytes to be filtered.width
- Width of a scanline in pixels.
-
filterUp
protected void filterUp(byte[] pixels, int startPos, int width) Perform "up" filtering on the given row. Side effect: refills the prior row with current row- Parameters:
pixels
- The array holding the scan lines being builtstartPos
- Starting position within pixels of bytes to be filtered.width
- Width of a scanline in pixels.
-
writeImageData
protected boolean writeImageData()Write the image data into the pngBytes array. This will write one or more PNG "IDAT" chunks. In order to conserve memory, this method grabs as many rows as will fit into 32K bytes, or the whole image; whichever is less.- Returns:
- true if no errors; false if error grabbing pixels
-
writeEnd
protected void writeEnd()Write a PNG "IEND" chunk into the pngBytes array.
-