patternjavaMinor
Colored image will become black and white
Viewed 0 times
imagewhitecoloredwillandbecomeblack
Problem
For a project of mine, which is targeting java 6, I had been in the need to have different algorithms/methods to change a
To test these implementations I therefore created an interface
and the, for me, most successful implementation is up for review
```
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
public class BlackAndWhiteRedThresholdConverter implements BlackAndWhiteConverter {
@Override
public BufferedImage toBlackAndWhite(final BufferedImage image) {
final int imageType = image.getColorModel().getColorSpace().getType();
if (imageType != ColorSpace.TYPE_RGB) {
throw new IllegalArgumentException();
}
final int width = image.getWidth();
final int height = image.getHeight();
final int upperLimit = width * height;
final DataBuffer buffer = image.getRaster().getDataBuffer();
final int[] source = ((DataBufferInt) buffer).getData();
byte[] destination = new byte[upperLimit];
for (int i = 0; i > 16) & 0x000000FF;
if (red > threshold) {
destination[i] = -1;
}
}
WritableRaster raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, 2, null);
raster.setDataElements(0, 0, width, height, destination);
BufferedImage destinationImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
destinationImage.setData(raster);
return destinationImage;
}
private int threshold = 110;
@Override
public void setThreshold(int threshold) {
this.th
BufferdImage with a ColorSpace.TYPE_RGB to a plain ole black and white one. To test these implementations I therefore created an interface
public interface BlackAndWhiteConverter {
BufferedImage toBlackAndWhite(BufferedImage image);
void setThreshold(int threshold);
}and the, for me, most successful implementation is up for review
```
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
public class BlackAndWhiteRedThresholdConverter implements BlackAndWhiteConverter {
@Override
public BufferedImage toBlackAndWhite(final BufferedImage image) {
final int imageType = image.getColorModel().getColorSpace().getType();
if (imageType != ColorSpace.TYPE_RGB) {
throw new IllegalArgumentException();
}
final int width = image.getWidth();
final int height = image.getHeight();
final int upperLimit = width * height;
final DataBuffer buffer = image.getRaster().getDataBuffer();
final int[] source = ((DataBufferInt) buffer).getData();
byte[] destination = new byte[upperLimit];
for (int i = 0; i > 16) & 0x000000FF;
if (red > threshold) {
destination[i] = -1;
}
}
WritableRaster raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, width, height, 1, 2, null);
raster.setDataElements(0, 0, width, height, destination);
BufferedImage destinationImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
destinationImage.setData(raster);
return destinationImage;
}
private int threshold = 110;
@Override
public void setThreshold(int threshold) {
this.th
Solution
Input validation
If the received image is not RGB, throwing an
The long chain
I would be good to move the validation to a dedicated helper method, let's say
Java conventions
Member variables should be declared at the top, followed by constructors, followed by methods. Very roughly speaking. So move the declaration of
Naming
As the interface name already implies the conversion to black and white,
the method name
Misc
If the received image is not RGB, throwing an
IllegalArgumentException without a custom message is not very friendly. Users would have to look at the code to understand what went wrong exactly.The long chain
image.getColorModel().getColorSpace().getType() is extracted to an imageType local variable that's only used for validation.I would be good to move the validation to a dedicated helper method, let's say
validateImageType.Java conventions
Member variables should be declared at the top, followed by constructors, followed by methods. Very roughly speaking. So move the declaration of
threshold higher.Naming
As the interface name already implies the conversion to black and white,
the method name
toBlackAndWhite is redundant. I'd rename it to simply convert.Misc
0x000000FF is the same as 0xFF, so you can shorten it.Context
StackExchange Code Review Q#152954, answer score: 6
Revisions (0)
No revisions yet.