blob: 46f39aa58e246e1a004098602680e8f7ba8a6344 [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 */
anthony490ab032010-09-20 00:02:08 +000074#define DEBUG_ELLIPSE 0 /* output ellipse info for debug */
anthony2e6ab682010-09-28 12:02:25 +000075#define DEBUG_HIT_MISS 0 /* output hit/miss pixels (as gnuplot commands) */
76#define DEBUG_NO_PIXEL_HIT 0 /* Make pixels that fail to hit anything - RED */
anthony490ab032010-09-20 00:02:08 +000077
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);
anthony72949792010-10-08 04:44:56 +0000217 SetResampleFilterInterpolateMethod(resample_filter,
218 resample_filter->image->interpolate);
219 SetResampleFilterVirtualPixelMethod(resample_filter,
220 GetImageVirtualPixelMethod(image));
cristy3ed852e2009-09-05 21:47:34 +0000221
cristy3ed852e2009-09-05 21:47:34 +0000222 return(resample_filter);
223}
224
225/*
226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227% %
228% %
229% %
230% D e s t r o y R e s a m p l e I n f o %
231% %
232% %
233% %
234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235%
236% DestroyResampleFilter() finalizes and cleans up the resampling
237% resample_filter as returned by AcquireResampleFilter(), freeing any memory
238% or other information as needed.
239%
240% The format of the DestroyResampleFilter method is:
241%
242% ResampleFilter *DestroyResampleFilter(ResampleFilter *resample_filter)
243%
244% A description of each parameter follows:
245%
246% o resample_filter: resampling information structure
247%
248*/
249MagickExport ResampleFilter *DestroyResampleFilter(
250 ResampleFilter *resample_filter)
251{
252 assert(resample_filter != (ResampleFilter *) NULL);
253 assert(resample_filter->signature == MagickSignature);
254 assert(resample_filter->image != (Image *) NULL);
255 if (resample_filter->debug != MagickFalse)
256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
257 resample_filter->image->filename);
258 resample_filter->view=DestroyCacheView(resample_filter->view);
259 resample_filter->image=DestroyImage(resample_filter->image);
260 resample_filter->signature=(~MagickSignature);
261 resample_filter=(ResampleFilter *) RelinquishMagickMemory(resample_filter);
262 return(resample_filter);
263}
264
265/*
266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267% %
268% %
269% %
270% 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 %
271% %
272% %
273% %
274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275%
276% InterpolateResampleFilter() applies bi-linear or tri-linear interpolation
277% between a floating point coordinate and the pixels surrounding that
278% coordinate. No pixel area resampling, or scaling of the result is
279% performed.
280%
281% The format of the InterpolateResampleFilter method is:
282%
283% MagickBooleanType InterpolateResampleFilter(
284% ResampleInfo *resample_filter,const InterpolatePixelMethod method,
285% const double x,const double y,MagickPixelPacket *pixel)
286%
287% A description of each parameter follows:
288%
289% o resample_filter: the resample filter.
290%
291% o method: the pixel clor interpolation method.
292%
293% o x,y: A double representing the current (x,y) position of the pixel.
294%
295% o pixel: return the interpolated pixel here.
296%
297*/
298
299static inline double MagickMax(const double x,const double y)
300{
301 if (x > y)
302 return(x);
303 return(y);
304}
305
306static void BicubicInterpolate(const MagickPixelPacket *pixels,const double dx,
307 MagickPixelPacket *pixel)
308{
309 MagickRealType
310 dx2,
311 p,
312 q,
313 r,
314 s;
315
316 dx2=dx*dx;
317 p=(pixels[3].red-pixels[2].red)-(pixels[0].red-pixels[1].red);
318 q=(pixels[0].red-pixels[1].red)-p;
319 r=pixels[2].red-pixels[0].red;
320 s=pixels[1].red;
321 pixel->red=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
322 p=(pixels[3].green-pixels[2].green)-(pixels[0].green-pixels[1].green);
323 q=(pixels[0].green-pixels[1].green)-p;
324 r=pixels[2].green-pixels[0].green;
325 s=pixels[1].green;
326 pixel->green=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
327 p=(pixels[3].blue-pixels[2].blue)-(pixels[0].blue-pixels[1].blue);
328 q=(pixels[0].blue-pixels[1].blue)-p;
329 r=pixels[2].blue-pixels[0].blue;
330 s=pixels[1].blue;
331 pixel->blue=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
332 p=(pixels[3].opacity-pixels[2].opacity)-(pixels[0].opacity-pixels[1].opacity);
333 q=(pixels[0].opacity-pixels[1].opacity)-p;
334 r=pixels[2].opacity-pixels[0].opacity;
335 s=pixels[1].opacity;
336 pixel->opacity=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
337 if (pixel->colorspace == CMYKColorspace)
338 {
339 p=(pixels[3].index-pixels[2].index)-(pixels[0].index-pixels[1].index);
340 q=(pixels[0].index-pixels[1].index)-p;
341 r=pixels[2].index-pixels[0].index;
342 s=pixels[1].index;
343 pixel->index=(dx*dx2*p)+(dx2*q)+(dx*r)+s;
344 }
345}
346
347static inline MagickRealType CubicWeightingFunction(const MagickRealType x)
348{
349 MagickRealType
350 alpha,
351 gamma;
352
353 alpha=MagickMax(x+2.0,0.0);
354 gamma=1.0*alpha*alpha*alpha;
355 alpha=MagickMax(x+1.0,0.0);
356 gamma-=4.0*alpha*alpha*alpha;
357 alpha=MagickMax(x+0.0,0.0);
358 gamma+=6.0*alpha*alpha*alpha;
359 alpha=MagickMax(x-1.0,0.0);
360 gamma-=4.0*alpha*alpha*alpha;
361 return(gamma/6.0);
362}
363
364static inline double MeshInterpolate(const PointInfo *delta,const double p,
365 const double x,const double y)
366{
367 return(delta->x*x+delta->y*y+(1.0-delta->x-delta->y)*p);
368}
369
cristybb503372010-05-27 20:51:26 +0000370static inline ssize_t NearestNeighbor(MagickRealType x)
cristy3ed852e2009-09-05 21:47:34 +0000371{
372 if (x >= 0.0)
cristybb503372010-05-27 20:51:26 +0000373 return((ssize_t) (x+0.5));
374 return((ssize_t) (x-0.5));
cristy3ed852e2009-09-05 21:47:34 +0000375}
376
377static MagickBooleanType InterpolateResampleFilter(
378 ResampleFilter *resample_filter,const InterpolatePixelMethod method,
379 const double x,const double y,MagickPixelPacket *pixel)
380{
381 MagickBooleanType
382 status;
383
384 register const IndexPacket
385 *indexes;
386
387 register const PixelPacket
388 *p;
389
cristybb503372010-05-27 20:51:26 +0000390 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000391 i;
392
393 assert(resample_filter != (ResampleFilter *) NULL);
394 assert(resample_filter->signature == MagickSignature);
395 status=MagickTrue;
anthony2e6ab682010-09-28 12:02:25 +0000396
cristy3ed852e2009-09-05 21:47:34 +0000397 switch (method)
398 {
399 case AverageInterpolatePixel:
400 {
401 MagickPixelPacket
402 pixels[16];
403
404 MagickRealType
405 alpha[16],
406 gamma;
407
cristy54ffe7c2010-07-04 23:54:03 +0000408 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x)-1,
409 (ssize_t) floor(y)-1,4,4,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000410 if (p == (const PixelPacket *) NULL)
411 {
412 status=MagickFalse;
413 break;
414 }
415 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
416 for (i=0; i < 16L; i++)
417 {
418 GetMagickPixelPacket(resample_filter->image,pixels+i);
419 SetMagickPixelPacket(resample_filter->image,p,indexes+i,pixels+i);
420 alpha[i]=1.0;
421 if (resample_filter->image->matte != MagickFalse)
422 {
cristy46f08202010-01-10 04:04:21 +0000423 alpha[i]=QuantumScale*((MagickRealType) GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000424 pixels[i].red*=alpha[i];
425 pixels[i].green*=alpha[i];
426 pixels[i].blue*=alpha[i];
427 if (resample_filter->image->colorspace == CMYKColorspace)
428 pixels[i].index*=alpha[i];
429 }
430 gamma=alpha[i];
431 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
432 pixel->red+=gamma*0.0625*pixels[i].red;
433 pixel->green+=gamma*0.0625*pixels[i].green;
434 pixel->blue+=gamma*0.0625*pixels[i].blue;
435 pixel->opacity+=0.0625*pixels[i].opacity;
436 if (resample_filter->image->colorspace == CMYKColorspace)
437 pixel->index+=gamma*0.0625*pixels[i].index;
438 p++;
439 }
440 break;
441 }
442 case BicubicInterpolatePixel:
443 {
444 MagickPixelPacket
445 pixels[16],
446 u[4];
447
448 MagickRealType
449 alpha[16];
450
451 PointInfo
452 delta;
453
cristy54ffe7c2010-07-04 23:54:03 +0000454 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x)-1,
455 (ssize_t) floor(y)-1,4,4,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000456 if (p == (const PixelPacket *) NULL)
457 {
458 status=MagickFalse;
459 break;
460 }
461 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
462 for (i=0; i < 16L; i++)
463 {
464 GetMagickPixelPacket(resample_filter->image,pixels+i);
465 SetMagickPixelPacket(resample_filter->image,p,indexes+i,pixels+i);
466 alpha[i]=1.0;
467 if (resample_filter->image->matte != MagickFalse)
468 {
cristy46f08202010-01-10 04:04:21 +0000469 alpha[i]=QuantumScale*((MagickRealType) GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000470 pixels[i].red*=alpha[i];
471 pixels[i].green*=alpha[i];
472 pixels[i].blue*=alpha[i];
473 if (resample_filter->image->colorspace == CMYKColorspace)
474 pixels[i].index*=alpha[i];
475 }
476 p++;
477 }
478 delta.x=x-floor(x);
479 for (i=0; i < 4L; i++)
480 BicubicInterpolate(pixels+4*i,delta.x,u+i);
481 delta.y=y-floor(y);
482 BicubicInterpolate(u,delta.y,pixel);
483 break;
484 }
485 case BilinearInterpolatePixel:
486 default:
487 {
488 MagickPixelPacket
489 pixels[4];
490
491 MagickRealType
492 alpha[4],
493 gamma;
494
495 PointInfo
496 delta,
497 epsilon;
498
cristy54ffe7c2010-07-04 23:54:03 +0000499 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x),
500 (ssize_t) floor(y),2,2,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000501 if (p == (const PixelPacket *) NULL)
502 {
503 status=MagickFalse;
504 break;
505 }
506 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
507 for (i=0; i < 4L; i++)
508 {
509 pixels[i].red=(MagickRealType) p[i].red;
510 pixels[i].green=(MagickRealType) p[i].green;
511 pixels[i].blue=(MagickRealType) p[i].blue;
512 pixels[i].opacity=(MagickRealType) p[i].opacity;
513 alpha[i]=1.0;
514 }
515 if (resample_filter->image->matte != MagickFalse)
516 for (i=0; i < 4L; i++)
517 {
518 alpha[i]=QuantumScale*((MagickRealType) QuantumRange-p[i].opacity);
519 pixels[i].red*=alpha[i];
520 pixels[i].green*=alpha[i];
521 pixels[i].blue*=alpha[i];
522 }
523 if (indexes != (IndexPacket *) NULL)
524 for (i=0; i < 4L; i++)
525 {
526 pixels[i].index=(MagickRealType) indexes[i];
527 if (resample_filter->image->colorspace == CMYKColorspace)
528 pixels[i].index*=alpha[i];
529 }
530 delta.x=x-floor(x);
531 delta.y=y-floor(y);
532 epsilon.x=1.0-delta.x;
533 epsilon.y=1.0-delta.y;
534 gamma=((epsilon.y*(epsilon.x*alpha[0]+delta.x*alpha[1])+delta.y*
535 (epsilon.x*alpha[2]+delta.x*alpha[3])));
536 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
537 pixel->red=gamma*(epsilon.y*(epsilon.x*pixels[0].red+delta.x*
538 pixels[1].red)+delta.y*(epsilon.x*pixels[2].red+delta.x*pixels[3].red));
539 pixel->green=gamma*(epsilon.y*(epsilon.x*pixels[0].green+delta.x*
540 pixels[1].green)+delta.y*(epsilon.x*pixels[2].green+delta.x*
541 pixels[3].green));
542 pixel->blue=gamma*(epsilon.y*(epsilon.x*pixels[0].blue+delta.x*
543 pixels[1].blue)+delta.y*(epsilon.x*pixels[2].blue+delta.x*
544 pixels[3].blue));
545 pixel->opacity=(epsilon.y*(epsilon.x*pixels[0].opacity+delta.x*
546 pixels[1].opacity)+delta.y*(epsilon.x*pixels[2].opacity+delta.x*
547 pixels[3].opacity));
548 if (resample_filter->image->colorspace == CMYKColorspace)
549 pixel->index=gamma*(epsilon.y*(epsilon.x*pixels[0].index+delta.x*
550 pixels[1].index)+delta.y*(epsilon.x*pixels[2].index+delta.x*
551 pixels[3].index));
552 break;
553 }
554 case FilterInterpolatePixel:
555 {
cristy54ffe7c2010-07-04 23:54:03 +0000556 CacheView
557 *filter_view;
558
cristy3ed852e2009-09-05 21:47:34 +0000559 Image
560 *excerpt_image,
561 *filter_image;
562
563 MagickPixelPacket
564 pixels[1];
565
566 RectangleInfo
567 geometry;
568
cristy3ed852e2009-09-05 21:47:34 +0000569 geometry.width=4L;
570 geometry.height=4L;
cristybb503372010-05-27 20:51:26 +0000571 geometry.x=(ssize_t) floor(x)-1L;
572 geometry.y=(ssize_t) floor(y)-1L;
cristy3ed852e2009-09-05 21:47:34 +0000573 excerpt_image=ExcerptImage(resample_filter->image,&geometry,
574 resample_filter->exception);
575 if (excerpt_image == (Image *) NULL)
576 {
577 status=MagickFalse;
578 break;
579 }
580 filter_image=ResizeImage(excerpt_image,1,1,resample_filter->image->filter,
581 resample_filter->image->blur,resample_filter->exception);
582 excerpt_image=DestroyImage(excerpt_image);
583 if (filter_image == (Image *) NULL)
584 break;
585 filter_view=AcquireCacheView(filter_image);
586 p=GetCacheViewVirtualPixels(filter_view,0,0,1,1,
587 resample_filter->exception);
588 if (p != (const PixelPacket *) NULL)
589 {
590 indexes=GetVirtualIndexQueue(filter_image);
591 GetMagickPixelPacket(resample_filter->image,pixels);
592 SetMagickPixelPacket(resample_filter->image,p,indexes,pixel);
593 }
594 filter_view=DestroyCacheView(filter_view);
595 filter_image=DestroyImage(filter_image);
596 break;
597 }
598 case IntegerInterpolatePixel:
599 {
600 MagickPixelPacket
601 pixels[1];
602
cristy54ffe7c2010-07-04 23:54:03 +0000603 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x),
604 (ssize_t) floor(y),1,1,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000605 if (p == (const PixelPacket *) NULL)
606 {
607 status=MagickFalse;
608 break;
609 }
610 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
611 GetMagickPixelPacket(resample_filter->image,pixels);
612 SetMagickPixelPacket(resample_filter->image,p,indexes,pixel);
613 break;
614 }
615 case MeshInterpolatePixel:
616 {
617 MagickPixelPacket
618 pixels[4];
619
620 MagickRealType
621 alpha[4],
622 gamma;
623
624 PointInfo
625 delta,
626 luminance;
627
cristy54ffe7c2010-07-04 23:54:03 +0000628 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x),
629 (ssize_t) floor(y),2,2,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000630 if (p == (const PixelPacket *) NULL)
631 {
632 status=MagickFalse;
633 break;
634 }
635 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
636 for (i=0; i < 4L; i++)
637 {
638 GetMagickPixelPacket(resample_filter->image,pixels+i);
639 SetMagickPixelPacket(resample_filter->image,p,indexes+i,pixels+i);
640 alpha[i]=1.0;
641 if (resample_filter->image->matte != MagickFalse)
642 {
cristy46f08202010-01-10 04:04:21 +0000643 alpha[i]=QuantumScale*((MagickRealType) GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000644 pixels[i].red*=alpha[i];
645 pixels[i].green*=alpha[i];
646 pixels[i].blue*=alpha[i];
647 if (resample_filter->image->colorspace == CMYKColorspace)
648 pixels[i].index*=alpha[i];
649 }
650 p++;
651 }
652 delta.x=x-floor(x);
653 delta.y=y-floor(y);
654 luminance.x=MagickPixelLuminance(pixels+0)-MagickPixelLuminance(pixels+3);
655 luminance.y=MagickPixelLuminance(pixels+1)-MagickPixelLuminance(pixels+2);
656 if (fabs(luminance.x) < fabs(luminance.y))
657 {
658 /*
659 Diagonal 0-3 NW-SE.
660 */
661 if (delta.x <= delta.y)
662 {
663 /*
664 Bottom-left triangle (pixel:2, diagonal: 0-3).
665 */
666 delta.y=1.0-delta.y;
667 gamma=MeshInterpolate(&delta,alpha[2],alpha[3],alpha[0]);
668 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
669 pixel->red=gamma*MeshInterpolate(&delta,pixels[2].red,
670 pixels[3].red,pixels[0].red);
671 pixel->green=gamma*MeshInterpolate(&delta,pixels[2].green,
672 pixels[3].green,pixels[0].green);
673 pixel->blue=gamma*MeshInterpolate(&delta,pixels[2].blue,
674 pixels[3].blue,pixels[0].blue);
675 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[2].opacity,
676 pixels[3].opacity,pixels[0].opacity);
677 if (resample_filter->image->colorspace == CMYKColorspace)
678 pixel->index=gamma*MeshInterpolate(&delta,pixels[2].index,
679 pixels[3].index,pixels[0].index);
680 }
681 else
682 {
683 /*
684 Top-right triangle (pixel:1, diagonal: 0-3).
685 */
686 delta.x=1.0-delta.x;
687 gamma=MeshInterpolate(&delta,alpha[1],alpha[0],alpha[3]);
688 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
689 pixel->red=gamma*MeshInterpolate(&delta,pixels[1].red,
690 pixels[0].red,pixels[3].red);
691 pixel->green=gamma*MeshInterpolate(&delta,pixels[1].green,
692 pixels[0].green,pixels[3].green);
693 pixel->blue=gamma*MeshInterpolate(&delta,pixels[1].blue,
694 pixels[0].blue,pixels[3].blue);
695 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[1].opacity,
696 pixels[0].opacity,pixels[3].opacity);
697 if (resample_filter->image->colorspace == CMYKColorspace)
698 pixel->index=gamma*MeshInterpolate(&delta,pixels[1].index,
699 pixels[0].index,pixels[3].index);
700 }
701 }
702 else
703 {
704 /*
705 Diagonal 1-2 NE-SW.
706 */
707 if (delta.x <= (1.0-delta.y))
708 {
709 /*
710 Top-left triangle (pixel 0, diagonal: 1-2).
711 */
712 gamma=MeshInterpolate(&delta,alpha[0],alpha[1],alpha[2]);
713 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
714 pixel->red=gamma*MeshInterpolate(&delta,pixels[0].red,
715 pixels[1].red,pixels[2].red);
716 pixel->green=gamma*MeshInterpolate(&delta,pixels[0].green,
717 pixels[1].green,pixels[2].green);
718 pixel->blue=gamma*MeshInterpolate(&delta,pixels[0].blue,
719 pixels[1].blue,pixels[2].blue);
720 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[0].opacity,
721 pixels[1].opacity,pixels[2].opacity);
722 if (resample_filter->image->colorspace == CMYKColorspace)
723 pixel->index=gamma*MeshInterpolate(&delta,pixels[0].index,
724 pixels[1].index,pixels[2].index);
725 }
726 else
727 {
728 /*
729 Bottom-right triangle (pixel: 3, diagonal: 1-2).
730 */
731 delta.x=1.0-delta.x;
732 delta.y=1.0-delta.y;
733 gamma=MeshInterpolate(&delta,alpha[3],alpha[2],alpha[1]);
734 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
735 pixel->red=gamma*MeshInterpolate(&delta,pixels[3].red,
736 pixels[2].red,pixels[1].red);
737 pixel->green=gamma*MeshInterpolate(&delta,pixels[3].green,
738 pixels[2].green,pixels[1].green);
739 pixel->blue=gamma*MeshInterpolate(&delta,pixels[3].blue,
740 pixels[2].blue,pixels[1].blue);
741 pixel->opacity=gamma*MeshInterpolate(&delta,pixels[3].opacity,
742 pixels[2].opacity,pixels[1].opacity);
743 if (resample_filter->image->colorspace == CMYKColorspace)
744 pixel->index=gamma*MeshInterpolate(&delta,pixels[3].index,
745 pixels[2].index,pixels[1].index);
746 }
747 }
748 break;
749 }
750 case NearestNeighborInterpolatePixel:
751 {
752 MagickPixelPacket
753 pixels[1];
754
755 p=GetCacheViewVirtualPixels(resample_filter->view,NearestNeighbor(x),
756 NearestNeighbor(y),1,1,resample_filter->exception);
757 if (p == (const PixelPacket *) NULL)
758 {
759 status=MagickFalse;
760 break;
761 }
762 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
763 GetMagickPixelPacket(resample_filter->image,pixels);
764 SetMagickPixelPacket(resample_filter->image,p,indexes,pixel);
765 break;
766 }
767 case SplineInterpolatePixel:
768 {
cristy3ed852e2009-09-05 21:47:34 +0000769 MagickPixelPacket
770 pixels[16];
771
772 MagickRealType
773 alpha[16],
774 dx,
775 dy,
776 gamma;
777
778 PointInfo
779 delta;
780
cristy54ffe7c2010-07-04 23:54:03 +0000781 ssize_t
782 j,
783 n;
784
785 p=GetCacheViewVirtualPixels(resample_filter->view,(ssize_t) floor(x)-1,
786 (ssize_t) floor(y)-1,4,4,resample_filter->exception);
cristy3ed852e2009-09-05 21:47:34 +0000787 if (p == (const PixelPacket *) NULL)
788 {
789 status=MagickFalse;
790 break;
791 }
792 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
793 n=0;
794 delta.x=x-floor(x);
795 delta.y=y-floor(y);
796 for (i=(-1); i < 3L; i++)
797 {
798 dy=CubicWeightingFunction((MagickRealType) i-delta.y);
799 for (j=(-1); j < 3L; j++)
800 {
801 GetMagickPixelPacket(resample_filter->image,pixels+n);
802 SetMagickPixelPacket(resample_filter->image,p,indexes+n,pixels+n);
803 alpha[n]=1.0;
804 if (resample_filter->image->matte != MagickFalse)
805 {
cristy54ffe7c2010-07-04 23:54:03 +0000806 alpha[n]=QuantumScale*((MagickRealType)
807 GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000808 pixels[n].red*=alpha[n];
809 pixels[n].green*=alpha[n];
810 pixels[n].blue*=alpha[n];
811 if (resample_filter->image->colorspace == CMYKColorspace)
812 pixels[n].index*=alpha[n];
813 }
814 dx=CubicWeightingFunction(delta.x-(MagickRealType) j);
815 gamma=alpha[n];
816 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
817 pixel->red+=gamma*dx*dy*pixels[n].red;
818 pixel->green+=gamma*dx*dy*pixels[n].green;
819 pixel->blue+=gamma*dx*dy*pixels[n].blue;
820 if (resample_filter->image->matte != MagickFalse)
821 pixel->opacity+=dx*dy*pixels[n].opacity;
822 if (resample_filter->image->colorspace == CMYKColorspace)
823 pixel->index+=gamma*dx*dy*pixels[n].index;
824 n++;
825 p++;
826 }
827 }
828 break;
829 }
830 }
831 return(status);
832}
833
834/*
835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836% %
837% %
838% %
839% R e s a m p l e P i x e l C o l o r %
840% %
841% %
842% %
843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844%
845% ResamplePixelColor() samples the pixel values surrounding the location
846% given using an elliptical weighted average, at the scale previously
847% calculated, and in the most efficent manner possible for the
848% VirtualPixelMethod setting.
849%
850% The format of the ResamplePixelColor method is:
851%
852% MagickBooleanType ResamplePixelColor(ResampleFilter *resample_filter,
853% const double u0,const double v0,MagickPixelPacket *pixel)
854%
855% A description of each parameter follows:
856%
857% o resample_filter: the resample filter.
858%
859% o u0,v0: A double representing the center of the area to resample,
860% The distortion transformed transformed x,y coordinate.
861%
862% o pixel: the resampled pixel is returned here.
863%
864*/
865MagickExport MagickBooleanType ResamplePixelColor(
866 ResampleFilter *resample_filter,const double u0,const double v0,
867 MagickPixelPacket *pixel)
868{
869 MagickBooleanType
870 status;
871
anthony490ab032010-09-20 00:02:08 +0000872 ssize_t u,v, v1, v2, uw, hit;
cristy3ed852e2009-09-05 21:47:34 +0000873 double u1;
874 double U,V,Q,DQ,DDQ;
875 double divisor_c,divisor_m;
876 register double weight;
877 register const PixelPacket *pixels;
878 register const IndexPacket *indexes;
879 assert(resample_filter != (ResampleFilter *) NULL);
880 assert(resample_filter->signature == MagickSignature);
881
882 status=MagickTrue;
883 GetMagickPixelPacket(resample_filter->image,pixel);
884 if ( resample_filter->do_interpolate ) {
885 status=InterpolateResampleFilter(resample_filter,
886 resample_filter->interpolate,u0,v0,pixel);
887 return(status);
888 }
889
anthony2e6ab682010-09-28 12:02:25 +0000890#if DEBUG_ELLIPSE
891 fprintf(stderr, "u0=%lf; v0=%lf;\n", u0, v0);
892#endif
893
cristy3ed852e2009-09-05 21:47:34 +0000894 /*
895 Does resample area Miss the image?
896 And is that area a simple solid color - then return that color
897 */
898 hit = 0;
899 switch ( resample_filter->virtual_pixel ) {
900 case BackgroundVirtualPixelMethod:
901 case ConstantVirtualPixelMethod:
902 case TransparentVirtualPixelMethod:
903 case BlackVirtualPixelMethod:
904 case GrayVirtualPixelMethod:
905 case WhiteVirtualPixelMethod:
906 case MaskVirtualPixelMethod:
907 if ( resample_filter->limit_reached
anthonyd638d312010-09-15 13:13:01 +0000908 || u0 + resample_filter->Ulimit < 0.0
909 || u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
910 || v0 + resample_filter->Vlimit < 0.0
911 || v0 - resample_filter->Vlimit > (double) resample_filter->image->rows
cristy3ed852e2009-09-05 21:47:34 +0000912 )
913 hit++;
914 break;
915
916 case UndefinedVirtualPixelMethod:
917 case EdgeVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000918 if ( ( u0 + resample_filter->Ulimit < 0.0 && v0 + resample_filter->Vlimit < 0.0 )
919 || ( u0 + resample_filter->Ulimit < 0.0
920 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows )
921 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
922 && v0 + resample_filter->Vlimit < 0.0 )
923 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
924 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows )
cristy3ed852e2009-09-05 21:47:34 +0000925 )
926 hit++;
927 break;
928 case HorizontalTileVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000929 if ( v0 + resample_filter->Vlimit < 0.0
930 || v0 - resample_filter->Vlimit > (double) resample_filter->image->rows
cristy3ed852e2009-09-05 21:47:34 +0000931 )
932 hit++; /* outside the horizontally tiled images. */
933 break;
934 case VerticalTileVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000935 if ( u0 + resample_filter->Ulimit < 0.0
936 || u0 - resample_filter->Ulimit > (double) resample_filter->image->columns
cristy3ed852e2009-09-05 21:47:34 +0000937 )
938 hit++; /* outside the vertically tiled images. */
939 break;
940 case DitherVirtualPixelMethod:
anthonyd638d312010-09-15 13:13:01 +0000941 if ( ( u0 + resample_filter->Ulimit < -32.0 && v0 + resample_filter->Vlimit < -32.0 )
942 || ( u0 + resample_filter->Ulimit < -32.0
943 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows+32.0 )
944 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns+32.0
945 && v0 + resample_filter->Vlimit < -32.0 )
946 || ( u0 - resample_filter->Ulimit > (double) resample_filter->image->columns+32.0
947 && v0 - resample_filter->Vlimit > (double) resample_filter->image->rows+32.0 )
cristy3ed852e2009-09-05 21:47:34 +0000948 )
949 hit++;
950 break;
951 case TileVirtualPixelMethod:
952 case MirrorVirtualPixelMethod:
953 case RandomVirtualPixelMethod:
954 case HorizontalTileEdgeVirtualPixelMethod:
955 case VerticalTileEdgeVirtualPixelMethod:
956 case CheckerTileVirtualPixelMethod:
957 /* resampling of area is always needed - no VP limits */
958 break;
959 }
960 if ( hit ) {
961 /* whole area is a solid color -- just return that color */
962 status=InterpolateResampleFilter(resample_filter,IntegerInterpolatePixel,
963 u0,v0,pixel);
964 return(status);
965 }
966
967 /*
968 Scaling limits reached, return an 'averaged' result.
969 */
970 if ( resample_filter->limit_reached ) {
971 switch ( resample_filter->virtual_pixel ) {
972 /* This is always handled by the above, so no need.
973 case BackgroundVirtualPixelMethod:
974 case ConstantVirtualPixelMethod:
975 case TransparentVirtualPixelMethod:
976 case GrayVirtualPixelMethod,
977 case WhiteVirtualPixelMethod
978 case MaskVirtualPixelMethod:
979 */
980 case UndefinedVirtualPixelMethod:
981 case EdgeVirtualPixelMethod:
982 case DitherVirtualPixelMethod:
983 case HorizontalTileEdgeVirtualPixelMethod:
984 case VerticalTileEdgeVirtualPixelMethod:
anthony9b8a5282010-09-15 07:48:39 +0000985 /* We need an average edge pixel, from the correct edge!
cristy3ed852e2009-09-05 21:47:34 +0000986 How should I calculate an average edge color?
987 Just returning an averaged neighbourhood,
988 works well in general, but falls down for TileEdge methods.
989 This needs to be done properly!!!!!!
990 */
991 status=InterpolateResampleFilter(resample_filter,
992 AverageInterpolatePixel,u0,v0,pixel);
993 break;
994 case HorizontalTileVirtualPixelMethod:
995 case VerticalTileVirtualPixelMethod:
996 /* just return the background pixel - Is there more direct way? */
997 status=InterpolateResampleFilter(resample_filter,
998 IntegerInterpolatePixel,(double)-1,(double)-1,pixel);
999 break;
1000 case TileVirtualPixelMethod:
1001 case MirrorVirtualPixelMethod:
1002 case RandomVirtualPixelMethod:
1003 case CheckerTileVirtualPixelMethod:
1004 default:
1005 /* generate a average color of the WHOLE image */
1006 if ( resample_filter->average_defined == MagickFalse ) {
1007 Image
1008 *average_image;
1009
1010 CacheView
1011 *average_view;
1012
1013 GetMagickPixelPacket(resample_filter->image,
1014 (MagickPixelPacket *)&(resample_filter->average_pixel));
1015 resample_filter->average_defined = MagickTrue;
1016
1017 /* Try to get an averaged pixel color of whole image */
1018 average_image=ResizeImage(resample_filter->image,1,1,BoxFilter,1.0,
1019 resample_filter->exception);
1020 if (average_image == (Image *) NULL)
1021 {
1022 *pixel=resample_filter->average_pixel; /* FAILED */
1023 break;
1024 }
1025 average_view=AcquireCacheView(average_image);
1026 pixels=(PixelPacket *)GetCacheViewVirtualPixels(average_view,0,0,1,1,
1027 resample_filter->exception);
1028 if (pixels == (const PixelPacket *) NULL) {
1029 average_view=DestroyCacheView(average_view);
1030 average_image=DestroyImage(average_image);
1031 *pixel=resample_filter->average_pixel; /* FAILED */
1032 break;
1033 }
1034 indexes=(IndexPacket *) GetCacheViewAuthenticIndexQueue(average_view);
1035 SetMagickPixelPacket(resample_filter->image,pixels,indexes,
1036 &(resample_filter->average_pixel));
1037 average_view=DestroyCacheView(average_view);
1038 average_image=DestroyImage(average_image);
anthony490ab032010-09-20 00:02:08 +00001039
1040 if ( resample_filter->virtual_pixel == CheckerTileVirtualPixelMethod )
1041 {
1042 /* CheckerTile is avergae of image average half background */
1043 /* FUTURE: replace with a 50% blend of both pixels */
1044
1045 weight = QuantumScale*((MagickRealType)(QuantumRange-
1046 resample_filter->average_pixel.opacity));
1047 resample_filter->average_pixel.red *= weight;
1048 resample_filter->average_pixel.green *= weight;
1049 resample_filter->average_pixel.blue *= weight;
1050 divisor_c = weight;
1051
1052 weight = QuantumScale*((MagickRealType)(QuantumRange-
1053 resample_filter->image->background_color.opacity));
1054 resample_filter->average_pixel.red +=
1055 weight*resample_filter->image->background_color.red;
1056 resample_filter->average_pixel.green +=
1057 weight*resample_filter->image->background_color.green;
1058 resample_filter->average_pixel.blue +=
1059 weight*resample_filter->image->background_color.blue;
cristybb66d9c2010-10-09 01:40:31 +00001060 resample_filter->average_pixel.opacity +=
anthony490ab032010-09-20 00:02:08 +00001061 resample_filter->image->background_color.opacity;
1062 divisor_c += weight;
1063
1064 resample_filter->average_pixel.red /= divisor_c;
1065 resample_filter->average_pixel.green /= divisor_c;
1066 resample_filter->average_pixel.blue /= divisor_c;
cristybb66d9c2010-10-09 01:40:31 +00001067 resample_filter->average_pixel.opacity /= 2;
anthony490ab032010-09-20 00:02:08 +00001068
1069 }
cristy3ed852e2009-09-05 21:47:34 +00001070 }
1071 *pixel=resample_filter->average_pixel;
1072 break;
1073 }
1074 return(status);
1075 }
1076
1077 /*
1078 Initialize weighted average data collection
1079 */
1080 hit = 0;
1081 divisor_c = 0.0;
1082 divisor_m = 0.0;
1083 pixel->red = pixel->green = pixel->blue = 0.0;
1084 if (resample_filter->image->matte != MagickFalse) pixel->opacity = 0.0;
1085 if (resample_filter->image->colorspace == CMYKColorspace) pixel->index = 0.0;
1086
1087 /*
1088 Determine the parellelogram bounding box fitted to the ellipse
1089 centered at u0,v0. This area is bounding by the lines...
cristy3ed852e2009-09-05 21:47:34 +00001090 */
anthony490ab032010-09-20 00:02:08 +00001091 v1 = (ssize_t)ceil(v0 - resample_filter->Vlimit); /* range of scan lines */
1092 v2 = (ssize_t)floor(v0 + resample_filter->Vlimit);
cristy3ed852e2009-09-05 21:47:34 +00001093
anthony490ab032010-09-20 00:02:08 +00001094 /* scan line start and width accross the parallelogram */
1095 u1 = u0 + (v1-v0)*resample_filter->slope - resample_filter->Uwidth;
1096 uw = (ssize_t)(2.0*resample_filter->Uwidth)+1;
1097
1098#if DEBUG_ELLIPSE
1099 fprintf(stderr, "v1=%ld; v2=%ld\n", (long)v1, (long)v2);
1100 fprintf(stderr, "u1=%ld; uw=%ld\n", (long)u1, (long)uw);
1101#else
1102# define DEBUG_HIT_MISS 0 /* only valid if DEBUG_ELLIPSE is enabled */
1103#endif
cristy3ed852e2009-09-05 21:47:34 +00001104
1105 /*
1106 Do weighted resampling of all pixels, within the scaled ellipse,
1107 bound by a Parellelogram fitted to the ellipse.
1108 */
1109 DDQ = 2*resample_filter->A;
anthony490ab032010-09-20 00:02:08 +00001110 for( v=v1; v<=v2; v++ ) {
1111#if DEBUG_HIT_MISS
1112 long uu = ceil(u1); /* actual pixel location (for debug only) */
1113 fprintf(stderr, "# scan line from pixel %ld, %ld\n", (long)uu, (long)v);
1114#endif
1115 u = (ssize_t)ceil(u1); /* first pixel in scanline */
1116 u1 += resample_filter->slope; /* start of next scan line */
1117
1118
1119 /* location of this first pixel, relative to u0,v0 */
1120 U = (double)u-u0;
cristy3ed852e2009-09-05 21:47:34 +00001121 V = (double)v-v0;
1122
1123 /* Q = ellipse quotent ( if Q<F then pixel is inside ellipse) */
anthony490ab032010-09-20 00:02:08 +00001124 Q = (resample_filter->A*U + resample_filter->B*V)*U + resample_filter->C*V*V;
cristy3ed852e2009-09-05 21:47:34 +00001125 DQ = resample_filter->A*(2.0*U+1) + resample_filter->B*V;
1126
1127 /* get the scanline of pixels for this v */
cristybb503372010-05-27 20:51:26 +00001128 pixels=GetCacheViewVirtualPixels(resample_filter->view,u,v,(size_t) uw,
cristy3ed852e2009-09-05 21:47:34 +00001129 1,resample_filter->exception);
1130 if (pixels == (const PixelPacket *) NULL)
1131 return(MagickFalse);
1132 indexes=GetCacheViewVirtualIndexQueue(resample_filter->view);
1133
1134 /* count up the weighted pixel colors */
1135 for( u=0; u<uw; u++ ) {
1136 /* Note that the ellipse has been pre-scaled so F = WLUT_WIDTH */
1137 if ( Q < (double)WLUT_WIDTH ) {
1138 weight = resample_filter->filter_lut[(int)Q];
1139
1140 pixel->opacity += weight*pixels->opacity;
1141 divisor_m += weight;
1142
1143 if (resample_filter->image->matte != MagickFalse)
1144 weight *= QuantumScale*((MagickRealType)(QuantumRange-pixels->opacity));
1145 pixel->red += weight*pixels->red;
1146 pixel->green += weight*pixels->green;
1147 pixel->blue += weight*pixels->blue;
1148 if (resample_filter->image->colorspace == CMYKColorspace)
1149 pixel->index += weight*(*indexes);
1150 divisor_c += weight;
1151
1152 hit++;
anthony490ab032010-09-20 00:02:08 +00001153#if DEBUG_HIT_MISS
1154 /* mark the pixel according to hit/miss of the ellipse */
1155 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 3\n",
1156 (long)uu-.1,(double)v-.1,(long)uu+.1,(long)v+.1);
1157 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 3\n",
1158 (long)uu+.1,(double)v-.1,(long)uu-.1,(long)v+.1);
1159 } else {
1160 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 1\n",
1161 (long)uu-.1,(double)v-.1,(long)uu+.1,(long)v+.1);
1162 fprintf(stderr, "set arrow from %lf,%lf to %lf,%lf nohead ls 1\n",
1163 (long)uu+.1,(double)v-.1,(long)uu-.1,(long)v+.1);
cristy3ed852e2009-09-05 21:47:34 +00001164 }
anthony490ab032010-09-20 00:02:08 +00001165 uu++;
1166#else
1167 }
1168#endif
cristy3ed852e2009-09-05 21:47:34 +00001169 pixels++;
1170 indexes++;
1171 Q += DQ;
1172 DQ += DDQ;
1173 }
1174 }
anthony490ab032010-09-20 00:02:08 +00001175#if DEBUG_ELLIPSE
1176 fprintf(stderr, "Hit=%ld; Total=%ld;\n", (long)hit, (long)uw*(v2-v1) );
1177#endif
cristy3ed852e2009-09-05 21:47:34 +00001178
1179 /*
1180 Result sanity check -- this should NOT happen
1181 */
anthony490ab032010-09-20 00:02:08 +00001182 if ( hit == 0 ) {
cristy3ed852e2009-09-05 21:47:34 +00001183 /* not enough pixels in resampling, resort to direct interpolation */
anthony490ab032010-09-20 00:02:08 +00001184#if DEBUG_NO_PIXEL_HIT
anthony9b8a5282010-09-15 07:48:39 +00001185 pixel->opacity = pixel->red = pixel->green = pixel->blue = 0;
1186 pixel->red = QuantumRange; /* show pixels for which EWA fails */
1187#else
cristy3ed852e2009-09-05 21:47:34 +00001188 status=InterpolateResampleFilter(resample_filter,
1189 resample_filter->interpolate,u0,v0,pixel);
anthony9b8a5282010-09-15 07:48:39 +00001190#endif
cristy3ed852e2009-09-05 21:47:34 +00001191 return status;
1192 }
1193
1194 /*
1195 Finialize results of resampling
1196 */
1197 divisor_m = 1.0/divisor_m;
cristyce70c172010-01-07 17:15:30 +00001198 pixel->opacity = (MagickRealType) ClampToQuantum(divisor_m*pixel->opacity);
cristy3ed852e2009-09-05 21:47:34 +00001199 divisor_c = 1.0/divisor_c;
cristyce70c172010-01-07 17:15:30 +00001200 pixel->red = (MagickRealType) ClampToQuantum(divisor_c*pixel->red);
1201 pixel->green = (MagickRealType) ClampToQuantum(divisor_c*pixel->green);
1202 pixel->blue = (MagickRealType) ClampToQuantum(divisor_c*pixel->blue);
cristy3ed852e2009-09-05 21:47:34 +00001203 if (resample_filter->image->colorspace == CMYKColorspace)
cristyce70c172010-01-07 17:15:30 +00001204 pixel->index = (MagickRealType) ClampToQuantum(divisor_c*pixel->index);
cristy3ed852e2009-09-05 21:47:34 +00001205 return(MagickTrue);
1206}
1207
anthonyc7b82f22010-09-27 10:42:29 +00001208#if EWA && EWA_CLAMP
1209/*
1210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1211% %
1212% %
1213% %
1214- C l a m p U p A x e s %
1215% %
1216% %
1217% %
1218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1219%
nicolasc90935c2010-09-27 16:47:39 +00001220% ClampUpAxes() function converts the input vectors into a major and
1221% minor axis unit vectors, and their magnatude. This form allows us
1222% to ensure that the ellipse generated is never smaller than the unit
1223% circle and thus never too small for use in EWA resampling.
anthonyc7b82f22010-09-27 10:42:29 +00001224%
nicolasc90935c2010-09-27 16:47:39 +00001225% This purely mathematical 'magic' was provided by Professor Nicolas
1226% Robidoux and his Masters student Chantal Racette.
anthonyc7b82f22010-09-27 10:42:29 +00001227%
1228% See Reference: "We Recommend Singular Value Decomposition", David Austin
1229% http://www.ams.org/samplings/feature-column/fcarc-svd
1230%
nicolasc90935c2010-09-27 16:47:39 +00001231% By generating Major and Minor Axis vectors, we can actually use the
1232% ellipse in its "canonical form", by remapping the dx,dy of the
1233% sampled point into distances along the major and minor axis unit
1234% vectors.
anthonyc7b82f22010-09-27 10:42:29 +00001235% http://en.wikipedia.org/wiki/Ellipse#Canonical_form
anthonyc7b82f22010-09-27 10:42:29 +00001236*/
nicolas15c331b2010-09-29 19:05:00 +00001237static inline void ClampUpAxes(const double dux,
1238 const double dvx,
1239 const double duy,
1240 const double dvy,
1241 double *major_mag,
1242 double *minor_mag,
1243 double *major_unit_x,
1244 double *major_unit_y,
1245 double *minor_unit_x,
1246 double *minor_unit_y)
anthonyc7b82f22010-09-27 10:42:29 +00001247{
1248 /*
1249 * ClampUpAxes takes an input 2x2 matrix
1250 *
1251 * [ a b ] = [ dux duy ]
1252 * [ c d ] = [ dvx dvy ]
1253 *
1254 * and computes from it the major and minor axis vectors [major_x,
1255 * major_y] and [minor_x,minor_y] of the smallest ellipse containing
1256 * both the unit disk and the ellipse which is the image of the unit
1257 * disk by the linear transformation
1258 *
1259 * [ dux duy ] [S] = [s]
1260 * [ dvx dvy ] [T] = [t]
1261 *
1262 * (The vector [S,T] is the difference between a position in output
1263 * space and [X,Y]; the vector [s,t] is the difference between a
1264 * position in input space and [x,y].)
1265 */
1266 /*
1267 * Outputs:
1268 *
1269 * major_mag is the half-length of the major axis of the "new"
1270 * ellipse (in input space).
1271 *
1272 * minor_mag is the half-length of the minor axis of the "new"
1273 * ellipse (in input space).
1274 *
1275 * major_unit_x is the x-coordinate of the major axis direction vector
1276 * of both the "old" and "new" ellipses.
1277 *
1278 * major_unit_y is the y-coordinate of the major axis direction vector.
1279 *
1280 * minor_unit_x is the x-coordinate of the minor axis direction vector.
1281 *
1282 * minor_unit_y is the y-coordinate of the minor axis direction vector.
1283 *
1284 * Unit vectors are useful for computing projections, in particular,
1285 * to compute the distance between a point in output space and the
1286 * center (of a disk) from the position of the corresponding point
1287 * in input space.
nicolasc90935c2010-09-27 16:47:39 +00001288 *
1289 * Now, if you want to modify the input pair of tangent vectors so
1290 * that it defines the modified ellipse, all you have to do is set
1291 *
nicolas8b1d9812010-09-29 18:41:55 +00001292 * newdux = major_mag * major_unit_x
1293 * newdvx = major_mag * major_unit_y
1294 * newduy = minor_mag * minor_unit_x = minor_mag * -major_unit_y
1295 * newdvy = minor_mag * minor_unit_y = minor_mag * major_unit_x
nicolasc90935c2010-09-27 16:47:39 +00001296 *
1297 * and use these new tangent vectors "as if" they were the original
1298 * ones. Most of the time this is a rather drastic change in the
1299 * tangent vectors (even if the singular values are large enough not
1300 * to be clampled). A technical explanation of why things still work
1301 * is found at the end of the discussion below.
1302 *
anthonyc7b82f22010-09-27 10:42:29 +00001303 */
1304 /*
1305 * Discussion:
1306 *
1307 * GOAL: Fix things so that the pullback, in input space, of a disk
1308 * of radius r in output space is an ellipse which contains, at
1309 * least, a disc of radius r. (Make this hold for any r>0.)
1310 *
1311 * METHOD: Find the singular values and (unit) left singular vectors
1312 * of Jinv, clampling up the singular values to 1, and multiplying
1313 * the unit left singular vectors by the new singular values in
1314 * order to get the minor and major ellipse axis vectors.
1315 *
1316 * Inputs:
1317 *
1318 * The Jacobian matrix of the transformation at the output point
1319 * under consideration is defined as follows:
1320 *
1321 * Consider the transformation (x,y) -> (X,Y) from input locations
nicolas8b1d9812010-09-29 18:41:55 +00001322 * to output locations. (Anthony Thyssen, elsewhere in resample.c,
1323 * uses the notation (u,v) -> (x,y) instead of (x,y) -> (X,Y).)
anthonyc7b82f22010-09-27 10:42:29 +00001324 *
1325 * The Jacobian matrix J is equal to
1326 *
nicolasc90935c2010-09-27 16:47:39 +00001327 * [ A, B ] = [ dX/dx, dX/dy ]
1328 * [ C, D ] = [ dY/dx, dY/dy ]
anthonyc7b82f22010-09-27 10:42:29 +00001329 *
1330 * Consequently, the vector [A,C] is the tangent vector
1331 * corresponding to input changes in the horizontal direction, and
1332 * the vector [B,D] is the tangent vector corresponding to input
1333 * changes in the vertical direction.
1334 *
1335 * In the context of resampling, it is more natural to use the
1336 * inverse Jacobian matrix Jinv. Jinv is
1337 *
nicolasc90935c2010-09-27 16:47:39 +00001338 * [ a, b ] = [ dx/dX, dx/dY ]
1339 * [ c, d ] = [ dy/dX, dy/dY ]
anthonyc7b82f22010-09-27 10:42:29 +00001340 *
1341 * Note: Jinv can be computed from J with the following matrix
1342 * formula:
1343 *
nicolasc90935c2010-09-27 16:47:39 +00001344 * Jinv = 1/(A*D-B*C) [ D, -B ]
1345 * [ -C, A ]
1346 *
nicolas703291a2010-09-27 18:21:32 +00001347 * What we (implicitly) want to do is replace Jinv by a new Jinv
nicolasc90935c2010-09-27 16:47:39 +00001348 * which generates an ellipse which is as close as possible to the
nicolas703291a2010-09-27 18:21:32 +00001349 * original but which contains the unit disk. This is accomplished
1350 * as follows:
nicolasc90935c2010-09-27 16:47:39 +00001351 *
1352 * Let
1353 *
1354 * Jinv = U Sigma V^T
1355 *
nicolas703291a2010-09-27 18:21:32 +00001356 * be an SVD decomposition of Jinv. (The SVD is not unique. The
1357 * final ellipse does not depend on the particular SVD.) In
nicolasc90935c2010-09-27 16:47:39 +00001358 * principle, what we want is to clamp up the entries of the
1359 * diagonal matrix Sigma so that they are at least 1, and then set
1360 *
1361 * Jinv = U newSigma V^T.
1362 *
1363 * However, we do not need to compute V^T for the following reason:
1364 * V is an orthogonal matrix (that is, it represents a combination
1365 * of a rotation and a reflexion). Consequently, V maps the unit
1366 * circle to itself. For this reason, the exact value of V does not
nicolas703291a2010-09-27 18:21:32 +00001367 * affect the final ellipse, and we choose the identity matrix.
1368 * That is, we simply set
nicolasc90935c2010-09-27 16:47:39 +00001369 *
1370 * Jinv = U newSigma,
1371 *
nicolas703291a2010-09-27 18:21:32 +00001372 * omitting the V^T factor altogether. In the end, we return the two
1373 * diagonal entries of newSigma together with the two columns of U,
1374 * for a total of six returned quantities.
anthonyc7b82f22010-09-27 10:42:29 +00001375 */
1376 /*
1377 * ClampUpAxes was written by Nicolas Robidoux and Chantal Racette
nicolasc90935c2010-09-27 16:47:39 +00001378 * of Laurentian University with funding from the National Science
1379 * and Engineering Research Council of Canada.
nicolas703291a2010-09-27 18:21:32 +00001380 *
nicolas553b36e2010-09-27 18:23:16 +00001381 * The idea of using the SVD to clamp the singular values of the
1382 * linear part of the affine approximation of the pullback
1383 * transformation comes from the astrophysicist Craig DeForest, who
1384 * implemented it for use with (approximate) Gaussian filtering in
nicolas8b1d9812010-09-29 18:41:55 +00001385 * his PDL::Transform code (PDL = Perl Data Language).
nicolas703291a2010-09-27 18:21:32 +00001386 *
1387 * The only (possibly) new math in the following is the selection of
1388 * the largest row of the eigen matrix system in order to stabilize
1389 * the computation in near rank-deficient cases, and the
1390 * corresponding efficient repair of degenerate cases using the norm
1391 * of this largest row. Omitting the "V^T" factor of the SVD may
1392 * also be a new "trick."
anthonyc7b82f22010-09-27 10:42:29 +00001393 */
1394 const double a = dux;
1395 const double b = duy;
1396 const double c = dvx;
1397 const double d = dvy;
1398 /*
1399 * n is the matrix Jinv * transpose(Jinv). Eigenvalues of n are the
1400 * squares of the singular values of Jinv.
1401 */
1402 const double aa = a*a;
1403 const double bb = b*b;
1404 const double cc = c*c;
1405 const double dd = d*d;
1406 /*
1407 * Eigenvectors of n are left singular vectors of Jinv.
1408 */
1409 const double n11 = aa+bb;
1410 const double n12 = a*c+b*d;
1411 const double n21 = n12;
1412 const double n22 = cc+dd;
1413 const double det = a*d-b*c;
1414 const double twice_det = det+det;
1415 const double frobenius_squared = n11+n22;
1416 const double discriminant =
1417 (frobenius_squared+twice_det)*(frobenius_squared-twice_det);
1418 const double sqrt_discriminant = sqrt(discriminant);
1419 /*
1420 * s1 is the largest singular value of the inverse Jacobian
1421 * matrix. In other words, its reciprocal is the smallest singular
1422 * value of the Jacobian matrix itself.
1423 * If s1 = 0, both singular values are 0, and any orthogonal pair of
1424 * left and right factors produces a singular decomposition of Jinv.
nicolasc90935c2010-09-27 16:47:39 +00001425 */
1426 /*
nicolas8b1d9812010-09-29 18:41:55 +00001427 * Initially, we only compute the squares of the singular values.
anthonyc7b82f22010-09-27 10:42:29 +00001428 */
1429 const double s1s1 = 0.5*(frobenius_squared+sqrt_discriminant);
1430 /*
1431 * s2 the smallest singular value of the inverse Jacobian
1432 * matrix. Its reciprocal is the largest singular value of the
1433 * Jacobian matrix itself.
1434 */
1435 const double s2s2 = 0.5*(frobenius_squared-sqrt_discriminant);
1436 const double s1s1minusn11 = s1s1-n11;
1437 const double s1s1minusn22 = s1s1-n22;
1438 /*
1439 * u1, the first column of the U factor of a singular decomposition
1440 * of Jinv, is a (non-normalized) left singular vector corresponding
nicolasc90935c2010-09-27 16:47:39 +00001441 * to s1. It has entries u11 and u21. We compute u1 from the fact
1442 * that it is an eigenvector of n corresponding to the eigenvalue
1443 * s1^2.
anthonyc7b82f22010-09-27 10:42:29 +00001444 */
1445 const double s1s1minusn11_squared = s1s1minusn11*s1s1minusn11;
1446 const double s1s1minusn22_squared = s1s1minusn22*s1s1minusn22;
1447 /*
1448 * The following selects the largest row of n-s1^2 I as the one
1449 * which is used to find the eigenvector. If both s1^2-n11 and
1450 * s1^2-n22 are zero, n-s1^2 I is the zero matrix. In that case,
1451 * any vector is an eigenvector; in addition, norm below is equal to
1452 * zero, and, in exact arithmetic, this is the only case in which
1453 * norm = 0. So, setting u1 to the simple but arbitrary vector [1,0]
1454 * if norm = 0 safely takes care of all cases.
1455 */
1456 const double temp_u11 =
1457 ( (s1s1minusn11_squared>=s1s1minusn22_squared) ? n12 : s1s1minusn22 );
1458 const double temp_u21 =
1459 ( (s1s1minusn11_squared>=s1s1minusn22_squared) ? s1s1minusn11 : n21 );
1460 const double norm = sqrt(temp_u11*temp_u11+temp_u21*temp_u21);
1461 /*
1462 * Finalize the entries of first left singular vector (associated
1463 * with the largest singular value).
1464 */
1465 const double u11 = ( (norm>0.0) ? temp_u11/norm : 1.0 );
1466 const double u21 = ( (norm>0.0) ? temp_u21/norm : 0.0 );
1467 /*
1468 * Clamp the singular values up to 1.
1469 */
nicolased227212010-09-27 17:24:57 +00001470 *major_mag = ( (s1s1<=1.0) ? 1.0 : sqrt(s1s1) );
1471 *minor_mag = ( (s2s2<=1.0) ? 1.0 : sqrt(s2s2) );
nicolasc90935c2010-09-27 16:47:39 +00001472 /*
1473 * Return the unit major and minor axis direction vectors.
1474 */
anthonyc7b82f22010-09-27 10:42:29 +00001475 *major_unit_x = u11;
1476 *major_unit_y = u21;
nicolasc90935c2010-09-27 16:47:39 +00001477 *minor_unit_x = -u21;
1478 *minor_unit_y = u11;
anthonyc7b82f22010-09-27 10:42:29 +00001479}
1480
1481#endif
cristy3ed852e2009-09-05 21:47:34 +00001482/*
1483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484% %
1485% %
1486% %
1487% S c a l e R e s a m p l e F i l t e r %
1488% %
1489% %
1490% %
1491%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492%
1493% ScaleResampleFilter() does all the calculations needed to resample an image
1494% at a specific scale, defined by two scaling vectors. This not using
1495% a orthogonal scaling, but two distorted scaling vectors, to allow the
1496% generation of a angled ellipse.
1497%
1498% As only two deritive scaling vectors are used the center of the ellipse
1499% must be the center of the lookup. That is any curvature that the
1500% distortion may produce is discounted.
1501%
1502% The input vectors are produced by either finding the derivitives of the
1503% distortion function, or the partial derivitives from a distortion mapping.
1504% They do not need to be the orthogonal dx,dy scaling vectors, but can be
1505% calculated from other derivatives. For example you could use dr,da/r
1506% polar coordinate vector scaling vectors
1507%
anthonyc7b82f22010-09-27 10:42:29 +00001508% If u,v = DistortEquation(x,y) OR u = Fu(x,y); v = Fv(x,y)
1509% Then the scaling vectors are determined from the deritives...
cristy3ed852e2009-09-05 21:47:34 +00001510% du/dx, dv/dx and du/dy, dv/dy
anthonyc7b82f22010-09-27 10:42:29 +00001511% If the resulting scaling vectors is othogonally aligned then...
cristy3ed852e2009-09-05 21:47:34 +00001512% dv/dx = 0 and du/dy = 0
anthonyc7b82f22010-09-27 10:42:29 +00001513% Producing an othogonally alligned ellipse in source space for the area to
1514% be resampled.
cristy3ed852e2009-09-05 21:47:34 +00001515%
1516% Note that scaling vectors are different to argument order. Argument order
1517% is the general order the deritives are extracted from the distortion
anthonyc7b82f22010-09-27 10:42:29 +00001518% equations, and not the scaling vectors. As such the middle two vaules
1519% may be swapped from what you expect. Caution is advised.
cristy3ed852e2009-09-05 21:47:34 +00001520%
anthony3ebea1e2010-09-27 13:29:00 +00001521% WARNING: It is assumed that any SetResampleFilter() method call will
1522% always be performed before the ScaleResampleFilter() method, so that the
1523% size of the ellipse will match the support for the resampling filter being
1524% used.
anthony490ab032010-09-20 00:02:08 +00001525%
cristy3ed852e2009-09-05 21:47:34 +00001526% The format of the ScaleResampleFilter method is:
1527%
1528% void ScaleResampleFilter(const ResampleFilter *resample_filter,
1529% const double dux,const double duy,const double dvx,const double dvy)
1530%
1531% A description of each parameter follows:
1532%
1533% o resample_filter: the resampling resample_filterrmation defining the
1534% image being resampled
1535%
1536% o dux,duy,dvx,dvy:
anthonyc7b82f22010-09-27 10:42:29 +00001537% The deritives or scaling vectors defining the EWA ellipse.
1538% NOTE: watch the order, which is based on the order deritives
1539% are usally determined from distortion equations (see above).
1540% The middle two values may need to be swapped if you are thinking
1541% in terms of scaling vectors.
cristy3ed852e2009-09-05 21:47:34 +00001542%
1543*/
1544MagickExport void ScaleResampleFilter(ResampleFilter *resample_filter,
1545 const double dux,const double duy,const double dvx,const double dvy)
1546{
anthonyd638d312010-09-15 13:13:01 +00001547 double A,B,C,F;
cristy3ed852e2009-09-05 21:47:34 +00001548
1549 assert(resample_filter != (ResampleFilter *) NULL);
1550 assert(resample_filter->signature == MagickSignature);
1551
1552 resample_filter->limit_reached = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001553
anthonyb821aaf2010-09-27 13:21:08 +00001554 /* A 'point' filter forces use of interpolation instead of area sampling */
1555 if ( resample_filter->filter == PointFilter )
1556 return; /* EWA turned off - nothing to do */
1557
anthonyc7b82f22010-09-27 10:42:29 +00001558#if DEBUG_ELLIPSE
1559 fprintf(stderr, "# -----\n" );
1560 fprintf(stderr, "dux=%lf; dvx=%lf; duy=%lf; dvy=%lf;\n",
1561 dux, dvx, duy, dvy);
1562#endif
cristy3ed852e2009-09-05 21:47:34 +00001563
1564 /* Find Ellipse Coefficents such that
1565 A*u^2 + B*u*v + C*v^2 = F
1566 With u,v relative to point around which we are resampling.
1567 And the given scaling dx,dy vectors in u,v space
1568 du/dx,dv/dx and du/dy,dv/dy
1569 */
anthonyc7b82f22010-09-27 10:42:29 +00001570#if EWA
anthonyd638d312010-09-15 13:13:01 +00001571 /* Direct conversion of derivatives into elliptical coefficients
anthonyb821aaf2010-09-27 13:21:08 +00001572 However when magnifying images, the scaling vectors will be small
1573 resulting in a ellipse that is too small to sample properly.
1574 As such we need to clamp the major/minor axis to a minumum of 1.0
1575 to prevent it getting too small.
cristy3ed852e2009-09-05 21:47:34 +00001576 */
anthonyc7b82f22010-09-27 10:42:29 +00001577#if EWA_CLAMP
1578 { double major_mag,
1579 minor_mag,
1580 major_x,
1581 major_y,
1582 minor_x,
1583 minor_y;
1584
1585 ClampUpAxes(dux,dvx,duy,dvy, &major_mag, &minor_mag,
1586 &major_x, &major_y, &minor_x, &minor_y);
anthonybdfddb02010-10-05 00:06:45 +00001587 major_x *= major_mag; major_y *= major_mag;
1588 minor_x *= minor_mag; minor_y *= minor_mag;
anthonyc7b82f22010-09-27 10:42:29 +00001589#if DEBUG_ELLIPSE
1590 fprintf(stderr, "major_x=%lf; major_y=%lf; minor_x=%lf; minor_y=%lf;\n",
1591 major_x, major_y, minor_x, minor_y);
1592#endif
1593 A = major_y*major_y+minor_y*minor_y;
1594 B = -2.0*(major_x*major_y+minor_x*minor_y);
1595 C = major_x*major_x+minor_x*minor_x;
nicolaseaa08622010-09-27 17:06:09 +00001596 F = major_mag*minor_mag;
anthonyc7b82f22010-09-27 10:42:29 +00001597 F *= F; /* square it */
1598 }
1599#else /* raw EWA */
cristy3ed852e2009-09-05 21:47:34 +00001600 A = dvx*dvx+dvy*dvy;
anthonyd638d312010-09-15 13:13:01 +00001601 B = -2.0*(dux*dvx+duy*dvy);
cristy3ed852e2009-09-05 21:47:34 +00001602 C = dux*dux+duy*duy;
anthonyc7b82f22010-09-27 10:42:29 +00001603 F = dux*dvy-duy*dvx;
anthony5708fc62010-09-14 13:52:50 +00001604 F *= F; /* square it */
anthonyc7b82f22010-09-27 10:42:29 +00001605#endif
anthonyd638d312010-09-15 13:13:01 +00001606
anthony490ab032010-09-20 00:02:08 +00001607#else /* HQ_EWA */
anthonyd638d312010-09-15 13:13:01 +00001608 /*
anthonyc7b82f22010-09-27 10:42:29 +00001609 This Paul Heckbert's "Higher Quality EWA" formula, from page 60 in his
1610 thesis, which adds a unit circle to the elliptical area so as to do both
1611 Reconstruction and Prefiltering of the pixels in the resampling. It also
1612 means it is always likely to have at least 4 pixels within the area of the
1613 ellipse, for weighted averaging. No scaling will result with F == 4.0 and
1614 a circle of radius 2.0, and F smaller than this means magnification is
1615 being used.
anthony490ab032010-09-20 00:02:08 +00001616
anthonyc7b82f22010-09-27 10:42:29 +00001617 NOTE: This method produces a very blury result at near unity scale while
anthonybdfddb02010-10-05 00:06:45 +00001618 producing perfect results for strong minitification and magnifications.
anthony490ab032010-09-20 00:02:08 +00001619
anthonyc7b82f22010-09-27 10:42:29 +00001620 However filter support is fixed to 2.0 (no good for Windowed Sinc filters)
cristy3ed852e2009-09-05 21:47:34 +00001621 */
1622 A = dvx*dvx+dvy*dvy+1;
anthonyd638d312010-09-15 13:13:01 +00001623 B = -2.0*(dux*dvx+duy*dvy);
cristy3ed852e2009-09-05 21:47:34 +00001624 C = dux*dux+duy*duy+1;
1625 F = A*C - B*B/4;
cristy3ed852e2009-09-05 21:47:34 +00001626#endif
1627
anthony490ab032010-09-20 00:02:08 +00001628#if DEBUG_ELLIPSE
cristy3ed852e2009-09-05 21:47:34 +00001629 fprintf(stderr, "A=%lf; B=%lf; C=%lf; F=%lf\n", A,B,C,F);
cristy3ed852e2009-09-05 21:47:34 +00001630
anthonyc7b82f22010-09-27 10:42:29 +00001631 /* Figure out the various information directly about the ellipse.
cristy3ed852e2009-09-05 21:47:34 +00001632 This information currently not needed at this time, but may be
1633 needed later for better limit determination.
anthonyd638d312010-09-15 13:13:01 +00001634
1635 It is also good to have as a record for future debugging
cristy3ed852e2009-09-05 21:47:34 +00001636 */
1637 { double alpha, beta, gamma, Major, Minor;
anthony490ab032010-09-20 00:02:08 +00001638 double Eccentricity, Ellipse_Area, Ellipse_Angle;
anthonyd638d312010-09-15 13:13:01 +00001639
cristy3ed852e2009-09-05 21:47:34 +00001640 alpha = A+C;
1641 beta = A-C;
1642 gamma = sqrt(beta*beta + B*B );
1643
1644 if ( alpha - gamma <= MagickEpsilon )
1645 Major = MagickHuge;
1646 else
1647 Major = sqrt(2*F/(alpha - gamma));
1648 Minor = sqrt(2*F/(alpha + gamma));
1649
anthony490ab032010-09-20 00:02:08 +00001650 fprintf(stderr, "# Major=%lf; Minor=%lf\n", Major, Minor );
cristy3ed852e2009-09-05 21:47:34 +00001651
1652 /* other information about ellipse include... */
1653 Eccentricity = Major/Minor;
1654 Ellipse_Area = MagickPI*Major*Minor;
nicolase2ecb242010-09-29 20:02:24 +00001655 Ellipse_Angle = atan2(B, A-C);
cristy3ed852e2009-09-05 21:47:34 +00001656
anthonyc7b82f22010-09-27 10:42:29 +00001657 fprintf(stderr, "# Angle=%lf Area=%lf\n",
nicolase2ecb242010-09-29 20:02:24 +00001658 RadiansToDegrees(Ellipse_Angle), Ellipse_Area);
cristy3ed852e2009-09-05 21:47:34 +00001659 }
1660#endif
1661
nicolas15c331b2010-09-29 19:05:00 +00001662 /* If one or both of the scaling vectors is impossibly large
1663 (producing a very large raw F value), we may as well not bother
1664 doing any form of resampling since resampled area is very large.
1665 In this case some alternative means of pixel sampling, such as
1666 the average of the whole image is needed to get a reasonable
1667 result. Calculate only as needed.
cristy3ed852e2009-09-05 21:47:34 +00001668 */
anthony490ab032010-09-20 00:02:08 +00001669 if ( (4*A*C - B*B) > MagickHuge ) {
cristy3ed852e2009-09-05 21:47:34 +00001670 resample_filter->limit_reached = MagickTrue;
1671 return;
1672 }
1673
nicolase2ecb242010-09-29 20:02:24 +00001674 /* Scale ellipse by the support (that is, multiply F by the square
1675 of the support).
1676 */
anthony490ab032010-09-20 00:02:08 +00001677 F *= resample_filter->support;
1678 F *= resample_filter->support;
cristy3ed852e2009-09-05 21:47:34 +00001679
nicolase2ecb242010-09-29 20:02:24 +00001680 /* Orthogonal bounds of the ellipse */
anthony490ab032010-09-20 00:02:08 +00001681 resample_filter->Ulimit = sqrt(4*C*F/(4*A*C-B*B));
1682 resample_filter->Vlimit = sqrt(4*A*F/(4*A*C-B*B));
1683
nicolase2ecb242010-09-29 20:02:24 +00001684 /* Horizontally aligned parallelogram fitted to Ellipse */
1685 resample_filter->Uwidth = sqrt(F/A); /* Half of the parallelogram width */
1686 resample_filter->slope = -B/(2*A); /* Reciprocal slope of the parallelogram */
anthony490ab032010-09-20 00:02:08 +00001687
1688#if DEBUG_ELLIPSE
anthony490ab032010-09-20 00:02:08 +00001689 fprintf(stderr, "Ulimit=%lf; Vlimit=%lf; UWidth=%lf; Slope=%lf;\n",
1690 resample_filter->Ulimit, resample_filter->Vlimit,
1691 resample_filter->Uwidth, resample_filter->slope );
1692#endif
cristy3ed852e2009-09-05 21:47:34 +00001693
nicolase2ecb242010-09-29 20:02:24 +00001694 /* Check the absolute area of the parallelogram involved.
1695 * This limit needs more work, as it is too slow for larger images
1696 * with tiled views of the horizon.
1697 */
cristy39f347a2010-09-20 00:29:31 +00001698 if ( (resample_filter->Uwidth * resample_filter->Vlimit)
1699 > (4.0*resample_filter->image_area)) {
cristy3ed852e2009-09-05 21:47:34 +00001700 resample_filter->limit_reached = MagickTrue;
1701 return;
1702 }
1703
anthony5708fc62010-09-14 13:52:50 +00001704 /* Scale ellipse formula to directly index the Filter Lookup Table */
cristy3ed852e2009-09-05 21:47:34 +00001705 { register double scale;
anthony490ab032010-09-20 00:02:08 +00001706 scale = (double)WLUT_WIDTH/F;
cristy3ed852e2009-09-05 21:47:34 +00001707 resample_filter->A = A*scale;
1708 resample_filter->B = B*scale;
1709 resample_filter->C = C*scale;
nicolase2ecb242010-09-29 20:02:24 +00001710 /* resample_filter->F = WLUT_WIDTH; -- hardcoded */
cristy3ed852e2009-09-05 21:47:34 +00001711 }
1712}
1713
1714/*
1715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1716% %
1717% %
1718% %
1719% S e t R e s a m p l e F i l t e r %
1720% %
1721% %
1722% %
1723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1724%
1725% SetResampleFilter() set the resampling filter lookup table based on a
1726% specific filter. Note that the filter is used as a radial filter not as a
1727% two pass othogonally aligned resampling filter.
1728%
1729% The default Filter, is Gaussian, which is the standard filter used by the
1730% original paper on the Elliptical Weighted Everage Algorithm. However other
1731% filters can also be used.
1732%
1733% The format of the SetResampleFilter method is:
1734%
1735% void SetResampleFilter(ResampleFilter *resample_filter,
1736% const FilterTypes filter,const double blur)
1737%
1738% A description of each parameter follows:
1739%
1740% o resample_filter: resampling resample_filterrmation structure
1741%
1742% o filter: the resize filter for elliptical weighting LUT
1743%
1744% o blur: filter blur factor (radial scaling) for elliptical weighting LUT
1745%
1746*/
1747MagickExport void SetResampleFilter(ResampleFilter *resample_filter,
1748 const FilterTypes filter,const double blur)
1749{
1750 register int
1751 Q;
1752
1753 double
1754 r_scale;
1755
1756 ResizeFilter
1757 *resize_filter;
1758
1759 assert(resample_filter != (ResampleFilter *) NULL);
1760 assert(resample_filter->signature == MagickSignature);
1761
anthony2e6ab682010-09-28 12:02:25 +00001762 resample_filter->do_interpolate = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001763 resample_filter->filter = filter;
1764
anthony490ab032010-09-20 00:02:08 +00001765 if ( filter == PointFilter )
anthonyb821aaf2010-09-27 13:21:08 +00001766 {
1767 resample_filter->do_interpolate = MagickTrue;
1768 return; /* EWA turned off - nothing more to do */
1769 }
cristy3ed852e2009-09-05 21:47:34 +00001770
anthony61b5ddd2010-10-05 02:33:31 +00001771 /* Set a default cylindrical filter of a 'low blur' Jinc windowed Jinc */
anthony490ab032010-09-20 00:02:08 +00001772 if ( filter == UndefinedFilter )
anthony853d6972010-10-08 06:01:31 +00001773 resample_filter->filter = RobidouxFilter;
anthony490ab032010-09-20 00:02:08 +00001774
1775 resize_filter = AcquireResizeFilter(resample_filter->image,
1776 resample_filter->filter,blur,MagickTrue,resample_filter->exception);
anthonybdfddb02010-10-05 00:06:45 +00001777 if (resize_filter == (ResizeFilter *) NULL)
anthony490ab032010-09-20 00:02:08 +00001778 {
cristy3ed852e2009-09-05 21:47:34 +00001779 (void) ThrowMagickException(resample_filter->exception,GetMagickModule(),
1780 ModuleError, "UnableToSetFilteringValue",
1781 "Fall back to default EWA gaussian filter");
anthony490ab032010-09-20 00:02:08 +00001782 resample_filter->filter = PointFilter;
cristy3ed852e2009-09-05 21:47:34 +00001783 }
anthony490ab032010-09-20 00:02:08 +00001784
anthony10b8bc82010-10-02 12:48:46 +00001785 /* Get the practical working support for the filter,
1786 * after any API call blur factors have been accoded for.
1787 */
anthonyc7b82f22010-09-27 10:42:29 +00001788#if EWA
anthony490ab032010-09-20 00:02:08 +00001789 resample_filter->support = GetResizeFilterSupport(resize_filter);
anthonyc7b82f22010-09-27 10:42:29 +00001790#else
1791 resample_filter->support = 2.0; /* fixed support size for HQ-EWA */
anthony490ab032010-09-20 00:02:08 +00001792#endif
1793
1794 /* Scale radius so the filter LUT covers the full support range */
1795 r_scale = resample_filter->support*sqrt(1.0/(double)WLUT_WIDTH);
1796
1797 /* Fill the LUT with a 1D resize filter function */
1798 for(Q=0; Q<WLUT_WIDTH; Q++)
1799 resample_filter->filter_lut[Q] = (double)
1800 GetResizeFilterWeight(resize_filter,sqrt((double)Q)*r_scale);
1801
1802 /* finished with the resize filter */
1803 resize_filter = DestroyResizeFilter(resize_filter);
1804
anthony3ebea1e2010-09-27 13:29:00 +00001805 /*
1806 Adjust the scaling of the default unit circle
1807 This assumes that any real scaling changes will always
1808 take place AFTER the filter method has been initialized.
1809 */
1810
1811 ScaleResampleFilter(resample_filter, 1.0, 0.0, 0.0, 1.0);
1812
anthony5708fc62010-09-14 13:52:50 +00001813#if 0
anthony490ab032010-09-20 00:02:08 +00001814 This is old code kept for reference only. It is very wrong.
anthonyd638d312010-09-15 13:13:01 +00001815 /*
1816 Create Normal Gaussian 2D Filter Weighted Lookup Table.
1817 A normal EWA guassual lookup would use exp(Q*ALPHA)
1818 where Q = distance squared from 0.0 (center) to 1.0 (edge)
1819 and ALPHA = -4.0*ln(2.0) ==> -2.77258872223978123767
1820 However the table is of length 1024, and equates to a radius of 2px
1821 thus needs to be scaled by ALPHA*4/1024 and any blur factor squared
anthony5708fc62010-09-14 13:52:50 +00001822
anthonyc7b82f22010-09-27 10:42:29 +00001823 The above came from some reference code provided by Fred Weinhaus
1824 and seems to have been a guess that was appropriate for its use
1825 in a 3d perspective landscape mapping program.
anthonyd638d312010-09-15 13:13:01 +00001826 */
anthonyd638d312010-09-15 13:13:01 +00001827 r_scale = -2.77258872223978123767/(WLUT_WIDTH*blur*blur);
1828 for(Q=0; Q<WLUT_WIDTH; Q++)
1829 resample_filter->filter_lut[Q] = exp((double)Q*r_scale);
1830 resample_filter->support = WLUT_WIDTH;
1831 break;
anthony5708fc62010-09-14 13:52:50 +00001832#endif
anthony490ab032010-09-20 00:02:08 +00001833
anthonye06e4c12010-09-15 04:03:52 +00001834#if defined(MAGICKCORE_OPENMP_SUPPORT)
anthony72949792010-10-08 04:44:56 +00001835 #pragma omp single
1836 {
anthonye06e4c12010-09-15 04:03:52 +00001837#endif
1838 if (GetImageArtifact(resample_filter->image,"resample:verbose")
1839 != (const char *) NULL)
1840 {
1841 /* Debug output of the filter weighting LUT
1842 Gnuplot the LUT with hoizontal adjusted to 'r' using...
1843 plot [0:2][-.2:1] "lut.dat" using (sqrt($0/1024)*2):1 with lines
1844 The filter values is normalized for comparision
1845 */
anthonyd638d312010-09-15 13:13:01 +00001846 printf("#\n");
anthonye06e4c12010-09-15 04:03:52 +00001847 printf("# Resampling Filter LUT (%d values)\n", WLUT_WIDTH);
1848 printf("#\n");
anthonyd638d312010-09-15 13:13:01 +00001849 printf("# Note: values in table are using a squared radius lookup.\n");
1850 printf("# And the whole table represents the filters support.\n");
anthony61b5ddd2010-10-05 02:33:31 +00001851 printf("\n"); /* generates a 'break' in gnuplot if multiple outputs */
anthonye06e4c12010-09-15 04:03:52 +00001852 for(Q=0; Q<WLUT_WIDTH; Q++)
anthonyd638d312010-09-15 13:13:01 +00001853 printf("%8.*g %.*g\n",
1854 GetMagickPrecision(),sqrt((double)Q)*r_scale,
1855 GetMagickPrecision(),resample_filter->filter_lut[Q] );
anthonye06e4c12010-09-15 04:03:52 +00001856 }
anthony72949792010-10-08 04:44:56 +00001857 /* output the above once only for each image, and each setting */
1858 (void) DeleteImageArtifact(resample_filter->image,"resample:verbose");
1859#if defined(MAGICKCORE_OPENMP_SUPPORT)
1860 }
1861#endif
cristy3ed852e2009-09-05 21:47:34 +00001862 return;
1863}
1864
1865/*
1866%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1867% %
1868% %
1869% %
1870% 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 %
1871% %
1872% %
1873% %
1874%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1875%
1876% SetResampleFilterInterpolateMethod() changes the interpolation method
1877% associated with the specified resample filter.
1878%
1879% The format of the SetResampleFilterInterpolateMethod method is:
1880%
1881% MagickBooleanType SetResampleFilterInterpolateMethod(
1882% ResampleFilter *resample_filter,const InterpolateMethod method)
1883%
1884% A description of each parameter follows:
1885%
1886% o resample_filter: the resample filter.
1887%
1888% o method: the interpolation method.
1889%
1890*/
1891MagickExport MagickBooleanType SetResampleFilterInterpolateMethod(
1892 ResampleFilter *resample_filter,const InterpolatePixelMethod method)
1893{
1894 assert(resample_filter != (ResampleFilter *) NULL);
1895 assert(resample_filter->signature == MagickSignature);
1896 assert(resample_filter->image != (Image *) NULL);
anthonyd638d312010-09-15 13:13:01 +00001897
cristy3ed852e2009-09-05 21:47:34 +00001898 if (resample_filter->debug != MagickFalse)
1899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1900 resample_filter->image->filename);
anthonyd638d312010-09-15 13:13:01 +00001901
cristy3ed852e2009-09-05 21:47:34 +00001902 resample_filter->interpolate=method;
anthonyd638d312010-09-15 13:13:01 +00001903
cristy3ed852e2009-09-05 21:47:34 +00001904 return(MagickTrue);
1905}
1906
1907/*
1908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1909% %
1910% %
1911% %
1912% 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 %
1913% %
1914% %
1915% %
1916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1917%
1918% SetResampleFilterVirtualPixelMethod() changes the virtual pixel method
1919% associated with the specified resample filter.
1920%
1921% The format of the SetResampleFilterVirtualPixelMethod method is:
1922%
1923% MagickBooleanType SetResampleFilterVirtualPixelMethod(
1924% ResampleFilter *resample_filter,const VirtualPixelMethod method)
1925%
1926% A description of each parameter follows:
1927%
1928% o resample_filter: the resample filter.
1929%
1930% o method: the virtual pixel method.
1931%
1932*/
1933MagickExport MagickBooleanType SetResampleFilterVirtualPixelMethod(
1934 ResampleFilter *resample_filter,const VirtualPixelMethod method)
1935{
1936 assert(resample_filter != (ResampleFilter *) NULL);
1937 assert(resample_filter->signature == MagickSignature);
1938 assert(resample_filter->image != (Image *) NULL);
1939 if (resample_filter->debug != MagickFalse)
1940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1941 resample_filter->image->filename);
1942 resample_filter->virtual_pixel=method;
cristy2d5e44d2010-03-12 01:56:29 +00001943 if (method != UndefinedVirtualPixelMethod)
1944 (void) SetCacheViewVirtualPixelMethod(resample_filter->view,method);
cristy3ed852e2009-09-05 21:47:34 +00001945 return(MagickTrue);
1946}