r10525: change coc so that it takes crops into consideration
[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-max (max (car imager-size) (cdr imager-size)))
29          (imager-min (min (car imager-size) (cdr imager-size)))
30          (print-max (max (car print-size) (cdr print-size)))
31          (print-min (min (car print-size) (cdr print-size)))
32          (magnification (max (/ print-max imager-max) (/ print-min imager-min)))
33          (resolution-factor (/ (* magnification lpm minimum-distance) viewing-distance))
34          (coc (/ 1.0d0 resolution-factor)))
35     (values coc magnification)))
36
37 (defun coc-format (format &key (lpm 5) (minimum-distance 250) 
38                           (viewing-distance 250)
39                           (print-size (output-dimensions :8x10in)))
40   "Returns circle of confusion in mm and print magnification for a format. 
41 Default resolving power is 5 lpm at 25cm."
42
43   (let* ((format-size (imager-dimensions format))
44          (format-diagonal (diagonal (car format-size) (cdr format-size)))
45          (print-diagonal (diagonal (car print-size) (cdr print-size)))
46          (resolution-factor (/ (* lpm print-diagonal minimum-distance)
47                                (* format-diagonal viewing-distance)))
48          (coc (/ 1.0d0 resolution-factor))
49          (print-magnification (/ print-diagonal format-diagonal)))
50     (values coc print-magnification)))
51
52 (defun coc-pixels (imager pixels)
53   "Returns lpm and circle of confusion based on pixel size."
54   (when (and (consp imager) (consp pixels))
55     (let ((coc-w (float (* 2 (/ (car imager) (car pixels)))))
56           (coc-h (float (* 2 (/ (cdr imager) (cdr pixels))))))
57     (values coc-w coc-h (/ 1. coc-w) (/ 1. coc-h)))))
58   
59 (defun coc-pixels-format (format)
60   "Returns circle of confusion based on pixel size."
61   (coc-pixels (imager-dimensions format) (pixel-dimensions format)))
62
63 (defun coc-airy (f-stop &optional (wavelength 0.000512))
64   "Return the circle of confusion based on the airy disk."
65   (float (/ 1 (rayleigh-limit f-stop wavelength))))
66
67 (defun rayleigh-limit (f-stop &optional (wavelength 0.0005))
68   "Returns the rayleigh limit in line pairs per mm (MTF 9%) as well as the MTF50"
69   (let ((rayleigh (float (/ 1 1.22 f-stop wavelength))))
70     (values rayleigh (* 0.46 rayleigh))))
71
72 (defun maximum-sharpness-aperture (format &optional (wavelength 0.0005))
73   (multiple-value-bind (coc-w coc-h lpm-w lpm-h) (coc-pixels-format format)
74     (declare (ignore coc-w coc-h))
75     (/ 1. (* 1.22 wavelength (/ (min lpm-w lpm-h) 0.46)))))
76
77 (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
78   "Returns depth of field based on focal-length, f-stop, distance, and coc.
79 Six values are returned:
80 near point, far point, total dof, magnification, blur size at infinity (mm).
81 Circle of confusion can either be a number or keyword designating format."
82   (let* ((aperture (/ focal-length f-stop)) 
83          (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
84                          coc focal-length))
85          (numerator-2 (* pupil-factor aperture focal-length distance))
86          (denominator-1 (* pupil-factor coc (- distance focal-length)))
87          (denominator-2 (* pupil-factor aperture focal-length))
88          (near (/ (+ numerator-1 numerator-2)
89                   (+ denominator-1 denominator-2)))
90          (far (/ (- numerator-1 numerator-2)
91                  (- denominator-1 denominator-2)))
92          (mag (float (/ focal-length (- distance focal-length))))
93          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
94          (depth (- far near)))
95     (values near far depth mag infinity-blur-diameter)))
96
97 ;; Simplified calculation for symmetric lens
98 (defun dof-symmetric-mm (focal-length f-stop distance coc)
99   "Returns depth of field based on focal-length, f-stop, distance, and coc.
100 Six values are returned:
101 near dof, far dof, total dof, near point, far point, magnification,
102 blur size at infinity (mm).
103 Circle of confusion can either be a number or keyword designating format.
104 Pupil factor is the ratio of the exit to enterance pupil diameters."
105   (let* ((aperture (/ focal-length f-stop))
106          (numerator (* distance coc (- distance focal-length)))
107          (factor-1 (* focal-length aperture))
108          (factor-2 (* coc (- distance focal-length)))
109          (near (- distance (/ numerator (+ factor-1 factor-2))))
110          (far (+ distance (/ numerator (- factor-1 factor-2))))
111          (mag (magnification focal-length distance))
112          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
113          (depth (- far near)))
114     (values near far depth mag infinity-blur-diameter)))
115
116 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
117   "Returns the Depth of Field.
118 Input: FOCAL-LENGTH, F-STOP, DISTANCE, CIRCLE-OF-CONFUSION. 
119 Output: NEAR-POINT, FAR-POINT, TOTAL-DOF, MAGNIFICATION, BLUR-SIZE-OF-INFINITY-POINT-IN-MM."
120   (multiple-value-bind (near-point far-point total-dof mag blur)
121       (dof-mm focal-length f-stop (length->mm distance units) coc 
122               :pupil-factor pupil-factor)
123     (values (mm->length near-point units)
124             (mm->length far-point units)
125             (mm->length total-dof units)
126             mag blur)))
127
128 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
129   (mm->length (+ focal-length (/ (* focal-length focal-length) f-stop coc)) units))
130
131 (defun effective-aperture (focal-length distance aperture)
132   (* aperture (bellows-factor focal-length distance)))
133
134 (defun mtf-scanner (freq dscan-freq &optional (order 3))
135   (abs (expt (kmrcl:sinc (* pi (/ freq dscan-freq))) order)))
136