use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.
the class MicroscopyXmlproducer method getXML.
/**
* This methods returns a XML represnetation of a VCImage object.
* Creation date: (3/1/2001 3:02:37 PM)
* @param param cbit.image.VCImage
* @return Element
* @throws XmlParseException
*/
private static Element getXML(UShortImage param, Xmlproducer vcellXMLProducer, boolean bSaveCompressed) throws XmlParseException {
Element image = new Element(MicroscopyXMLTags.UShortImageTag);
// add atributes
if (param.getName() != null && param.getName().length() > 0) {
image.setAttribute(XMLTags.NameAttrTag, mangle(param.getName()));
}
// Add annotation
if (param.getDescription() != null && param.getDescription().length() > 0) {
Element annotationElement = new Element(XMLTags.AnnotationTag);
annotationElement.setText(mangle(param.getDescription()));
image.addContent(annotationElement);
}
// Add extent
if (param.getExtent() != null) {
image.addContent(vcellXMLProducer.getXML(param.getExtent()));
}
// Add Origin
if (param.getOrigin() != null) {
image.addContent(vcellXMLProducer.getXML(param.getOrigin()));
}
final int BYTES_PER_SHORT = 2;
int aNumX = param.getNumX();
int aNumY = param.getNumY();
int aNumZ = param.getNumZ();
// Add Imagedata subelement
int UNCOMPRESSED_SIZE_BYTES = aNumX * aNumY * aNumZ * BYTES_PER_SHORT;
short[] pixelsShort = param.getPixels();
byte[] compressedPixels = null;
boolean bForceUncompressed = false;
while (true) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStream dos = bos;
if (bSaveCompressed && !bForceUncompressed) {
dos = new DeflaterOutputStream(bos);
}
ByteBuffer byteBuffer = ByteBuffer.allocate(UNCOMPRESSED_SIZE_BYTES);
for (int i = 0; i < pixelsShort.length; i++) {
byteBuffer.putShort(pixelsShort[i]);
}
try {
dos.write(byteBuffer.array());
} catch (IOException e) {
e.printStackTrace(System.out);
throw new XmlParseException("failed to create compressed pixel data");
} finally {
if (dos != null) {
try {
dos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
compressedPixels = bos.toByteArray();
if (!bSaveCompressed || bForceUncompressed) {
if (UNCOMPRESSED_SIZE_BYTES != compressedPixels.length) {
throw new XmlParseException("failed to create uncompressed pixel data");
}
} else if (!bForceUncompressed) {
if (UNCOMPRESSED_SIZE_BYTES <= compressedPixels.length) {
// Compression didn't compress
// Save uncompressed so reader not confused
bForceUncompressed = true;
continue;
}
}
break;
}
Element imagedata = new Element(XMLTags.ImageDataTag);
// Get imagedata attributes
imagedata.setAttribute(XMLTags.XAttrTag, String.valueOf(aNumX));
imagedata.setAttribute(XMLTags.YAttrTag, String.valueOf(aNumY));
imagedata.setAttribute(XMLTags.ZAttrTag, String.valueOf(aNumZ));
imagedata.setAttribute(XMLTags.CompressedSizeTag, String.valueOf(compressedPixels.length));
// Get imagedata content
// encode
imagedata.addContent(org.vcell.util.Hex.toString(compressedPixels));
// Add imagedata to VCImage element
image.addContent(imagedata);
return image;
}
use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.
the class VCImageUncompressed method getPixelsCompressed.
/**
* This method was created in VisualAge.
* @return byte[]
*/
public byte[] getPixelsCompressed() throws ImageException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(bos);
// DeflaterOutputStream dos = new DeflaterOutputStream(bos,new Deflater(5,false));
dos.write(pixels, 0, pixels.length);
dos.close();
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace(System.out);
throw new ImageException(e.getMessage());
}
}
use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.
the class ByteImage method getPixelsCompressed.
/**
* This method was created in VisualAge.
* @return byte[]
*/
public byte[] getPixelsCompressed() throws ImageException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(bos);
// DeflaterOutputStream dos = new DeflaterOutputStream(bos,new Deflater(5,false));
dos.write(pixels, 0, pixels.length);
dos.close();
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace(System.out);
throw new ImageException(e.getMessage());
}
}
use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.
the class BeanUtils method toCompressedSerialized.
public static byte[] toCompressedSerialized(Serializable cacheObj) throws java.io.IOException {
byte[] bytes = toSerialized(cacheObj);
long before = System.currentTimeMillis();
byte[] objData = null;
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(bos);
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(dos);
oos.writeObject(cacheObj);
oos.flush();
dos.close();
bos.flush();
objData = bos.toByteArray();
oos.close();
bos.close();
long after = System.currentTimeMillis();
System.out.println("BeanUtils.toSerialized, t=" + (after - before) + " ms, (" + cacheObj + ") ratio=" + objData.length + "/" + bytes.length);
return objData;
}
use of java.util.zip.DeflaterOutputStream in project vcell by virtualcell.
the class VCellApiClient method toCompressedSerialized.
public static byte[] toCompressedSerialized(Serializable cacheObj) throws java.io.IOException {
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(bos);
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(dos);
oos.writeObject(cacheObj);
oos.flush();
dos.close();
bos.flush();
byte[] objData = bos.toByteArray();
oos.close();
bos.close();
return objData;
}
Aggregations