Search code examples
rebolrebol3

How do I remove all "black pixels" from a binary (image)?


I am looking for the best performing code, which removes all black pixels = #{000000} from a binary. Code Example:

img: make image! [100x75 0.0.255]
loop 1000 [change at img random 99x74 0.0.0]
probe length? foo: copy img/rgb
probe delta-time [remove-each [r g b] foo [ all [zero? r zero? g zero? b] ]]
probe length? foo
foo: copy img/rgb
probe delta-time [trim/with foo #{000000}]
probe length? probe foo

Trim performs quite fast but doesn't work as supposed, as it removes all zero-byte #{00} from the binary.

What is the fastest code to remove all "black pixels" = three zero-bytes = #{000000} from a binary? Any further suggestions? Might be using parse performs better?


Solution

  • four times faster than Remove-each example :-)

    print "=========="
    probe length? foo: copy img/rgb
    new: make binary! length? foo
    probe delta-time [
        parse foo [
            here:
            there: 
            any [
                #{000000} (append/part new here there ) here: there: 
                | 3 skip there: 
            ]
            (append/part new here there  )    
        ]
    ]
    probe length? new
    

    results

    ; remove-each
    ==========
    0:00:00.003395
    19683
    
    ; parse with concatenated append
    ==========
    0:00:00.000872
    19683