Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

Using PARSE and creating a new binary series should be the fastest method:

probe length? foo: copy img/rgb
new: make binary! length? foo

probe delta-time [
    parse foo [any [s: #{000000} | 3 skip (append/part new s 3)]]
]
probe length? new

Here it performs almost twice faster than using REMOVE-EACH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top