Paul:

Below my sig is a Ruby script that is attempting to convert a color PNG to 
black and white, and to burn and dodge so near-white is white and 
near-black is black. One invokes the script like this:

ruby convert.rb --in input.png \
        --minGrey 180 --maxGrey 200 \
        --out output.png

The problem is 'draw_pixel' does not appear to do anything. I can muddle by 
with fill_rect, but this seems to blend pixels.

How can we make 'draw_pixel' do anything?

-- 
  Phlip

#!/usr/bin/env ruby

require 'imlib2'
require 'parsearg'


def colorIsLessThan pix, threshold
        return ( pix.red   < threshold or 
                 pix.green < threshold or 
                 pix.blue  < threshold )
end


def convert
        im = Imlib2::Image.load_image $OPT_in
        w, h = im.width, im.height

        rect = [0, 0, 1, 1]
        blackness    = $OPT_minGrey.to_i

        puts im.get_format
        puts im.query_pixel(2, 2).red
        puts im.query_pixel(2, 2).green
        puts im.query_pixel(2, 2).blue
        puts im.has_alpha
        
        return if $OPT_minGrey == nil

# TODO more realistic argument response
        
        puts "Converting..."
        
        palegreyness = $OPT_maxGrey.to_i
        diff = palegreyness - blackness
        darkgreyness = diff / 3.0
        greyness     = 2 * diff / 3.0
        
        levels = [[blackness,    Imlib2::Color::BLACK]]
        
        rate = 255 / (palegreyness - blackness)
        
        ((blackness + 1)..palegreyness).each do | tick |
                puts tick
                level = (tick - blackness) * rate
                puts level
                color = Imlib2::Color::RgbaColor.new (level, level, level, 0)

                '''puts color.red
                puts color.green
                puts color.blue'''

                levels << [tick, color]
        end     

0.upto(w) { |x|
  0.upto(h) { |y|
      pix = im.query_pixel(x, y)
      
      rect[0] = x
      rect[1] = y
      nuColor = Imlib2::Color::WHITE

      levels.each do | test, aColor |

      if colorIsLessThan(pix, test) then
         nuColor = aColor
         break       
      end

        end
      
      # im.fill_rect rect, nuColor
      im.draw_pixel x, y, nuColor
  }
}

        system 'rm -f ' + $OPT_out
        im.save $OPT_out
        system 'kview ' + $OPT_out

end


parseArgs(0, nil, "", "in:", "out:", "minGrey:", "maxGrey:")

convert
exit