[Openmcl-devel] Any ideas why this isn't working?

Ron Garret ron at flownet.com
Mon Feb 10 19:57:03 PST 2020


Figured out the problem.  NSImages and CGImages are not toll-free bridgeable.  You have to convert the NSImage to a CGImage using:

(setf cgimg (#/CGImageForProposedRect:context:hints: img +null-ptr+ +null-ptr+ +null-ptr+))

and before that will work you have to force the NSImage to render itself into a bitmapImageRep.

It boggles my mind that anyone ever actually gets anything done in Objective C.

On Feb 10, 2020, at 5:19 PM, Ron Garret <ron at flownet.com> wrote:

> I was able to get this to work in ObjC (see code below).  I suspect the problem has to do with “toll-free bridging” of NS and CF classes which I had to do explicitly in ObjC using __bridge casts.  Is there an equivalent thing I have to do in CCL?  I thought those were just there to make the compiler happy and that these classes are actually the same under the hood.
> 
> rg
> 
> ======================================
> 
> #import <Foundation/Foundation.h>
> #import <Cocoa/Cocoa.h>
> 
> int main( int argc, const char* argv[]) {
> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
> 
> NSDictionary *props =
>  [NSDictionary
>    dictionaryWithObject:
>      [NSDictionary
>        dictionaryWithObject:[NSNumber numberWithFloat:0.2f]
>        forKey:(NSString *) kCGImagePropertyGIFDelayTime]
>    forKey: (NSString *) kCGImagePropertyGIFDictionary];
> 
> CFDictionaryRef props1 = (__bridge CFDictionaryRef)(props);
> 
> NSURL *fileURL =
>  [NSURL fileURLWithPath:@"/Users/ron/devel/qt-capture/foo.gif"];
> 
> CGImageDestinationRef dst =
>  CGImageDestinationCreateWithURL((__bridge CFURLRef)(fileURL), kUTTypeGIF,
>                                  2, nil);
> 
> NSImage *img = [[NSImage alloc] initWithSize: NSMakeSize(10.0, 10.0)];
> [img lockFocus];
> [[NSColor blackColor] set];
> NSRectFill(NSMakeRect(0, 0, 22, 12));
> [img unlockFocus];
> 
> CGImageRef imageRef =
>  [img CGImageForProposedRect:nil context:nil hints:nil];
> CGImageDestinationAddImage(dst, imageRef, props1);
> CGImageDestinationAddImage(dst, imageRef, props1);
> 
> bool fileSave = CGImageDestinationFinalize(dst);
> CFRelease(dst);
> return fileSave;
> 
> }
> 
> 
> 
> On Feb 10, 2020, at 6:46 AM, Ron Garret <ron at flownet.com> wrote:
> 
>> Trying to use a CGImageDestination to create an animated GIF following the recipe at:
>> 
>> https://stackoverflow.com/questions/15584931/creating-an-animated-gif-in-cocoa-defining-frame-type
>> 
>> but creating the CGImageDestination seems to fail:
>> 
>> Welcome to Clozure Common Lisp Version 1.11-r16812M  (DarwinX8664)!
>> 
>> ? (setf url (#_CFURLCreateWithString +null-ptr+ (nsstr "file:///Users/ron/Desktop/foo.gif") +null-ptr+))
>> #<NS-URL file:///Users/ron/Desktop/foo.gif (#x6000008650C0)>
>> ? (#_CGImageDestinationCreateWithURL url #$kUTTypeGIF 2 +null-ptr+)
>> #<NS-OBJECT [uninitialized] (#x6000000B49A0)>
>> ? 
>> 
>> Trying to use the uninitialized object doesn’t crash CCL, but also doesn’t produce any results.  Any idea what I’m doing wrong here?
>> 
>> Thanks,
>> rg
>> 
>> P.S.: Some additional code just in case you want to noodle around with this:
>> 
>> (defun make-blank-image (x y)
>> (#/initWithSize: (make-instance 'ns:ns-image) (nssize x y)))
>> 
>> (defun nsstr (s)
>> (with-encoded-cstrs :utf-8 ((s s))
>> (#/stringWithUTF8String: ns:ns-string s)))
>> 
>> (setf prep (#/dictionaryWithObject:forKey:
>>         ns:ns-dictionary
>>         (#/dictionaryWithObject:forKey:
>>          ns:ns-dictionary
>>          (#/initWithFloat: (#/alloc ns:ns-number) (coerce 0.2 'single-float))
>>          #$kCGImagePropertyGIFDelayTime)
>>         #$kCGImagePropertyGIFDictionary))
>> 
>> (setf img (make-blank-image 10 10))
>> (setf dest (#_CGImageDestinationCreateWithURL url #$kUTTypeGIF 2 +null-ptr+)) ; Not working
>> (#_CGImageDestinationAddImage dest img prep)
>> (#_CGImageDestinationFinalize dest)
>> 
>> _______________________________________________
>> Openmcl-devel mailing list
>> Openmcl-devel at clozure.com
>> https://lists.clozure.com/mailman/listinfo/openmcl-devel
> 




More information about the Openmcl-devel mailing list