# Class PngEncoder

java.lang.Object
javajs.img.ImageEncoder
javajs.img.PngEncoder

All Implemented Interfaces:
  `GenericImageEncoder`

public class PngEncoder extends ImageEncoder
Modified by Bob Hanson hansonr@stolaf.edu to be a subclass of ImageEncoder and to use javajs.util.OutputChannel instead of just returning bytes. Also includes: -- JavaScript-compatible image processing -- transparent background option -- more efficient calculation of needs for pngBytes -- option to use pre-created PNGJ image data (3/19/14; Jmol 14.1.12) -- PNGJ format: // IHDR chunk // tEXt chunk "Jmol type - invalid input: '<'PNG0|PNGJ>invalid input: '<'0000000pt>+invalid input: '<'000000len>" // tEXt chunk "Software - Jmol <version>" // tEXt chunk "Creation Time - <date>" // tRNS chunk transparent color, if desired // IDAT chunk (image data) // IEND chunk // [JMOL ZIP FILE APPENDIX] Original Comment: 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. Thanks to Jay Denny at KeyPoint Software http://www.keypoint.com/ who let me develop this code on company time. You may contact me with (probably very-much-needed) improvements, comments, and bug fixes at: david@catcode.com

Version:
  1.4, 31 March 2000
Author:
  J. David Eisenberg, http://catcode.com/pngencoder/, Christian Ribeaud (christian.ribeaud@genedata.com), Bob Hanson (hansonr@stolaf.edu)

## Field Summary

Fields
Modifier and Type
Field
Description
`protected int`
`bytePos`
`protected int`
`dataLen`
`static final int`
`FILTER_LAST`
`static final int`
`FILTER_NONE`
Constants for filters
`static final int`
`FILTER_SUB`
`static final int`
`FILTER_UP`
`protected byte[]`
`pngBytes`
`protected int`
`startPos`

### Fields inherited from class ImageEncoder

`date, doClose, dpi, height, logging, out, pixels, quality, width`

## Constructor Summary

Constructors
Constructor
Description
`PngEncoder()`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`generate()`
`protected byte[]`
`getBytes()`
`protected static void`
`getInt4(int n, byte[] int4)`
`protected void`
`setData(byte[] b, int pt)`
`protected void`
`setParams(Map<String,Object> params)`
`protected void`
`writeByte(int b)`
Write a single byte into the pngBytes array at a given position.
`protected void`
`writeBytes(byte[] data)`
Write an array of bytes into the pngBytes array.
`protected void`
`writeCRC()`
`protected void`
`writeInt2(int n)`
Write a two-byte integer into the pngBytes array at a given position.
`protected void`
`writeInt4(int n)`
Write a four-byte integer into the pngBytes array at a given position.
`protected void`
`writeString(String s)`
Write a string into the pngBytes array at a given position.

### Methods inherited from class ImageEncoder

`close, createImage, putByte, putString`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### FILTER_NONE

public static final int FILTER_NONE
Constants for filters

See Also:
  - Constant Field Values

### FILTER_SUB

public static final int FILTER_SUB

See Also:
  - Constant Field Values

### FILTER_UP

public static final int FILTER_UP

See Also:
  - Constant Field Values

### FILTER_LAST

public static final int FILTER_LAST

See Also:
  - Constant Field Values

### startPos

protected int startPos

### bytePos

protected int bytePos

### pngBytes

protected byte[] pngBytes

### dataLen

protected int dataLen

## Constructor Details

### PngEncoder

public PngEncoder()

## Method Details

### setParams

protected void setParams(Map<String,Object> params)

Specified by:
  `setParams` in class `ImageEncoder`

### generate

protected void generate() throws IOException

Specified by:
  `generate` in class `ImageEncoder`
Throws:
  `IOException`

### setData

protected void setData(byte[] b, int pt)

### getBytes

protected byte[] getBytes()

### writeCRC

protected void writeCRC()

### writeInt2

protected void writeInt2(int n)
Write a two-byte integer into the pngBytes array at a given position.

Parameters:
  `n` - The integer to be written into pngBytes.

### writeInt4

protected void writeInt4(int n)
Write a four-byte integer into the pngBytes array at a given position.

Parameters:
  `n` - The integer to be written into pngBytes.

### getInt4

protected static void getInt4(int n, byte[] int4)

### writeByte

protected void writeByte(int b)
Write a single byte into the pngBytes array at a given position.

Parameters:
  `b` - The byte to be written into pngBytes.

### writeString

protected void writeString(String s)
Write a string into the pngBytes array at a given position. This uses the getBytes method, so the encoding used will be its default.

Parameters:
  `s` - The string to be written into pngBytes.
See Also:
  - `String.getBytes()`

### writeBytes

protected void writeBytes(byte[] data)
Write an array of bytes into the pngBytes array. Both dataLen and bytePos are updated. If we don't have enough room, this is certainly in image data writing, so we add just enough for CRC END CRC

Parameters:
  `data` - The data to be written into pngBytes.