4

I have a C# method:

  public static IEnumerator getPixels(Picture picture) {
    for (int x=0; x < picture.width; x++) {
      for (int y=0; y < picture.height; y++) {
    yield return picture.getPixel(x, y);
      }
    }
  }

I can call this fine in IronPython:

for pixel in getPixels(pic):
    r, g, b = getRGB(pixel)
    gray = (r + g + b)/3
    setRGB(pixel, gray, gray, gray)

But I don't see how to call this from IronRuby:

Myro::getPixels(pic) do |pixel|
    r, g, b = Myro::getRGB pixel
    gray = (r + g + b)/3
    Myro::setRGB(pixel, gray, gray, gray)
end

All I get back is Graphics+<getPixels>c__Iterator0.

What do I need to do to actually get each pixel in IronRuby and process it?

Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
Doug Blank
  • 2,031
  • 18
  • 36
  • I cant directly answer you question but there is an innacuracy of calculation of the perceptive gray: It should be a dot product of rgb and 0.3, 0.59, 0.11. People percive brightness differently across colours. All this applies if you wish to represent the preceptive luminosity of the colour. – Marino Šimić May 23 '11 at 22:15
  • You're right: that isn't a direct answer. And not even an indirect answer :) You are also correct that people perceive the luminance of RGB unequally, and there are many possible values for RGB weightings to make a more pleasing grayscale image. But this has nothing to do with the issue at hand (and the code, in fact, makes a perfectly fine grayscale image). Now I need to figure out how to invoke a Generator in IronRuby... – Doug Blank May 23 '11 at 22:37

1 Answers1

3

From Jimmy Schementi:

http://rubyforge.org/pipermail/ironruby-core/2011-May/007982.html

If you change the return type of getPixels to IEnumerable, then this works:

Myro::getPixels(pic).each do |pixel|
    ...
end

And arguably it should be IEnumerable rather than IEnumerator, as an IEnumerable.GetEnumerator() gives you an IEnumerator.

Your code example passes a closure to the getPixels method, which just gets ignored (all Ruby methods syntactically accept a block/closure, and they can choose to use it), and returns the IEnumerator.

IronRuby today doesn't support mapping Ruby's Enumerable module to IEnumerator objects, since it's kind awkward to return an IEnumerator rather than an IEnumerable from a public API, but since IronPython sets a precedence for supporting it we should look into it. Opened http://ironruby.codeplex.com/workitem/6154.

~Jimmy

Doug Blank
  • 2,031
  • 18
  • 36