r10485: Automated commit for Debian build of cl-photo upstream-version-0.7
[cl-photo.git] / dof.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10; Package: photo -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          dof.lisp
6 ;;;; Purpose:       Depth of field functions for cl-photo
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  April 2005
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of cl-photo, is Copyright (c) 2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; cl-photo users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the GNU General Public License v2
16 ;;;; (http://www.gnu.org/licenses/gpl.html)
17 ;;;;
18 ;;;; *************************************************************************
19
20 (in-package #:photo)
21
22 (defun coc (imager-size &key (lpm 5) (minimum-distance 250) 
23                    (viewing-distance 250)
24                    (print-size (output-dimensions :8x10in)))
25   "Returns circle of confusion in mm and print magnification for a format. 
26 Default resolving power is 5 lpm at 25cm."
27
28   (let* ((imager-diagonal (diagonal (car imager-size) (cdr imager-size)))
29          (print-diagonal (diagonal (car print-size) (cdr print-size)))
30          (resolution-factor (/ (* lpm print-diagonal minimum-distance)
31                                (* imager-diagonal viewing-distance)))
32          (coc (/ 1.0d0 resolution-factor))
33          (print-magnification (/ print-diagonal imager-diagonal)))
34     (values coc print-magnification)))
35
36 (defun coc-format (format &key (lpm 5) (minimum-distance 250) 
37                           (viewing-distance 250)
38                           (print-size (output-dimensions :8x10in)))
39   "Returns circle of confusion in mm and print magnification for a format. 
40 Default resolving power is 5 lpm at 25cm."
41
42   (let* ((format-size (imager-dimensions format))
43          (format-diagonal (diagonal (car format-size) (cdr format-size)))
44          (print-diagonal (diagonal (car print-size) (cdr print-size)))
45          (resolution-factor (/ (* lpm print-diagonal minimum-distance)
46                                (* format-diagonal viewing-distance)))
47          (coc (/ 1.0d0 resolution-factor))
48          (print-magnification (/ print-diagonal format-diagonal)))
49     (values coc print-magnification)))
50
51 (defun coc-pixels (imager pixels)
52   "Returns lpm and circle of confusion based on pixel size."
53   (when (and (consp imager) (consp pixels))
54     (let ((coc-w (float (* 2 (/ (car imager) (car pixels)))))
55           (coc-h (float (* 2 (/ (cdr imager) (cdr pixels))))))
56     (values coc-w coc-h (/ 1. coc-w) (/ 1. coc-h)))))
57   
58 (defun coc-pixels-format (format)
59   "Returns circle of confusion based on pixel size."
60   (coc-pixels (imager-dimensions format) (pixel-dimensions format)))
61
62 (defun coc-airy (f-stop &optional (wavelength 0.000512))
63   "Return the circle of confusion based on the airy disk."
64   (float (/ 1 (rayleigh-limit f-stop wavelength))))
65
66 (defun rayleigh-limit (f-stop &optional (wavelength 0.0005))
67   "Returns the rayleigh limit in line pairs per mm (MTF 9%) as well as the MTF50"
68   (let ((rayleigh (float (/ 1 1.22 f-stop wavelength))))
69     (values rayleigh (* 0.46 rayleigh))))
70
71 (defun maximum-sharpness-aperture (format &optional (wavelength 0.0005))
72   (multiple-value-bind (coc-w coc-h lpm-w lpm-h) (coc-pixels-format format)
73     (declare (ignore coc-w coc-h))
74     (/ 1. (* 1.22 wavelength (/ (min lpm-w lpm-h) 0.46)))))
75
76 (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
77   "Returns depth of field based on focal-length, f-stop, distance, and coc.
78 Six values are returned:
79 near point, far point, total dof, magnification, blur size at infinity (mm).
80 Circle of confusion can either be a number or keyword designating format."
81   (let* ((aperture (/ focal-length f-stop)) 
82          (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
83                          coc focal-length))
84          (numerator-2 (* pupil-factor aperture focal-length distance))
85          (denominator-1 (* pupil-factor coc (- distance focal-length)))
86          (denominator-2 (* pupil-factor aperture focal-length))
87          (near (/ (+ numerator-1 numerator-2)
88                   (+ denominator-1 denominator-2)))
89          (far (/ (- numerator-1 numerator-2)
90                  (- denominator-1 denominator-2)))
91          (mag (float (/ focal-length (- distance focal-length))))
92          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
93          (depth (- far near)))
94     (values near far depth mag infinity-blur-diameter)))
95
96 ;; Simplified calculation for symmetric lens
97 (defun dof-symmetric-mm (focal-length f-stop distance coc)
98   "Returns depth of field based on focal-length, f-stop, distance, and coc.
99 Six values are returned:
100 near dof, far dof, total dof, near point, far point, magnification,
101 blur size at infinity (mm).
102 Circle of confusion can either be a number or keyword designating format.
103 Pupil factor is the ratio of the exit to enterance pupil diameters."
104   (let* ((aperture (/ focal-length f-stop))
105          (numerator (* distance coc (- distance focal-length)))
106          (factor-1 (* focal-length aperture))
107          (factor-2 (* coc (- distance focal-length)))
108          (near (- distance (/ numerator (+ factor-1 factor-2))))
109          (far (+ distance (/ numerator (- factor-1 factor-2))))
110          (mag (magnification focal-length distance))
111          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
112          (depth (- far near)))
113     (values near far depth mag infinity-blur-diameter)))
114
115 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
116   "Returns the Depth of Field.
117 Input: FOCAL-LENGTH, F-STOP, DISTANCE, CIRCLE-OF-CONFUSION. 
118 Output: NEAR-POINT, FAR-POINT, TOTAL-DOF, MAGNIFICATION, BLUR-SIZE-OF-INFINITY-POINT-IN-MM."
119   (multiple-value-bind (near-point far-point total-dof mag blur)
120       (dof-mm focal-length f-stop (length->mm distance units) coc 
121               :pupil-factor pupil-factor)
122     (values (mm->length near-point units)
123             (mm->length far-point units)
124             (mm->length total-dof units)
125             mag blur)))
126
127 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
128   (mm->length (+ focal-length (/ (* focal-length focal-length) f-stop coc)) units))
129
130 (defun effective-aperture (focal-length distance aperture)
131   (* aperture (bellows-factor focal-length distance)))
132
133 (defun mtf-scanner (freq dscan-freq &optional (order 3))
134   (abs (expt (kmrcl:sinc (* pi (/ freq dscan-freq))) order)))
135
136 (defun freq-mtf-scanner (dscan-freq mtf &optional (order 3))
137   (* dscan-freq (/ (asin (* x (exp  (/ (log mtf) order)))) pi)))