Jump to content

How would I fix this? (SOLVED)


Recommended Posts

  

EDIT : Solved.

 

I want to leave this thread open if anyone has any feedback.

 

// Name: Image Modulo Toolkit
// Submenu: Adjustment
// Author: Reptorian
// Title: Image Modulo Toolkit
// Version: 2
// Desc: Adjust image values based on I%(Amount+1)
// Keywords: Modulo
// URL:
// Help:
#region UICode
ListBoxControl<ModuloMode> moduloMode = ModuloMode.Modulo; // Mode | Modulo | Modulo Continuous | Divisive Modulo | Divisive Modulo Continuous | Modulo Addition | Modulo Addition Continuous
IntSliderControl maxNumber = 255; // [1,255] {!ClipboardMode} Maximum Number
CheckboxControl ClipboardMode = false; // Use Clipboard
CheckboxControl Negate = false;// Invert
#endregion

// Aux surface
Surface aux = null;
Surface ModVal = null;

private Surface clipboardSurface = null;
private bool readClipboard = false;

protected override void OnDispose(bool disposing)
{
    if (disposing)
    {
        // Release any surfaces or effects you've created
        if (aux != null) aux.Dispose();
        aux = null;
        if (clipboardSurface != null) clipboardSurface.Dispose();
        clipboardSurface = null;
    }

    base.OnDispose(disposing);
}

// This single-threaded function is called after the UI changes and before the Render function is called
// The purpose is to prepare anything you'll need in the Render function
void PreRender(Surface dst, Surface src)
{
    int R1,G1,B1,R2,G2,B2;
    
    ColorBgra CurrentPixel;
    ColorBgra ClipPixel;
    
    ModVal = new Surface(src.Size);
    
    float DivMultR,DivMultG,DivMultB,DivMultClipR,DivMultClipG,DivMultClipB;
    
    int maxR,maxG,maxB,maxClipR,maxClipG,maxClipB;
    DivMultR = DivMultG = DivMultB = DivMultClipR = DivMultClipG = DivMultClipB = 255f/maxNumber;
    
    maxR = maxG = maxB = maxClipR = maxClipG = maxClipB = maxNumber;
    R1 = G1 = B1 = R2 = G2 = B2 = 255;
    
    if (aux == null)
    {
        aux = new Surface(src.Size);
    }

    if (!readClipboard && ClipboardMode)
    {
        readClipboard = true;
        clipboardSurface = Services.GetService<IClipboardService>().TryGetSurface();
    }
    // Copy from the Clipboard to the aux surface
    for (int y = 0; y < aux.Size.Height; y++)
    {
        if (IsCancelRequested) return;
        for (int x = 0; x < aux.Size.Width; x++)
        {
            CurrentPixel = src[x, y];
            int R = CurrentPixel.R;
            int G = CurrentPixel.G;
            int B = CurrentPixel.B;

            if (readClipboard){
                    if (clipboardSurface != null)
                {
                    aux[x,y] = clipboardSurface.GetBilinearSampleWrapped(x, y);
                }
                else
                {
                    aux[x,y] = Color.Transparent;
                }
                ClipPixel=aux[x,y];
                maxClipR = ClipPixel.R > 0 ? ClipPixel.R : 255;
                maxClipG = ClipPixel.G > 0 ? ClipPixel.G : 255;
                maxClipB = ClipPixel.B > 0 ? ClipPixel.B : 255;
                DivMultClipR = 255f / maxClipR;
                DivMultClipG = 255f / maxClipG;
                DivMultClipB = 255f / maxClipB;
            }

            switch (moduloMode)
            {
                case ModuloMode.Modulo:
                    R1 = (byte)(R % (maxR + 1));
                    G1 = (byte)(G % (maxG + 1));
                    B1 = (byte)(B % (maxB + 1));
                    R2 = (byte)(R % (maxClipR + 1));
                    G2 = (byte)(G % (maxClipG + 1));
                    B2 = (byte)(B % (maxClipB + 1));
                    break;
                case ModuloMode.ModuloContinuous:
                    if (readClipboard){
                    }
                    int TR1 = R / (maxR + 1);
                    int TG1 = G / (maxG + 1);
                    int TB1 = B / (maxB + 1);
                    R1 = (TR1 % 2 == 1) ? (byte)(maxNumber - (R % (maxR + 1))) : (byte)(R % (maxR + 1));
                    G1 = (TG1 % 2 == 1) ? (byte)(maxNumber - (G % (maxG + 1))) : (byte)(G % (maxG + 1));
                    B1 = (TB1 % 2 == 1) ? (byte)(maxNumber - (B % (maxB + 1))) : (byte)(R % (maxB + 1));
                    int TR2 = R / (maxClipR + 1);
                    int TG2 = G / (maxClipG + 1);
                    int TB2 = B / (maxClipB + 1);
                    R2 = (TR2 % 2 == 1) ? (byte)(maxClipR - (R % (maxClipR + 1))) : (byte)(R % (maxClipR + 1));
                    G2 = (TG2 % 2 == 1) ? (byte)(maxClipG - (G % (maxClipG + 1))) : (byte)(G % (maxClipG + 1));
                    B2 = (TB2 % 2 == 1) ? (byte)(maxClipB - (B % (maxClipB + 1))) : (byte)(R % (maxClipB + 1));
                    break;
                case ModuloMode.DivisiveModulo:
                    if (maxR != 1) { R1 = (byte)(R * DivMultR);}
                    if (maxG != 1) { G1 = (byte)(G * DivMultG);}
                    if (maxB != 1) { B1 = (byte)(B * DivMultB);}
                    if (maxClipR != 1) { R2 = (byte)(R * DivMultClipR);}
                    if (maxClipG != 1) { G2 = (byte)(G * DivMultClipG);}
                    if (maxClipB != 1) { B2 = (byte)(B * DivMultClipB);}
                    break;
                case ModuloMode.DivisiveModuloContinuous:
                    if (maxR != 1){ float MR1 = R * DivMultR; int CR21 = (int)(MR1 / 256); R1 = (CR21 % 2 == 0) ? (int)(MR1) % 256 : 255 - ((int)(MR1) % 256);}
                    if (maxG != 1){ float MG1 = G * DivMultG; int CG21 = (int)(MG1 / 256); G1 = (CG21 % 2 == 0) ? (int)(MG1) % 256 : 255 - ((int)(MG1) % 256);}
                    if (maxB != 1){ float MB1 = B * DivMultB; int CB21 = (int)(MB1 / 256); B1 = (CB21 % 2 == 0) ? (int)(MB1) % 256 : 255 - ((int)(MB1) % 256);}
                    if (maxClipR != 1){ float MR2 = R * DivMultClipR; int CR22 = (int)(MR2 / 256); R2 = (CR22 % 2 == 0) ? (int)(MR2) % 256 : 255 - ((int)(MR2) % 256);}
                    if (maxClipG != 1){ float MG2 = G * DivMultClipG; int CG22 = (int)(MG2 / 256); G2 = (CG22 % 2 == 0) ? (int)(MG2) % 256 : 255 - ((int)(MG2) % 256);}
                    if (maxClipB != 1){ float MB2 = B * DivMultClipB; int CB22 = (int)(MB2 / 256); B2 = (CB22 % 2 == 0) ? (int)(MB2) % 256 : 255 - ((int)(MB2) % 256);}
                    break;
                case ModuloMode.ModuloAddition:
                    R1 = (byte)((R + maxR) % 256);
                    G1 = (byte)((G + maxG) % 256);
                    B1 = (byte)((B + maxB) % 256);
                    R2 = (byte)((R + maxClipR) % 256);
                    G2 = (byte)((G + maxClipG) % 256);
                    B2 = (byte)((B + maxClipB) % 256);
                    break;
                case ModuloMode.ModuloAdditionContinuous:
                    R1 = ((R + maxR) > 255) ? (int)(255 - (R + maxR) % 256) : R + maxR;
                    G1 = ((G + maxG) > 255) ? (int)(255 - (G + maxG) % 256) : G + maxG;
                    B1 = ((B + maxB) > 255) ? (int)(255 - (B + maxB) % 256) : B + maxB;
                    R2 = ((R + maxClipR) > 255) ? (int)(255 - (R + maxClipR) % 256) : R + maxClipR;
                    G2 = ((G + maxClipG) > 255) ? (int)(255 - (G + maxClipG) % 256) : G + maxClipG;
                    B2 = ((B + maxClipB) > 255) ? (int)(255 - (B + maxClipB) % 256) : B + maxClipB;
                    break;
            }

            if(Negate){
                R1 = 255 - R1;
                G1 = 255 - G1;
                B1 = 255 - B1;
                R2 = 255 - R1;
                G2 = 255 - G1;
                B2 = 255 - B1;
            }
            ModVal[x,y] = ColorBgra.FromBgraClamped(B1, G1, R1, CurrentPixel.A);
            aux[x,y] = ColorBgra.FromBgraClamped(B2, G2, R2, CurrentPixel.A);
        }
    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra CurrentPixel;

    // Step through each row of the current rectangle
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        // Step through each pixel on the current row of the rectangle
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra ModPixel = ModVal[x,y];
            ColorBgra AuxPixel = aux[x,y];
            if (ClipboardMode){CurrentPixel = AuxPixel;}
            else {CurrentPixel = ModPixel;}

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

enum ModuloMode
{
    Modulo,
    ModuloContinuous,
    DivisiveModulo,
    DivisiveModuloContinuous,
    ModuloAddition,
    ModuloAdditionContinuous
}

 

Edited by Reptillian

G'MIC Filter Developer

Link to comment
Share on other sites

  • Reptillian changed the title to How would I fix this? (SOLVED)

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