Paste: example of using the images vocabulary

Author: _phred
Mode: factor
Date: Mon, 17 Jan 2011 21:10:08
Plain Text |
USING: images sequences byte-arrays images.viewer ;

<image>
16 16 2array >>dim  #! dimensions of the image
ubyte-components >>component-type   #! each image element is an unsigned byte
RGB >>component-order   #! and the order is red, green, blue
16 16 3 * * iota >byte-array    #! make a 16x16x(3 bytes per pixel) array of
                                #! ascending integers

dup image-window    #! open a window with the image

Annotation: sample code to resize and output bitmaps 50%

Author: otoburb
Mode: factor
Date: Mon, 17 Jan 2011 22:23:43
Plain Text |
USING: io.pathnames sequences locals kernel prettyprint accessors math math.functions io.encodings.binary io.files images images.bitmap images.loader ;
IN: bitmaps

! Set the working directory
: image-directory ( -- string ) 
    home "/Downloads/factor/work/bitmaps/" append ;
    
:: image-file ( filename -- filename-path ) 
    image-directory filename append ;
        
:: (bottom-bitmap) ( image -- image' )
    image [ dup 1 swap [ 2 / round ] change-nth ] change-dim
    dup dim>> dup first [ 3 * ] [ 4 mod ] bi + swap 1 swap ?nth * :> top-length
    [ top-length head ] change-bitmap ;

:: (top-bitmap) ( image -- image' )
    image [ dup 1 swap [ 2 / floor ] change-nth ] change-dim
    dup dim>> dup first [ 3 * ] [ 4 mod ] bi + swap 1 swap ?nth * :> bottom-length
    [ dup length bottom-length - tail ] change-bitmap ;
       
: (write-bitmap) ( image filename -- ) 
    image-file binary [ output-bmp ] with-file-writer ;
    
: (clone-image) ( image -- image image' )
    dup clone [ clone ] change-bitmap [ clone ] change-dim ;     
    ! two separate image tuples on the stack
    
! Horizontal (row) 50% split    
: image-resize50%h ( image -- )
    (clone-image)
    (top-bitmap) "out1.bmp" (write-bitmap) ! write the top half
    (bottom-bitmap) "out2.bmp" (write-bitmap) ; ! write the bottom half
        
: resize50% ( bitmap-filename -- )
    image-file load-image image-resize50%h ;

New Annotation

Summary:
Author:
Mode:
Body: