INSTALL @lib$+"SORTLIB" Sort% = FN_sortinit(0,0) Width% = 200 Height% = 200 VDU 23,22,Width%;Height%;8,16,16,128 *display c:\lenagrey DIM hist%(255), idx%(255) FOR i% = 0 TO 255 : idx%(i%) = i% : NEXT REM Build histogram: FOR y% = 0 TO Height%-1 FOR x% = 0 TO Width%-1 l% = FNgetpixel(x%,y%) AND &FF hist%(l%) += 1 NEXT NEXT y% REM Sort histogram: C% = 256 CALL Sort%, hist%(0), idx%(0) REM Find median: total% = SUM(hist%()) half% = 0 FOR i% = 0 TO 255 half% += hist%(i%) IF half% >= total%/2 THEN median% = idx%(i%) EXIT FOR ENDIF NEXT REM Display black & white version: FOR y% = 0 TO Height%-1 FOR x% = 0 TO Width%-1 l% = FNgetpixel(x%,y%) AND &FF IF l% > median% THEN PROCsetpixel(x%,y%,255,255,255) ELSE PROCsetpixel(x%,y%,0,0,0) ENDIF NEXT NEXT y% END DEF PROCsetpixel(x%,y%,r%,g%,b%) COLOUR 1,r%,g%,b% GCOL 1 LINE x%*2,y%*2,x%*2,y%*2 ENDPROC DEF FNgetpixel(x%,y%) LOCAL col% col% = TINT(x%*2,y%*2) SWAP ?^col%,?(^col%+2) = col%