我如何仅将rgb的红色数字(0x00ff0000)提高10%这样的值?

dluptydi  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(340)
  1. class TestFilter extends RGBImageFilter {
  2. public int filterRGB(int x, int y, int pixel) { //0xffffffff
  3. float redfilteredPixel;
  4. int restPixel;
  5. redfilteredPixel = (pixel & 0x00ff0000); //red pixel
  6. restPixel = (pixel & 0xff00ffff); //"restpixel"
  7. redfilteredPixel *= 1.1f;
  8. int redpixel = (int) redfilteredPixel & 0x00ff0000;
  9. return (redpixel | restPixel);
  10. }
  11. }

这是一个学校项目,但我应该只改变方法的中间部分。

jgwigjjp

jgwigjjp1#

你写这篇文章的方式已经很有效了。问题是当结果太大,无法放入红色字段的8位时会发生什么。
您可以在转换回int后添加一个检查,如下所示:

  1. int redpixel = (int) redfilteredPixel & 0x00ff0000;
  2. if (redpixel > 0x00FF0000)
  3. redpixel = 0x00FF0000;

那会管用的,但有点不寻常。通常情况下,执行此操作的代码不会转换为float。
此外,如果在[0255]中将红色值一直转换为int,则更容易理解,但在这种情况下,没有必要这样做(+10%的工作方式与此相同),通常在编写类似的低级像素代码时,最好以快速方式进行。

bybem2ql

bybem2ql2#

在我脑子里,应该是这样的:

  1. //shift the bytes to only get the red value as a numerical value
  2. int redValue = redfilteredPixel >> 16;
  3. redValue *= 1.1f;
  4. //make sure it doesnt exceed 255 after the multiplication
  5. redValue = Math.min(255, redValue);
  6. //shift the bytes back to the correct place
  7. redfilteredPixel = redValue << 16;

相关问题