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