Jump to content

CodeLab problem (solved)


Recommended Posts

I was searching for this problem but I didn't found anything like this.

I was trying to create my own plugin - 3D Heightmap. I never programmed in C# but I can program well in Visual Basic (they're similar).

Therefore I downloaded BoltBait's CodeLab. I have CodeLab v1.6 (but I'm not sure if this is latest version) and i'm using PDN v3.5.8.

Code was at first bigger (it had more options) but I made it shorter because it's easier to find a problem in smaller code.

Problem is, that when I open this code in CodeLab and press Build it works fine (even on lasso select!), but when I press Save as Dll then it shows me lot of errors.

How plugin works:

Imagine that you are looking at uneven surface - heightmap (image in PDN). Lightness of each pixel represents it's height (red, green and blue channels are separated). Light is coming from right at 45° angle. Plugin generates how will heightmap look if you will look at it like this. It generates lightness of each pixel by calculating at which angle it is from next (right) pixel and generating lightness of pixel form angle.

Code:

// Name: 3D heightmap
// Author: Radobot
// Submenu: Render
// URL: http://www.getpaint.net/redirect/plugins.html
// Title: 3D Heightmap
#region UICode 
int Amount1 = 10; // [1,127] Multiplyer  (1,127,dft=10)
bool Amount2 = true; // [0,1] Replace Alpha  (checked)
int Amount3 = 255; // [0,255] Replace Alpha with  (0,255,dft=255)
#endregion 

void Render(Surface dst, Surface src, Rectangle rect) 
{ 
   int differenceR;   // Define some variables
   int differenceG;   // Integer is fine because I need to store in variables numbers from -255 to +255
   int differenceB; 
   int finalR; 
   int finalG; 
   int finalB; 
   ColorBgra CurrentPixel; 
   ColorBgra NextPixel;      // CurrentPixel's right neighbor (one pixel to right from CurrentPixel)
   for(int y = rect.Top; y < rect.Bottom; y++) 
   { 
       for (int x = rect.Left; x < rect.Right; x++) 
       { 
           CurrentPixel = src[x,y]; 
           if (x == rect.Right - 1)    // If we are at rightmost pixel = (If I can use "x + 1")
           { 
           NextPixel = src[x,y];   	// We are at rightmost pixel - "x + 1" doesn't exist -> we will simulate that "x + 1 = x" -> NextPixel = CurrentPixel
           } 
           else 
           { 
           NextPixel = src[x + 1,y];   // We aren't at rightmost pixel - I can use "x + 1"
           } 
           differenceR = NextPixel.R - CurrentPixel.R;   // Count difference NextPixel - CurrentPixel
           differenceG = NextPixel.G - CurrentPixel.G; 
           differenceB = NextPixel.B - CurrentPixel.B; 
           finalR = (differenceR * Amount1) + 128;   	// Count wich color will final pixel have
           finalG = (differenceG * Amount1) + 128; 
           finalB = (differenceB * Amount1) + 128; 
           if (finalR < 0) {finalR = 0;};   	// If color < 0 then set color to 0
           if (finalR > 255) {finalR = 255;};   // If color > 255 then set color to 255
           if (finalG < 0) {finalG = 0;}; 
           if (finalG > 255) {finalG = 255;}; 
           if (finalB < 0) {finalB = 0;}; 
           if (finalB > 255) {finalB = 255;}; 
           CurrentPixel.R = (Byte)finalR;   	// Set final color
           CurrentPixel.G = (Byte)finalG; 
           CurrentPixel.B = (Byte)finalB; 
           if (Amount2 == true) 				// If checkbox "Replace Alpha" is checked
           { 
           CurrentPixel.A = (byte)Amount3;      // Replace Alpha with entered value
           } 
           dst[x,y] = CurrentPixel; 			// Apply color
       } 
   } 
}

post-81250-129942416617_thumb.png

Image before effect

post-81250-129942417351_thumb.png

Image after effect

And another strange thing is that CodeLab slows down whole PDN.

Sorry for my bad english.

Edited by Radobot
Link to comment
Share on other sites

I think it can have something to do with Visual Studio 2010 which I have installed.

List of errors:

Error at line 25: Identifier expected (CS1001)

Error at line 70: Identifier expected (CS1001)

Error at line 70: } expected (CS1513)

Error at line 72: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 80: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 88: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 101: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 103: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 105: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 106: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 107: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 109: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 112: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 124: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 137: Expected class, delegate, enum, interface, or struct (CS1518)

Error at line 139: Type or namespace definition, or end-of-file expected (CS1022)

Error at line 137: The namespace '<global namespace>' already contains a definition for '?' (CS0101)

Link to comment
Share on other sites

This series of IF statements:

if (finalR < 0) {finalR = 0;}; // If color < 0 then set color to 0

if (finalR > 255) {finalR = 255;}; // If color > 255 then set color to 255

if (finalG < 0) {finalG = 0;};

if (finalG > 255) {finalG = 255;};

if (finalB < 0) {finalB = 0;};

if (finalB > 255) {finalB = 255;};

You can do these more cleanly with a call to Int32Util.ClampToByte. EG

finalR = Int32Util.ClampToByte((differenceR * Amount1) + 128);

These lines:

CurrentPixel.R = (Byte)finalR; // Set final color

CurrentPixel.G = (Byte)finalG;

CurrentPixel.B = (Byte)finalB;

Should have a lowercase B for byte (C# is case sensitive).

Link to comment
Share on other sites

I got the same errors.... until I renamed the file! :D

Rename the file (lose the '3D' from the front of the filename) and see if it works for you. This is what I compiled (simplified version of your original code)

// Name: Radobot's 3D Heightmap
// Author: Radobot
// Submenu: Render
// URL: http://www.getpaint.net/redirect/plugins.html
// Title: Radobot's 3D Heightmap

#region UICode
int Amount1 = 10; // [1,127] Multiplier
bool Amount2 = true; // [0,1] Replace Alpha
int Amount3 = 255; // [0,255] Replace Alpha with
#endregion 

void Render(Surface dst, Surface src, Rectangle rect)
{
int finalR, finalG, finalB;
ColorBgra CurrentPixel, NextPixel; 

for(int y = rect.Top; y < rect.Bottom; y++)
	{
		for (int x = rect.Left; x < rect.Right; x++)
		{
			CurrentPixel = src[x,y];
			if (x == rect.Right - 1)	
			{
				NextPixel = src[x,y]; 	
			}
			else
			{
				NextPixel = src[x + 1,y]; 
			} 
			finalR = Int32Util.ClampToByte( (NextPixel.R - CurrentPixel.R) * Amount1 + 128 );
			finalG = Int32Util.ClampToByte( (NextPixel.G - CurrentPixel.G) * Amount1 + 128 );
			finalB = Int32Util.ClampToByte( (NextPixel.B - CurrentPixel. * Amount1 + 128 );

			CurrentPixel.R = (byte) finalR;
			CurrentPixel.G = (byte) finalG;
			CurrentPixel.B = (byte) finalB;
			if (Amount2) CurrentPixel.A = (byte) Amount3; 	
			dst[x,y] = CurrentPixel;
		}
	}
}

Link to comment
Share on other sites

Thank you very much for cleaning my code and making it even shorter :D! I originally copied this code to CodeLab form my Visual Basic application and then optimized for C#, so there's capital "B" because Visual Studio is changing it itself.

I put it to Render submenu because I didn't know where to put it (but now it's in Stylize submenu). So I renamed it to "Heightmap", add icon and it works fine!

P.S. I will add it to Plugins - Publishing Only section, so you can then add it to the list :). And I added finished version (I don't know why, maybe to show you finished product).

Heightmap.zip

Link to comment
Share on other sites

Tis a nice effect as BoltBait said. Great job on your first plugin!

If you wanted to simplify it even more, you could probably get rid of the three finalR type variables;

finalR = Int32Util.ClampToByte( (NextPixel.R - CurrentPixel.R) * Amount1 + 128 );

CurrentPixel.R = (byte) finalR;

could become:

CurrentPixel.R = Int32Util.ClampToByte( (NextPixel.R - CurrentPixel.R) * Amount1 + 128 ); // haven't tried this - may/may not need the cast to (byte)!

I like to make code readable too, so if you find these changes less readable, ignore them!

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...