Feedback

Please leave feedback and comments. I am always interested to hear how people get on using these LScripts!

Friday, 15 March 2013

LScript - Image_Pixelate

LScript (Layout) to create a pixelated effect with envelope control.

Changes
  • Override alpha

Compatible with Newtek LightWave 9.6 and above.

// LScript Image Filter - www.StephenCulley.co.uk
//
// web   address: http://www.stephenculley.co.uk
// email address: email@stephenculley.co.uk

/* 
    LScript Image Filter - Pixelate

    Image_Pixelate.ls

*/

@version 2.5
@warnings
@script image
@name *Pixelate

    // Title
    sTitle = "*Pixelate";

    // Version
    sVersion = "v1.0";

    // Envelope
    envSize; // Size

    // Control
    ctrl_c0;

create
{
    setdesc(sTitle);

    // Envelope
    envSize = Envelope("Size (" + sTitle + ")",CHAN_NUMBER,sTitle); // Size
    envSize.persist(false);
    envSize.createKey(0,20.0);
}

process: ifo
{
    // Variable
    iSize = ceil(envSize.value(Scene().currenttime));

    // Error
    if(iSize < 2){return;}
    if(ifo.width < 1 || ifo.height < 1){return;}

    iGridX = ceil(ifo.width / iSize);
    iGridY = ceil(ifo.height / iSize);

    iGridXOffset = abs((ifo.width - (iGridX * iSize)) * 0.5);
    iGridYOffset = abs((ifo.height - (iGridY * iSize)) * 0.5);

    iProgress = iGridX * iGridY;
    if(runningUnder() != SCREAMERNET) moninit(iProgress);

    for(y = 0;y <= iGridY - 1; ++y)
      {

      for(x = 0;x <= iGridX - 1; ++x)
        {
        v1 = ;
        v2 = ;

        // Draw 
        pixelateIFO(ifo,v1,v2);

        if(runningUnder() != SCREAMERNET) if(monstep()) return;
        }      
      }
  
}

pixelateIFO: ifo,v1,v2 // vector,red,green,blue
{
    // Order
    if(v1.x > v2.x){n = v2.x;v2.x = v1.x; v1.x = n;}
    if(v1.y > v2.y){n = v2.y;v2.y = v1.y; v1.y = n;}

    if(v1.x < 1 || v2.x > ifo.width ||
       v1.y < 1 || v2.y > ifo.height)
      {
      // Offscreen
      if(v2.x < 1){return;}
      if(v1.x > ifo.width){return;}
      if(v2.y < 1){return;}
      if(v1.y > ifo.width){return;}
  
      // Clip
      if(v1.x < 1){v1.x = 1;}
      if(v1.x > ifo.width){v1.x = ifo.width;}
      if(v1.y < 1){v1.y = 1;}
      if(v1.y > ifo.height){v1.y = ifo.height;}
      if(v2.x < 1){v2.x = 1;}
      if(v2.x > ifo.width){v2.x = ifo.width;}
      if(v2.y < 1){v2.y = 1;}
      if(v2.y > ifo.height){v2.y = ifo.height;}
      }

    r = 0;
    g = 0;
    b = 0;
    c = 0;
    
    // Average
    for(y = v1.y; y <= v2.y; ++y)
      {
      for(x = v1.x; x <= v2.x; ++x)
        {
        r += ifo.red[x,y];
        g += ifo.green[x,y];
        b += ifo.blue[x,y];
        ++c;
        }
      }

    if(c > 1)
      {
      r *= 1 / c;
      g *= 1 / c;
      b *= 1 / c;
      }

    // Draw
    for(y = v1.y; y <= v2.y; ++y)
      {
      for(x = v1.x; x <= v2.x; ++x)
        {
        ifo.red[x,y] = r;
        ifo.green[x,y] = g;
        ifo.blue[x,y] = b;
        ifo.alpha[x,y] = 1.0;
        }
      }
}

load: what,io
{
    if(what == SCENEMODE)   // processing an ASCII scene file
    {
        if(io.read().asStr() == sTitle + " " + sVersion)
            {
            // Envelope
            envSize.load(); // Size
            }
    }
}

save: what,io
{
    if(what == SCENEMODE)
    {    
        // Header
        io.writeln(sTitle + " " + sVersion);

        // Envelope
        envSize.save(); // Size
    }
}

options
{
    if(reqisopen())
        {
        reqend();
        return;
        }

    reqbegin(sTitle + " " + sVersion);

    ctrl_c0 = ctlpercent("Size",envSize.value(Scene().currenttime) * 0.01); // Size
    ctrl_c1e = ctlbutton("E",20,"button_c1e"); // Button

    // Developer
    ctlsep();
    ctrl_dev0 = ctltext("","developer: Stephen Culley","http://www.stephenculley.co.uk");

    // Refresh
    ctlrefresh(ctrl_c0,"refresh_c0"); // Size

    reqopen();
}

button_c1e // Size
{
    envSize.edit();
}

refresh_c0:value // Size
{
    setvalue(ctrl_c0,max(0.0,value));
    iKey = envSize.keyExists(Scene().currenttime);
    if(iKey == nil)
        {envSize.createKey(Scene().currenttime,max(0.0,value * 100));}
    else
        {envSize.setKeyValue(iKey,max(0.0,value * 100));}
}


All scripts available at my Google Drive at
https://drive.google.com/open?id=1cR_q2GVUAJHumic1-A3eXV16acQnVTWs

1 comment:

  1. Another very cool script that does exactly as it says.

    Thank you Stephen :)


    Would it be possible to include an "Add to Alpha" option to the script?

    ReplyDelete