require 'ftools'
require 'rubygems'
require 'RMagick'
include Magick
class PiceditController < ApplicationController
WorkFile='temp.jpg'
def get_selected_pic
session[:picfile] ||= "yes.jpeg"
end
def designers
'Designer:Anmol
Anand'
end
def get_choices
session[:choices]=['border','wave','rotate','blur','radialblur','charcoal']
end
def get_percentage
session[:percentage]=((get_selected_item )* ("#{get_stepsize}".to_i)) % 100
session[:percentage]
end
def first_time
session[:motherpic]=session[:pic]=get_selected_pic
session[:selected_item]=0
session[:freeze]=nil
# session[:separator]=','
session[:percentage]=session[:stepsize].to_i
#state 0 => we are choosing what to change
#state 1 => we have chosen what to change
session[:state]=0
session[:title]='select an image effect'
#session[:expiry]=
#''
#so that any older copy of temp.jpg in the cache is not used
redirect_to :action => "index"
end
def border(path,borderfile,borderval)
path= path.chomp
borderfile= borderfile.chomp
borderval= (borderval.to_i/10)*5
picwidth= 560 ;
picheight= 400;
if File.exists?(borderfile)
# logger.info("filepresent:#{borderfile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img.border(borderval,borderval,'rgb(255,110,140)').write(borderfile)
end
end
def wave(path,wavefile,percentwave)
path= path.chomp
wavefile= wavefile.chomp
percentlength=percentwave*4
percentwave= percentwave.to_i/2.0
picwidth= 560 ; #width of the pic (in pixels)
picheight= 400; #height of the pic (pixels)
if File.exists?(wavefile)
# logger.info("filepresent:#{wavefile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.wave(percentwave, percentlength)
img.write(wavefile)
end
end
def rotate(path,rotatefile,rotateangle)
path= path.chomp
rotatefile= rotatefile.chomp
picwidth= 560 ; #width of the pic (in pixels)
picheight= 400; #height of the pic (pixels)
if File.exists?(rotatefile)
# logger.info("filepresent:#{rotatefile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.rotate!(rotateangle)
img.write(rotatefile)
end
end
def flip(path,flipfile)
path= path.chomp
flipfile= flipfile.chomp
picwidth= 560 ; #width of the pic (in pixels)
picheight= 400; #height of the pic (pixels)
if File.exists?(flipfile)
# logger.info("filepresent:#{flipfile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.flip
img.write(flipfile)
end
end
def flop(path,flopfile)
path= path.chomp
flopfile= flopfile.chomp
picwidth= 560 ; #width of the pic (in pixels)
picheight= 400; #height of the pic (pixels)
if File.exists?(flopfile)
#logger.info("filepresent:#{flopfile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.flop
img.write(flopfile)
end
end
def blur(path,blurfile,percentblur)
path= path.chomp
blurfile= blurfile.chomp
percentblur= percentblur.to_i/5.0
picwidth= 560 ; #width of the pic (in pixels)
picheight= 400; #height of the pic (pixels)
if File.exists?(blurfile)
# logger.info("filepresent:#{blurfile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.blur_image(0.0,percentblur)
img.write(blurfile)
end
end
def radialblur(path,bfile,blurpercent)
path= path.chomp
bfile= bfile.chomp
blurpercent=(blurpercent*2.0)
if File.exists?(bfile)
#logger.info("filepresent:#{bfile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.radial_blur(blurpercent)
img.write(bfile)
end
end
def charcoal(path,coalfile,coalpercent)
path= path.chomp
coalfile= coalfile.chomp
coalpercent= coalpercent.to_i/10.0
picwidth= 560 ; #width of the pic (in pixels)
picheight= 400; #height of the pic (pixels)
if File.exists?(coalfile)
logger.info("filepresent:#{coalfile}exists")
else
img= Magick::Image.read(path).first #reads the image specfied by the user path
img= img.scale(560,400) #scales image to 640x480
img=img.charcoal(radius=0.0, sigma=coalpercent)
img.write(coalfile)
end
end
def selected(str)
session[:motherpic]= session[:pic]
end
def get_freeze
session[:freeze] ||= session[:selected_item]
end
def backspace
session[:state]=0
session[:selected_item]=0
session[:title]='select an image effect'
session[:pic]=session[:motherpic]
session[:freeze]=nil
redirect_to :action => "index"
end
def process_choice selected_choice
if session[:state]==1
session[:state]=0
session[:selected_item]=0
session[:freeze]=nil
session[:title]='select an image effect'
session[:motherpic]=session[:pic]
else
session[:state]=1
session[:title]="#{session[:percentage]}%"+
get_choices[get_freeze%session[:choices].size]
logger.info("freeze:#{session[:freeze]}")
end
logger.info("sel_state:#{session[:state]}")
logger.info("croppercent:#{session[:percentage]}")
end
def updownevent
logger.info("reached updownevent")
if session[:state]==1
#session[:percentage]=(get_selected_item % 9+1) * 10
session[:percentage]=get_percentage
tempchoice = get_choices
choice=tempchoice[get_freeze % tempchoice.size]
logger.info("choice:#{choice}")
st=(Dir.pwd).chomp("/public")+"/public/images/"
session[:mainfile]=if session[:motherpic]=~/home/
session[:motherpic]
else
st+session[:motherpic]
end
case choice
when "border"
filename="#{session[:mainfile]}".gsub(/\./,"b#{session[:percentage]}"+".")
session[:pic]=filename
session[:title]="#{session[:percentage]} pixel "+get_choices[get_freeze%session[:choices].size]
border(session[:mainfile],filename,session[:percentage])
when "wave"
filename="#{session[:mainfile]}".gsub(/\./,"w#{session[:percentage]}"+".")
session[:pic]=filename
session[:title]="#{session[:percentage]}%"+get_choices[get_freeze%session[:choices].size]
wave(session[:mainfile],filename,session[:percentage])
when "rotate"
filename="#{session[:mainfile]}".gsub(/\./,"rot#{session[:percentage]}"+".")
session[:pic]=filename
session[:title]=get_choices[get_freeze%session[:choices].size]+" #{session[:percentage]} degrees"
rotate(session[:mainfile],filename,session[:percentage])
when "blur"
filename="#{session[:mainfile]}".gsub(/\./,"bl#{session[:percentage]}"+".")
session[:pic]=filename
session[:title]="#{session[:percentage]}%"+get_choices[get_freeze%session[:choices].size]
blur(session[:mainfile],filename,session[:percentage])
when "radialblur"
filename="#{session[:mainfile]}".gsub(/\./,"rdb#{session[:percentage]}"+".")
session[:pic]=filename
session[:title]="#{session[:percentage]}%"+get_choices[get_freeze%session[:choices].size]
radialblur(session[:mainfile],filename,session[:percentage])
when "charcoal"
filename="#{session[:mainfile]}".gsub(/\./,"co#{session[:percentage]}"+".")
session[:pic]=filename
session[:title]="#{session[:percentage]}%"+get_choices[get_freeze%session[:choices].size]+"effect"
charcoal(session[:mainfile],filename,session[:percentage])
end
end
end
def extras
# logger.info("reached extras")
if session[:state]==1
@selected_item = session[:freeze]%get_choices.size
end
if session[:pic].match("(/images/.*)$")
@picfile=$1
else
@picfile=session[:pic]
end
# logger.info("mypic: #{@picfile}")
end
end