import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
FileOutputStream out=new FileOutputStream( "a.jpeg ");
BufferedImage image=new BufferedImage(150,100,BufferedImage.TYPE_INT_RGB);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
FileOutputStream out=new FileOutputStream( "a.jpeg ");
BufferedImage image=new BufferedImage(150,100,BufferedImage.TYPE_INT_RGB);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
jdk1.2就有
javax.swing
類 ImageIcon
java.lang.Object
javax.swing.ImageIcon
- 所有已實現的接口:
- Serializable, Accessible, Icon
public class ImageIconextends Objectimplements Icon, Serializable, Accessible
一個 Icon 接口的實現,它根據 Image 繪製 Icon。可使用 MediaTracker 預載根據 URL、文件名或字節數組創建的圖像,以監視該圖像的加載狀態。
有關使用圖像圖標的更多信息和示例,請參閱《The Java Tutorial》中的 How to Use Icons 一節。
警告:此類的序列化對象與以後的 Swing 版本不兼容。當前序列化支持適用於短期存儲,或適用於在運行相同 Swing 版本的應用程序之間進行 RMI(Remote Method Invocation,遠程方法調用)。從 1.4 版本開始,已在 java.beans
包中添加了支持所有 JavaBeansTM 長期存儲的功能。請參見 XMLEncoder
。
嵌套類摘要 | |
---|---|
protected class |
ImageIcon.AccessibleImageIcon 此類實現 ImageIcon 類的可訪問性支持。 |
字段摘要 | |
---|---|
protected static Component |
component |
protected static MediaTracker |
tracker |
構造方法摘要 | |
---|---|
ImageIcon() 創建一個未初始化的圖像圖標。 |
|
ImageIcon(byte[] imageData) 根據字節數組創建一個 ImageIcon,這些字節讀取自一個包含受支持圖像格式(比如 GIF、JPEG 或從 1.3 版本開始的 PNG)的圖像文件。 |
|
ImageIcon(byte[] imageData, String description) 根據字節數組創建一個 ImageIcon,這些字節讀取自一個包含受支持圖像格式(比如 GIF、JPEG 或從 1.3 版本開始的 PNG)的圖像文件。 |
|
ImageIcon(Image image) 根據圖像對象創建一個 ImageIcon。 |
|
ImageIcon(Image image, String description) 根據圖像創建一個 ImageIcon。 |
|
ImageIcon(String filename) 根據指定的文件創建一個 ImageIcon。 |
|
ImageIcon(String filename, String description) 根據指定的文件創建一個 ImageIcon。 |
|
ImageIcon(URL location) 根據指定的 URL 創建一個 ImageIcon。 |
|
ImageIcon(URL location, String description) 根據指定的 URL 創建一個 ImageIcon。 |
方法摘要 | |
---|---|
AccessibleContext |
getAccessibleContext() 獲得與此 ImageIcon 關聯的 AccessibleContext。 |
String |
getDescription() 獲得圖像的描述。 |
int |
getIconHeight() 獲得圖標的高度。 |
int |
getIconWidth() 獲得圖標的寬度。 |
Image |
getImage() 返回此圖標的 Image 。 |
int |
getImageLoadStatus() 返回圖像加載操作的狀態。 |
ImageObserver |
getImageObserver() 返回圖像的圖像觀察者。 |
protected void |
loadImage(Image image) 加載圖像,並且只在圖像已加載時返回。 |
void |
paintIcon(Component c, Graphics g, int x, int y) 繪製圖標。 |
void |
setDescription(String description) 設置圖像的描述。 |
void |
setImage(Image image) 設置由此圖標顯示的圖像。 |
void |
setImageObserver(ImageObserver observer) 設置圖像的圖像觀察者。 |
String |
toString() 返回此圖像的字符串表示形式。 |
從類 java.lang.Object 繼承的方法 |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
字段詳細信息 |
---|
component
protected static final Component component
tracker
protected static final MediaTracker tracker
構造方法詳細信息 |
---|
ImageIcon
public ImageIcon(String filename, String description)
- 根據指定的文件創建一個 ImageIcon。使用 MediaTracker 預載圖像以監視圖像的加載狀態。
- 參數:
filename
- 包含圖像的文件的名稱description
- 圖像的簡明文本描述- 另請參見:
ImageIcon(String)
ImageIcon
public ImageIcon(String filename)
- 根據指定的文件創建一個 ImageIcon。使用 MediaTracker 預載圖像以監視圖像的加載狀態。指定 String 可以是一個文件名或是一條文件路徑。在指定一條路徑時,可使用 Internet 標準正斜槓 ("/") 作爲分隔符。(該字符串被轉換成一個 URL,而正斜槓適用於所有系統。)例如,指定:
new ImageIcon("images/myImage.gif")
該描述被初始化爲filename
字符串。- 參數:
filename
- 指定文件名或路徑的 String- 另請參見:
getDescription()
ImageIcon
public ImageIcon(URL location, String description)
- 根據指定的 URL 創建一個 ImageIcon。使用 MediaTracker 預載圖像以監視圖像的加載狀態。
- 參數:
location
- 圖像的 URLdescription
- 圖像的簡明文本描述- 另請參見:
ImageIcon(String)
ImageIcon
public ImageIcon(URL location)
- 根據指定的 URL 創建一個 ImageIcon。使用 MediaTracker 預載圖像以監視圖像的加載狀態。圖標的描述被初始化爲 URL 的字符串表示形式。
- 參數:
location
- 圖像的 URL- 另請參見:
getDescription()
ImageIcon
public ImageIcon(Image image, String description)
- 根據圖像創建一個 ImageIcon。
- 參數:
image
- 圖像description
- 圖像的簡明文本描述
ImageIcon
public ImageIcon(Image image)
- 根據圖像對象創建一個 ImageIcon。如果圖像有一個字符串形式的 "comment" 屬性,則該字符串被用作此圖標的描述。
- 參數:
image
- 圖像- 另請參見:
getDescription()
,Image.getProperty(java.lang.String, java.awt.image.ImageObserver)
ImageIcon
public ImageIcon(byte[] imageData, String description)
- 根據字節數組創建一個 ImageIcon,這些字節讀取自一個包含受支持圖像格式(比如 GIF、JPEG 或從 1.3 版本開始的 PNG)的圖像文件。通常,此數組是通過使用 Class.getResourceAsStream() 讀取圖像文件來創建的,但該字節數組也可以靜態地存儲在某個類中。
- 參數:
imageData
- 一個像素數組,具有受 AWT Toolkit 支持的圖像格式,比如 GIF、JPEG 或從 1.3 版本開始的 PNGdescription
- 圖像的簡明文本描述- 另請參見:
Toolkit.createImage(java.lang.String)
ImageIcon
public ImageIcon(byte[] imageData)
- 根據字節數組創建一個 ImageIcon,這些字節讀取自一個包含受支持圖像格式(比如 GIF、JPEG 或從 1.3 版本開始的 PNG)的圖像文件。通常,此數組是通過使用 Class.getResourceAsStream() 讀取一幅圖像來創建的,但該字節數組也可以靜態地存儲在某個類中。如果得到的圖像有一個字符串形式的 "comment" 屬性,則該字符串被用作此圖標的描述。
- 參數:
imageData
- 一個像素數組,具有受 AWT Toolkit 支持的圖像格式,比如 GIF、JPEG 或從 1.3 版本開始的 PNG- 另請參見:
Toolkit.createImage(java.lang.String)
,getDescription()
,Image.getProperty(java.lang.String, java.awt.image.ImageObserver)
ImageIcon
public ImageIcon()
- 創建一個未初始化的圖像圖標。
方法詳細信息 |
---|
loadImage
protected void loadImage(Image image)
- 加載圖像,並且只在圖像已加載時返回。
- 參數:
image
- 圖像
getImageLoadStatus
public int getImageLoadStatus()
- 返回圖像加載操作的狀態。
- 返回:
- 由 java.awt.MediaTracker 定義的加載狀態
- 另請參見:
MediaTracker.ABORTED
,MediaTracker.ERRORED
,MediaTracker.COMPLETE
getImage
public Image getImage()
- 返回此圖標的
Image
。 - 返回:
- 此
ImageIcon
的Image
對象
setImage
public void setImage(Image image)
- 設置由此圖標顯示的圖像。
- 參數:
image
- 圖像
getDescription
public String getDescription()
- 獲得圖像的描述。此描述應該是該對象的簡明文本描述。例如,可以將它呈現給盲人用戶,爲他們提供該圖像用途的指示。該描述可以爲 null。
- 返回:
- 圖像的簡明文本描述。
setDescription
public void setDescription(String description)
- 設置圖像的描述。此描述應該是該對象的簡明文本描述。例如,可以將它呈現給盲人用戶,爲他們提供該圖像用途的指示。
- 參數:
description
- 圖像的簡明文本描述。
paintIcon
public void paintIcon(Component c, Graphics g, int x, int y)
- 繪製圖標。圖標的左上角在圖形上下文
g
的座標空間中的點 (x
,y
) 處繪製。如果此圖標沒有圖像觀察者,則此方法使用c
組件作爲觀察者。 - 指定者:
- 接口
Icon
中的paintIcon
- 參數:
c
- 用作觀察者的組件,如果此圖標沒有圖像觀察者g
- 圖形上下文x
- 圖標左上角的 X 座標y
- 圖標左上角的 Y 座標
getIconWidth
public int getIconWidth()
- 獲得圖標的寬度。
- 指定者:
- 接口
Icon
中的getIconWidth
- 返回:
- 此圖標的寬度,以像素爲單位
getIconHeight
public int getIconHeight()
- 獲得圖標的高度。
- 指定者:
- 接口
Icon
中的getIconHeight
- 返回:
- 此圖標的高度,以像素爲單位
setImageObserver
public void setImageObserver(ImageObserver observer)
- 設置圖像的圖像觀察者。如果 ImageIcon 包含一個動畫 GIF,則設置此屬性,而觀察者獲得更新其顯示的通知。例如:
icon = new ImageIcon(...) button.setIcon(icon); icon.setImageObserver(button);
- 參數:
observer
- 圖像觀察者
getImageObserver
public ImageObserver getImageObserver()
- 返回圖像的圖像觀察者。
- 返回:
- 圖像觀察者,它可以爲 null
toString
public String toString()
- 返回此圖像的字符串表示形式。
- 覆蓋:
- 類
Object
中的toString
- 返回:
- 表示此圖像的字符串
getAccessibleContext
public AccessibleContext getAccessibleContext()
- 獲得與此 ImageIcon 關聯的 AccessibleContext。對於圖像圖標,AccessibleContext 採用 AccessibleImageIcon 的形式。如有必要,可創建一個新的 AccessibleImageIcon 實例。
- 指定者:
- 接口
Accessible
中的getAccessibleContext
- 返回:
- 一個 AccessibleImageIcon,它將充當此 ImageIcon 的 AccessibleContext
將Java image對象轉換成PNG格式字節數組 |
發佈時間:2006.04.26 15:32 來源:天新網 作者: |
/** * 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: * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * A copy of the GNU LGPL may be found at * http://www.gnu.org/copyleft/lesser.html, * * @author J. David Eisenberg * @version 1.4, 31 March 2000 */
import java.awt.*; import java.awt.image.*; import java.util.*; import java.util.zip.*; import java.io.*;
public class PngEncoder extends Object { /** Constant specifying that alpha channel should be encoded. */ public static final boolean ENCODE_ALPHA=true; /** Constant specifying that alpha channel should not be encoded. */ public static final boolean NO_ALPHA=false; /** Constants for filters */ public static final int FILTER_NONE = 0; public static final int FILTER_SUB = 1; public static final int FILTER_UP = 2; public static final int FILTER_LAST = 2;
protected byte[] pngBytes; protected byte[] priorRow; protected byte[] leftBytes; protected Image image; protected int width, height; protected int bytePos, maxPos; protected int hdrPos, dataPos, endPos; protected CRC32 crc = new CRC32(); protected long crcValue; protected boolean encodeAlpha; protected int filter; protected int bytesPERPixel; protected int compressionLevel;
/** * Class constructor * */ public PngEncoder() { this( null, false, FILTER_NONE, 0 ); }
/** * Class constructor specifying Image to encode, with no alpha channel encoding. * * @param image A Java Image object which uses the DirectColorModel * @see java.awt.Image */ public PngEncoder( Image image ) { this(image, false, FILTER_NONE, 0); }
/** * Class constructor specifying Image to encode, and whether to encode alpha. * * @param image A Java Image object which uses the DirectColorModel * @param encodeAlpha Encode the alpha channel? false=no; true=yes * @see java.awt.Image */ public PngEncoder( Image image, boolean encodeAlpha ) { this(image, encodeAlpha, FILTER_NONE, 0); }
/** * Class constructor specifying Image to encode, whether to encode alpha, and filter to use. * * @param image A Java Image object which uses the DirectColorModel * @param encodeAlpha Encode the alpha channel? false=no; true=yes * @param whichFilter 0=none, 1=sub, 2=up * @see java.awt.Image */ public PngEncoder( Image image, boolean encodeAlpha, int whichFilter ) { this( image, encodeAlpha, whichFilter, 0 ); }
/** * Class constructor specifying Image source to encode, whether to encode alpha, filter to use, and compression level. * * @param image A Java Image object * @param encodeAlpha Encode the alpha channel? false=no; true=yes * @param whichFilter 0=none, 1=sub, 2=up * @param compLevel 0..9 * @see java.awt.Image */ public PngEncoder( Image image, boolean encodeAlpha, int whichFilter, int compLevel) { this.image = image; this.encodeAlpha = encodeAlpha; setFilter( whichFilter ); if (compLevel >=0 && compLevel <=9) { this.compressionLevel = compLevel; } }
/** * Set the image to be encoded * * @param image A Java Image object which uses the DirectColorModel * @see java.awt.Image * @see java.awt.image.DirectColorModel */ public void setImage( Image image ) { this.image = image; pngBytes = null; }
/** * Creates an array of bytes that is the PNG equivalent of the current image, specifying whether to encode alpha or not. * * @param encodeAlpha boolean false=no alpha, true=encode alpha * @return an array of bytes, or null if there was a problem */ public byte[] pngEncode( boolean encodeAlpha ) { byte[] pngIdBytes = { -119, 80, 78, 71, 13, 10, 26, 10 }; int i;
if (image == null) { return null; } width = image.getWidth( null ); height = image.getHeight( null ); this.image = image;
/* * start with an array that is big enough to hold all the pixels * (plus filter bytes), and an extra 200 bytes for header info */ pngBytes = new byte[((width+1) * height * 3) + 200];
/* * keep track of largest byte written to the array */ maxPos = 0;
bytePos = writeBytes( pngIdBytes, 0 ); hdrPos = bytePos; writeHeader(); dataPos = bytePos; if (writeImageData()) { writeEnd(); pngBytes = resizeByteArray( pngBytes, maxPos ); } else { pngBytes = null; } return pngBytes; }
/** * Creates an array of bytes that is the PNG equivalent of the current image. * Alpha encoding is determined by its setting in the constructor. * * @return an array of bytes, or null if there was a problem */ public byte[] pngEncode() { return pngEncode( encodeAlpha ); }
/** * Set the alpha encoding on or off. * * @param encodeAlpha false=no, true=yes */ public void setEncodeAlpha( boolean encodeAlpha ) { this.encodeAlpha = encodeAlpha; }
/** * Retrieve alpha encoding status. * * @return boolean false=no, true=yes */ public boolean getEncodeAlpha() { return encodeAlpha; }
/** * Set the filter to use * * @param whichFilter from constant list */ public void setFilter( int whichFilter ) { this.filter = FILTER_NONE; if ( whichFilter <= FILTER_LAST ) { this.filter = whichFilter; } }
/** * Retrieve filtering scheme * * @return int (see constant list) */ public int getFilter() { return filter; }
/** * Set the compression level to use * * @param level 0 through 9 */ public void setCompressionLevel( int level ) { if ( level >= 0 && level <= 9) { this.compressionLevel = level; } }
/** * Retrieve compression level * * @return int in range 0-9 */ public int getCompressionLevel() { return compressionLevel; }
/** * Increase or decrease the length of a byte array. * * @param array The original array. * @param newLength The length you wish the new array to have. * @return Array of newly desired length. If shorter than the * original, the trailing elements are truncated. */ protected byte[] resizeByteArray( byte[] array, int newLength ) { byte[] newArray = new byte[newLength]; int oldLength = array.length;
System.arraycopy( array, 0, newArray, 0, Math.min( oldLength, newLength ) ); return newArray; }
/** * 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. * * @param data The data to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeBytes( byte[] data, int offset ) { maxPos = Math.max( maxPos, offset + data.length ); if (data.length + offset > pngBytes.length) { pngBytes = resizeByteArray( pngBytes, pngBytes.length + Math.max( 1000, data.length ) ); } System.arraycopy( data, 0, pngBytes, offset, data.length ); return offset + data.length; }
/** * 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. * * @param data The data to be written into pngBytes. * @param nBytes The number of bytes to be written. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeBytes( byte[] data, int nBytes, int offset ) { maxPos = Math.max( maxPos, offset + nBytes ); if (nBytes + offset > pngBytes.length) { pngBytes = resizeByteArray( pngBytes, pngBytes.length + Math.max( 1000, nBytes ) ); } System.arraycopy( data, 0, pngBytes, offset, nBytes ); return offset + nBytes; }
/** * Write a two-byte integer into the pngBytes array at a given position. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeInt2( int n, int offset ) { byte[] temp = { (byte)((n >> 8) & 0xff), (byte) (n & 0xff) }; return writeBytes( temp, offset ); }
/** * Write a four-byte integer into the pngBytes array at a given position. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeInt4( int n, int offset ) { byte[] temp = { (byte)((n >> 24) & 0xff), (byte) ((n >> 16) & 0xff ), (byte) ((n >> 8) & 0xff ), (byte) ( n & 0xff ) }; return writeBytes( temp, offset ); }
/** * Write a single byte into the pngBytes array at a given position. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeByte( int b, int offset ) { byte[] temp = { (byte) b }; return writeBytes( temp, offset ); }
/** * Write a string into the pngBytes array at a given position. * This uses the getBytes method, so the encoding used will * be its default. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. * @see java.lang.String#getBytes() */ protected int writeString( String s, int offset ) { return writeBytes( s.getBytes(), offset ); }
/** * Write a PNG "IHDR" chunk into the pngBytes array. */ protected void writeHeader() { int startPos;
startPos = bytePos = writeInt4( 13, bytePos ); bytePos = writeString( "IHDR", bytePos ); width = image.getWidth( null ); height = image.getHeight( null ); bytePos = writeInt4( width, bytePos ); bytePos = writeInt4( height, bytePos ); bytePos = writeByte( 8, bytePos ); // bit depth bytePos = writeByte( (encodeAlpha) ? 6 : 2, bytePos ); // direct model bytePos = writeByte( 0, bytePos ); // compression method bytePos = writeByte( 0, bytePos ); // filter method bytePos = writeByte( 0, bytePos ); // no interlace crc.reset(); crc.update( pngBytes, startPos, bytePos-startPos ); crcValue = crc.getValue(); bytePos = writeInt4( (int) crcValue, bytePos ); }
/** * 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. * * @param pixels The array holding the scan lines being built * @param startPos Starting position within pixels of bytes to be filtered. * @param width Width of a scanline in pixels. */ protected void filterSub( byte[] pixels, int startPos, int width ) { int i; int offset = bytesPerPixel; int actualStart = startPos + offset; int nBytes = width * bytesPerPixel; int leftInsert = offset; int leftExtract = 0; byte current_byte;
for (i=actualStart; i < startPos + nBytes; i++) { leftBytes[leftInsert] = pixels[i]; pixels[i] = (byte) ((pixels[i] - leftBytes[leftExtract]) % 256); leftInsert = (leftInsert+1) % 0x0f; leftExtract = (leftExtract + 1) % 0x0f; } }
/** * Perform "up" filtering on the given row. * Side effect: refills the prior row with current row * * @param pixels The array holding the scan lines being built * @param startPos Starting position within pixels of bytes to be filtered. * @param width Width of a scanline in pixels. */ protected void filterUp( byte[] pixels, int startPos, int width ) { int i, nBytes; byte current_byte;
nBytes = width * bytesPerPixel;
for (i=0; i < nBytes; i++) { current_byte = pixels[startPos + i]; pixels[startPos + i] = (byte) ((pixels[startPos + i] - priorRow[i]) % 256); priorRow[i] = current_byte; } }
/** * 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. * * * @return true if no errors; false if error grabbing pixels */ protected boolean writeImageData() { int rowsLeft = height; // number of rows remaining to write int startRow = 0; // starting row to process this time through int nRows; // how many rows to grab at a time
byte[] scanLines; // the scan lines to be compressed int scanPos; // where we are in the scan lines int startPos; // where this line's actual pixels start (used for filtering)
byte[] compressedLines; // the resultant compressed lines int nCompressed; // how big is the compressed area?
int depth; // color depth ( handle only 8 or 32 )
PixelGrabber pg;
bytesPerPixel = (encodeAlpha) ? 4 : 3;
Deflater scrunch = new Deflater( compressionLevel ); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024);
DeflaterOutputStream comPBytes = new DeflaterOutputStream( outBytes, scrunch ); try { while (rowsLeft > 0) { nRows = Math.min( 32767 / (width*(bytesPerPixel+1)), rowsLeft ); // nRows = rowsLeft;
int[] pixels = new int[width * nRows];
pg = new PixelGrabber(image, 0, startRow, width, nRows, pixels, 0, width); try { pg.grabPixels(); } catch (Exception e) { System.err.println("interrupted waiting for pixels!"); return false; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return false; }
/* * Create a data chunk. scanLines adds "nRows" for * the filter bytes. */ scanLines = new byte[width * nRows * bytesPerPixel + nRows];
if (filter == FILTER_SUB) { leftBytes = new byte[16]; } if (filter == FILTER_UP) { priorRow = new byte[width*bytesPerPixel]; }
scanPos = 0; startPos = 1; for (int i=0; i { if (i % width == 0) { scanLines[scanPos++] = (byte) filter; startPos = scanPos; } scanLines[scanPos++] = (byte) ((pixels[i] >> 16) & 0xff); scanLines[scanPos++] = (byte) ((pixels[i] >> 8) & 0xff); scanLines[scanPos++] = (byte) ((pixels[i] ) & 0xff); if (encodeAlpha) { scanLines[scanPos++] = (byte) ((pixels[i] >> 24) & 0xff ); } if ((i % width == width-1) && (filter != FILTER_NONE)) { if (filter == FILTER_SUB) { filterSub( scanLines, startPos, width ); } if (filter == FILTER_UP) { filterUp( scanLines, startPos, width ); } } }
/* * Write these lines to the output area */ compBytes.write( scanLines, 0, scanPos );
startRow += nRows; rowsLeft -= nRows; } compBytes.close();
/* * Write the compressed bytes */ compressedLines = outBytes.toByteArray(); nCompressed = compressedLines.length;
crc.reset(); bytePos = writeInt4( nCompressed, bytePos ); bytePos = writeString("IDAT", bytePos ); crc.update("IDAT".getBytes()); bytePos = writeBytes( compressedLines, nCompressed, bytePos ); crc.update( compressedLines, 0, nCompressed );
crcValue = crc.getValue(); bytePos = writeInt4( (int) crcValue, bytePos ); scrunch.finish(); return true; } catch (IOException e) { System.err.println( e.toString()); return false; } }
/** * Write a PNG "IEND" chunk into the pngBytes array. */ protected void writeEnd() { bytePos = writeInt4( 0, bytePos ); bytePos = writeString( "IEND", bytePos ); crc.reset(); crc.update("IEND".getBytes()); crcValue = crc.getValue(); bytePos = writeInt4( (int) crcValue, bytePos ); } } |