Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

There are new messages.


devel / comp.lang.c / Re: filling area by color atack safety

SubjectAuthor
* filling area by color atack safetyfir
+* Re: filling area by color atack safetyMalcolm McLean
|+* Re: filling area by color atack safetyBen Bacarisse
||+* Re: filling area by color atack safetybart
|||+- Re: filling area by color atack safetyBen Bacarisse
|||+* Re: filling area by color atack safetyTim Rentsch
||||`* Re: filling area by color atack safetyMichael S
|||| +- Re: filling area by color atack safetyTim Rentsch
|||| `* Re: filling area by color atack safetyfir
||||  `* Re: filling area by color atack safetybart
||||   +- Re: filling area by color atack safetybart
||||   +- Re: filling area by color atack safetyfir
||||   `- Re: filling area by color atack safetyDavid Brown
|||`- Re: filling area by color atack safetyTim Rentsch
||`* Re: filling area by color atack safetyMalcolm McLean
|| +* Re: filling area by color atack safetyScott Lurndal
|| |+* Re: filling area by color atack safetyMalcolm McLean
|| ||+* Re: filling area by color atack safetyChris M. Thomasson
|| |||`* Re: filling area by color atack safetyChris M. Thomasson
|| ||| `- Re: filling area by color atack safetyMichael S
|| ||`- Re: filling area by color atack safetyfir
|| |`- Re: filling area by color atack safetyMichael S
|| `* Re: filling area by color atack safetyBen Bacarisse
||  `* Re: filling area by color atack safetyMalcolm McLean
||   +- Re: filling area by color atack safetyKaz Kylheku
||   +- Re: filling area by color atack safetyBen Bacarisse
||   `* Re: filling area by color atack safetyScott Lurndal
||    `* Re: filling area by color atack safetyTim Rentsch
||     `* Re: filling area by color atack safetyScott Lurndal
||      `- Re: filling area by color atack safetyTim Rentsch
|+* Re: filling area by color atack safetyDavid Brown
||+* Re: filling area by color atack safetyMalcolm McLean
|||+* Re: filling area by color atack safetyMalcolm McLean
||||+- Re: filling area by color atack safetyMichael S
||||+* Re: filling area by color atack safetyMichael S
|||||+* Re: filling area by color atack safetyMichael S
||||||`* Re: filling area by color atack safetyTim Rentsch
|||||| `* Re: filling area by color atack safetyMalcolm McLean
||||||  `* Re: filling area by color atack safetyTim Rentsch
||||||   `- Re: filling area by color atack safetyMalcolm McLean
|||||`* Re: filling area by color atack safetyfir
||||| `* Re: filling area by color atack safetyMichael S
|||||  `* Re: filling area by color atack safetyfir
|||||   `* Re: filling area by color atack safetyMichael S
|||||    `* Re: filling area by color atack safetyfir
|||||     `* Re: filling area by color atack safetyMichael S
|||||      `- Re: filling area by color atack safetyfir
||||`- Re: filling area by color atack safetyfir
|||`* Re: filling area by color atack safetyDavid Brown
||| `* Re: filling area by color atack safetyMalcolm McLean
|||  `* Re: filling area by color atack safetyDavid Brown
|||   `* Re: filling area by color atack safetyMalcolm McLean
|||    `* Re: filling area by color atack safetyDavid Brown
|||     `* Re: filling area by color atack safetyMalcolm McLean
|||      +* Re: filling area by color atack safetyScott Lurndal
|||      |+- Re: filling area by color atack safetybart
|||      |`* Re: filling area by color atack safetyDavid Brown
|||      | `- Re: filling area by color atack safetyRichard Harnden
|||      +* Re: filling area by color atack safetyKeith Thompson
|||      |+- Keith-world (Was: filling area by color atack safety)Kenny McCormack
|||      |`- Re: filling area by color atack safetyDavid Brown
|||      +- Re: filling area by color atack safetyChris M. Thomasson
|||      `- Re: filling area by color atack safetyDavid Brown
||+* Re: filling area by color atack safetyScott Lurndal
|||`* Re: filling area by color atack safetyBen Bacarisse
||| +* Re: filling area by color atack safetyMalcolm McLean
||| |`- Re: filling area by color atack safetybart
||| `- Re: filling area by color atack safetyDavid Brown
||`* Re: filling area by color atack safetyfir
|| `- Re: filling area by color atack safetyfir
|+* Re: filling area by color atack safetyMichael S
||+* Re: filling area by color atack safetybart
|||`* Re: filling area by color atack safetyMichael S
||| `* Re: filling area by color atack safetybart
|||  +* Re: filling area by color atack safetyMichael S
|||  |`- Re: filling area by color atack safetyDavid Brown
|||  `* Re: filling area by color atack safetyBen Bacarisse
|||   `* Re: filling area by color atack safetySpiros Bousbouras
|||    `* Re: filling area by color atack safetyBen Bacarisse
|||     `- Re: filling area by color atack safetybart
||+* Re: filling area by color atack safetyTim Rentsch
|||`* Re: filling area by color atack safetyMalcolm McLean
||| `* Re: filling area by color atack safetyTim Rentsch
|||  `* Re: filling area by color atack safetyMalcolm McLean
|||   `- Re: filling area by color atack safetyTim Rentsch
||`* Re: filling area by color atack safetyfir
|| `* Re: filling area by color atack safetyMichael S
||  `- Re: filling area by color atack safetyfir
|`* Re: filling area by color atack safetyLew Pitcher
| +- Re: filling area by color atack safetybart
| `* Re: filling area by color atack safetyPeter 'Shaggy' Haywood
|  `- Re: filling area by color atack safetyfir
+* Re: filling area by color atack safetybart
|+* Re: filling area by color atack safetybart
||`- Re: filling area by color atack safetyMalcolm McLean
|`* Re: filling area by color atack safetyfir
| `* Re: filling area by color atack safetyPeter 'Shaggy' Haywood
|  +* Re: filling area by color atack safetyMichael S
|  |`- Re: filling area by color atack safetyMichael S
|  `- Re: filling area by color atack safetyfir
+- Re: filling area by color atack safetyPeter 'Shaggy' Haywood
`* Re: filling area by color atack safetyTim Rentsch

Pages:1234567
Re: filling area by color atack safety

<20240317153203.00006be1@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34451&group=comp.lang.c#34451

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 15:32:03 +0200
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20240317153203.00006be1@yahoo.com>
References: <ut3669$21eur$1@i2pn2.org>
<ut4020$2s8ov$1@dont-email.me>
<87wmq2jn7s.fsf@bsb.me.uk>
<ut4ba7$2uits$1@dont-email.me>
<SqlJN.144025$CYpe.59290@fx40.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="fd62a22e31fcf9828d4eedf77229489a";
logging-data="3728424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IRkS1nrzBIu0qjLEBn4VkiFckfB4Z4cQ="
Cancel-Lock: sha1:oNVPzUZ2FoPoH3U9/aYJHxRL39s=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Sun, 17 Mar 2024 13:32 UTC

On Sat, 16 Mar 2024 18:21:38 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:

> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> >On 16/03/2024 13:55, Ben Bacarisse wrote:
> >> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> >>
> >>> Recursion make programs harder to reason about and prove correct.
> >>>
> >>
> >> Are you prepared to offer any evidence to support this astonishing
> >> statement or can we just assume it's another Malcolmism?
> >>
> >
> >Example given. A recursive algorithm which is hard to reason about
> >and
>
> Perhaps hard for _you_ to reason about. That doesn't
> generalize to every other programmer that might read that
> code.
>
>

As a matter of fact, David Brown was not able to reason about depth of
recursion in fir's code. And you answered David's post without spotting
his mistake.
Now, I don't know if you didn't spot his mistake because you didn't read
this part of his message or because for you too it was hard to reason
about depth of recursion.

Re: filling area by color atack safety

<20240317153742.00002435@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34452&group=comp.lang.c#34452

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 15:37:42 +0200
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <20240317153742.00002435@yahoo.com>
References: <ut3669$21eur$1@i2pn2.org>
<ut4020$2s8ov$1@dont-email.me>
<20240317144625.00002011@yahoo.com>
<ut6p67$3h8t0$2@dont-email.me>
<20240317151519.000057d0@yahoo.com>
<ut6qt8$3hnat$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: dont-email.me; posting-host="fd62a22e31fcf9828d4eedf77229489a";
logging-data="3728424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6bDYbieh3muHvk4JUqbet4zID9IUxUPQ="
Cancel-Lock: sha1:GGOc+8PW61bH+SkLMWplUtc6rAo=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Sun, 17 Mar 2024 13:37 UTC

On Sun, 17 Mar 2024 13:23:55 +0000
bart <bc@freeuk.com> wrote:

> On 17/03/2024 13:15, Michael S wrote:
> > On Sun, 17 Mar 2024 12:54:34 +0000
> > bart <bc@freeuk.com> wrote:
> >
> >> On 17/03/2024 12:46, Michael S wrote:
> >>> On Sat, 16 Mar 2024 11:33:20 +0000
> >>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> >>>
> >>>> On 16/03/2024 04:11, fir wrote:
> >>>>> i was writing simple editor (something like paint but more
> >>>>> custom for my eventual needs) for big pixel (low resolution)
> >>>>> drawing
> >>>>>
> >>>>> it showed in a minute i need a click for changing given drawed
> >>>>> area of of one color into another color (becouse if no someone
> >>>>> would need to do it  by hand pixel by pixel and the need to
> >>>>> change color of given element is very common)
> >>>>>
> >>>>> there is very simple method of doing it - i men i click in given
> >>>>> color pixel then replace it by my color and call the same
> >>>>> function on adjacent 4 pixels (only need check if it is in
> >>>>> screen at all and if the color to change is that initial color
> >>>>>
> >>>>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned
> >>>>> old_color, unsigned new_color)
> >>>>> {
> >>>>>   if(old_color == new_color) return 0;
> >>>>>
> >>>>>   if(XYIsInScreen( x,  y))
> >>>>>   if(GetPixelUnsafe(x,y)==old_color)
> >>>>>   {
> >>>>>     SetPixelSafe(x,y,new_color);
> >>>>>     RecolorizePixelAndAdjacentOnes(x+1, y,  old_color,
> >>>>> new_color); RecolorizePixelAndAdjacentOnes(x-1, y,  old_color,
> >>>>> new_color); RecolorizePixelAndAdjacentOnes(x, y-1,  old_color,
> >>>>> new_color); RecolorizePixelAndAdjacentOnes(x, y+1,  old_color,
> >>>>> new_color); return 1;
> >>>>>   }
> >>>>>
> >>>>>   return 0;
> >>>>> }
> >>>>>
> >>>>> it work but im not quite sure how to estimate the safety of this
> >>>>> - incidentally as i said i use this editor to low res graphics
> >>>>> like 200x200 pixels or less, and it is only a toll of private
> >>>>> use, yet i got no time to work on it more than 1-2-3 days i
> >>>>> guess but still
> >>>>>
> >>>>> is there maybe simple way to improve it?
> >>>> >
> >>>> This is a cheap and cheerful fllod fill. And it's easy to get
> >>>> right and shouldn't afall over.
> >>>
> >>> Except I don't understand why it works it all.
> >>> Can't fill area have sub-areas that only connected through
> >>> diagonal?
> >>
> >> Suppose you have an image which is a chessboard. You want to fill
> >> one of the black squares so that it is red.
> >>
> >> If you allow connectivity through the diagonals (so two notionally
> >> square pixels that only meet at their corners would be connected),
> >> then all the black squares would turn red, not just one.
> >>
> >
> > That's what I want.
> > Do fir wants something else?
> >
>
> His algorithm is the same as that presented in my textbook, where it
> is called FloodFill4.
>
> If I reread the notes I see now the significance of the '4', as it
> talks about 4-connected and 8-connected versions.
>
> Presumably you want the 8-connected version, which will have 4 extra
> calls for the pixels at each corner.
>
>

'4' variant does not appear useful for changing colors of drawn shapes,
like lines or circles. Nor would it work for changing color of text
except when font is unusually bold.

Re: filling area by color atack safety

<877ci1j6ew.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34453&group=comp.lang.c#34453

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 14:10:15 +0000
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <877ci1j6ew.fsf@bsb.me.uk>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<20240317144625.00002011@yahoo.com> <ut6p67$3h8t0$2@dont-email.me>
<20240317151519.000057d0@yahoo.com> <ut6qt8$3hnat$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="664dca1fae3f1391a07afac504c68131";
logging-data="3735635"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nXlyRAXm0Kh4cKorMGmzrAhtvjG8nioM="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:dEnrJNFZywXbNtP2qrDoV1pH95A=
sha1:NmuHbVY039fJyJyNBfGI9M7MmF4=
X-BSB-Auth: 1.fed02ca164411037bac3.20240317141015GMT.877ci1j6ew.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 17 Mar 2024 14:10 UTC

bart <bc@freeuk.com> writes:

> His algorithm is the same as that presented in my textbook, where it is
> called FloodFill4.

s/my/his/?

--
Ben.

Re: filling area by color atack safety

<ut6ul9$3gr73$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34454&group=comp.lang.c#34454

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 14:27:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <ut6ul9$3gr73$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 14:27:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b1330aeeb9339fe0167b221482a8d088";
logging-data="3697891"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vz5otvRlB5OBMadrqYifmTvY7dv/EUPI="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:cIWGPwpW2SvLxWjE/pE3itc7DWI=
 by: Lew Pitcher - Sun, 17 Mar 2024 14:27 UTC

On Sat, 16 Mar 2024 11:33:20 +0000, Malcolm McLean wrote:

> On 16/03/2024 04:11, fir wrote:
>> i was writing simple editor (something like paint but more custom for my
>> eventual needs) for big pixel (low resolution) drawing
>>
>> it showed in a minute i need a click for changing given drawed area of
>> of one color into another color (becouse if no someone would need to do
>> it  by hand pixel by pixel and the need to change color of given element
>> is very common)
>>
>> there is very simple method of doing it - i men i click in given color
>> pixel then replace it by my color and call the same function on adjacent
>> 4 pixels (only need check if it is in screen at all and if the color to
>> change is that initial color
>>
>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned old_color,
>> unsigned new_color)
>> {
>>   if(old_color == new_color) return 0;
>>
>>   if(XYIsInScreen( x,  y))
>>   if(GetPixelUnsafe(x,y)==old_color)
>>   {
>>     SetPixelSafe(x,y,new_color);
>>     RecolorizePixelAndAdjacentOnes(x+1, y,  old_color, new_color);
>>     RecolorizePixelAndAdjacentOnes(x-1, y,  old_color, new_color);
>>     RecolorizePixelAndAdjacentOnes(x, y-1,  old_color, new_color);
>>     RecolorizePixelAndAdjacentOnes(x, y+1,  old_color, new_color);
>>     return 1;
>>   }
>>
>>   return 0;
>> }
>>
>> it work but im not quite sure how to estimate the safety of this  -
>> incidentally as i said i use this editor to low res graphics  like
>> 200x200 pixels or less, and it is only a toll of private use,
>> yet i got no time to work on it more than 1-2-3 days i guess but still
>>
>> is there maybe simple way to improve it?
> >
> This is a cheap and cheerful fllod fill. And it's easy to get right and
> shouldn't afall over. But but makes an awful not of unnecessary calls,
> and on a small system and large image might even blow the stack.
>
> Recursion make programs harder to reason about and prove correct.

I would have said that those unfamiliar with the concept of recursion
have a harder time reasoning about the effects of recursion, or proving
their recursive code correct.

Take fir's example code above; a simple single call to
RecolorizePixelAndAdjacentOnes() will effectively recolour the
origin cell multiple times, because of how the recursion is handled.

As an example:
RecolorizePixelAndAdjacentOnes(0,0,1 2)
will
SetPixelSafe(0,0,2);
then invoke
RecolorizePixelAndAdjacentOnes(1,0,1 2)
which will
SetPixelSafe(1,0,2)
and subsequently invoke
...
RecolorizePixelAndAdjacentOnes(0,0,1 2)
which will
SetPixelSafe(0,0,2);
and then invoke
RecolorizePixelAndAdjacentOnes(1,0,1 2)
etc.

[snip]

--
Lew Pitcher
"In Skills We Trust"

Re: filling area by color atack safety

<ut70b4$3itvb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34455&group=comp.lang.c#34455

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 14:56:34 +0000
Organization: A noiseless patient Spider
Lines: 209
Message-ID: <ut70b4$3itvb$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me> <ut4cnc$2ut2t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 17 Mar 2024 14:56:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="1302203e67d64edb589b5f20956e00a5";
logging-data="3766251"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0mNb3zbpYUX2Eg4Lrohecrlebe5zvdTM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xjzENMIi63EWgpMfGeLLvLjG3Ww=
In-Reply-To: <ut4cnc$2ut2t$1@dont-email.me>
Content-Language: en-GB
 by: Malcolm McLean - Sun, 17 Mar 2024 14:56 UTC

On 16/03/2024 15:09, Malcolm McLean wrote:
> On 16/03/2024 14:40, David Brown wrote:
>> On 16/03/2024 12:33, Malcolm McLean wrote:
>>
>>> And here's some code I wrote a while ago. Use that as a pattern. But
>>> not sure how well it works. Haven't used it for a long time.
>>>
>>> https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/drawbinary.c
>>>
>>
>> Your implementation is a mess, /vastly/ more difficult to prove
>> correct than the OP's original one, and unlikely to be very much
>> faster (it will certainly scale in the same way in both time and
>> memory usage).
>>
> Now is this David Brown being David Borwn, ot its it actaully ture?

> And I need to run some tests, don't I?
>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int floodfill_r(unsigned char *grey, int width, int height, int x, int y,
unsigned char target, unsigned char dest)
{ if (x < 0 || x >= width || y < 0 || y >= height)
return 0;
if (grey[y*width+x] != target)
return 0;
grey[y*width+x] = dest;
floodfill_r(grey, width, height, x - 1, y, target, dest);
floodfill_r(grey, width, height, x + 1, y, target, dest);
floodfill_r(grey, width, height, x, y - 1, target, dest);
floodfill_r(grey, width, height, x, y + 1, target, dest);

return 0;
}

/**
Floodfill4 - floodfill, 4 connectivity.

@param[in,out] grey - the image (formally it's greyscale but it could be
binary or indexed)
@param width - image width
@param height - image height
@param x - seed point x
@param y - seed point y
@param target - the colour to flood
@param dest - the colur to replace it by.
@returns Number of pixels flooded.
*/
int floodfill4(unsigned char *grey, int width, int height, int x, int y,
unsigned char target, unsigned char dest)
{ int *qx = 0;
int *qy = 0;
int qN = 0;
int qpos = 0;
int qcapacity = 0;
int wx, wy;
int ex, ey;
int tx, ty;
int ix;
int *temp;
int answer = 0;

if(grey[y * width + x] != target)
return 0;
qx = malloc(width * sizeof(int));
qy = malloc(width * sizeof(int));
if(qx == 0 || qy == 0)
goto error_exit;
qcapacity = width;
qx[qpos] = x;
qy[qpos] = y;
qN = 1;

while(qN != 0)
{
tx = qx[qpos];
ty = qy[qpos];
qpos++;
qN--;

if(qpos == 256)
{
memmove(qx, qx + 256, qN*sizeof(int));
memmove(qy, qy + 256, qN*sizeof(int));
qpos = 0;
}
if(grey[ty*width+tx] != target)
continue;
wx = tx;
wy = ty;
while(wx >= 0 && grey[wy*width+wx] == target)
wx--;
wx++;
ex = tx;
ey = ty;
while(ex < width && grey[ey*width+ex] == target)
ex++;
ex--;

for(ix=wx;ix<=ex;ix++)
{
grey[ty*width+ix] = dest;
answer++;
}

if(ty > 0)
for(ix=wx;ix<=ex;ix++)
{
if(grey[(ty-1)*width+ix] == target)
{
if(qpos + qN == qcapacity)
{
temp = realloc(qx, (qcapacity + width) * sizeof(int));
if(temp == 0)
goto error_exit;
qx = temp;
temp = realloc(qy, (qcapacity + width) * sizeof(int));
if(temp == 0)
goto error_exit;
qy = temp;
qcapacity += width;
}
qx[qpos+qN] = ix;
qy[qpos+qN] = ty-1;
qN++;
}
}
if(ty < height -1)
for(ix=wx;ix<=ex;ix++)
{
if(grey[(ty+1)*width+ix] == target)
{
if(qpos + qN == qcapacity)
{
temp = realloc(qx, (qcapacity + width) * sizeof(int));
if(temp == 0)
goto error_exit;
qx = temp;
temp = realloc(qy, (qcapacity + width) * sizeof(int));
if(temp == 0)
goto error_exit;
qy = temp;
qcapacity += width;
}
qx[qpos+qN] = ix;
qy[qpos+qN] = ty+1;
qN++;
}
}
}

free(qx);
free(qy);

return answer;
error_exit:
free(qx);
free(qy);
return -1;
}

int main(void)
{ unsigned char *image;
clock_t tick, tock;
int i;

image = malloc(100 * 100);
tick = clock();
for (i = 0 ; i < 10000; i++)
{
memset(image, 0, 100 * 100);
floodfill_r(image, 100, 100, 50, 50, 0, 1);
}
tock = clock();
printf("floodfill_r %g\n", ((double)(tock - tick))/CLOCKS_PER_SEC);

tick = clock();
for (i = 0 ; i < 10000; i++)
{
memset(image, 0, 100 * 100);
floodfill4(image, 100, 100, 50, 50, 0, 1);
}
tock = clock();
printf("floodfill4 %g\n", ((double)(tock - tick))/CLOCKS_PER_SEC);

return 0;
}

Let's give it a whirl

malcolm@Malcolms-iMac cscratch % gcc -O3 testfloodfill.c
malcolm@Malcolms-iMac cscratch % ./a.out
floodfill_r 1.69274
floodfill4 0.336705

--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: filling area by color atack safety

<ut71ab$3j1n2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34456&group=comp.lang.c#34456

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 15:13:18 +0000
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <ut71ab$3j1n2$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut6ul9$3gr73$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 15:13:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="86cdd72051d773a92e0c8f332750da77";
logging-data="3770082"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18itQyNMUU+NcTVXEyYQAAN"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Gp/yRMnjkgDbwgeeM+LFuNMw0aA=
In-Reply-To: <ut6ul9$3gr73$1@dont-email.me>
Content-Language: en-GB
 by: bart - Sun, 17 Mar 2024 15:13 UTC

On 17/03/2024 14:27, Lew Pitcher wrote:
> On Sat, 16 Mar 2024 11:33:20 +0000, Malcolm McLean wrote:
>
>> On 16/03/2024 04:11, fir wrote:
>>> i was writing simple editor (something like paint but more custom for my
>>> eventual needs) for big pixel (low resolution) drawing
>>>
>>> it showed in a minute i need a click for changing given drawed area of
>>> of one color into another color (becouse if no someone would need to do
>>> it  by hand pixel by pixel and the need to change color of given element
>>> is very common)
>>>
>>> there is very simple method of doing it - i men i click in given color
>>> pixel then replace it by my color and call the same function on adjacent
>>> 4 pixels (only need check if it is in screen at all and if the color to
>>> change is that initial color
>>>
>>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned old_color,
>>> unsigned new_color)
>>> {
>>>   if(old_color == new_color) return 0;
>>>
>>>   if(XYIsInScreen( x,  y))
>>>   if(GetPixelUnsafe(x,y)==old_color)
>>>   {
>>>     SetPixelSafe(x,y,new_color);
>>>     RecolorizePixelAndAdjacentOnes(x+1, y,  old_color, new_color);
>>>     RecolorizePixelAndAdjacentOnes(x-1, y,  old_color, new_color);
>>>     RecolorizePixelAndAdjacentOnes(x, y-1,  old_color, new_color);
>>>     RecolorizePixelAndAdjacentOnes(x, y+1,  old_color, new_color);
>>>     return 1;
>>>   }
>>>
>>>   return 0;
>>> }
>>>
>>> it work but im not quite sure how to estimate the safety of this  -
>>> incidentally as i said i use this editor to low res graphics  like
>>> 200x200 pixels or less, and it is only a toll of private use,
>>> yet i got no time to work on it more than 1-2-3 days i guess but still
>>>
>>> is there maybe simple way to improve it?
>> >
>> This is a cheap and cheerful fllod fill. And it's easy to get right and
>> shouldn't afall over. But but makes an awful not of unnecessary calls,
>> and on a small system and large image might even blow the stack.
>>
>> Recursion make programs harder to reason about and prove correct.
>
> I would have said that those unfamiliar with the concept of recursion
> have a harder time reasoning about the effects of recursion, or proving
> their recursive code correct.
>
> Take fir's example code above; a simple single call to
> RecolorizePixelAndAdjacentOnes() will effectively recolour the
> origin cell multiple times, because of how the recursion is handled.

I don't think so. It may look at the original cell, but it will only
recolour it (and recursively process its neighbours) if the colour
hasn't yet changed to the new one.

If I take a 100x100 image with 10,000 cells, which all have to be filled
to the new colour, then SetPixelSafe is called exactly 10,000 times.

The problem is that most of the work is done along a 10,000-deep chain
of nested calls.

Re: filling area by color atack safety

<20240317174218.0000471e@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34457&group=comp.lang.c#34457

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 17:42:18 +0200
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <20240317174218.0000471e@yahoo.com>
References: <ut3669$21eur$1@i2pn2.org>
<ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me>
<ut4cnc$2ut2t$1@dont-email.me>
<ut70b4$3itvb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="fd62a22e31fcf9828d4eedf77229489a";
logging-data="3728424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qIUjGk0pYKMLUjum0TPiQLpocRTAaO1E="
Cancel-Lock: sha1:vIe4EbPy1KqH0udeN4cGxudARnE=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Sun, 17 Mar 2024 15:42 UTC

On Sun, 17 Mar 2024 14:56:34 +0000
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:

> On 16/03/2024 15:09, Malcolm McLean wrote:
> > On 16/03/2024 14:40, David Brown wrote:
> >> On 16/03/2024 12:33, Malcolm McLean wrote:
> >>
> >>> And here's some code I wrote a while ago. Use that as a pattern.
> >>> But not sure how well it works. Haven't used it for a long time.
> >>>
> >>> https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/drawbinary.c
> >>>
> >>
> >> Your implementation is a mess, /vastly/ more difficult to prove
> >> correct than the OP's original one, and unlikely to be very much
> >> faster (it will certainly scale in the same way in both time and
> >> memory usage).
> >>
> > Now is this David Brown being David Borwn, ot its it actaully ture?
> >
>
> > And I need to run some tests, don't I?
> >
>
> Let's give it a whirl
>

<snip>

> malcolm@Malcolms-iMac cscratch % gcc -O3 testfloodfill.c
> malcolm@Malcolms-iMac cscratch % ./a.out
> floodfill_r 1.69274
> floodfill4 0.336705
>
>

Now try the case in which original recursion is particularly deep.
Something like that:
*.***.**
*.*.*.*.
*.*.*.*.
*.*.*.*.
*.*.*.*.
*.*.*.*.
*.*.*.*.
***.***.

Re: filling area by color atack safety

<20240317182520.00002390@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34458&group=comp.lang.c#34458

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 18:25:20 +0200
Organization: A noiseless patient Spider
Lines: 259
Message-ID: <20240317182520.00002390@yahoo.com>
References: <ut3669$21eur$1@i2pn2.org>
<ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me>
<ut4cnc$2ut2t$1@dont-email.me>
<ut70b4$3itvb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="fd62a22e31fcf9828d4eedf77229489a";
logging-data="3728424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sbw9RRgv2ZVofBOkFdv2AzpIx8HFDEM4="
Cancel-Lock: sha1:yd/wiJqC2+NA0VsMvbSeO8gMUzg=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Sun, 17 Mar 2024 16:25 UTC

On Sun, 17 Mar 2024 14:56:34 +0000
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:

> On 16/03/2024 15:09, Malcolm McLean wrote:
> > On 16/03/2024 14:40, David Brown wrote:
> >> On 16/03/2024 12:33, Malcolm McLean wrote:
> >>
> >>> And here's some code I wrote a while ago. Use that as a pattern.
> >>> But not sure how well it works. Haven't used it for a long time.
> >>>
> >>> https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/drawbinary.c
> >>>
> >>
> >> Your implementation is a mess, /vastly/ more difficult to prove
> >> correct than the OP's original one, and unlikely to be very much
> >> faster (it will certainly scale in the same way in both time and
> >> memory usage).
> >>
> > Now is this David Brown being David Borwn, ot its it actaully ture?
> >
>
> > And I need to run some tests, don't I?
> >
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <time.h>
>
> int floodfill_r(unsigned char *grey, int width, int height, int x,
> int y, unsigned char target, unsigned char dest)
> {
> if (x < 0 || x >= width || y < 0 || y >= height)
> return 0;
> if (grey[y*width+x] != target)
> return 0;
> grey[y*width+x] = dest;
> floodfill_r(grey, width, height, x - 1, y, target, dest);
> floodfill_r(grey, width, height, x + 1, y, target, dest);
> floodfill_r(grey, width, height, x, y - 1, target, dest);
> floodfill_r(grey, width, height, x, y + 1, target, dest);
>
> return 0;
> }
>
> /**
> Floodfill4 - floodfill, 4 connectivity.
>
> @param[in,out] grey - the image (formally it's greyscale but it
> could be binary or indexed)
> @param width - image width
> @param height - image height
> @param x - seed point x
> @param y - seed point y
> @param target - the colour to flood
> @param dest - the colur to replace it by.
> @returns Number of pixels flooded.
> */
> int floodfill4(unsigned char *grey, int width, int height, int x, int
> y, unsigned char target, unsigned char dest)
> {
> int *qx = 0;
> int *qy = 0;
> int qN = 0;
> int qpos = 0;
> int qcapacity = 0;
> int wx, wy;
> int ex, ey;
> int tx, ty;
> int ix;
> int *temp;
> int answer = 0;
>
> if(grey[y * width + x] != target)
> return 0;
> qx = malloc(width * sizeof(int));
> qy = malloc(width * sizeof(int));
> if(qx == 0 || qy == 0)
> goto error_exit;
> qcapacity = width;
> qx[qpos] = x;
> qy[qpos] = y;
> qN = 1;
>
> while(qN != 0)
> {
> tx = qx[qpos];
> ty = qy[qpos];
> qpos++;
> qN--;
>
> if(qpos == 256)
> {
> memmove(qx, qx + 256, qN*sizeof(int));
> memmove(qy, qy + 256, qN*sizeof(int));
> qpos = 0;
> }
> if(grey[ty*width+tx] != target)
> continue;
> wx = tx;
> wy = ty;
> while(wx >= 0 && grey[wy*width+wx] == target)
> wx--;
> wx++;
> ex = tx;
> ey = ty;
> while(ex < width && grey[ey*width+ex] == target)
> ex++;
> ex--;
>
>
> for(ix=wx;ix<=ex;ix++)
> {
> grey[ty*width+ix] = dest;
> answer++;
> }
>
> if(ty > 0)
> for(ix=wx;ix<=ex;ix++)
> {
> if(grey[(ty-1)*width+ix] == target)
> {
> if(qpos + qN == qcapacity)
> {
> temp = realloc(qx, (qcapacity + width) * sizeof(int));
> if(temp == 0)
> goto error_exit;
> qx = temp;
> temp = realloc(qy, (qcapacity + width) * sizeof(int));
> if(temp == 0)
> goto error_exit;
> qy = temp;
> qcapacity += width;
> }
> qx[qpos+qN] = ix;
> qy[qpos+qN] = ty-1;
> qN++;
> }
> }
> if(ty < height -1)
> for(ix=wx;ix<=ex;ix++)
> {
> if(grey[(ty+1)*width+ix] == target)
> {
> if(qpos + qN == qcapacity)
> {
> temp = realloc(qx, (qcapacity + width) * sizeof(int));
> if(temp == 0)
> goto error_exit;
> qx = temp;
> temp = realloc(qy, (qcapacity + width) * sizeof(int));
> if(temp == 0)
> goto error_exit;
> qy = temp;
> qcapacity += width;
> }
> qx[qpos+qN] = ix;
> qy[qpos+qN] = ty+1;
> qN++;
> }
> }
> }
>
> free(qx);
> free(qy);
>
> return answer;
> error_exit:
> free(qx);
> free(qy);
> return -1;
> }
>
> int main(void)
> {
> unsigned char *image;
> clock_t tick, tock;
> int i;
>
> image = malloc(100 * 100);
> tick = clock();
> for (i = 0 ; i < 10000; i++)
> {
> memset(image, 0, 100 * 100);
> floodfill_r(image, 100, 100, 50, 50, 0, 1);
> }
> tock = clock();
> printf("floodfill_r %g\n", ((double)(tock -
> tick))/CLOCKS_PER_SEC);
>
> tick = clock();
> for (i = 0 ; i < 10000; i++)
> {
> memset(image, 0, 100 * 100);
> floodfill4(image, 100, 100, 50, 50, 0, 1);
> }
> tock = clock();
> printf("floodfill4 %g\n", ((double)(tock - tick))/CLOCKS_PER_SEC);
>
> return 0;
> }
>
>
> Let's give it a whirl
>
> malcolm@Malcolms-iMac cscratch % gcc -O3 testfloodfill.c
> malcolm@Malcolms-iMac cscratch % ./a.out
> floodfill_r 1.69274
> floodfill4 0.336705
>
>

I find your performance measurement non-decisive for two reasons:
(1) because your test case is too trivial and probably uncharacteristic
and
(2) because recursive variant could be trivially rewritten in a way
that reduces # of stack memory accesses by factor of 2 or 3.
Like that:

struct recursive_context_t {
unsigned char *grey;
int width, height;
unsigned char target, dest;
};

static void floodfill_r_core(const struct recursive_context_t* context,
int x, int y) {
if (x < 0 || x >= context->width || y < 0 || y >= context->height)
return;
if (context->grey[y*context->width+x] == context->target) {
context->grey[y*context->width+x] = context->dest;
floodfill_r_core(context, x - 1, y);
floodfill_r_core(context, x + 1, y);
floodfill_r_core(context, x, y - 1);
floodfill_r_core(context, x, y + 1);
}
}

int floodfill_r(
unsigned char *grey,
int width, int height,
int x, int y,
unsigned char target, unsigned char dest)
{ if (x < 0 || x >= width || y < 0 || y >= height)
return 0;
if (grey[y*width+x] != target)
return 0;
struct recursive_context_t context = {
.grey = grey,
.width = width,
.height = height,
.target = target,
.dest = dest,
};
floodfill_r_core(&context, x, y);
return 1;
}

Re: filling area by color atack safety

<ut76ms$3kal7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34459&group=comp.lang.c#34459

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 17:45:16 +0100
Organization: A noiseless patient Spider
Lines: 111
Message-ID: <ut76ms$3kal7$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me> <ut4cnc$2ut2t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 16:45:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="212cee952392c1402e125e2adac79188";
logging-data="3812007"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/TZGy73Q91SbKGHzses/1zdjj0ArDe0M="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xEsj8UwHJ/nY7Xr3hSObJDwCClo=
In-Reply-To: <ut4cnc$2ut2t$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sun, 17 Mar 2024 16:45 UTC

On 16/03/2024 16:09, Malcolm McLean wrote:
> On 16/03/2024 14:40, David Brown wrote:
>> On 16/03/2024 12:33, Malcolm McLean wrote:
>>
>>> And here's some code I wrote a while ago. Use that as a pattern. But
>>> not sure how well it works. Haven't used it for a long time.
>>>
>>> https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/drawbinary.c
>>>
>>
>> Your implementation is a mess, /vastly/ more difficult to prove
>> correct than the OP's original one, and unlikely to be very much
>> faster (it will certainly scale in the same way in both time and
>> memory usage).
>>
> Now is this David Brown being David Borwn, ot its it actaully ture?

I don't know who "David Borwn" might be, nor what "ture" means. If you
can't type, and can't spell, then at least pay the group the respect of
using a spell-checker.

> It's not designed to be eay to prove correct, that's true. And the
> maintain it's mess is that we are managing the queue manually for speed.

It is badly designed code. It is a jumble of wildly different concepts,
thrown together in one huge function with no structure or organisation,
and with meaningless names for the variables and absurd names for the
parameters.

The OP's code is simple and obvious, as is its correctness (assuming
reasonable definitions of the pixel access and setting functions) and
its time and space requirements. Yours is not.

Your algorithm could be used in a proper implementation, with separate
functions to handle the different parts (such as the stack). The
algorithm itself is not bad, it's the implementation that is the main
problem.

>
> But the naive recursive algorithm is O(N) (N = pixels to flood), and
> inherently we can't beat that without special hardware.

Assuming you are measuring the number of pixels read or written here,
then that is, I think, correct.

> The recursive
> one tends to be slow because calls are expensive.

Yes, I agree that recursion can be slow (unless it is simple enough for
the compiler to turn it into a loop). And it typically takes more stack
space than you'd need for a dedicated queue. But whether or not that
makes a significant difference depends on the code in question, and how
much work you are doing within the code. If step of the algorithm takes
a lot of time anyway, the call overhead will be of less relevance.

I would expect that your code would be several times faster than the
OP's, with similar scaling. But the OP's is understandable and easily
seen to be correct, unlike yours, and correctness trumps speed every time.

And starting from a correct recursive version, it's possible to improve
on it in many ways while retaining correctness.

> And mine makes calls
> to malloc() and realloc to manage the queue. And of course whilst we
> might blow the stack, we are much less likely to run out of heap.

True.

>
> And it's been tweaked abit in hacky way to make it faster on real
> images. And whilst it's still going to work, is it out of date?
>

I have no idea if your code is "out of date" or not. It seems to be
written for images consisting of unsigned chars, so I a not sure it was
ever designed for real-world images.

What is clear is that you have taken an okay algorithm - not state of
the art, but not the worst - and made a dog's breakfast of an
implementation in your attempts to micro-optimise. This means you have
code that can't be easily analysed or seen to be correct, cannot be
improved algorithmically, and cannot be expanded or gain new features
without a massive re-write.

> And I need to run some tests, don't I?
>

If you like.

> >
>> There are a variety of different flood-fill algorithms, with different
>> advantages and disadvantages.  Speeds will often depend as much on the
>> way the get/set pixel code works, especially if the flood-fill is on
>> live displayed data rather than in a buffer off-screen.  But typically
>> you need to get a /lot/ more advanced (i.e., not your algorithm) to
>> improve on the OP's version by an order of magnitude, so if speed is
>> not essential but understanding that it is correct is important, then
>> it makes more sense to stick to the original recursive version.
>>
>
> What are these / lot / more advanced algorithms? Maybe they exist. But
> don't people deserve some sort of link?
>

<https://gprivate.com/6a2yp>

I don't know if it is fair to call them a /lot/ more advanced, but
certainly a bit more advanced. And certainly better implementations are
possible.

Re: filling area by color atack safety

<qMTIrtMGRLIELXCKB@bongo-ra.co>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34460&group=comp.lang.c#34460

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 16:58:56 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <qMTIrtMGRLIELXCKB@bongo-ra.co>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me> <20240317144625.00002011@yahoo.com>
<ut6p67$3h8t0$2@dont-email.me> <20240317151519.000057d0@yahoo.com> <ut6qt8$3hnat$1@dont-email.me>
<877ci1j6ew.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 16:58:56 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3626095fab494a475dc1429e775ff6be";
logging-data="3817336"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OaSPre2qGekqrkzCfIKO7"
Cancel-Lock: sha1:jiMB9+T6jroArzrS+im0ITrXsmU=
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
In-Reply-To: <877ci1j6ew.fsf@bsb.me.uk>
 by: Spiros Bousbouras - Sun, 17 Mar 2024 16:58 UTC

On Sun, 17 Mar 2024 14:10:15 +0000
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> bart <bc@freeuk.com> writes:
>
> > His algorithm is the same as that presented in my textbook, where it is
> > called FloodFill4.
>
> s/my/his/?

What is mentioned in <ut4v4r$32mgb$1@dont-email.me> : "I've just looked in
my Computer Graphics Principles and Practice book" .

Re: filling area by color atack safety

<ut77hr$3kal7$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34461&group=comp.lang.c#34461

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 17:59:39 +0100
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <ut77hr$3kal7$2@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me> <BulJN.144026$CYpe.133853@fx40.iad>
<87r0g9jgjt.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 17 Mar 2024 16:59:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="212cee952392c1402e125e2adac79188";
logging-data="3812007"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FoUAVkG2BciHm+1bZIgS6h7q/H8Q45dE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:j7cNg9bLNHMQ8hv1b9qazo3thlM=
Content-Language: en-GB
In-Reply-To: <87r0g9jgjt.fsf@bsb.me.uk>
 by: David Brown - Sun, 17 Mar 2024 16:59 UTC

On 17/03/2024 11:31, Ben Bacarisse wrote:
> scott@slp53.sl.home (Scott Lurndal) writes:
>
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 16/03/2024 12:33, Malcolm McLean wrote:
>>
>>>> And here's some code I wrote a while ago. Use that as a pattern. But not
>>>> sure how well it works. Haven't used it for a long time.
>>>>
>>>> https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/drawbinary.c
>>>>
>>>
>>> Your implementation is a mess, /vastly/ more difficult to prove correct
>>
>> Malcolm can't even spell 'integer' correctly in that code blob :-).
>
> As someone with dyslexia I have never liked mocking remarks about
> spelling errors. Using "even" suggests that a superficial issue hints
> at deeper problems. This is rarely the case.
>
> However, I /would/ urge Malcolm to correct the spelling if Bresenham
> since the intent was clearly to credit the discoverer. Also,
> misspellings don't play well with library databases.
>

I also have dyslexia. I am dependent on a spell checker to spell
accurately. And that is one of the reasons why Malcolm should do a
better job of writing accurately - it is much easier for others to read
posts when the spelling and the grammar is correct. I would, of course,
also like him to do a better job at his grammar, but using a
spell-checker is so simple and low-cost that it is inexcusable for him
not to use one.

I have nothing bad to say about people who can't spell well, or who
can't type well, and I have nothing but respect for people who are
trying their best to write in a second (or third, or more) language.

But Malcolm is a native English speaker with a degree in English. He is
not dyslexic (or at least, the mistakes he makes are not typical signs
of dyslexia). He is simply a poor typist and too lazy to make an effort
to correct his errors.

>> Certainly the intent of Fir's algorithm is easily discerned from
>> his code. I can't say that about Malcolms.
>
> I have some reservations about the code, but he posted a link so there
> is no indication that he wants a review of it.
>

Re: filling area by color atack safety

<ut77si$3kal7$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34462&group=comp.lang.c#34462

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 18:05:22 +0100
Organization: A noiseless patient Spider
Lines: 100
Message-ID: <ut77si$3kal7$3@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<20240317144625.00002011@yahoo.com> <ut6p67$3h8t0$2@dont-email.me>
<20240317151519.000057d0@yahoo.com> <ut6qt8$3hnat$1@dont-email.me>
<20240317153742.00002435@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 17:05:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="212cee952392c1402e125e2adac79188";
logging-data="3812007"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CVUa9kTRc9y0QVNbcFBVqHGFOJNmM4OU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:apQIApEBi2nW1lSrPmMY8FUkp74=
In-Reply-To: <20240317153742.00002435@yahoo.com>
Content-Language: en-GB
 by: David Brown - Sun, 17 Mar 2024 17:05 UTC

On 17/03/2024 14:37, Michael S wrote:
> On Sun, 17 Mar 2024 13:23:55 +0000
> bart <bc@freeuk.com> wrote:
>
>> On 17/03/2024 13:15, Michael S wrote:
>>> On Sun, 17 Mar 2024 12:54:34 +0000
>>> bart <bc@freeuk.com> wrote:
>>>
>>>> On 17/03/2024 12:46, Michael S wrote:
>>>>> On Sat, 16 Mar 2024 11:33:20 +0000
>>>>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>>>>>
>>>>>> On 16/03/2024 04:11, fir wrote:
>>>>>>> i was writing simple editor (something like paint but more
>>>>>>> custom for my eventual needs) for big pixel (low resolution)
>>>>>>> drawing
>>>>>>>
>>>>>>> it showed in a minute i need a click for changing given drawed
>>>>>>> area of of one color into another color (becouse if no someone
>>>>>>> would need to do it  by hand pixel by pixel and the need to
>>>>>>> change color of given element is very common)
>>>>>>>
>>>>>>> there is very simple method of doing it - i men i click in given
>>>>>>> color pixel then replace it by my color and call the same
>>>>>>> function on adjacent 4 pixels (only need check if it is in
>>>>>>> screen at all and if the color to change is that initial color
>>>>>>>
>>>>>>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned
>>>>>>> old_color, unsigned new_color)
>>>>>>> {
>>>>>>>   if(old_color == new_color) return 0;
>>>>>>>
>>>>>>>   if(XYIsInScreen( x,  y))
>>>>>>>   if(GetPixelUnsafe(x,y)==old_color)
>>>>>>>   {
>>>>>>>     SetPixelSafe(x,y,new_color);
>>>>>>>     RecolorizePixelAndAdjacentOnes(x+1, y,  old_color,
>>>>>>> new_color); RecolorizePixelAndAdjacentOnes(x-1, y,  old_color,
>>>>>>> new_color); RecolorizePixelAndAdjacentOnes(x, y-1,  old_color,
>>>>>>> new_color); RecolorizePixelAndAdjacentOnes(x, y+1,  old_color,
>>>>>>> new_color); return 1;
>>>>>>>   }
>>>>>>>
>>>>>>>   return 0;
>>>>>>> }
>>>>>>>
>>>>>>> it work but im not quite sure how to estimate the safety of this
>>>>>>> - incidentally as i said i use this editor to low res graphics
>>>>>>> like 200x200 pixels or less, and it is only a toll of private
>>>>>>> use, yet i got no time to work on it more than 1-2-3 days i
>>>>>>> guess but still
>>>>>>>
>>>>>>> is there maybe simple way to improve it?
>>>>>> >
>>>>>> This is a cheap and cheerful fllod fill. And it's easy to get
>>>>>> right and shouldn't afall over.
>>>>>
>>>>> Except I don't understand why it works it all.
>>>>> Can't fill area have sub-areas that only connected through
>>>>> diagonal?
>>>>
>>>> Suppose you have an image which is a chessboard. You want to fill
>>>> one of the black squares so that it is red.
>>>>
>>>> If you allow connectivity through the diagonals (so two notionally
>>>> square pixels that only meet at their corners would be connected),
>>>> then all the black squares would turn red, not just one.
>>>>
>>>
>>> That's what I want.
>>> Do fir wants something else?
>>>
>>
>> His algorithm is the same as that presented in my textbook, where it
>> is called FloodFill4.
>>
>> If I reread the notes I see now the significance of the '4', as it
>> talks about 4-connected and 8-connected versions.
>>
>> Presumably you want the 8-connected version, which will have 4 extra
>> calls for the pixels at each corner.
>>
>>
>
> '4' variant does not appear useful for changing colors of drawn shapes,
> like lines or circles. Nor would it work for changing color of text
> except when font is unusually bold.
>

An 8-connected flood fill is typically too much, and a 4-connected flood
fill is often too little. Neither is perfect for all cases, but I think
the 4-connected version is the most commonly used.

And as they stand, both are useless for images that are come from
realistic pictures (as distinct from drawings), since real colours
change gradually.

That's why graphics programs have feathered selection and masking, fuzzy
fills, and so on.

Re: filling area by color atack safety

<20240317095014.365@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34463&group=comp.lang.c#34463

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 17:11:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <20240317095014.365@kylheku.com>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<87wmq2jn7s.fsf@bsb.me.uk> <ut4ba7$2uits$1@dont-email.me>
<87frwpje2b.fsf@bsb.me.uk> <ut6o5l$3h3cb$3@dont-email.me>
Injection-Date: Sun, 17 Mar 2024 17:11:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="864941e21c918841c5381afe3a2ae04e";
logging-data="3823714"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UU6WAm/926QXyYZ0sc5ncLTOqxQ2Z9s8="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:opRRttq8ML5imt17Ea8INHtBA3E=
 by: Kaz Kylheku - Sun, 17 Mar 2024 17:11 UTC

On 2024-03-17, Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> The convetional wisdom is the opposite, But here, conventional wisdom
> fails. Because heaps are unlimited whilst stacks are not.

The strawman, absolutist conventional wisdom that "recursion is always
easier to analyze than iteration" is wrong in the first place.

Any program graph based on nothing but IF and GOTO primitives can be
mechanically transliterated into a (tail) recursive structure that has
the same shape, and is no easier to understand.

Your point is not very well made, though. Even though recursion may run
into a resource limit, its structure can still help analyze the logic of
the algorithm apart from that resource issue. The resource issue can be
separately analyzed and provisions made for the algorithm to handle the
required inputs, and reject others.

Most algorithms (especially ones working with all inputs in memory)
are constrained by resources. The iterative version of that image
processing algorithm might handle larger images than the recursive
one, but there are yet image sizes it won't handle.

The idea of calling algorithm implementations "incorrect" if they have
any limitations on their input sizes and such isn't particularly
informative or useful.

Obviously it is incorrect if something has limitations, and is used
in such a way that they are exceeded. E.g. the C <int> + <int>
operation when a result is implied that is beyond INT_MIN or INT_MAX.
Oops, + is not "correct"; don't use it!

Now, there is a bit of value in algorithms that will successfully
operate on any object that was successfully fit into memory. Do
these really exist though? Pretty much any algorithm implementation
requires some space to do its work, even if that space is small and
fixed. It's possible that the input fit into memory, yet that small and
fixed amount of space is not available.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: filling area by color atack safety

<20240317193908.00002634@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34464&group=comp.lang.c#34464

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 19:39:08 +0200
Organization: A noiseless patient Spider
Lines: 272
Message-ID: <20240317193908.00002634@yahoo.com>
References: <ut3669$21eur$1@i2pn2.org>
<ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me>
<ut4cnc$2ut2t$1@dont-email.me>
<ut70b4$3itvb$1@dont-email.me>
<20240317182520.00002390@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="fd62a22e31fcf9828d4eedf77229489a";
logging-data="3825776"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18j0rPBXtlUC9dTmv+gbaxskYamRLsFjtI="
Cancel-Lock: sha1:1xq3HYa6SxOsxqs78wUiSgZ6TDQ=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Sun, 17 Mar 2024 17:39 UTC

On Sun, 17 Mar 2024 18:25:20 +0200
Michael S <already5chosen@yahoo.com> wrote:

> On Sun, 17 Mar 2024 14:56:34 +0000
> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>
> > On 16/03/2024 15:09, Malcolm McLean wrote:
> > > On 16/03/2024 14:40, David Brown wrote:
> > >> On 16/03/2024 12:33, Malcolm McLean wrote:
> > >>
> > >>> And here's some code I wrote a while ago. Use that as a pattern.
> > >>> But not sure how well it works. Haven't used it for a long time.
> > >>>
> > >>> https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/drawbinary.c
> > >>>
> > >>
> > >> Your implementation is a mess, /vastly/ more difficult to prove
> > >> correct than the OP's original one, and unlikely to be very much
> > >> faster (it will certainly scale in the same way in both time and
> > >> memory usage).
> > >>
> > > Now is this David Brown being David Borwn, ot its it actaully
> > > ture?
> >
> > > And I need to run some tests, don't I?
> > >
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> > #include <time.h>
> >
> > int floodfill_r(unsigned char *grey, int width, int height, int x,
> > int y, unsigned char target, unsigned char dest)
> > {
> > if (x < 0 || x >= width || y < 0 || y >= height)
> > return 0;
> > if (grey[y*width+x] != target)
> > return 0;
> > grey[y*width+x] = dest;
> > floodfill_r(grey, width, height, x - 1, y, target, dest);
> > floodfill_r(grey, width, height, x + 1, y, target, dest);
> > floodfill_r(grey, width, height, x, y - 1, target, dest);
> > floodfill_r(grey, width, height, x, y + 1, target, dest);
> >
> > return 0;
> > }
> >
> > /**
> > Floodfill4 - floodfill, 4 connectivity.
> >
> > @param[in,out] grey - the image (formally it's greyscale but it
> > could be binary or indexed)
> > @param width - image width
> > @param height - image height
> > @param x - seed point x
> > @param y - seed point y
> > @param target - the colour to flood
> > @param dest - the colur to replace it by.
> > @returns Number of pixels flooded.
> > */
> > int floodfill4(unsigned char *grey, int width, int height, int x,
> > int y, unsigned char target, unsigned char dest)
> > {
> > int *qx = 0;
> > int *qy = 0;
> > int qN = 0;
> > int qpos = 0;
> > int qcapacity = 0;
> > int wx, wy;
> > int ex, ey;
> > int tx, ty;
> > int ix;
> > int *temp;
> > int answer = 0;
> >
> > if(grey[y * width + x] != target)
> > return 0;
> > qx = malloc(width * sizeof(int));
> > qy = malloc(width * sizeof(int));
> > if(qx == 0 || qy == 0)
> > goto error_exit;
> > qcapacity = width;
> > qx[qpos] = x;
> > qy[qpos] = y;
> > qN = 1;
> >
> > while(qN != 0)
> > {
> > tx = qx[qpos];
> > ty = qy[qpos];
> > qpos++;
> > qN--;
> >
> > if(qpos == 256)
> > {
> > memmove(qx, qx + 256, qN*sizeof(int));
> > memmove(qy, qy + 256, qN*sizeof(int));
> > qpos = 0;
> > }
> > if(grey[ty*width+tx] != target)
> > continue;
> > wx = tx;
> > wy = ty;
> > while(wx >= 0 && grey[wy*width+wx] == target)
> > wx--;
> > wx++;
> > ex = tx;
> > ey = ty;
> > while(ex < width && grey[ey*width+ex] == target)
> > ex++;
> > ex--;
> >
> >
> > for(ix=wx;ix<=ex;ix++)
> > {
> > grey[ty*width+ix] = dest;
> > answer++;
> > }
> >
> > if(ty > 0)
> > for(ix=wx;ix<=ex;ix++)
> > {
> > if(grey[(ty-1)*width+ix] == target)
> > {
> > if(qpos + qN == qcapacity)
> > {
> > temp = realloc(qx, (qcapacity + width) * sizeof(int));
> > if(temp == 0)
> > goto error_exit;
> > qx = temp;
> > temp = realloc(qy, (qcapacity + width) * sizeof(int));
> > if(temp == 0)
> > goto error_exit;
> > qy = temp;
> > qcapacity += width;
> > }
> > qx[qpos+qN] = ix;
> > qy[qpos+qN] = ty-1;
> > qN++;
> > }
> > }
> > if(ty < height -1)
> > for(ix=wx;ix<=ex;ix++)
> > {
> > if(grey[(ty+1)*width+ix] == target)
> > {
> > if(qpos + qN == qcapacity)
> > {
> > temp = realloc(qx, (qcapacity + width) * sizeof(int));
> > if(temp == 0)
> > goto error_exit;
> > qx = temp;
> > temp = realloc(qy, (qcapacity + width) * sizeof(int));
> > if(temp == 0)
> > goto error_exit;
> > qy = temp;
> > qcapacity += width;
> > }
> > qx[qpos+qN] = ix;
> > qy[qpos+qN] = ty+1;
> > qN++;
> > }
> > }
> > }
> >
> > free(qx);
> > free(qy);
> >
> > return answer;
> > error_exit:
> > free(qx);
> > free(qy);
> > return -1;
> > }
> >
> > int main(void)
> > {
> > unsigned char *image;
> > clock_t tick, tock;
> > int i;
> >
> > image = malloc(100 * 100);
> > tick = clock();
> > for (i = 0 ; i < 10000; i++)
> > {
> > memset(image, 0, 100 * 100);
> > floodfill_r(image, 100, 100, 50, 50, 0, 1);
> > }
> > tock = clock();
> > printf("floodfill_r %g\n", ((double)(tock -
> > tick))/CLOCKS_PER_SEC);
> >
> > tick = clock();
> > for (i = 0 ; i < 10000; i++)
> > {
> > memset(image, 0, 100 * 100);
> > floodfill4(image, 100, 100, 50, 50, 0, 1);
> > }
> > tock = clock();
> > printf("floodfill4 %g\n", ((double)(tock -
> > tick))/CLOCKS_PER_SEC);
> >
> > return 0;
> > }
> >
> >
> > Let's give it a whirl
> >
> > malcolm@Malcolms-iMac cscratch % gcc -O3 testfloodfill.c
> > malcolm@Malcolms-iMac cscratch % ./a.out
> > floodfill_r 1.69274
> > floodfill4 0.336705
> >
> >
>
> I find your performance measurement non-decisive for two reasons:
> (1) because your test case is too trivial and probably
> uncharacteristic and
> (2) because recursive variant could be trivially rewritten in a way
> that reduces # of stack memory accesses by factor of 2 or 3.
> Like that:
>
> struct recursive_context_t {
> unsigned char *grey;
> int width, height;
> unsigned char target, dest;
> };
>
> static void floodfill_r_core(const struct recursive_context_t*
> context, int x, int y) {
> if (x < 0 || x >= context->width || y < 0 || y >= context->height)
> return;
> if (context->grey[y*context->width+x] == context->target) {
> context->grey[y*context->width+x] = context->dest;
> floodfill_r_core(context, x - 1, y);
> floodfill_r_core(context, x + 1, y);
> floodfill_r_core(context, x, y - 1);
> floodfill_r_core(context, x, y + 1);
> }
> }
>
> int floodfill_r(
> unsigned char *grey,
> int width, int height,
> int x, int y,
> unsigned char target, unsigned char dest)
> {
> if (x < 0 || x >= width || y < 0 || y >= height)
> return 0;
> if (grey[y*width+x] != target)
> return 0;
> struct recursive_context_t context = {
> .grey = grey,
> .width = width,
> .height = height,
> .target = target,
> .dest = dest,
> };
> floodfill_r_core(&context, x, y);
> return 1;
> }
>
>


Click here to read the complete article
Re: filling area by color atack safety

<ut7co3$3lmeo$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34465&group=comp.lang.c#34465

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 18:28:17 +0000
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <ut7co3$3lmeo$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me> <ut4cnc$2ut2t$1@dont-email.me>
<ut76ms$3kal7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 18:28:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="1302203e67d64edb589b5f20956e00a5";
logging-data="3856856"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NjjRhv8VjI6LftK5wBa58MkrhlCongik="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:iToTF3gyCglBSXr+9ghzRcAwiKU=
Content-Language: en-GB
In-Reply-To: <ut76ms$3kal7$1@dont-email.me>
 by: Malcolm McLean - Sun, 17 Mar 2024 18:28 UTC

On 17/03/2024 16:45, David Brown wrote:
> On 16/03/2024 16:09, Malcolm McLean wrote:
>>
> The OP's code is simple and obvious, as is its correctness (assuming
> reasonable definitions of the pixel access and setting functions) and
> its time and space requirements.  Yours is not.
>
Except it is not. You didn't give the right answer for the space
requirements.
> Your algorithm could be used in a proper implementation, with separate
> functions to handle the different parts (such as the stack).  The
> algorithm itself is not bad, it's the implementation that is the main
> problem.
>
It's better to have one function. Subroutines have a way of getting lost.>
> I have no idea if your code is "out of date" or not.  It seems to be
> written for images consisting of unsigned chars, so I a not sure it was
> ever designed for real-world images.
>
It was written a long time ago. But it is writeen in a conservative
subset of ANSI C, and so of course it still works, and should work for
along time to come. But the 256 integer queue tweak might be out of
date. And cache use is far more important now that it was on big
processors. So it might be a bit long in the tooth.

And it's part of the binary image library, and it's designed for marking
8- or 4- connected sections of those images by setting the 1 to a
different value. And then further processing. The binary images are
often derived from photographs by Otsu thresholding, which is in the
same library. But they aren't usually meant for human viewing by end users.
>
> I don't know if it is fair to call them a /lot/ more advanced, but
> certainly a bit more advanced.  And certainly better implementations are
> possible.
>
And are you going to be constructive or not? Suggest one which might be
better? Even implement it?

--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: filling area by color atack safety

<ut7j8h$3mt70$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34466&group=comp.lang.c#34466

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 13:19:29 -0700
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <ut7j8h$3mt70$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<87wmq2jn7s.fsf@bsb.me.uk> <ut4ba7$2uits$1@dont-email.me>
<SqlJN.144025$CYpe.59290@fx40.iad> <ut4tsj$3291j$1@dont-email.me>
<ut4vf6$32n1u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 17 Mar 2024 20:19:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fa1fc55c573ae485c64f06b83fc54ff2";
logging-data="3896544"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184A6jGx53WvYRf7MdcfvzhDm00CbUAP4k="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Lm/FMprgSmlBUfnx00Iecudy+bs=
Content-Language: en-US
In-Reply-To: <ut4vf6$32n1u$1@dont-email.me>
 by: Chris M. Thomasson - Sun, 17 Mar 2024 20:19 UTC

On 3/16/2024 1:29 PM, Chris M. Thomasson wrote:
> On 3/16/2024 1:02 PM, Malcolm McLean wrote:
>> On 16/03/2024 18:21, Scott Lurndal wrote:
>>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>>>> On 16/03/2024 13:55, Ben Bacarisse wrote:
>>>>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>>>>>
>>>>>> Recursion make programs harder to reason about and prove correct.
>>>>>
>>>>> Are you prepared to offer any evidence to support this astonishing
>>>>> statement or can we just assume it's another Malcolmism?
>>>>>
>>>>
>>>> Example given. A recursive algorithm which is hard to reason about and
>>>
>>> Perhaps hard for _you_ to reason about.  That doesn't
>>> generalize to every other programmer that might read that
>>> code.
>>>
>>>
>>  From experience this one blows the stack, but not always. Sometimes
>> it's OK to use.
>
> Blowing the stack is not good at all. However, sometimes, I consider a
> recursive algorithm easier to understand. So, I build it first... Get it
> working, _then_ think about an iterative solution...

Gaining the iterative solution from a working recursive solution is the
fun part!

:^)

>
>
>>
>> Since you can reason about it so easily, you can tell the others when
>> you're OK and when you are not, in a handy intuitive way so that
>> someone thinking of implementing it will know.
>>
>

Re: filling area by color atack safety

<87y1agik0j.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34468&group=comp.lang.c#34468

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 22:14:04 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <87y1agik0j.fsf@bsb.me.uk>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<20240317144625.00002011@yahoo.com> <ut6p67$3h8t0$2@dont-email.me>
<20240317151519.000057d0@yahoo.com> <ut6qt8$3hnat$1@dont-email.me>
<877ci1j6ew.fsf@bsb.me.uk> <qMTIrtMGRLIELXCKB@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="664dca1fae3f1391a07afac504c68131";
logging-data="3953979"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9BC66xkM5gIxnzokqcjes2WNc6IMrgUg="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:Fpt37LcwL3nz4ob4Waeo+YsBPYs=
sha1:EVnTWyTZzt9C8roLg6QRfkzRk5k=
X-BSB-Auth: 1.24389cfdf174207aac6e.20240317221404GMT.87y1agik0j.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 17 Mar 2024 22:14 UTC

Spiros Bousbouras <spibou@gmail.com> writes:

> On Sun, 17 Mar 2024 14:10:15 +0000
> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> bart <bc@freeuk.com> writes:
>>
>> > His algorithm is the same as that presented in my textbook, where it is
>> > called FloodFill4.
>>
>> s/my/his/?
>
> What is mentioned in <ut4v4r$32mgb$1@dont-email.me> : "I've just looked in
> my Computer Graphics Principles and Practice book" .

That context seems to have got lost, and MM was quoting from his
textbook (or book at any rate). Thanks for pointing out the missing
context.

--
Ben.

Re: filling area by color atack safety

<ut7qd5$3oti7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34469&group=comp.lang.c#34469

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Sun, 17 Mar 2024 22:21:28 +0000
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <ut7qd5$3oti7$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<20240317144625.00002011@yahoo.com> <ut6p67$3h8t0$2@dont-email.me>
<20240317151519.000057d0@yahoo.com> <ut6qt8$3hnat$1@dont-email.me>
<877ci1j6ew.fsf@bsb.me.uk> <qMTIrtMGRLIELXCKB@bongo-ra.co>
<87y1agik0j.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 17 Mar 2024 22:21:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="86cdd72051d773a92e0c8f332750da77";
logging-data="3962439"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198+ys0IKsbJLAWDIEduv63"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JA7+3TV4GSvoQnm8phEovMWYTLE=
In-Reply-To: <87y1agik0j.fsf@bsb.me.uk>
Content-Language: en-GB
 by: bart - Sun, 17 Mar 2024 22:21 UTC

On 17/03/2024 22:14, Ben Bacarisse wrote:
> Spiros Bousbouras <spibou@gmail.com> writes:
>
>> On Sun, 17 Mar 2024 14:10:15 +0000
>> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>>> bart <bc@freeuk.com> writes:
>>>
>>>> His algorithm is the same as that presented in my textbook, where it is
>>>> called FloodFill4.
>>>
>>> s/my/his/?
>>
>> What is mentioned in <ut4v4r$32mgb$1@dont-email.me> : "I've just looked in
>> my Computer Graphics Principles and Practice book" .
>
> That context seems to have got lost, and MM was quoting from his
> textbook (or book at any rate). Thanks for pointing out the missing
> context.
>

'His' algorithm was the one in the OP posted by fir.

'My' (bart's) textbook was the CGPP book by Foley, van Dam, et al.

Re: filling area by color atack safety

<ut8on5$200c$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34471&group=comp.lang.c#34471

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 07:58:44 +0100
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <ut8on5$200c$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me> <ut4cnc$2ut2t$1@dont-email.me>
<ut76ms$3kal7$1@dont-email.me> <ut7co3$3lmeo$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 18 Mar 2024 06:58:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a84dbbe7de3aa075fbf411563c6ab90e";
logging-data="65548"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/U4X2ZJktT5pqP9lM+t0HBm8U9S5smxTo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JBbhILPs+BjClcHLpCAw2duRb/0=
Content-Language: en-GB
In-Reply-To: <ut7co3$3lmeo$1@dont-email.me>
 by: David Brown - Mon, 18 Mar 2024 06:58 UTC

On 17/03/2024 19:28, Malcolm McLean wrote:
> On 17/03/2024 16:45, David Brown wrote:
>> On 16/03/2024 16:09, Malcolm McLean wrote:
>>>
>> The OP's code is simple and obvious, as is its correctness (assuming
>> reasonable definitions of the pixel access and setting functions) and
>> its time and space requirements.  Yours is not.
>>
> Except it is not. You didn't give the right answer for the space
> requirements.

Unfortunately, I am still fallible - /easier/ does not mean I'll get it
right :-( And I apologise for unhelpfully rushing that and getting it
wrong.

However, I stand by my claim that the recursive version is much easier
to analyse.

>> Your algorithm could be used in a proper implementation, with separate
>> functions to handle the different parts (such as the stack).  The
>> algorithm itself is not bad, it's the implementation that is the main
>> problem.
>>
> It's better to have one function. Subroutines have a way of getting lost.>

Seriously? "Subroutines get lost" ? So your answer is to put all your
ideas in a mixer and scrunch them up until any semblance of logic and
structure is lost, and the code works (if it does) by trial and error?
And then the whole mess is cut-and-paste duplicated - along with any
subtle bugs it might have - for 8-connected version. And that's better,
in your eyes, than re-using code?

>> I have no idea if your code is "out of date" or not.  It seems to be
>> written for images consisting of unsigned chars, so I a not sure it
>> was ever designed for real-world images.
>>
> It was written a long time ago. But it is writeen in a conservative
> subset of ANSI C, and so of course it still works, and should work for
> along time to come. But the 256 integer queue tweak might be out of
> date. And cache use is far more important now that it was on big
> processors. So it might be a bit long in the tooth.
>

I have been most interested in being able to be sure the algorithm is
correct, rather than considering its absolute (rather than "big O")
efficiency in different systems. It is certainly the case that cache
considerations are more relevant now than they used to be on many
systems. And for working on PC's, you would likely dispense with your
growing stack entirely and simply allocate a queue big enough for every
pixel in the image.

> And it's part of the binary image library, and it's designed for marking
> 8- or 4- connected sections of those images by setting the 1 to a
> different value. And then further processing. The binary images are
> often derived from photographs by Otsu thresholding, which is in the
> same library. But they aren't usually meant for human viewing by end users.
>>
>> I don't know if it is fair to call them a /lot/ more advanced, but
>> certainly a bit more advanced.  And certainly better implementations
>> are possible.
>>
> And are you going to be constructive or not? Suggest one which might be
> better? Even implement it?
>

I suggested separating the code into functions - that is /definitely/
constructive. I suggested using sensible names for parameters and
variables (well, the suggestion was implied by my criticism).

And I am also suggesting now that you allocate a queue that is big
enough for every pixel in the image. Much of what you don't touch of
that space, will probably never be physically allocated by the OS,
depending on page sizes and free memory.

And I would also suggest you drop the requirement for coding in an
ancient tongue, and instead switch to reasonably modern C. Make
abstractions for the types and the access functions - it will make the
code far easier to follow, easier to show correct, and easier to modify
and reuse, without affecting efficiency at run-time.

Re: filling area by color atack safety

<ut91bm$3m2q$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34472&group=comp.lang.c#34472

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 09:26:12 +0000
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <ut91bm$3m2q$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<ut4b09$2uhpm$1@dont-email.me> <ut4cnc$2ut2t$1@dont-email.me>
<ut76ms$3kal7$1@dont-email.me> <ut7co3$3lmeo$1@dont-email.me>
<ut8on5$200c$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 18 Mar 2024 09:26:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0909a5289999e8e117f7896cdedb74a6";
logging-data="120922"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Dvtm0zIau3BeOeYh2T3ij9rBekTJmXUQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hBApdVdImlJOpSgGRYhMqQQ3tbc=
Content-Language: en-GB
In-Reply-To: <ut8on5$200c$1@dont-email.me>
 by: Malcolm McLean - Mon, 18 Mar 2024 09:26 UTC

On 18/03/2024 06:58, David Brown wrote:
> On 17/03/2024 19:28, Malcolm McLean wrote:
>> On 17/03/2024 16:45, David Brown wrote:
>>> On 16/03/2024 16:09, Malcolm McLean wrote:
>>>>
>>> The OP's code is simple and obvious, as is its correctness (assuming
>>> reasonable definitions of the pixel access and setting functions) and
>>> its time and space requirements.  Yours is not.
>>>
>> Except it is not. You didn't give the right answer for the space
>> requirements.
>
> Unfortunately, I am still fallible - /easier/ does not mean I'll get it
> right :-(  And I apologise for unhelpfully rushing that and getting it
> wrong.
>
> However, I stand by my claim that the recursive version is much easier
> to analyse.
>
I this case it s very short code and easy to see that it is right, so a
win for recursion. Except that it is only right if the stack is bigger
than N/2 calls deep, where N is the number fo pixels in the image. Now a
100x100 woked fine an my machine - I just checked the main stack, and
it's 8MB by default. BUt of cuuurse the bigger than machine, the bigger
the image th euser might want to load.

>> It's better to have one function. Subroutines have a way of getting
>> lost.>
>
> Seriously?  "Subroutines get lost" ?  So your answer is to put all your
> ideas in a mixer and scrunch them up until any semblance of logic and
> structure is lost, and the code works (if it does) by trial and error?
> And then the whole mess is cut-and-paste duplicated - along with any
> subtle bugs it might have - for 8-connected version.  And that's better,
> in your eyes, than re-using code?
>
Exactly. If a routine ia leaf, you can cut and paste it and use it where
you will. If you have to take subroutines, you've got to explore the
code to understand what you neeed to take, then you have to out them
somewhere. So it's better to keep routines leaf is possible and fold a
few trivial operations into the code body, even if ideally they would be
subroutines. And I understand these trade offs. >
> I have been most interested in being able to be sure the algorithm is
> correct, rather than considering its absolute (rather than "big O")
> efficiency in different systems.  It is certainly the case that cache
> considerations are more relevant now than they used to be on many
> systems.  And for working on PC's, you would likely dispense with your
> growing stack entirely and simply allocate a queue big enough for every
> pixel in the image.
>
That is an idea. But a bit extravanagant. I'd like to try to work out
how much quue s actually used in typical as well as worst case.
>
> I suggested separating the code into functions - that is /definitely/
> constructive.  I suggested using sensible names for parameters and
> variables (well, the suggestion was implied by my criticism).
>
> And I am also suggesting now that you allocate a queue that is big
> enough for every pixel in the image.  Much of what you don't touch of
> that space, will probably never be physically allocated by the OS,
> depending on page sizes and free memory.
>
> And I would also suggest you drop the requirement for coding in an
> ancient tongue, and instead switch to reasonably modern C.  Make
> abstractions for the types and the access functions - it will make the
> code far easier to follow, easier to show correct, and easier to modify
> and reuse, without affecting efficiency at run-time.
>
>
And of course the entire binary image library has a consistent style.
And we don't want the user mee=ssing about with writing his own getpixel
/ setpixel functions, thouhg there would be a case for that for a
geneeral purpose flood fill.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: filling area by color atack safety

<86y1afopik.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34473&group=comp.lang.c#34473

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 02:30:59 -0700
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <86y1afopik.fsf@linuxsc.com>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me> <20240317144625.00002011@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="392b353a6389bc0498ba35815d55dc9d";
logging-data="119735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18n6GJVOZ+/Jc8Svom5baMw0NgIWcPwG/Y="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:ELUpQqGAtBo8oGFg+KfXfziM/Ro=
sha1:ES2PPKvH5Q//VM3PrEz0pdGECdE=
 by: Tim Rentsch - Mon, 18 Mar 2024 09:30 UTC

Michael S <already5chosen@yahoo.com> writes:

>> On 16/03/2024 04:11, fir wrote:
>>
>>> i was writing simple editor (something like paint but more custom
>>> for my eventual needs) for big pixel (low resolution) drawing
>>>
>>> it showed in a minute i need a click for changing given drawed area
>>> of of one color into another color (becouse if no someone would
>>> need to do it by hand pixel by pixel and the need to change color
>>> of given element is very common)
>>>
>>> there is very simple method of doing it - i men i click in given
>>> color pixel then replace it by my color and call the same function
>>> on adjacent 4 pixels (only need check if it is in screen at all and
>>> if the color to change is that initial color
>>>
>>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned
>>> old_color, unsigned new_color)
>>> {
>>> if(old_color == new_color) return 0;
>>>
>>> if(XYIsInScreen( x, y))
>>> if(GetPixelUnsafe(x,y)==old_color)
>>> {
>>> SetPixelSafe(x,y,new_color);
>>> RecolorizePixelAndAdjacentOnes(x+1, y, old_color, new_color);
>>> RecolorizePixelAndAdjacentOnes(x-1, y, old_color, new_color);
>>> RecolorizePixelAndAdjacentOnes(x, y-1, old_color, new_color);
>>> RecolorizePixelAndAdjacentOnes(x, y+1, old_color, new_color);
>>> return 1;
>>> }
>>>
>>> return 0;
>>> }

[...]

> Except I don't understand why it works it all.
> Can't fill area have sub-areas that only connected through diagonal?

It is customary in raster graphics to count pixels as adjacent
only if they share an edge, not if they just share a corner.
Usually that gives better results; the exceptions tend to need
special handling anyway and not just connecting through
diagonals.

Re: filling area by color atack safety

<86ttl3oo5b.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34474&group=comp.lang.c#34474

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 03:00:32 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <86ttl3oo5b.fsf@linuxsc.com>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me> <87wmq2jn7s.fsf@bsb.me.uk> <ut4b3c$2ugk7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="392b353a6389bc0498ba35815d55dc9d";
logging-data="134046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199feCDjwqOgc40mFl1TNW6+AehyJEFMK0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:wvXpiIKDilAyRWcp9RSowgDkCus=
sha1:b38h+oSXfXf2R0oNBqDCPL/PK9Q=
 by: Tim Rentsch - Mon, 18 Mar 2024 10:00 UTC

bart <bc@freeuk.com> writes:

> On 16/03/2024 13:55, Ben Bacarisse wrote:
>
>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
>>
>>> Recursion make programs harder to reason about and prove correct.
>>
>> Are you prepared to offer any evidence to support this astonishing
>> statement or can we just assume it's another Malcolmism?
>
> You have evidence to suggest that the opposite is true?

The claim is that recursion always makes programs harder to reason
about and prove correct. It's easy to find examples that show
recursion does not always makes programs harder to reason about and
prove correct.

> I personally find recursion hard work and errors much harder to
> debug.

Most likely that's because you haven't had the relevant background
in learning how to program in a functional style. That matches my
own experience: it was only after learning how to write programs in
a functional style that I really started to appreciate the benefits
of using recursion, and to understand how to write and reason about
recursive programs.

> It is also becomes much more important to show that will not cause
> stack overflow.

In most cases it's enough to show that the stack depth never exceeds
log N for an input of size N. I use recursion quite routinely
without there being any significant danger of stack overflow. It's
a matter of learning which patterns are safe and which patterns are
potentially dangerous, and avoiding the dangerous patterns (unless
certain guarantees can be made to make them safe again).

Re: filling area by color atack safety

<86plvronxy.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34475&group=comp.lang.c#34475

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 03:04:57 -0700
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <86plvronxy.fsf@linuxsc.com>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me> <87wmq2jn7s.fsf@bsb.me.uk> <ut4b3c$2ugk7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="392b353a6389bc0498ba35815d55dc9d";
logging-data="134046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19KEH0HNgQ8d2mmSTww/huInUQ2pNCB2rU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:VlRMKEySe9wl7E/dmACY5GDgaNQ=
sha1:ru5wpp5zcR8tyQ0yvND4VBszNgI=
 by: Tim Rentsch - Mon, 18 Mar 2024 10:04 UTC

bart <bc@freeuk.com> writes:

P.S. You deserve credit for pointing out that the worst case
for flood fill is changing the color of the entire pixel
field. Maybe it was obvious to other people but I appreciate
it.

Re: filling area by color atack safety

<ut97c4$4rqf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34476&group=comp.lang.c#34476

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 11:08:52 +0000
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <ut97c4$4rqf$1@dont-email.me>
References: <ut3669$21eur$1@i2pn2.org> <ut4020$2s8ov$1@dont-email.me>
<20240317144625.00002011@yahoo.com> <86y1afopik.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 18 Mar 2024 11:08:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0909a5289999e8e117f7896cdedb74a6";
logging-data="159567"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wZJRnYFEUdkHzg2vK1jCKxnbeXCRBfy8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gYReohKE+GMWvqbNOTdjYlWYvsA=
In-Reply-To: <86y1afopik.fsf@linuxsc.com>
Content-Language: en-GB
 by: Malcolm McLean - Mon, 18 Mar 2024 11:08 UTC

On 18/03/2024 09:30, Tim Rentsch wrote:
> Michael S <already5chosen@yahoo.com> writes:
>
>>> On 16/03/2024 04:11, fir wrote:
>>>
>>>> i was writing simple editor (something like paint but more custom
>>>> for my eventual needs) for big pixel (low resolution) drawing
>>>>
>>>> it showed in a minute i need a click for changing given drawed area
>>>> of of one color into another color (becouse if no someone would
>>>> need to do it by hand pixel by pixel and the need to change color
>>>> of given element is very common)
>>>>
>>>> there is very simple method of doing it - i men i click in given
>>>> color pixel then replace it by my color and call the same function
>>>> on adjacent 4 pixels (only need check if it is in screen at all and
>>>> if the color to change is that initial color
>>>>
>>>> int RecolorizePixelAndAdjacentOnes(int x, int y, unsigned
>>>> old_color, unsigned new_color)
>>>> {
>>>> if(old_color == new_color) return 0;
>>>>
>>>> if(XYIsInScreen( x, y))
>>>> if(GetPixelUnsafe(x,y)==old_color)
>>>> {
>>>> SetPixelSafe(x,y,new_color);
>>>> RecolorizePixelAndAdjacentOnes(x+1, y, old_color, new_color);
>>>> RecolorizePixelAndAdjacentOnes(x-1, y, old_color, new_color);
>>>> RecolorizePixelAndAdjacentOnes(x, y-1, old_color, new_color);
>>>> RecolorizePixelAndAdjacentOnes(x, y+1, old_color, new_color);
>>>> return 1;
>>>> }
>>>>
>>>> return 0;
>>>> }
>
> [...]
>
>> Except I don't understand why it works it all.
>> Can't fill area have sub-areas that only connected through diagonal?
>
> It is customary in raster graphics to count pixels as adjacent
> only if they share an edge, not if they just share a corner.
> Usually that gives better results; the exceptions tend to need
> special handling anyway and not just connecting through
> diagonals.
>
Though with a binary image, if the foreground is 4-connected, the
background must therefore be 8-connected.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: filling area by color atack safety

<20240318142351.00001849@yahoo.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34477&group=comp.lang.c#34477

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: filling area by color atack safety
Date: Mon, 18 Mar 2024 14:23:51 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <20240318142351.00001849@yahoo.com>
References: <ut3669$21eur$1@i2pn2.org>
<ut4020$2s8ov$1@dont-email.me>
<87wmq2jn7s.fsf@bsb.me.uk>
<ut4b3c$2ugk7$1@dont-email.me>
<86ttl3oo5b.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: dont-email.me; posting-host="e438cc7fdcf3a4669168787da7dbc3da";
logging-data="3825776"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18m4njrRe+3YiuWGKi+tkThYJVimxDbCKI="
Cancel-Lock: sha1:hoYLuR7Cinc/0of9eDpl29CALHk=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Mon, 18 Mar 2024 12:23 UTC

On Mon, 18 Mar 2024 03:00:32 -0700
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

> bart <bc@freeuk.com> writes:
>
> > On 16/03/2024 13:55, Ben Bacarisse wrote:
> >
> >> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> >>
> >>> Recursion make programs harder to reason about and prove correct.
> >>>
> >>
> >> Are you prepared to offer any evidence to support this astonishing
> >> statement or can we just assume it's another Malcolmism?
> >
> > You have evidence to suggest that the opposite is true?
>
> The claim is that recursion always makes programs harder to reason
> about and prove correct. It's easy to find examples that show
> recursion does not always makes programs harder to reason about and
> prove correct.
>
> > I personally find recursion hard work and errors much harder to
> > debug.
>
> Most likely that's because you haven't had the relevant background
> in learning how to program in a functional style. That matches my
> own experience: it was only after learning how to write programs in
> a functional style that I really started to appreciate the benefits
> of using recursion, and to understand how to write and reason about
> recursive programs.
>
> > It is also becomes much more important to show that will not cause
> > stack overflow.
>
> In most cases it's enough to show that the stack depth never exceeds
> log N for an input of size N. I use recursion quite routinely
> without there being any significant danger of stack overflow. It's
> a matter of learning which patterns are safe and which patterns are
> potentially dangerous, and avoiding the dangerous patterns (unless
> certain guarantees can be made to make them safe again).

The problem in this case is that max. depth of recursion is O(N) where N
is total number of pixels to change color. So far I didn't find an
obvious way to cut the worst case by more than small factor without
turning recursive algorithm into something that is unrecognizably
different from original and require proof of correction of its own.
Classic 'divide and conquer smaller part first" strategy does not
appear applicable here, or at least not obviously.


devel / comp.lang.c / Re: filling area by color atack safety

Pages:1234567
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor