blob: 572fd559f24f4c4b3ba5e596c61206bc189b98ab [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% %
cristy16af1cb2009-12-11 21:38:29 +000021% Copyright 1999-2010 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*/
43#include "magick/studio.h"
44#include "magick/artifact.h"
45#include "magick/color-private.h"
46#include "magick/cache.h"
47#include "magick/draw.h"
48#include "magick/exception-private.h"
49#include "magick/gem.h"
50#include "magick/image.h"
51#include "magick/image-private.h"
52#include "magick/log.h"
anthonyd638d312010-09-15 13:13:01 +000053#include "magick/magick.h"
cristy3ed852e2009-09-05 21:47:34 +000054#include "magick/memory_.h"
55#include "magick/pixel-private.h"
56#include "magick/quantum.h"
57#include "magick/random_.h"
58#include "magick/resample.h"
59#include "magick/resize.h"
60#include "magick/resize-private.h"
61#include "magick/transform.h"
62#include "magick/signature-private.h"
63/*
anthony490ab032010-09-20 00:02:08 +000064 EWA Resampling Options
65*/
66#define WLUT_WIDTH 1024 /* size of the filter cache */
anthonyc7b82f22010-09-27 10:42:29 +000067
68/* select ONE resampling method */
69#define EWA 1 /* Normal EWA handling - raw or clamped */
70 /* if 0 then use "High Quality EWA" */
71#define EWA_CLAMP 1 /* EWA Clamping from Nicolas Robidoux */
72
73/* output debugging information */
74#define DEBUG_NO_HIT_PIXELS 1 /* Make pixels that fail to 'hit' anything red */
anthony490ab032010-09-20 00:02:08 +000075#define DEBUG_ELLIPSE 0 /* output ellipse info for debug */
76#define DEBUG_HIT_MISS 0 /* output hit/miss pixels with above switch */
77
anthony490ab032010-09-20 00:02:08 +000078/*
cristy3ed852e2009-09-05 21:47:34 +000079 Typedef declarations.
80*/
cristy3ed852e2009-09-05 21:47:34 +000081struct _ResampleFilter
82{
cristy3ed852e2009-09-05 21:47:34 +000083 CacheView
84 *view;
85
cristyc4c8d132010-01-07 01:58:38 +000086 Image
87 *image;
88
cristy3ed852e2009-09-05 21:47:34 +000089 ExceptionInfo
90 *exception;
91
92 MagickBooleanType
93 debug;
94
95 /* Information about image being resampled */
cristybb503372010-05-27 20:51:26 +000096 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000097 image_area;
98
99 InterpolatePixelMethod
100 interpolate;
101
102 VirtualPixelMethod
103 virtual_pixel;
104
105 FilterTypes
106 filter;
107
108 /* processing settings needed */
109 MagickBooleanType
110 limit_reached,
111 do_interpolate,
112 average_defined;
113
114 MagickPixelPacket
115 average_pixel;
116
117 /* current ellipitical area being resampled around center point */
118 double
119 A, B, C,
anthonyd638d312010-09-15 13:13:01 +0000120 Vlimit, Ulimit, Uwidth, slope;
cristy3ed852e2009-09-05 21:47:34 +0000121
122 /* LUT of weights for filtered average in elliptical area */
123 double
124 filter_lut[WLUT_WIDTH],
125 support;
126
cristybb503372010-05-27 20:51:26 +0000127 size_t
cristy3ed852e2009-09-05 21:47:34 +0000128 signature;
129};
130
131/*
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% %
134% %
135% %
136% A c q u i r e R e s a m p l e I n f o %
137% %
138% %
139% %
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141%
142% AcquireResampleFilter() initializes the information resample needs do to a
143% scaled lookup of a color from an image, using area sampling.
144%
145% The algorithm is based on a Elliptical Weighted Average, where the pixels
146% found in a large elliptical area is averaged together according to a
147% weighting (filter) function. For more details see "Fundamentals of Texture
148% Mapping and Image Warping" a master's thesis by Paul.S.Heckbert, June 17,
149% 1989. Available for free from, http://www.cs.cmu.edu/~ph/
150%
151% As EWA resampling (or any sort of resampling) can require a lot of
152% calculations to produce a distorted scaling of the source image for each
153% output pixel, the ResampleFilter structure generated holds that information
154% between individual image resampling.
155%
156% This function will make the appropriate AcquireCacheView() calls
157% to view the image, calling functions do not need to open a cache view.
158%
159% Usage Example...
160% resample_filter=AcquireResampleFilter(image,exception);
anthonyc7b82f22010-09-27 10:42:29 +0000161% SetResampleFilter(resample_filter, GaussianFilter, 1.0);
cristybb503372010-05-27 20:51:26 +0000162% for (y=0; y < (ssize_t) image->rows; y++) {
163% for (x=0; x < (ssize_t) image->columns; x++) {
anthonyc7b82f22010-09-27 10:42:29 +0000164% u= ....; v= ....;
cristy3ed852e2009-09-05 21:47:34 +0000165% ScaleResampleFilter(resample_filter, ... scaling vectors ...);
anthonyc7b82f22010-09-27 10:42:29 +0000166% (void) ResamplePixelColor(resample_filter,u,v,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000167% ... assign resampled pixel value ...
168% }
169% }
170% DestroyResampleFilter(resample_filter);
171%
172% The format of the AcquireResampleFilter method is:
173%
174% ResampleFilter *AcquireResampleFilter(const Image *image,
175% ExceptionInfo *exception)
176%
177% A description of each parameter follows:
178%
179% o image: the image.
180%
181% o exception: return any errors or warnings in this structure.
182%
183*/
184MagickExport ResampleFilter *AcquireResampleFilter(const Image *image,
185 ExceptionInfo *exception)
186{
187 register ResampleFilter
188 *resample_filter;
189
190 assert(image != (Image *) NULL);
191 assert(image->signature == MagickSignature);
192 if (image->debug != MagickFalse)
193 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
194 assert(exception != (ExceptionInfo *) NULL);
195 assert(exception->signature == MagickSignature);
196
197 resample_filter=(ResampleFilter *) AcquireMagickMemory(
198 sizeof(*resample_filter));
199 if (resample_filter == (ResampleFilter *) NULL)
200 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
201 (void) ResetMagickMemory(resample_filter,0,sizeof(*resample_filter));
202
203 resample_filter->image=ReferenceImage((Image *) image);
204 resample_filter->view=AcquireCacheView(resample_filter->image);
205 resample_filter->exception=exception;
206
207 resample_filter->debug=IsEventLogging();
208 resample_filter->signature=MagickSignature;
209
cristyeaedf062010-05-29 22:36:02 +0000210 resample_filter->image_area=(ssize_t) (resample_filter->image->columns*
211 resample_filter->image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000212 resample_filter->average_defined = MagickFalse;
213
214 /* initialise the resampling filter settings */
215 SetResampleFilter(resample_filter, resample_filter->image->filter,
216 resample_filter->image->blur);
217 resample_filter->interpolate = resample_filter->image->interpolate;
218 resample_filter->virtual_pixel=GetImageVirtualPixelMethod(image);
219
cristy3ed852e2009-09-05 21:47:34 +0000220 return(resample_filter);
221}
222
223/*
224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225% %
226% %
227% %
228% D e s t r o y R e s a m p l e I n f o %
229% %
230% %
231% %
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233%
234% DestroyResampleFilter() finalizes and cleans up the resampling
235% resample_filter as returned by AcquireResampleFilter(), freeing any memory
236% or other information as needed.
237%
238% The format of the DestroyResampleFilter method is:
239%
240% ResampleFilter *DestroyResampleFilter(ResampleFilter *resample_filter)
241%
242% A description of each parameter follows:
243%
244% o resample_filter: resampling information structure
245%
246*/
247MagickExport ResampleFilter *DestroyResampleFilter(
248 ResampleFilter *resample_filter)
249{
250 assert(resample_filter != (ResampleFilter *) NULL);
251 assert(resample_filter->signature == MagickSignature);
252 assert(resample_filter->image != (Image *) NULL);
253 if (resample_filter->debug != MagickFalse)
254 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
255 resample_filter->image->filename);
256 resample_filter->view=DestroyCacheView(resample_filter->view);
257 resample_filter->image=DestroyImage(resample_filter->image);
258 resample_filter->signature=(~MagickSignature);
259 resample_filter=(ResampleFilter *) RelinquishMagickMemory(resample_filter);
260 return(resample_filter);
261}
262
263/*
264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265% %
266% %
267% %
268% I n t e r p o l a t e R e s a m p l e F i l t e r %
269% %
270% %
271% %
272%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273%
274% InterpolateResampleFilter() applies bi-linear or tri-linear interpolation
275% between a floating point coordinate and the pixels surrounding that
276% coordinate. No pixel area resampling, or scaling of the result is
277% performed.
278%
279% The format of the InterpolateResampleFilter method is:
280%
281% MagickBooleanType InterpolateResampleFilter(
282% ResampleInfo *resample_filter,const InterpolatePixelMethod method,
283% const double x,const double y,MagickPixelPacket *pixel)
284%
285% A description of each parameter follows:
286%
287% o resample_filter: the resample filter.
288%
289% o method: the pixel clor interpolation method.
290%
291% o x,y: A double representing the current (x,y) position of the pixel.
292%
293% o pixel: return the interpolated pixel here.
294%
295*/
296
297static inline double MagickMax(const double x,const double y)
298{
299 if (x > y)
300 return(x);
301 return(y);
302}
303
304static void BicubicInterpolate(const MagickPixelPacket *pixels,const double dx,
305 MagickPixelPacket *pixel)
306{
307 MagickRealType
308 dx2,
309 p,
310 q,
311 r,
312 s;
313
314 dx2=dx*dx;
315 p=(pixels[3].red-pixels[2].red)-(pixels[0].red-pixels[1].red);
316 q=(pixels[0].red-pixels[1].red)-p;
317 r=pixels[2].red-pixels[0].red;
318 s=pixels[1].red;
319 pixel->red=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
320 p=(pixels[3].green-pixels[2].green)-(pixels[0].green-pixels[1].green);
321 q=(pixels[0].green-pixels[1].green)-p;
322 r=pixels[2].green-pixels[0].green;
323 s=pixels[1].green;
324 pixel->green=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
325 p=(pixels[3].blue-pixels[2].blue)-(pixels[0].blue-pixels[1].blue);
326 q=(pixels[0].blue-pixels[1].blue)-p;
327 r=pixels[2].blue-pixels[0].blue;
328 s=pixels[1].blue;
329 pixel->blue=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
330 p=(pixels[3].opacity-pixels[2].opacity)-(pixels[0].opacity-pixels[1].opacity);
331 q=(pixels[0].opacity-pixels[1].opacity)-p;
332 r=pixels[2].opacity-pixels[0].opacity;
333 s=pixels[1].opacity;
334 pixel->opacity=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
335 if (pixel->colorspace == CMYKColorspace)
336 {
337 p=(pixels[3].index-pixels[2].index)-(pixels[0].index-pixels[1].index);
338 q=(pixels[0].index-pixels[1].index)-p;
339 r=pixels[2].index-pixels[0].index;
340 s=pixels[1].index;
341 pixel->index=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
342 }
343}
344
345static inline MagickRealType CubicWeightingFunction(const MagickRealType x)
346{
347 MagickRealType
348 alpha,
349 gamma;
350
351 alpha=MagickMax(x+2.0,0.0);
352 gamma=1.0*alpha*alpha*alpha;
353 alpha=MagickMax(x+1.0,0.0);
354 gamma-=4.0*alpha*alpha*alpha;
355 alpha=MagickMax(x+0.0,0.0);
356 gamma+=6.0*alpha*alpha*alpha;
357 alpha=MagickMax(x-1.0,0.0);
358 gamma-=4.0*alpha*alpha*alpha;
359 return(gamma/6.0);
360}
361
362static inline double MeshInterpolate(const PointInfo *delta,const double p,
363 const double x,const double y)
364{
365 return(delta->x*x+delta->y*y+(1.0-delta->x-delta->y)*p);
366}
367
cristybb503372010-05-27 20:51:26 +0000368static inline ssize_t NearestNeighbor(MagickRealType x)
cristy3ed852e2009-09-05 21:47:34 +0000369{
370 if (x >= 0.0)
cristybb503372010-05-27 20:51:26 +0000371 return((ssize_t) (x+0.5));
372 return((ssize_t) (x-0.5));
cristy3ed852e2009-09-05 21:47:34 +0000373}
374
375static MagickBooleanType InterpolateResampleFilter(
376 ResampleFilter *resample_filter,const InterpolatePixelMethod method,
377 const double x,const double y,MagickPixelPacket *pixel)
378{
379 MagickBooleanType
380 status;
381
382 register const IndexPacket
383 *indexes;
384
385 register const PixelPacket
386 *p;
387
cristybb503372010-05-27 20:51:26 +0000388 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000389 i;
390
391 assert(resample_filter != (ResampleFilter *) NULL);
392 assert(resample_filter->signature == MagickSignature);
393 status=MagickTrue;
394 switch (method)
395 {
396 case AverageInterpolatePixel:
397 {
398 MagickPixelPacket
399 pixels[16];
400
401 MagickRealType
402 alpha[16],
403 gamma;
404
cristy54ffe7c2010-07-04 23:54:03 +0000405 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x)-1,
406 (ssize_t) floor(y)-1,4,4,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000407 if (p == (const PixelPacket *) NULL)
408 {
409 status=MagickFalse;
410 break;
411 }
412 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
413 for (i=0; i < 16L; i++)
414 {
415 GetMagickPixelPacket(resample_filter->image,pixels+i);
416 SetMagickPixelPacket(resample_filter->image,p,indexes+i,pixels+i);
417 alpha[i]=1.0;
418 if (resample_filter->image->matte != MagickFalse)
419 {
cristy46f08202010-01-10 04:04:21 +0000420 alpha[i]=QuantumScale*((MagickRealType) GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000421 pixels[i].red*=alpha[i];
422 pixels[i].green*=alpha[i];
423 pixels[i].blue*=alpha[i];
424 if (resample_filter->image->colorspace == CMYKColorspace)
425 pixels[i].index*=alpha[i];
426 }
427 gamma=alpha[i];
428 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
429 pixel->red+=gamma*0.0625*pixels[i].red;
430 pixel->green+=gamma*0.0625*pixels[i].green;
431 pixel->blue+=gamma*0.0625*pixels[i].blue;
432 pixel->opacity+=0.0625*pixels[i].opacity;
433 if (resample_filter->image->colorspace == CMYKColorspace)
434 pixel->index+=gamma*0.0625*pixels[i].index;
435 p++;
436 }
437 break;
438 }
439 case BicubicInterpolatePixel:
440 {
441 MagickPixelPacket
442 pixels[16],
443 u[4];
444
445 MagickRealType
446 alpha[16];
447
448 PointInfo
449 delta;
450
cristy54ffe7c2010-07-04 23:54:03 +0000451 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x)-1,
452 (ssize_t) floor(y)-1,4,4,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000453 if (p == (const PixelPacket *) NULL)
454 {
455 status=MagickFalse;
456 break;
457 }
458 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
459 for (i=0; i < 16L; i++)
460 {
461 GetMagickPixelPacket(resample_filter->image,pixels+i);
462 SetMagickPixelPacket(resample_filter->image,p,indexes+i,pixels+i);
463 alpha[i]=1.0;
464 if (resample_filter->image->matte != MagickFalse)
465 {
cristy46f08202010-01-10 04:04:21 +0000466 alpha[i]=QuantumScale*((MagickRealType) GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000467 pixels[i].red*=alpha[i];
468 pixels[i].green*=alpha[i];
469 pixels[i].blue*=alpha[i];
470 if (resample_filter->image->colorspace == CMYKColorspace)
471 pixels[i].index*=alpha[i];
472 }
473 p++;
474 }
475 delta.x=x-floor(x);
476 for (i=0; i < 4L; i++)
477 BicubicInterpolate(pixels+4*i,delta.x,u+i);
478 delta.y=y-floor(y);
479 BicubicInterpolate(u,delta.y,pixel);
480 break;
481 }
482 case BilinearInterpolatePixel:
483 default:
484 {
485 MagickPixelPacket
486 pixels[4];
487
488 MagickRealType
489 alpha[4],
490 gamma;
491
492 PointInfo
493 delta,
494 epsilon;
495
cristy54ffe7c2010-07-04 23:54:03 +0000496 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x),
497 (ssize_t) floor(y),2,2,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000498 if (p == (const PixelPacket *) NULL)
499 {
500 status=MagickFalse;
501 break;
502 }
503 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
504 for (i=0; i < 4L; i++)
505 {
506 pixels[i].red=(MagickRealType) p[i].red;
507 pixels[i].green=(MagickRealType) p[i].green;
508 pixels[i].blue=(MagickRealType) p[i].blue;
509 pixels[i].opacity=(MagickRealType) p[i].opacity;
510 alpha[i]=1.0;
511 }
512 if (resample_filter->image->matte != MagickFalse)
513 for (i=0; i < 4L; i++)
514 {
515 alpha[i]=QuantumScale*((MagickRealType) QuantumRange-p[i].opacity);
516 pixels[i].red*=alpha[i];
517 pixels[i].green*=alpha[i];
518 pixels[i].blue*=alpha[i];
519 }
520 if (indexes != (IndexPacket *) NULL)
521 for (i=0; i < 4L; i++)
522 {
523 pixels[i].index=(MagickRealType) indexes[i];
524 if (resample_filter->image->colorspace == CMYKColorspace)
525 pixels[i].index*=alpha[i];
526 }
527 delta.x=x-floor(x);
528 delta.y=y-floor(y);
529 epsilon.x=1.0-delta.x;
530 epsilon.y=1.0-delta.y;
531 gamma=((epsilon.y*(epsilon.x*alpha[0]+delta.x*alpha[1])+delta.y*
532 (epsilon.x*alpha[2]+delta.x*alpha[3])));
533 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
534 pixel->red=gamma*(epsilon.y*(epsilon.x*pixels[0].red+delta.x*
535 pixels[1].red)+delta.y*(epsilon.x*pixels[2].red+delta.x*pixels[3].red));
536 pixel->green=gamma*(epsilon.y*(epsilon.x*pixels[0].green+delta.x*
537 pixels[1].green)+delta.y*(epsilon.x*pixels[2].green+delta.x*
538 pixels[3].green));
539 pixel->blue=gamma*(epsilon.y*(epsilon.x*pixels[0].blue+delta.x*
540 pixels[1].blue)+delta.y*(epsilon.x*pixels[2].blue+delta.x*
541 pixels[3].blue));
542 pixel->opacity=(epsilon.y*(epsilon.x*pixels[0].opacity+delta.x*
543 pixels[1].opacity)+delta.y*(epsilon.x*pixels[2].opacity+delta.x*
544 pixels[3].opacity));
545 if (resample_filter->image->colorspace == CMYKColorspace)
546 pixel->index=gamma*(epsilon.y*(epsilon.x*pixels[0].index+delta.x*
547 pixels[1].index)+delta.y*(epsilon.x*pixels[2].index+delta.x*
548 pixels[3].index));
549 break;
550 }
551 case FilterInterpolatePixel:
552 {
cristy54ffe7c2010-07-04 23:54:03 +0000553 CacheView
554 *filter_view;
555
cristy3ed852e2009-09-05 21:47:34 +0000556 Image
557 *excerpt_image,
558 *filter_image;
559
560 MagickPixelPacket
561 pixels[1];
562
563 RectangleInfo
564 geometry;
565
cristy3ed852e2009-09-05 21:47:34 +0000566 geometry.width=4L;
567 geometry.height=4L;
cristybb503372010-05-27 20:51:26 +0000568 geometry.x=(ssize_t) floor(x)-1L;
569 geometry.y=(ssize_t) floor(y)-1L;
cristy3ed852e2009-09-05 21:47:34 +0000570 excerpt_image=ExcerptImage(resample_filter->image,&geometry,
571 resample_filter->exception);
572 if (excerpt_image == (Image *) NULL)
573 {
574 status=MagickFalse;
575 break;
576 }
577 filter_image=ResizeImage(excerpt_image,1,1,resample_filter->image->filter,
578 resample_filter->image->blur,resample_filter->exception);
579 excerpt_image=DestroyImage(excerpt_image);
580 if (filter_image == (Image *) NULL)
581 break;
582 filter_view=AcquireCacheView(filter_image);
583 p=GetCacheViewVirtualPixels(filter_view,0,0,1,1,
584 resample_filter->exception);
585 if (p != (const PixelPacket *) NULL)
586 {
587 indexes=GetVirtualIndexQueue(filter_image);
588 GetMagickPixelPacket(resample_filter->image,pixels);
589 SetMagickPixelPacket(resample_filter->image,p,indexes,pixel);
590 }
591 filter_view=DestroyCacheView(filter_view);
592 filter_image=DestroyImage(filter_image);
593 break;
594 }
595 case IntegerInterpolatePixel:
596 {
597 MagickPixelPacket
598 pixels[1];
599
cristy54ffe7c2010-07-04 23:54:03 +0000600 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x),
601 (ssize_t) floor(y),1,1,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000602 if (p == (const PixelPacket *) NULL)
603 {
604 status=MagickFalse;
605 break;
606 }
607 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
608 GetMagickPixelPacket(resample_filter->image,pixels);
609 SetMagickPixelPacket(resample_filter->image,p,indexes,pixel);
610 break;
611 }
612 case MeshInterpolatePixel:
613 {
614 MagickPixelPacket
615 pixels[4];
616
617 MagickRealType
618 alpha[4],
619 gamma;
620
621 PointInfo
622 delta,
623 luminance;
624
cristy54ffe7c2010-07-04 23:54:03 +0000625 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x),
626 (ssize_t) floor(y),2,2,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000627 if (p == (const PixelPacket *) NULL)
628 {
629 status=MagickFalse;
630 break;
631 }
632 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
633 for (i=0; i < 4L; i++)
634 {
635 GetMagickPixelPacket(resample_filter->image,pixels+i);
636 SetMagickPixelPacket(resample_filter->image,p,indexes+i,pixels+i);
637 alpha[i]=1.0;
638 if (resample_filter->image->matte != MagickFalse)
639 {
cristy46f08202010-01-10 04:04:21 +0000640 alpha[i]=QuantumScale*((MagickRealType) GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000641 pixels[i].red*=alpha[i];
642 pixels[i].green*=alpha[i];
643 pixels[i].blue*=alpha[i];
644 if (resample_filter->image->colorspace == CMYKColorspace)
645 pixels[i].index*=alpha[i];
646 }
647 p++;
648 }
649 delta.x=x-floor(x);
650 delta.y=y-floor(y);
651 luminance.x=MagickPixelLuminance(pixels+0)-MagickPixelLuminance(pixels+3);
652 luminance.y=MagickPixelLuminance(pixels+1)-MagickPixelLuminance(pixels+2);
653 if (fabs(luminance.x) < fabs(luminance.y))
654 {
655 /*
656 Diagonal 0-3 NW-SE.
657 */
658 if (delta.x <= delta.y)
659 {
660 /*
661 Bottom-left triangle (pixel:2, diagonal: 0-3).
662 */
663 delta.y=1.0-delta.y;
664 gamma=MeshInterpolate(&delta,alpha[2],alpha[3],alpha[0]);
665 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
666 pixel->red=gamma*MeshInterpolate(&delta,pixels[2].red,
667 pixels[3].red,pixels[0].red);
668 pixel->green=gamma*MeshInterpolate(&delta,pixels[2].green,
669 pixels[3].green,pixels[0].green);
670 pixel->blue=gamma*MeshInterpolate(&delta,pixels[2].blue,
671 pixels[3].blue,pixels[0].blue);
672 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[2].opacity,
673 pixels[3].opacity,pixels[0].opacity);
674 if (resample_filter->image->colorspace == CMYKColorspace)
675 pixel->index=gamma*MeshInterpolate(&delta,pixels[2].index,
676 pixels[3].index,pixels[0].index);
677 }
678 else
679 {
680 /*
681 Top-right triangle (pixel:1, diagonal: 0-3).
682 */
683 delta.x=1.0-delta.x;
684 gamma=MeshInterpolate(&delta,alpha[1],alpha[0],alpha[3]);
685 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
686 pixel->red=gamma*MeshInterpolate(&delta,pixels[1].red,
687 pixels[0].red,pixels[3].red);
688 pixel->green=gamma*MeshInterpolate(&delta,pixels[1].green,
689 pixels[0].green,pixels[3].green);
690 pixel->blue=gamma*MeshInterpolate(&delta,pixels[1].blue,
691 pixels[0].blue,pixels[3].blue);
692 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[1].opacity,
693 pixels[0].opacity,pixels[3].opacity);
694 if (resample_filter->image->colorspace == CMYKColorspace)
695 pixel->index=gamma*MeshInterpolate(&delta,pixels[1].index,
696 pixels[0].index,pixels[3].index);
697 }
698 }
699 else
700 {
701 /*
702 Diagonal 1-2 NE-SW.
703 */
704 if (delta.x <= (1.0-delta.y))
705 {
706 /*
707 Top-left triangle (pixel 0, diagonal: 1-2).
708 */
709 gamma=MeshInterpolate(&delta,alpha[0],alpha[1],alpha[2]);
710 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
711 pixel->red=gamma*MeshInterpolate(&delta,pixels[0].red,
712 pixels[1].red,pixels[2].red);
713 pixel->green=gamma*MeshInterpolate(&delta,pixels[0].green,
714 pixels[1].green,pixels[2].green);
715 pixel->blue=gamma*MeshInterpolate(&delta,pixels[0].blue,
716 pixels[1].blue,pixels[2].blue);
717 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[0].opacity,
718 pixels[1].opacity,pixels[2].opacity);
719 if (resample_filter->image->colorspace == CMYKColorspace)
720 pixel->index=gamma*MeshInterpolate(&delta,pixels[0].index,
721 pixels[1].index,pixels[2].index);
722 }
723 else
724 {
725 /*
726 Bottom-right triangle (pixel: 3, diagonal: 1-2).
727 */
728 delta.x=1.0-delta.x;
729 delta.y=1.0-delta.y;
730 gamma=MeshInterpolate(&delta,alpha[3],alpha[2],alpha[1]);
731 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
732 pixel->red=gamma*MeshInterpolate(&delta,pixels[3].red,
733 pixels[2].red,pixels[1].red);
734 pixel->green=gamma*MeshInterpolate(&delta,pixels[3].green,
735 pixels[2].green,pixels[1].green);
736 pixel->blue=gamma*MeshInterpolate(&delta,pixels[3].blue,
737 pixels[2].blue,pixels[1].blue);
738 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[3].opacity,
739 pixels[2].opacity,pixels[1].opacity);
740 if (resample_filter->image->colorspace == CMYKColorspace)
741 pixel->index=gamma*MeshInterpolate(&delta,pixels[3].index,
742 pixels[2].index,pixels[1].index);
743 }
744 }
745 break;
746 }
747 case NearestNeighborInterpolatePixel:
748 {
749 MagickPixelPacket
750 pixels[1];
751
752 p=GetCacheViewVirtualPixels(resample_filter->view,NearestNeighbor(x),
753 NearestNeighbor(y),1,1,resample_filter->exception);
754 if (p == (const PixelPacket *) NULL)
755 {
756 status=MagickFalse;
757 break;
758 }
759 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
760 GetMagickPixelPacket(resample_filter->image,pixels);
761 SetMagickPixelPacket(resample_filter->image,p,indexes,pixel);
762 break;
763 }
764 case SplineInterpolatePixel:
765 {
cristy3ed852e2009-09-05 21:47:34 +0000766 MagickPixelPacket
767 pixels[16];
768
769 MagickRealType
770 alpha[16],
771 dx,
772 dy,
773 gamma;
774
775 PointInfo
776 delta;
777
cristy54ffe7c2010-07-04 23:54:03 +0000778 ssize_t
779 j,
780 n;
781
782 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x)-1,
783 (ssize_t) floor(y)-1,4,4,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000784 if (p == (const PixelPacket *) NULL)
785 {
786 status=MagickFalse;
787 break;
788 }
789 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
790 n=0;
791 delta.x=x-floor(x);
792 delta.y=y-floor(y);
793 for (i=(-1); i < 3L; i++)
794 {
795 dy=CubicWeightingFunction((MagickRealType) i-delta.y);
796 for (j=(-1); j < 3L; j++)
797 {
798 GetMagickPixelPacket(resample_filter->image,pixels+n);
799 SetMagickPixelPacket(resample_filter->image,p,indexes+n,pixels+n);
800 alpha[n]=1.0;
801 if (resample_filter->image->matte != MagickFalse)
802 {
cristy54ffe7c2010-07-04 23:54:03 +0000803 alpha[n]=QuantumScale*((MagickRealType)
804 GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000805 pixels[n].red*=alpha[n];
806 pixels[n].green*=alpha[n];
807 pixels[n].blue*=alpha[n];
808 if (resample_filter->image->colorspace == CMYKColorspace)
809 pixels[n].index*=alpha[n];
810 }
811 dx=CubicWeightingFunction(delta.x-(MagickRealType) j);
812 gamma=alpha[n];
813 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
814 pixel->red+=gamma*dx*dy*pixels[n].red;
815 pixel->green+=gamma*dx*dy*pixels[n].green;
816 pixel->blue+=gamma*dx*dy*pixels[n].blue;
817 if (resample_filter->image->matte != MagickFalse)
818 pixel->opacity+=dx*dy*pixels[n].opacity;
819 if (resample_filter->image->colorspace == CMYKColorspace)
820 pixel->index+=gamma*dx*dy*pixels[n].index;
821 n++;
822 p++;
823 }
824 }
825 break;
826 }
827 }
828 return(status);
829}
830
831/*
832%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833% %
834% %
835% %
836% R e s a m p l e P i x e l C o l o r %
837% %
838% %
839% %
840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841%
842% ResamplePixelColor() samples the pixel values surrounding the location
843% given using an elliptical weighted average, at the scale previously
844% calculated, and in the most efficent manner possible for the
845% VirtualPixelMethod setting.
846%
847% The format of the ResamplePixelColor method is:
848%
849% MagickBooleanType ResamplePixelColor(ResampleFilter *resample_filter,
850% const double u0,const double v0,MagickPixelPacket *pixel)
851%
852% A description of each parameter follows:
853%
854% o resample_filter: the resample filter.
855%
856% o u0,v0: A double representing the center of the area to resample,
857% The distortion transformed transformed x,y coordinate.
858%
859% o pixel: the resampled pixel is returned here.
860%
861*/
862MagickExport MagickBooleanType ResamplePixelColor(
863 ResampleFilter *resample_filter,const double u0,const double v0,
864 MagickPixelPacket *pixel)
865{
866 MagickBooleanType
867 status;
868
anthony490ab032010-09-20 00:02:08 +0000869 ssize_t u,v, v1, v2, uw, hit;
cristy3ed852e2009-09-05 21:47:34 +0000870 double u1;
871 double U,V,Q,DQ,DDQ;
872 double divisor_c,divisor_m;
873 register double weight;
874 register const PixelPacket *pixels;
875 register const IndexPacket *indexes;
876 assert(resample_filter != (ResampleFilter *) NULL);
877 assert(resample_filter->signature == MagickSignature);
878
anthony490ab032010-09-20 00:02:08 +0000879#if DEBUG_ELLIPSE
880 fprintf(stderr, "u0=%lf; v0=%lf;\n", u0, v0);
881#endif
882
cristy3ed852e2009-09-05 21:47:34 +0000883 status=MagickTrue;
884 GetMagickPixelPacket(resample_filter->image,pixel);
885 if ( resample_filter->do_interpolate ) {
886 status=InterpolateResampleFilter(resample_filter,
887 resample_filter->interpolate,u0,v0,pixel);
888 return(status);
889 }
890
891 /*
892 Does resample area Miss the image?
893 And is that area a simple solid color - then return that color
894 */
895 hit = 0;
896 switch ( resample_filter->virtual_pixel ) {
897 case BackgroundVirtualPixelMethod:
898 case ConstantVirtualPixelMethod:
899 case TransparentVirtualPixelMethod:
900 case BlackVirtualPixelMethod:
901 case GrayVirtualPixelMethod:
902 case WhiteVirtualPixelMethod:
903 case MaskVirtualPixelMethod:
904 if ( resample_filter->limit_reached
anthonyd638d312010-09-15 13:13:01 +0000905 || u0 + resample_filter->Ulimit < 0.0
906 || u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
907 || v0 + resample_filter->Vlimit < 0.0
908 || v0 - resample_filter->Vlimit > (double) resample_filter->image->rows
cristy3ed852e2009-09-05 21:47:34 +0000909 )
910 hit++;
911 break;
912
913 case UndefinedVirtualPixelMethod:
914 case EdgeVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000915 if ( ( u0 + resample_filter->Ulimit < 0.0 && v0 + resample_filter->Vlimit < 0.0 )
916 || ( u0 + resample_filter->Ulimit < 0.0
917 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows )
918 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
919 && v0 + resample_filter->Vlimit < 0.0 )
920 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
921 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows )
cristy3ed852e2009-09-05 21:47:34 +0000922 )
923 hit++;
924 break;
925 case HorizontalTileVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000926 if ( v0 + resample_filter->Vlimit < 0.0
927 || v0 - resample_filter->Vlimit > (double) resample_filter->image->rows
cristy3ed852e2009-09-05 21:47:34 +0000928 )
929 hit++; /* outside the horizontally tiled images. */
930 break;
931 case VerticalTileVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000932 if ( u0 + resample_filter->Ulimit < 0.0
933 || u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
cristy3ed852e2009-09-05 21:47:34 +0000934 )
935 hit++; /* outside the vertically tiled images. */
936 break;
937 case DitherVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000938 if ( ( u0 + resample_filter->Ulimit < -32.0 && v0 + resample_filter->Vlimit < -32.0 )
939 || ( u0 + resample_filter->Ulimit < -32.0
940 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows+32.0 )
941 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns+32.0
942 && v0 + resample_filter->Vlimit < -32.0 )
943 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns+32.0
944 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows+32.0 )
cristy3ed852e2009-09-05 21:47:34 +0000945 )
946 hit++;
947 break;
948 case TileVirtualPixelMethod:
949 case MirrorVirtualPixelMethod:
950 case RandomVirtualPixelMethod:
951 case HorizontalTileEdgeVirtualPixelMethod:
952 case VerticalTileEdgeVirtualPixelMethod:
953 case CheckerTileVirtualPixelMethod:
954 /* resampling of area is always needed - no VP limits */
955 break;
956 }
957 if ( hit ) {
958 /* whole area is a solid color -- just return that color */
959 status=InterpolateResampleFilter(resample_filter,IntegerInterpolatePixel,
960 u0,v0,pixel);
961 return(status);
962 }
963
964 /*
965 Scaling limits reached, return an 'averaged' result.
966 */
967 if ( resample_filter->limit_reached ) {
968 switch ( resample_filter->virtual_pixel ) {
969 /* This is always handled by the above, so no need.
970 case BackgroundVirtualPixelMethod:
971 case ConstantVirtualPixelMethod:
972 case TransparentVirtualPixelMethod:
973 case GrayVirtualPixelMethod,
974 case WhiteVirtualPixelMethod
975 case MaskVirtualPixelMethod:
976 */
977 case UndefinedVirtualPixelMethod:
978 case EdgeVirtualPixelMethod:
979 case DitherVirtualPixelMethod:
980 case HorizontalTileEdgeVirtualPixelMethod:
981 case VerticalTileEdgeVirtualPixelMethod:
anthony9b8a5282010-09-15 07:48:39 +0000982 /* We need an average edge pixel, from the correct edge!
cristy3ed852e2009-09-05 21:47:34 +0000983 How should I calculate an average edge color?
984 Just returning an averaged neighbourhood,
985 works well in general, but falls down for TileEdge methods.
986 This needs to be done properly!!!!!!
987 */
988 status=InterpolateResampleFilter(resample_filter,
989 AverageInterpolatePixel,u0,v0,pixel);
990 break;
991 case HorizontalTileVirtualPixelMethod:
992 case VerticalTileVirtualPixelMethod:
993 /* just return the background pixel - Is there more direct way? */
994 status=InterpolateResampleFilter(resample_filter,
995 IntegerInterpolatePixel,(double)-1,(double)-1,pixel);
996 break;
997 case TileVirtualPixelMethod:
998 case MirrorVirtualPixelMethod:
999 case RandomVirtualPixelMethod:
1000 case CheckerTileVirtualPixelMethod:
1001 default:
1002 /* generate a average color of the WHOLE image */
1003 if ( resample_filter->average_defined == MagickFalse ) {
1004 Image
1005 *average_image;
1006
1007 CacheView
1008 *average_view;
1009
1010 GetMagickPixelPacket(resample_filter->image,
1011 (MagickPixelPacket *)&(resample_filter->average_pixel));
1012 resample_filter->average_defined = MagickTrue;
1013
1014 /* Try to get an averaged pixel color of whole image */
1015 average_image=ResizeImage(resample_filter->image,1,1,BoxFilter,1.0,
1016 resample_filter->exception);
1017 if (average_image == (Image *) NULL)
1018 {
1019 *pixel=resample_filter->average_pixel; /* FAILED */
1020 break;
1021 }
1022 average_view=AcquireCacheView(average_image);
1023 pixels=(PixelPacket *)GetCacheViewVirtualPixels(average_view,0,0,1,1,
1024 resample_filter->exception);
1025 if (pixels == (const PixelPacket *) NULL) {
1026 average_view=DestroyCacheView(average_view);
1027 average_image=DestroyImage(average_image);
1028 *pixel=resample_filter->average_pixel; /* FAILED */
1029 break;
1030 }
1031 indexes=(IndexPacket *) GetCacheViewAuthenticIndexQueue(average_view);
1032 SetMagickPixelPacket(resample_filter->image,pixels,indexes,
1033 &(resample_filter->average_pixel));
1034 average_view=DestroyCacheView(average_view);
1035 average_image=DestroyImage(average_image);
anthony490ab032010-09-20 00:02:08 +00001036
1037 if ( resample_filter->virtual_pixel == CheckerTileVirtualPixelMethod )
1038 {
1039 /* CheckerTile is avergae of image average half background */
1040 /* FUTURE: replace with a 50% blend of both pixels */
1041
1042 weight = QuantumScale*((MagickRealType)(QuantumRange-
1043 resample_filter->average_pixel.opacity));
1044 resample_filter->average_pixel.red *= weight;
1045 resample_filter->average_pixel.green *= weight;
1046 resample_filter->average_pixel.blue *= weight;
1047 divisor_c = weight;
1048
1049 weight = QuantumScale*((MagickRealType)(QuantumRange-
1050 resample_filter->image->background_color.opacity));
1051 resample_filter->average_pixel.red +=
1052 weight*resample_filter->image->background_color.red;
1053 resample_filter->average_pixel.green +=
1054 weight*resample_filter->image->background_color.green;
1055 resample_filter->average_pixel.blue +=
1056 weight*resample_filter->image->background_color.blue;
1057 resample_filter->average_pixel.matte +=
1058 resample_filter->image->background_color.opacity;
1059 divisor_c += weight;
1060
1061 resample_filter->average_pixel.red /= divisor_c;
1062 resample_filter->average_pixel.green /= divisor_c;
1063 resample_filter->average_pixel.blue /= divisor_c;
1064 resample_filter->average_pixel.matte /= 2;
1065
1066 }
cristy3ed852e2009-09-05 21:47:34 +00001067 }
1068 *pixel=resample_filter->average_pixel;
1069 break;
1070 }
1071 return(status);
1072 }
1073
1074 /*
1075 Initialize weighted average data collection
1076 */
1077 hit = 0;
1078 divisor_c = 0.0;
1079 divisor_m = 0.0;
1080 pixel->red = pixel->green = pixel->blue = 0.0;
1081 if (resample_filter->image->matte != MagickFalse) pixel->opacity = 0.0;
1082 if (resample_filter->image->colorspace == CMYKColorspace) pixel->index = 0.0;
1083
1084 /*
1085 Determine the parellelogram bounding box fitted to the ellipse
1086 centered at u0,v0. This area is bounding by the lines...
cristy3ed852e2009-09-05 21:47:34 +00001087 */
anthony490ab032010-09-20 00:02:08 +00001088 v1 = (ssize_t)ceil(v0 - resample_filter->Vlimit); /* range of scan lines */
1089 v2 = (ssize_t)floor(v0 + resample_filter->Vlimit);
cristy3ed852e2009-09-05 21:47:34 +00001090
anthony490ab032010-09-20 00:02:08 +00001091 /* scan line start and width accross the parallelogram */
1092 u1 = u0 + (v1-v0)*resample_filter->slope - resample_filter->Uwidth;
1093 uw = (ssize_t)(2.0*resample_filter->Uwidth)+1;
1094
1095#if DEBUG_ELLIPSE
1096 fprintf(stderr, "v1=%ld; v2=%ld\n", (long)v1, (long)v2);
1097 fprintf(stderr, "u1=%ld; uw=%ld\n", (long)u1, (long)uw);
1098#else
1099# define DEBUG_HIT_MISS 0 /* only valid if DEBUG_ELLIPSE is enabled */
1100#endif
cristy3ed852e2009-09-05 21:47:34 +00001101
1102 /*
1103 Do weighted resampling of all pixels, within the scaled ellipse,
1104 bound by a Parellelogram fitted to the ellipse.
1105 */
1106 DDQ = 2*resample_filter->A;
anthony490ab032010-09-20 00:02:08 +00001107 for( v=v1; v<=v2; v++ ) {
1108#if DEBUG_HIT_MISS
1109 long uu = ceil(u1); /* actual pixel location (for debug only) */
1110 fprintf(stderr, "# scan line from pixel %ld, %ld\n", (long)uu, (long)v);
1111#endif
1112 u = (ssize_t)ceil(u1); /* first pixel in scanline */
1113 u1 += resample_filter->slope; /* start of next scan line */
1114
1115
1116 /* location of this first pixel, relative to u0,v0 */
1117 U = (double)u-u0;
cristy3ed852e2009-09-05 21:47:34 +00001118 V = (double)v-v0;
1119
1120 /* Q = ellipse quotent ( if Q<F then pixel is inside ellipse) */
anthony490ab032010-09-20 00:02:08 +00001121 Q = (resample_filter->A*U + resample_filter->B*V)*U + resample_filter->C*V*V;
cristy3ed852e2009-09-05 21:47:34 +00001122 DQ = resample_filter->A*(2.0*U+1) + resample_filter->B*V;
1123
1124 /* get the scanline of pixels for this v */
cristybb503372010-05-27 20:51:26 +00001125 pixels=GetCacheViewVirtualPixels(resample_filter->view,u,v,(size_t) uw,
cristy3ed852e2009-09-05 21:47:34 +00001126 1,resample_filter->exception);
1127 if (pixels == (const PixelPacket *) NULL)
1128 return(MagickFalse);
1129 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
1130
1131 /* count up the weighted pixel colors */
1132 for( u=0; u<uw; u++ ) {
1133 /* Note that the ellipse has been pre-scaled so F = WLUT_WIDTH */
1134 if ( Q < (double)WLUT_WIDTH ) {
1135 weight = resample_filter->filter_lut[(int)Q];
1136
1137 pixel->opacity += weight*pixels->opacity;
1138 divisor_m += weight;
1139
1140 if (resample_filter->image->matte != MagickFalse)
1141 weight *= QuantumScale*((MagickRealType)(QuantumRange-pixels->opacity));
1142 pixel->red += weight*pixels->red;
1143 pixel->green += weight*pixels->green;
1144 pixel->blue += weight*pixels->blue;
1145 if (resample_filter->image->colorspace == CMYKColorspace)
1146 pixel->index += weight*(*indexes);
1147 divisor_c += weight;
1148
1149 hit++;
anthony490ab032010-09-20 00:02:08 +00001150#if DEBUG_HIT_MISS
1151 /* mark the pixel according to hit/miss of the ellipse */
1152 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 3\n",
1153 (long)uu-.1,(double)v-.1,(long)uu+.1,(long)v+.1);
1154 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 3\n",
1155 (long)uu+.1,(double)v-.1,(long)uu-.1,(long)v+.1);
1156 } else {
1157 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 1\n",
1158 (long)uu-.1,(double)v-.1,(long)uu+.1,(long)v+.1);
1159 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 1\n",
1160 (long)uu+.1,(double)v-.1,(long)uu-.1,(long)v+.1);
cristy3ed852e2009-09-05 21:47:34 +00001161 }
anthony490ab032010-09-20 00:02:08 +00001162 uu++;
1163#else
1164 }
1165#endif
cristy3ed852e2009-09-05 21:47:34 +00001166 pixels++;
1167 indexes++;
1168 Q += DQ;
1169 DQ += DDQ;
1170 }
1171 }
anthony490ab032010-09-20 00:02:08 +00001172#if DEBUG_ELLIPSE
1173 fprintf(stderr, "Hit=%ld; Total=%ld;\n", (long)hit, (long)uw*(v2-v1) );
1174#endif
cristy3ed852e2009-09-05 21:47:34 +00001175
1176 /*
1177 Result sanity check -- this should NOT happen
1178 */
anthony490ab032010-09-20 00:02:08 +00001179 if ( hit == 0 ) {
cristy3ed852e2009-09-05 21:47:34 +00001180 /* not enough pixels in resampling, resort to direct interpolation */
anthony490ab032010-09-20 00:02:08 +00001181#if DEBUG_NO_PIXEL_HIT
anthony9b8a5282010-09-15 07:48:39 +00001182 pixel->opacity = pixel->red = pixel->green = pixel->blue = 0;
1183 pixel->red = QuantumRange; /* show pixels for which EWA fails */
1184#else
cristy3ed852e2009-09-05 21:47:34 +00001185 status=InterpolateResampleFilter(resample_filter,
1186 resample_filter->interpolate,u0,v0,pixel);
anthony9b8a5282010-09-15 07:48:39 +00001187#endif
cristy3ed852e2009-09-05 21:47:34 +00001188 return status;
1189 }
1190
1191 /*
1192 Finialize results of resampling
1193 */
1194 divisor_m = 1.0/divisor_m;
cristyce70c172010-01-07 17:15:30 +00001195 pixel->opacity = (MagickRealType) ClampToQuantum(divisor_m*pixel->opacity);
cristy3ed852e2009-09-05 21:47:34 +00001196 divisor_c = 1.0/divisor_c;
cristyce70c172010-01-07 17:15:30 +00001197 pixel->red = (MagickRealType) ClampToQuantum(divisor_c*pixel->red);
1198 pixel->green = (MagickRealType) ClampToQuantum(divisor_c*pixel->green);
1199 pixel->blue = (MagickRealType) ClampToQuantum(divisor_c*pixel->blue);
cristy3ed852e2009-09-05 21:47:34 +00001200 if (resample_filter->image->colorspace == CMYKColorspace)
cristyce70c172010-01-07 17:15:30 +00001201 pixel->index = (MagickRealType) ClampToQuantum(divisor_c*pixel->index);
cristy3ed852e2009-09-05 21:47:34 +00001202 return(MagickTrue);
1203}
1204
anthonyc7b82f22010-09-27 10:42:29 +00001205#if EWA && EWA_CLAMP
1206/*
1207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208% %
1209% %
1210% %
1211- C l a m p U p A x e s %
1212% %
1213% %
1214% %
1215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216%
nicolasc90935c2010-09-27 16:47:39 +00001217% ClampUpAxes() function converts the input vectors into a major and
1218% minor axis unit vectors, and their magnatude. This form allows us
1219% to ensure that the ellipse generated is never smaller than the unit
1220% circle and thus never too small for use in EWA resampling.
anthonyc7b82f22010-09-27 10:42:29 +00001221%
nicolasc90935c2010-09-27 16:47:39 +00001222% This purely mathematical 'magic' was provided by Professor Nicolas
1223% Robidoux and his Masters student Chantal Racette.
anthonyc7b82f22010-09-27 10:42:29 +00001224%
1225% See Reference: "We Recommend Singular Value Decomposition", David Austin
1226% http://www.ams.org/samplings/feature-column/fcarc-svd
1227%
nicolasc90935c2010-09-27 16:47:39 +00001228% By generating Major and Minor Axis vectors, we can actually use the
1229% ellipse in its "canonical form", by remapping the dx,dy of the
1230% sampled point into distances along the major and minor axis unit
1231% vectors.
anthonyc7b82f22010-09-27 10:42:29 +00001232% http://en.wikipedia.org/wiki/Ellipse#Canonical_form
anthonyc7b82f22010-09-27 10:42:29 +00001233*/
1234static void ClampUpAxes(const double dux,
1235 const double dvx,
1236 const double duy,
1237 const double dvy,
1238 double *major_mag,
1239 double *minor_mag,
1240 double *major_unit_x,
1241 double *major_unit_y,
1242 double *minor_unit_x,
1243 double *minor_unit_y)
1244{
1245 /*
1246 * ClampUpAxes takes an input 2x2 matrix
1247 *
1248 * [ a b ] = [ dux duy ]
1249 * [ c d ] = [ dvx dvy ]
1250 *
1251 * and computes from it the major and minor axis vectors [major_x,
1252 * major_y] and [minor_x,minor_y] of the smallest ellipse containing
1253 * both the unit disk and the ellipse which is the image of the unit
1254 * disk by the linear transformation
1255 *
1256 * [ dux duy ] [S] = [s]
1257 * [ dvx dvy ] [T] = [t]
1258 *
1259 * (The vector [S,T] is the difference between a position in output
1260 * space and [X,Y]; the vector [s,t] is the difference between a
1261 * position in input space and [x,y].)
1262 */
1263 /*
1264 * Outputs:
1265 *
1266 * major_mag is the half-length of the major axis of the "new"
1267 * ellipse (in input space).
1268 *
1269 * minor_mag is the half-length of the minor axis of the "new"
1270 * ellipse (in input space).
1271 *
1272 * major_unit_x is the x-coordinate of the major axis direction vector
1273 * of both the "old" and "new" ellipses.
1274 *
1275 * major_unit_y is the y-coordinate of the major axis direction vector.
1276 *
1277 * minor_unit_x is the x-coordinate of the minor axis direction vector.
1278 *
1279 * minor_unit_y is the y-coordinate of the minor axis direction vector.
1280 *
1281 * Unit vectors are useful for computing projections, in particular,
1282 * to compute the distance between a point in output space and the
1283 * center (of a disk) from the position of the corresponding point
1284 * in input space.
nicolasc90935c2010-09-27 16:47:39 +00001285 *
1286 * Now, if you want to modify the input pair of tangent vectors so
1287 * that it defines the modified ellipse, all you have to do is set
1288 *
1289 * newdux = sigmamajor * unitmajor1
1290 * newdvx = sigmamajor * unitmajor2
1291 * newduy = sigmaminor * -unitmajor2
1292 * newdvy = sigmaminor * unitmajor1
1293 *
1294 * and use these new tangent vectors "as if" they were the original
1295 * ones. Most of the time this is a rather drastic change in the
1296 * tangent vectors (even if the singular values are large enough not
1297 * to be clampled). A technical explanation of why things still work
1298 * is found at the end of the discussion below.
1299 *
anthonyc7b82f22010-09-27 10:42:29 +00001300 */
1301 /*
1302 * Discussion:
1303 *
1304 * GOAL: Fix things so that the pullback, in input space, of a disk
1305 * of radius r in output space is an ellipse which contains, at
1306 * least, a disc of radius r. (Make this hold for any r>0.)
1307 *
1308 * METHOD: Find the singular values and (unit) left singular vectors
1309 * of Jinv, clampling up the singular values to 1, and multiplying
1310 * the unit left singular vectors by the new singular values in
1311 * order to get the minor and major ellipse axis vectors.
1312 *
1313 * Inputs:
1314 *
1315 * The Jacobian matrix of the transformation at the output point
1316 * under consideration is defined as follows:
1317 *
1318 * Consider the transformation (x,y) -> (X,Y) from input locations
1319 * to output locations.
1320 *
1321 * The Jacobian matrix J is equal to
1322 *
nicolasc90935c2010-09-27 16:47:39 +00001323 * [ A, B ] = [ dX/dx, dX/dy ]
1324 * [ C, D ] = [ dY/dx, dY/dy ]
anthonyc7b82f22010-09-27 10:42:29 +00001325 *
1326 * Consequently, the vector [A,C] is the tangent vector
1327 * corresponding to input changes in the horizontal direction, and
1328 * the vector [B,D] is the tangent vector corresponding to input
1329 * changes in the vertical direction.
1330 *
1331 * In the context of resampling, it is more natural to use the
1332 * inverse Jacobian matrix Jinv. Jinv is
1333 *
nicolasc90935c2010-09-27 16:47:39 +00001334 * [ a, b ] = [ dx/dX, dx/dY ]
1335 * [ c, d ] = [ dy/dX, dy/dY ]
anthonyc7b82f22010-09-27 10:42:29 +00001336 *
1337 * Note: Jinv can be computed from J with the following matrix
1338 * formula:
1339 *
nicolasc90935c2010-09-27 16:47:39 +00001340 * Jinv = 1/(A*D-B*C) [ D, -B ]
1341 * [ -C, A ]
1342 *
nicolas703291a2010-09-27 18:21:32 +00001343 * What we (implicitly) want to do is replace Jinv by a new Jinv
nicolasc90935c2010-09-27 16:47:39 +00001344 * which generates an ellipse which is as close as possible to the
nicolas703291a2010-09-27 18:21:32 +00001345 * original but which contains the unit disk. This is accomplished
1346 * as follows:
nicolasc90935c2010-09-27 16:47:39 +00001347 *
1348 * Let
1349 *
1350 * Jinv = U Sigma V^T
1351 *
nicolas703291a2010-09-27 18:21:32 +00001352 * be an SVD decomposition of Jinv. (The SVD is not unique. The
1353 * final ellipse does not depend on the particular SVD.) In
nicolasc90935c2010-09-27 16:47:39 +00001354 * principle, what we want is to clamp up the entries of the
1355 * diagonal matrix Sigma so that they are at least 1, and then set
1356 *
1357 * Jinv = U newSigma V^T.
1358 *
1359 * However, we do not need to compute V^T for the following reason:
1360 * V is an orthogonal matrix (that is, it represents a combination
1361 * of a rotation and a reflexion). Consequently, V maps the unit
1362 * circle to itself. For this reason, the exact value of V does not
nicolas703291a2010-09-27 18:21:32 +00001363 * affect the final ellipse, and we choose the identity matrix.
1364 * That is, we simply set
nicolasc90935c2010-09-27 16:47:39 +00001365 *
1366 * Jinv = U newSigma,
1367 *
nicolas703291a2010-09-27 18:21:32 +00001368 * omitting the V^T factor altogether. In the end, we return the two
1369 * diagonal entries of newSigma together with the two columns of U,
1370 * for a total of six returned quantities.
anthonyc7b82f22010-09-27 10:42:29 +00001371 */
1372 /*
1373 * ClampUpAxes was written by Nicolas Robidoux and Chantal Racette
nicolasc90935c2010-09-27 16:47:39 +00001374 * of Laurentian University with funding from the National Science
1375 * and Engineering Research Council of Canada.
nicolas703291a2010-09-27 18:21:32 +00001376 *
nicolas553b36e2010-09-27 18:23:16 +00001377 * The idea of using the SVD to clamp the singular values of the
1378 * linear part of the affine approximation of the pullback
1379 * transformation comes from the astrophysicist Craig DeForest, who
1380 * implemented it for use with (approximate) Gaussian filtering in
1381 * his PDL::Transform code.
nicolas703291a2010-09-27 18:21:32 +00001382 *
1383 * The only (possibly) new math in the following is the selection of
1384 * the largest row of the eigen matrix system in order to stabilize
1385 * the computation in near rank-deficient cases, and the
1386 * corresponding efficient repair of degenerate cases using the norm
1387 * of this largest row. Omitting the "V^T" factor of the SVD may
1388 * also be a new "trick."
anthonyc7b82f22010-09-27 10:42:29 +00001389 */
1390 const double a = dux;
1391 const double b = duy;
1392 const double c = dvx;
1393 const double d = dvy;
1394 /*
1395 * n is the matrix Jinv * transpose(Jinv). Eigenvalues of n are the
1396 * squares of the singular values of Jinv.
1397 */
1398 const double aa = a*a;
1399 const double bb = b*b;
1400 const double cc = c*c;
1401 const double dd = d*d;
1402 /*
1403 * Eigenvectors of n are left singular vectors of Jinv.
1404 */
1405 const double n11 = aa+bb;
1406 const double n12 = a*c+b*d;
1407 const double n21 = n12;
1408 const double n22 = cc+dd;
1409 const double det = a*d-b*c;
1410 const double twice_det = det+det;
1411 const double frobenius_squared = n11+n22;
1412 const double discriminant =
1413 (frobenius_squared+twice_det)*(frobenius_squared-twice_det);
1414 const double sqrt_discriminant = sqrt(discriminant);
1415 /*
1416 * s1 is the largest singular value of the inverse Jacobian
1417 * matrix. In other words, its reciprocal is the smallest singular
1418 * value of the Jacobian matrix itself.
1419 * If s1 = 0, both singular values are 0, and any orthogonal pair of
1420 * left and right factors produces a singular decomposition of Jinv.
nicolasc90935c2010-09-27 16:47:39 +00001421 */
1422 /*
anthonyc7b82f22010-09-27 10:42:29 +00001423 * At first, we only compute the squares of the singular values.
1424 */
1425 const double s1s1 = 0.5*(frobenius_squared+sqrt_discriminant);
1426 /*
1427 * s2 the smallest singular value of the inverse Jacobian
1428 * matrix. Its reciprocal is the largest singular value of the
1429 * Jacobian matrix itself.
1430 */
1431 const double s2s2 = 0.5*(frobenius_squared-sqrt_discriminant);
1432 const double s1s1minusn11 = s1s1-n11;
1433 const double s1s1minusn22 = s1s1-n22;
1434 /*
1435 * u1, the first column of the U factor of a singular decomposition
1436 * of Jinv, is a (non-normalized) left singular vector corresponding
nicolasc90935c2010-09-27 16:47:39 +00001437 * to s1. It has entries u11 and u21. We compute u1 from the fact
1438 * that it is an eigenvector of n corresponding to the eigenvalue
1439 * s1^2.
anthonyc7b82f22010-09-27 10:42:29 +00001440 */
1441 const double s1s1minusn11_squared = s1s1minusn11*s1s1minusn11;
1442 const double s1s1minusn22_squared = s1s1minusn22*s1s1minusn22;
1443 /*
1444 * The following selects the largest row of n-s1^2 I as the one
1445 * which is used to find the eigenvector. If both s1^2-n11 and
1446 * s1^2-n22 are zero, n-s1^2 I is the zero matrix. In that case,
1447 * any vector is an eigenvector; in addition, norm below is equal to
1448 * zero, and, in exact arithmetic, this is the only case in which
1449 * norm = 0. So, setting u1 to the simple but arbitrary vector [1,0]
1450 * if norm = 0 safely takes care of all cases.
1451 */
1452 const double temp_u11 =
1453 ( (s1s1minusn11_squared>=s1s1minusn22_squared) ? n12 : s1s1minusn22 );
1454 const double temp_u21 =
1455 ( (s1s1minusn11_squared>=s1s1minusn22_squared) ? s1s1minusn11 : n21 );
1456 const double norm = sqrt(temp_u11*temp_u11+temp_u21*temp_u21);
1457 /*
1458 * Finalize the entries of first left singular vector (associated
1459 * with the largest singular value).
1460 */
1461 const double u11 = ( (norm>0.0) ? temp_u11/norm : 1.0 );
1462 const double u21 = ( (norm>0.0) ? temp_u21/norm : 0.0 );
1463 /*
1464 * Clamp the singular values up to 1.
1465 */
nicolased227212010-09-27 17:24:57 +00001466 *major_mag = ( (s1s1<=1.0) ? 1.0 : sqrt(s1s1) );
1467 *minor_mag = ( (s2s2<=1.0) ? 1.0 : sqrt(s2s2) );
nicolasc90935c2010-09-27 16:47:39 +00001468 /*
1469 * Return the unit major and minor axis direction vectors.
1470 */
anthonyc7b82f22010-09-27 10:42:29 +00001471 *major_unit_x = u11;
1472 *major_unit_y = u21;
nicolasc90935c2010-09-27 16:47:39 +00001473 *minor_unit_x = -u21;
1474 *minor_unit_y = u11;
anthonyc7b82f22010-09-27 10:42:29 +00001475}
1476
1477#endif
cristy3ed852e2009-09-05 21:47:34 +00001478/*
1479%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480% %
1481% %
1482% %
1483% S c a l e R e s a m p l e F i l t e r %
1484% %
1485% %
1486% %
1487%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488%
1489% ScaleResampleFilter() does all the calculations needed to resample an image
1490% at a specific scale, defined by two scaling vectors. This not using
1491% a orthogonal scaling, but two distorted scaling vectors, to allow the
1492% generation of a angled ellipse.
1493%
1494% As only two deritive scaling vectors are used the center of the ellipse
1495% must be the center of the lookup. That is any curvature that the
1496% distortion may produce is discounted.
1497%
1498% The input vectors are produced by either finding the derivitives of the
1499% distortion function, or the partial derivitives from a distortion mapping.
1500% They do not need to be the orthogonal dx,dy scaling vectors, but can be
1501% calculated from other derivatives. For example you could use dr,da/r
1502% polar coordinate vector scaling vectors
1503%
anthonyc7b82f22010-09-27 10:42:29 +00001504% If u,v = DistortEquation(x,y) OR u = Fu(x,y); v = Fv(x,y)
1505% Then the scaling vectors are determined from the deritives...
cristy3ed852e2009-09-05 21:47:34 +00001506% du/dx, dv/dx and du/dy, dv/dy
anthonyc7b82f22010-09-27 10:42:29 +00001507% If the resulting scaling vectors is othogonally aligned then...
cristy3ed852e2009-09-05 21:47:34 +00001508% dv/dx = 0 and du/dy = 0
anthonyc7b82f22010-09-27 10:42:29 +00001509% Producing an othogonally alligned ellipse in source space for the area to
1510% be resampled.
cristy3ed852e2009-09-05 21:47:34 +00001511%
1512% Note that scaling vectors are different to argument order. Argument order
1513% is the general order the deritives are extracted from the distortion
anthonyc7b82f22010-09-27 10:42:29 +00001514% equations, and not the scaling vectors. As such the middle two vaules
1515% may be swapped from what you expect. Caution is advised.
cristy3ed852e2009-09-05 21:47:34 +00001516%
anthony3ebea1e2010-09-27 13:29:00 +00001517% WARNING: It is assumed that any SetResampleFilter() method call will
1518% always be performed before the ScaleResampleFilter() method, so that the
1519% size of the ellipse will match the support for the resampling filter being
1520% used.
anthony490ab032010-09-20 00:02:08 +00001521%
cristy3ed852e2009-09-05 21:47:34 +00001522% The format of the ScaleResampleFilter method is:
1523%
1524% void ScaleResampleFilter(const ResampleFilter *resample_filter,
1525% const double dux,const double duy,const double dvx,const double dvy)
1526%
1527% A description of each parameter follows:
1528%
1529% o resample_filter: the resampling resample_filterrmation defining the
1530% image being resampled
1531%
1532% o dux,duy,dvx,dvy:
anthonyc7b82f22010-09-27 10:42:29 +00001533% The deritives or scaling vectors defining the EWA ellipse.
1534% NOTE: watch the order, which is based on the order deritives
1535% are usally determined from distortion equations (see above).
1536% The middle two values may need to be swapped if you are thinking
1537% in terms of scaling vectors.
cristy3ed852e2009-09-05 21:47:34 +00001538%
1539*/
1540MagickExport void ScaleResampleFilter(ResampleFilter *resample_filter,
1541 const double dux,const double duy,const double dvx,const double dvy)
1542{
anthonyd638d312010-09-15 13:13:01 +00001543 double A,B,C,F;
cristy3ed852e2009-09-05 21:47:34 +00001544
anthonyc7b82f22010-09-27 10:42:29 +00001545
cristy3ed852e2009-09-05 21:47:34 +00001546 assert(resample_filter != (ResampleFilter *) NULL);
1547 assert(resample_filter->signature == MagickSignature);
1548
1549 resample_filter->limit_reached = MagickFalse;
1550 resample_filter->do_interpolate = MagickFalse;
1551
anthonyb821aaf2010-09-27 13:21:08 +00001552 /* A 'point' filter forces use of interpolation instead of area sampling */
1553 if ( resample_filter->filter == PointFilter )
1554 return; /* EWA turned off - nothing to do */
1555
anthonyc7b82f22010-09-27 10:42:29 +00001556#if DEBUG_ELLIPSE
1557 fprintf(stderr, "# -----\n" );
1558 fprintf(stderr, "dux=%lf; dvx=%lf; duy=%lf; dvy=%lf;\n",
1559 dux, dvx, duy, dvy);
1560#endif
cristy3ed852e2009-09-05 21:47:34 +00001561
1562 /* Find Ellipse Coefficents such that
1563 A*u^2 + B*u*v + C*v^2 = F
1564 With u,v relative to point around which we are resampling.
1565 And the given scaling dx,dy vectors in u,v space
1566 du/dx,dv/dx and du/dy,dv/dy
1567 */
anthonyc7b82f22010-09-27 10:42:29 +00001568#if EWA
anthonyd638d312010-09-15 13:13:01 +00001569 /* Direct conversion of derivatives into elliptical coefficients
anthonyb821aaf2010-09-27 13:21:08 +00001570 However when magnifying images, the scaling vectors will be small
1571 resulting in a ellipse that is too small to sample properly.
1572 As such we need to clamp the major/minor axis to a minumum of 1.0
1573 to prevent it getting too small.
cristy3ed852e2009-09-05 21:47:34 +00001574 */
anthonyc7b82f22010-09-27 10:42:29 +00001575#if EWA_CLAMP
1576 { double major_mag,
1577 minor_mag,
1578 major_x,
1579 major_y,
1580 minor_x,
1581 minor_y;
1582
1583 ClampUpAxes(dux,dvx,duy,dvy, &major_mag, &minor_mag,
1584 &major_x, &major_y, &minor_x, &minor_y);
1585 major_x *= major_mag; major_y *= major_mag;
1586 minor_x *= minor_mag; minor_y *= minor_mag;
1587#if DEBUG_ELLIPSE
1588 fprintf(stderr, "major_x=%lf; major_y=%lf; minor_x=%lf; minor_y=%lf;\n",
1589 major_x, major_y, minor_x, minor_y);
1590#endif
1591 A = major_y*major_y+minor_y*minor_y;
1592 B = -2.0*(major_x*major_y+minor_x*minor_y);
1593 C = major_x*major_x+minor_x*minor_x;
nicolaseaa08622010-09-27 17:06:09 +00001594 F = major_mag*minor_mag;
anthonyc7b82f22010-09-27 10:42:29 +00001595 F *= F; /* square it */
1596 }
1597#else /* raw EWA */
cristy3ed852e2009-09-05 21:47:34 +00001598 A = dvx*dvx+dvy*dvy;
anthonyd638d312010-09-15 13:13:01 +00001599 B = -2.0*(dux*dvx+duy*dvy);
cristy3ed852e2009-09-05 21:47:34 +00001600 C = dux*dux+duy*duy;
anthonyc7b82f22010-09-27 10:42:29 +00001601 F = dux*dvy-duy*dvx;
anthony5708fc62010-09-14 13:52:50 +00001602 F *= F; /* square it */
anthonyc7b82f22010-09-27 10:42:29 +00001603#endif
anthonyd638d312010-09-15 13:13:01 +00001604
anthony490ab032010-09-20 00:02:08 +00001605#else /* HQ_EWA */
anthonyd638d312010-09-15 13:13:01 +00001606 /*
anthonyc7b82f22010-09-27 10:42:29 +00001607 This Paul Heckbert's "Higher Quality EWA" formula, from page 60 in his
1608 thesis, which adds a unit circle to the elliptical area so as to do both
1609 Reconstruction and Prefiltering of the pixels in the resampling. It also
1610 means it is always likely to have at least 4 pixels within the area of the
1611 ellipse, for weighted averaging. No scaling will result with F == 4.0 and
1612 a circle of radius 2.0, and F smaller than this means magnification is
1613 being used.
anthony490ab032010-09-20 00:02:08 +00001614
anthonyc7b82f22010-09-27 10:42:29 +00001615 NOTE: This method produces a very blury result at near unity scale while
anthony490ab032010-09-20 00:02:08 +00001616 producing perfect results for string minitification and magnifications.
1617
anthonyc7b82f22010-09-27 10:42:29 +00001618 However filter support is fixed to 2.0 (no good for Windowed Sinc filters)
cristy3ed852e2009-09-05 21:47:34 +00001619 */
1620 A = dvx*dvx+dvy*dvy+1;
anthonyd638d312010-09-15 13:13:01 +00001621 B = -2.0*(dux*dvx+duy*dvy);
cristy3ed852e2009-09-05 21:47:34 +00001622 C = dux*dux+duy*duy+1;
1623 F = A*C - B*B/4;
cristy3ed852e2009-09-05 21:47:34 +00001624#endif
1625
anthony490ab032010-09-20 00:02:08 +00001626#if DEBUG_ELLIPSE
cristy3ed852e2009-09-05 21:47:34 +00001627 fprintf(stderr, "A=%lf; B=%lf; C=%lf; F=%lf\n", A,B,C,F);
cristy3ed852e2009-09-05 21:47:34 +00001628
anthonyc7b82f22010-09-27 10:42:29 +00001629 /* Figure out the various information directly about the ellipse.
cristy3ed852e2009-09-05 21:47:34 +00001630 This information currently not needed at this time, but may be
1631 needed later for better limit determination.
anthonyd638d312010-09-15 13:13:01 +00001632
1633 It is also good to have as a record for future debugging
cristy3ed852e2009-09-05 21:47:34 +00001634 */
1635 { double alpha, beta, gamma, Major, Minor;
anthony490ab032010-09-20 00:02:08 +00001636 double Eccentricity, Ellipse_Area, Ellipse_Angle;
anthonyd638d312010-09-15 13:13:01 +00001637
cristy3ed852e2009-09-05 21:47:34 +00001638 alpha = A+C;
1639 beta = A-C;
1640 gamma = sqrt(beta*beta + B*B );
1641
1642 if ( alpha - gamma <= MagickEpsilon )
1643 Major = MagickHuge;
1644 else
1645 Major = sqrt(2*F/(alpha - gamma));
1646 Minor = sqrt(2*F/(alpha + gamma));
1647
anthony490ab032010-09-20 00:02:08 +00001648 fprintf(stderr, "# Major=%lf; Minor=%lf\n", Major, Minor );
cristy3ed852e2009-09-05 21:47:34 +00001649
1650 /* other information about ellipse include... */
1651 Eccentricity = Major/Minor;
1652 Ellipse_Area = MagickPI*Major*Minor;
anthony490ab032010-09-20 00:02:08 +00001653 Ellipse_Angle = atan2(B, A-C);
cristy3ed852e2009-09-05 21:47:34 +00001654
anthonyc7b82f22010-09-27 10:42:29 +00001655 fprintf(stderr, "# Angle=%lf Area=%lf\n",
anthony490ab032010-09-20 00:02:08 +00001656 RadiansToDegrees(Ellipse_Angle), Ellipse_Area );
cristy3ed852e2009-09-05 21:47:34 +00001657 }
1658#endif
1659
anthony490ab032010-09-20 00:02:08 +00001660 /* The scaling vectors is impossibly large (producing a very large raw F
1661 value), we may as well not bother doing any form of resampling, as you
1662 risk an near infinite resampled area. In this case some alturnative
1663 means of pixel sampling, such as the average of the whole image is needed
anthonyc7b82f22010-09-27 10:42:29 +00001664 to get a reasonable result. Calculate only as needed.
cristy3ed852e2009-09-05 21:47:34 +00001665 */
anthony490ab032010-09-20 00:02:08 +00001666 if ( (4*A*C - B*B) > MagickHuge ) {
cristy3ed852e2009-09-05 21:47:34 +00001667 resample_filter->limit_reached = MagickTrue;
1668 return;
1669 }
1670
anthony490ab032010-09-20 00:02:08 +00001671 /* Scale ellipse by the appropriate size */
1672 F *= resample_filter->support;
1673 F *= resample_filter->support;
cristy3ed852e2009-09-05 21:47:34 +00001674
anthony490ab032010-09-20 00:02:08 +00001675 /* Othogonal bounds of the Ellipse */
1676 resample_filter->Ulimit = sqrt(4*C*F/(4*A*C-B*B));
1677 resample_filter->Vlimit = sqrt(4*A*F/(4*A*C-B*B));
1678
1679 /* Horizontally aligned Parallelogram fitted to Ellipse */
1680 resample_filter->Uwidth = sqrt(F/A); /* Parallelogram Width / 2 */
1681 resample_filter->slope = -B/(2*A); /* Slope of the parallelogram */
1682
1683#if DEBUG_ELLIPSE
anthony490ab032010-09-20 00:02:08 +00001684 fprintf(stderr, "Ulimit=%lf; Vlimit=%lf; UWidth=%lf; Slope=%lf;\n",
1685 resample_filter->Ulimit, resample_filter->Vlimit,
1686 resample_filter->Uwidth, resample_filter->slope );
1687#endif
cristy3ed852e2009-09-05 21:47:34 +00001688
anthonyd638d312010-09-15 13:13:01 +00001689 /* Check the absolute area of the Parallogram involved...
cristy3ed852e2009-09-05 21:47:34 +00001690 * This limit needs more work, as it gets too slow for
1691 * larger images involved with tiled views of the horizon. */
cristy39f347a2010-09-20 00:29:31 +00001692 if ( (resample_filter->Uwidth * resample_filter->Vlimit)
1693 > (4.0*resample_filter->image_area)) {
cristy3ed852e2009-09-05 21:47:34 +00001694 resample_filter->limit_reached = MagickTrue;
1695 return;
1696 }
1697
anthony5708fc62010-09-14 13:52:50 +00001698 /* Scale ellipse formula to directly index the Filter Lookup Table */
cristy3ed852e2009-09-05 21:47:34 +00001699 { register double scale;
anthony490ab032010-09-20 00:02:08 +00001700 scale = (double)WLUT_WIDTH/F;
cristy3ed852e2009-09-05 21:47:34 +00001701 resample_filter->A = A*scale;
1702 resample_filter->B = B*scale;
1703 resample_filter->C = C*scale;
1704 /* ..ple_filter->F = WLUT_WIDTH; -- hardcoded */
1705 }
1706}
1707
1708/*
1709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710% %
1711% %
1712% %
1713% S e t R e s a m p l e F i l t e r %
1714% %
1715% %
1716% %
1717%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1718%
1719% SetResampleFilter() set the resampling filter lookup table based on a
1720% specific filter. Note that the filter is used as a radial filter not as a
1721% two pass othogonally aligned resampling filter.
1722%
1723% The default Filter, is Gaussian, which is the standard filter used by the
1724% original paper on the Elliptical Weighted Everage Algorithm. However other
1725% filters can also be used.
1726%
1727% The format of the SetResampleFilter method is:
1728%
1729% void SetResampleFilter(ResampleFilter *resample_filter,
1730% const FilterTypes filter,const double blur)
1731%
1732% A description of each parameter follows:
1733%
1734% o resample_filter: resampling resample_filterrmation structure
1735%
1736% o filter: the resize filter for elliptical weighting LUT
1737%
1738% o blur: filter blur factor (radial scaling) for elliptical weighting LUT
1739%
1740*/
1741MagickExport void SetResampleFilter(ResampleFilter *resample_filter,
1742 const FilterTypes filter,const double blur)
1743{
1744 register int
1745 Q;
1746
1747 double
1748 r_scale;
1749
1750 ResizeFilter
1751 *resize_filter;
1752
1753 assert(resample_filter != (ResampleFilter *) NULL);
1754 assert(resample_filter->signature == MagickSignature);
1755
1756 resample_filter->filter = filter;
1757
anthony490ab032010-09-20 00:02:08 +00001758 if ( filter == PointFilter )
anthonyb821aaf2010-09-27 13:21:08 +00001759 {
1760 resample_filter->do_interpolate = MagickTrue;
1761 return; /* EWA turned off - nothing more to do */
1762 }
cristy3ed852e2009-09-05 21:47:34 +00001763
anthony490ab032010-09-20 00:02:08 +00001764 if ( filter == UndefinedFilter )
anthonyb821aaf2010-09-27 13:21:08 +00001765 resample_filter->filter = LanczosFilter;
anthony490ab032010-09-20 00:02:08 +00001766
1767 resize_filter = AcquireResizeFilter(resample_filter->image,
1768 resample_filter->filter,blur,MagickTrue,resample_filter->exception);
1769 if (resize_filter == (ResizeFilter *) NULL)
1770 {
cristy3ed852e2009-09-05 21:47:34 +00001771 (void) ThrowMagickException(resample_filter->exception,GetMagickModule(),
1772 ModuleError, "UnableToSetFilteringValue",
1773 "Fall back to default EWA gaussian filter");
anthony490ab032010-09-20 00:02:08 +00001774 resample_filter->filter = PointFilter;
cristy3ed852e2009-09-05 21:47:34 +00001775 }
anthony490ab032010-09-20 00:02:08 +00001776
anthonyc7b82f22010-09-27 10:42:29 +00001777#if EWA
anthony490ab032010-09-20 00:02:08 +00001778 resample_filter->support = GetResizeFilterSupport(resize_filter);
anthony490ab032010-09-20 00:02:08 +00001779 if ( resample_filter->filter == GaussianFilter )
anthonyc7b82f22010-09-27 10:42:29 +00001780 resample_filter->support = 2.0; /* larger gaussian support */
1781#else
1782 resample_filter->support = 2.0; /* fixed support size for HQ-EWA */
anthony490ab032010-09-20 00:02:08 +00001783#endif
1784
1785 /* Scale radius so the filter LUT covers the full support range */
1786 r_scale = resample_filter->support*sqrt(1.0/(double)WLUT_WIDTH);
1787
1788 /* Fill the LUT with a 1D resize filter function */
1789 for(Q=0; Q<WLUT_WIDTH; Q++)
1790 resample_filter->filter_lut[Q] = (double)
1791 GetResizeFilterWeight(resize_filter,sqrt((double)Q)*r_scale);
1792
1793 /* finished with the resize filter */
1794 resize_filter = DestroyResizeFilter(resize_filter);
1795
anthony3ebea1e2010-09-27 13:29:00 +00001796 /*
1797 Adjust the scaling of the default unit circle
1798 This assumes that any real scaling changes will always
1799 take place AFTER the filter method has been initialized.
1800 */
1801
1802 ScaleResampleFilter(resample_filter, 1.0, 0.0, 0.0, 1.0);
1803
anthony5708fc62010-09-14 13:52:50 +00001804#if 0
anthony490ab032010-09-20 00:02:08 +00001805 This is old code kept for reference only. It is very wrong.
anthonyd638d312010-09-15 13:13:01 +00001806 /*
1807 Create Normal Gaussian 2D Filter Weighted Lookup Table.
1808 A normal EWA guassual lookup would use exp(Q*ALPHA)
1809 where Q = distance squared from 0.0 (center) to 1.0 (edge)
1810 and ALPHA = -4.0*ln(2.0) ==> -2.77258872223978123767
1811 However the table is of length 1024, and equates to a radius of 2px
1812 thus needs to be scaled by ALPHA*4/1024 and any blur factor squared
anthony5708fc62010-09-14 13:52:50 +00001813
anthonyc7b82f22010-09-27 10:42:29 +00001814 The above came from some reference code provided by Fred Weinhaus
1815 and seems to have been a guess that was appropriate for its use
1816 in a 3d perspective landscape mapping program.
anthonyd638d312010-09-15 13:13:01 +00001817 */
anthonyd638d312010-09-15 13:13:01 +00001818 r_scale = -2.77258872223978123767/(WLUT_WIDTH*blur*blur);
1819 for(Q=0; Q<WLUT_WIDTH; Q++)
1820 resample_filter->filter_lut[Q] = exp((double)Q*r_scale);
1821 resample_filter->support = WLUT_WIDTH;
1822 break;
anthony5708fc62010-09-14 13:52:50 +00001823#endif
anthony490ab032010-09-20 00:02:08 +00001824
anthonye06e4c12010-09-15 04:03:52 +00001825#if defined(MAGICKCORE_OPENMP_SUPPORT)
anthony9b8a5282010-09-15 07:48:39 +00001826 /* if( GetOpenMPThreadId() == 0 ) */
anthonye06e4c12010-09-15 04:03:52 +00001827#endif
1828 if (GetImageArtifact(resample_filter->image,"resample:verbose")
1829 != (const char *) NULL)
1830 {
1831 /* Debug output of the filter weighting LUT
1832 Gnuplot the LUT with hoizontal adjusted to 'r' using...
1833 plot [0:2][-.2:1] "lut.dat" using (sqrt($0/1024)*2):1 with lines
1834 The filter values is normalized for comparision
1835 */
anthonyd638d312010-09-15 13:13:01 +00001836 printf("#\n");
anthonye06e4c12010-09-15 04:03:52 +00001837 printf("# Resampling Filter LUT (%d values)\n", WLUT_WIDTH);
1838 printf("#\n");
anthonyd638d312010-09-15 13:13:01 +00001839 printf("# Note: values in table are using a squared radius lookup.\n");
1840 printf("# And the whole table represents the filters support.\n");
anthonye06e4c12010-09-15 04:03:52 +00001841 printf("#\n");
1842 for(Q=0; Q<WLUT_WIDTH; Q++)
anthonyd638d312010-09-15 13:13:01 +00001843 printf("%8.*g %.*g\n",
1844 GetMagickPrecision(),sqrt((double)Q)*r_scale,
1845 GetMagickPrecision(),resample_filter->filter_lut[Q] );
anthonye06e4c12010-09-15 04:03:52 +00001846 }
cristy3ed852e2009-09-05 21:47:34 +00001847 return;
1848}
1849
1850/*
1851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1852% %
1853% %
1854% %
1855% 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 %
1856% %
1857% %
1858% %
1859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1860%
1861% SetResampleFilterInterpolateMethod() changes the interpolation method
1862% associated with the specified resample filter.
1863%
1864% The format of the SetResampleFilterInterpolateMethod method is:
1865%
1866% MagickBooleanType SetResampleFilterInterpolateMethod(
1867% ResampleFilter *resample_filter,const InterpolateMethod method)
1868%
1869% A description of each parameter follows:
1870%
1871% o resample_filter: the resample filter.
1872%
1873% o method: the interpolation method.
1874%
1875*/
1876MagickExport MagickBooleanType SetResampleFilterInterpolateMethod(
1877 ResampleFilter *resample_filter,const InterpolatePixelMethod method)
1878{
1879 assert(resample_filter != (ResampleFilter *) NULL);
1880 assert(resample_filter->signature == MagickSignature);
1881 assert(resample_filter->image != (Image *) NULL);
anthonyd638d312010-09-15 13:13:01 +00001882
cristy3ed852e2009-09-05 21:47:34 +00001883 if (resample_filter->debug != MagickFalse)
1884 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1885 resample_filter->image->filename);
anthonyd638d312010-09-15 13:13:01 +00001886
cristy3ed852e2009-09-05 21:47:34 +00001887 resample_filter->interpolate=method;
anthonyd638d312010-09-15 13:13:01 +00001888
cristy3ed852e2009-09-05 21:47:34 +00001889 return(MagickTrue);
1890}
1891
1892/*
1893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1894% %
1895% %
1896% %
1897% 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 %
1898% %
1899% %
1900% %
1901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1902%
1903% SetResampleFilterVirtualPixelMethod() changes the virtual pixel method
1904% associated with the specified resample filter.
1905%
1906% The format of the SetResampleFilterVirtualPixelMethod method is:
1907%
1908% MagickBooleanType SetResampleFilterVirtualPixelMethod(
1909% ResampleFilter *resample_filter,const VirtualPixelMethod method)
1910%
1911% A description of each parameter follows:
1912%
1913% o resample_filter: the resample filter.
1914%
1915% o method: the virtual pixel method.
1916%
1917*/
1918MagickExport MagickBooleanType SetResampleFilterVirtualPixelMethod(
1919 ResampleFilter *resample_filter,const VirtualPixelMethod method)
1920{
1921 assert(resample_filter != (ResampleFilter *) NULL);
1922 assert(resample_filter->signature == MagickSignature);
1923 assert(resample_filter->image != (Image *) NULL);
1924 if (resample_filter->debug != MagickFalse)
1925 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1926 resample_filter->image->filename);
1927 resample_filter->virtual_pixel=method;
cristy2d5e44d2010-03-12 01:56:29 +00001928 if (method != UndefinedVirtualPixelMethod)
1929 (void) SetCacheViewVirtualPixelMethod(resample_filter->view,method);
cristy3ed852e2009-09-05 21:47:34 +00001930 return(MagickTrue);
1931}