Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

upload a planarImage via ftp 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
hi,

i got my ftp client ready to move files to a server. however, how could i send a PlanarImage if I don't want to save it to hard drive first? e.g. resize and send to server (without saving to a temp file)

any clue?
cheers.
 
okay, that sexy and clean. I'll post my code later. thanks for that sedj.
 
sedj,

how could you read a ByteArrayOutputStream into a ByteArrayInputStream in order to retrieve a BufferedImage afterwards?
 
Maybe I should state what i try to accomplish.

I have a class called ImageTreater (hehe... trashy name) with the public method compress(). I simply want it to apply certain JPEG quality and return a PlanarImage. I DO NOT WANT TO SAVE IT TO DISK BY NOW. However, I casted, I serialized, I "byted" but I failed to put the outputstream back into a PlanarImage (or something else I could use). Any clue on how to fill PlanarIamge or BufferedImage object with a outputstream?

hope that's my last question for a while. thanks a lot.
method
------------------------------------------------

public PlanarImage compress(PlanarImage img){
BufferedImage buffImg = img.getAsBufferedImage();
ByteArrayOutputStream out = new ByteArrayOutputStream( 0xfff );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param;
param = encoder.getDefaultJPEGEncodeParam(buffImg);
param.setQuality(0.5F, true);
try {
encoder.encode(buffImg, param);
}
catch (IOException e) {
System.out.println("IOException at encoding..");
}
return out.???????
}
 
Here ya go - haven't tried compiling or running, but I think it should work :)

Code:
public BufferedImage compress(PlanarImage img) {
 	// not sure you actually need this line - 
 	// try encoding the PlanarImage directly
	BufferedImage buffImg = img.getAsBufferedImage();	

	// The image to return	
	BufferedImage retImg = null;
	
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
	JPEGEncodeParam param;
	param = encoder.getDefaultJPEGEncodeParam(buffImg);
	param.setQuality(0.5F, true);
	try {
		// encode the image
	  	encoder.encode(buffImg, param);

	  	// write the image into a byte[] array
	  	byte[] compressedData = out.toByteArray();

	  	// now read that data back into a BufferedImage
	  	ByteArrayInputStream bais = new ByteArrayInputStream(compressedData);
	  	retImg = javax.imageio.ImageIO.read(bais);

	}
	catch (IOException e) {
		e.printStackTrace(System.err);
	}
	
	return retImg;
  }

--------------------------------------------------
Free Database Connection Pooling Software
 
last question: how do you get from a BufferedImaged to a PlanarImage? visversa is clear to me.

Holy moly! that seems limp-wristed... are you working as a contractor? if not, let me know. we might need some for our sap project ;) you got a website with your own posts?

Anyway. thanks a lot for all your help regarding JAI an image manipulation. I certainly appreciate.

Cheers

 
Careful russland - this site doesn't allow recruiting [rofl]
My site is on the bottom of my posts.

I *think* you can create a PlanarImage from a BufferedImage like this :

Code:
BufferedImage bi ...
PlanarImage pi = PlanarImage.wrapRenderedImage(bi) ;

because BufferedImage implements RenderedImage

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj.... Okay. I ordered a JAVA image processing book from amazon. i hope i wouldn't have to bother you anymore.

however, another question came up. when i resize using
PlanarImage pi = JAI.create("scale", param, null)
the saved pi (JAI.create("filestore", pi)) file size becomes bigger than the orginal one. even though it's resized to thumbnail. adding compresssion makes it more uglier but doesn't change the file size. is PlanarImage a bad Object to use for JAI.create("filestore", pi)? or what else might be the reason for it to become way larger?
from 16KB up to 254KB. Strange enough is that it is exactly the geometrical size I wanted. so the resizeFactor is correct.

code snippet i use to resize
---------------------------------------------------
public PlanarImage resize(String imgPath, float resizeFactor){
RenderedImage source = JAI.create("fileload", imgPath);
ParameterBlock pb = new ParameterBlock();
pb = new ParameterBlock();
pb.addSource(source);
pb.add(resizeFactor);
pb.add(resizeFactor);
pb.add(0.0f);
pb.add(0.0f);
pb.add(new InterpolationNearest());
PlanarImage img = JAI.create("scale", pb, null);

return img;
}

 
It entirely depends on the source image format, the compression used, whether the format is lossy etc etc etc.

For example, you can load an LZW TIFF into JAI, then resave it with Group4 compression, and the file will be 4 - 10 times the size.

It also depends what you've done with the image, how you've rationailized the colours etc

To really answer you, we would need to know the original file format, the compression, the lossy factor if applicatble, and all these factors for the destination image.

--------------------------------------------------
Free Database Connection Pooling Software
 
okay, i look into it. i only know that the source images are not progressive. (images are in the root, resized and heavily compressed images in the previews folder - yes, the ones that are of biger file size. the 3 java file that make the applet are also there - plus the entire jbuilder project imageeditor.zip). just in case you're intrested.

ftp://
however, you have seen the resize function and here's the compress function. Not much i could do wrong, could i? ehh.. i think i'll try to operate with BufferedImage instead after my classes today. maybe that helps. thanks

-----------------------------------------------------

public PlanarImage compress(PlanarImage planImg, float quality){
BufferedImage buffImg = planImg.getAsBufferedImage();
BufferedImage retImg = null;
ByteArrayOutputStream out = new ByteArrayOutputStream( 0xfff );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param;
param = encoder.getDefaultJPEGEncodeParam(buffImg);
param.setQuality(quality, true);
try {
encoder.encode(buffImg, param);
// write the image into a byte[] array
byte[] compressedData = out.toByteArray();
// now read that data back into a BufferedImage
ByteArrayInputStream bais = new ByteArrayInputStream(compressedData);
retImg = javax.imageio.ImageIO.read(bais);
}
catch (IOException e) {
System.out.println("IOException at encoding..");
}
PlanarImage pi = PlanarImage.wrapRenderedImage(retImg) ;
return pi;
}
 
Remember that JAI works on a delayed rendering chain - its best to work with a RenderedOp class, because this delays actual rendering until you really need it (ie when you finally encode the in-memory bitmap into an actual format). Using a BufferedImage forces rendering unecessarily.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top