Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Gosu and ChunkyPNG/MiniMagick: Blob length mismatch
- - By NYU Date 2017-01-11 11:18
Hello.

I tried to add in a first time MiniMagick then ChunkyPNG to my Gosu project. However, I found some strange issues.

First, for both libraries, they work nice without Gosu. But when I'm creating an image like this (here with ChunkyPNG):
image = ChunkyPNG::Image.from_file 'img.png'
@my_image = Gosu::Image.new image

I have an error telling me that both methods columns and rows are undefined. Well, I created them quickly to fix that (example with ChunkyPNG):
module ChunkyPNG
  class Canvas
    def columns ; @width ; end
    def rows ; @height ; end
  end
end

And of course the errors disappeared. But now, I have a new issue: Blob length mismatch (RuntimeError). But I checked, columns and rows return the right value. I think it's related to compression level or something like that when generating the blob.

I have the same issue with MiniMagick: columns and rows return the right value but the blob is not accepted.

So. Do you have some idea to fix it ?

Thank you for your attention,
NYU.
Parent - - By jlnr (dev) Date 2017-01-12 06:32 Edited 2017-01-12 06:42
Gosu expects raw RGBA pixel data in the string, and Canvas#to_blob returns compressed data. The return value of to_blob has to have a length of either columns * rows * 4 or columns * rows * 16 (the latter is rare, when float RGBA values).

Looking at the ChunkyPNG docs, I think to_rgba_stream returns something that can be used with Gosu. In that case, you should probably create a wrapper for ChunkyPNG::Canvas instead of monkey-patching Canvas and changing the names. Example (untested):

module ChunkyPNG
  class Canvas
    GosuWrapper = Struct.new(:columns, :rows, :to_blob)
    def to_gosu
      GosuWrapper.new(@width, @height, to_rgba_stream)
    end
  end
end
...
Gosu::Image.new(my_canvas.to_gosu)


Does that work?
Parent - By NYU Date 2017-01-12 09:07
It works like a charm. Thank you very much.

It's really curious however: I just checked an older project using an older version of Gosu and it was working nicely without that fix. Well.
Parent - - By jlnr (dev) Date 2017-01-12 06:41
Hm, not sure what MiniMagick::Image#to_blob is good for, it just seems to read a file (expand the source code): http://www.rubydoc.info/github/minimagick/minimagick/MiniMagick%2FImage%3Ato_blob

MiniMagick has an import_pixels method that can import a "blob" (as defined by Gosu), but I can't find the other direction.

This StackOverflow answer also doesn't lead anywhere: http://stackoverflow.com/questions/7152362/return-array-of-pixel-values-using-ruby-and-mini-magick-or-rmagick

It's probably easier to read the image from disk with Gosu.
Parent - By NYU Date 2017-01-12 09:12
I will just go with ChunkyPNG I think. The only interest I had in MiniMagick was the ability to load animated gif.
I don't know if another library allows it. I will look for an alternative ; in any case, MiniMagick was too big for what I needed.
Up Topic Gosu / Gosu Exchange / Gosu and ChunkyPNG/MiniMagick: Blob length mismatch

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill