C
Cheok Yan Cheng
one of the rule of thumbs in designing a method is that to make the
return
type of the method as specific as possible (means that we always tend
to
return the lowest level of the inheritance tree)
take the example of one of my method.
the below method will take PlanarImage as my parameter (that is my
wish, i
dun want to make the parameter as RenderedOp type) - RenderedOp is the
subclass of PlanarImage.
private PlanarImage color2Gray(PlanarImage pi) throws Exception
{
assert (pi!=null) : "pi ="+pi;
int numBands = pi.getSampleModel().getNumBands();
boolean hasAlpha = pi.getColorModel().hasAlpha();
double[][] matrix = null;
if(numBands == 1)
{
return pi;
}
else if(numBands == 3)
{
matrix = new double[1][4];
matrix[0][0] = .144D;
matrix[0][1] = .587D;
matrix[0][2] = .299D;
matrix[0][3] = 0;
}
else if(numBands == 4 && hasAlpha)
{
matrix = new double[1][5];
matrix[0][0] = .144D;
matrix[0][1] = .587D;
matrix[0][2] = .299D;
matrix[0][3] = 0;
matrix[0][4] = 0;
}
else
{
throw new Exception("Invalid number of color
bands
= " + numBands + " with alpha component = " + hasAlpha);
}
ParameterBlock pb = new ParameterBlock();
pb.addSource(pi);
pb.add(matrix);
return JAI.create("bandcombine", pb, null);
}
for the last line of statement, should i use
return JAI.create("bandcombine", pb, null); // this one return
RenderedOp
or
return JAI.create("bandcombine", pb, null).getRendering(); // this one
return PlanarImage
for good design purpose?
thank you.
regards
yancheng
return
type of the method as specific as possible (means that we always tend
to
return the lowest level of the inheritance tree)
take the example of one of my method.
the below method will take PlanarImage as my parameter (that is my
wish, i
dun want to make the parameter as RenderedOp type) - RenderedOp is the
subclass of PlanarImage.
private PlanarImage color2Gray(PlanarImage pi) throws Exception
{
assert (pi!=null) : "pi ="+pi;
int numBands = pi.getSampleModel().getNumBands();
boolean hasAlpha = pi.getColorModel().hasAlpha();
double[][] matrix = null;
if(numBands == 1)
{
return pi;
}
else if(numBands == 3)
{
matrix = new double[1][4];
matrix[0][0] = .144D;
matrix[0][1] = .587D;
matrix[0][2] = .299D;
matrix[0][3] = 0;
}
else if(numBands == 4 && hasAlpha)
{
matrix = new double[1][5];
matrix[0][0] = .144D;
matrix[0][1] = .587D;
matrix[0][2] = .299D;
matrix[0][3] = 0;
matrix[0][4] = 0;
}
else
{
throw new Exception("Invalid number of color
bands
= " + numBands + " with alpha component = " + hasAlpha);
}
ParameterBlock pb = new ParameterBlock();
pb.addSource(pi);
pb.add(matrix);
return JAI.create("bandcombine", pb, null);
}
for the last line of statement, should i use
return JAI.create("bandcombine", pb, null); // this one return
RenderedOp
or
return JAI.create("bandcombine", pb, null).getRendering(); // this one
return PlanarImage
for good design purpose?
thank you.
regards
yancheng