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