4117c5a0f7d703dacce30a0e2cd51315c211945f
[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 circle of confusion based on pixel size."
53   (when (and (consp imager) (consp pixels))
54     (values (float (* 2 (/ (car imager) (car pixels))))
55             (float (* 2 (/ (cdr imager) (cdr pixels)))))))
56   
57 (defun coc-pixels-format (format)
58   "Returns circle of confusion based on pixel size."
59   (coc-pixels (imager-dimensions format) (pixel-dimensions format)))
60
61 (defun coc-airy (f-stop)
62   "Return the circle of confusion based on the airy disk."
63   (let ((airy (/ f-stop 1500)))
64     (float (* 2 airy))))
65
66 (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
67   "Returns depth of field based on focal-length, f-stop, distance, and coc.
68 Six values are returned:
69 near dof, far dof, total dof, near point, far point, magnification,
70 blur size at infinity (mm).
71 Circle of confusion can either be a number or keyword designating format."
72   (let* ((aperture (/ focal-length f-stop)) 
73          (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
74                          coc focal-length))
75          (numerator-2 (* pupil-factor aperture focal-length distance))
76          (denominator-1 (* pupil-factor coc (- distance focal-length)))
77          (denominator-2 (* pupil-factor aperture focal-length))
78          (near (/ (+ numerator-1 numerator-2)
79                   (+ denominator-1 denominator-2)))
80          (far (/ (- numerator-1 numerator-2)
81                  (- denominator-1 denominator-2)))
82          (mag (float (/ focal-length (- distance focal-length))))
83          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
84          (depth (- far near)))
85     (values near far depth mag infinity-blur-diameter)))
86
87 ;; Simplified calculation for symmetric lens
88 (defun dof-symmetric-mm (focal-length f-stop distance coc)
89   "Returns depth of field based on focal-length, f-stop, distance, and coc.
90 Six values are returned:
91 near dof, far dof, total dof, near point, far point, magnification,
92 blur size at infinity (mm).
93 Circle of confusion can either be a number or keyword designating format.
94 Pupil factor is the ratio of the exit to enterance pupil diameters."
95   (let* ((aperture (/ focal-length f-stop))
96          (numerator (* distance coc (- distance focal-length)))
97          (factor-1 (* focal-length aperture))
98          (factor-2 (* coc (- distance focal-length)))
99          (near (- distance (/ numerator (+ factor-1 factor-2))))
100          (far (+ distance (/ numerator (- factor-1 factor-2))))
101          (mag (magnification focal-length distance))
102          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
103          (depth (- far near)))
104     (values near far depth mag infinity-blur-diameter)))
105
106 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
107   "Returns the Depth of Field.
108 Input: FOCAL-LENGTH, F-STOP, DISTANCE, CIRCLE-OF-CONFUSION. 
109 Output: NEAR-POINT, FAR-POINT, TOTAL-DOF, MAGNIFICATION, BLUR-SIZE-OF-INFINITY-POINT-IN-MM."
110   (multiple-value-bind (near-point far-point total-dof mag blur)
111       (dof-mm focal-length f-stop (length->mm distance units) coc 
112               :pupil-factor pupil-factor)
113     (values (mm->length near-point units)
114             (mm->length far-point units)
115             (mm->length total-dof units)
116             mag blur)))
117
118 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
119   (mm->length (+ focal-length (/ (* focal-length focal-length) f-stop coc)) units))
120
121 (defun effective-aperture (focal-length distance aperture)
122   (* aperture (bellows-factor focal-length distance)))