r10433: remove package not included
[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
23 (defun coc-format (format &key (lpm 5) (minimum-distance 250) 
24                           (viewing-distance 250)
25                           (print-size (format-dimensions :8x10in)))
26   "Returns circle of confusion in mm and print magnification for a format. 
27 Default resolving power is 5 lpm at 25cm."
28
29   (let* ((format-size (format-dimensions format))
30          (format-diagonal (diagonal (car format-size) (cdr format-size)))
31          (print-diagonal (diagonal (car print-size) (cdr print-size)))
32          (resolution-factor (/ (* lpm print-diagonal minimum-distance)
33                                (* format-diagonal viewing-distance)))
34          (coc (/ 1.0d0 resolution-factor))
35          (print-magnification (/ print-diagonal format-diagonal)))
36     (values coc print-magnification)))
37
38 (defun coc-sensor (format nx ny)
39   "Returns circle of confusion based on pixel size."
40   (let* ((dim (format-dimensions format))
41          (pixel-width (/ (car dim) nx))
42          (pixel-height (/ (cdr dim) ny))) 
43     (values (float (* 2 pixel-width))
44             (float (* 2 pixel-height)))))
45
46 (defun coc-sensor-camera (camera &key (format :aps))
47   (let ((dim (sensor-dimensions camera :format format)))
48     (coc-sensor format (car dim) (cdr dim))))
49
50 (defun sensor-dimensions-megapixels (format megapixels)
51   (let* ((dim (format-dimensions format))
52          (aspect-ratio (/ (car dim) (cdr dim)))
53          (width (round (sqrt (* aspect-ratio 1000000 megapixels)))))
54     (cons width (round (/ width aspect-ratio)))))
55
56
57 (defun sensor-dimensions (sensor-spec &key (format :aps))
58   "Returns the number of pixels for a sensor. 
59 CAMERA-SPEC is either a keyword designating the camera or
60 the number of megapixels of the sensor.
61 FORMAT should be defined if the CAMERA-SPEC is the number of megapixels
62 so the proper aspect ratio is used."
63   (etypecase sensor-spec
64     (keyword
65      (ecase sensor-spec
66        ;; nikon
67        (:d2x (cons 4288 2848))
68        (:d100 (cons 3037 2024))
69        (:d2h (cons 2464 1632))
70        (:d70 (cons 3008 2000))
71
72        ;; canon
73        (:1d (cons 2464 1648))
74        (:1d2 (cons 3504 2336))
75        (:1ds (cons 4064 2704))
76        (:1ds2 (cons 4992 3328))
77
78        ))
79     (number
80      (sensor-dimensions-megapixels format sensor-spec))))
81
82 (defun coc-airy (f-stop)
83   "Return the circle of confusion based on the airy disk."
84   (let ((airy (/ f-stop 1500)))
85     (float (* 2 airy))))
86
87 (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
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   (let* ((aperture (/ focal-length f-stop)) 
94          (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
95                          coc focal-length))
96          (numerator-2 (* pupil-factor aperture focal-length distance))
97          (denominator-1 (* pupil-factor coc (- distance focal-length)))
98          (denominator-2 (* pupil-factor aperture focal-length))
99          (near (/ (+ numerator-1 numerator-2)
100                   (+ denominator-1 denominator-2)))
101          (far (/ (- numerator-1 numerator-2)
102                  (- denominator-1 denominator-2)))
103          (mag (float (/ focal-length (- distance focal-length))))
104          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
105          (depth (- far near)))
106     (values near far depth mag infinity-blur-diameter)))
107
108 ;; Simplified calculation for symmetric lens
109 (defun dof-symmetric-mm (focal-length f-stop distance coc)
110   "Returns depth of field based on focal-length, f-stop, distance, and coc.
111 Six values are returned:
112 near dof, far dof, total dof, near point, far point, magnification,
113 blur size at infinity (mm).
114 Circle of confusion can either be a number or keyword designating format.
115 Pupil factor is the ratio of the exit to enterance pupil diameters."
116   (let* ((aperture (/ focal-length f-stop))
117          (numerator (* distance coc (- distance focal-length)))
118          (factor-1 (* focal-length aperture))
119          (factor-2 (* coc (- distance focal-length)))
120          (near (- distance (/ numerator (+ factor-1 factor-2))))
121          (far (+ distance (/ numerator (- factor-1 factor-2))))
122          (mag (magnification focal-length distance))
123          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
124          (depth (- far near)))
125     (values near far depth mag infinity-blur-diameter)))
126
127 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
128   (multiple-value-bind (near-point far-point total-dof mag blur)
129       (dof-mm focal-length f-stop (length->mm distance units) coc 
130               :pupil-factor pupil-factor)
131     (values (mm->length near-point units)
132             (mm->length far-point units)
133             (mm->length total-dof units)
134             mag blur)))
135
136 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
137   (mm->length 
138    (+ focal-length (/ (* focal-length focal-length) f-stop coc))
139    units))
140
141 (defun magnification (focal-length distance)
142   (float (/ focal-length (- distance focal-length))))
143
144 (defun bellows-factor (focal-length distance)
145   (1+ (magnification focal-length distance)))
146
147 (defun effective-aperture (focal-length distance aperture)
148   (* aperture (bellows-factor focal-length distance)))