Re: filling area by color atack safety

Liste des GroupesRevenir à l c 
Sujet : Re: filling area by color atack safety
De : fir (at) *nospam* grunge.pl (fir)
Groupes : comp.lang.c
Date : 20. Mar 2024, 01:03:04
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <utd5in$2e5ok$1@i2pn2.org>
References : 1 2
User-Agent : Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
Michael S 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;
}
>
>
im not quite sure what you do here.. pass the structure? in fact
the thing you name context you may not pass at all just make is standalone static variables becouse they/it is the same for whole "branch" (given recursive branch of recolorisation)
something like
int old_color = 0xff0000;
int new_color = 0x00ff00;
void RecolorizePixelAndAdjacentPixels(int x, int y)
{
   //...
}

Date Sujet#  Auteur
16 Mar 24 * filling area by color atack safety163fir
16 Mar 24 +* Re: filling area by color atack safety86Malcolm McLean
16 Mar 24 i+* Re: filling area by color atack safety25Ben Bacarisse
16 Mar 24 ii+* Re: filling area by color atack safety11bart
17 Mar 24 iii+- Re: filling area by color atack safety1Ben Bacarisse
18 Mar 24 iii+* Re: filling area by color atack safety8Tim Rentsch
18 Mar 24 iiii`* Re: filling area by color atack safety7Michael S
18 Mar 24 iiii +- Re: filling area by color atack safety1Tim Rentsch
19 Mar 24 iiii `* Re: filling area by color atack safety5fir
19 Mar 24 iiii  `* Re: filling area by color atack safety4bart
19 Mar 24 iiii   +- Re: filling area by color atack safety1bart
20 Mar 24 iiii   +- Re: filling area by color atack safety1fir
20 Mar 24 iiii   `- Re: filling area by color atack safety1David Brown
18 Mar 24 iii`- Re: filling area by color atack safety1Tim Rentsch
16 Mar 24 ii`* Re: filling area by color atack safety13Malcolm McLean
16 Mar 24 ii +* Re: filling area by color atack safety5Malcolm McLean
16 Mar 24 ii i+* Re: filling area by color atack safety3Chris M. Thomasson
17 Mar 24 ii ii`* Re: filling area by color atack safety2Chris M. Thomasson
18 Mar 24 ii ii `- Re: filling area by color atack safety1Michael S
20 Mar 24 ii i`- Re: filling area by color atack safety1fir
17 Mar 24 ii +* Re: filling area by color atack safety6Ben Bacarisse
17 Mar 24 ii i`* Re: filling area by color atack safety5Malcolm McLean
17 Mar 24 ii i +- Re: filling area by color atack safety1Kaz Kylheku
23 Mar 24 ii i +- Re: filling area by color atack safety1Ben Bacarisse
23 Mar 24 ii i `* Re: filling area by color atack safety2Tim Rentsch
29 Mar 24 ii i  `- Re: filling area by color atack safety1Tim Rentsch
17 Mar 24 ii `- Re: filling area by color atack safety1Michael S
16 Mar 24 i+* Re: filling area by color atack safety38David Brown
16 Mar 24 ii+* Re: filling area by color atack safety31Malcolm McLean
17 Mar 24 iii+* Re: filling area by color atack safety16Malcolm McLean
17 Mar 24 iiii+- Re: filling area by color atack safety1Michael S
17 Mar 24 iiii+* Re: filling area by color atack safety13Michael S
17 Mar 24 iiiii+* Re: filling area by color atack safety5Michael S
18 Mar 24 iiiiii`* Re: filling area by color atack safety4Tim Rentsch
18 Mar 24 iiiiii `* Re: filling area by color atack safety3Malcolm McLean
19 Mar 24 iiiiii  `* Re: filling area by color atack safety2Tim Rentsch
19 Mar 24 iiiiii   `- Re: filling area by color atack safety1Malcolm McLean
20 Mar 24 iiiii`* Re: filling area by color atack safety7fir
20 Mar 24 iiiii `* Re: filling area by color atack safety6Michael S
20 Mar 24 iiiii  `* Re: filling area by color atack safety5fir
20 Mar 24 iiiii   `* Re: filling area by color atack safety4Michael S
20 Mar 24 iiiii    `* Re: filling area by color atack safety3fir
20 Mar 24 iiiii     `* Re: filling area by color atack safety2Michael S
20 Mar 24 iiiii      `- Re: filling area by color atack safety1fir
20 Mar 24 iiii`- Re: filling area by color atack safety1fir
17 Mar 24 iii`* Re: filling area by color atack safety14David Brown
17 Mar 24 iii `* Re: filling area by color atack safety13Malcolm McLean
18 Mar 24 iii  `* Re: filling area by color atack safety12David Brown
18 Mar 24 iii   `* Re: filling area by color atack safety11Malcolm McLean
18 Mar 24 iii    `* Re: filling area by color atack safety10David Brown
18 Mar 24 iii     `* Re: filling area by color atack safety9Malcolm McLean
18 Mar 24 iii      +* Re: filling area by color atack safety3Keith Thompson
19 Mar 24 iii      i+- Keith-world (Was: filling area by color atack safety)1Kenny McCormack
19 Mar 24 iii      i`- Re: filling area by color atack safety1David Brown
18 Mar 24 iii      +- Re: filling area by color atack safety1bart
19 Mar 24 iii      +- Re: filling area by color atack safety1Chris M. Thomasson
19 Mar 24 iii      +- Re: filling area by color atack safety1David Brown
19 Mar 24 iii      `* Re: filling area by color atack safety2David Brown
19 Mar 24 iii       `- Re: filling area by color atack safety1Richard Harnden
17 Mar 24 ii+* Re: filling area by color atack safety4Ben Bacarisse
17 Mar 24 iii+* Re: filling area by color atack safety2Malcolm McLean
17 Mar 24 iiii`- Re: filling area by color atack safety1bart
17 Mar 24 iii`- Re: filling area by color atack safety1David Brown
20 Mar 24 ii`* Re: filling area by color atack safety2fir
20 Mar 24 ii `- Re: filling area by color atack safety1fir
17 Mar 24 i+* Re: filling area by color atack safety18Michael S
17 Mar 24 ii+* Re: filling area by color atack safety9bart
17 Mar 24 iii`* Re: filling area by color atack safety8Michael S
17 Mar 24 iii `* Re: filling area by color atack safety7bart
17 Mar 24 iii  +* Re: filling area by color atack safety2Michael S
17 Mar 24 iii  i`- Re: filling area by color atack safety1David Brown
17 Mar 24 iii  `* Re: filling area by color atack safety4Ben Bacarisse
17 Mar 24 iii   `* Re: filling area by color atack safety3Spiros Bousbouras
18 Mar 24 iii    `* Re: filling area by color atack safety2Ben Bacarisse
18 Mar 24 iii     `- Re: filling area by color atack safety1bart
18 Mar 24 ii+* Re: filling area by color atack safety5Tim Rentsch
18 Mar 24 iii`* Re: filling area by color atack safety4Malcolm McLean
19 Mar 24 iii `* Re: filling area by color atack safety3Tim Rentsch
19 Mar 24 iii  `* Re: filling area by color atack safety2Malcolm McLean
20 Mar 24 iii   `- Re: filling area by color atack safety1Tim Rentsch
20 Mar 24 ii`* Re: filling area by color atack safety3fir
20 Mar 24 ii `* Re: filling area by color atack safety2Michael S
20 Mar 24 ii  `- Re: filling area by color atack safety1fir
17 Mar 24 i`* Re: filling area by color atack safety4Lew Pitcher
17 Mar 24 i +- Re: filling area by color atack safety1bart
19 Mar 24 i `* Re: filling area by color atack safety2Peter 'Shaggy' Haywood
20 Mar 24 i  `- Re: filling area by color atack safety1fir
16 Mar 24 +* Re: filling area by color atack safety8bart
16 Mar 24 i+* Re: filling area by color atack safety2bart
16 Mar 24 ii`- Re: filling area by color atack safety1Malcolm McLean
20 Mar 24 i`* Re: filling area by color atack safety5fir
22 Mar 24 i `* Re: filling area by color atack safety4Peter 'Shaggy' Haywood
22 Mar 24 i  +* Re: filling area by color atack safety2Michael S
22 Mar 24 i  i`- Re: filling area by color atack safety1Michael S
23 Mar 24 i  `- Re: filling area by color atack safety1fir
17 Mar 24 +- Re: filling area by color atack safety1Peter 'Shaggy' Haywood
18 Mar 24 `* Re: filling area by color atack safety67Tim Rentsch
19 Mar 24  `* Re: filling area by color atack safety66Tim Rentsch
19 Mar 24   `* Re: filling area by color atack safety65Michael S
19 Mar 24    +* Re: filling area by color atack safety29Malcolm McLean
19 Mar 24    i+* Re: filling area by color atack safety4Michael S
19 Mar 24    i`* Re: filling area by color atack safety24Michael S
20 Mar 24    `* Re: filling area by color atack safety35Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal