[Openmcl-devel] Advice needed: best way to fram a Lisp array into an ns-image

R. Matthew Emerson rme at clozure.com
Fri Jan 18 22:44:54 PST 2008


On Jan 19, 2008, at 1:19 AM, Ron Garret wrote:

>
> On Jan 18, 2008, at 7:10 PM, Ron Garret wrote:
>
>> That's pretty much what I'm doing, so I guess that means I'm kinda on
>> the right track.
>
> Well, I thought I was on the right track.  I tried this:
>
> (defun stuff-pixels (pixels)
>  (bind ((x y bpp) (array-dimensions pixels)
>         (:values v ptr) (make-heap-ivector (* x y bpp) '(unsigned- 
> byte 8)))
>    (dotimes (i (* x y bpp))
>      (setf (aref v i) (row-major-aref pixels i)))
>    (#/ 
> initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bytesPerRow:bitsPerPixel 
> :
>     (#/alloc ns:ns-bitmap-image-rep)
>     ptr
>     x y 8 3 0 0 #@"NSCalibratedRGBColorSpace" (* x 3) 24)))
>
> It seems to work:
>
> ? (stuff-pixels #3A(((0 0 0) (0 0 0) (0 0 0) (0 0 0))
> ((0 0 0) (0 0 0) (0 0 0) (0 0 0))
> ((0 0 0) (0 0 0) (0 0 0) (0 0 0))((0 0 0) (0 0 0) (0 0 0) (0 0 0)))
> )
> #<NS-BITMAP-IMAGE-REP NSBitmapImageRep 0xa992900 Size={4, 4}  
> ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=4x4  
> Alpha=NO Planar=NO Format=0 (#xA992900)>
> ?
>
> But the resulting bitmapRep is corrupt:
>
> ? (#/bitmapData *)
> Unhandled exception 11 at 0x7fff82855d80, context->regs at #xb0c3e350
> Exception occurred while executing foreign code
> ? for help
> [58183] OpenMCL kernel debugger:

I think I ran into this same thing myself a while ago.

The first arg passed to #/initWithBitmapDataPlanes:... actually needs  
to be a pointer to a pointer (a char ** in this case).

Try something like

(defun stuff-pixels (pixels)
   ...
   (rlet ((p :address ptr)) ; p now points to ptr
     (#/initWith....
       (#/alloc ...)
       p
       ...)))


Here's a snippet from the Cocoa IDE that creates an image from data in  
a lisp array.  Note the use of RLET, and the CLOS-y way to create an  
Objective-C instance, which is particularly nice in the case of this  
method, which is probably the longest Cocoa method name in the  
framework.

(defun create-modeline-pattern-image ()
   (let* ((n (length *modeline-grays*)))
     (multiple-value-bind (samples-array samples-macptr)
         (make-heap-ivector n '(unsigned-byte 8))
       (dotimes (i n)
         (setf (aref samples-array i) (aref *modeline-grays* i)))
       (rlet ((p :address samples-macptr))
         (let* ((rep (make-instance 'ns:ns-bitmap-image-rep
                                    :with-bitmap-data-planes p
                                    :pixels-wide 1
                                    :pixels-high n
                                    :bits-per-sample 8
                                    :samples-per-pixel 1
                                    :has-alpha #$NO
                                    :is-planar #$NO
                                    :color-space-name  
#&NSDeviceWhiteColorSpace
                                    :bytes-per-row 1
                                    :bits-per-pixel 8))
                (image (make-instance 'ns:ns-image
                                      :with-size (ns:make-ns-size 1  
n))))
           (#/addRepresentation: image rep)
           (#/release rep)
           (setf *modeline-pattern-image* image))))))
  
    



More information about the Openmcl-devel mailing list