更改像素后如何还原像素颜色?

时间:2019-04-05 03:41:44

标签: java image colors

我需要为图片添加色彩,然后将图片恢复为原始颜色。我可以添加色彩,但不知道如何将图片恢复为原始颜色。

我想创建一种方法,将图片重置为原始颜色。

尝试

public void changeBlues(double param){
    Pixel[] pixelArray =  this.getPixels();
    Pixel pixel = null;
    int value = 0;
    int index = 0;

    if(param <= 0.0){
      System.out.println("Error! Parameter less than or equal to 0.0");
      return;
    }
    else if(param > 5.0){
      System.out.println("Error! Paramater is greater than five.");
      return;
    }



    //loop through all the pixels

    //get the current pixel
    while(index < (int)(pixelArray.length)){  
      if(param < 1.0){
        pixel = pixelArray[index];

        //get the value
        value = pixel.getBlue();

        // decrease the value by param
        value = (int)((value - (value * param)));

        // set he blue value of the current pixel to the new value
        pixel.setBlue(value);

        // increment the index
        index = index + 1;
      }

      pixel = pixelArray[index];

      //get the value
      value = pixel.getBlue();

      // decrease the value by param
      value = (int)((value + (value * param)));

      // set he blue value of the current pixel to the new value
      pixel.setBlue(value);

      // increment the index
      index = index + 1;


  }
}

1 个答案:

答案 0 :(得分:0)

如MadProgrammer所建议,您需要维护原始副本,或为每个更改创建一个(双向)增量。后者的优点是,您可以还原到编辑的所有阶段,就像历史记录一样(在Windows下的许多程序中为CTRL + Z / Y)。

为此,您需要创建一个对象,该对象存储像素更改信息,并且可以应用或还原。由于循环遍历所有像素,几乎几乎所有像素都会受到影响,因此您可以将整个图像存储在历史记录中。这取决于您的程序中是否有操作会在更有限的基础上更改图像,而不是进行全图修改。

@编辑: 由于您想在多个不同的有色图片之间旋转,因此,只需生成三个缓冲图片并对其进行旋转即可。

相关问题