DocumentationOverviewBuilding ASL Documentation Library Wiki Docs Indices Browse Perforce More InfoRelease NotesWiki Site Search License Success Stories Contributors MediaDownloadPerforce Depots SupportASL SourceForge HomeMailing Lists Discussion Forums Report Bugs Suggest Features Contribute to ASL RSSShort-text newsFull-text news File releases Other Adobe ProjectsAdobe AirAdobe GIL Adobe Labs Adobe Media Gallery Adobe XMP Tamarin project (Mozilla Foundation) Other ResourcesBoostRIAForge SGI STL |
dng_lens_correction.h00001 /*****************************************************************************/ 00002 // Copyright 2008 Adobe Systems Incorporated 00003 // All Rights Reserved. 00004 // 00005 // NOTICE: Adobe permits you to use, modify, and distribute this file in 00006 // accordance with the terms of the Adobe license agreement accompanying it. 00007 /*****************************************************************************/ 00008 00009 /* $Id: //mondo/workarea/stern/camera_raw/dng_sdk/source/dng_lens_correction.h#14 $ */ 00010 /* $DateTime: 2009/06/18 17:47:41 $ */ 00011 /* $Change: 577711 $ */ 00012 /* $Author: stern $ */ 00013 00014 /*****************************************************************************/ 00015 00016 #ifndef __dng_lens_correction__ 00017 #define __dng_lens_correction__ 00018 00019 /*****************************************************************************/ 00020 00021 #include "dng_1d_function.h" 00022 #include "dng_matrix.h" 00023 #include "dng_opcodes.h" 00024 #include "dng_pixel_buffer.h" 00025 #include "dng_point.h" 00026 #include "dng_resample.h" 00027 #include "dng_sdk_limits.h" 00028 00029 #include <vector> 00030 00031 /*****************************************************************************/ 00032 00035 00036 class dng_warp_params 00037 { 00038 00039 public: 00040 00041 // Number of planes to be warped. Must be either 1 or equal to the number of 00042 // planes of the image to be processed. If set to 1, then a single set of 00043 // warp parameters applies to all planes of the image. fPlanes must be at 00044 // least 1 and no greater than kMaxColorPlanes (see dng_sdk_limits.h). 00045 00046 uint32 fPlanes; 00047 00048 // The optical center of the lens in normalized [0,1] coordinates with 00049 // respect to the image's active area. For example, a value of (0.5, 0.5) 00050 // indicates that the optical center of the lens is at the center of the 00051 // image's active area. A normalized radius of 1.0 corresponds to the 00052 // distance from fCenter to the farthest corner of the image's active area. 00053 // Each component of fCenter must lie in the range [0,1]. 00054 00055 dng_point_real64 fCenter; 00056 00057 public: 00058 00059 dng_warp_params (); 00060 00061 // planes is the number of planes of parameters specified: It must be either 00062 // 1 or equal to the number of planes of the image to be processed. 00063 00064 dng_warp_params (uint32 planes, 00065 const dng_point_real64 &fCenter); 00066 00067 virtual ~dng_warp_params (); 00068 00069 // Is the entire correction a NOP for all planes? 00070 00071 virtual bool IsNOPAll () const; 00072 00073 // Is the entire correction a NOP for the specified plane? 00074 00075 virtual bool IsNOP (uint32 plane) const; 00076 00077 // Is the radial correction a NOP for all planes? 00078 00079 virtual bool IsRadNOPAll () const; 00080 00081 // Is the radial correction a NOP for the specified plane? 00082 00083 virtual bool IsRadNOP (uint32 plane) const; 00084 00085 // Is the tangential correction a NOP for all planes? 00086 00087 virtual bool IsTanNOPAll () const; 00088 00089 // Is the tangential correction a NOP for the specified plane? 00090 00091 virtual bool IsTanNOP (uint32 plane) const; 00092 00093 // Do these warp params appear valid? 00094 00095 virtual bool IsValid () const; 00096 00097 // Are these warp params valid for the specified negative? 00098 00099 virtual bool IsValidForNegative (const dng_negative &negative) const; 00100 00101 // Propagate warp parameters from first plane to all other planes. 00102 00103 virtual void PropagateToAllPlanes (uint32 totalPlanes) = 0; 00104 00105 // Evaluate the 1D radial warp function for the specified plane. Parameter r 00106 // is the destination (i.e., corrected) normalized radius, i.e., the 00107 // normalized Euclidean distance between a corrected pixel position and the 00108 // optical center in the image. r lies in the range [0,1]. The returned 00109 // result is non-negative. 00110 00111 virtual real64 Evaluate (uint32 plane, 00112 real64 r) const = 0; 00113 00114 // Compute and return the inverse of Evaluate () above. The base 00115 // implementation uses Newton's method to perform the inversion. Parameter r 00116 // is the source (i.e., uncorrected) normalized radius, i.e., normalized 00117 // Euclidean distance between a corrected pixel position and the optical 00118 // center in the image. Both r and the computed result are non-negative. 00119 00120 virtual real64 EvaluateInverse (uint32 plane, 00121 real64 r) const; 00122 00123 // Evaluate the 1D radial warp ratio function for the specified plane. 00124 // Parameter r2 is the square of the destination (i.e., corrected) normalized 00125 // radius, i.e., the square of the normalized Euclidean distance between a 00126 // corrected pixel position and the optical center in the image. r2 must lie 00127 // in the range [0,1]. Note that this is different than the Evaluate () 00128 // function, above, in that the argument to EvaluateRatio () is the square of 00129 // the radius, not the radius itself. The returned result is non-negative. 00130 // Mathematically, EvaluateRatio (r * r) is the same as Evaluate (r) / r. 00131 00132 virtual real64 EvaluateRatio (uint32 plane, 00133 real64 r2) const = 0; 00134 00135 // Evaluate the 2D tangential warp for the specified plane. Parameter r2 is 00136 // the square of the destination (i.e., corrected) normalized radius, i.e., 00137 // the square of the normalized Euclidean distance between a corrected pixel 00138 // position P and the optical center in the image. r2 must lie in the range 00139 // [0,1]. diff contains the vertical and horizontal Euclidean distances (in 00140 // pixels) between P and the optical center. diff2 contains the squares of 00141 // the vertical and horizontal Euclidean distances (in pixels) between P and 00142 // the optical center. The returned result is the tangential warp offset, 00143 // measured in pixels. 00144 00145 virtual dng_point_real64 EvaluateTangential (uint32 plane, 00146 real64 r2, 00147 const dng_point_real64 &diff, 00148 const dng_point_real64 &diff2) const = 0; 00149 00150 // Evaluate the 2D tangential warp for the specified plane. diff contains the 00151 // vertical and horizontal Euclidean distances (in pixels) between the 00152 // destination (i.e., corrected) pixel position and the optical center in the 00153 // image. The returned result is the tangential warp offset, measured in 00154 // pixels. 00155 00156 dng_point_real64 EvaluateTangential2 (uint32 plane, 00157 const dng_point_real64 &diff) const; 00158 00159 // Evaluate the 2D tangential warp for the specified plane. Parameter r2 is 00160 // the square of the destination (i.e., corrected) normalized radius, i.e., 00161 // the square of the normalized Euclidean distance between a corrected pixel 00162 // position P and the optical center in the image. r2 must lie in the range 00163 // [0,1]. diff contains the vertical and horizontal Euclidean distances (in 00164 // pixels) between P and the optical center. The returned result is the 00165 // tangential warp offset, measured in pixels. 00166 00167 dng_point_real64 EvaluateTangential3 (uint32 plane, 00168 real64 r2, 00169 const dng_point_real64 &diff) const; 00170 00171 // Compute and return the maximum warped radius gap. Let D be a rectangle in 00172 // a destination (corrected) image. Let rDstFar and rDstNear be the farthest 00173 // and nearest points to the image center, respectively. Then the specified 00174 // parameter maxDstGap is the Euclidean distance between rDstFar and 00175 // rDstNear. Warp D through this warp function to a closed and bounded 00176 // (generally not rectangular) region S. Let rSrcfar and rSrcNear be the 00177 // farthest and nearest points to the image center, respectively. This 00178 // routine returns a value that is at least (rSrcFar - rSrcNear). 00179 00180 virtual real64 MaxSrcRadiusGap (real64 maxDstGap) const = 0; 00181 00182 // Compute and return the maximum warped tangential gap. minDst is the 00183 // top-left pixel of the image in normalized pixel coordinates. maxDst is the 00184 // bottom-right pixel of the image in normalized pixel coordinates. 00185 // MaxSrcTanGap () computes the maximum absolute shift in normalized pixels 00186 // in the horizontal and vertical directions that can occur as a result of 00187 // the tangential warp. 00188 00189 virtual dng_point_real64 MaxSrcTanGap (dng_point_real64 minDst, 00190 dng_point_real64 maxDst) const = 0; 00191 00192 // Debug parameters. 00193 00194 virtual void Dump () const; 00195 00196 }; 00197 00198 /*****************************************************************************/ 00199 00204 00205 class dng_warp_params_rectilinear: public dng_warp_params 00206 { 00207 00208 public: 00209 00210 // Radial and tangential polynomial coefficients. These define a warp from 00211 // corrected pixel coordinates (xDst, yDst) to uncorrected pixel coordinates 00212 // (xSrc, ySrc) for each plane P as follows: 00213 // 00214 // Let kr0 = fRadParams [P][0] 00215 // kr1 = fRadParams [P][1] 00216 // kr2 = fRadParams [P][2] 00217 // kr3 = fRadParams [P][3] 00218 // 00219 // kt0 = fTanParams [P][0] 00220 // kt1 = fTanParams [P][1] 00221 // 00222 // Let (xCenter, yCenter) be the optical image center (see fCenter, below) 00223 // expressed in pixel coordinates. Let maxDist be the Euclidean distance (in 00224 // pixels) from (xCenter, yCenter) to the farthest image corner. 00225 // 00226 // First, compute the normalized distance of the corrected pixel position 00227 // (xDst, yDst) from the image center: 00228 // 00229 // dx = (xDst - xCenter) / maxDist 00230 // dy = (yDst - yCenter) / maxDist 00231 // 00232 // r^2 = dx^2 + dy^2 00233 // 00234 // Compute the radial correction term: 00235 // 00236 // ratio = kr0 + (kr1 * r^2) + (kr2 * r^4) + (kr3 * r^6) 00237 // 00238 // dxRad = dx * ratio 00239 // dyRad = dy * ratio 00240 // 00241 // Compute the tangential correction term: 00242 // 00243 // dxTan = (2 * kt0 * dx * dy) + kt1 * (r^2 + 2 * dx^2) 00244 // dyTan = (2 * kt1 * dx * dy) + kt0 * (r^2 + 2 * dy^2) 00245 // 00246 // Compute the uncorrected pixel position (xSrc, ySrc): 00247 // 00248 // xSrc = xCenter + (dxRad + dxTan) * maxDist 00249 // ySrc = yCenter + (dyRad + dyTan) * maxDist 00250 // 00251 // Mathematical definitions and restrictions: 00252 // 00253 // Let { xSrc, ySrc } = f (xDst, yDst) be the warp function defined above. 00254 // 00255 // Let xSrc = fx (xDst, yDst) be the x-component of the warp function. 00256 // Let ySrc = fy (xDst, yDst) be the y-component of the warp function. 00257 // 00258 // f (x, y) must be an invertible function. 00259 // 00260 // fx (x, y) must be an increasing function of x. 00261 // fy (x, y) must be an increasing function of x. 00262 // 00263 // The parameters kr0, kr1, kr2, and kr3 must define an increasing radial 00264 // warp function. Specifically, let w (r) be the radial warp function: 00265 // 00266 // w (r) = (kr0 * r) + (kr1 * r^3) + (kr2 * r^5) + (kr3 * r^7). 00267 // 00268 // w (r) must be an increasing function. 00269 00270 dng_vector fRadParams [kMaxColorPlanes]; 00271 dng_vector fTanParams [kMaxColorPlanes]; 00272 00273 public: 00274 00275 dng_warp_params_rectilinear (); 00276 00277 dng_warp_params_rectilinear (uint32 planes, 00278 const dng_vector radParams [], 00279 const dng_vector tanParams [], 00280 const dng_point_real64 &fCenter); 00281 00282 virtual ~dng_warp_params_rectilinear (); 00283 00284 // Overridden methods. 00285 00286 virtual bool IsRadNOP (uint32 plane) const; 00287 00288 virtual bool IsTanNOP (uint32 plane) const; 00289 00290 virtual bool IsValid () const; 00291 00292 virtual void PropagateToAllPlanes (uint32 totalPlanes); 00293 00294 virtual real64 Evaluate (uint32 plane, 00295 real64 r) const; 00296 00297 virtual real64 EvaluateRatio (uint32 plane, 00298 real64 r2) const; 00299 00300 virtual dng_point_real64 EvaluateTangential (uint32 plane, 00301 real64 r2, 00302 const dng_point_real64 &diff, 00303 const dng_point_real64 &diff2) const; 00304 00305 virtual real64 MaxSrcRadiusGap (real64 maxDstGap) const; 00306 00307 virtual dng_point_real64 MaxSrcTanGap (dng_point_real64 minDst, 00308 dng_point_real64 maxDst) const; 00309 00310 virtual void Dump () const; 00311 00312 }; 00313 00314 /*****************************************************************************/ 00315 00318 00319 class dng_warp_params_fisheye: public dng_warp_params 00320 { 00321 00322 public: 00323 00324 // Radial warp coefficients. These define a warp from corrected pixel 00325 // coordinates (xDst, yDst) to uncorrected pixel coordinates (xSrc, ySrc) for 00326 // each plane P as follows: 00327 // 00328 // Let kr0 = fRadParams [P][0] 00329 // kr1 = fRadParams [P][1] 00330 // kr2 = fRadParams [P][2] 00331 // kr3 = fRadParams [P][3] 00332 // 00333 // Let (xCenter, yCenter) be the optical image center (see fCenter, below) 00334 // expressed in pixel coordinates. Let maxDist be the Euclidean distance (in 00335 // pixels) from (xCenter, yCenter) to the farthest image corner. 00336 // 00337 // First, compute the normalized distance of the corrected pixel position 00338 // (xDst, yDst) from the image center: 00339 // 00340 // dx = (xDst - xCenter) / maxDist 00341 // dy = (yDst - yCenter) / maxDist 00342 // 00343 // r = sqrt (dx^2 + dy^2) 00344 // 00345 // Compute the radial correction term: 00346 // 00347 // t = atan (r) 00348 // 00349 // rWarp = (kr0 * t) + (kr1 * t^3) + (kr2 * t^5) + (kr3 * t^7) 00350 // 00351 // ratio = rWarp / r 00352 // 00353 // dxRad = dx * ratio 00354 // dyRad = dy * ratio 00355 // 00356 // Compute the uncorrected pixel position (xSrc, ySrc): 00357 // 00358 // xSrc = xCenter + (dxRad * maxDist) 00359 // ySrc = yCenter + (dyRad * maxDist) 00360 // 00361 // The parameters kr0, kr1, kr2, and kr3 must define an increasing radial 00362 // warp function. Specifically, let w (r) be the radial warp function: 00363 // 00364 // t = atan (r) 00365 // 00366 // w (r) = (kr0 * t) + (kr1 * t^3) + (kr2 * t^5) + (kr3 * t^7). 00367 // 00368 // w (r) must be an increasing function. 00369 00370 dng_vector fRadParams [kMaxColorPlanes]; 00371 00372 public: 00373 00374 dng_warp_params_fisheye (); 00375 00376 dng_warp_params_fisheye (uint32 planes, 00377 const dng_vector radParams [], 00378 const dng_point_real64 &fCenter); 00379 00380 virtual ~dng_warp_params_fisheye (); 00381 00382 // Overridden methods. 00383 00384 virtual bool IsRadNOP (uint32 plane) const; 00385 00386 virtual bool IsTanNOP (uint32 plane) const; 00387 00388 virtual bool IsValid () const; 00389 00390 virtual void PropagateToAllPlanes (uint32 totalPlanes); 00391 00392 virtual real64 Evaluate (uint32 plane, 00393 real64 r) const; 00394 00395 virtual real64 EvaluateRatio (uint32 plane, 00396 real64 r2) const; 00397 00398 virtual dng_point_real64 EvaluateTangential (uint32 plane, 00399 real64 r2, 00400 const dng_point_real64 &diff, 00401 const dng_point_real64 &diff2) const; 00402 00403 virtual real64 MaxSrcRadiusGap (real64 maxDstGap) const; 00404 00405 virtual dng_point_real64 MaxSrcTanGap (dng_point_real64 minDst, 00406 dng_point_real64 maxDst) const; 00407 00408 virtual void Dump () const; 00409 00410 }; 00411 00412 /*****************************************************************************/ 00413 00415 00416 class dng_opcode_WarpRectilinear: public dng_opcode 00417 { 00418 00419 protected: 00420 00421 dng_warp_params_rectilinear fWarpParams; 00422 00423 public: 00424 00425 dng_opcode_WarpRectilinear (const dng_warp_params_rectilinear ¶ms, 00426 uint32 flags); 00427 00428 explicit dng_opcode_WarpRectilinear (dng_stream &stream); 00429 00430 // Overridden methods. 00431 00432 virtual bool IsNOP () const; 00433 00434 virtual bool IsValidForNegative (const dng_negative &negative) const; 00435 00436 virtual void PutData (dng_stream &stream) const; 00437 00438 virtual void Apply (dng_host &host, 00439 dng_negative &negative, 00440 AutoPtr<dng_image> &image); 00441 00442 protected: 00443 00444 static uint32 ParamBytes (uint32 planes); 00445 00446 }; 00447 00448 /*****************************************************************************/ 00449 00451 00452 class dng_opcode_WarpFisheye: public dng_opcode 00453 { 00454 00455 protected: 00456 00457 dng_warp_params_fisheye fWarpParams; 00458 00459 public: 00460 00461 dng_opcode_WarpFisheye (const dng_warp_params_fisheye ¶ms, 00462 uint32 flags); 00463 00464 explicit dng_opcode_WarpFisheye (dng_stream &stream); 00465 00466 // Overridden methods. 00467 00468 virtual bool IsNOP () const; 00469 00470 virtual bool IsValidForNegative (const dng_negative &negative) const; 00471 00472 virtual void PutData (dng_stream &stream) const; 00473 00474 virtual void Apply (dng_host &host, 00475 dng_negative &negative, 00476 AutoPtr<dng_image> &image); 00477 00478 protected: 00479 00480 static uint32 ParamBytes (uint32 planes); 00481 00482 }; 00483 00484 /*****************************************************************************/ 00485 00488 00489 class dng_vignette_radial_params 00490 { 00491 00492 public: 00493 00494 static const uint32 kNumTerms = 5; 00495 00496 public: 00497 00498 // Let v be an uncorrected pixel value of a pixel p in linear space. 00499 // 00500 // Let r be the Euclidean distance between p and the optical center. 00501 // 00502 // Compute corrected pixel value v' = v * g, where g is the gain. 00503 // 00504 // Let k0 = fParams [0] 00505 // Let k1 = fParams [1] 00506 // Let k2 = fParams [2] 00507 // Let k3 = fParams [3] 00508 // Let k4 = fParams [4] 00509 // 00510 // Gain g = 1 + (k0 * r^2) + (k1 * r^4) + (k2 * r^6) + (k3 * r^8) + (k4 * r^10) 00511 00512 std::vector<real64> fParams; 00513 00514 dng_point_real64 fCenter; 00515 00516 public: 00517 00518 dng_vignette_radial_params (); 00519 00520 dng_vignette_radial_params (const std::vector<real64> ¶ms, 00521 const dng_point_real64 ¢er); 00522 00523 bool IsNOP () const; 00524 00525 bool IsValid () const; 00526 00527 // For debugging. 00528 00529 void Dump () const; 00530 00531 }; 00532 00533 /*****************************************************************************/ 00534 00536 00537 class dng_opcode_FixVignetteRadial: public dng_inplace_opcode 00538 { 00539 00540 protected: 00541 00542 dng_vignette_radial_params fParams; 00543 00544 uint32 fImagePlanes; 00545 00546 int64 fSrcOriginH; 00547 int64 fSrcOriginV; 00548 00549 int64 fSrcStepH; 00550 int64 fSrcStepV; 00551 00552 uint32 fTableInputBits; 00553 uint32 fTableOutputBits; 00554 00555 AutoPtr<dng_memory_block> fGainTable; 00556 00557 AutoPtr<dng_memory_block> fMaskBuffers [kMaxMPThreads]; 00558 00559 public: 00560 00561 dng_opcode_FixVignetteRadial (const dng_vignette_radial_params ¶ms, 00562 uint32 flags); 00563 00564 explicit dng_opcode_FixVignetteRadial (dng_stream &stream); 00565 00566 virtual bool IsNOP () const; 00567 00568 virtual bool IsValidForNegative (const dng_negative &) const; 00569 00570 virtual void PutData (dng_stream &stream) const; 00571 00572 virtual uint32 BufferPixelType (uint32 /* imagePixelType */) 00573 { 00574 return ttSShort; 00575 } 00576 00577 virtual void Prepare (dng_negative &negative, 00578 uint32 threadCount, 00579 const dng_point &tileSize, 00580 const dng_rect &imageBounds, 00581 uint32 imagePlanes, 00582 uint32 bufferPixelType, 00583 dng_memory_allocator &allocator); 00584 00585 virtual void ProcessArea (dng_negative &negative, 00586 uint32 threadIndex, 00587 dng_pixel_buffer &buffer, 00588 const dng_rect &dstArea, 00589 const dng_rect &imageBounds); 00590 00591 protected: 00592 00593 static uint32 ParamBytes (); 00594 00595 }; 00596 00597 /*****************************************************************************/ 00598 00599 #endif 00600 00601 /*****************************************************************************/ |