1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10; Package: photo -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Depth of field functions for cl-photo
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: April 2005
12 ;;;; This file, part of cl-photo, is Copyright (c) 2005 by Kevin M. Rosenberg
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)
18 ;;;; *************************************************************************
22 (defun sort-size (size)
23 "Returns a cons pair with the smaller size first."
24 (if (>= (car size) (cdr size))
25 (cons (cdr size) (car size))
26 (cons (car size) (cdr size))))
28 (defun print-magnification (imager-size print-size)
29 "Returns the magnification required between an imager and print sizes
30 while taking crop into consideration."
31 (setf imager-size (sort-size imager-size))
32 (setf print-size (sort-size print-size))
33 (float (max (/ (car print-size) (car imager-size))
34 (/ (cdr print-size) (cdr print-size)))))
36 (defun coc (imager-size &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 (let* ((magnification (print-magnification imager-size print-size))
42 (resolution-factor (/ (* magnification lpm minimum-distance) viewing-distance))
43 (coc (/ 1.0d0 resolution-factor)))
44 (values coc magnification)))
46 (defun coc-format (format &key (lpm 5) (minimum-distance 250)
47 (viewing-distance 250)
48 (print-size (output-dimensions :8x10in)))
49 "Returns circle of confusion in mm and print magnification for a format.
50 Default resolving power is 5 lpm at 25cm."
52 (let* ((format-size (imager-dimensions format))
53 (format-diagonal (diagonal (car format-size) (cdr format-size)))
54 (print-diagonal (diagonal (car print-size) (cdr print-size)))
55 (resolution-factor (/ (* lpm print-diagonal minimum-distance)
56 (* format-diagonal viewing-distance)))
57 (coc (/ 1.0d0 resolution-factor))
58 (print-magnification (/ print-diagonal format-diagonal)))
59 (values coc print-magnification)))
61 (defun coc-pixels (imager pixels)
62 "Returns lpm and circle of confusion based on pixel size."
63 (when (and (consp imager) (consp pixels))
64 (let ((coc-w (float (* 2 (/ (car imager) (car pixels)))))
65 (coc-h (float (* 2 (/ (cdr imager) (cdr pixels))))))
66 (values coc-w coc-h (/ 1. coc-w) (/ 1. coc-h)))))
68 (defun coc-pixels-format (format)
69 "Returns circle of confusion based on pixel size."
70 (coc-pixels (imager-dimensions format) (pixel-dimensions format)))
72 (defun coc-airy (f-stop &optional (wavelength 0.000512))
73 "Return the circle of confusion based on the airy disk."
74 (float (/ 1 (rayleigh-limit f-stop wavelength))))
76 (defun rayleigh-limit (f-stop &optional (wavelength 0.0005))
77 "Returns the rayleigh limit in line pairs per mm (MTF 9%) as well as the MTF50"
78 (let ((rayleigh (float (/ 1 1.22 f-stop wavelength))))
79 (values rayleigh (* 0.46 rayleigh))))
81 (defun maximum-sharpness-aperture (format &optional (wavelength 0.0005))
82 (multiple-value-bind (coc-w coc-h lpm-w lpm-h) (coc-pixels-format format)
83 (declare (ignore coc-w coc-h))
84 (/ 1. (* 1.22 wavelength (/ (min lpm-w lpm-h) 0.46)))))
86 (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
87 "Returns depth of field based on focal-length, f-stop, distance, and coc.
88 Six values are returned:
89 near point, far point, total dof, magnification, blur size at infinity (mm).
90 Circle of confusion can either be a number or keyword designating format.
91 Reference: http://www.vanwalree.com/optics/dofderivation.html"
92 (let* ((aperture (/ focal-length f-stop))
93 (hyperfocal (hyperfocal focal-length f-stop coc))
94 (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
96 (numerator-2 (* pupil-factor aperture focal-length distance))
97 (denominator-1 (* pupil-factor coc (- distance focal-length)))
98 (denominator-2 (* pupil-factor aperture focal-length))
99 (near (/ (+ numerator-1 numerator-2)
100 (+ denominator-1 denominator-2)))
101 (far (when (/= denominator-1 denominator-2)
102 (/ (- numerator-1 numerator-2)
103 (- denominator-1 denominator-2))))
104 (mag (float (/ focal-length (- distance focal-length))))
105 (infinity-blur-diameter (/ (* mag focal-length) f-stop))
106 (depth (when far (- far near))))
107 (when (or (>= distance hyperfocal)
108 (and (null far) (>= distance (* hyperfocal 0.99))))
109 (setq near (/ hyperfocal 2)
110 far most-positive-short-float
111 depth most-positive-short-float))
112 (values near far depth mag infinity-blur-diameter)))
114 ;; Simplified calculation for symmetric lens
115 (defun dof-symmetric-mm (focal-length f-stop distance coc)
116 "Returns depth of field based on focal-length, f-stop, distance, and coc.
117 Six values are returned:
118 near point, far point, total dof, near point, far point, magnification,
119 blur size at infinity (mm).
120 Circle of confusion can either be a number or keyword designating format."
121 (let* ((aperture (/ focal-length f-stop))
122 (hyperfocal (hyperfocal focal-length f-stop coc))
123 (numerator (* distance coc (- distance focal-length)))
124 (factor-1 (* focal-length aperture))
125 (factor-2 (* coc (- distance focal-length)))
126 (near (- distance (/ numerator (+ factor-1 factor-2))))
127 (far (when (/= factor-1 factor-2)
128 (+ distance (/ numerator (- factor-1 factor-2)))))
129 (mag (magnification :focal-length focal-length :object-distance distance :units :mm))
130 (infinity-blur-diameter (/ (* mag focal-length) f-stop))
131 (depth (when far (- far near))))
132 (when (or (>= distance hyperfocal)
133 (and (null far) (>= distance (* hyperfocal 0.99))))
134 (setq near (/ hyperfocal 2)
135 far most-positive-short-float
136 depth most-positive-short-float))
137 (values near far depth mag infinity-blur-diameter)))
139 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
140 "Returns the Depth of Field.
141 Input: FOCAL-LENGTH, F-STOP, DISTANCE, CIRCLE-OF-CONFUSION.
142 Output: NEAR-POINT, FAR-POINT, TOTAL-DOF, MAGNIFICATION, BLUR-SIZE-OF-INFINITY-POINT-IN-MM."
143 (multiple-value-bind (near-point far-point total-dof mag blur)
144 (dof-mm focal-length f-stop (length->mm distance units) coc
145 :pupil-factor pupil-factor)
146 (values (mm->length near-point units)
147 (mm->length far-point units)
148 (mm->length total-dof units)
151 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
152 (mm->length (+ focal-length (/ (* focal-length focal-length) f-stop coc)) units))
154 (defun effective-aperture (focal-length distance aperture)
155 (* aperture (bellows-factor focal-length distance)))
157 (defun mtf-scanner (freq dscan-freq &optional (order 3))
158 (abs (expt (kmrcl:sinc (* pi (/ freq dscan-freq))) order)))