eMage - Computer Graphics Algorithms and other Ideas


Links
Home
Open Bitmaps with Turbo-C++
Mouse Events Tutorial - 1
Mouse Events Tutorial - 2
Mouse Events Tutorial - 3
Algorithms
Feedback

Support writeKA


Web writeka.com

writeKA Language Tools

Buy books at Amazon.com

Valid XHTML 1.0 Strict
Valid CSS

First uploaded on 08-Mar-2003.
Standardised html and moved to new domain on 04-Mar-2006.

Filling color into a closed space
I developed a new filling algorithm for eMage. The filling takes immensely long - up to 30 seconds is normal. The delay is caused by the fact that I've employed swap files instead of putting the bitmap in memory and not the algorithm itself. The algorithm I've used lets me pattern fill with minor modification. Assume you have got the entire bitmap in memory row after row and each pixel is represented by 8 bits. Yes I am being wasteful here - I've used 8 bits instead of 4. This gives me around 240 values per pixel which have not been used, which means that I can use them to store special information.

The wasteful storage format (it takes double the actual amount of space required) makes the filling algorithm relatively easy. When I click on a point demanding a fill operation, I wish to convert all pixels of the same color in the region common to the point where I clicked to be converted to the fill color. So whaddoi do? I just mark the pixel clicked with one of the unused numbers say 18. Next I check the pixels up, down left and right whether they are of the same color as the original point clicked. If they are, then I mark them with a new number say, 17. Next I scan the entire bitmap for all pixels with value 17. If any are found, then I mark them as 18 and check if the pixels up, down, left or right are the same color as the original point clicked. If so mark them as 17 ... after all that's done scan again and repeat the procedure. Do it till there is no change noted.

Then convert all the points marked as 18 to the desired color or draw the suitable pattern over the 18-marked areas. Might seem wasteful to you. But its pretty fast if done with the bitmap stored in main memory. If you got better solutions, do tell me.

Drawing Spirals
I had to hunt around quite a bit for this one. Its pretty easy once you see it though. You just draw line segments in increasing inclinations away from the centre of the spiral.

Drawing Lines, Circles and Ellipses
The following functions use standard algorithms to draw circles and ellipses. I've modified them from my Canvas library - didn't get time to test it after that. They originally used different functions for graphical o/p so just watch out for errors. Tell me if you find errors.

Freehand Drawing
Instead of drawing pixels whenever you detect a mouse down at freehand draw mode you might want to draw lines instead. This gives smoother output. Just remember the previous point detected and draw a line to the new point detected. Then store the new point so that you can draw a line from there when another point is detected.

Rubber band Technique - The easy way
Rubber banding is what you see when you move your mouse around while keeping the button pressed down to draw lines, circles etc. You see that lines, circles etc. are drawn and can be moved around until you reach your desired figure. This can be done easily by inverting the background pixels and then inverting them back again as you move around. The inverting technique ensures that you don't have to refresh the entire canvas each time you move the mouse while drawing the line, circle etc. An easy way to do this is encapsulating the putpixel() function as below:


This means that passing color value as 16, puts a pixel which is the inverse of the original. Inverting twice gives the original color. You could modify the functions to draw line, circle etc. to use setpixel() instead of putpixel(). This means that you can easily draw these figures showing the inverse of the background. Repeating the inverse function to draw the same figure would return the Canvas to the original state. No cleaning! No refreshing!