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