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 (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
95 (numerator-2 (* pupil-factor aperture focal-length distance))
96 (denominator-1 (* pupil-factor coc (- distance focal-length)))
97 (denominator-2 (* pupil-factor aperture focal-length))
98 (near (/ (+ numerator-1 numerator-2)
99 (+ denominator-1 denominator-2)))
100 (far (/ (- numerator-1 numerator-2)
101 (- denominator-1 denominator-2)))
102 (mag (float (/ focal-length (- distance focal-length))))
103 (infinity-blur-diameter (/ (* mag focal-length) f-stop))
104 (depth (- far near)))
105 (when (and (< far 0) (> distance focal-length))
106 (setq far most-positive-short-float)
107 (setq depth most-positive-short-float))
108 (values near far depth mag infinity-blur-diameter)))
110 ;; Simplified calculation for symmetric lens
111 (defun dof-symmetric-mm (focal-length f-stop distance coc)
112 "Returns depth of field based on focal-length, f-stop, distance, and coc.
113 Six values are returned:
114 near point, far point, total dof, near point, far point, magnification,
115 blur size at infinity (mm).
116 Circle of confusion can either be a number or keyword designating format."
117 (let* ((aperture (/ focal-length f-stop))
118 (numerator (* distance coc (- distance focal-length)))
119 (factor-1 (* focal-length aperture))
120 (factor-2 (* coc (- distance focal-length)))
121 (near (- distance (/ numerator (+ factor-1 factor-2))))
122 (far (+ distance (/ numerator (- factor-1 factor-2))))
123 (mag (magnification :focal-length focal-length :object-distance distance :units :mm))
124 (infinity-blur-diameter (/ (* mag focal-length) f-stop))
125 (depth (- far near)))
126 (when (and (< far 0) (> distance focal-length))
127 (setq far most-positive-short-float)
128 (setq depth most-positive-short-float))
129 (values near far depth mag infinity-blur-diameter)))
131 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
132 "Returns the Depth of Field.
133 Input: FOCAL-LENGTH, F-STOP, DISTANCE, CIRCLE-OF-CONFUSION.
134 Output: NEAR-POINT, FAR-POINT, TOTAL-DOF, MAGNIFICATION, BLUR-SIZE-OF-INFINITY-POINT-IN-MM."
135 (multiple-value-bind (near-point far-point total-dof mag blur)
136 (dof-mm focal-length f-stop (length->mm distance units) coc
137 :pupil-factor pupil-factor)
138 (values (mm->length near-point units)
139 (mm->length far-point units)
140 (mm->length total-dof units)
143 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
144 (mm->length (+ focal-length (/ (* focal-length focal-length) f-stop coc)) units))
146 (defun effective-aperture (focal-length distance aperture)
147 (* aperture (bellows-factor focal-length distance)))
149 (defun mtf-scanner (freq dscan-freq &optional (order 3))
150 (abs (expt (kmrcl:sinc (* pi (/ freq dscan-freq))) order)))