r10418: Automated commit for cl-photo debian-version-0.1-1
[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 ;; Based on http://www.photostuff.co.uk/dofmstr.htm
23
24 (defun coc-format (format &key (lpm 5) (minimum-distance 250) 
25                           (viewing-distance 250)
26                           (print-size (format-dimensions :8x10in)))
27   "Returns circle of confusion in mm and print magnification for a format. 
28 Default resolving power is 5 lpm at 25cm."
29
30   (let* ((format-size (format-dimensions format))
31          (format-diagonal (diagonal (car format-size) (cdr format-size)))
32          (print-diagonal (diagonal (car print-size) (cdr print-size)))
33          (resolution-factor (/ (* lpm print-diagonal minimum-distance)
34                                (* format-diagonal viewing-distance)))
35          (coc (/ 1.0d0 resolution-factor))
36          (print-magnification (/ print-diagonal format-diagonal)))
37     (values coc print-magnification)))
38
39 (defun coc-sensor (format nx ny)
40   "Returns circle of confusion based on pixel size."
41   (let* ((dim (format-dimensions format))
42          (pixel-width (/ (car dim) nx))
43          (pixel-height (/ (cdr dim) ny))) 
44     (values (coerce (* 2 pixel-width) 'float)
45             (coerce (* 2 pixel-height) 'float))))
46
47 (defun coc-sensor-camera (camera &key (format :aps))
48   (let ((dim (sensor-dimensions camera :format format)))
49     (coc-sensor format (car dim) (cdr dim))))
50
51 (defun sensor-dimensions-megapixels (format megapixels)
52   (let* ((dim (format-dimensions format))
53          (aspect-ratio (/ (car dim) (cdr dim)))
54          (width (round (sqrt (* aspect-ratio 1000000 megapixels)))))
55     (cons width (round (/ width aspect-ratio)))))
56
57 (defun sensor-dimensions (camera &key (format :aps))
58   (etypecase camera
59     (keyword
60      (ecase camera
61        ;; nikon
62        (:d2x (cons 4288 2848))
63        (:d2x (cons 2484 1242))
64        (:d70 (cons 3008 2000))
65
66        ;; canon
67        (:1d (cons 2464 1648))
68        (:1d2 (cons 3504 2336))
69        (:1ds (cons 4064 2704))
70        (:1ds2 (cons 4992 3328))
71
72        ))
73     (number
74      (sensor-dimensions-megapixels format camera))))
75
76 (defun coc-airy-disk (f-stop)
77   "Return the circle of confusion based on the airy disk."
78   (let ((airy (/ f-stop 1500)))
79     (coerce (* 2 airy) 'float)))
80
81 (defun dof (focal-length f-stop distance coc)
82   "Returns depth of field as fives values: 
83 near dof, far dof, total dof, near point, far point.
84 Circle of confusion can either be a number or keyword designating format."
85   (let* ((aperature (/ focal-length f-stop))
86          (numerator (* distance coc (- distance focal-length)))
87          (factor-1 (* focal-length aperature))
88          (factor-2 (* coc (- distance focal-length)))
89          (near (/ numerator (+ factor-1 factor-2)))
90          (far (/ numerator (- factor-1 factor-2)))
91          (depth (+ far near)))
92     (values near far depth (- distance near) (+ distance far))))
93
94 (defun dof-feet (focal-length f-stop distance coc)
95   (multiple-value-bind (near-dof far-dof total-dof near-point far-point)
96       (dof focal-length f-stop (feet->mm distance) coc)
97     (values (mm->feet near-dof) (mm->feet far-dof) (mm->feet total-dof)
98             (mm->feet near-point) (mm->feet far-point))))
99
100 (defun dof-meters (focal-length f-stop distance coc)
101   (multiple-value-bind (near-dof far-dof total-dof near-point far-point)
102       (dof focal-length f-stop (* 1000 distance) coc)
103     (values (* 0.001 near-dof) (* 0.001 far-dof) (* 0.001 total-dof)
104             (* 0.001 near-point) (* 0.001 far-point))))
105
106 (defun hyperfocal (focal-length f-stop coc)
107   (+ focal-length (/ (* focal-length focal-length) (* f-stop coc))))
108
109 (defun hyperfocal-feet (focal-length f-stop coc)
110   (mm->feet (hyperfocal focal-length f-stop coc)))
111
112