Jump to content

Basic Codelab Coding Help Needed!


Recommended Posts

Seasons Greetings!

I am trying my luck in codelab, and I'm trying to make a simple effect that detects if the adjacent pixels are transparent or not.

No matter what I do, codelab runs the code fine, but it has no effect whatsoever on the layer I'm applying it to. :x

I think I must be missing something plainly obvious, or something very subtle. I've read through the forums, checked I'm saving and building correctly, and making sure my layer has random opaque squiggles all over it (surrounded by transparency).

Any ideas what I'm doing wrong? Please?

#region UICode
int Amount1=0;    //[0,100]Slider 1 Description
int Amount2=0;    //[0,100]Slider 2 Description
int Amount3=0;    //[0,100]Slider 3 Description
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
   ...
   ColorBgra CurrentPixel;
   ColorBgra LeftPixel;
   ColorBgra RightPixel;
   ColorBgra UpPixel;
   ColorBgra DownPixel;
   int PixelCat;    
   for(int y = rect.Top + 1; y < rect.Bottom - 1; y++)
   {
       for (int x = rect.Left + 1; x < rect.Right - 1; x++)
       {
           // Here, we're grabbing the BGRA values from src[x,y] and it's neighbours.
           CurrentPixel = src[x,y];
           LeftPixel = src[x - 1,y];            
           RightPixel = src[x + 1,y];            
           UpPixel = src[x,y - 1];            
           DownPixel = src[x,y + 1];            
           PixelCat = 0; // This variable is reset to 0 for each pixel render.

           // Here, we add 1 to PixelCat for each of the adjacent pixels that have Alpha over 0.
           if (LeftPixel.A > 0) PixelCat++;
           if (RightPixel.A > 0) PixelCat++;
           if (UpPixel.A > 0) PixelCat++;
           if (DownPixel.A > 0) PixelCat++;

           // Here, we... erm... recompile?... values of transparency, yellow, or purple back to CurrentPixel.
           if (PixelCat == 0) CurrentPixel = ColorBgra.FromBgra(0,0,0,0);
           if (PixelCat == 1) CurrentPixel = ColorBgra.FromBgra(0,255,255,255);
           if (PixelCat > 1) CurrentPixel = ColorBgra.FromBgra(255,0,255,255);

           dst[x,y] = CurrentPixel;
       }
   }
}

Any help is appreciated!

[edit]

Uuuurrghhh! The quote function removed the space before the beginning of each line! :x

I know these are correct, so please assume so if possible. Thanks.

Link to comment
Share on other sites

First, use [code] tags instead of [quote] tags. That will fix the indent problem. (I edited your message for you.)

Next, your problem is probably here:

for(int y = rect.Top + 1; y 

If you look at CodeLab Effects Design Overview, you will see that your Render function is usually called with very small rectangles (1 or 2 lines tall). By adding 1 to the top and subtracting 1 from the bottom, your loop variables are given nothing to loop through!

To give you an idea on how you can solve your problem, read this script that I wrote: viewtopic.php?p=179496#p179496 Everything you need to solve your problem is there.

Enjoy. 8)

Click to play:
j.pngs.pngd.pnga.pngp.png
Download: BoltBait's Plugin Pack | CodeLab | and how about a Computer Dominos Game

Link to comment
Share on other sites

First, use [code] tags instead of [quote] tags. That will fix the indent problem. (I edited your message for you.)

Next, your problem is probably here:

for(int y = rect.Top + 1; y < rect.Bottom - 1; y++)

If you look at CodeLab Effects Design Overview, you will see that your Render function is usually called with very small rectangles (1 or 2 lines tall). By adding 1 to the top and selecting 1 from the bottom, your loop variables are given nothing to loop through!

To give you an idea on how you can solve your problem, read this script that I wrote: viewtopic.php?p=179496#p179496 Everything you need to solve your problem is there.

Enjoy. 8)

Wow! Thank you! :mrgreen:

Erm... I don't mean to be a pain, but in the code you linked to ("Outline"), I can't see where the code checks to see if the neighbouring pixels are transparent or not. Can you point me to the line that does this please?

Once again, sorry to be a pain! :oops:

[edit]

Scratch that! I was looking at the wrong code! :shock: :x :roll:

Link to comment
Share on other sites

@Boltbait (or any other kind expert)

Firstly, thanks again.

There's been some great progress. I managed to chop parts from the thread you linked to and put something together that worked. Well... produced a result.

Here is the revised code as it stands:

#region UICode
//int Amount1=0;    //[0,100]Slider 1 Description
//int Amount2=0;    //[0,100]Slider 2 Description
//int Amount3=0;    //[0,100]Slider 3 Description
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
   PdnRegion selectionRegion = EnvironmentParameters.GetSelection(src.Bounds);
   ColorBgra CurrentPixel;
   int PixelCat;

   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
       CurrentPixel = src[x,y];
       PixelCat = 0; // This variable is reset to 0 for each pixel render.

       if ( (x>0 && x0 && y        {
           if (selectionRegion.IsVisible(x-1, y)) PixelCat++;
           if (selectionRegion.IsVisible(x, y-1)) PixelCat++;
           if (selectionRegion.IsVisible(x+1, y)) PixelCat++;
           if (selectionRegion.IsVisible(x, y+1)) PixelCat++;

           // Here, we assign values of transparency, yellow, or purple back to CurrentPixel.
           if (PixelCat == 0) CurrentPixel = ColorBgra.FromBgra(0,0,0,0);
           if (PixelCat == 1) CurrentPixel = ColorBgra.FromBgra(0,255,255,255);
           if (PixelCat > 1) CurrentPixel = ColorBgra.FromBgra(255,0,255,255);                
       }                                        
       dst[x,y] = CurrentPixel;
       }
   }
}

The result is that it turns the entire layer purple, except for when either x or y = 0.

I'm pretty sure the problem is

- my if statements are wrong and they are adding 1 to PixelCat regardless of conditions. I tested the "if (PixelCat > 1)..." line and basically every time PixelCat got to those final if statements, it had a value of 4.

or,

- I'm using selectionRegion.IsVisible the wrong way or it is simply the wrong function for this purpose. When I replaced the '!' back in (as in "!selectionRegion.IsVisible"), the whole screen remained black, again, except for when either x or y = 0. I think "if (selectionRegion.IsVisible(...))" is returning true for everything! :shock:

Is there anywhere I can see a list of functions like 'selectionRegion.IsVisible'? Kind of like a 'function encyclopedia', if you see what I mean?

PdN forum search and google yielded very little on this. To be honest I dont even know exactly what 'selectionRegion.IsVisible' does. :?

Boltbait, or any other kind expert in these matters, please shed thy wisdom, I beg of thee!

Thanks in advance.

Just in case you're wondering...

PixelCat = "Pixel Category", not...

PixelCat.jpg

Link to comment
Share on other sites

The selectionRegion.IsVisible function returns true if the pixel is currently selected and false if it is not. Since you ran the function with the entire layer selected, it always returned true.

Now, the important code I was linking you to was src.Height and src.Width so that you can test if you are near the edge of the canvas. These functions return the height and width of the entire canvas, not just the rectangle that your Render function was passed. This will keep you from testing pixels that do not exist. You'll notice that the code I linked you to had checks to see if the pixel existed before it was checked (i.e. "if (m>=src.Height) m=src.Height;" etc.)

Click to play:
j.pngs.pngd.pnga.pngp.png
Download: BoltBait's Plugin Pack | CodeLab | and how about a Computer Dominos Game

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...