/**
* Copyright: t3am_C9 (Alexander Schäffer, Johannes Ebersold, Sebastian Geib, Thomas Kisiel)
*
* This file is part of GifToApngConverter
*
* GifToApngConverter is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GifToApngConverter 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GifToApngConverter. If not, see <a href="http://www.gnu.org/licenses/">here</a>
*/
package giftoapng;
import java.util.Arrays;
import java.util.zip.Deflater;
import util.ByteBuilder;
/**
* Holds the Imagedata for the first frame of the (A)PNG. It will be created with raw-image-bytes
* and uses the deflate-class to compress the data to fit into the png-format.
* For more information look here: <a href="http://www.w3.org/TR/PNG/">PNG-specifcation</a>
* @author Alexander Schäffer
*
*/
public class ChunkIDAT extends Chunk implements iHasSequence {
private byte[] mbaDeflatedStream;
public ChunkIDAT(byte[] imagedata){
mbaHeader=new Property("IDAT");
byte[] output = new byte[1024]; //buffer to read from Deflater
ByteBuilder deflated=new ByteBuilder(); //complete deflate-stream
Deflater compresser = new Deflater(Deflater.DEFAULT_COMPRESSION); //deflater-obj
compresser.setInput(imagedata);
compresser.finish(); //indicate: there is no more input
int compressedDataLength=0;
while( compresser.finished()==false ){ //reading loop
int tmpComprLength = compresser.deflate(output);
//if stop-button is clicked
if(Thread.currentThread().isInterrupted()){
return;
}
deflated.appendArray(
(tmpComprLength==output.length)
?output
:Arrays.copyOf(output, tmpComprLength));
//append new data to existing data
compressedDataLength+=tmpComprLength; //total read bytes so far
}
assert(compresser.getBytesWritten()==compressedDataLength);
mbaDeflatedStream=deflated.getArray();
}
@Override
protected byte[] getData() {
return mbaDeflatedStream;
}
@Override
public int getSequenceNumber()
{
return -1; // means that we don't have a real sequencnumber
}
}