# astronomical, geodetic and meteorological formula
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program (http://www.fsf.org/licensing/licenses/ );
#    if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#
# The author (web.victor.reijs@gmail.com ) is interested in feedback.
# http://www.iol.ie/~geniet/3d/


from JascApp import *

import sys

def ScriptProperties():
    return {
        'Author': 'Victor Reijs',
        'Copyright': '2005',
        'Description': 'Barrel and pincushion distortion correction. No garantees can be given on its function. And the author takes up NO liability whatsoever',
        'Host': 'Paint Shop Pro',
        'Host Version': '8.10'
        }

def Do(Environment):
    print
    print '******'
    # EnableOptimizedScriptUndo
    App.Do( Environment, 'EnableOptimizedScriptUndo', {
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            }) 
    Info = App.Do( Environment, 'ReturnImageInfo' )
    commentstring = Info['ExifTitle']

    comment='Barrel/pincushion distortion correction software of V. Reijs, (c) 2005. '
    modelstring='FinePix2600Zoom'
    model = Info['ExifModel']
    if model == '' :
		print >> sys.stderr,'***WARNING: No camera model found!'
    if model != modelstring and model != '':
		print >> sys.stderr,'***WARNING: ',model,' is (proberly) not supported with this conversion'
    if model == modelstring :
		print 'Camera model: ', model		
    modelstring=model
    testcomment = Info['ExifTitle']
    if testcomment.find(comment)<>-1:		
	  print >> sys.stderr,'***WARNING: File is already corrected'
	  
    else:
      print 'File: ', Info['FileName']
      commentstring=commentstring+comment
      if (model==modelstring and model!=''):
		focallength=Info['ExifFocalLength']
		print 'Focal length: ', focallength
		sm=focallength.find('m')
		fcnumber=eval(focallength[0:sm])
# conversion of Fujifilm FinePix 2600Zoom
		firststrength=3.6035*fcnumber*fcnumber - 67.356*fcnumber + 313.85
		strength= firststrength
# conversion of Fujifilm FinePix 2600Zoom
		secondstrength=-0.7731*fcnumber + 7.6354
		if secondstrength<=0 : 
			strength=abs(secondstrength)
			App.Do( Environment, 'PincushionCorrection', {
            'PreserveCentralScale': App.Constants.Boolean.false, 
            'Strength': strength, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

		else:
			strength=firststrength
			App.Do( Environment, 'BarrelCorrection', {
            'PreserveCentralScale': App.Constants.Boolean.false, 
            'Strength': strength, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })
		print 'Strength: ',int(strength)
		App.Do( Environment, 'ImageInfo', {
            'ExifTitle': commentstring, 
             'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })
    print commentstring
    print '******'
		

