我有这个简单的OpenCL
代码块,我得到了意想不到的结果。参数image
是一个浮点数组,value
是一个从 -255 到 +255 的数字。使用 java 我使用 JSlider 来更改value
. 默认值为 0,问题是当我将滑块移动到 0 以上时,图像是黑色的,如果我将滑块移动到小于 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
错了吗?据我了解, iskernel
将被调用,直到返回被击中。我在 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;
}
}
}