[ANN] Ruby/GD2 alternative to Ruby/GD

Discussion in 'Ruby' started by Rob Leslie, Nov 5, 2005.

  1. Rob Leslie

    Rob Leslie Guest

    Hi all,

    I'm a fairly recent Ruby convert, and I'd appreciate some help
    getting my first library in a form suitable for mass consumption.

    The library I wrote is an alternative to Ruby/GD. I wrote it because
    I wasn't satisfied with the original; in particular, there was no
    support for creating images from JPEG or PNG data already in memory.
    My alternative is 100% Ruby, relying on Ruby/DL to link with the gd
    shared library.

    Most of the gd API is available albeit in quite different form,
    because I tried to write an interface The Ruby Way. Here is a taste:

    include GD2
    image = File.open('image.png') { |f| Image.load(f) }
    image.resize! 200, 300
    image.draw do |pen|
    pen.color = image.palette.find! Color.new(1.0, 0.75, 0.5)
    pen.thickness = 2
    pen.move_to 25, 50
    pen.line_to 175, 50
    pen.move -150, 25
    pen.font = Font::TrueType.new('/usr/share/fonts/times.ttf', 20)
    pen.text 'Hello, world!'
    end
    File.open('new-image.png', 'w') { |f| f.write(image.png_data) }

    I needed this library for another project, but I'd like to make it
    available for anyone else who might also find it useful. Since this
    is my first Ruby project of any significance, I'd appreciate some
    feedback and guidance on what is good and what could be better, and
    especially norms for packaging and documentation.

    Here is my preliminary release:

    ftp://ftp.mars.org/pub/ruby/ruby-gd2-0.1.tar.gz

    Thanks for any help,

    --
    Rob Leslie
    Rob Leslie, Nov 5, 2005
    #1
    1. Advertising

  2. ------=_Part_7287_6880156.1131183404572
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    A 'fairly recent Ruby convert', huh? Sure doesn't look like it :) Great job=
    Daniel Amelang, Nov 5, 2005
    #2
    1. Advertising

  3. Hi,

    In message "Re: [ANN] Ruby/GD2 alternative to Ruby/GD"
    on Sat, 5 Nov 2005 13:36:29 +0900, Rob Leslie <> writes:

    |The library I wrote is an alternative to Ruby/GD. I wrote it because
    |I wasn't satisfied with the original; in particular, there was no
    |support for creating images from JPEG or PNG data already in memory.
    |My alternative is 100% Ruby, relying on Ruby/DL to link with the gd
    |shared library.

    It's good to have another "Ruby Way" library.

    | image.resize! 200, 300
    | image.draw do |pen|
    | pen.color = image.palette.find! Color.new(1.0, 0.75, 0.5)

    I don't think resize! and find! do not require bang sign in the name.
    Note that, unlike Scheme, bang sign indicates "more dangerous"
    alternatives to non bang ones in Ruby, for example, sub (returns
    modified copy of the receiver) and sub! (modifies the receiver in
    place).

    matz.
    Yukihiro Matsumoto, Nov 5, 2005
    #3
  4. Rob Leslie

    Rob Leslie Guest

    Hi matz,

    On Nov 5, 2005, at 5:01 AM, Yukihiro Matsumoto wrote:
    >> image.resize! 200, 300
    >> image.draw do |pen|
    >> pen.color = image.palette.find! Color.new(1.0, 0.75, 0.5)

    >
    > I don't think resize! and find! do not require bang sign in the
    > name. Note that, unlike Scheme, bang sign indicates "more
    > dangerous" alternatives to non bang ones in Ruby, for example, sub
    > (returns modified copy of the receiver) and sub! (modifies the
    > receiver in place).


    Actually, I was conscious of this; the non-bang Image#resize does not
    modify the receiver but returns a new image instead. Also,
    Palette#find never modifies the palette, but Palette#find! allocates
    the color if it doesn't already exist.

    So I didn't use the bang sign without reason, but perhaps it's a bit
    much to have non-destructive image operations? (Besides #resize,
    there is also #rotate, #crop, #uncrop, and #polar_transform.)

    Thanks for the feedback.

    Cheers,
    -rob
    Rob Leslie, Nov 5, 2005
    #4
  5. Hi,

    In message "Re: [ANN] Ruby/GD2 alternative to Ruby/GD"
    on Sat, 5 Nov 2005 23:08:32 +0900, Rob Leslie <> writes:

    |> I don't think resize! and find! do not require bang sign in the
    |> name. Note that, unlike Scheme, bang sign indicates "more
    |> dangerous" alternatives to non bang ones in Ruby, for example, sub
    |> (returns modified copy of the receiver) and sub! (modifies the
    |> receiver in place).
    |
    |Actually, I was conscious of this; the non-bang Image#resize does not
    |modify the receiver but returns a new image instead. Also,
    |Palette#find never modifies the palette, but Palette#find! allocates
    |the color if it doesn't already exist.

    Hmm, I should have checked the API before making comments, sorry. But
    I still feel something weird with find!, but I can't express myself well.

    matz.
    Yukihiro Matsumoto, Nov 5, 2005
    #5
  6. Rob Leslie

    Jeff Wood Guest

    ------=_Part_3559_18397658.1131200589049
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    PUBLISH A GEM !!!!!! please!!!!
    oh, so nice ... adding imaging to rails apps .... mmmm
    j.

    On 11/5/05, Yukihiro Matsumoto <> wrote:
    >
    > Hi,
    >
    > In message "Re: [ANN] Ruby/GD2 alternative to Ruby/GD"
    > on Sat, 5 Nov 2005 23:08:32 +0900, Rob Leslie <> writes:
    >
    > |> I don't think resize! and find! do not require bang sign in the
    > |> name. Note that, unlike Scheme, bang sign indicates "more
    > |> dangerous" alternatives to non bang ones in Ruby, for example, sub
    > |> (returns modified copy of the receiver) and sub! (modifies the
    > |> receiver in place).
    > |
    > |Actually, I was conscious of this; the non-bang Image#resize does not
    > |modify the receiver but returns a new image instead. Also,
    > |Palette#find never modifies the palette, but Palette#find! allocates
    > |the color if it doesn't already exist.
    >
    > Hmm, I should have checked the API before making comments, sorry. But
    > I still feel something weird with find!, but I can't express myself well.
    >
    > matz.
    >
    >



    --
    "http://ruby-lang.org -- do you ruby?"

    Jeff Wood

    ------=_Part_3559_18397658.1131200589049--
    Jeff Wood, Nov 5, 2005
    #6
  7. Rob Leslie

    daz Guest

    Rob Leslie wrote:
    > On Nov 5, 2005, at 5:01 AM, Yukihiro Matsumoto wrote:
    > >> image.resize! 200, 300
    > >> image.draw do |pen|
    > >> pen.color = image.palette.find! Color.new(1.0, 0.75, 0.5)

    > >
    > > I don't think resize! and find! do not require bang sign in the
    > > name.

    >
    > Actually, I was conscious of this; the non-bang Image#resize does not
    > modify the receiver but returns a new image instead. Also,
    > Palette#find never modifies the palette, but Palette#find! allocates
    > the color if it doesn't already exist.
    >
    > So I didn't use the bang sign without reason, [...]



    Hi Rob,

    This is based on a cursory look, so forgive the inaccuracies.

    IMHO, resize! is fine but find! & allocate! could be better.

    You've already, perhaps, checked out other API's available
    from interfaces to other languages. Some are listed here:
    http://www.boutell.com/boutell/gdmanualtest.html#gdother

    ( None for Ruby -- you'll rectify that with an e-mail, later :-? )

    A clean-looking Lisp one I found is here:
    http://www.weitz.de/cl-gd/


    Always a good idea to keep an eye on the competition.

    ----- ----- ----- ----- -----

    > [...] but perhaps it's a bit much to have non-destructive
    > image operations?


    You surely wouldn't want to be doing non-destructive operations
    /accidentally/ on an in-memory copy. IOW, you might insist
    that *all* operations are destructive and provide an explicit
    dup/clone method to prevent excessive memory loading (?)


    > pen.color = image.palette.find! Color.new(1.0, 0.75, 0.5)


    A possibility here is:

    image.palette.find_color(1.0, 0.75, 0.5, :exact, :alloc)

    - where the first arg can be a Color, otherwise the first three args
    are R-G-B (with the rest as options). It's a tough call when there
    are multiple options but I tend away from adding methods for various
    option combinations ...

    e.g. avoid: find_exact_color(...)
    find_color_alloc(...)
    find_exact_color_alloc(...)
    ... etc.

    - when each is a variation on find_color(...).

    (That may not apply to what you've written but the Lisp version
    uses the 'options' approach.)

    In general, the fewer methods - the better, IMO.

    I wonder if red= , green=, blue= could be improved by, say:

    rgb = (nil, 45, nil) # ?
    rgb = (-1, 45, -1) # ?

    - presumably, setting more than one gives a gain and it's fairly
    readable.
    No? - OK, I'm nowhere near as close to this as you are ;-)

    Alternatives are well worth exploring to squeeze every ounce of
    usability out of the API design.
    (Reduces the number of moaners later, also, I expect :)


    But, hey - Thanks !!
    I hope to acquire the skills to use this soon :))


    daz
    daz, Nov 5, 2005
    #7
  8. Rob Leslie

    Lionel Thiry Guest

    Yukihiro Matsumoto a écrit :
    > Hi,
    >
    > In message "Re: [ANN] Ruby/GD2 alternative to Ruby/GD"
    > on Sat, 5 Nov 2005 23:08:32 +0900, Rob Leslie <> writes:
    >
    > |> I don't think resize! and find! do not require bang sign in the
    > |> name. Note that, unlike Scheme, bang sign indicates "more
    > |> dangerous" alternatives to non bang ones in Ruby, for example, sub
    > |> (returns modified copy of the receiver) and sub! (modifies the
    > |> receiver in place).
    > |
    > |Actually, I was conscious of this; the non-bang Image#resize does not
    > |modify the receiver but returns a new image instead. Also,
    > |Palette#find never modifies the palette, but Palette#find! allocates
    > |the color if it doesn't already exist.
    >
    > Hmm, I should have checked the API before making comments, sorry. But
    > I still feel something weird with find!, but I can't express myself well.
    >
    > matz.


    find usually takes a block, doesn't it?

    Perhaps Palette#find should be Palette#include? and Palette#find! should
    be Palette#add! ? Doesn't it make sense?

    --
    Lionel Thiry

    Personal web site: http://users.skynet.be/lthiry/
    Lionel Thiry, Nov 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Leslie

    [ANN] Ruby/GD2 1.0

    Rob Leslie, Nov 15, 2005, in forum: Ruby
    Replies:
    5
    Views:
    114
    Tom Copeland
    Nov 15, 2005
  2. Chris Webb

    GD2

    Chris Webb, Feb 1, 2006, in forum: Ruby
    Replies:
    0
    Views:
    84
    Chris Webb
    Feb 1, 2006
  3. Cheshire Cat

    500 errors with GD2 image generation

    Cheshire Cat, Jun 1, 2006, in forum: Ruby
    Replies:
    0
    Views:
    97
    Cheshire Cat
    Jun 1, 2006
  4. Jonathan Métillon
    Replies:
    0
    Views:
    117
    Jonathan Métillon
    Jun 19, 2008
  5. yahata hisashi

    Does Ruby/GD2 work on ruby-mswin32?

    yahata hisashi, Sep 15, 2008, in forum: Ruby
    Replies:
    0
    Views:
    112
    yahata hisashi
    Sep 15, 2008
Loading...

Share This Page