views:

155

answers:

3

I want to write a paint program in the style of MS Paint.

For painting things on screen when the user moves the mouse, I have to wait for mouse move events and draw on the screen whenever I receive one. Apparently, mose move events are not sent very often, so I have to interpolate the mouse movement by drawing a line between the current mouse position and the previous one. In pseudocode, this looks something like this:

var positionOld = null

def handleMouseMove(positionNew):
    if mouse.button.down:
        if positionOld == null:
            positionOld = positionNew
        screen.draw.line(positionOld,positionNew)
        positionOld = positionNew

Now my question: interpolating with straight line segments looks too jagged for my taste, can you recommend a better interpolation method? What method do GIMP or Adobe Photoshop implement?

Alternatively, is there a way to increase the frequency of the mouse move events that I receive? The GUI framework I'm using is wxWidgets.

GUI framework: wxWidgets.
(Programming language: Haskell, but that's irrelevant here)

EDIT: Clarification: I want something that looks smoother than straight line segments, see the picture (original size):

jagged lines drawn between mouse positions

EDIT2: The code I'm using looks like this:

-- create bitmap and derive drawing context
im      <- imageCreateSized (sy 800 600)
bitmap  <- bitmapCreateFromImage im (-1)    -- wxBitmap
dc      <- memoryDCCreate                   -- wxMemoryDC
memoryDCSelectObject dc bitmap

...
-- handle mouse move
onMouse ... sw (MouseLeftDrag posNew _) = do
    ...
    line dc posOld posNew [color     := white
                          , penJoin  := JoinRound
                          , penWidth := 2]
    repaint sw                              -- a wxScrolledWindow

-- handle paint event
onPaint ... = do
    ...
    -- draw bitmap on the wxScrolledWindow
    drawBitmap dc_sw bitmap pointZero False []

which might make a difference. Maybe my choices of wx-classes is why I'm getting a rather low frequency of mouse move events.

+2  A: 

I think you need to look into the Device Context documentation for wxWidgets.

I have some code that draws like this:

//screenArea is a wxStaticBitmap
int startx, starty;
void OnMouseDown(wxMouseEvent& event)
{
    screenArea->CaptureMouse();
    xstart = event.GetX();
    ystart = event.GetY();
    event.Skip();
}
void OnMouseMove(wxMouseEvent& event)
{
    if(event.Dragging() && event.LeftIsDown())
    {
        wxClientDC dc(screenArea);
        dc.SetPen(*wxBLACK_PEN);
        dc.DrawLine(startx, starty, event.GetX(), event.GetY());
    }
    startx = event.GetX();
    starty = event.GetY();
    event.Skip();
}

I know it's C++ but you said the language was irrelevant, so I hope it helps anyway.

This lets me do this:

alt text

which seems significantly smoother than your example.

Evan Cordell
Thanks for your help, but I would like to draw something that's smoother than lines. Either that or have the mouse move events appear more often.
Heinrich Apfelmus
Perhaps I'm misunderstanding; this is pretty smooth. See this image: http://farm5.static.flickr.com/4108/4837817079_a0c191586d.jpg for an example (I used the code above). What do you mean by smoother? Wider and anti-aliased?
Evan Cordell
Ah, ok, that looks smooth, i.e. the line segments are not visible. It appears you're getting more mouse move events, probably because I'm using a different setup. Edited my question. It appears that `wxStaticBitmap` has a maximum size of 64x64; do you have another recommendation for the screen area?
Heinrich Apfelmus
The 64x64 restriction only applies to Windows 9x, which I assumed you weren't targeting. wxStaticBitmap should be fine for pretty much anything.However, if you want something really efficient, the best way to manipulate image data in wxWidgets that I've found is to store an array of bytes (I use unsigned char in c++), where each byte is a the Red, Green, or Blue value for a point. Then you can load that into a wxImage and display in whatever container you want. I know that wasn't an excellent explanation; just let me know if you want to go that route and I'll add an answer explaining it.
Evan Cordell
I'm currently storing the drawing in a wxBitmap and copying that into a wxScrolledWindow every now and then. From your comment, I gather that this approach is reasonably efficient, thanks!
Heinrich Apfelmus
On the matter of smoothness, the lines segments turn up when the mouse is moved very rapidly. If I move my mouse fast enough, I get them to show up in the GIMP, too. So, using lines for interpolation is standard, it's just that wxWidgets doesn't fire mouse move events fast enough for me. I estimate that it fires a mouse move event every 50ms. Is there a straightforward way to lower that number to say 20ms?
Heinrich Apfelmus
I haven't timed wxWidgets to see what performance I get, but I'd guess that I get closer to 20ms than 50ms. The issue might be some bottleneck that occurs going wxWidgets->Haskell? Unless you're on older hardware, I'd imagine that's the case.
Evan Cordell
I'm not sure. Evan, could you post the code you already have as an answer to my [new question](http://stackoverflow.com/questions/3484058/example-code-for-a-paint-program-a-la-ms-paint) that asks for example code?
Heinrich Apfelmus
+2  A: 

You can make them really smooth using splines: http://freespace.virgin.net/hugo.elias/graphics/x_bezier.htm

But you'll have to delay the drawing of each line segment until one frame later, so that you have the start and end points, plus the next and previous points available for the calculation.

Rocketmagnet
Nice link about splines, but it only defines splines in terms of two points and two directions. Do you have details on how to create a spline segment given three or four points on the spline? After all, all I'm receiving is a list of mouse positions.
Heinrich Apfelmus
Sure, you just define the direction for each point to be parallel to the line between the previous and next points. For the first and last points in the sequence, just set the directions to be the zero vector.
Rocketmagnet
A: 

Interpolating mouse movements with line segments is fine, GIMP does it that way, too, as the following screenshot from a very fast mouse movement shows:

GIMP uses line segments, too

So, smoothness comes from a high frequency of mouse move events. WxWidgets can do that, as the example code for a related question demonstrates.

The problem is in your code, Heinrich. Namely, drawing into a large bitmap first and then copying the whole bitmap to the screen is not cheap! To estimate how efficient you need to be, compare your problem to video games: a smooth rate of 30 mouse move events per second correspond to 30fps. Copying a double buffer is no problem for modern machines, but WxHaskell is likely not optimized for video games, so it's not surprising that you experience some jitter.

The solution is to draw only as much as necessary, i.e. just the lines, directly on the screen, for example as shown in the link above.

Heinrich Apfelmus
How is the code you posted any different from mine, only without interpolating between dots?
Evan Cordell
My code in the [related question](http://stackoverflow.com/questions/3484058/example-code-for-a-minimal-paint-program-ms-paint-style/3487944#3487944) is probably almost the same as yours. (I'd accept yours if you post it there). My Haskell code in the question here is different in that it copies a 800x600 bitmap to the screen on every mouse move.
Heinrich Apfelmus