RosettaCodeData/Task/Bitmap-Histogram/Phix/bitmap-histogram.phix

40 lines
1.2 KiB
Plaintext

-- demo\rosetta\Bitmap_Histogram.exw (runnable version)
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements)
function to_bw(sequence image)
sequence hist = repeat(0,256)
for x=1 to length(image) do
for y=1 to length(image[x]) do
integer pixel = image[x][y] -- red,green,blue
sequence r_g_b = sq_and_bits(pixel,{#FF0000,#FF00,#FF})
integer {r,g,b} = sq_floor_div(r_g_b,{#010000,#0100,#01}),
lum = floor(0.2126*r + 0.7152*g + 0.0722*b)
image[x][y] = lum
hist[lum+1] += 1
end for
end for
integer lo = 1, hi = 256,
ltot = hist[lo],
rtot = hist[hi]
while lo!=hi do
if ltot<rtot then
lo += 1
ltot += hist[lo]
else
hi -= 1
rtot += hist[hi]
end if
end while
integer lum = lo
for i=1 to length(image) do
for j=1 to length(image[i]) do
image[i][j] = iff(image[i][j]<lum?black:white)
end for
end for
return image
end function
sequence img = read_ppm("Lena.ppm")
img = to_bw(img)
write_ppm("LenaBW.ppm",img)