Monday, March 21, 2011

Reduce pdf file size using gs

#!/bin/bash
# From http://scriptdemo.blogspot.com
# to reduce pdf file size using gs with different optimization level
# Usage:
# pdfsmaller.sh in.pdf out.pdf [opt]
# option:
#            screen    (72dpi)
#            ebook     (150dpi)
#            printer    (300dpi)
#            prepress (color preserving, 300dpi)
# e.g.,
#            pdfsmaller.sh Big.pdf smaller.pdf ebook # save output to smaller.pdf
# or        pdfsmaller.sh Big.pdf ebook                  # overwrite original Big.pdf
# ref: http://milan.kupcevic.net/ghostscript-ps-pdf

isOverWrite=0
if [ $# -eq 1 ]; then
   inFile=$1
   outFile=smaller_$1
   opt="prepress"
   isOverWrite=1
elif [ $# -eq 2 ]; then
   inFile=$1
   nf=`echo $2 | awk -F. '{print NF}'`
   if [ ${nf} -eq 2 ]; then
      outFile=$2
      opt="prepress"
   else
      outFile=smaller_$1
      opt=$2
      isOverWrite=1
   fi
elif [ $# -eq 3 ]; then
   inFile=$1
   outFile=$2
   opt=$3
else
   sed -n '2,12p' pdfsmaller.sh
   exit
fi
if [ ! -e "${inFile}" ]; then
   echo "${inFile} not exist"
   echo "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${opt} -dNOPAUSE \
                  -dQUIET -dBATCH -sOutputFile=\"${outFile}\" \"${inFile}\""
   exit
else
   eval "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${opt} -dNOPAUSE  \
                 -dQUIET -dBATCH -sOutputFile=\"${outFile}\" \"${inFile}\""
fi
[ ${isOverWrite} -eq 1 ] && mv "${outFile}" "${inFile}"

Thursday, March 3, 2011

[Processing] More Perlin Noise demo with Processing

Actually it is generated with the Perlin Noise Particle System from Dann Van Hasselt (http://www.openprocessing.org/visuals/?visualID=10475)
What I did is set the initial particle position in every loop (within draw()):
e.g., 
           myt=elapseFrames/10/TWO_PI;
           pos.x=0.5*width +(1-sin(myt))*cos(myt)*0.2*width;
           pos.y=0.5*height-(1-sin(myt))*sin(myt)*0.2*height;

Also you can change the color and particle details to get something like (online):
or a colourful one:

Perlin Noise demo with Processing

#copyright @ http://scriptdemo.blogspot.com
// inspired by the Perlin Noise Particle System from Daan van Hasselt (http://www.openprocessing.org/visuals/?visualID=10475)
// I recoded it with more additional features. Also it may be easier to understand without the class.
// Online test  from my dropbox (my Dropbox reference link)
float myw=400.0;
float myh=350.0;
float x, y;
float u=0;
float v=0;
float x0, y0, dx, dy;
float coefx, coefy, coefz;
boolean isAlive=false;
boolean isColor=true;
boolean isSmooth=true;
boolean isLoop=true;

int NumP=400;
float Npi;
void setup()
{
  Npi=2.3; coefx=0.009; coefy=0.011; coefz=0.0013;
  size(floor(myw),floor(myh));
  background(255,255,255);
  if (isSmooth) smooth();
}

void draw()
{

  for (int n=0; n<=NumP; n++)
  {
      if (isAlive)
      {
        float nv=noise(x*coefx,y*coefy,frameCount*coefz);
        if (isColor)
        {
           float mysin=sin((nv-0.3)*TWO_PI*Npi);
           float mycos=cos((nv-0.3)*TWO_PI*Npi);
           if (mycos>=0 & mysin >=0) fill(floor(abs(mysin)*255),floor(abs(mycos)*255),0,10);
           if (mycos>=0 & mysin <0) fill(floor(abs(mysin)*255),0,floor(abs(mycos)*255),10);
           if (mycos<0 & mysin >=0) fill(0,floor(abs(mycos)*255),floor(abs(mysin)*255),10);
           if (mycos<0 & mysin <0) fill(floor(abs(mysin)*255),floor(abs(mycos)*255),0,10);
        } else {
           fill(10,10);
        }
         u=u+cos((nv-0.3)*TWO_PI*Npi);
         v=v+sin((nv-0.3)*TWO_PI*Npi);
         u=u/2;
         v=v/2;
         x=x+u;
         y=y+v;
      } else {
        x0=0.5*myw;
        y0=0.5*myh;
        x=x0; y=y0;
        dx=100*frameCount/2000+50;
        dy=dx;
        isAlive=true;
      }
      noStroke();
      ellipse(x,y,1,1);
  }
  if (x>width || y> height || x <0 || y< 0) isAlive=false;
}

void mouseClicked()
{
  isLoop=!isLoop;
  if (!isLoop)
  {
     noLoop();
  } else {
     loop();
  }
}
Here are some demo videos:

ShowCalendar