Click here to Skip to main content
12,955,493 members (62,209 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hey I am trying to render some fonts on the screen. Actually I have done that.
Now what I am trying is to fill the characters i.e. fill the bounded regions.

The only thing I have is a 2-d buffer which stores information for each pixel i.e. it is set if the pixel is to be illuminated otherwise it contains '0'.

I cant think of a logic to fill the bounded regions using this.
This is what I tried but it failed as it cant recognize the horizontal lines.

for(int p=0 ; p<401 ; ++p){
            for(int q=0 ; q<401 ; ++q){
                if( buffer[p][q] == 1 ){
                    ++q ;
                    while (buffer[p][q] == 0){
                          buffer[p][q]  = 1 ;
                          ++q ;
Posted 15-Jul-11 1:37am
Updated 15-Jul-11 2:20am
R. Erasmus 15-Jul-11 9:53am
This piece of code is prone to having bugs....
incrementing q will cause array out of bounds exception when q = 400 if buffer size contains 401 elements.
Loop is also way to complex... Its better to use functions that devides the program up into simpler segments. Its a testing nightmare.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Why don't you have a look at Flood Fill algorithm[^] at Wikipedia?
Espen Harlinn 15-Jul-11 8:13am
Right :)
Member 8086596 15-Jul-11 8:17am
That's entirely different thing i guess. I don't have any idea about the behaviour of the edges or you can say the bounded regions. I only have the pixel info in the 2-d buffer , but for standard flood fill algorithm i need to know the coordinates of corners.
CPallini 15-Jul-11 8:23am
I guess your guess is wrong. Please read carefully the linked page.
Member 8086596 15-Jul-11 8:28am
Suppose i have rendered "ABC" on the canvas. Firstly i dont have start node to start with , secondly, even if I get the start node by scanning the buffer[][] that would become very inefficient in case the text is lenghty coz i have to find the start node for each and every character.
CPallini 15-Jul-11 8:34am
Could you please post one (or more) image(s)?
Member 8086596 15-Jul-11 8:37am
Sorry I am new to this forums world . How am I supposed to post images?
CPallini 15-Jul-11 8:44am
You may upload the image (on a free image hosting service) and then post the link here.
BTW cannot you use different colors for the initiali printout of the characters (I mean, for instance, given "AKA", 'A'<->1, 'K'<->2, 'A'<->3 , this way internal pixels are bounded by external pixels having the same value)?
Member 8086596 15-Jul-11 8:55am
I can do that I guess. But hows that gonna help ? I still will have to find the Start node for each character.
Actually I am rendering the fonts from SVG file on the mobile device. So i have to make it as efficient as possible. So I am looking for the way in which i can fill all the bounded regions by just scanning the buffer[][] once.
CPallini 15-Jul-11 9:00am
It helps because, otherwise, you cannot recognize horizontal lines (if I got you: you cannot distingush 'inside' from 'outside'. Having different colors helps: a blank pixel between a 1-pixel and a 2-pixel is 'outside').
Member 8086596 15-Jul-11 9:26am
here is the image

I need to fill the "BOTS".
Member 8086596 15-Jul-11 8:56am
I dont have access to the image now. Will do that later.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Search for "fill polygon" or "scanline fill algorithm".

It is a scan-line problem. If you can draw the font outline, then you can also store the points in a Y/X sorted list. If you did not draw the outline, then you can scan it to create the list.

[y1][x1], [y1][x2], [y1][x3], [y1][x4], ...
[y2][x1], [y2][x2], [y2][x3], [y2][x4]...

x1 => far left
x2 => first right
x3 => next left
x4 => next right

[y1][x1] line to [y1][x2] no line [y1][x3] line to [y1][x4]

I just wrote the following - so it has not been tested. But it should give you an idea of how a scan-line fill works.

struct MyPoint {
	int x, y;
// Fill horizontal lines between points in given point list
// - does not draw on outline (border)
void FillBetweens(MyPoint* list, size_t list_size)
	// assumes list is sorted from top to bottom and left to right
	int current_y = 0;
	for (size_t i=0; i<list_size-1; ++i)
		if( list[i].y != current_y )
			current_y = list[i].y; // or ++current_y (depends on your choices)
		if( list[i+1].y == current_y )
			// your horizontal line drawing function (y,x1,x2)
			// - assumes drawing between end points
			draw_betweens(list[i].y, list[i].x, list[i+1].x);
		// else we skip single point lines
Member 8086596 15-Jul-11 13:28pm
that means for characters like "C" which have a curve I need to get the cordinates corresponding to each pixel.
John R. Shaw 15-Jul-11 13:58pm
Basically - you do need to create a scan-line list to use the normal method - which relies on knowing the coordinates of the edges.

Example line to fill:
[0][0] = 1, ..., [0][10] = 1, [0][11] = 1, [0][12] = 1

in MyPoint list that would be
list[0] = {0,0}; // left
list[1] = {0,10}; // right

In this case we do not want [0][11] or [0][12] in our list.

The method you are currently using would work, but resembles a flood fill. May be a look at the diagrams in my QuickFill article at codeproject would give you a better idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,719
CHill60 3,550
Maciej Los 3,153
ppolymorphe 2,060
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 15 Jul 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100