gpt4 book ai didi

java - OpenCL 脚本给出意想不到的结果

转载 作者:行者123 更新时间:2023-12-01 15:02:27 26 4
gpt4 key购买 nike

我有这个简单的 OpenCL 代码块,并且得到了意想不到的结果。参数image是 float 数组,value是-255到+255之间的数字。使用 java 我使用 JSlider 来更改。默认值是 0,问题是当我将 slider 移动超过 0 时,图像是黑色的,如果我将 slider 移动到小于 0,图像是白色的,这是不应该发生的。这应该单独检查每个像素并调整该像素。似乎不是出于某种原因。

此代码块应更改图像的阈值。任何红绿蓝大于阈值的像素都应该是白色,否则应该是黑色。

kernel void threshold(global float* image, const float value, const int max){
int index = get_global_id(0);
if (index >= max){
return;
}

int color = image[index];
int red = color >> 16 & 0x0FF;
int green = color >> 8 & 0x0FF;
int blue = color & 0x0FF;

if(red > value && green > value && blue > value){
red = 255;
green = 255;
blue = 255;
}else{
red = 0;
green = 0;
blue = 0;
}

int rgba = 255;
rgba = (rgba << 8) + red;
rgba = (rgba << 8) + green;
rgba = (rgba << 8) + blue;

image[index] = rgba;
}

如果我将中间的 if/else 语句替换为:

red += value;
if(red > 255){red = 255;}
else if(red < 0){red = 0;}

green += value;
if(green > 255){green = 255;}
else if(green < 0){green = 0;}

blue += value;
if(blue > 255){blue = 255;}
else if(blue < 0){blue = 0;}

我得到了我正在寻找的该操作的结果,即调整图像的亮度。

我使用 OpenCL 是错误的吗?据我了解,将调用 kernel 直到返回。我在 java 中使用 JOCL 来执行此操作,这是我用来调用它的代码:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pocketshop.graphics;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opencl.CLBuffer;
import com.jogamp.opencl.CLCommandQueue;
import com.jogamp.opencl.CLContext;
import com.jogamp.opencl.CLKernel;
import com.jogamp.opencl.CLPlatform;
import com.jogamp.opencl.CLProgram;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;
import pocketshop.Canvas;
import pocketshop.dialogs.BrightnessContrastDialog;

/**
*
* @author Ryan
*/
public class CL {

protected static CLBuffer<FloatBuffer> buffer;
protected static float[] pixels;

public static CLBuffer<FloatBuffer> getBuffer() {
return buffer;
}

public static float[] getPixels() {
return pixels;
}

public static void start(String script, float val) {

CLPlatform platform = CLPlatform.getDefault(/*type(CPU)*/);
CLContext context = CLContext.create(platform.getMaxFlopsDevice());
try {
CLProgram program = context.createProgram(getStreamFor("../scripts/" + script + ".cl"));
program.build(CLProgram.CompilerOptions.FAST_RELAXED_MATH);
assert program.isExecutable();

BufferedImage image = Canvas.image;
assert image.getColorModel().getNumComponents() == 3;

pixels = image.getRaster().getPixels(0, 0, image.getWidth(), image.getHeight(), (float[]) null);
FloatBuffer fb = Buffers.newDirectFloatBuffer(pixels);

// allocate a OpenCL buffer using the direct fb as working copy
buffer = context.createBuffer(fb, CLBuffer.Mem.READ_WRITE);

// creade a command queue with benchmarking flag set
CLCommandQueue queue = context.getDevices()[0].createCommandQueue(CLCommandQueue.Mode.PROFILING_MODE);

int localWorkSize = queue.getDevice().getMaxWorkGroupSize(); // Local work size dimensions
int globalWorkSize = roundUp(localWorkSize, fb.capacity()); // rounded up to the nearest multiple of the localWorkSize

// create kernel and set function parameters
CLKernel kernel = program.createCLKernel(script.toLowerCase());
//adjustment(val, queue, kernel, buffer, localWorkSize, globalWorkSize);

kernel.putArg(buffer).putArg((float) val).putArg(buffer.getNIOSize()).rewind();
queue.putWriteBuffer(buffer, false);
queue.put1DRangeKernel(kernel, 0, globalWorkSize, localWorkSize);
queue.putReadBuffer(buffer, true);

} catch (IOException e) {
}
context.release();
}

private static InputStream getStreamFor(String filename) {
return BrightnessContrastDialog.class.getResourceAsStream(filename);
}

private static int roundUp(int groupSize, int globalSize) {
int r = globalSize % groupSize;
if (r == 0) {
return globalSize;
} else {
return globalSize + groupSize - r;
}
}
}

最佳答案

经过多次反复试验,我发现 int color = image[index]; 实际上是红绿或蓝颜色,而不是 3 的整数。

所以,

image[0] = Red;
image[1] = Green;
image[2] = Blue;
image[3] = Red;
image[4] = Green;
image[5] = Blue;

等等

关于java - OpenCL 脚本给出意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com