working version for generating a binary image