Sign in to follow this  
jsonchiu

Diagonal Lines Plugin [v2.5, September 13th, 2009]

Recommended Posts

I've been searching for a plugin that makes diagonal lines for a long time...
Since I couldn't, I decided that maybe I should make one myself!
It turned out to be a lot easier than I thought, and here it is!

Link appears to be broken (21 Feb 2013) >> Plugin file: http://jason.jjtchiu.com/downloads/Diagonal%20Lines.dll

 

Plugin recompiled by EER and attached to the bottom of this post.

The plugin will be placed under Effects -> Render

source:

#region UICode
int Amount1 = 60; // [0,500] Interval
int Amount2 = 1; // [0,500] Brush Width
int Amount3 = 6; // [0,10] Anti-alias Level
int Amount4 = 45; // [0,360] Angle
bool Amount5 = true; // [0,1] Use Primary Color
ColorBgra Amount6 = ColorBgra.FromBgr(0,0,0); // Color
int Amount7 = 255; // [0,255] Alpha
#endregion

ColorBgra multiplyRGB(ColorBgra CurrentPixel, ColorBgra PrimaryColor,
double mul, double div)
{
double inv_mul = div - mul;
if (CurrentPixel.A == 0 || Math.Round(255 * mul/div) == 255)
{
CurrentPixel.R = PrimaryColor.R;
CurrentPixel.G = PrimaryColor.G;
CurrentPixel.B = PrimaryColor.B;
}
else
{
CurrentPixel.R = (byte)(((int)(PrimaryColor.R) * mul + (int)(CurrentPixel.R) * inv_mul) / div);
CurrentPixel.G = (byte)(((int)(PrimaryColor.G) * mul + (int)(CurrentPixel.G) * inv_mul) / div);
CurrentPixel.B = (byte)(((int)(PrimaryColor.  * mul + (int)(CurrentPixel.  * inv_mul) / div);
}
return CurrentPixel;
}

int multiplyAlpha(int currentA, int newA)
{
//if no alpha needed to add or if totally opaque
if (newA == 0 || currentA == 255)
{
return currentA;
}
//if current alpha is zero, new alpha would not need adjustment
else if (currentA == 0 || newA == 255)
{
return newA;
}
else if (currentA < 255)
{
currentA += newA * (255 - currentA) / 255;
return currentA;
}
return 0;
}

void Render(Surface dst, Surface src, Rectangle rect)
{
ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
ColorBgra CurrentPixel;
int interval = Amount1;
int bwidth = Amount2;
int aalevel = Amount3;
int angle = Amount4;

if (Amount5 == false)
{
PrimaryColor = Amount6;
PrimaryColor.A = (byte)(Amount7);
}

//modifying my (x + y) % interval Algorithm, we multiply the x and y by certain value
double xfactor = 1;
double yfactor = 1;

//if y is longer than x, we modify y factor, and vice versa
if (angle <= 45 || (angle > 135 && angle < 225) || angle > 315)
{
yfactor = Math.Tan(angle * (3.14159 / 180));
}
else
{
xfactor = 1 / Math.Tan(angle * (3.14159 / 180));
}

for (int y = rect.Top; y < rect.Bottom; y++)
{
for (int x = rect.Left; x < rect.Right; x++)
{
CurrentPixel = src[x, y];
double testnum = (x * xfactor + y * yfactor);
if (testnum < 0)
{
//make the number a positive before doing the modular (%) operation
testnum = testnum + interval * 1000000;
}
double modular = testnum % interval;

//if modular < thickness
if (modular < bwidth)
{
if ((int)(PrimaryColor.A) == 255)
{
CurrentPixel.R = (byte)PrimaryColor.R;
CurrentPixel.G = (byte)PrimaryColor.G;
CurrentPixel.B = (byte)PrimaryColor.B;
CurrentPixel.A = (byte)PrimaryColor.A;
}
else
{
double alpha = (double)(PrimaryColor.A);
CurrentPixel = multiplyRGB(CurrentPixel, PrimaryColor, alpha, 255); //if PrimaryColor.A < 255, multiply colors
CurrentPixel.A = (byte)(multiplyAlpha((int)(CurrentPixel.A), (int)(alpha))); //if CurrentPixel.A < 255 and PrimaryColor.A < 255
}
}
else if (aalevel > 0)
{
//if pixel is beside the line, we do our antialias operation
if (modular <= bwidth + 1 ||
modular >= interval - 1)
{
double factor = 0;
if (modular >= interval - 1)
{
factor = 1 - (modular - interval + 1);
}
else
{
factor = 1 - (bwidth - modular + 1);
}

//hacks to get around special angles
if (angle == 45)
{
factor += 0.8;
}
else if (angle == 90)
{
factor = 0;
}
else if (angle == 135)
{
factor -= 0.7;
}

//our antialias routine
double mul = (double)((10 - aalevel) * factor);
double div = mul + 1;
double alpha = (double)(PrimaryColor.A);
ColorBgra tempPixel = new ColorBgra();

tempPixel = multiplyRGB(CurrentPixel, PrimaryColor, 1, div); //Antialias pixel by factor
CurrentPixel = multiplyRGB(CurrentPixel, tempPixel, (double)(PrimaryColor.A), 255); //if PrimaryColor.A < 255, multiply colors
CurrentPixel.A = (byte)(multiplyAlpha((int)(CurrentPixel.A), (int)(alpha/div))); //if CurrentPixel.A < 255, multiply alpha
}
}


//and we are done
dst[x, y] = CurrentPixel;
}
}
}


Enjoy!

Update1: added brushwidth slide bar.
Update2: added antialias level & menu icon.
Update3: fixed transparency bug.
Update4: added angle slide bar.
Update5: added colorwheel, alpha slide bar, fixed more transparency issues

Diagonal Lines.zip

Share this post


Link to post
Share on other sites

Personaly, I don't like the fact that it uses the brushwidth. I would prefer an extra slider to choose the width. Otherwise you always have to think about changing the width before starting the effect...

But, nice job!

Share this post


Link to post
Share on other sites

Funky plugin, thanks jsonchiu :).

I do have a suggestion though, could an anti-aliasing option and a menu icon be added? Nothing serious, though and so far it's working like a charm. Thanks again.

Share this post


Link to post
Share on other sites
Yeah, but this plugin take options like brush width and exact amount of pixels between lines. :wink:

Neat.

Share this post


Link to post
Share on other sites
Nooooez! Another plug-in in the "Effect" section! *Faints*

Please put it under Render.

Done

Did you use BoltBait's scanline source code for it?

Nope, I wrote the code from scratch.

-------------------------------------------------------------------------------

Update: fixed transparency bug. You can now apply the plugin using a semi-transparent brush color and it will multiply the color to the background just like what it does when you use the line/curve tool.

Share this post


Link to post
Share on other sites

I really wish I had something like this a week ago. Instead, I had to do all the lines by hand with the line tool, which took about 5 minutes, and way not accurate at all with spacing.

Ill certanly be using this a ton in the future. Thanks!

Share this post


Link to post
Share on other sites

snospmiS's userbars (in his signature) and my userbar (in my signature). Then there's LiquidCore's signature which uses diagonal lines, whether with this plugin or not is beside the point. Diagonal lines in general can be used for anything just like Clouds, Gaussian blur or even your Centre Lines plugin, it's all a case of application.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this