实现类Img,参考形式如下: Class Img { Img(){} void readImg(string file);//从参数file指定的路径读取图像文件,有合理的函数体即可 void writeImg(string file);//将图像数据写出到参数file指定的路径,有合理的函数体即可 enum IMG_STORE_TYPE{GRAY,GRAY_A,RGB,RGB_A;};//图像的存储形式有四种: 灰度通道,灰度通道+透明度通道,RGB通道,RGB通道+透明度通道 IMG_STORE_TYPE store_type;//根据readImg的信息设定具体的store_type。 short[] pixel_data;存储的像素颜色数据 Img UnaryOperation(ShortUnaryOperator unary_op); Img BinaryOperation(Img img, ShortBinaryOperator binary_op); } 实现方法UnaryOperation和BinaryOperation,根据上述图像运算关系的定义,对pixel_data中的数据进行遍历运算,参数img是另一个参与运算的相同大小的图像,binary_op是具体运算的形式,对UnaryOperation实现反色操作(使用颜色的最大值255减去当前像素值得到新的像素值),对BinaryOperation实现加减两种运算形式(减法结果小于0的情况,截断到0)。
时间: 2024-02-11 09:09:03 浏览: 66
以下是Img类的实现代码:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.ShortBinaryOperator;
import java.util.function.ShortUnaryOperator;
import javax.imageio.ImageIO;
public class Img {
private static final int MAX_COLOR_VALUE = 255;
private static final int ALPHA_MASK = 0xff000000;
private static final int RED_MASK = 0xff0000;
private static final int GREEN_MASK = 0xff00;
private static final int BLUE_MASK = 0xff;
private int width;
private int height;
private IMG_STORE_TYPE storeType;
private short[] pixelData;
public Img() {}
public void readImg(String file) {
try {
BufferedImage image = ImageIO.read(new File(file));
width = image.getWidth();
height = image.getHeight();
storeType = getImgStoreType(image);
pixelData = new short[width * height * getNumChannels(storeType)];
switch (storeType) {
case GRAY:
readGrayImage(image);
break;
case GRAY_A:
readGrayAlphaImage(image);
break;
case RGB:
readRgbImage(image);
break;
case RGB_A:
readRgbAlphaImage(image);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeImg(String file) {
BufferedImage image = new BufferedImage(width, height, getImageType(storeType));
int[] pixels = new int[width * height];
for (int i = 0; i < pixels.length; i++) {
pixels[i] = getPixelValue(i);
}
image.setRGB(0, 0, width, height, pixels, 0, width);
try {
ImageIO.write(image, "png", new File(file));
} catch (IOException e) {
e.printStackTrace();
}
}
public Img UnaryOperation(ShortUnaryOperator unary_op) {
Img result = new Img();
result.width = width;
result.height = height;
result.storeType = storeType;
result.pixelData = new short[pixelData.length];
for (int i = 0; i < pixelData.length; i++) {
result.pixelData[i] = (short) unary_op.applyAsShort(pixelData[i]);
}
return result;
}
public Img BinaryOperation(Img img, ShortBinaryOperator binary_op) {
if (width != img.width || height != img.height || storeType != img.storeType) {
throw new IllegalArgumentException("The two images must have the same dimensions and store type.");
}
Img result = new Img();
result.width = width;
result.height = height;
result.storeType = storeType;
result.pixelData = new short[pixelData.length];
for (int i = 0; i < pixelData.length; i++) {
int value = binary_op.applyAsShort(pixelData[i], img.pixelData[i]);
result.pixelData[i] = (short) Math.max(0, Math.min(value, MAX_COLOR_VALUE));
}
return result;
}
private void readGrayImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int gray = (pixels[i] >> 16) & 0xff;
pixelData[i] = (short) gray;
}
}
private void readGrayAlphaImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int gray = (pixels[i] >> 16) & 0xff;
int alpha = (pixels[i] >> 24) & 0xff;
pixelData[2 * i] = (short) gray;
pixelData[2 * i + 1] = (short) alpha;
}
}
private void readRgbImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
pixelData[3 * i] = (short) red;
pixelData[3 * i + 1] = (short) green;
pixelData[3 * i + 2] = (short) blue;
}
}
private void readRgbAlphaImage(BufferedImage image) {
int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++) {
int red = (pixels[i] >> 16) & 0xff;
int green = (pixels[i] >> 8) & 0xff;
int blue = pixels[i] & 0xff;
int alpha = (pixels[i] >> 24) & 0xff;
pixelData[4 * i] = (short) red;
pixelData[4 * i + 1] = (short) green;
pixelData[4 * i + 2] = (short) blue;
pixelData[4 * i + 3] = (short) alpha;
}
}
private int getPixelValue(int index) {
switch (storeType) {
case GRAY:
return (pixelData[index] << 16) | (pixelData[index] << 8) | pixelData[index] | ALPHA_MASK;
case GRAY_A:
return (pixelData[2 * index] << 16) | (pixelData[2 * index] << 8) | pixelData[2 * index] |
(pixelData[2 * index + 1] << 24);
case RGB:
return (pixelData[3 * index] << 16) | (pixelData[3 * index + 1] << 8) | pixelData[3 * index] | ALPHA_MASK;
case RGB_A:
return (pixelData[4 * index] << 16) | (pixelData[4 * index + 1] << 8) | pixelData[4 * index] |
(pixelData[4 * index + 3] << 24);
default:
return 0;
}
}
private IMG_STORE_TYPE getImgStoreType(BufferedImage image) {
switch (image.getType()) {
case BufferedImage.TYPE_BYTE_GRAY:
return IMG_STORE_TYPE.GRAY;
case BufferedImage.TYPE_BYTE_GRAY_ALPHA:
return IMG_STORE_TYPE.GRAY_A;
case BufferedImage.TYPE_3BYTE_BGR:
return IMG_STORE_TYPE.RGB;
case BufferedImage.TYPE_4BYTE_ABGR:
return IMG_STORE_TYPE.RGB_A;
default:
throw new IllegalArgumentException("Unsupported image type.");
}
}
private int getImageType(IMG_STORE_TYPE storeType) {
switch (storeType) {
case GRAY:
return BufferedImage.TYPE_BYTE_GRAY;
case GRAY_A:
return BufferedImage.TYPE_BYTE_GRAY_ALPHA;
case RGB:
return BufferedImage.TYPE_3BYTE_BGR;
case RGB_A:
return BufferedImage.TYPE_4BYTE_ABGR;
default:
return BufferedImage.TYPE_INT_ARGB;
}
}
private int getNumChannels(IMG_STORE_TYPE storeType) {
switch (storeType) {
case GRAY:
case GRAY_A:
return 1;
case RGB:
return 3;
case RGB_A:
return 4;
default:
return 0;
}
}
public Img UnaryOperation() {
return UnaryOperation(pixel -> MAX_COLOR_VALUE - pixel);
}
public Img BinaryOperation(Img img, IMG_OPERATION_TYPE operationType) {
ShortBinaryOperator binary_op;
switch (operationType) {
case ADD:
binary_op = (p1, p2) -> p1 + p2;
break;
case SUBTRACT:
binary_op = (p1, p2) -> p1 - p2;
break;
default:
throw new IllegalArgumentException("Unsupported image operation type.");
}
return BinaryOperation(img, binary_op);
}
enum IMG_STORE_TYPE {
GRAY,
GRAY_A,
RGB,
RGB_A
}
enum IMG_OPERATION_TYPE {
ADD,
SUBTRACT
}
}
```
在Img类中实现了readImg和writeImg方法,用于读取和写出图像文件。其中,readImg方法根据图像的存储形式调用不同的读取方法,并将读取的像素颜色数据存储在pixelData数组中。writeImg方法则将pixelData数组中的像素颜色数据写出到图像文件中。
实现了UnaryOperation和BinaryOperation方法,对pixelData中的数据进行遍历运算。对于UnaryOperation,实现了反色操作;对于BinaryOperation,实现了加减两种运算形式,对于减法结果小于0的情况,截断到0。
此外,Img类还定义了IMG_STORE_TYPE枚举类型,表示图像的存储形式,以及IMG_OPERATION_TYPE枚举类型,表示图像的运算类型。在实现BinaryOperation方法时,根据IMG_OPERATION_TYPE选择具体的运算操作。
阅读全文