Forum: Ruby RMagick 1.9.3: examples/histogram.rb - erro

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ces.fci.junk (Guest)
on 2005-11-14 08:09
(Received via mailing list)
Trying to run the histogram.rb ~/$myimage produces the following error:

Creating b194000514_7064_Histogram.miffhistogram.rb:180:in `histogram':
undefined method `+' for nil:NilClass (NoMethodError)
        from histogram.rb:179:in `each'
        from histogram.rb:179:in `histogram'
        from histogram.rb:177:in `times'
        from histogram.rb:177:in `histogram'
        from histogram.rb:280

For the moment commenting these lines will let it run and output a
viewable miff (beginning at line 180):
                    #red[pixel.red] += 1
                    #green[pixel.green] += 1
                    #blue[pixel.blue] += 1

                    # Only count opacity channel if some pixels are not
opaque.
                    if !opaque?
                        alpha[pixel.opacity] += 1
                    end
                    v = pixel_intensity(pixel)
                    #int[v] += 1


Potentially relevent (or not relevent) information:
Ruby 1.8.2
ImageMagick 6.1.9
Linux 2.6.10

The other examples appear to run without issue.
I am also relatively new to Ruby if that matters.

I tried adding these in the appropiate locations (figured it may have
had problems adding to nil):
red[pixel.red]     = 0 if red[pixel.red].nil?
blue[pixel.blue]   = 0 if blue[pixel.blue].nil?
green[pixel.green] = 0 if green[pixel.green].nil?
#...
alpha[pixel.opacity] = 0 if alpha[pixel.opacity].nil?
#...
int[v]  = 0 if int[v].nil?

The result is another error, but I am not sure how to get around it:
Creating b194000514_7064_Histogram.miff.histogram.rb:5:in `max':
undefined method `<=>' for nil:NilClass (NoMethodError)
        from histogram.rb:208:in `each'
        from histogram.rb:208:in `max'
        from histogram.rb:208:in `histogram'
        from histogram.rb:292

any thoughts on the matter?

Clayton
rmagick (Guest)
on 2005-11-14 15:13
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> Trying to run the histogram.rb ~/$myimage produces the following error:
>
> Creating b194000514_7064_Histogram.miffhistogram.rb:180:in `histogram':
> undefined method `+' for nil:NilClass (NoMethodError)
>         from histogram.rb:179:in `each'
>         from histogram.rb:179:in `histogram'
>         from histogram.rb:177:in `times'
>         from histogram.rb:177:in `histogram'
>         from histogram.rb:280

Hmmm...I get it, too. I should I say I "got" it, because after I
started investigating the problem I started getting other very odd
errors and then it started working. Now I can't make it happen anymore.
These are classic symptoms of a wild pointer either in RMagick or
ImageMagick. I'll poke around a little bit more. Whatever the problem
is, it can't be fixed by changing the script.

Thanks for pointing this out.
ces.fci.junk (Guest)
on 2005-11-14 18:20
(Received via mailing list)
I believe I figured it out although I'm not entirely sure of the
solution (unless you can tell me how to loop through all the values and
unset the nil values -- note: i'm still new to ruby).
running this lets me know there is a lot of nil values which would
explain the "nil:NilClass (NoMethodError)" -- nil doesn't have the max
method:
for i in red
    p "nil!" if i.nil?
end

Now, what is strange about this is that the arrays are *already*
defined to all have zero:
red   = Array.new(HISTOGRAM_COLS, 0)
green = Array.new(HISTOGRAM_COLS, 0)
blue  = Array.new(HISTOGRAM_COLS, 0)
alpha = Array.new(HISTOGRAM_COLS, 0)
int   = Array.new(HISTOGRAM_COLS, 0)

I tried changing all of the 0's to 1's to see if that would make a
difference (it did not).  I also tried using the fill method which had
no effect.

but, if you say it is a wild pointer I'll concede.

I had to download xwindow.c and xwindow.h from the sourceforge page
since they weren't included with my source[for ImageMagick] -- I had
received a compile error which said they were missing (maybe that is
related[?]).

Clayton
Timothy H. (Guest)
on 2005-11-15 00:44
(Received via mailing list)
> Now, what is strange about this is that the arrays are *already*
> defined to all have zero:

I'm reasonably sure there's nothing wrong with the script itself, at
least sure enought that it's not at the top of my list for possible
causes. I'll investigate.
Timothy H. (Guest)
on 2005-11-15 01:59
(Received via mailing list)
removed_email_address@domain.invalid wrote:
> I believe I figured it out although I'm not entirely sure of the
> solution (unless you can tell me how to loop through all the values and
> unset the nil values -- note: i'm still new to ruby).
> running this lets me know there is a lot of nil values which would
> explain the "nil:NilClass (NoMethodError)" -- nil doesn't have the max
> method:
> for i in red
>     p "nil!" if i.nil?
> end
>

Would you mind issuing this little command and sending me the output?

ruby -r rmagick -e'p Magick::Long_version'

Thanks!
ces.fci.junk (Guest)
on 2005-11-15 04:54
(Received via mailing list)
Here's the output:
% ruby -r RMagick -e'puts Magick::Long_version'
This is RMagick 1.9.3 ($Date: 2005/10/13 23:01:29 $) Copyright (C) 2005
by Timothy P. Hunter
Built with ImageMagick 6.1.9 01/20/05 Q16 http://www.imagemagick.org
Built for ruby 1.8.2 (2004-12-25) [i686-linux]
Web page: http://rmagick.rubyforge.org
Email: removed_email_address@domain.invalid

Clayton
This topic is locked and can not be replied to.