RSS/Atom feed Twitter
Site is read-only, email is disabled

Drawing a line in a GIMP plugin

This discussion is connected to the gimp-developer-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.

This is a read-only list on gimpusers.com so this discussion thread is read-only, too.

15 of 15 messages available
Toggle history

Please log in to manage your subscriptions.

Drawing a line in a GIMP plugin Adam C Powell IV 29 May 23:51
  Drawing a line in a GIMP plugin Sven Neumann 29 May 23:59
   Drawing a line in a GIMP plugin Adam C Powell IV 30 May 01:41
    Drawing a line in a GIMP plugin Ryan Krauss 30 May 03:22
    Drawing a line in a GIMP plugin Sven Neumann 30 May 21:58
    Drawing a line in a GIMP plugin Rob Antonishen 31 May 01:20
     Drawing a line in a GIMP plugin Adam C Powell IV 31 May 02:47
      Drawing a line in a GIMP plugin David Hodson 31 May 03:50
       Drawing a line in a GIMP plugin Adam C Powell IV 03 Jun 18:29
      Drawing a line in a GIMP plugin Rob Antonishen 31 May 03:56
       Drawing a line in a GIMP plugin Adam C Powell IV 31 May 21:35
        Drawing a line in a GIMP plugin Liam R E Quin 01 Jun 05:13
         Drawing a line in a GIMP plugin Adam C Powell IV 02 Jun 00:51
      Drawing a line in a GIMP plugin Sven Neumann 31 May 08:39
       Drawing a line in a GIMP plugin Sven Neumann 31 May 08:45
Adam C Powell IV
2009-05-29 23:51:42 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Hi,

I'm a relative newbie trying to have a GIMP plugin draw a line (among other things).

I found gdk_draw_line which seems to be what I want. But I don't know what to do about drawable. I of course have the GimpDrawable, and I have a guint8 array which holds the data from the GimpDrawable:

gint32 drawable_ID; int x1, y1, x2, y2;
guint8 *source_data;
GimpDrawable *drawable;
GimpPixelRgn source_region;

if (!(drawable = (GimpDrawable *) gimp_drawable_get (drawable_ID))) return FALSE;
has_selection = gimp_drawable_mask_bounds (drawable_ID, &x1, &y1, &x2, &y2);

if (!(source_data = g_new (guchar, drawable->bpp * (x2-x1) * (y2-y1)))) return FALSE;
gimp_pixel_rgn_init (&source_region, drawable, x1, y1, x2-x1, y2-y1, FALSE, FALSE);
gimp_pixel_rgn_get_rect (&source_region, source_data, x1, y1, x2-x1, y2-y1);

But I don't see how to get or make a GdkDrawable which I can pass to gdk_draw_line. Do I need to create a GdkPixmap from source_data, and draw into that?

Thanks,
-Adam

Sven Neumann
2009-05-29 23:59:14 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Hi,

On Fri, 2009-05-29 at 17:51 -0400, Adam C Powell IV wrote:

I'm a relative newbie trying to have a GIMP plugin draw a line (among other things).

I found gdk_draw_line which seems to be what I want.

It is not what you want. GDK is a drawing toolkit used by GTK+ for drawing user interfaces. It is not meant and not suited for drawing to GIMP drawables.

You probably want to use the procedures gimp-paint or gimp-pencil that allow you to draw strokes using the paintbrush or pencil tools.

Sven

Adam C Powell IV
2009-05-30 01:41:45 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

On Sat, 2009-05-30 at 00:00 +0200, Sven Neumann wrote:

Hi,

On Fri, 2009-05-29 at 17:51 -0400, Adam C Powell IV wrote:

I'm a relative newbie trying to have a GIMP plugin draw a line (among other things).

I found gdk_draw_line which seems to be what I want.

It is not what you want. GDK is a drawing toolkit used by GTK+ for drawing user interfaces. It is not meant and not suited for drawing to GIMP drawables.

You probably want to use the procedures gimp-paint or gimp-pencil that allow you to draw strokes using the paintbrush or pencil tools.

I see. I was hoping to avoid the complexity of choosing brushes and whatnot, and just draw a simple line with squared-off end...

But okay. A search for "gimp plugin draw line" doesn't turn up anything looking helpful. Can you point me to an object or method to get started?

Thanks,
-Adam

Ryan Krauss
2009-05-30 03:22:12 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

The procedure browser is your best bet (should be under the help menu). gimp-pencil and gimp-paintbrush look the ones.

On Fri, May 29, 2009 at 6:41 PM, Adam C Powell IV wrote:

On Sat, 2009-05-30 at 00:00 +0200, Sven Neumann wrote:

Hi,

On Fri, 2009-05-29 at 17:51 -0400, Adam C Powell IV wrote:

I'm a relative newbie trying to have a GIMP plugin draw a line (among other things).

I found gdk_draw_line which seems to be what I want.

It is not what you want. GDK is a drawing toolkit used by GTK+ for drawing user interfaces. It is not meant and not suited for drawing to GIMP drawables.

You probably want to use the procedures gimp-paint or gimp-pencil that allow you to draw strokes using the paintbrush or pencil tools.

I see. I was hoping to avoid the complexity of choosing brushes and whatnot, and just draw a simple line with squared-off end...

But okay. A search for "gimp plugin draw line" doesn't turn up anything looking helpful. Can you point me to an object or method to get started?

Thanks,
-Adam
--
GPG fingerprint: D54D 1AEE B11C CE9B A02B C5DD 526F 01E8 564E E4B6

Engineering consulting with open source tools http://www.opennovation.com/

Sven Neumann
2009-05-30 21:58:14 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Hi,

On Fri, 2009-05-29 at 19:41 -0400, Adam C Powell IV wrote:

I see. I was hoping to avoid the complexity of choosing brushes and whatnot, and just draw a simple line with squared-off end...

Of course you are free to implement your own line drawing algorithm or to pull in another library to do this. You would then manipulate the drawable pixels directly.

Sven

Rob Antonishen
2009-05-31 01:20:06 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Here is the code to draw a single pixel line, black on white, on the drawable "layer" and x1 y1 are one end and x2 y2 are the other.

(gimp-context-set-brush "Circle (01)") (gimp-context-set-foreground "Black") (gimp-context-set-background "White") (gimp-context-set-opacity 100)
(gimp-context-set-paint-mode NORMAL-MODE) (gimp-pencil layer 4 (vector x1 y1 x2 x2)) (gimp-displays-flush)

0,0 is upper left hand side. This is with the pencil tool. Replace with any other tool as desired, for example: (gimp-paintbrush-default layer 4 (vector x1 y1 x2 y2))

Hope that helps,

-Rob A>

Adam C Powell IV
2009-05-31 02:47:52 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

Funny, a search for "gimp c api line" turns up this thread. :-) Going further, searching for "gimp plugin api draw line" and restricting to developer.gimp.org turns up GDK drawing primitives.

So it sounds like the best way to proceed is to wrap the pixel data into a GdkPixbuf and use GDK to draw into it. Figured there might be a more straightforward way to go from a GimpDrawable to a GdkDrawable (since it is after all the "GIMP Drawing Kit"). Hmm, and there's a GimpCanvas object, but that doesn't seem to be a plugin thing.

Thanks, Adam

On Sat, 2009-05-30 at 19:20 -0400, Rob Antonishen wrote:

Here is the code to draw a single pixel line, black on white, on the drawable "layer" and x1 y1 are one end and x2 y2 are the other.

(gimp-context-set-brush "Circle (01)") (gimp-context-set-foreground "Black") (gimp-context-set-background "White") (gimp-context-set-opacity 100)
(gimp-context-set-paint-mode NORMAL-MODE) (gimp-pencil layer 4 (vector x1 y1 x2 x2)) (gimp-displays-flush)

0,0 is upper left hand side. This is with the pencil tool. Replace with any other tool as desired, for example: (gimp-paintbrush-default layer 4 (vector x1 y1 x2 y2))

Hope that helps,

-Rob A>

David Hodson
2009-05-31 03:50:30 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

On Sat, 2009-05-30 at 20:47 -0400, Adam C Powell IV wrote:

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

Open Gimp
Select Help > Procedure Browser
In the search box, type "brush"
Click on "gimp-context-set-brush"
Read the documentation
In your plugin, add the line:

gboolean result = gimp_context_set_brush("Circle (01)");

Rinse, lather, repeat.

(The hardest part is changing the dashes in the name to underscores.)

--David

Rob Antonishen
2009-05-31 03:56:21 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Sure, but the call process should be the same. Just use the equivalent C API calls, no?

-Rob A>

On 5/30/09, Adam C Powell IV wrote:

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

Funny, a search for "gimp c api line" turns up this thread. :-) Going further, searching for "gimp plugin api draw line" and restricting to developer.gimp.org turns up GDK drawing primitives.

So it sounds like the best way to proceed is to wrap the pixel data into a GdkPixbuf and use GDK to draw into it. Figured there might be a more straightforward way to go from a GimpDrawable to a GdkDrawable (since it is after all the "GIMP Drawing Kit"). Hmm, and there's a GimpCanvas object, but that doesn't seem to be a plugin thing.

Thanks, Adam

On Sat, 2009-05-30 at 19:20 -0400, Rob Antonishen wrote:

Here is the code to draw a single pixel line, black on white, on the drawable "layer" and x1 y1 are one end and x2 y2 are the other.

(gimp-context-set-brush "Circle (01)") (gimp-context-set-foreground "Black") (gimp-context-set-background "White") (gimp-context-set-opacity 100)
(gimp-context-set-paint-mode NORMAL-MODE) (gimp-pencil layer 4 (vector x1 y1 x2 x2)) (gimp-displays-flush)

0,0 is upper left hand side. This is with the pencil tool. Replace with any other tool as desired, for example: (gimp-paintbrush-default layer 4 (vector x1 y1 x2 y2))

Hope that helps,

-Rob A>

--
GPG fingerprint: D54D 1AEE B11C CE9B A02B C5DD 526F 01E8 564E E4B6

Engineering consulting with open source tools http://www.opennovation.com/

Sven Neumann
2009-05-31 08:39:04 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

On Sat, 2009-05-30 at 20:47 -0400, Adam C Powell IV wrote:

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

http://developer.gimp.org/api/2.0/libgimp/index.html

Sven

Sven Neumann
2009-05-31 08:45:16 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

On Sun, 2009-05-31 at 08:40 +0200, Sven Neumann wrote:

On Sat, 2009-05-30 at 20:47 -0400, Adam C Powell IV wrote:

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

http://developer.gimp.org/api/2.0/libgimp/index.html

Actually, you may find it more convenient to make sure that the GIMP API reference manual is installed locally and then use devhelp to access it. devhelp provides search functionality and many editors (such as emacs for example) integrate it so that you can easily get to the documentation from your source code.

On a Debian or Ubuntu system you would install the packages devhelp and libgimp2.0-doc.

Sven

PS: I am somewhat surprised that this needs to be explained on a developer list, but perhaps it is good to post an explanation like this to the mailing-list from time to time so that the search engines pick it up...

Adam C Powell IV
2009-05-31 21:35:27 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Indeed. So where is the C API documentation for drawing lines? When I search, all that comes up is one C paintbrush function, the Scheme interface, and this email thread.

I've spent a bunch of time at http://developer.gimp.org/api/2.0/libgimp/ to no avail... (That's a lot of how I produced the code snippet in the message which started this thread.)

Ah, a grep search through /usr/include/gimp-2.0/libgimp turned up gimpvectors, with a bezier line method. Why didn't I see it earlier? Ah, on developer.gimp.org there's no text next to the object name, no Description, no Details, no indication that this is how one draws lines.

So I guess I create a gimpvectors object somehow, then use gimp_vectors_bezier_stroke_new_moveto() and gimp_vectors_bezier_stroke_lineto()? Or should I gimp_vectors_stroke_new_from_points()? How do I "stroke" the resulting path?

Sven, just got your replies, thanks. Still scratching my head. I'll bang on it for a bit longer, and go back to Gdk if I can't make it work.

Thanks, Adam

On Sat, 2009-05-30 at 21:56 -0400, Rob Antonishen wrote:

Sure, but the call process should be the same. Just use the equivalent C API calls, no?

-Rob A>

On 5/30/09, Adam C Powell IV wrote:

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

Funny, a search for "gimp c api line" turns up this thread. :-) Going further, searching for "gimp plugin api draw line" and restricting to developer.gimp.org turns up GDK drawing primitives.

So it sounds like the best way to proceed is to wrap the pixel data into a GdkPixbuf and use GDK to draw into it. Figured there might be a more straightforward way to go from a GimpDrawable to a GdkDrawable (since it is after all the "GIMP Drawing Kit"). Hmm, and there's a GimpCanvas object, but that doesn't seem to be a plugin thing.

Thanks, Adam

On Sat, 2009-05-30 at 19:20 -0400, Rob Antonishen wrote:

Here is the code to draw a single pixel line, black on white, on the drawable "layer" and x1 y1 are one end and x2 y2 are the other.

(gimp-context-set-brush "Circle (01)") (gimp-context-set-foreground "Black") (gimp-context-set-background "White") (gimp-context-set-opacity 100)
(gimp-context-set-paint-mode NORMAL-MODE) (gimp-pencil layer 4 (vector x1 y1 x2 x2)) (gimp-displays-flush)

0,0 is upper left hand side. This is with the pencil tool. Replace with any other tool as desired, for example: (gimp-paintbrush-default layer 4 (vector x1 y1 x2 y2))

Hope that helps,

-Rob A>

--
GPG fingerprint: D54D 1AEE B11C CE9B A02B C5DD 526F 01E8 564E E4B6

Engineering consulting with open source tools http://www.opennovation.com/

-Adam

Liam R E Quin
2009-06-01 05:13:49 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

On Sun, 2009-05-31 at 15:35 -0400, Adam C Powell IV wrote:

Indeed. So where is the C API documentation for drawing lines?

At the risk of stating the obvious, you do realise, I hope, that GIMP is a bitmap editor, and not a drawing program? If you are trying to create vector art, e.g. SVG or EPS, Inkscape or librsvg are better things to investigate.

There's also the Gfig plugin (filters->render->Gfig) that does some drawing and might be a source of ideas.

Liam

Adam C Powell IV
2009-06-02 00:51:26 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

On Sun, 2009-05-31 at 23:13 -0400, Liam R E Quin wrote:

On Sun, 2009-05-31 at 15:35 -0400, Adam C Powell IV wrote:

Indeed. So where is the C API documentation for drawing lines?

At the risk of stating the obvious, you do realise, I hope, that GIMP is a bitmap editor, and not a drawing program? If you are trying to create vector art, e.g. SVG or EPS, Inkscape or librsvg are better things to investigate.

Yes, thanks. In this regard, GIMP doesn't differ from Gdk -- which has a well-exposed API for drawing lines, and not just horizontal or vertical ones.

I'm making an image processing plugin which uses lines over image features to visualize some of the output. I should be able to release it soon.

There's also the Gfig plugin (filters->render->Gfig) that does some drawing and might be a source of ideas.

Ooh, very cool. Thanks!

-Adam

Adam C Powell IV
2009-06-03 18:29:57 UTC (over 15 years ago)

Drawing a line in a GIMP plugin

Okay, one more bit of help should finish this. :-)

On Sun, 2009-05-31 at 11:50 +1000, David Hodson wrote:

On Sat, 2009-05-30 at 20:47 -0400, Adam C Powell IV wrote:

I'm sorry, I'm developing a C plugin (tried Scheme first and it was DOG slow). Is there a C equivalent? All I'm seeing is gimp-paintbrush-register.

Open Gimp
Select Help > Procedure Browser
In the search box, type "brush"
Click on "gimp-context-set-brush"
Read the documentation
In your plugin, add the line:

gboolean result = gimp_context_set_brush("Circle (01)");

Rinse, lather, repeat.

(The hardest part is changing the dashes in the name to underscores.)

Thanks very much, this and Rob's sequence got me almost there. And apologies to Rob, Liam and Sven for missing this post on Scheme -> C procedure names until after my Sunday replies.

But using gimp_pencil (drawable_ID, etc.) in C only draws on the main window. I want it to draw on the preview or window as appropriate.

Here's my function in a nutshell:

if (!(drawable = (GimpDrawable *) gimp_drawable_get (drawable_ID))) return FALSE;
has_selection = gimp_drawable_mask_bounds (drawable_ID, &x1, &y1, &x2, &y2);

if (!(source_data = g_new (guchar, drawable->bpp * (x2-x1) * (y2-y1)))) return FALSE;
gimp_pixel_rgn_init (&source_region, drawable, x1, y1, x2-x1, y2-y1, FALSE, FALSE);
gimp_pixel_rgn_get_rect (&source_region, source_data, x1, y1, x2-x1, y2-y1);

// Do some stuff to source_data

gimp_pixel_rgn_init (&output_region, drawable, x1, y1, x2-x1, y2-y1, (preview == NULL), TRUE);

gimp_pixel_rgn_set_rect (&output_region, source_data + y1*rowskip, 0, y1, full_width, y2-y1);

// I want to draw the lines here

if (preview) gimp_drawable_preview_draw_region (GIMP_DRAWABLE_PREVIEW (preview), &output_region);
else
{
gimp_drawable_flush (drawable); gimp_drawable_merge_shadow (bs_param->drawable_ID, TRUE); gimp_drawable_update (bs_param->drawable_ID, x1, y1, win_width, win_height); }

This is called by both the "invalidated" callback of the preview in the dialog, and also the run() method of the plugin after "OK".

The first non-lines part works fine: the changes to source_data get merged into the preview when it's running, and into the original image when I click "OK".

However, it draws the lines on the original image when the preview is running in the dialog. So that messes up the image analysis (Do some stuff... above) when that runs at the end of run(). And gimp_pixel_rgn_set_rect overwrites the lines, so they're all gone.

How can I get this to behave like gimp_pixel_rgn_set_rect , i.e. draw on the preview in preview mode, and draw on the original window in run() ? Can I draw the lines into the output_region somehow?

-Adam