Results 1 to 1 of 1
  1. #1
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default RescaleOp problem

    I am trying to use a RescaleOp to change the coloring of a BufferedImage, but I'm getting a really weird exception. My code (with irrelevant parts omitted) is:

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    
    
    
    public class Bitmap{
    	private BufferedImage image;
    
    	public Bitmap(File file) throws IOException
    		{this(ImageIO.read(file));}
    
    	//this constructor enforces that image is always of type INT_ARGB
    	public Bitmap(BufferedImage image){
    		int w=image.getWidth(),h=image.getHeight();
    		this.image=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
    		this.image.getGraphics().drawImage(image,0,0,w,h,0,0,w,h,null);
    	}
    
    	public Bitmap recolor(double minr,double maxr,double ming,double maxg,double minb,double maxb){
    		float[] scale={(float)(maxr-minr),(float)(maxg-ming),(float)(maxb-minb)};
    		float[] offset={255*(float)minr,255*(float)ming,255*(float)minb};
    		RescaleOp op=new RescaleOp(scale,offset,null);
    		return new Bitmap(op.filter(image,null)); //the exception is thrown on this line
    	}
    }
    And I'm getting the following exception:

    Exception in thread "main" java.lang.IllegalArgumentException: Number of bands in src 4 does not equal number of bands in dest 2

    I also tried creating a 2nd BufferedImage to pass as the 2nd argument to op.filter() rather than passing null, but that had the exact same problem.


    edit: adding a 4th element to the end of each of the float arrays (for alpha) fixes the problem. This still doesn't make sense, though, since, according to the api docs for RescaleOp:

    "For BufferedImages, rescaling operates on color and alpha components. The number of sets of scaling constants may be one, in which case the same constants are applied to all color (but not alpha) components. Otherwise, the number of sets of scaling constants may equal the number of Source color components, in which case no rescaling of the alpha component (if present) is performed. If neither of these cases apply, the number of sets of scaling constants must equal the number of Source color components plus alpha components, in which case all color and alpha components are rescaled."

    So, in theory, I should be able to use scale/offset arrays of length 1 (same for r, g, and b), 3 (different for r, g, and b), or 4 (different for a, r, g, and b), but the case with length 3 doesn't work (I haven't tried it with length 1).
    Last edited by DiamondSoul; 10-09-2011 at 04:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •