5,661,954 members and growing! (14,275 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate License: The GNU General Public License (GPL)

A C++ blob editor in less than 128 lines of code

By Ronounours

A very simple C++ source code showing how to create an interactive 2D graphical blobs editor, using the CImg library.
C++ (VC6, VC7, VC7.1, VC8.0, C++), C, Windows (Windows, NT4, Win2K, WinXP, Win2003, Vista), Win32, Dev

Posted: 7 May 2008
Updated: 7 May 2008
Views: 6,787
Bookmarked: 10 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.65 Rating: 4.06 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
4 votes, 28.6%
3
1 vote, 7.1%
4
9 votes, 64.3%
5

CImg_blob_editor

Introduction

This article provides a very small piece of C++ code (less than 128 lines) that creates a simple but functional 2D blobs editor.

The editor consists of a main window where the user can create, delete, and move blobs having random sizes and colors. The underlying algorithm is based on implicit functions to create and merge blobs together. This is very similar to the famous Metaballs technique, but here, applied on 2D functions.

A video of this proposed blobs editor can be seen here.

Background

This project illustrates the use of a very handy and simple image class provided by the CImg Library, a C++ template image processing toolkit. It allows to shorten the code by focusing on the algorithm itself, instead of having to do all the low-level image stuffs.

CImg is a very portable library, so I've been able to provide both Windows and Linux executables of the same program in the above archive.

Using the code

The code is quite small, and mainly consists of a main event loop, waiting for user interaction. When the user clicks the mouse with the left button, it creates a blob (or moves an existing one). Using the right mouse button deletes a blob. The colors and sizes of the created blobs are randomly chosen.

The code uses the main image structure of the CImg library, which is, in our case, a CImg<unsigned int>, i.e., a 2D container of pixels of type unsigned int. Pixel access and modification are classically done through the function CImg<T>::operator(), which is provided with the image class.

The blob positions, sizes, and colors are stored in lists structures, also provided by the CImg library (although, it would be possible to use std::vector<> instead).

Finally, the display window is created and updated using the CImgDisplay class of the CImg library, which is great since it is simple to use and multi-platform, so the same code will work the same way on different architectures.

From a mathematical point of view, each blob is considered as an implicit Gaussian function. All the blobs are added into a potential field (here, described by the main image img). Then, this potential field is colored and threshold-ed according to the blob colors and sizes. As a result, a color version of the potential field that contains the blobs is created. Additionally, small reflections are drawn with successive spheres located relative to the blob coordinates, in order to create the final rendered blob image.

Points of interest

I think this code is a nice way to discover the use of implicit functions to model simple mathematical objects. The same kind of techniques are applied in 3D rendering for doing metaballs, and is heavily used nowadays to model smooth 3D objects.

From a more technical point of view, it may make you discover the CImg library, which is a nice C++ toolkit for doing generic image processing. It really simplifies the coding of algorithms related to images, as well as provides a lightweight and multi-platform development framework for image/signal processing.

I really hope you'll enjoy this piece of code.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPL)

About the Author

Ronounours



Location: France France

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralTres JoliememberMember 364564817:06 23 Aug '08  
Generalsome minor issuessupporterpeterchen1:04 7 May '08  
GeneralRe: some minor issuesmemberRonounours1:54 7 May '08  
GeneralRe: some minor issuesmemberGuillaume Geffard2:56 7 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 May 2008
Editor: Smitha Vijayan
Copyright 2008 by Ronounours
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project