jjzjj

android - RenderScript 错误地操作内核的输出

coder 2023-12-05 原文

我正在尝试使用 Android 的 RenderScript 在图像后面渲染一个半透明的圆圈,但是当从 RenderScript 内核返回一个值时,事情变得非常错误。

这是我的内核:

#pragma version(1)
#pragma rs java_package_name(be.abyx.aurora)
// We don't need very high precision floating points
#pragma rs_fp_relaxed

// Center position of the circle
int centerX = 0;
int centerY = 0;

// Radius of the circle
int radius = 0;

// Destination colour of the background can be set here.
float destinationR;
float destinationG;
float destinationB;
float destinationA;

static int square(int input) {
    return input * input;
}

uchar4 RS_KERNEL circleRender(uchar4 in, uint32_t x, uint32_t y) {
    //Convert input uchar4 to float4
    float4 f4 = rsUnpackColor8888(in);

    // Check if the current coordinates fall inside the circle
    if (square(x - centerX) + square(y - centerY) < square(radius)) {
        // Check if current position is transparent, we then need to add the background!)
        if (f4.a == 0) {
            uchar4 temp = rsPackColorTo8888(0.686f, 0.686f, 0.686f, 0.561f);
            return temp;
        }
    }

    return rsPackColorTo8888(f4);
}

现在,rsPackColorTo8888() 函数采用 4 个 float ,其值介于 0.0 和 1.0 之间。然后通过计算每个浮点值的 255 次来找到生成的 ARGB 颜色。因此给定的 float 对应于颜色 R = 0.686 * 255 = 175,G = 0.686 * 255 = 175,B = 0.686 * 255 = 175 和 A = 0.561 * 255 = 143。

rsPackColorTo8888() 函数本身可以正常工作,但是当找到的 uchar4 值从内核返回时,会发生一些非常奇怪的事情。 R、G 和 B 值分别变为红色 * Alpha = 56、绿色 * Alpha = 56 和蓝色 * Alpha = 56,其中 Alpha 为 0.561。这意味着 R、G 和 B 的值永远不会大于 A = 0.561 * 255。

手动设置输出,而不是使用 rsPackColorTo8888() 会产生完全相同的行为。我的意思是以下代码产生完全相同的结果,这反过来证明 rsPackColorTo8888() 不是问题所在:

if (square(x - centerX) + square(y - centerY) < square(radius)) {
    // Check if current position is transparent, we then need to add the background!)
    if (f4.a == 0) {
        uchar4 temp;
        temp[0] = 175;
        temp[1] = 175;
        temp[2] = 175;
        temp[3] = 143;
        return temp;
    }
}

这是调用脚本的 Java 代码:

@Override
public Bitmap renderParallel(Bitmap input, int backgroundColour, int padding) {
    ResizeUtility resizeUtility = new ResizeUtility();

    // We want to end up with a square Bitmap with some padding applied to it, so we use the
    // the length of the largest dimension (width or height) as the width of our square.
    int dimension = resizeUtility.getLargestDimension(input.getWidth(), input.getHeight()) + 2 * padding;

    Bitmap output = resizeUtility.createSquareBitmapWithPadding(input, padding);
    output.setHasAlpha(true);

    RenderScript rs = RenderScript.create(this.context);

    Allocation inputAlloc = Allocation.createFromBitmap(rs, output);
    Type t = inputAlloc.getType();

    Allocation outputAlloc = Allocation.createTyped(rs, t);

    ScriptC_circle_render circleRenderer = new ScriptC_circle_render(rs);

    circleRenderer.set_centerX(dimension / 2);
    circleRenderer.set_centerY(dimension / 2);
    circleRenderer.set_radius(dimension / 2);
    circleRenderer.set_destinationA(((float) Color.alpha(backgroundColour)) / 255.0f);
    circleRenderer.set_destinationR(((float) Color.red(backgroundColour)) / 255.0f);
    circleRenderer.set_destinationG(((float) Color.green(backgroundColour)) / 255.0f);
    circleRenderer.set_destinationB(((float) Color.blue(backgroundColour)) / 255.0f);

    circleRenderer.forEach_circleRender(inputAlloc, outputAlloc);
    outputAlloc.copyTo(output);

    inputAlloc.destroy();
    outputAlloc.destroy();
    circleRenderer.destroy();
    rs.destroy();

    return output;
}

当 alpha 设置为 255(或 1.0 作为 float )时,返回的颜色值(在我的应用程序的 Java 代码中)是正确的。

我是不是做错了什么,或者这真的是 RenderScript 实现中某处的错误?

注意:我已经在 Oneplus 3T (Android 7.1.1)、Nexus 5 (Android 7.1.2)、Android 模拟器版本 7.1.2 和 6.0 上检查并验证了此行为

最佳答案

而不是传递具有以下类型的值:

uchar4 temp = rsPackColorTo8888(0.686f, 0.686f, 0.686f, 0.561f);

尝试创建一个 float4 并传递它。

float4 newFloat4 = { 0.686, 0.686, 0.686, 0.561 };
uchar4 temp = rsPackColorTo8888(newFloat4);

关于android - RenderScript 错误地操作内核的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45954160/

有关android - RenderScript 错误地操作内核的输出的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  3. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  4. ruby - 如何进行排列以有效地定制输出 - 2

    这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][

  5. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  9. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  10. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

随机推荐