/plugins/ImageMagick-6.3.2/magick/image.c
C | 3762 lines | 2310 code | 147 blank | 1305 comment | 829 complexity | 6250dc824598c85f03ef84d4deec9003 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % IIIII M M AAA GGGG EEEEE %
- % I MM MM A A G E %
- % I M M M AAAAA G GG EEE %
- % I M M A A G G E %
- % IIIII M M A A GGGG EEEEE %
- % %
- % %
- % ImageMagick Image Methods %
- % %
- % Software Design %
- % John Cristy %
- % July 1992 %
- % %
- % %
- % Copyright 1999-2006 ImageMagick Studio LLC, a non-profit organization %
- % dedicated to making software imaging solutions freely available. %
- % %
- % You may not use this file except in compliance with the License. You may %
- % obtain a copy of the License at %
- % %
- % http://www.imagemagick.org/script/license.php %
- % %
- % Unless required by applicable law or agreed to in writing, software %
- % distributed under the License is distributed on an "AS IS" BASIS, %
- % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
- % See the License for the specific language governing permissions and %
- % limitations under the License. %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- %
- */
- /*
- Include declarations.
- */
- #include "magick/studio.h"
- #include "magick/animate.h"
- #include "magick/blob.h"
- #include "magick/blob-private.h"
- #include "magick/cache.h"
- #include "magick/cache-private.h"
- #include "magick/cache-view.h"
- #include "magick/client.h"
- #include "magick/color.h"
- #include "magick/color-private.h"
- #include "magick/colorspace.h"
- #include "magick/colorspace-private.h"
- #include "magick/composite.h"
- #include "magick/composite-private.h"
- #include "magick/compress.h"
- #include "magick/constitute.h"
- #include "magick/deprecate.h"
- #include "magick/display.h"
- #include "magick/draw.h"
- #include "magick/enhance.h"
- #include "magick/exception.h"
- #include "magick/exception-private.h"
- #include "magick/gem.h"
- #include "magick/geometry.h"
- #include "magick/list.h"
- #include "magick/image-private.h"
- #include "magick/magic.h"
- #include "magick/magick.h"
- #include "magick/memory_.h"
- #include "magick/module.h"
- #include "magick/monitor.h"
- #include "magick/option.h"
- #include "magick/paint.h"
- #include "magick/pixel-private.h"
- #include "magick/profile.h"
- #include "magick/property.h"
- #include "magick/quantize.h"
- #include "magick/random_.h"
- #include "magick/segment.h"
- #include "magick/semaphore.h"
- #include "magick/signature.h"
- #include "magick/string_.h"
- #include "magick/threshold.h"
- #include "magick/timer.h"
- #include "magick/utility.h"
- #include "magick/version.h"
- #include "magick/xwindow-private.h"
- /*
- Constant declaration.
- */
- const char
- *BackgroundColor = "#ffffff", /* white */
- *BorderColor = "#dfdfdf", /* gray */
- *DefaultTileFrame = "15x15+3+3",
- *DefaultTileGeometry = "120x120+4+3>",
- *DefaultTileLabel = "%f\n%wx%h\n%b",
- *ForegroundColor = "#000", /* black */
- *LoadImageTag = "Load/Image",
- *LoadImagesTag = "Load/Images",
- *MatteColor = "#bdbdbd", /* gray */
- *PSDensityGeometry = "72.0x72.0",
- *PSPageGeometry = "612x792",
- *SaveImageTag = "Save/Image",
- *SaveImagesTag = "Save/Images",
- *TransparentColor = "#00000000"; /* transparent black */
- const double
- DefaultResolution = 72.0;
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % A c q u i r e I m a g e I n f o %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % AcquireImageInfo() allocates the ImageInfo structure.
- %
- % The format of the AcquireImageInfo method is:
- %
- % ImageInfo *AcquireImageInfo(void)
- %
- */
- MagickExport ImageInfo *AcquireImageInfo(void)
- {
- ImageInfo
- *image_info;
- image_info=(ImageInfo *) AcquireMagickMemory(sizeof(*image_info));
- if (image_info == (ImageInfo *) NULL)
- ThrowMagickFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
- image_info->filename);
- GetImageInfo(image_info);
- return(image_info);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % A l l o c a t e I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % AllocateImage() returns a pointer to an image structure initialized to
- % default values.
- %
- % The format of the AllocateImage method is:
- %
- % Image *AllocateImage(const ImageInfo *image_info)
- %
- % A description of each parameter follows:
- %
- % o image_info: Many of the image default values are set from this
- % structure. For example, filename, compression, depth, background color,
- % and others.
- %
- */
- MagickExport Image *AllocateImage(const ImageInfo *image_info)
- {
- Image
- *allocate_image;
- MagickStatusType
- flags;
- /*
- Allocate image structure.
- */
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
- allocate_image=(Image *) AcquireMagickMemory(sizeof(*allocate_image));
- if (allocate_image == (Image *) NULL)
- ThrowMagickFatalException(ResourceLimitFatalError,"MemoryAllocationFailed",
- image_info->filename);
- (void) ResetMagickMemory(allocate_image,0,sizeof(*allocate_image));
- /*
- Initialize Image structure.
- */
- (void) CopyMagickString(allocate_image->magick,"MIFF",MaxTextExtent);
- allocate_image->storage_class=DirectClass;
- allocate_image->depth=QuantumDepth;
- allocate_image->colorspace=RGBColorspace;
- allocate_image->interlace=NoInterlace;
- allocate_image->ticks_per_second=UndefinedTicksPerSecond;
- allocate_image->compression=NoCompression;
- allocate_image->compose=OverCompositeOp;
- allocate_image->blur=1.0;
- GetExceptionInfo(&allocate_image->exception);
- (void) QueryColorDatabase(BackgroundColor,&allocate_image->background_color,
- &allocate_image->exception);
- (void) QueryColorDatabase(BorderColor,&allocate_image->border_color,
- &allocate_image->exception);
- (void) QueryColorDatabase(MatteColor,&allocate_image->matte_color,
- &allocate_image->exception);
- (void) QueryColorDatabase(TransparentColor,&allocate_image->transparent_color,
- &allocate_image->exception);
- allocate_image->x_resolution=DefaultResolution;
- allocate_image->y_resolution=DefaultResolution;
- allocate_image->units=PixelsPerInchResolution;
- GetTimerInfo(&allocate_image->timer);
- (void) GetCacheInfo(&allocate_image->cache);
- allocate_image->blob=CloneBlobInfo((BlobInfo *) NULL);
- allocate_image->debug=IsEventLogging();
- allocate_image->reference_count=1;
- allocate_image->signature=MagickSignature;
- if (image_info == (ImageInfo *) NULL)
- return(allocate_image);
- /*
- Transfer image info.
- */
- SetBlobExempt(allocate_image,image_info->file != (FILE *) NULL ?
- MagickTrue : MagickFalse);
- (void) CopyMagickString(allocate_image->filename,image_info->filename,
- MaxTextExtent);
- (void) CopyMagickString(allocate_image->magick_filename,image_info->filename,
- MaxTextExtent);
- (void) CopyMagickString(allocate_image->magick,image_info->magick,
- MaxTextExtent);
- if (image_info->size != (char *) NULL)
- {
- (void) ParseAbsoluteGeometry(image_info->size,
- &allocate_image->extract_info);
- allocate_image->columns=allocate_image->extract_info.width;
- allocate_image->rows=allocate_image->extract_info.height;
- allocate_image->offset=allocate_image->extract_info.x;
- allocate_image->extract_info.x=0;
- allocate_image->extract_info.y=0;
- }
- if (image_info->extract != (char *) NULL)
- {
- RectangleInfo
- geometry;
- flags=ParseAbsoluteGeometry(image_info->extract,&geometry);
- if (((flags & XValue) != 0) || ((flags & YValue) != 0))
- {
- allocate_image->extract_info=geometry;
- Swap(allocate_image->columns,allocate_image->extract_info.width);
- Swap(allocate_image->rows,allocate_image->extract_info.height);
- }
- }
- if (image_info->colorspace != UndefinedColorspace)
- allocate_image->colorspace=image_info->colorspace;
- allocate_image->compression=image_info->compression;
- allocate_image->quality=image_info->quality;
- allocate_image->endian=image_info->endian;
- allocate_image->interlace=image_info->interlace;
- allocate_image->units=image_info->units;
- if (image_info->density != (char *) NULL)
- {
- GeometryInfo
- geometry_info;
- flags=ParseGeometry(image_info->density,&geometry_info);
- allocate_image->x_resolution=geometry_info.rho;
- allocate_image->y_resolution=geometry_info.sigma;
- if ((flags & SigmaValue) == 0)
- allocate_image->y_resolution=allocate_image->x_resolution;
- }
- if (image_info->page != (char *) NULL)
- {
- char
- *geometry;
- allocate_image->page=allocate_image->extract_info;
- geometry=GetPageGeometry(image_info->page);
- (void) ParseAbsoluteGeometry(geometry,&allocate_image->page);
- geometry=(char *) RelinquishMagickMemory(geometry);
- }
- if (image_info->depth != 0)
- allocate_image->depth=image_info->depth;
- allocate_image->background_color=image_info->background_color;
- allocate_image->border_color=image_info->border_color;
- allocate_image->matte_color=image_info->matte_color;
- allocate_image->transparent_color=image_info->transparent_color;
- allocate_image->progress_monitor=image_info->progress_monitor;
- allocate_image->client_data=image_info->client_data;
- if (image_info->cache != (void *) NULL)
- CloneCacheMethods(allocate_image->cache,image_info->cache);
- (void) SetImageVirtualPixelMethod(allocate_image,
- image_info->virtual_pixel_method);
- (void) SyncImageOptions(image_info,allocate_image);
- return(allocate_image);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % A l l o c a t e I m a g e C o l o r m a p %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % AllocateImageColormap() allocates an image colormap and initializes
- % it to a linear gray colorspace. If the image already has a colormap,
- % it is replaced. AllocateImageColormap() returns MagickTrue if successful,
- % otherwise MagickFalse if there is not enough memory.
- %
- % The format of the AllocateImageColormap method is:
- %
- % MagickBooleanType AllocateImageColormap(Image *image,
- % const unsigned long colors)
- %
- % A description of each parameter follows:
- %
- % o image: The image.
- %
- % o colors: The number of colors in the image colormap.
- %
- %
- */
- static inline unsigned long MagickMax(const unsigned long x,
- const unsigned long y)
- {
- if (x > y)
- return(x);
- return(y);
- }
- static inline unsigned long MagickMin(const unsigned long x,
- const unsigned long y)
- {
- if (x < y)
- return(x);
- return(y);
- }
- MagickExport MagickBooleanType AllocateImageColormap(Image *image,
- const unsigned long colors)
- {
- register long
- i;
- size_t
- length;
- unsigned long
- pixel;
- /*
- Allocate image colormap.
- */
- assert(image != (Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- image->colors=MagickMin(colors,MaxColormapSize);
- length=(size_t) colors*sizeof(PixelPacket);
- if (image->colormap == (PixelPacket *) NULL)
- image->colormap=(PixelPacket *) AcquireMagickMemory(length);
- else
- image->colormap=(PixelPacket *) ResizeMagickMemory(image->colormap,length);
- if (image->colormap == (PixelPacket *) NULL)
- return(MagickFalse);
- for (i=0; i < (long) image->colors; i++)
- {
- pixel=(unsigned long) (i*(QuantumRange/MagickMax(colors-1,1)));
- image->colormap[i].red=(Quantum) pixel;
- image->colormap[i].green=(Quantum) pixel;
- image->colormap[i].blue=(Quantum) pixel;
- image->colormap[i].opacity=OpaqueOpacity;
- }
- return(SetImageStorageClass(image,PseudoClass));
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % A l l o c a t e N e x t I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % AllocateNextImage() initializes the next image in a sequence to
- % default values. The next member of image points to the newly allocated
- % image. If there is a memory shortage, next is assigned NULL.
- %
- % The format of the AllocateNextImage method is:
- %
- % void AllocateNextImage(const ImageInfo *image_info,Image *image)
- %
- % A description of each parameter follows:
- %
- % o image_info: Many of the image default values are set from this
- % structure. For example, filename, compression, depth, background color,
- % and others.
- %
- % o image: The image.
- %
- %
- */
- MagickExport void AllocateNextImage(const ImageInfo *image_info,Image *image)
- {
- /*
- Allocate image structure.
- */
- assert(image != (Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- image->next=AllocateImage(image_info);
- if (GetNextImageInList(image) == (Image *) NULL)
- return;
- (void) CopyMagickString(GetNextImageInList(image)->filename,image->filename,
- MaxTextExtent);
- if (image_info != (ImageInfo *) NULL)
- (void) CopyMagickString(GetNextImageInList(image)->filename,
- image_info->filename,MaxTextExtent);
- DestroyBlob(GetNextImageInList(image));
- image->next->blob=ReferenceBlob(image->blob);
- image->next->endian=image->endian;
- image->next->scene=image->scene+1;
- image->next->previous=image;
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % A p p e n d I m a g e s %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % AppendImages() takes all images from the current image pointer to the end
- % of the image list and and appends them to each other top-to-bottom if the
- % stack parameter is true, otherwise left-to-right.
- %
- % The format of the AppendImage method is:
- %
- % Image *AppendImages(const Image *image,const MagickBooleanType stack,
- % ExceptionInfo *exception)
- %
- % A description of each parameter follows:
- %
- % o image: The image sequence.
- %
- % o stack: A value other than 0 stacks the images top-to-bottom.
- %
- % o exception: Return any errors or warnings in this structure.
- %
- */
- MagickExport Image *AppendImages(const Image *image,
- const MagickBooleanType stack,ExceptionInfo *exception)
- {
- #define AppendImageTag "Append/Image"
- Image
- *append_image;
- long
- n,
- y;
- MagickBooleanType
- matte,
- status;
- register IndexPacket
- *append_indexes,
- *indexes;
- register const Image
- *next;
- register const PixelPacket
- *p;
- register long
- i,
- x;
- register PixelPacket
- *q;
- unsigned long
- height,
- number_images,
- width;
- /*
- Ensure the image have the same column width.
- */
- assert(image != (Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(exception != (ExceptionInfo *) NULL);
- assert(exception->signature == MagickSignature);
- matte=image->matte;
- number_images=1;
- width=image->columns;
- height=image->rows;
- next=GetNextImageInList(image);
- for ( ; next != (Image *) NULL; next=GetNextImageInList(next))
- {
- if (next->matte != MagickFalse)
- matte=MagickTrue;
- number_images++;
- if (stack != MagickFalse)
- {
- if (next->columns > width)
- width=next->columns;
- height+=next->rows;
- continue;
- }
- width+=next->columns;
- if (next->rows > height)
- height=next->rows;
- }
- /*
- Initialize append next attributes.
- */
- append_image=CloneImage(image,width,height,MagickTrue,exception);
- if (append_image == (Image *) NULL)
- return((Image *) NULL);
- if (SetImageStorageClass(append_image,DirectClass) == MagickFalse)
- {
- InheritException(exception,&append_image->exception);
- append_image=DestroyImage(append_image);
- return((Image *) NULL);
- }
- append_image->matte=matte;
- (void) SetImageBackgroundColor(append_image);
- if (stack != MagickFalse)
- {
- /*
- Stack top-to-bottom.
- */
- i=0;
- for (n=0; n < (long) number_images; n++)
- {
- for (y=0; y < (long) image->rows; y++)
- {
- p=AcquireImagePixels(image,0,y,image->columns,1,exception);
- q=SetImagePixels(append_image,0,i++,append_image->columns,1);
- if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
- break;
- indexes=GetIndexes(image);
- append_indexes=GetIndexes(append_image);
- for (x=0; x < (long) image->columns; x++)
- {
- q->red=p->red;
- q->green=p->green;
- q->blue=p->blue;
- if (append_image->matte != MagickFalse)
- q->opacity=p->opacity;
- if (append_image->colorspace == CMYKColorspace)
- append_indexes[x]=indexes[x];
- p++;
- q++;
- }
- if (SyncImagePixels(append_image) == MagickFalse)
- break;
- }
- if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
- (QuantumTick(n,number_images) != MagickFalse))
- {
- status=image->progress_monitor(AppendImageTag,n,number_images,
- image->client_data);
- if (status == MagickFalse)
- break;
- }
- image=GetNextImageInList(image);
- }
- return(append_image);
- }
- /*
- Stack left-to-right.
- */
- i=0;
- for (n=0; n < (long) number_images; n++)
- {
- for (y=0; y < (long) image->rows; y++)
- {
- p=AcquireImagePixels(image,0,y,image->columns,1,exception);
- q=SetImagePixels(append_image,i,y,image->columns,1);
- if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
- break;
- indexes=GetIndexes(image);
- append_indexes=GetIndexes(append_image);
- for (x=0; x < (long) image->columns; x++)
- {
- q->red=p->red;
- q->green=p->green;
- q->blue=p->blue;
- if (append_image->matte != MagickFalse)
- q->opacity=p->opacity;
- if (append_image->colorspace == CMYKColorspace)
- append_indexes[x]=indexes[x];
- p++;
- q++;
- }
- if (SyncImagePixels(append_image) == MagickFalse)
- break;
- }
- if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
- (QuantumTick(n,number_images) != MagickFalse))
- {
- status=image->progress_monitor(AppendImageTag,n,number_images,
- image->client_data);
- if (status == MagickFalse)
- break;
- }
- i+=image->columns;
- image=GetNextImageInList(image);
- }
- return(append_image);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % A v e r a g e I m a g e s %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % The Average() method takes a set of images and averages them together.
- % Each image in the set must have the same width and height. Average()
- % returns a single image with each corresponding pixel component of
- % each image averaged. On failure, a NULL image is returned and
- % exception describes the reason for the failure.
- %
- % The format of the AverageImage method is:
- %
- % Image *AverageImages(Image *image,ExceptionInfo *exception)
- %
- % A description of each parameter follows:
- %
- % o image: The image sequence.
- %
- % o exception: Return any errors or warnings in this structure.
- %
- %
- */
- MagickExport Image *AverageImages(const Image *image,ExceptionInfo *exception)
- {
- #define AverageImageTag "Average/Image"
- Image
- *average_image;
- long
- n,
- y;
- MagickBooleanType
- status;
- MagickPixelPacket
- *pixels;
- MagickSizeType
- length;
- register const Image
- *next;
- register const PixelPacket
- *p;
- register long
- i,
- x;
- register PixelPacket
- *q;
- unsigned long
- number_images;
- /*
- Ensure the image are the same size.
- */
- assert(image != (Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(exception != (ExceptionInfo *) NULL);
- assert(exception->signature == MagickSignature);
- for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
- {
- if ((next->columns != image->columns) || (next->rows != image->rows))
- ThrowImageException(OptionError,"ImageWidthsOrHeightsDiffer");
- }
- /*
- Allocate sum accumulation buffer.
- */
- length=(MagickSizeType) image->columns*image->rows*sizeof(*pixels);
- if (length != (MagickSizeType) ((size_t) length))
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- pixels=(MagickPixelPacket *) AcquireMagickMemory((size_t) length);
- if (pixels == (MagickPixelPacket *) NULL)
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- (void) ResetMagickMemory(pixels,0,(size_t) length);
- /*
- Initialize average next attributes.
- */
- average_image=CloneImage(image,0,0,MagickTrue,exception);
- if (average_image == (Image *) NULL)
- {
- pixels=(MagickPixelPacket *) RelinquishMagickMemory(pixels);
- return((Image *) NULL);
- }
- if (SetImageStorageClass(average_image,DirectClass) == MagickFalse)
- {
- InheritException(exception,&average_image->exception);
- average_image=DestroyImage(average_image);
- return((Image *) NULL);
- }
- /*
- Compute sum over each pixel color component.
- */
- number_images=GetImageListLength(image);
- for (n=0; n < (long) number_images; n++)
- {
- i=0;
- for (y=0; y < (long) image->rows; y++)
- {
- p=AcquireImagePixels(image,0,y,image->columns,1,exception);
- if (p == (const PixelPacket *) NULL)
- break;
- for (x=0; x < (long) image->columns; x++)
- {
- pixels[i].red+=p->red;
- pixels[i].green+=p->green;
- pixels[i].blue+=p->blue;
- pixels[i].opacity+=p->opacity;
- p++;
- i++;
- }
- }
- if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
- (QuantumTick(n,number_images) != MagickFalse))
- {
- status=image->progress_monitor(AverageImageTag,n,number_images,
- image->client_data);
- if (status == MagickFalse)
- break;
- }
- image=GetNextImageInList(image);
- }
- if (n < (long) number_images)
- return(average_image);
- /*
- Average next pixels.
- */
- i=0;
- for (y=0; y < (long) average_image->rows; y++)
- {
- q=GetImagePixels(average_image,0,y,average_image->columns,1);
- if (q == (PixelPacket *) NULL)
- break;
- for (x=0; x < (long) average_image->columns; x++)
- {
- q->red=(Quantum) (pixels[i].red/number_images+0.5);
- q->green=(Quantum) (pixels[i].green/number_images+0.5);
- q->blue=(Quantum) (pixels[i].blue/number_images+0.5);
- q->opacity=(Quantum) (pixels[i].opacity/number_images+0.5);
- q++;
- i++;
- }
- if (SyncImagePixels(average_image) == MagickFalse)
- break;
- }
- pixels=(MagickPixelPacket *) RelinquishMagickMemory(pixels);
- return(average_image);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C a t c h I m a g e E x c e p t i o n %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % CatchImageException() returns if no exceptions are found in the image
- % sequence, otherwise it determines the most severe exception and reports
- % it as a warning or error depending on the severity.
- %
- % The format of the CatchImageException method is:
- %
- % ExceptionType CatchImageException(Image *image)
- %
- % A description of each parameter follows:
- %
- % o image: An image sequence.
- %
- %
- */
- MagickExport ExceptionType CatchImageException(Image *image)
- {
- ExceptionInfo
- exception;
- assert(image != (const Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- GetExceptionInfo(&exception);
- GetImageException(image,&exception);
- CatchException(&exception);
- (void) DestroyExceptionInfo(&exception);
- return(exception.severity);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C l i p P a t h I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % ClipPathImage() sets the image clip mask based any clipping path information
- % if it exists.
- %
- % The format of the ClipImage method is:
- %
- % MagickBooleanType ClipPathImage(Image *image,const char *pathname,
- % const MagickBooleanType inside)
- %
- % A description of each parameter follows:
- %
- % o image: The image.
- %
- % o pathname: name of clipping path resource. If name is preceded by #, use
- % clipping path numbered by name.
- %
- % o inside: if non-zero, later operations take effect inside clipping path.
- % Otherwise later operations take effect outside clipping path.
- %
- */
- MagickExport MagickBooleanType ClipImage(Image *image)
- {
- return(ClipPathImage(image,"#1",MagickTrue));
- }
- MagickExport MagickBooleanType ClipPathImage(Image *image,const char *pathname,
- const MagickBooleanType inside)
- {
- #define ClipPathImageTag "ClipPath/Image"
- char
- *property;
- const char
- *value;
- Image
- *clip_mask;
- ImageInfo
- *image_info;
- assert(image != (const Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(pathname != NULL);
- property=AcquireString(pathname);
- (void) FormatMagickString(property,MaxTextExtent,"8BIM:1999,2998:%s",
- pathname);
- value=GetImageProperty(image,property);
- property=(char *) RelinquishMagickMemory(property);
- if (value == (const char *) NULL)
- {
- ThrowFileException(&image->exception,OptionError,"NoClipPathDefined",
- image->filename);
- return(MagickFalse);
- }
- image_info=AcquireImageInfo();
- clip_mask=BlobToImage(image_info,value,strlen(value),&image->exception);
- image_info=DestroyImageInfo(image_info);
- if (clip_mask == (Image *) NULL)
- return(MagickFalse);
- if (clip_mask->storage_class == PseudoClass)
- {
- (void) SyncImage(clip_mask);
- if (SetImageStorageClass(clip_mask,DirectClass) == MagickFalse)
- return(MagickFalse);
- }
- if (inside == MagickFalse)
- (void) NegateImage(clip_mask,MagickFalse);
- (void) FormatMagickString(clip_mask->magick_filename,MaxTextExtent,
- "8BIM:1999,2998:%s\nPS",pathname);
- (void) SetImageClipMask(image,clip_mask);
- clip_mask=DestroyImage(clip_mask);
- return(MagickTrue);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C l o n e I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % CloneImage() copies an image and returns the copy as a new image object.
- % If the specified columns and rows is 0, an exact copy of the image is
- % returned, otherwise the pixel data is undefined and must be initialized
- % with the SetImagePixels() and SyncImagePixels() methods. On failure,
- % a NULL image is returned and exception describes the reason for the
- % failure.
- %
- % The format of the CloneImage method is:
- %
- % Image *CloneImage(const Image *image,const unsigned long columns,
- % const unsigned long rows,const MagickBooleanType orphan,
- % ExceptionInfo *exception)
- %
- % A description of each parameter follows:
- %
- % o image: The image.
- %
- % o columns: The number of columns in the cloned image.
- %
- % o rows: The number of rows in the cloned image.
- %
- % o orphan: With a value other than 0, the cloned image is an orphan. An
- % orphan is a stand-alone image that is not assocated with an image list.
- % In effect, the next and previous members of the cloned image is set to
- % NULL.
- %
- % o exception: Return any errors or warnings in this structure.
- %
- */
- MagickExport Image *CloneImage(const Image *image,const unsigned long columns,
- const unsigned long rows,const MagickBooleanType orphan,
- ExceptionInfo *exception)
- {
- Image
- *clone_image;
- MagickRealType
- scale;
- size_t
- length;
- /*
- Clone the image.
- */
- assert(image != (const Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(exception != (ExceptionInfo *) NULL);
- assert(exception->signature == MagickSignature);
- clone_image=(Image *) AcquireMagickMemory(sizeof(*clone_image));
- if (clone_image == (Image *) NULL)
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- (void) ResetMagickMemory(clone_image,0,sizeof(*clone_image));
- clone_image->signature=MagickSignature;
- clone_image->storage_class=image->storage_class;
- clone_image->colorspace=image->colorspace;
- clone_image->matte=image->matte;
- clone_image->columns=image->columns;
- clone_image->rows=image->rows;
- if (image->colormap != (PixelPacket *) NULL)
- {
- /*
- Allocate and copy the image colormap.
- */
- clone_image->colors=image->colors;
- length=(size_t) image->colors*sizeof(PixelPacket);
- clone_image->colormap=(PixelPacket *) AcquireMagickMemory(length);
- if (clone_image->colormap == (PixelPacket *) NULL)
- ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
- length=(size_t) image->colors*sizeof(PixelPacket);
- (void) CopyMagickMemory(clone_image->colormap,image->colormap,length);
- }
- (void) CloneImageProfiles(clone_image,image);
- (void) CloneImageProperties(clone_image,image);
- clone_image->semaphore=(SemaphoreInfo *) NULL;
- GetTimerInfo(&clone_image->timer);
- GetExceptionInfo(&clone_image->exception);
- InheritException(&clone_image->exception,&image->exception);
- if (image->ascii85 != (void *) NULL)
- Ascii85Initialize(clone_image);
- clone_image->magick_columns=image->magick_columns;
- clone_image->magick_rows=image->magick_rows;
- (void) CopyMagickString(clone_image->magick_filename,image->magick_filename,
- MaxTextExtent);
- (void) CopyMagickString(clone_image->magick,image->magick,MaxTextExtent);
- (void) CopyMagickString(clone_image->filename,image->filename,MaxTextExtent);
- clone_image->progress_monitor=image->progress_monitor;
- clone_image->client_data=image->client_data;
- clone_image->reference_count=1;
- clone_image->previous=NewImageList();
- clone_image->list=NewImageList();
- clone_image->next=NewImageList();
- clone_image->clip_mask=NewImageList();
- clone_image->mask=NewImageList();
- clone_image->blob=ReferenceBlob(image->blob);
- clone_image->debug=IsEventLogging();
- if (orphan == MagickFalse)
- {
- if (GetPreviousImageInList(image) != (Image *) NULL)
- clone_image->previous->next=clone_image;
- if (GetNextImageInList(image) != (Image *) NULL)
- clone_image->next->previous=clone_image;
- }
- if (((columns == 0) && (rows == 0)) ||
- ((columns == image->columns) && (rows == image->rows)))
- {
- if (image->montage != (char *) NULL)
- (void) CloneString(&clone_image->montage,image->montage);
- if (image->directory != (char *) NULL)
- (void) CloneString(&clone_image->directory,image->directory);
- if (image->clip_mask != (Image *) NULL)
- clone_image->clip_mask=CloneImage(image->clip_mask,0,0,MagickTrue,
- exception);
- if (image->mask != (Image *) NULL)
- clone_image->mask=CloneImage(image->mask,0,0,MagickTrue,exception);
- }
- (void) SetImagePixels((Image *) image,0,0,image->columns,1);
- clone_image->cache=ReferenceCache(image->cache);
- if (((columns == 0) && (rows == 0)) ||
- ((columns == image->columns) && (rows == image->rows)))
- return(clone_image);
- clone_image->columns=columns;
- clone_image->rows=rows;
- scale=(MagickRealType) clone_image->columns/(MagickRealType) image->columns;
- clone_image->page.width=(unsigned long) (scale*image->page.width+0.5);
- clone_image->page.x=(long) (scale*image->page.x+0.5);
- clone_image->origin.x=(long) (scale*image->origin.x+0.5);
- scale=(MagickRealType) clone_image->rows/(MagickRealType) image->rows;
- clone_image->page.height=(unsigned long) (scale*image->page.height+0.5);
- clone_image->page.y=(long) (image->page.y*scale+0.5);
- clone_image->origin.y=(long) (scale*image->origin.y+0.5);
- (void) SetImagePixels(clone_image,0,0,clone_image->columns,1);
- return(clone_image);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C l o n e I m a g e I n f o %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % CloneImageInfo() makes a copy of the given image info structure. If
- % NULL is specified, a new image info structure is created initialized to
- % default values.
- %
- % The format of the CloneImageInfo method is:
- %
- % ImageInfo *CloneImageInfo(const ImageInfo *image_info)
- %
- % A description of each parameter follows:
- %
- % o image_info: The image info.
- %
- %
- */
- MagickExport ImageInfo *CloneImageInfo(const ImageInfo *image_info)
- {
- ImageInfo
- *clone_info;
- clone_info=AcquireImageInfo();
- if (image_info == (ImageInfo *) NULL)
- return(clone_info);
- clone_info->compression=image_info->compression;
- clone_info->temporary=image_info->temporary;
- clone_info->adjoin=image_info->adjoin;
- clone_info->antialias=image_info->antialias;
- clone_info->scene=image_info->scene;
- clone_info->number_scenes=image_info->number_scenes;
- clone_info->depth=image_info->depth;
- if (image_info->size != (char *) NULL)
- (void) CloneString(&clone_info->size,image_info->size);
- if (image_info->extract != (char *) NULL)
- (void) CloneString(&clone_info->extract,image_info->extract);
- if (image_info->scenes != (char *) NULL)
- (void) CloneString(&clone_info->scenes,image_info->scenes);
- if (image_info->page != (char *) NULL)
- (void) CloneString(&clone_info->page,image_info->page);
- clone_info->interlace=image_info->interlace;
- clone_info->endian=image_info->endian;
- clone_info->units=image_info->units;
- clone_info->quality=image_info->quality;
- if (image_info->sampling_factor != (char *) NULL)
- (void) CloneString(&clone_info->sampling_factor,
- image_info->sampling_factor);
- if (image_info->server_name != (char *) NULL)
- (void) CloneString(&clone_info->server_name,image_info->server_name);
- if (image_info->font != (char *) NULL)
- (void) CloneString(&clone_info->font,image_info->font);
- if (image_info->texture != (char *) NULL)
- (void) CloneString(&clone_info->texture,image_info->texture);
- if (image_info->density != (char *) NULL)
- (void) CloneString(&clone_info->density,image_info->density);
- clone_info->pointsize=image_info->pointsize;
- clone_info->fuzz=image_info->fuzz;
- clone_info->pen=image_info->pen;
- clone_info->background_color=image_info->background_color;
- clone_info->border_color=image_info->border_color;
- clone_info->matte_color=image_info->matte_color;
- clone_info->transparent_color=image_info->transparent_color;
- clone_info->dither=image_info->dither;
- clone_info->monochrome=image_info->monochrome;
- clone_info->colors=image_info->colors;
- clone_info->colorspace=image_info->colorspace;
- clone_info->type=image_info->type;
- clone_info->orientation=image_info->orientation;
- clone_info->preview_type=image_info->preview_type;
- clone_info->group=image_info->group;
- clone_info->ping=image_info->ping;
- clone_info->verbose=image_info->verbose;
- if (image_info->view != (char *) NULL)
- (void) CloneString(&clone_info->view,image_info->view);
- if (image_info->authenticate != (char *) NULL)
- (void) CloneString(&clone_info->authenticate,image_info->authenticate);
- (void) CloneImageOptions(clone_info,image_info);
- clone_info->progress_monitor=image_info->progress_monitor;
- clone_info->client_data=image_info->client_data;
- clone_info->cache=image_info->cache;
- if (image_info->cache != (void *) NULL)
- clone_info->cache=ReferenceCache(image_info->cache);
- if (image_info->profile != (void *) NULL)
- clone_info->profile=(void *) CloneStringInfo((StringInfo *)
- image_info->profile);
- SetImageInfoFile(clone_info,image_info->file);
- SetImageInfoBlob(clone_info,image_info->blob,image_info->length);
- clone_info->stream=image_info->stream;
- clone_info->virtual_pixel_method=image_info->virtual_pixel_method;
- (void) CopyMagickString(clone_info->magick,image_info->magick,MaxTextExtent);
- (void) CopyMagickString(clone_info->unique,image_info->unique,MaxTextExtent);
- (void) CopyMagickString(clone_info->zero,image_info->zero,MaxTextExtent);
- (void) CopyMagickString(clone_info->filename,image_info->filename,
- MaxTextExtent);
- clone_info->subimage=image_info->scene;
- clone_info->subrange=image_info->number_scenes;
- clone_info->channel=image_info->channel;
- clone_info->debug=IsEventLogging();
- clone_info->signature=image_info->signature;
- return(clone_info);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % C o m b i n e I m a g e s %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % CombineImages() combines one or more images into a single image. The
- % grayscale value of the pixels of each image in the sequence is assigned in
- % order to the specified channels of the combined image. The typical
- % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
- %
- % The format of the CombineImages method is:
- %
- % Image *CombineImages(const Image *image,const ChannelType channel,
- % ExceptionInfo *exception)
- %
- % A description of each parameter follows:
- %
- % o image: The image.
- %
- % o exception: Return any errors or warnings in this structure.
- %
- */
- MagickExport Image *CombineImages(const Image *image,const ChannelType channel,
- ExceptionInfo *exception)
- {
- #define CombineImageTag "Combine/Image"
- Image
- *combine_image;
- long
- y;
- MagickBooleanType
- status;
- PixelPacket
- *pixels;
- register const Image
- *next;
- register const PixelPacket
- *p;
- register long
- x;
- register PixelPacket
- *q;
- /*
- Ensure the image are the same size.
- */
- assert(image != (const Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(exception != (ExceptionInfo *) NULL);
- assert(exception->signature == MagickSignature);
- for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
- {
- if ((next->columns != image->columns) || (next->rows != image->rows))
- ThrowImageException(OptionError,"ImagesAreNotTheSameSize");
- }
- combine_image=CloneImage(image,0,0,MagickTrue,exception);
- if (combine_image == (Image *) NULL)
- return((Image *) NULL);
- if (SetImageStorageClass(combine_image,DirectClass) == MagickFalse)
- {
- InheritException(exception,&combine_image->exception);
- combine_image=DestroyImage(combine_image);
- return((Image *) NULL);
- }
- if ((channel & OpacityChannel) != 0)
- combine_image->matte=MagickTrue;
- (void) SetImageBackgroundColor(combine_image);
- for (y=0; y < (long) combine_image->rows; y++)
- {
- pixels=GetImagePixels(combine_image,0,y,combine_image->columns,1);
- if (pixels == (PixelPacket *) NULL)
- break;
- next=image;
- if (((channel & RedChannel) != 0) && (next != (Image *) NULL))
- {
- p=AcquireImagePixels(next,0,y,next->columns,1,exception);
- if (p == (const PixelPacket *) NULL)
- break;
- q=pixels;
- for (x=0; x < (long) combine_image->columns; x++)
- {
- q->red=PixelIntensityToQuantum(p);
- p++;
- q++;
- }
- next=GetNextImageInList(next);
- }
- if (((channel & GreenChannel) != 0) && (next != (Image *) NULL))
- {
- p=AcquireImagePixels(next,0,y,next->columns,1,exception);
- if (p == (const PixelPacket *) NULL)
- break;
- q=pixels;
- for (x=0; x < (long) combine_image->columns; x++)
- {
- q->green=PixelIntensityToQuantum(p);
- p++;
- q++;
- }
- next=GetNextImageInList(next);
- }
- if (((channel & BlueChannel) != 0) && (next != (Image *) NULL))
- {
- p=AcquireImagePixels(next,0,y,next->columns,1,exception);
- if (p == (const PixelPacket *) NULL)
- break;
- q=pixels;
- for (x=0; x < (long) combine_image->columns; x++)
- {
- q->blue=PixelIntensityToQuantum(p);
- p++;
- q++;
- }
- next=GetNextImageInList(next);
- }
- if (((channel & OpacityChannel) != 0) && (next != (Image *) NULL))
- {
- p=AcquireImagePixels(next,0,y,next->columns,1,exception);
- if (p == (const PixelPacket *) NULL)
- break;
- q=pixels;
- for (x=0; x < (long) combine_image->columns; x++)
- {
- q->opacity=PixelIntensityToQuantum(p);
- p++;
- q++;
- }
- next=GetNextImageInList(next);
- }
- if (((channel & IndexChannel) != 0) &&
- (image->colorspace == CMYKColorspace) &&
- (next != (Image *) NULL))
- {
- IndexPacket
- *indexes;
- p=AcquireImagePixels(next,0,y,next->columns,1,exception);
- if (p == (const PixelPacket *) NULL)
- break;
- indexes=GetIndexes(combine_image);
- for (x=0; x < (long) combine_image->columns; x++)
- {
- indexes[x]=PixelIntensityToQuantum(p);
- p++;
- }
- next=GetNextImageInList(next);
- }
- if (SyncImagePixels(combine_image) == MagickFalse)
- break;
- if ((combine_image->progress_monitor != (MagickProgressMonitor) NULL) &&
- (QuantumTick(y,combine_image->rows) != MagickFalse))
- {
- status=image->progress_monitor(CombineImageTag,y,combine_image->rows,
- combine_image->client_data);
- if (status == MagickFalse)
- break;
- }
- }
- return(combine_image);
- }
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % C y c l e C o l o r m a p I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % CycleColormap() displaces an image's colormap by a given number of
- % positions. If you cycle the c…
Large files files are truncated, but you can click here to view the full file