blob: 5d7af2f819a004a70dbfe37998708b30b5535fee [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% RRRR EEEEE SSSSS AAA M M PPPP L EEEEE %
7% R R E SS A A MM MM P P L E %
8% RRRR EEE SSS AAAAA M M M PPPP L EEE %
9% R R E SS A A M M P L E %
10% R R EEEEE SSSSS A A M M P LLLLL EEEEE %
11% %
12% %
13% MagickCore Pixel Resampling Methods %
14% %
15% Software Design %
16% John Cristy %
17% Anthony Thyssen %
18% August 2007 %
19% %
20% %
cristy1454be72011-12-19 01:52:48 +000021% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000022% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% http://www.imagemagick.org/script/license.php %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/color-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/draw.h"
48#include "MagickCore/exception-private.h"
49#include "MagickCore/gem.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/log.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/pixel.h"
56#include "MagickCore/pixel-accessor.h"
57#include "MagickCore/quantum.h"
58#include "MagickCore/random_.h"
59#include "MagickCore/resample.h"
60#include "MagickCore/resize.h"
61#include "MagickCore/resize-private.h"
cristyac245f82012-05-05 17:13:57 +000062#include "MagickCore/resource_.h"
cristy63a81872012-03-22 15:52:52 +000063#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/transform.h"
65#include "MagickCore/signature-private.h"
66#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000067#include "MagickCore/utility-private.h"
anthony9cb63cc2012-04-25 06:10:49 +000068#include "MagickCore/option.h"
cristy3ed852e2009-09-05 21:47:34 +000069/*
anthony490ab032010-09-20 00:02:08 +000070 EWA Resampling Options
71*/
anthonyc7b82f22010-09-27 10:42:29 +000072
73/* select ONE resampling method */
74#define EWA 1 /* Normal EWA handling - raw or clamped */
75 /* if 0 then use "High Quality EWA" */
76#define EWA_CLAMP 1 /* EWA Clamping from Nicolas Robidoux */
77
anthony5b697cd2010-10-10 03:48:57 +000078#define FILTER_LUT 1 /* Use a LUT rather then direct filter calls */
79
anthonyc7b82f22010-09-27 10:42:29 +000080/* output debugging information */
anthony490ab032010-09-20 00:02:08 +000081#define DEBUG_ELLIPSE 0 /* output ellipse info for debug */
anthony2e6ab682010-09-28 12:02:25 +000082#define DEBUG_HIT_MISS 0 /* output hit/miss pixels (as gnuplot commands) */
83#define DEBUG_NO_PIXEL_HIT 0 /* Make pixels that fail to hit anything - RED */
anthony490ab032010-09-20 00:02:08 +000084
anthony5b697cd2010-10-10 03:48:57 +000085#if ! FILTER_DIRECT
86#define WLUT_WIDTH 1024 /* size of the filter cache */
87#endif
88
anthony490ab032010-09-20 00:02:08 +000089/*
cristy3ed852e2009-09-05 21:47:34 +000090 Typedef declarations.
91*/
cristy3ed852e2009-09-05 21:47:34 +000092struct _ResampleFilter
93{
cristy3ed852e2009-09-05 21:47:34 +000094 CacheView
95 *view;
96
cristyc4c8d132010-01-07 01:58:38 +000097 Image
98 *image;
99
cristy3ed852e2009-09-05 21:47:34 +0000100 ExceptionInfo
101 *exception;
102
103 MagickBooleanType
104 debug;
105
106 /* Information about image being resampled */
cristybb503372010-05-27 20:51:26 +0000107 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000108 image_area;
109
cristy5c4e2582011-09-11 19:21:03 +0000110 PixelInterpolateMethod
cristy3ed852e2009-09-05 21:47:34 +0000111 interpolate;
112
113 VirtualPixelMethod
114 virtual_pixel;
115
116 FilterTypes
117 filter;
118
119 /* processing settings needed */
120 MagickBooleanType
121 limit_reached,
122 do_interpolate,
123 average_defined;
124
cristy4c08aed2011-07-01 19:47:50 +0000125 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000126 average_pixel;
127
128 /* current ellipitical area being resampled around center point */
129 double
130 A, B, C,
anthonyd638d312010-09-15 13:13:01 +0000131 Vlimit, Ulimit, Uwidth, slope;
cristy3ed852e2009-09-05 21:47:34 +0000132
anthony175defe2010-10-10 04:28:31 +0000133#if FILTER_LUT
cristy3ed852e2009-09-05 21:47:34 +0000134 /* LUT of weights for filtered average in elliptical area */
135 double
anthony5b697cd2010-10-10 03:48:57 +0000136 filter_lut[WLUT_WIDTH];
137#else
138 /* Use a Direct call to the filter functions */
139 ResizeFilter
140 *filter_def;
anthony582b6d72010-10-10 06:45:41 +0000141
142 double
143 F;
anthony5b697cd2010-10-10 03:48:57 +0000144#endif
145
146 /* the practical working support of the filter */
147 double
cristy3ed852e2009-09-05 21:47:34 +0000148 support;
149
cristybb503372010-05-27 20:51:26 +0000150 size_t
cristy3ed852e2009-09-05 21:47:34 +0000151 signature;
152};
153
154/*
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156% %
157% %
158% %
159% A c q u i r e R e s a m p l e I n f o %
160% %
161% %
162% %
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164%
165% AcquireResampleFilter() initializes the information resample needs do to a
166% scaled lookup of a color from an image, using area sampling.
167%
168% The algorithm is based on a Elliptical Weighted Average, where the pixels
169% found in a large elliptical area is averaged together according to a
170% weighting (filter) function. For more details see "Fundamentals of Texture
171% Mapping and Image Warping" a master's thesis by Paul.S.Heckbert, June 17,
172% 1989. Available for free from, http://www.cs.cmu.edu/~ph/
173%
174% As EWA resampling (or any sort of resampling) can require a lot of
175% calculations to produce a distorted scaling of the source image for each
176% output pixel, the ResampleFilter structure generated holds that information
177% between individual image resampling.
178%
179% This function will make the appropriate AcquireCacheView() calls
180% to view the image, calling functions do not need to open a cache view.
181%
182% Usage Example...
183% resample_filter=AcquireResampleFilter(image,exception);
anthony9cb63cc2012-04-25 06:10:49 +0000184% SetResampleFilter(resample_filter, GaussianFilter);
cristybb503372010-05-27 20:51:26 +0000185% for (y=0; y < (ssize_t) image->rows; y++) {
186% for (x=0; x < (ssize_t) image->columns; x++) {
anthonyc7b82f22010-09-27 10:42:29 +0000187% u= ....; v= ....;
cristy3ed852e2009-09-05 21:47:34 +0000188% ScaleResampleFilter(resample_filter, ... scaling vectors ...);
anthonyc7b82f22010-09-27 10:42:29 +0000189% (void) ResamplePixelColor(resample_filter,u,v,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000190% ... assign resampled pixel value ...
191% }
192% }
193% DestroyResampleFilter(resample_filter);
194%
195% The format of the AcquireResampleFilter method is:
196%
197% ResampleFilter *AcquireResampleFilter(const Image *image,
198% ExceptionInfo *exception)
199%
200% A description of each parameter follows:
201%
202% o image: the image.
203%
204% o exception: return any errors or warnings in this structure.
205%
206*/
207MagickExport ResampleFilter *AcquireResampleFilter(const Image *image,
208 ExceptionInfo *exception)
209{
210 register ResampleFilter
211 *resample_filter;
212
213 assert(image != (Image *) NULL);
214 assert(image->signature == MagickSignature);
215 if (image->debug != MagickFalse)
216 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
217 assert(exception != (ExceptionInfo *) NULL);
218 assert(exception->signature == MagickSignature);
219
220 resample_filter=(ResampleFilter *) AcquireMagickMemory(
221 sizeof(*resample_filter));
222 if (resample_filter == (ResampleFilter *) NULL)
223 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
224 (void) ResetMagickMemory(resample_filter,0,sizeof(*resample_filter));
225
cristy3ed852e2009-09-05 21:47:34 +0000226 resample_filter->exception=exception;
cristy2ab242e2011-03-11 02:45:46 +0000227 resample_filter->image=ReferenceImage((Image *) image);
cristydb070952012-04-20 14:33:00 +0000228 resample_filter->view=AcquireVirtualCacheView(resample_filter->image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000229
230 resample_filter->debug=IsEventLogging();
231 resample_filter->signature=MagickSignature;
232
anthony5b697cd2010-10-10 03:48:57 +0000233 resample_filter->image_area=(ssize_t) (image->columns*image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000234 resample_filter->average_defined = MagickFalse;
235
236 /* initialise the resampling filter settings */
cristyaa2c16c2012-03-25 22:21:35 +0000237 SetResampleFilter(resample_filter, image->filter);
238 (void) SetResampleFilterInterpolateMethod(resample_filter,image->interpolate);
cristy82fea932010-10-14 01:17:55 +0000239 (void) SetResampleFilterVirtualPixelMethod(resample_filter,
anthony72949792010-10-08 04:44:56 +0000240 GetImageVirtualPixelMethod(image));
cristy3ed852e2009-09-05 21:47:34 +0000241 return(resample_filter);
242}
243
244/*
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246% %
247% %
248% %
249% D e s t r o y R e s a m p l e I n f o %
250% %
251% %
252% %
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254%
255% DestroyResampleFilter() finalizes and cleans up the resampling
256% resample_filter as returned by AcquireResampleFilter(), freeing any memory
257% or other information as needed.
258%
259% The format of the DestroyResampleFilter method is:
260%
261% ResampleFilter *DestroyResampleFilter(ResampleFilter *resample_filter)
262%
263% A description of each parameter follows:
264%
265% o resample_filter: resampling information structure
266%
267*/
268MagickExport ResampleFilter *DestroyResampleFilter(
269 ResampleFilter *resample_filter)
270{
271 assert(resample_filter != (ResampleFilter *) NULL);
272 assert(resample_filter->signature == MagickSignature);
273 assert(resample_filter->image != (Image *) NULL);
274 if (resample_filter->debug != MagickFalse)
275 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
276 resample_filter->image->filename);
277 resample_filter->view=DestroyCacheView(resample_filter->view);
278 resample_filter->image=DestroyImage(resample_filter->image);
anthony5b697cd2010-10-10 03:48:57 +0000279#if ! FILTER_LUT
280 resample_filter->filter_def=DestroyResizeFilter(resample_filter->filter_def);
281#endif
cristy3ed852e2009-09-05 21:47:34 +0000282 resample_filter->signature=(~MagickSignature);
283 resample_filter=(ResampleFilter *) RelinquishMagickMemory(resample_filter);
284 return(resample_filter);
285}
286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289% %
290% %
291% %
cristy3ed852e2009-09-05 21:47:34 +0000292% R e s a m p l e P i x e l C o l o r %
293% %
294% %
295% %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298% ResamplePixelColor() samples the pixel values surrounding the location
299% given using an elliptical weighted average, at the scale previously
300% calculated, and in the most efficent manner possible for the
301% VirtualPixelMethod setting.
302%
303% The format of the ResamplePixelColor method is:
304%
305% MagickBooleanType ResamplePixelColor(ResampleFilter *resample_filter,
cristydb070952012-04-20 14:33:00 +0000306% const double u0,const double v0,PixelInfo *pixel,
307% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000308%
309% A description of each parameter follows:
310%
311% o resample_filter: the resample filter.
312%
313% o u0,v0: A double representing the center of the area to resample,
314% The distortion transformed transformed x,y coordinate.
315%
316% o pixel: the resampled pixel is returned here.
317%
cristydb070952012-04-20 14:33:00 +0000318% o exception: return any errors or warnings in this structure.
319%
cristy3ed852e2009-09-05 21:47:34 +0000320*/
321MagickExport MagickBooleanType ResamplePixelColor(
322 ResampleFilter *resample_filter,const double u0,const double v0,
cristydb070952012-04-20 14:33:00 +0000323 PixelInfo *pixel,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000324{
325 MagickBooleanType
326 status;
327
anthony490ab032010-09-20 00:02:08 +0000328 ssize_t u,v, v1, v2, uw, hit;
cristy3ed852e2009-09-05 21:47:34 +0000329 double u1;
330 double U,V,Q,DQ,DDQ;
331 double divisor_c,divisor_m;
332 register double weight;
cristy4c08aed2011-07-01 19:47:50 +0000333 register const Quantum *pixels;
cristy3ed852e2009-09-05 21:47:34 +0000334 assert(resample_filter != (ResampleFilter *) NULL);
335 assert(resample_filter->signature == MagickSignature);
336
337 status=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +0000338 /* GetPixelInfo(resample_filter->image,pixel); */
cristy3ed852e2009-09-05 21:47:34 +0000339 if ( resample_filter->do_interpolate ) {
cristyf931f072012-01-01 21:19:15 +0000340 status=InterpolatePixelInfo(resample_filter->image,resample_filter->view,
341 resample_filter->interpolate,u0,v0,pixel,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000342 return(status);
343 }
344
anthony2e6ab682010-09-28 12:02:25 +0000345#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +0000346 (void) FormatLocaleFile(stderr, "u0=%lf; v0=%lf;\n", u0, v0);
anthony2e6ab682010-09-28 12:02:25 +0000347#endif
348
cristy3ed852e2009-09-05 21:47:34 +0000349 /*
350 Does resample area Miss the image?
351 And is that area a simple solid color - then return that color
352 */
353 hit = 0;
354 switch ( resample_filter->virtual_pixel ) {
355 case BackgroundVirtualPixelMethod:
cristy3ed852e2009-09-05 21:47:34 +0000356 case TransparentVirtualPixelMethod:
357 case BlackVirtualPixelMethod:
358 case GrayVirtualPixelMethod:
359 case WhiteVirtualPixelMethod:
360 case MaskVirtualPixelMethod:
361 if ( resample_filter->limit_reached
anthonyd638d312010-09-15 13:13:01 +0000362 || u0 + resample_filter->Ulimit < 0.0
363 || u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
364 || v0 + resample_filter->Vlimit < 0.0
365 || v0 - resample_filter->Vlimit > (double) resample_filter->image->rows
cristy3ed852e2009-09-05 21:47:34 +0000366 )
367 hit++;
368 break;
369
370 case UndefinedVirtualPixelMethod:
371 case EdgeVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000372 if ( ( u0 + resample_filter->Ulimit < 0.0 && v0 + resample_filter->Vlimit < 0.0 )
373 || ( u0 + resample_filter->Ulimit < 0.0
374 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows )
375 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
376 && v0 + resample_filter->Vlimit < 0.0 )
377 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
378 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows )
cristy3ed852e2009-09-05 21:47:34 +0000379 )
380 hit++;
381 break;
382 case HorizontalTileVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000383 if ( v0 + resample_filter->Vlimit < 0.0
384 || v0 - resample_filter->Vlimit > (double) resample_filter->image->rows
cristy3ed852e2009-09-05 21:47:34 +0000385 )
386 hit++; /* outside the horizontally tiled images. */
387 break;
388 case VerticalTileVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000389 if ( u0 + resample_filter->Ulimit < 0.0
390 || u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
cristy3ed852e2009-09-05 21:47:34 +0000391 )
392 hit++; /* outside the vertically tiled images. */
393 break;
394 case DitherVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000395 if ( ( u0 + resample_filter->Ulimit < -32.0 && v0 + resample_filter->Vlimit < -32.0 )
396 || ( u0 + resample_filter->Ulimit < -32.0
397 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows+32.0 )
398 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns+32.0
399 && v0 + resample_filter->Vlimit < -32.0 )
400 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns+32.0
401 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows+32.0 )
cristy3ed852e2009-09-05 21:47:34 +0000402 )
403 hit++;
404 break;
405 case TileVirtualPixelMethod:
406 case MirrorVirtualPixelMethod:
407 case RandomVirtualPixelMethod:
408 case HorizontalTileEdgeVirtualPixelMethod:
409 case VerticalTileEdgeVirtualPixelMethod:
410 case CheckerTileVirtualPixelMethod:
411 /* resampling of area is always needed - no VP limits */
412 break;
413 }
414 if ( hit ) {
415 /* whole area is a solid color -- just return that color */
cristy4c08aed2011-07-01 19:47:50 +0000416 status=InterpolatePixelInfo(resample_filter->image,
cristyd76c51e2011-03-26 00:21:26 +0000417 resample_filter->view,IntegerInterpolatePixel,u0,v0,pixel,
418 resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000419 return(status);
420 }
421
422 /*
423 Scaling limits reached, return an 'averaged' result.
424 */
425 if ( resample_filter->limit_reached ) {
426 switch ( resample_filter->virtual_pixel ) {
427 /* This is always handled by the above, so no need.
428 case BackgroundVirtualPixelMethod:
429 case ConstantVirtualPixelMethod:
430 case TransparentVirtualPixelMethod:
431 case GrayVirtualPixelMethod,
432 case WhiteVirtualPixelMethod
433 case MaskVirtualPixelMethod:
434 */
435 case UndefinedVirtualPixelMethod:
436 case EdgeVirtualPixelMethod:
437 case DitherVirtualPixelMethod:
438 case HorizontalTileEdgeVirtualPixelMethod:
439 case VerticalTileEdgeVirtualPixelMethod:
anthony9b8a5282010-09-15 07:48:39 +0000440 /* We need an average edge pixel, from the correct edge!
cristy3ed852e2009-09-05 21:47:34 +0000441 How should I calculate an average edge color?
442 Just returning an averaged neighbourhood,
443 works well in general, but falls down for TileEdge methods.
444 This needs to be done properly!!!!!!
445 */
cristy4c08aed2011-07-01 19:47:50 +0000446 status=InterpolatePixelInfo(resample_filter->image,
cristyd76c51e2011-03-26 00:21:26 +0000447 resample_filter->view,AverageInterpolatePixel,u0,v0,pixel,
448 resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000449 break;
450 case HorizontalTileVirtualPixelMethod:
451 case VerticalTileVirtualPixelMethod:
452 /* just return the background pixel - Is there more direct way? */
cristy4c08aed2011-07-01 19:47:50 +0000453 status=InterpolatePixelInfo(resample_filter->image,
cristyd76c51e2011-03-26 00:21:26 +0000454 resample_filter->view,IntegerInterpolatePixel,-1.0,-1.0,pixel,
455 resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000456 break;
457 case TileVirtualPixelMethod:
458 case MirrorVirtualPixelMethod:
459 case RandomVirtualPixelMethod:
460 case CheckerTileVirtualPixelMethod:
461 default:
462 /* generate a average color of the WHOLE image */
463 if ( resample_filter->average_defined == MagickFalse ) {
464 Image
465 *average_image;
466
467 CacheView
468 *average_view;
469
cristy4c08aed2011-07-01 19:47:50 +0000470 GetPixelInfo(resample_filter->image,(PixelInfo *)
cristy065f8be2010-10-16 00:21:58 +0000471 &resample_filter->average_pixel);
472 resample_filter->average_defined=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000473
474 /* Try to get an averaged pixel color of whole image */
cristyaa2c16c2012-03-25 22:21:35 +0000475 average_image=ResizeImage(resample_filter->image,1,1,BoxFilter,
cristy065f8be2010-10-16 00:21:58 +0000476 resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000477 if (average_image == (Image *) NULL)
478 {
479 *pixel=resample_filter->average_pixel; /* FAILED */
480 break;
481 }
cristydb070952012-04-20 14:33:00 +0000482 average_view=AcquireVirtualCacheView(average_image,exception);
cristy4c08aed2011-07-01 19:47:50 +0000483 pixels=GetCacheViewVirtualPixels(average_view,0,0,1,1,
cristy3ed852e2009-09-05 21:47:34 +0000484 resample_filter->exception);
cristy4c08aed2011-07-01 19:47:50 +0000485 if (pixels == (const Quantum *) NULL) {
cristy3ed852e2009-09-05 21:47:34 +0000486 average_view=DestroyCacheView(average_view);
487 average_image=DestroyImage(average_image);
488 *pixel=resample_filter->average_pixel; /* FAILED */
489 break;
490 }
cristy803640d2011-11-17 02:11:32 +0000491 GetPixelInfoPixel(resample_filter->image,pixels,
cristy3ed852e2009-09-05 21:47:34 +0000492 &(resample_filter->average_pixel));
493 average_view=DestroyCacheView(average_view);
494 average_image=DestroyImage(average_image);
anthony490ab032010-09-20 00:02:08 +0000495
496 if ( resample_filter->virtual_pixel == CheckerTileVirtualPixelMethod )
497 {
anthony9cb63cc2012-04-25 06:10:49 +0000498 /* CheckerTile is a alpha blend of the image's average pixel
499 color and the current background color */
anthony490ab032010-09-20 00:02:08 +0000500
anthony9cb63cc2012-04-25 06:10:49 +0000501 /* image's average pixel color */
cristy4c08aed2011-07-01 19:47:50 +0000502 weight = QuantumScale*((MagickRealType)
503 resample_filter->average_pixel.alpha);
anthony490ab032010-09-20 00:02:08 +0000504 resample_filter->average_pixel.red *= weight;
505 resample_filter->average_pixel.green *= weight;
506 resample_filter->average_pixel.blue *= weight;
507 divisor_c = weight;
508
anthony9cb63cc2012-04-25 06:10:49 +0000509 /* background color */
cristy4c08aed2011-07-01 19:47:50 +0000510 weight = QuantumScale*((MagickRealType)
511 resample_filter->image->background_color.alpha);
anthony490ab032010-09-20 00:02:08 +0000512 resample_filter->average_pixel.red +=
513 weight*resample_filter->image->background_color.red;
514 resample_filter->average_pixel.green +=
515 weight*resample_filter->image->background_color.green;
516 resample_filter->average_pixel.blue +=
517 weight*resample_filter->image->background_color.blue;
cristy4c08aed2011-07-01 19:47:50 +0000518 resample_filter->average_pixel.alpha +=
519 resample_filter->image->background_color.alpha;
anthony490ab032010-09-20 00:02:08 +0000520 divisor_c += weight;
521
anthony9cb63cc2012-04-25 06:10:49 +0000522 /* alpha blend */
anthony490ab032010-09-20 00:02:08 +0000523 resample_filter->average_pixel.red /= divisor_c;
524 resample_filter->average_pixel.green /= divisor_c;
525 resample_filter->average_pixel.blue /= divisor_c;
anthony9cb63cc2012-04-25 06:10:49 +0000526 resample_filter->average_pixel.alpha /= 2; /* 50% blend */
anthony490ab032010-09-20 00:02:08 +0000527
528 }
cristy3ed852e2009-09-05 21:47:34 +0000529 }
530 *pixel=resample_filter->average_pixel;
531 break;
532 }
533 return(status);
534 }
535
536 /*
537 Initialize weighted average data collection
538 */
539 hit = 0;
540 divisor_c = 0.0;
541 divisor_m = 0.0;
542 pixel->red = pixel->green = pixel->blue = 0.0;
cristy4c08aed2011-07-01 19:47:50 +0000543 if (pixel->colorspace == CMYKColorspace)
544 pixel->black = 0.0;
545 if (pixel->matte != MagickFalse)
546 pixel->alpha = 0.0;
cristy3ed852e2009-09-05 21:47:34 +0000547
548 /*
549 Determine the parellelogram bounding box fitted to the ellipse
550 centered at u0,v0. This area is bounding by the lines...
cristy3ed852e2009-09-05 21:47:34 +0000551 */
anthony490ab032010-09-20 00:02:08 +0000552 v1 = (ssize_t)ceil(v0 - resample_filter->Vlimit); /* range of scan lines */
553 v2 = (ssize_t)floor(v0 + resample_filter->Vlimit);
cristy3ed852e2009-09-05 21:47:34 +0000554
anthony490ab032010-09-20 00:02:08 +0000555 /* scan line start and width accross the parallelogram */
556 u1 = u0 + (v1-v0)*resample_filter->slope - resample_filter->Uwidth;
557 uw = (ssize_t)(2.0*resample_filter->Uwidth)+1;
558
559#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +0000560 (void) FormatLocaleFile(stderr, "v1=%ld; v2=%ld\n", (long)v1, (long)v2);
561 (void) FormatLocaleFile(stderr, "u1=%ld; uw=%ld\n", (long)u1, (long)uw);
anthony490ab032010-09-20 00:02:08 +0000562#else
563# define DEBUG_HIT_MISS 0 /* only valid if DEBUG_ELLIPSE is enabled */
564#endif
cristy3ed852e2009-09-05 21:47:34 +0000565
566 /*
567 Do weighted resampling of all pixels, within the scaled ellipse,
568 bound by a Parellelogram fitted to the ellipse.
569 */
570 DDQ = 2*resample_filter->A;
anthony490ab032010-09-20 00:02:08 +0000571 for( v=v1; v<=v2; v++ ) {
572#if DEBUG_HIT_MISS
573 long uu = ceil(u1); /* actual pixel location (for debug only) */
cristy5acdd942011-05-27 19:45:39 +0000574 (void) FormatLocaleFile(stderr, "# scan line from pixel %ld, %ld\n", (long)uu, (long)v);
anthony490ab032010-09-20 00:02:08 +0000575#endif
576 u = (ssize_t)ceil(u1); /* first pixel in scanline */
577 u1 += resample_filter->slope; /* start of next scan line */
578
579
580 /* location of this first pixel, relative to u0,v0 */
581 U = (double)u-u0;
cristy3ed852e2009-09-05 21:47:34 +0000582 V = (double)v-v0;
583
584 /* Q = ellipse quotent ( if Q<F then pixel is inside ellipse) */
anthony490ab032010-09-20 00:02:08 +0000585 Q = (resample_filter->A*U + resample_filter->B*V)*U + resample_filter->C*V*V;
cristy3ed852e2009-09-05 21:47:34 +0000586 DQ = resample_filter->A*(2.0*U+1) + resample_filter->B*V;
587
588 /* get the scanline of pixels for this v */
cristybb503372010-05-27 20:51:26 +0000589 pixels=GetCacheViewVirtualPixels(resample_filter->view,u,v,(size_t) uw,
cristy3ed852e2009-09-05 21:47:34 +0000590 1,resample_filter->exception);
cristy4c08aed2011-07-01 19:47:50 +0000591 if (pixels == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000592 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000593
594 /* count up the weighted pixel colors */
595 for( u=0; u<uw; u++ ) {
anthony5b697cd2010-10-10 03:48:57 +0000596#if FILTER_LUT
cristy3ed852e2009-09-05 21:47:34 +0000597 /* Note that the ellipse has been pre-scaled so F = WLUT_WIDTH */
598 if ( Q < (double)WLUT_WIDTH ) {
599 weight = resample_filter->filter_lut[(int)Q];
anthony5b697cd2010-10-10 03:48:57 +0000600#else
601 /* Note that the ellipse has been pre-scaled so F = support^2 */
anthony582b6d72010-10-10 06:45:41 +0000602 if ( Q < (double)resample_filter->F ) {
603 weight = GetResizeFilterWeight(resample_filter->filter_def,
604 sqrt(Q)); /* a SquareRoot! Arrggghhhhh... */
anthony5b697cd2010-10-10 03:48:57 +0000605#endif
cristy3ed852e2009-09-05 21:47:34 +0000606
cristy4c08aed2011-07-01 19:47:50 +0000607 pixel->alpha += weight*GetPixelAlpha(resample_filter->image,pixels);
cristy3ed852e2009-09-05 21:47:34 +0000608 divisor_m += weight;
609
cristycb180922011-03-11 14:41:24 +0000610 if (pixel->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000611 weight *= QuantumScale*((MagickRealType) GetPixelAlpha(resample_filter->image,pixels));
612 pixel->red += weight*GetPixelRed(resample_filter->image,pixels);
613 pixel->green += weight*GetPixelGreen(resample_filter->image,pixels);
614 pixel->blue += weight*GetPixelBlue(resample_filter->image,pixels);
cristy2ab242e2011-03-11 02:45:46 +0000615 if (pixel->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000616 pixel->black += weight*GetPixelBlack(resample_filter->image,pixels);
cristy3ed852e2009-09-05 21:47:34 +0000617 divisor_c += weight;
618
619 hit++;
anthony490ab032010-09-20 00:02:08 +0000620#if DEBUG_HIT_MISS
621 /* mark the pixel according to hit/miss of the ellipse */
cristy5acdd942011-05-27 19:45:39 +0000622 (void) FormatLocaleFile(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 3\n",
anthony490ab032010-09-20 00:02:08 +0000623 (long)uu-.1,(double)v-.1,(long)uu+.1,(long)v+.1);
cristy5acdd942011-05-27 19:45:39 +0000624 (void) FormatLocaleFile(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 3\n",
anthony490ab032010-09-20 00:02:08 +0000625 (long)uu+.1,(double)v-.1,(long)uu-.1,(long)v+.1);
626 } else {
cristy5acdd942011-05-27 19:45:39 +0000627 (void) FormatLocaleFile(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 1\n",
anthony490ab032010-09-20 00:02:08 +0000628 (long)uu-.1,(double)v-.1,(long)uu+.1,(long)v+.1);
cristy5acdd942011-05-27 19:45:39 +0000629 (void) FormatLocaleFile(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 1\n",
anthony490ab032010-09-20 00:02:08 +0000630 (long)uu+.1,(double)v-.1,(long)uu-.1,(long)v+.1);
cristy3ed852e2009-09-05 21:47:34 +0000631 }
anthony490ab032010-09-20 00:02:08 +0000632 uu++;
633#else
634 }
635#endif
cristyed231572011-07-14 02:18:59 +0000636 pixels+=GetPixelChannels(resample_filter->image);
cristy3ed852e2009-09-05 21:47:34 +0000637 Q += DQ;
638 DQ += DDQ;
639 }
640 }
anthony490ab032010-09-20 00:02:08 +0000641#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +0000642 (void) FormatLocaleFile(stderr, "Hit=%ld; Total=%ld;\n", (long)hit, (long)uw*(v2-v1) );
anthony490ab032010-09-20 00:02:08 +0000643#endif
cristy3ed852e2009-09-05 21:47:34 +0000644
645 /*
646 Result sanity check -- this should NOT happen
647 */
anthony490ab032010-09-20 00:02:08 +0000648 if ( hit == 0 ) {
cristy3ed852e2009-09-05 21:47:34 +0000649 /* not enough pixels in resampling, resort to direct interpolation */
anthony490ab032010-09-20 00:02:08 +0000650#if DEBUG_NO_PIXEL_HIT
cristy4c08aed2011-07-01 19:47:50 +0000651 pixel->alpha = pixel->red = pixel->green = pixel->blue = 0;
anthony9b8a5282010-09-15 07:48:39 +0000652 pixel->red = QuantumRange; /* show pixels for which EWA fails */
653#else
cristy4c08aed2011-07-01 19:47:50 +0000654 status=InterpolatePixelInfo(resample_filter->image,
cristyd76c51e2011-03-26 00:21:26 +0000655 resample_filter->view,resample_filter->interpolate,u0,v0,pixel,
656 resample_filter->exception);
anthony9b8a5282010-09-15 07:48:39 +0000657#endif
cristy3ed852e2009-09-05 21:47:34 +0000658 return status;
659 }
660
661 /*
662 Finialize results of resampling
663 */
664 divisor_m = 1.0/divisor_m;
cristy4c08aed2011-07-01 19:47:50 +0000665 pixel->alpha = (MagickRealType) ClampToQuantum(divisor_m*pixel->alpha);
cristy3ed852e2009-09-05 21:47:34 +0000666 divisor_c = 1.0/divisor_c;
cristyce70c172010-01-07 17:15:30 +0000667 pixel->red = (MagickRealType) ClampToQuantum(divisor_c*pixel->red);
668 pixel->green = (MagickRealType) ClampToQuantum(divisor_c*pixel->green);
669 pixel->blue = (MagickRealType) ClampToQuantum(divisor_c*pixel->blue);
cristy2ab242e2011-03-11 02:45:46 +0000670 if (pixel->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000671 pixel->black = (MagickRealType) ClampToQuantum(divisor_c*pixel->black);
cristy3ed852e2009-09-05 21:47:34 +0000672 return(MagickTrue);
673}
674
anthonyc7b82f22010-09-27 10:42:29 +0000675#if EWA && EWA_CLAMP
676/*
677%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678% %
679% %
680% %
681- C l a m p U p A x e s %
682% %
683% %
684% %
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686%
nicolasc90935c2010-09-27 16:47:39 +0000687% ClampUpAxes() function converts the input vectors into a major and
nicolas40ae4632010-10-28 13:03:53 +0000688% minor axis unit vectors, and their magnitude. This allows us to
689% ensure that the ellipse generated is never smaller than the unit
nicolasc90935c2010-09-27 16:47:39 +0000690% circle and thus never too small for use in EWA resampling.
anthonyc7b82f22010-09-27 10:42:29 +0000691%
nicolasc90935c2010-09-27 16:47:39 +0000692% This purely mathematical 'magic' was provided by Professor Nicolas
693% Robidoux and his Masters student Chantal Racette.
anthonyc7b82f22010-09-27 10:42:29 +0000694%
nicolas40ae4632010-10-28 13:03:53 +0000695% Reference: "We Recommend Singular Value Decomposition", David Austin
anthonyc7b82f22010-09-27 10:42:29 +0000696% http://www.ams.org/samplings/feature-column/fcarc-svd
697%
nicolas40ae4632010-10-28 13:03:53 +0000698% By generating major and minor axis vectors, we can actually use the
nicolasc90935c2010-09-27 16:47:39 +0000699% ellipse in its "canonical form", by remapping the dx,dy of the
700% sampled point into distances along the major and minor axis unit
701% vectors.
nicolas40ae4632010-10-28 13:03:53 +0000702%
703% Reference: http://en.wikipedia.org/wiki/Ellipse#Canonical_form
anthonyc7b82f22010-09-27 10:42:29 +0000704*/
nicolas15c331b2010-09-29 19:05:00 +0000705static inline void ClampUpAxes(const double dux,
706 const double dvx,
707 const double duy,
708 const double dvy,
709 double *major_mag,
710 double *minor_mag,
711 double *major_unit_x,
712 double *major_unit_y,
713 double *minor_unit_x,
714 double *minor_unit_y)
anthonyc7b82f22010-09-27 10:42:29 +0000715{
716 /*
717 * ClampUpAxes takes an input 2x2 matrix
718 *
719 * [ a b ] = [ dux duy ]
720 * [ c d ] = [ dvx dvy ]
721 *
722 * and computes from it the major and minor axis vectors [major_x,
723 * major_y] and [minor_x,minor_y] of the smallest ellipse containing
724 * both the unit disk and the ellipse which is the image of the unit
725 * disk by the linear transformation
726 *
727 * [ dux duy ] [S] = [s]
728 * [ dvx dvy ] [T] = [t]
729 *
730 * (The vector [S,T] is the difference between a position in output
731 * space and [X,Y]; the vector [s,t] is the difference between a
732 * position in input space and [x,y].)
733 */
734 /*
nicolas082f7e42011-05-04 05:34:48 +0000735 * Output:
anthonyc7b82f22010-09-27 10:42:29 +0000736 *
737 * major_mag is the half-length of the major axis of the "new"
nicolas40ae4632010-10-28 13:03:53 +0000738 * ellipse.
anthonyc7b82f22010-09-27 10:42:29 +0000739 *
740 * minor_mag is the half-length of the minor axis of the "new"
nicolas40ae4632010-10-28 13:03:53 +0000741 * ellipse.
anthonyc7b82f22010-09-27 10:42:29 +0000742 *
743 * major_unit_x is the x-coordinate of the major axis direction vector
744 * of both the "old" and "new" ellipses.
745 *
746 * major_unit_y is the y-coordinate of the major axis direction vector.
747 *
748 * minor_unit_x is the x-coordinate of the minor axis direction vector.
749 *
750 * minor_unit_y is the y-coordinate of the minor axis direction vector.
751 *
752 * Unit vectors are useful for computing projections, in particular,
753 * to compute the distance between a point in output space and the
nicolas082f7e42011-05-04 05:34:48 +0000754 * center of a unit disk in output space, using the position of the
755 * corresponding point [s,t] in input space. Following the clamping,
756 * the square of this distance is
757 *
758 * ( ( s * major_unit_x + t * major_unit_y ) / major_mag )^2
759 * +
760 * ( ( s * minor_unit_x + t * minor_unit_y ) / minor_mag )^2
761 *
762 * If such distances will be computed for many [s,t]'s, it makes
763 * sense to actually compute the reciprocal of major_mag and
764 * minor_mag and multiply them by the above unit lengths.
nicolasc90935c2010-09-27 16:47:39 +0000765 *
766 * Now, if you want to modify the input pair of tangent vectors so
767 * that it defines the modified ellipse, all you have to do is set
768 *
nicolas8b1d9812010-09-29 18:41:55 +0000769 * newdux = major_mag * major_unit_x
770 * newdvx = major_mag * major_unit_y
771 * newduy = minor_mag * minor_unit_x = minor_mag * -major_unit_y
772 * newdvy = minor_mag * minor_unit_y = minor_mag * major_unit_x
nicolasc90935c2010-09-27 16:47:39 +0000773 *
nicolas932ef842010-10-27 16:05:12 +0000774 * and use these tangent vectors as if they were the original ones.
nicolas40ae4632010-10-28 13:03:53 +0000775 * Usually, this is a drastic change in the tangent vectors even if
nicolasc263aa72010-10-29 01:09:44 +0000776 * the singular values are not clamped; for example, the minor axis
777 * vector always points in a direction which is 90 degrees
778 * counterclockwise from the direction of the major axis vector.
anthonyc7b82f22010-09-27 10:42:29 +0000779 */
780 /*
781 * Discussion:
782 *
783 * GOAL: Fix things so that the pullback, in input space, of a disk
784 * of radius r in output space is an ellipse which contains, at
785 * least, a disc of radius r. (Make this hold for any r>0.)
786 *
nicolas40ae4632010-10-28 13:03:53 +0000787 * ESSENCE OF THE METHOD: Compute the product of the first two
788 * factors of an SVD of the linear transformation defining the
nicolasf170e5f2010-10-27 13:21:30 +0000789 * ellipse and make sure that both its columns have norm at least 1.
790 * Because rotations and reflexions map disks to themselves, it is
nicolas40ae4632010-10-28 13:03:53 +0000791 * not necessary to compute the third (rightmost) factor of the SVD.
nicolasf170e5f2010-10-27 13:21:30 +0000792 *
793 * DETAILS: Find the singular values and (unit) left singular
794 * vectors of Jinv, clampling up the singular values to 1, and
nicolas932ef842010-10-27 16:05:12 +0000795 * multiply the unit left singular vectors by the new singular
nicolasf170e5f2010-10-27 13:21:30 +0000796 * values in order to get the minor and major ellipse axis vectors.
anthonyc7b82f22010-09-27 10:42:29 +0000797 *
nicolas40ae4632010-10-28 13:03:53 +0000798 * Image resampling context:
anthonyc7b82f22010-09-27 10:42:29 +0000799 *
800 * The Jacobian matrix of the transformation at the output point
801 * under consideration is defined as follows:
802 *
803 * Consider the transformation (x,y) -> (X,Y) from input locations
nicolas8b1d9812010-09-29 18:41:55 +0000804 * to output locations. (Anthony Thyssen, elsewhere in resample.c,
nicolas40ae4632010-10-28 13:03:53 +0000805 * uses the notation (u,v) -> (x,y).)
anthonyc7b82f22010-09-27 10:42:29 +0000806 *
nicolas40ae4632010-10-28 13:03:53 +0000807 * The Jacobian matrix of the transformation at (x,y) is equal to
anthonyc7b82f22010-09-27 10:42:29 +0000808 *
nicolas40ae4632010-10-28 13:03:53 +0000809 * J = [ A, B ] = [ dX/dx, dX/dy ]
810 * [ C, D ] [ dY/dx, dY/dy ]
anthonyc7b82f22010-09-27 10:42:29 +0000811 *
nicolas40ae4632010-10-28 13:03:53 +0000812 * that is, the vector [A,C] is the tangent vector corresponding to
813 * input changes in the horizontal direction, and the vector [B,D]
814 * is the tangent vector corresponding to input changes in the
815 * vertical direction.
anthonyc7b82f22010-09-27 10:42:29 +0000816 *
nicolas40ae4632010-10-28 13:03:53 +0000817 * In the context of resampling, it is natural to use the inverse
818 * Jacobian matrix Jinv because resampling is generally performed by
819 * pulling pixel locations in the output image back to locations in
820 * the input image. Jinv is
anthonyc7b82f22010-09-27 10:42:29 +0000821 *
nicolasd0026352011-05-09 15:46:42 +0000822 * Jinv = [ a, b ] = [ dx/dX, dx/dY ]
nicolas40ae4632010-10-28 13:03:53 +0000823 * [ c, d ] [ dy/dX, dy/dY ]
anthonyc7b82f22010-09-27 10:42:29 +0000824 *
825 * Note: Jinv can be computed from J with the following matrix
826 * formula:
827 *
nicolasc90935c2010-09-27 16:47:39 +0000828 * Jinv = 1/(A*D-B*C) [ D, -B ]
829 * [ -C, A ]
830 *
nicolas40ae4632010-10-28 13:03:53 +0000831 * What we do is modify Jinv so that it generates an ellipse which
832 * is as close as possible to the original but which contains the
833 * unit disk. This can be accomplished as follows:
nicolasc90935c2010-09-27 16:47:39 +0000834 *
835 * Let
836 *
837 * Jinv = U Sigma V^T
838 *
nicolas932ef842010-10-27 16:05:12 +0000839 * be an SVD decomposition of Jinv. (The SVD is not unique, but the
nicolas40ae4632010-10-28 13:03:53 +0000840 * final ellipse does not depend on the particular SVD.)
cristycb180922011-03-11 14:41:24 +0000841 *
nicolas40ae4632010-10-28 13:03:53 +0000842 * We could clamp up the entries of the diagonal matrix Sigma so
843 * that they are at least 1, and then set
nicolasc90935c2010-09-27 16:47:39 +0000844 *
845 * Jinv = U newSigma V^T.
846 *
nicolas40ae4632010-10-28 13:03:53 +0000847 * However, we do not need to compute V for the following reason:
848 * V^T is an orthogonal matrix (that is, it represents a combination
849 * of rotations and reflexions) so that it maps the unit circle to
850 * itself. For this reason, the exact value of V does not affect the
851 * final ellipse, and we can choose V to be the identity
852 * matrix. This gives
nicolasc90935c2010-09-27 16:47:39 +0000853 *
nicolas40ae4632010-10-28 13:03:53 +0000854 * Jinv = U newSigma.
nicolasc90935c2010-09-27 16:47:39 +0000855 *
nicolas40ae4632010-10-28 13:03:53 +0000856 * In the end, we return the two diagonal entries of newSigma
857 * together with the two columns of U.
anthonyc7b82f22010-09-27 10:42:29 +0000858 */
859 /*
860 * ClampUpAxes was written by Nicolas Robidoux and Chantal Racette
nicolas47b95652010-11-09 20:51:33 +0000861 * of Laurentian University with insightful suggestions from Anthony
862 * Thyssen and funding from the National Science and Engineering
863 * Research Council of Canada. It is distinguished from its
864 * predecessors by its efficient handling of degenerate cases.
nicolas703291a2010-09-27 18:21:32 +0000865 *
nicolas8c741cc2010-11-09 20:37:24 +0000866 * The idea of clamping up the EWA ellipse's major and minor axes so
867 * that the result contains the reconstruction kernel filter support
nicolas47b95652010-11-09 20:51:33 +0000868 * is taken from Andreas Gustaffson's Masters thesis "Interactive
869 * Image Warping", Helsinki University of Technology, Faculty of
870 * Information Technology, 59 pages, 1993 (see Section 3.6).
nicolas8c741cc2010-11-09 20:37:24 +0000871 *
nicolas47b95652010-11-09 20:51:33 +0000872 * The use of the SVD to clamp up the singular values of the
873 * Jacobian matrix of the pullback transformation for EWA resampling
874 * is taken from the astrophysicist Craig DeForest. It is
875 * implemented in his PDL::Transform code (PDL = Perl Data
876 * Language).
anthonyc7b82f22010-09-27 10:42:29 +0000877 */
878 const double a = dux;
879 const double b = duy;
880 const double c = dvx;
881 const double d = dvy;
882 /*
883 * n is the matrix Jinv * transpose(Jinv). Eigenvalues of n are the
884 * squares of the singular values of Jinv.
885 */
886 const double aa = a*a;
887 const double bb = b*b;
888 const double cc = c*c;
889 const double dd = d*d;
890 /*
891 * Eigenvectors of n are left singular vectors of Jinv.
892 */
893 const double n11 = aa+bb;
894 const double n12 = a*c+b*d;
895 const double n21 = n12;
896 const double n22 = cc+dd;
897 const double det = a*d-b*c;
898 const double twice_det = det+det;
899 const double frobenius_squared = n11+n22;
900 const double discriminant =
901 (frobenius_squared+twice_det)*(frobenius_squared-twice_det);
902 const double sqrt_discriminant = sqrt(discriminant);
903 /*
904 * s1 is the largest singular value of the inverse Jacobian
905 * matrix. In other words, its reciprocal is the smallest singular
906 * value of the Jacobian matrix itself.
907 * If s1 = 0, both singular values are 0, and any orthogonal pair of
908 * left and right factors produces a singular decomposition of Jinv.
nicolasc90935c2010-09-27 16:47:39 +0000909 */
910 /*
nicolas8b1d9812010-09-29 18:41:55 +0000911 * Initially, we only compute the squares of the singular values.
anthonyc7b82f22010-09-27 10:42:29 +0000912 */
913 const double s1s1 = 0.5*(frobenius_squared+sqrt_discriminant);
914 /*
915 * s2 the smallest singular value of the inverse Jacobian
916 * matrix. Its reciprocal is the largest singular value of the
917 * Jacobian matrix itself.
918 */
919 const double s2s2 = 0.5*(frobenius_squared-sqrt_discriminant);
920 const double s1s1minusn11 = s1s1-n11;
921 const double s1s1minusn22 = s1s1-n22;
922 /*
923 * u1, the first column of the U factor of a singular decomposition
924 * of Jinv, is a (non-normalized) left singular vector corresponding
nicolasc90935c2010-09-27 16:47:39 +0000925 * to s1. It has entries u11 and u21. We compute u1 from the fact
926 * that it is an eigenvector of n corresponding to the eigenvalue
927 * s1^2.
anthonyc7b82f22010-09-27 10:42:29 +0000928 */
929 const double s1s1minusn11_squared = s1s1minusn11*s1s1minusn11;
930 const double s1s1minusn22_squared = s1s1minusn22*s1s1minusn22;
931 /*
932 * The following selects the largest row of n-s1^2 I as the one
933 * which is used to find the eigenvector. If both s1^2-n11 and
934 * s1^2-n22 are zero, n-s1^2 I is the zero matrix. In that case,
935 * any vector is an eigenvector; in addition, norm below is equal to
936 * zero, and, in exact arithmetic, this is the only case in which
937 * norm = 0. So, setting u1 to the simple but arbitrary vector [1,0]
938 * if norm = 0 safely takes care of all cases.
939 */
940 const double temp_u11 =
941 ( (s1s1minusn11_squared>=s1s1minusn22_squared) ? n12 : s1s1minusn22 );
942 const double temp_u21 =
943 ( (s1s1minusn11_squared>=s1s1minusn22_squared) ? s1s1minusn11 : n21 );
944 const double norm = sqrt(temp_u11*temp_u11+temp_u21*temp_u21);
945 /*
946 * Finalize the entries of first left singular vector (associated
947 * with the largest singular value).
948 */
949 const double u11 = ( (norm>0.0) ? temp_u11/norm : 1.0 );
950 const double u21 = ( (norm>0.0) ? temp_u21/norm : 0.0 );
951 /*
952 * Clamp the singular values up to 1.
953 */
nicolased227212010-09-27 17:24:57 +0000954 *major_mag = ( (s1s1<=1.0) ? 1.0 : sqrt(s1s1) );
955 *minor_mag = ( (s2s2<=1.0) ? 1.0 : sqrt(s2s2) );
nicolasc90935c2010-09-27 16:47:39 +0000956 /*
957 * Return the unit major and minor axis direction vectors.
958 */
anthonyc7b82f22010-09-27 10:42:29 +0000959 *major_unit_x = u11;
960 *major_unit_y = u21;
nicolasc90935c2010-09-27 16:47:39 +0000961 *minor_unit_x = -u21;
962 *minor_unit_y = u11;
anthonyc7b82f22010-09-27 10:42:29 +0000963}
964
965#endif
cristy3ed852e2009-09-05 21:47:34 +0000966/*
967%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968% %
969% %
970% %
971% S c a l e R e s a m p l e F i l t e r %
972% %
973% %
974% %
975%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976%
977% ScaleResampleFilter() does all the calculations needed to resample an image
978% at a specific scale, defined by two scaling vectors. This not using
979% a orthogonal scaling, but two distorted scaling vectors, to allow the
980% generation of a angled ellipse.
981%
982% As only two deritive scaling vectors are used the center of the ellipse
983% must be the center of the lookup. That is any curvature that the
984% distortion may produce is discounted.
985%
986% The input vectors are produced by either finding the derivitives of the
987% distortion function, or the partial derivitives from a distortion mapping.
988% They do not need to be the orthogonal dx,dy scaling vectors, but can be
989% calculated from other derivatives. For example you could use dr,da/r
990% polar coordinate vector scaling vectors
991%
anthonyc7b82f22010-09-27 10:42:29 +0000992% If u,v = DistortEquation(x,y) OR u = Fu(x,y); v = Fv(x,y)
993% Then the scaling vectors are determined from the deritives...
cristy3ed852e2009-09-05 21:47:34 +0000994% du/dx, dv/dx and du/dy, dv/dy
anthonyc7b82f22010-09-27 10:42:29 +0000995% If the resulting scaling vectors is othogonally aligned then...
cristy3ed852e2009-09-05 21:47:34 +0000996% dv/dx = 0 and du/dy = 0
anthonyc7b82f22010-09-27 10:42:29 +0000997% Producing an othogonally alligned ellipse in source space for the area to
998% be resampled.
cristy3ed852e2009-09-05 21:47:34 +0000999%
1000% Note that scaling vectors are different to argument order. Argument order
1001% is the general order the deritives are extracted from the distortion
anthonyc7b82f22010-09-27 10:42:29 +00001002% equations, and not the scaling vectors. As such the middle two vaules
1003% may be swapped from what you expect. Caution is advised.
cristy3ed852e2009-09-05 21:47:34 +00001004%
anthony3ebea1e2010-09-27 13:29:00 +00001005% WARNING: It is assumed that any SetResampleFilter() method call will
1006% always be performed before the ScaleResampleFilter() method, so that the
1007% size of the ellipse will match the support for the resampling filter being
1008% used.
anthony490ab032010-09-20 00:02:08 +00001009%
cristy3ed852e2009-09-05 21:47:34 +00001010% The format of the ScaleResampleFilter method is:
1011%
1012% void ScaleResampleFilter(const ResampleFilter *resample_filter,
1013% const double dux,const double duy,const double dvx,const double dvy)
1014%
1015% A description of each parameter follows:
1016%
1017% o resample_filter: the resampling resample_filterrmation defining the
1018% image being resampled
1019%
1020% o dux,duy,dvx,dvy:
anthonyc7b82f22010-09-27 10:42:29 +00001021% The deritives or scaling vectors defining the EWA ellipse.
1022% NOTE: watch the order, which is based on the order deritives
1023% are usally determined from distortion equations (see above).
1024% The middle two values may need to be swapped if you are thinking
1025% in terms of scaling vectors.
cristy3ed852e2009-09-05 21:47:34 +00001026%
1027*/
1028MagickExport void ScaleResampleFilter(ResampleFilter *resample_filter,
1029 const double dux,const double duy,const double dvx,const double dvy)
1030{
anthonyd638d312010-09-15 13:13:01 +00001031 double A,B,C,F;
cristy3ed852e2009-09-05 21:47:34 +00001032
1033 assert(resample_filter != (ResampleFilter *) NULL);
1034 assert(resample_filter->signature == MagickSignature);
1035
1036 resample_filter->limit_reached = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001037
anthonyb821aaf2010-09-27 13:21:08 +00001038 /* A 'point' filter forces use of interpolation instead of area sampling */
1039 if ( resample_filter->filter == PointFilter )
1040 return; /* EWA turned off - nothing to do */
1041
anthonyc7b82f22010-09-27 10:42:29 +00001042#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +00001043 (void) FormatLocaleFile(stderr, "# -----\n" );
1044 (void) FormatLocaleFile(stderr, "dux=%lf; dvx=%lf; duy=%lf; dvy=%lf;\n",
anthonyc7b82f22010-09-27 10:42:29 +00001045 dux, dvx, duy, dvy);
1046#endif
cristy3ed852e2009-09-05 21:47:34 +00001047
1048 /* Find Ellipse Coefficents such that
1049 A*u^2 + B*u*v + C*v^2 = F
1050 With u,v relative to point around which we are resampling.
1051 And the given scaling dx,dy vectors in u,v space
1052 du/dx,dv/dx and du/dy,dv/dy
1053 */
anthonyc7b82f22010-09-27 10:42:29 +00001054#if EWA
anthonyd638d312010-09-15 13:13:01 +00001055 /* Direct conversion of derivatives into elliptical coefficients
anthonyb821aaf2010-09-27 13:21:08 +00001056 However when magnifying images, the scaling vectors will be small
1057 resulting in a ellipse that is too small to sample properly.
1058 As such we need to clamp the major/minor axis to a minumum of 1.0
1059 to prevent it getting too small.
cristy3ed852e2009-09-05 21:47:34 +00001060 */
anthonyc7b82f22010-09-27 10:42:29 +00001061#if EWA_CLAMP
1062 { double major_mag,
1063 minor_mag,
1064 major_x,
1065 major_y,
1066 minor_x,
1067 minor_y;
1068
1069 ClampUpAxes(dux,dvx,duy,dvy, &major_mag, &minor_mag,
1070 &major_x, &major_y, &minor_x, &minor_y);
anthonybdfddb02010-10-05 00:06:45 +00001071 major_x *= major_mag; major_y *= major_mag;
1072 minor_x *= minor_mag; minor_y *= minor_mag;
anthonyc7b82f22010-09-27 10:42:29 +00001073#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +00001074 (void) FormatLocaleFile(stderr, "major_x=%lf; major_y=%lf; minor_x=%lf; minor_y=%lf;\n",
anthonyc7b82f22010-09-27 10:42:29 +00001075 major_x, major_y, minor_x, minor_y);
1076#endif
1077 A = major_y*major_y+minor_y*minor_y;
1078 B = -2.0*(major_x*major_y+minor_x*minor_y);
1079 C = major_x*major_x+minor_x*minor_x;
nicolaseaa08622010-09-27 17:06:09 +00001080 F = major_mag*minor_mag;
anthonyc7b82f22010-09-27 10:42:29 +00001081 F *= F; /* square it */
1082 }
anthony5b697cd2010-10-10 03:48:57 +00001083#else /* raw unclamped EWA */
cristy3ed852e2009-09-05 21:47:34 +00001084 A = dvx*dvx+dvy*dvy;
anthonyd638d312010-09-15 13:13:01 +00001085 B = -2.0*(dux*dvx+duy*dvy);
cristy3ed852e2009-09-05 21:47:34 +00001086 C = dux*dux+duy*duy;
anthonyc7b82f22010-09-27 10:42:29 +00001087 F = dux*dvy-duy*dvx;
anthony5708fc62010-09-14 13:52:50 +00001088 F *= F; /* square it */
anthony5b697cd2010-10-10 03:48:57 +00001089#endif /* EWA_CLAMP */
anthonyd638d312010-09-15 13:13:01 +00001090
anthony490ab032010-09-20 00:02:08 +00001091#else /* HQ_EWA */
anthonyd638d312010-09-15 13:13:01 +00001092 /*
anthonyc7b82f22010-09-27 10:42:29 +00001093 This Paul Heckbert's "Higher Quality EWA" formula, from page 60 in his
1094 thesis, which adds a unit circle to the elliptical area so as to do both
1095 Reconstruction and Prefiltering of the pixels in the resampling. It also
1096 means it is always likely to have at least 4 pixels within the area of the
1097 ellipse, for weighted averaging. No scaling will result with F == 4.0 and
1098 a circle of radius 2.0, and F smaller than this means magnification is
1099 being used.
anthony490ab032010-09-20 00:02:08 +00001100
anthonyc7b82f22010-09-27 10:42:29 +00001101 NOTE: This method produces a very blury result at near unity scale while
anthonybdfddb02010-10-05 00:06:45 +00001102 producing perfect results for strong minitification and magnifications.
anthony490ab032010-09-20 00:02:08 +00001103
anthonyc7b82f22010-09-27 10:42:29 +00001104 However filter support is fixed to 2.0 (no good for Windowed Sinc filters)
cristy3ed852e2009-09-05 21:47:34 +00001105 */
1106 A = dvx*dvx+dvy*dvy+1;
anthonyd638d312010-09-15 13:13:01 +00001107 B = -2.0*(dux*dvx+duy*dvy);
cristy3ed852e2009-09-05 21:47:34 +00001108 C = dux*dux+duy*duy+1;
1109 F = A*C - B*B/4;
cristy3ed852e2009-09-05 21:47:34 +00001110#endif
1111
anthony490ab032010-09-20 00:02:08 +00001112#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +00001113 (void) FormatLocaleFile(stderr, "A=%lf; B=%lf; C=%lf; F=%lf\n", A,B,C,F);
cristy3ed852e2009-09-05 21:47:34 +00001114
anthonyc7b82f22010-09-27 10:42:29 +00001115 /* Figure out the various information directly about the ellipse.
cristy3ed852e2009-09-05 21:47:34 +00001116 This information currently not needed at this time, but may be
1117 needed later for better limit determination.
anthonyd638d312010-09-15 13:13:01 +00001118
1119 It is also good to have as a record for future debugging
cristy3ed852e2009-09-05 21:47:34 +00001120 */
1121 { double alpha, beta, gamma, Major, Minor;
anthony490ab032010-09-20 00:02:08 +00001122 double Eccentricity, Ellipse_Area, Ellipse_Angle;
anthonyd638d312010-09-15 13:13:01 +00001123
cristy3ed852e2009-09-05 21:47:34 +00001124 alpha = A+C;
1125 beta = A-C;
1126 gamma = sqrt(beta*beta + B*B );
1127
1128 if ( alpha - gamma <= MagickEpsilon )
1129 Major = MagickHuge;
1130 else
1131 Major = sqrt(2*F/(alpha - gamma));
1132 Minor = sqrt(2*F/(alpha + gamma));
1133
cristy5acdd942011-05-27 19:45:39 +00001134 (void) FormatLocaleFile(stderr, "# Major=%lf; Minor=%lf\n", Major, Minor );
cristy3ed852e2009-09-05 21:47:34 +00001135
1136 /* other information about ellipse include... */
1137 Eccentricity = Major/Minor;
1138 Ellipse_Area = MagickPI*Major*Minor;
nicolase2ecb242010-09-29 20:02:24 +00001139 Ellipse_Angle = atan2(B, A-C);
cristy3ed852e2009-09-05 21:47:34 +00001140
cristy5acdd942011-05-27 19:45:39 +00001141 (void) FormatLocaleFile(stderr, "# Angle=%lf Area=%lf\n",
nicolase2ecb242010-09-29 20:02:24 +00001142 RadiansToDegrees(Ellipse_Angle), Ellipse_Area);
cristy3ed852e2009-09-05 21:47:34 +00001143 }
1144#endif
1145
nicolas15c331b2010-09-29 19:05:00 +00001146 /* If one or both of the scaling vectors is impossibly large
1147 (producing a very large raw F value), we may as well not bother
1148 doing any form of resampling since resampled area is very large.
1149 In this case some alternative means of pixel sampling, such as
1150 the average of the whole image is needed to get a reasonable
1151 result. Calculate only as needed.
cristy3ed852e2009-09-05 21:47:34 +00001152 */
anthony490ab032010-09-20 00:02:08 +00001153 if ( (4*A*C - B*B) > MagickHuge ) {
cristy3ed852e2009-09-05 21:47:34 +00001154 resample_filter->limit_reached = MagickTrue;
1155 return;
1156 }
1157
anthony582b6d72010-10-10 06:45:41 +00001158 /* Scale ellipse to match the filters support
anthony9cb63cc2012-04-25 06:10:49 +00001159 (that is, multiply F by the square of the support)
1160 Simplier to just multiply it by the support twice!
nicolase2ecb242010-09-29 20:02:24 +00001161 */
anthony490ab032010-09-20 00:02:08 +00001162 F *= resample_filter->support;
1163 F *= resample_filter->support;
cristy3ed852e2009-09-05 21:47:34 +00001164
nicolase2ecb242010-09-29 20:02:24 +00001165 /* Orthogonal bounds of the ellipse */
cristyc120ce32011-05-10 21:38:57 +00001166 resample_filter->Ulimit = sqrt(C*F/(A*C-0.25*B*B));
1167 resample_filter->Vlimit = sqrt(A*F/(A*C-0.25*B*B));
anthony490ab032010-09-20 00:02:08 +00001168
nicolase2ecb242010-09-29 20:02:24 +00001169 /* Horizontally aligned parallelogram fitted to Ellipse */
1170 resample_filter->Uwidth = sqrt(F/A); /* Half of the parallelogram width */
cristyc120ce32011-05-10 21:38:57 +00001171 resample_filter->slope = -B/(2.0*A); /* Reciprocal slope of the parallelogram */
anthony490ab032010-09-20 00:02:08 +00001172
1173#if DEBUG_ELLIPSE
cristy5acdd942011-05-27 19:45:39 +00001174 (void) FormatLocaleFile(stderr, "Ulimit=%lf; Vlimit=%lf; UWidth=%lf; Slope=%lf;\n",
anthony490ab032010-09-20 00:02:08 +00001175 resample_filter->Ulimit, resample_filter->Vlimit,
1176 resample_filter->Uwidth, resample_filter->slope );
1177#endif
cristy3ed852e2009-09-05 21:47:34 +00001178
nicolase2ecb242010-09-29 20:02:24 +00001179 /* Check the absolute area of the parallelogram involved.
1180 * This limit needs more work, as it is too slow for larger images
1181 * with tiled views of the horizon.
1182 */
cristy39f347a2010-09-20 00:29:31 +00001183 if ( (resample_filter->Uwidth * resample_filter->Vlimit)
1184 > (4.0*resample_filter->image_area)) {
cristy3ed852e2009-09-05 21:47:34 +00001185 resample_filter->limit_reached = MagickTrue;
1186 return;
1187 }
1188
anthony5708fc62010-09-14 13:52:50 +00001189 /* Scale ellipse formula to directly index the Filter Lookup Table */
cristy3ed852e2009-09-05 21:47:34 +00001190 { register double scale;
anthony5b697cd2010-10-10 03:48:57 +00001191#if FILTER_LUT
anthony582b6d72010-10-10 06:45:41 +00001192 /* scale so that F = WLUT_WIDTH; -- hardcoded */
anthony490ab032010-09-20 00:02:08 +00001193 scale = (double)WLUT_WIDTH/F;
anthony5b697cd2010-10-10 03:48:57 +00001194#else
anthony582b6d72010-10-10 06:45:41 +00001195 /* scale so that F = resample_filter->F (support^2) */
1196 scale = resample_filter->F/F;
anthony5b697cd2010-10-10 03:48:57 +00001197#endif
cristy3ed852e2009-09-05 21:47:34 +00001198 resample_filter->A = A*scale;
1199 resample_filter->B = B*scale;
1200 resample_filter->C = C*scale;
cristy3ed852e2009-09-05 21:47:34 +00001201 }
1202}
1203
1204/*
1205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1206% %
1207% %
1208% %
1209% S e t R e s a m p l e F i l t e r %
1210% %
1211% %
1212% %
1213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214%
1215% SetResampleFilter() set the resampling filter lookup table based on a
1216% specific filter. Note that the filter is used as a radial filter not as a
1217% two pass othogonally aligned resampling filter.
1218%
cristy3ed852e2009-09-05 21:47:34 +00001219% The format of the SetResampleFilter method is:
1220%
1221% void SetResampleFilter(ResampleFilter *resample_filter,
cristyaa2c16c2012-03-25 22:21:35 +00001222% const FilterTypes filter)
cristy3ed852e2009-09-05 21:47:34 +00001223%
1224% A description of each parameter follows:
1225%
1226% o resample_filter: resampling resample_filterrmation structure
1227%
1228% o filter: the resize filter for elliptical weighting LUT
1229%
cristy3ed852e2009-09-05 21:47:34 +00001230*/
1231MagickExport void SetResampleFilter(ResampleFilter *resample_filter,
cristyaa2c16c2012-03-25 22:21:35 +00001232 const FilterTypes filter)
cristy3ed852e2009-09-05 21:47:34 +00001233{
cristy3ed852e2009-09-05 21:47:34 +00001234 ResizeFilter
1235 *resize_filter;
1236
1237 assert(resample_filter != (ResampleFilter *) NULL);
1238 assert(resample_filter->signature == MagickSignature);
1239
anthony2e6ab682010-09-28 12:02:25 +00001240 resample_filter->do_interpolate = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001241 resample_filter->filter = filter;
1242
anthony9cb63cc2012-04-25 06:10:49 +00001243 /* Default cylindrical filter is a Cubic Keys filter */
anthony490ab032010-09-20 00:02:08 +00001244 if ( filter == UndefinedFilter )
anthony853d6972010-10-08 06:01:31 +00001245 resample_filter->filter = RobidouxFilter;
anthony490ab032010-09-20 00:02:08 +00001246
anthony9cb63cc2012-04-25 06:10:49 +00001247 if ( resample_filter->filter == PointFilter ) {
1248 resample_filter->do_interpolate = MagickTrue;
1249 return; /* EWA turned off - nothing more to do */
1250 }
1251
anthony490ab032010-09-20 00:02:08 +00001252 resize_filter = AcquireResizeFilter(resample_filter->image,
cristyaa2c16c2012-03-25 22:21:35 +00001253 resample_filter->filter,MagickTrue,resample_filter->exception);
anthony9cb63cc2012-04-25 06:10:49 +00001254 if (resize_filter == (ResizeFilter *) NULL) {
1255 (void) ThrowMagickException(resample_filter->exception,GetMagickModule(),
1256 ModuleError, "UnableToSetFilteringValue",
1257 "Fall back to Interpolated 'Point' filter");
1258 resample_filter->filter = PointFilter;
1259 resample_filter->do_interpolate = MagickTrue;
1260 return; /* EWA turned off - nothing more to do */
1261 }
anthony490ab032010-09-20 00:02:08 +00001262
anthony10b8bc82010-10-02 12:48:46 +00001263 /* Get the practical working support for the filter,
1264 * after any API call blur factors have been accoded for.
1265 */
anthonyc7b82f22010-09-27 10:42:29 +00001266#if EWA
anthony490ab032010-09-20 00:02:08 +00001267 resample_filter->support = GetResizeFilterSupport(resize_filter);
anthonyc7b82f22010-09-27 10:42:29 +00001268#else
1269 resample_filter->support = 2.0; /* fixed support size for HQ-EWA */
anthony490ab032010-09-20 00:02:08 +00001270#endif
1271
anthony5b697cd2010-10-10 03:48:57 +00001272#if FILTER_LUT
1273 /* Fill the LUT with the weights from the selected filter function */
1274 { register int
1275 Q;
1276 double
1277 r_scale;
anthony9cb63cc2012-04-25 06:10:49 +00001278
anthony5b697cd2010-10-10 03:48:57 +00001279 /* Scale radius so the filter LUT covers the full support range */
1280 r_scale = resample_filter->support*sqrt(1.0/(double)WLUT_WIDTH);
1281 for(Q=0; Q<WLUT_WIDTH; Q++)
1282 resample_filter->filter_lut[Q] = (double)
1283 GetResizeFilterWeight(resize_filter,sqrt((double)Q)*r_scale);
anthony490ab032010-09-20 00:02:08 +00001284
anthony5b697cd2010-10-10 03:48:57 +00001285 /* finished with the resize filter */
1286 resize_filter = DestroyResizeFilter(resize_filter);
1287 }
1288#else
anthony582b6d72010-10-10 06:45:41 +00001289 /* save the filter and the scaled ellipse bounds needed for filter */
anthony5b697cd2010-10-10 03:48:57 +00001290 resample_filter->filter_def = resize_filter;
anthony582b6d72010-10-10 06:45:41 +00001291 resample_filter->F = resample_filter->support*resample_filter->support;
anthony5b697cd2010-10-10 03:48:57 +00001292#endif
anthony490ab032010-09-20 00:02:08 +00001293
anthony3ebea1e2010-09-27 13:29:00 +00001294 /*
1295 Adjust the scaling of the default unit circle
1296 This assumes that any real scaling changes will always
1297 take place AFTER the filter method has been initialized.
1298 */
anthony3ebea1e2010-09-27 13:29:00 +00001299 ScaleResampleFilter(resample_filter, 1.0, 0.0, 0.0, 1.0);
1300
anthony5708fc62010-09-14 13:52:50 +00001301#if 0
anthonyd638d312010-09-15 13:13:01 +00001302 /*
anthony9cb63cc2012-04-25 06:10:49 +00001303 This is old code kept as a reference only. Basically it generates
1304 a Gaussian bell curve, with sigma = 0.5 if the support is 2.0
1305
anthonyd638d312010-09-15 13:13:01 +00001306 Create Normal Gaussian 2D Filter Weighted Lookup Table.
1307 A normal EWA guassual lookup would use exp(Q*ALPHA)
1308 where Q = distance squared from 0.0 (center) to 1.0 (edge)
1309 and ALPHA = -4.0*ln(2.0) ==> -2.77258872223978123767
anthony5b697cd2010-10-10 03:48:57 +00001310 The table is of length 1024, and equates to support radius of 2.0
anthonyd638d312010-09-15 13:13:01 +00001311 thus needs to be scaled by ALPHA*4/1024 and any blur factor squared
anthony5708fc62010-09-14 13:52:50 +00001312
anthony9cb63cc2012-04-25 06:10:49 +00001313 The it comes from reference code provided by Fred Weinhaus.
anthonyd638d312010-09-15 13:13:01 +00001314 */
anthonyd638d312010-09-15 13:13:01 +00001315 r_scale = -2.77258872223978123767/(WLUT_WIDTH*blur*blur);
1316 for(Q=0; Q<WLUT_WIDTH; Q++)
1317 resample_filter->filter_lut[Q] = exp((double)Q*r_scale);
1318 resample_filter->support = WLUT_WIDTH;
anthony5708fc62010-09-14 13:52:50 +00001319#endif
anthony490ab032010-09-20 00:02:08 +00001320
anthony5b697cd2010-10-10 03:48:57 +00001321#if FILTER_LUT
anthonye06e4c12010-09-15 04:03:52 +00001322#if defined(MAGICKCORE_OPENMP_SUPPORT)
anthony72949792010-10-08 04:44:56 +00001323 #pragma omp single
anthonye06e4c12010-09-15 04:03:52 +00001324#endif
anthony9cb63cc2012-04-25 06:10:49 +00001325 {
anthony451f9092012-05-11 01:56:24 +00001326 if (IfStringTrue(GetImageArtifact(resample_filter->image,
anthony9cb63cc2012-04-25 06:10:49 +00001327 "resample:verbose")) )
anthonye06e4c12010-09-15 04:03:52 +00001328 {
anthony9cb63cc2012-04-25 06:10:49 +00001329 register int
1330 Q;
1331 double
1332 r_scale;
1333
anthonye06e4c12010-09-15 04:03:52 +00001334 /* Debug output of the filter weighting LUT
anthony9cb63cc2012-04-25 06:10:49 +00001335 Gnuplot the LUT data, the x scale index has been adjusted
1336 plot [0:2][-.2:1] "lut.dat" with lines
1337 The filter values should be normalized for comparision
anthonye06e4c12010-09-15 04:03:52 +00001338 */
anthonyd638d312010-09-15 13:13:01 +00001339 printf("#\n");
anthony9cb63cc2012-04-25 06:10:49 +00001340 printf("# Resampling Filter LUT (%d values) for '%s' filter\n",
1341 WLUT_WIDTH, CommandOptionToMnemonic(MagickFilterOptions,
1342 resample_filter->filter) );
anthonye06e4c12010-09-15 04:03:52 +00001343 printf("#\n");
anthonyd638d312010-09-15 13:13:01 +00001344 printf("# Note: values in table are using a squared radius lookup.\n");
anthony9cb63cc2012-04-25 06:10:49 +00001345 printf("# As such its distribution is not uniform.\n");
1346 printf("#\n");
1347 printf("# The X value is the support distance for the Y weight\n");
1348 printf("# so you can use gnuplot to plot this cylindrical filter\n");
1349 printf("# plot [0:2][-.2:1] \"lut.dat\" with lines\n");
1350 printf("#\n");
1351
1352 /* Scale radius so the filter LUT covers the full support range */
1353 r_scale = resample_filter->support*sqrt(1.0/(double)WLUT_WIDTH);
anthonye06e4c12010-09-15 04:03:52 +00001354 for(Q=0; Q<WLUT_WIDTH; Q++)
anthonyd638d312010-09-15 13:13:01 +00001355 printf("%8.*g %.*g\n",
anthony9cb63cc2012-04-25 06:10:49 +00001356 GetMagickPrecision(),sqrt((double)Q)*r_scale,
1357 GetMagickPrecision(),resample_filter->filter_lut[Q] );
1358 printf("\n\n"); /* generate a 'break' in gnuplot if multiple outputs */
anthonye06e4c12010-09-15 04:03:52 +00001359 }
anthony9cb63cc2012-04-25 06:10:49 +00001360 /* Output the above once only for each image, and each setting
anthony72949792010-10-08 04:44:56 +00001361 (void) DeleteImageArtifact(resample_filter->image,"resample:verbose");
anthony9cb63cc2012-04-25 06:10:49 +00001362 */
anthony72949792010-10-08 04:44:56 +00001363 }
anthony5b697cd2010-10-10 03:48:57 +00001364#endif /* FILTER_LUT */
cristy3ed852e2009-09-05 21:47:34 +00001365 return;
1366}
1367
1368/*
1369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1370% %
1371% %
1372% %
1373% S e t R e s a m p l e F i l t e r I n t e r p o l a t e M e t h o d %
1374% %
1375% %
1376% %
1377%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378%
cristy2ab242e2011-03-11 02:45:46 +00001379% SetResampleFilterInterpolateMethod() sets the resample filter interpolation
1380% method.
cristy3ed852e2009-09-05 21:47:34 +00001381%
1382% The format of the SetResampleFilterInterpolateMethod method is:
1383%
1384% MagickBooleanType SetResampleFilterInterpolateMethod(
1385% ResampleFilter *resample_filter,const InterpolateMethod method)
1386%
1387% A description of each parameter follows:
1388%
1389% o resample_filter: the resample filter.
1390%
1391% o method: the interpolation method.
1392%
1393*/
1394MagickExport MagickBooleanType SetResampleFilterInterpolateMethod(
cristy5c4e2582011-09-11 19:21:03 +00001395 ResampleFilter *resample_filter,const PixelInterpolateMethod method)
cristy3ed852e2009-09-05 21:47:34 +00001396{
1397 assert(resample_filter != (ResampleFilter *) NULL);
1398 assert(resample_filter->signature == MagickSignature);
1399 assert(resample_filter->image != (Image *) NULL);
1400 if (resample_filter->debug != MagickFalse)
1401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1402 resample_filter->image->filename);
1403 resample_filter->interpolate=method;
cristy2ab242e2011-03-11 02:45:46 +00001404 return(MagickTrue);
1405}
1406
1407/*
1408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409% %
1410% %
1411% %
cristy3ed852e2009-09-05 21:47:34 +00001412% S e t R e s a m p l e F i l t e r V i r t u a l P i x e l M e t h o d %
1413% %
1414% %
1415% %
1416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417%
1418% SetResampleFilterVirtualPixelMethod() changes the virtual pixel method
1419% associated with the specified resample filter.
1420%
1421% The format of the SetResampleFilterVirtualPixelMethod method is:
1422%
1423% MagickBooleanType SetResampleFilterVirtualPixelMethod(
1424% ResampleFilter *resample_filter,const VirtualPixelMethod method)
1425%
1426% A description of each parameter follows:
1427%
1428% o resample_filter: the resample filter.
1429%
1430% o method: the virtual pixel method.
1431%
1432*/
1433MagickExport MagickBooleanType SetResampleFilterVirtualPixelMethod(
1434 ResampleFilter *resample_filter,const VirtualPixelMethod method)
1435{
1436 assert(resample_filter != (ResampleFilter *) NULL);
1437 assert(resample_filter->signature == MagickSignature);
1438 assert(resample_filter->image != (Image *) NULL);
1439 if (resample_filter->debug != MagickFalse)
1440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1441 resample_filter->image->filename);
1442 resample_filter->virtual_pixel=method;
cristy2d5e44d2010-03-12 01:56:29 +00001443 if (method != UndefinedVirtualPixelMethod)
1444 (void) SetCacheViewVirtualPixelMethod(resample_filter->view,method);
cristy3ed852e2009-09-05 21:47:34 +00001445 return(MagickTrue);
1446}