首页 > 解决方案 > RGBimage 数组和创建 ppm 文件

问题描述

这是大学任务的一部分

我得到了一个名为 RGBColor 的类,没有私有访问权限(但有关于每个方法/构造函数的信息)对象是(r,g,b):r 代表红色,g 代表绿色,b 代表蓝色,它们中的每一个代表0-255 之间的数字

我需要基于RBGColor编写一个名为RBGImage的新类,该类对象是一个存储RGBColor对象的二维数组。

我的斗争是创建一个 ppm 文件以检查程序,但我不知道该怎么做。这是我到目前为止所做的:

public class RGBImage
{
    RGBColor[][] _image;
    public RGBImage(int rows , int cols){
        
        _image=new RGBColor[rows][cols];
        for(int i=0;i<rows;i++)
            for(int j=0;j<cols;j++)
                _image[i][j]=new RGBColor(_image[i][j]);
        
}

public RGBImage(RGBColor[][] pixels) {
      
int hight=pixels.length;
int width=pixels[0].length;
_image=new RGBColor[hight][width];
        for(int i=0;i<hight;i++)
            for(int j=0;j<width;j++)
                _image[i][j] = new RGBColor(pixels[i][j]);
                ;   

} 


public RGBImage(RGBImage other)
{
_image = other._image;    
}

public int getHeight()
{
    int hight = _image.length;
    return  hight;
}

public int getWidth() {
int width = _image[0].length;

return width;

}

public RGBColor getPixel(int row,int col)
{
int _red = _image[row][col].getRed();
int _green = _image[row][col].getGreen();
int _blue = _image[row][col].getBlue();
return new RGBColor(_red,_green,_blue);    
}

public void setPixel(int row,int col,RGBColor pixel)
{
int _red = pixel.getRed();
int _green = pixel.getGreen();
int _blue = pixel.getBlue(); 
// set if to do nothing
if(row <= _image.length && col <= _image[0].length) 

    
_image[row][col].setRed(_red);
_image[row][col].setGreen(_green);
_image[row][col].setBlue(_blue);    
}

public double[][] toGrayscaleArray() {
     int width = _image[0].length;
     int hight = _image.length;
    
     double[][] grayScale = new double[hight][width];
        
        for(int i=0;i<hight;i++)
            for(int j=0;j<width;j++)
                grayScale[i][j]=_image[i][j].convertToGrayscale();
        
        return grayScale;
}


public boolean equals (RGBImage other)
{
if(_image == other._image)
return true;
return false;    
}

抱歉,我无法提供任何文件

标签: javaarraysrgb

解决方案


推荐阅读