From: Paul Duncan Date: 2002-04-23T04:07:23+09:00 Subject: Imlib2-Ruby Image#draw_pixel Bug (was Re: [ANNOUNCE] Imlib2-Ruby 0.4.0) * Phlip (phlip_cpp@yahoo.com) wrote: > 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? Hi Phlip, It looks like there's a bug in Imlib2::Image#draw_pixel for the three-argument form of the commands (the array or hash-version of the command work correctly). Unfortunately there also appears to be a bug in Imlib2's imlib_image_draw_pixel(). I fixed the problem with Imlib2::Image#draw_pixel, and I added an override in Imlib2-Ruby for the Imlib2 bug; I'll take a better look at it this evening and see if I can't pin the problem down. Anyway, I applied the fixes above, your suggestion from the previous email, and a few other minor changes and released Imlib2-Ruby 0.4.1; you can find it at http://www.pablotron.org/software/imlib2-ruby/ (or skip straight to the tarball at http://www.pablotron.org/download/imlib2-ruby-0.4.1.tar.gz). A few comments about your code below: you should probably be using Imlib2::Image::load instead of Imlib2::Image::load_image; the former is the ruby-ized version of image loading, complete with exceptions, and the latter is a wrapper for imlib_load_image() (eg no error return, no exceptions, etc). I also noticed you shell out to remove a file before saving it. You can probably save the file w/o removing the previous one; your OS should automatically remove the existing file when you call Imlib2::Image#save. Anyway, nice to see people using stuff I've made. Thanks for the suggestsions, and feel free to holler at me if you have any more problems. :) > -- > 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 -- Paul Duncan pabs in #gah (OPN IRC) http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562