Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fun with OpenCL

  • Loading branch information...
commit 67e0a5f24f30a39a71fd701c44e0b59b5e4bf292 1 parent 865130a
@ccgus authored
View
5 plugins/imagetools/JSTOpenCLProgram.h
@@ -90,6 +90,9 @@ typedef struct _JSTOCLFloatPixel {
- (id)initWithContext:(JSTOpenCLContext*)theContext width:(size_t)w height:(size_t)h;
+- (id)initWithContext:(JSTOpenCLContext*)theContext usingImageAtPath:(NSString*)path;
+
+
@end
@@ -111,6 +114,8 @@ typedef struct _JSTOCLFloatPixel {
- (void)setArgument: (cl_uint) argIndex size: (size_t) argumentSize value: (void*) value;
- (void)setArgument: (cl_uint) argIndex buffer: (JSTOpenCLBuffer*) buffer;
+- (void)setArgument: (cl_uint) argIndex int:(size_t)v;
+
- (void)enqueueCallWithDimensions: (cl_uint) workDimension globalSizes: (const size_t*) globalWorkSize localSizes: (const size_t*) localWorkSize;
View
74 plugins/imagetools/JSTOpenCLProgram.m
@@ -16,7 +16,7 @@ - (id)printController;
@interface JSTOpenCLKernel ()
@property (readwrite) cl_kernel computeKernel;
-@property (readwrite) int workGroupSize;
+@property (readwrite) size_t workGroupSize;
@end
@implementation JSTOpenCLProgram
@@ -225,16 +225,11 @@ - (id)initWithContext:(JSTOpenCLContext*)theContext width:(size_t)w height:(size
int err = 0;
- if (NO) {
- computeBuffer = clCreateBuffer(context.computeContext, attributes, size, _bitmapData, &err);
- }
- else {
- cl_image_format format;
- format.image_channel_order = CL_RGBA;
- format.image_channel_data_type = CL_FLOAT;
-
- computeBuffer = clCreateImage2D(context.computeContext, attributes, &format, _width, _height, _bytesPerRow, _bitmapData, &err);
- }
+ cl_image_format format;
+ format.image_channel_order = CL_RGBA;
+ format.image_channel_data_type = CL_FLOAT;
+
+ computeBuffer = clCreateImage2D(context.computeContext, attributes, &format, _width, _height, _bytesPerRow, _bitmapData, &err);
if (!computeBuffer) {
NSLog(@"%s:%d", __FUNCTION__, __LINE__);
@@ -246,6 +241,63 @@ - (id)initWithContext:(JSTOpenCLContext*)theContext width:(size_t)w height:(size
return self;
}
++ (id)instanceWithContext:(JSTOpenCLContext*)theContext usingImageAtPath:(NSString*)path {
+ return [[[self alloc] initWithContext:theContext usingImageAtPath:path] autorelease];
+}
+
+- (id)initWithContext:(JSTOpenCLContext*)theContext usingImageAtPath:(NSString*)path {
+
+ CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], nil);
+
+ if (!imageSourceRef) {
+ NSLog(@"Could not turn the file into an image");
+ return nil;
+ }
+
+
+ CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSourceRef, 0, (CFDictionaryRef)[NSDictionary dictionary]);
+
+ _width = CGImageGetWidth(imageRef);
+ _height = CGImageGetHeight(imageRef);
+
+ debug(@"_width: %ld", _width);
+ debug(@"_height: %ld", _height);
+
+
+ self = [self initWithContext:theContext width:_width height:_height];
+ if (!self) {
+ CFRelease(imageRef);
+ CFRelease(imageSourceRef);
+ return nil;
+ }
+
+
+ CFRelease(imageSourceRef);
+
+ CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
+
+ CGContextRef bcontext = CGBitmapContextCreate(_bitmapData, _width, _height, 32, _bytesPerRow, cs, kCGBitmapFloatComponents | kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Host);
+
+ if (!bcontext) {
+ NSLog(@"Could not create a context for drawing");
+ [self release];
+ CFRelease(imageRef);
+ return nil;
+ }
+
+ CGContextSetBlendMode(bcontext, kCGBlendModeCopy);
+ CGContextDrawImage(bcontext, CGRectMake(0, 0, _width, _height), imageRef);
+
+ CGContextRelease(bcontext);
+ CFRelease(imageRef);
+
+ debug(@"hurray!");
+
+ debug(@"[self width]: %ld", [self width]);
+
+ return self;
+}
+
- (void)dealloc {
View
35 plugins/imagetools/samples/openclDesaturate.jstalk
@@ -0,0 +1,35 @@
+var src = """
+
+__kernel void clDesaturate(image2d_t src, image2d_t dst, int width, int height) {
+
+ int2 coord = (int2)(get_global_id(0), get_global_id(1));
+
+ if (coord.x >= width || coord.y >= height) {
+ return;
+ }
+
+ float4 color = read_imagef(src, CLK_FILTER_NEAREST, coord);
+ float lum = (0.30f * color.x + 0.59f * color.y + 0.11f * color.z);
+ float4 val = (float4)(lum, lum, lum, color.w);
+
+ write_imagef(dst, coord, val);
+}
+""";
+
+var context = [JSTOpenCLContext new];
+var program = [context programWithSource:src];
+[program build];
+
+var kernel = [program createKernel:"clDesaturate"];
+var inBuffer = [JSTOpenCLImageBuffer instanceWithContext:context usingImageAtPath:@"/Volumes/srv/Users/gus/Desktop/uhcorn.png"];
+var outBuffer = [JSTOpenCLImageBuffer instanceWithContext:context width:[inBuffer width] height:[inBuffer height]];
+
+[kernel setArgument:0 buffer:inBuffer];
+[kernel setArgument:1 buffer:outBuffer];
+[kernel setIntArgumentAtIndex:2 value:[inBuffer width]];
+[kernel setIntArgumentAtIndex:3 value:[inBuffer height]];
+
+[kernel enqueueCallWithGlobalSizeX:[inBuffer width] Y:[inBuffer height]];
+[context finish];
+
+[JSTImageTools viewImageBuffer:outBuffer inWindowNamed:"OpenCL"];
Please sign in to comment.
Something went wrong with that request. Please try again.