org.apache.batik.ext.awt.image.rendered.ColorMatrixRed Maven / Gradle / Ivy
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.batik.ext.awt.image.rendered;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import java.awt.image.WritableRaster;
import org.apache.batik.ext.awt.image.GraphicsUtil;
/**
*
* @author Vincent Hardy
* @version $Id: ColorMatrixRed.java 1733416 2016-03-03 07:07:13Z gadams $
*/
public class ColorMatrixRed extends AbstractRed{
/**
* Matrix to apply to color components
*/
private float[][] matrix;
public float[][] getMatrix(){
return copyMatrix(matrix);
}
public void setMatrix(float[][] matrix){
float[][] tmp = copyMatrix(matrix);
if(tmp == null){
throw new IllegalArgumentException();
}
if(tmp.length != 4){
throw new IllegalArgumentException();
}
for(int i=0; i<4; i++){
if(tmp[i].length != 5){
throw new IllegalArgumentException( String.valueOf( i ) + " : " + tmp[i].length);
}
}
this.matrix = matrix;
}
private float[][] copyMatrix(float[][] m){
if(m == null){
return null;
}
float[][] cm = new float[m.length][];
for(int i=0; i>> 24;
int r = (pel >> 16) & 0xff;
int g = (pel >> 8 ) & 0xff;
int b = pel & 0xff;
int dr = (int)((a00*r + a01*g + a02*b + a03*a + a04)*255.0f);
int dg = (int)((a10*r + a11*g + a12*b + a13*a + a14)*255.0f);
int db = (int)((a20*r + a21*g + a22*b + a23*a + a24)*255.0f);
int da = (int)((a30*r + a31*g + a32*b + a33*a + a34)*255.0f);
/*dr = dr > 255 ? 255 : dr < 0 ? 0 : dr;
dg = dg > 255 ? 255 : dg < 0 ? 0 : dg;
db = db > 255 ? 255 : db < 0 ? 0 : db;
da = da > 255 ? 255 : da < 0 ? 0 : da;*/
// If any high bits are set we are not in range.
// If the highest bit is set then we are negative so
// clamp to zero else we are > 255 so clamp to 255.
if ((dr & 0xFFFFFF00) != 0)
dr = ((dr & 0x80000000) != 0)?0:255;
if ((dg & 0xFFFFFF00) != 0)
dg = ((dg & 0x80000000) != 0)?0:255;
if ((db & 0xFFFFFF00) != 0)
db = ((db & 0x80000000) != 0)?0:255;
if ((da & 0xFFFFFF00) != 0)
da = ((da & 0x80000000) != 0)?0:255;
pixels[p++] = (da << 24
|
dr << 16
|
dg << 8
|
db);
}
p += adjust;
}
//System.out.println("Result is : " + wr.getWidth() + "/" + wr.getHeight()+ "/" + wr.getMinX() + "/" + wr.getMinY());
return wr;
}
}