X-Git-Url: http://git.kpe.io/?p=cl-photo.git;a=blobdiff_plain;f=dof.lisp;h=d8677d307ad3b40d873f6f4180e66c023f8456a4;hp=9cb2486cf719de2373bf9ac45723825423c8c4b0;hb=HEAD;hpb=afb52c66a55ede18c0dc3b9ca8ee4cf0e7570937 diff --git a/dof.lisp b/dof.lisp old mode 100755 new mode 100644 index 9cb2486..d8677d3 --- a/dof.lisp +++ b/dof.lisp @@ -19,14 +19,37 @@ (in-package #:photo) +(defun sort-size (size) + "Returns a cons pair with the smaller size first." + (if (>= (car size) (cdr size)) + (cons (cdr size) (car size)) + (cons (car size) (cdr size)))) + +(defun print-magnification (imager-size print-size) + "Returns the magnification required between an imager and print sizes +while taking crop into consideration." + (setf imager-size (sort-size imager-size)) + (setf print-size (sort-size print-size)) + (float (max (/ (car print-size) (car imager-size)) + (/ (cdr print-size) (cdr print-size))))) + +(defun coc (imager-size &key (lpm 5) (minimum-distance 250) + (viewing-distance 250) + (print-size (output-dimensions :8x10in))) + "Returns circle of confusion in mm and print magnification for a format. +Default resolving power is 5 lpm at 25cm." + (let* ((magnification (print-magnification imager-size print-size)) + (resolution-factor (/ (* magnification lpm minimum-distance) viewing-distance)) + (coc (/ 1.0d0 resolution-factor))) + (values coc magnification))) -(defun coc-format (format &key (lpm 5) (minimum-distance 250) +(defun coc-format (format &key (lpm 5) (minimum-distance 250) (viewing-distance 250) - (print-size (format-dimensions :8x10in))) - "Returns circle of confusion in mm and print magnification for a format. + (print-size (output-dimensions :8x10in))) + "Returns circle of confusion in mm and print magnification for a format. Default resolving power is 5 lpm at 25cm." - (let* ((format-size (format-dimensions format)) + (let* ((format-size (imager-dimensions format)) (format-diagonal (diagonal (car format-size) (cdr format-size))) (print-diagonal (diagonal (car print-size) (cdr print-size))) (resolution-factor (/ (* lpm print-diagonal minimum-distance) @@ -35,62 +58,39 @@ Default resolving power is 5 lpm at 25cm." (print-magnification (/ print-diagonal format-diagonal))) (values coc print-magnification))) -(defun coc-sensor (format nx ny) +(defun coc-pixels (imager pixels) + "Returns lpm and circle of confusion based on pixel size." + (when (and (consp imager) (consp pixels)) + (let ((coc-w (float (* 2 (/ (car imager) (car pixels))))) + (coc-h (float (* 2 (/ (cdr imager) (cdr pixels)))))) + (values coc-w coc-h (/ 1. coc-w) (/ 1. coc-h))))) + +(defun coc-pixels-format (format) "Returns circle of confusion based on pixel size." - (let* ((dim (format-dimensions format)) - (pixel-width (/ (car dim) nx)) - (pixel-height (/ (cdr dim) ny))) - (values (float (* 2 pixel-width)) - (float (* 2 pixel-height))))) - -(defun coc-sensor-camera (camera &key (format :aps)) - (let ((dim (sensor-dimensions camera :format format))) - (coc-sensor format (car dim) (cdr dim)))) - -(defun sensor-dimensions-megapixels (format megapixels) - (let* ((dim (format-dimensions format)) - (aspect-ratio (/ (car dim) (cdr dim))) - (width (round (sqrt (* aspect-ratio 1000000 megapixels))))) - (cons width (round (/ width aspect-ratio))))) - - -(defun sensor-dimensions (sensor-spec &key (format :aps)) - "Returns the number of pixels for a sensor. -CAMERA-SPEC is either a keyword designating the camera or -the number of megapixels of the sensor. -FORMAT should be defined if the CAMERA-SPEC is the number of megapixels -so the proper aspect ratio is used." - (etypecase sensor-spec - (keyword - (ecase sensor-spec - ;; nikon - (:d2x (cons 4288 2848)) - (:d100 (cons 3037 2024)) - (:d2h (cons 2464 1632)) - (:d70 (cons 3008 2000)) - - ;; canon - (:1d (cons 2464 1648)) - (:1d2 (cons 3504 2336)) - (:1ds (cons 4064 2704)) - (:1ds2 (cons 4992 3328)) - - )) - (number - (sensor-dimensions-megapixels format sensor-spec)))) - -(defun coc-airy (f-stop) + (coc-pixels (imager-dimensions format) (pixel-dimensions format))) + +(defun coc-airy (f-stop &optional (wavelength 0.000512)) "Return the circle of confusion based on the airy disk." - (let ((airy (/ f-stop 1500))) - (float (* 2 airy)))) + (float (/ 1 (rayleigh-limit f-stop wavelength)))) + +(defun rayleigh-limit (f-stop &optional (wavelength 0.0005)) + "Returns the rayleigh limit in line pairs per mm (MTF 9%) as well as the MTF50" + (let ((rayleigh (float (/ 1 1.22 f-stop wavelength)))) + (values rayleigh (* 0.46 rayleigh)))) + +(defun maximum-sharpness-aperture (format &optional (wavelength 0.0005)) + (multiple-value-bind (coc-w coc-h lpm-w lpm-h) (coc-pixels-format format) + (declare (ignore coc-w coc-h)) + (/ 1. (* 1.22 wavelength (/ (min lpm-w lpm-h) 0.46))))) (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1)) "Returns depth of field based on focal-length, f-stop, distance, and coc. Six values are returned: -near dof, far dof, total dof, near point, far point, magnification, -blur size at infinity (mm). -Circle of confusion can either be a number or keyword designating format." - (let* ((aperture (/ focal-length f-stop)) +near point, far point, total dof, magnification, blur size at infinity (mm). +Circle of confusion can either be a number or keyword designating format. +Reference: http://www.vanwalree.com/optics/dofderivation.html" + (let* ((aperture (/ focal-length f-stop)) + (hyperfocal (hyperfocal focal-length f-stop coc)) (numerator-1 (* (- pupil-factor 1) (- distance focal-length) coc focal-length)) (numerator-2 (* pupil-factor aperture focal-length distance)) @@ -98,35 +98,50 @@ Circle of confusion can either be a number or keyword designating format." (denominator-2 (* pupil-factor aperture focal-length)) (near (/ (+ numerator-1 numerator-2) (+ denominator-1 denominator-2))) - (far (/ (- numerator-1 numerator-2) - (- denominator-1 denominator-2))) + (far (when (/= denominator-1 denominator-2) + (/ (- numerator-1 numerator-2) + (- denominator-1 denominator-2)))) (mag (float (/ focal-length (- distance focal-length)))) (infinity-blur-diameter (/ (* mag focal-length) f-stop)) - (depth (- far near))) + (depth (when far (- far near)))) + (when (or (>= distance hyperfocal) + (and (null far) (>= distance (* hyperfocal 0.99)))) + (setq near (/ hyperfocal 2) + far most-positive-short-float + depth most-positive-short-float)) (values near far depth mag infinity-blur-diameter))) ;; Simplified calculation for symmetric lens (defun dof-symmetric-mm (focal-length f-stop distance coc) "Returns depth of field based on focal-length, f-stop, distance, and coc. Six values are returned: -near dof, far dof, total dof, near point, far point, magnification, +near point, far point, total dof, near point, far point, magnification, blur size at infinity (mm). -Circle of confusion can either be a number or keyword designating format. -Pupil factor is the ratio of the exit to enterance pupil diameters." +Circle of confusion can either be a number or keyword designating format." (let* ((aperture (/ focal-length f-stop)) + (hyperfocal (hyperfocal focal-length f-stop coc)) (numerator (* distance coc (- distance focal-length))) (factor-1 (* focal-length aperture)) (factor-2 (* coc (- distance focal-length))) (near (- distance (/ numerator (+ factor-1 factor-2)))) - (far (+ distance (/ numerator (- factor-1 factor-2)))) - (mag (magnification focal-length distance)) + (far (when (/= factor-1 factor-2) + (+ distance (/ numerator (- factor-1 factor-2))))) + (mag (magnification :focal-length focal-length :object-distance distance :units :mm)) (infinity-blur-diameter (/ (* mag focal-length) f-stop)) - (depth (- far near))) + (depth (when far (- far near)))) + (when (or (>= distance hyperfocal) + (and (null far) (>= distance (* hyperfocal 0.99)))) + (setq near (/ hyperfocal 2) + far most-positive-short-float + depth most-positive-short-float)) (values near far depth mag infinity-blur-diameter))) (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1)) + "Returns the Depth of Field. +Input: FOCAL-LENGTH, F-STOP, DISTANCE, CIRCLE-OF-CONFUSION. +Output: NEAR-POINT, FAR-POINT, TOTAL-DOF, MAGNIFICATION, BLUR-SIZE-OF-INFINITY-POINT-IN-MM." (multiple-value-bind (near-point far-point total-dof mag blur) - (dof-mm focal-length f-stop (length->mm distance units) coc + (dof-mm focal-length f-stop (length->mm distance units) coc :pupil-factor pupil-factor) (values (mm->length near-point units) (mm->length far-point units) @@ -134,15 +149,11 @@ Pupil factor is the ratio of the exit to enterance pupil diameters." mag blur))) (defun hyperfocal (focal-length f-stop coc &key (units :mm)) - (mm->length - (+ focal-length (/ (* focal-length focal-length) f-stop coc)) - units)) + (mm->length (+ focal-length (/ (* focal-length focal-length) f-stop coc)) units)) -(defun magnification (focal-length distance) - (float (/ focal-length (- distance focal-length)))) +(defun effective-aperture (focal-length distance aperture) + (* aperture (bellows-factor focal-length distance))) -(defun bellows-factor (focal-length distance) - (1+ (magnification focal-length distance))) +(defun mtf-scanner (freq dscan-freq &optional (order 3)) + (abs (expt (kmrcl:sinc (* pi (/ freq dscan-freq))) order))) -(defun effective-aperture (focal-length distance aperture) - (* aperture (bellows-factor focal-length distance))) \ No newline at end of file