Ben Greenman
Ben Greenman

Reputation: 2002

racket: pict to bytes (pixels)

How can I turn a pict into a byte string of the pixels in it?

(I mean, a byte string of the pixels that would be in the bitmap you'd get from rendering the pict)

This function works:

#lang racket/base
(require pict racket/class)

(define (pict->pixels p)                                                                    
  (define b (pict->bitmap p))                                                               
  (define w (send b get-width))                                                             
  (define h (send b get-height))                                                            
  (define pixbuf (make-bytes (* 4 (inexact->exact (ceiling (* w h))))))                     
  (send b get-argb-pixels 0 0 w h pixbuf)                                                   
  pixbuf)

But is there a simpler way?

Upvotes: 2

Views: 139

Answers (1)

Ben Greenman
Ben Greenman

Reputation: 2002

Yes! There is a simpler way: pict->argb-pixels

Here's a test case:

#lang racket/base
(require pict racket/class rackunit)

(define (pict->pixels p)
  (define b (pict->bitmap p))
  (define w (send b get-width))
  (define h (send b get-height))
  (define pixbuf (make-bytes (* 4 (inexact->exact (ceiling (* w h))))))
  (send b get-argb-pixels 0 0 w h pixbuf)
  pixbuf)

(define my-pict (jack-o-lantern 100))
(check-equal? (pict->pixels my-pict) (pict->argb-pixels my-pict))

Upvotes: 1

Related Questions