如何在不预乘alpha的情况下获取真实的RGBA或ARGB颜色值?

如何解决如何在不预乘alpha的情况下获取真实的RGBA或ARGB颜色值?

| 我正在使用带有
kCGImageAlphaPremultipliedFirst
选项的
CGBitmapContextCreate
创建位图上下文。 我制作了一个5 x 5的测试图像,其中包含一些主要颜色(纯红色,绿色,蓝色,白色,黑色),一些混合颜色(即紫色)和一些alpha变化。每当alpha分量不为255时,颜色值都是错误的。 我发现当执行以下操作时,我可以重新计算颜色:
almostCorrectRed = wrongRed * (255 / alphaValue);
almostCorrectGreen = wrongGreen * (255 / alphaValue);
almostCorrectBlue = wrongBlue * (255 / alphaValue);
但是问题是,我的计算有时会偏离3甚至更多。例如,对于绿色,我得到的值为242而不是245,我100%确信它必须恰好是245。Alpha是128。 然后,对于PNG位图中具有相同alpha透明度的完全相同的颜色,我得到的alpha = 255和绿色= 245。 如果alpha为0,则红色,绿色和蓝色也为0。这里所有数据都丢失了,我无法弄清像素的颜色。 如何完全避免或完全取消此Alpha预乘,以便可以像在Photoshop中创建图像时一样,根据真实的R G B像素值修改图像中的像素?如何恢复R,G,B和A的原始值? 背景信息(此问题可能不是必需的): 我正在做的是:我将UIImage绘制到位图上下文上,以便对其执行一些简单的图像处理算法,并根据之前的颜色移动每个像素的颜色。没什么特别的。但是我的代码需要真实的颜色。当一个像素是透明的(意味着它的alpha小于255)时,我的算法不会在乎这一点,它只需要根据需要修改R,G,B,而Alpha则保持不变。有时,尽管它也会使alpha向上或向下移动。但是我将它们视为两个独立的事物。 Alpha扭曲了透明度,而R G B控制了颜色。     

解决方法

        这是整数类型中预乘的基本问题: 245 *(128/255)= 122.98 122.98被截断为整数= 122 122 *(255/128)= 243.046875 我不知道为什么得到的是242,而不是243,但是这个问题仍然存在,而且随着alpha的降低,情况变得更糟。 解决方案是改用浮点组件。 《 Quartz 2D编程指南》提供了您将需要使用的格式的全部详细信息。 要点:您需要在创建原始图像时使用浮点数(而且我认为将这样的图像另存为PNG甚至是不可能的;您可能必须使用TIFF)。已经以整数类型预乘的图像已经失去了精度。没有找回来。 零字母大小写是这种情况的极端版本​​,以至于即使浮点数也无法帮助您。零(alpha)等于零的任何时间,都无法从该点恢复原始的未乘数值。     ,        将alpha与整数颜色类型预乘是一种信息有损操作。数据在量化过程中被破坏(四舍五入到8位)。 由于某些数据被破坏(通过舍入),因此无法恢复精确的原始像素颜色(某些幸运值除外)。您必须先保存Photoshop图像的颜色,然后再将其绘制到位图上下文中,并使用该原始颜色数据,而不是位图中的相乘颜色数据。     ,尝试读取图像数据,使用CoreGraphics将其渲染到另一幅图像,然后将结果另存为非预乘数据时,我遇到了同样的问题。我发现对我有用的解决方案是保存一个表,该表包含CoreGraphics用于将未预乘的数据映射到预乘的数据的精确映射。然后,通过调用mult和floor()来估计原始的预乘值是多少。然后,如果估算值与表查找结果不匹配,则只需检查表中估算值以下和估算值以上的值是否完全匹配即可。
// Execute premultiply logic on RGBA components split into componenets.
// For example,a pixel RGB (128,0) with A = 128
// would return (255,0) with A = 128

static
inline
uint32_t premultiply_bgra_inline(uint32_t red,uint32_t green,uint32_t blue,uint32_t alpha)
{
  const uint8_t* const restrict alphaTable = &extern_alphaTablesPtr[alpha * PREMULT_TABLEMAX];
  uint32_t result = (alpha << 24) | (alphaTable[red] << 16) | (alphaTable[green] << 8) | alphaTable[blue];
  return result;
}

static inline
int unpremultiply(const uint32_t premultRGBComponent,const float alphaMult,const uint32_t alpha)
{
  float multVal = premultRGBComponent * alphaMult;
  float floorVal = floor(multVal);
  uint32_t unpremultRGBComponent = (uint32_t)floorVal;
  assert(unpremultRGBComponent >= 0);
  if (unpremultRGBComponent > 255) {
    unpremultRGBComponent = 255;
  }

  // Pass the unpremultiplied estimated value through the
  // premultiply table again to verify that the result
  // maps back to the same rgb component value that was
  // passed in. It is possible that the result of the
  // multiplication is smaller or larger than the
  // original value,so this will either add or remove
  // one int value to the result rgb component to account
  // for the error possibility.

  uint32_t premultPixel = premultiply_bgra_inline(unpremultRGBComponent,alpha);

  uint32_t premultActualRGBComponent = (premultPixel >> 16) & 0xFF;

  if (premultRGBComponent != premultActualRGBComponent) {
    if ((premultActualRGBComponent < premultRGBComponent) && (unpremultRGBComponent < 255)) {
      unpremultRGBComponent += 1;
    } else if ((premultActualRGBComponent > premultRGBComponent) && (unpremultRGBComponent > 0)) {
      unpremultRGBComponent -= 1;
    } else {
      // This should never happen
      assert(0);
    }
  }

  return unpremultRGBComponent;
}
您可以在此github链接中找到完整的静态值表。 注意,当原始未预乘像素被预乘时,该方法将不会恢复信息“丢失”。但是,它的确会返回最小的未预乘像素,一旦再次通过预乘逻辑,它将变为预乘像素。当图形子系统仅接受预乘像素(例如OSX上的CoreGraphics)时,此功能很有用。如果图形子系统仅接受预乘像素,那么最好不要存储预乘像素,因为与未预乘像素相比,它消耗的空间更少。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-