Re: [racket-users] Re: [racket] How to extend image-snip% with a new field
Ah, right: that code didn't actually save the bitmap itself. How do you like this code? Robby #lang racket/gui (require (prefix-in - racket/base)) (provide (rename-out [image-data-snip-class snip-class]) image-data-snip%) (define image-data-snip% (class image-snip% (inherit set-snipclass get-flags set-flags get-admin get-extent draw) (define metadata (make-hash)) (define/public (set-metadata md) (set! metadata md)) (super-new) (set-snipclass image-data-snip-class) (set-flags (cons 'handles-events (get-flags))) (inherit get-bitmap) (define/override (copy) (define ids (new image-data-snip%)) (send ids set-bitmap (get-bitmap)) (send ids set-metadata metadata) ids) (define/override (write f) (define bmp (get-bitmap)) (define w (send bmp get-width)) (define h (send bmp get-height) ) (define p (open-output-bytes)) (-write (list metadata w h) p) (define b (get-output-bytes p)) (define ib (make-bytes (* 4 w h))) (send bmp get-argb-pixels 0 0 w h ib) (send f put (bytes-length b) b) (send f put (bytes-length ib) ib)) ;;Operations on the metadata ;;; (define/public (get-arg-pos arg) (hash-ref metadata arg '(0.5 0.5))) (define/public (set-arg-pos arg pos) (hash-set! metadata arg pos)) (define/public (get-args-pos) (hash->list metadata (define image-data-snip-class% (class snip-class% (inherit set-classname) (super-new) (set-classname (~s '(lib "main.rkt" "image-data-snip"))) (define/override (read f) (match-define (list md w h) (-read (open-input-bytes (send f get-unterminated-bytes (define img-bytes (send f get-unterminated-bytes)) (define bmp (make-bitmap w h)) (send bmp set-argb-pixels 0 0 w h img-bytes) (define ids (new image-data-snip%)) (send ids set-metadata md) (send ids set-bitmap bmp) ids))) (define image-data-snip-class (new image-data-snip-class%)) (send (get-the-snip-class-list) add image-data-snip-class) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Re: [racket] How to extend image-snip% with a new field
quarta-feira, 2 de Dezembro de 2015 às 13:34:21 UTC, Robby Findler escreveu: > I think this code is doing what you want. > > Robby > > #lang racket/gui > > (require (prefix-in - racket/base)) > (provide (rename-out [image-data-snip-class snip-class]) > image-data-snip%) > > (define image-data-snip% > (class image-snip% > (inherit set-snipclass > get-flags set-flags > get-admin > get-extent > draw) > > (define metadata (make-hash)) > (define/public (set-metadata md) (set! metadata md)) > (super-new) > > (set-snipclass image-data-snip-class) > (set-flags (cons 'handles-events (get-flags))) > > (define/override (copy) > (define ids (new image-data-snip%)) > (send ids set-metadata metadata) > ids) > > (define/override (write f) > (define p (open-output-bytes)) > (-write metadata p) > (define b (get-output-bytes p)) > (send f put (bytes-length b) b)) > > > ;;Operations on the metadata ;;; > > > (define/public (get-arg-pos arg) > (hash-ref metadata arg '(0.5 0.5))) > > (define/public (set-arg-pos arg pos) > (hash-set! metadata arg pos)) > > (define/public (get-args-pos) > (hash->list metadata > > (define image-data-snip-class% > (class snip-class% > (inherit set-classname) > > (super-new) > (set-classname (~s '(lib "main.rkt" "image-data-snip"))) > > (define/override (read f) > (define byts (send f get-unterminated-bytes)) > (define ids (new image-data-snip%)) > (send ids set-metadata (-read (open-input-bytes byts))) > ids))) > > (define image-data-snip-class (new image-data-snip-class%)) > (send (get-the-snip-class-list) add image-data-snip-class) Thanks Robby, for the reply, this code do almost everything that I want. The missing part is to display the bitmap when the object is created. For example, the following evocation returns an empty image >> (define ids (make-object image-data-snip% "path/to/img.png")) >> ids |x| But if I try.. >> (send ids get-bitmap) It returns the filled bitmap. I tried to rewrite the draw method, but in the subclass I don't have the bitmap yet unless I store it there. However, it seems awkward to store the bitmap in this class again. Can you tell me what is the right way to solve this problem. Thank you very much, and sorry for my inconvenience. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Re: [racket] How to extend image-snip% with a new field
I think this code is doing what you want. Robby #lang racket/gui (require (prefix-in - racket/base)) (provide (rename-out [image-data-snip-class snip-class]) image-data-snip%) (define image-data-snip% (class image-snip% (inherit set-snipclass get-flags set-flags get-admin get-extent draw) (define metadata (make-hash)) (define/public (set-metadata md) (set! metadata md)) (super-new) (set-snipclass image-data-snip-class) (set-flags (cons 'handles-events (get-flags))) (define/override (copy) (define ids (new image-data-snip%)) (send ids set-metadata metadata) ids) (define/override (write f) (define p (open-output-bytes)) (-write metadata p) (define b (get-output-bytes p)) (send f put (bytes-length b) b)) ;;Operations on the metadata ;;; (define/public (get-arg-pos arg) (hash-ref metadata arg '(0.5 0.5))) (define/public (set-arg-pos arg pos) (hash-set! metadata arg pos)) (define/public (get-args-pos) (hash->list metadata (define image-data-snip-class% (class snip-class% (inherit set-classname) (super-new) (set-classname (~s '(lib "main.rkt" "image-data-snip"))) (define/override (read f) (define byts (send f get-unterminated-bytes)) (define ids (new image-data-snip%)) (send ids set-metadata (-read (open-input-bytes byts))) ids))) (define image-data-snip-class (new image-data-snip-class%)) (send (get-the-snip-class-list) add image-data-snip-class) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] Re: [racket] How to extend image-snip% with a new field
Hello again, I have a more concrete problem here. The following class extends the image-snip% with a float (size), and overrides the methods copy and write as supposed. (define extend-snip% (class image-snip% (init-field [size 20.0]) (super-new) (define/override (copy) (new circle-snip% [size size])) (define/override (write f) (send f put size But when I create an object of this class, for example: > (make-object extend-snip% 30 "~/Documents/img.png") I expected to see the bitmap here (wrapped in this class) However, it throws the following error: draw-rectangle: contract violation expected: (not/c negative?) given: -2.0 ... Someone can give me a more useful explanation? Thanks. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.