Threshold filters (Rev Newsletter #48)

BNig niggemann at uni-wh.de
Thu Feb 14 07:36:42 EST 2008


Wilhelm,

try this for the threshold from red

on mouseup
    answer "Select threshold to create the black-and-white image:" with  
"80"  or "100"  or "128" or "150" or "180"
    wait 3 milliseconds
    put it into threshold
    set the cursor to watch
    put the imagedata of image x into iData
    put "" into tOutData
    put numtochar(255) into tAlphabit
    put length(iData) into tLength
    
    put the millisec into tStart
    
    repeat with i = 2 to tLength  step 4  -- i=2 for Red, i=3 for green, i=4
for blue
        if chartonum(char i of iData) >= threshold then put numtochar(255)
into tC else put numtochar(0) into tC
        put tAlphabit & tC & tC & tC after tOutData
    end repeat
    
      put the millisec - tStart
      set the imagedata of image x  to tOutData 
end mouseUp


apparently 'for each' is slower for chars, maybe Rev has to load each char
into the variable and accessing chars with some pointer magic is faster ??,
especially since only one fourth of the chars is accessed here (step 4)

interestingly I found the following script to be minimally but consistently
faster then the above although it has to load chunks of data and a nested
repeat loop

on mouseup
    answer "Select threshold to create the black-and-white image:" with  
"80"  or "100"  or "128" or "150" or "180"
    wait 3 milliseconds
    put it into threshold
    put imagedata of image x into tData
    put the length of tData into tDataLength
    put the width of image x into theWidth
    put the height of image x into theHeight
    put theWidth * 4 into OneRow
     
    put numtochar(255) into tAlphaBit
    put 1 into tIncrement
    put "" into tDataOUt
    
    put the millisec into tStart
     
    repeat with n = 1 to theHeight
        put char tIncrement to (tIncrement + OneRow -1) of tData into
tOneRowData
        add OneRow to tIncrement
        
        repeat with p = 2 to OneRow - 4 step 4 -- i = 2 for Red, i=3 for
green, i=4 for blue
            if chartonum(char p of tOneRowData) >= threshold then put
numtochar(255) into tC else put numtochar(0) into tC
            put tAlphaBit & tC & tC & tC after tDataOUt
        end repeat
         
    end repeat
    put the millisec - tStart 
    set the imagedata of image x  to tDataOUt 
end mouseUp


best regards and thanks for your great image toolkit

Bernd



Wilhelm Sanke wrote:
> 
> 
> "on mouseup
>   answer "Select threshold to create the black-and-white image:" with  
> "80"  or "100"  or "128" or "150" or "180"
>   wait 3 milliseconds
>   put it into threshold
>   set the cursor to watch
>   put the imageData of image x into iData
>  
>   put 0 into counter
>   repeat for each char C in idata
>     add 1 to counter
>     if counter mod 4 = 2 then # the red pixel
>       put chartonum(C)  into tC
>       if tC > threshold then
>         put 255 into tC
>       else
>         put 0 into tC
>       end if
>     end if
>     put numtochar(tC) into char counter of idata
>   end repeat
>  
>   set the imageData of image x  to iData
> end mouseUp"
> 
> 
> Regards,
> 
> Wilhelm Sanke
> <http://www.sanke.org/MetaMedia>
> 
> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
> 
> 

-- 
View this message in context: http://www.nabble.com/Threshold-filters-%28Rev-Newsletter--48%29-tp15461928p15479415.html
Sent from the Revolution - User mailing list archive at Nabble.com.




More information about the use-livecode mailing list