function to_bw(sequence image) sequence color integer lum sequence hist = repeat(0,256) integer l = 1, r = 256 integer ltot, rtot for i=1 to length(image) do for j=1 to length(image[i]) do color = sq_div(sq_and_bits(image[i][j], {#FF0000,#FF00,#FF}), {#010000,#0100,#01}) lum = floor(0.2126*color[1] + 0.7152*color[2] + 0.0722*color[3]) image[i][j] = lum hist[lum+1] += 1 end for end for ltot = hist[l] rtot = hist[r] while l!=r do if ltot