如何从二进制(图像)中删除所有“黑色像素”?

时间:2013-11-06 05:59:04

标签: rebol rebol3

我正在寻找性能最佳的代码,它会从二进制文件中删除所有黑色像素=#{000000}。代码示例:

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执行速度非常快但不能正常工作,因为它会从二进制文件中删除所有零字节#{00}。

从二进制文件中删除所有“黑色像素”=三个零字节=#{000000}的最快代码是什么?还有什么建议?可能使用解析表现更好吗?

2 个答案:

答案 0 :(得分:4)

使用PARSE并创建新的二进制序列应该是最快的方法:

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

此处它的执行速度几乎是使用REMOVE-EACH的两倍。

答案 1 :(得分:2)

比移除快四倍 - 每个例子: - )

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

结果

; remove-each
==========
0:00:00.003395
19683

; parse with concatenated append
==========
0:00:00.000872
19683