/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % M a g i c k S e t L a s t I t e r a t o r % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % MagickSetLastIterator() sets the wand iterator to the last image. % % The format of the MagickSetLastIterator method is: % % void MagickSetLastIterator(MagickWand *wand) % % A description of each parameter follows: % % o wand: The magick wand. % */ WandExport void MagickSetLastIterator(MagickWand *wand) { assert(wand != (MagickWand *) NULL); assert(wand->signature == MagickSignature); if (wand->debug != MagickFalse) (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name); wand->iterator=MagickFalse; wand->images=GetLastImageInList(wand->images); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % M a g i c k S e t L a s t I t e r a t o r % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % MagickSetLastIterator() sets the wand iterator to the last image. % % The last image is actually the current image, and the next use of % MagickPreviousImage() will not change this allowing this function to be % used to iterate over the images in the reverse direction. In this sense it % is more like MagickResetIterator() than MagickSetFirstIterator(). % % Typically this function is used before MagickAddImage(), MagickReadImage() % functions to ensure new images are appended to the very end of wand's image % list. % % The format of the MagickSetLastIterator method is: % % void MagickSetLastIterator(MagickWand *wand) % % A description of each parameter follows: % % o wand: the magick wand. % */ WandExport void MagickSetLastIterator(MagickWand *wand) { assert(wand != (MagickWand *) NULL); assert(wand->signature == WandSignature); if (wand->debug != MagickFalse) (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name); wand->images=GetLastImageInList(wand->images); wand->insert_before=MagickFalse; /* Insert/add after current (last) image */ wand->image_pending=MagickTrue; /* PreviousImage will return last image */ }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % A p p e n d I m a g e T o L i s t % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % AppendImageToList() appends the second image list to the end of the first % list. The given image list pointer is left unchanged, unless it was empty. % % The format of the AppendImageToList method is: % % AppendImageToList(Image *images,const Image *image) % % A description of each parameter follows: % % o images: the image list to be appended to. % % o image: the appended image or image list. % */ MagickExport void AppendImageToList(Image **images,const Image *image) { register Image *p, *q; assert(images != (Image **) NULL); if (image == (Image *) NULL) return; assert(image->signature == MagickSignature); if (image->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); if ((*images) == (Image *) NULL) { *images=(Image *) image; return; } assert((*images)->signature == MagickSignature); p=GetLastImageInList(*images); q=GetFirstImageInList(image); p->next=q; q->previous=p; SyncImageList(*images); }
MagickExport Image *ChannelFxImage(const Image *image,const char *expression, ExceptionInfo *exception) { #define ChannelFxImageTag "ChannelFx/Image" ChannelFx channel_op; ChannelType channel_mask; char token[MaxTextExtent]; const char *p; const Image *source_image; double pixel; Image *destination_image; MagickBooleanType status; PixelChannel source_channel, destination_channel; ssize_t channels; 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); source_image=image; destination_image=CloneImage(source_image,0,0,MagickTrue,exception); if (destination_image == (Image *) NULL) return((Image *) NULL); if (expression == (const char *) NULL) return(destination_image); destination_channel=RedPixelChannel; channel_mask=UndefinedChannel; pixel=0.0; p=(char *) expression; GetMagickToken(p,&p,token); channel_op=ExtractChannelOp; for (channels=0; *token != '\0'; ) { ssize_t i; /* Interpret channel expression. */ switch (*token) { case ',': { GetMagickToken(p,&p,token); break; } case '|': { if (GetNextImageInList(source_image) != (Image *) NULL) source_image=GetNextImageInList(source_image); else source_image=GetFirstImageInList(source_image); GetMagickToken(p,&p,token); break; } case ';': { Image *canvas; SetPixelChannelMask(destination_image,channel_mask); if ((channel_op == ExtractChannelOp) && (channels == 1)) (void) SetImageColorspace(destination_image,GRAYColorspace,exception); status=SetImageStorageClass(destination_image,DirectClass,exception); if (status == MagickFalse) { destination_image=DestroyImageList(destination_image); return(destination_image); } canvas=CloneImage(source_image,0,0,MagickTrue,exception); if (canvas == (Image *) NULL) { destination_image=DestroyImageList(destination_image); return(destination_image); } AppendImageToList(&destination_image,canvas); destination_image=GetLastImageInList(destination_image); GetMagickToken(p,&p,token); channels=0; destination_channel=RedPixelChannel; channel_mask=UndefinedChannel; break; } default: break; } i=ParsePixelChannelOption(token); if (i < 0) { (void) ThrowMagickException(exception,GetMagickModule(),OptionError, "UnrecognizedChannelType","`%s'",token); destination_image=DestroyImageList(destination_image); return(destination_image); } source_channel=(PixelChannel) i; channel_op=ExtractChannelOp; GetMagickToken(p,&p,token); if (*token == '<') { channel_op=ExchangeChannelOp; GetMagickToken(p,&p,token); } if (*token == '=') { if (channel_op != ExchangeChannelOp) channel_op=AssignChannelOp; GetMagickToken(p,&p,token); } if (*token == '>') { if (channel_op != ExchangeChannelOp) channel_op=TransferChannelOp; GetMagickToken(p,&p,token); } switch (channel_op) { case AssignChannelOp: { pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0); GetMagickToken(p,&p,token); break; } case ExchangeChannelOp: case TransferChannelOp: { i=ParsePixelChannelOption(token); if (i < 0) { (void) ThrowMagickException(exception,GetMagickModule(),OptionError, "UnrecognizedChannelType","`%s'",token); destination_image=DestroyImageList(destination_image); return(destination_image); } destination_channel=(PixelChannel) i; switch (destination_channel) { case RedPixelChannel: case GreenPixelChannel: case BluePixelChannel: case BlackPixelChannel: case IndexPixelChannel: break; case AlphaPixelChannel: { destination_image->alpha_trait=BlendPixelTrait; break; } case ReadMaskPixelChannel: { destination_image->read_mask=MagickTrue; break; } case WriteMaskPixelChannel: { destination_image->write_mask=MagickTrue; break; } case MetaPixelChannel: default: { (void) SetPixelMetaChannels(destination_image,(size_t) (i- GetPixelChannels(destination_image)+1),exception); break; } } channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token)); if (((channels >= 1) || (destination_channel >= 1)) && (IsGrayColorspace(destination_image->colorspace) != MagickFalse)) (void) SetImageColorspace(destination_image,sRGBColorspace,exception); GetMagickToken(p,&p,token); break; } default: break; } status=ChannelImage(destination_image,destination_channel,channel_op, source_image,source_channel,ClampToQuantum(pixel),exception); if (status == MagickFalse) { destination_image=DestroyImageList(destination_image); break; } channels++; if (channel_op == ExchangeChannelOp) { status=ChannelImage(destination_image,source_channel,channel_op, source_image,destination_channel,ClampToQuantum(pixel),exception); if (status == MagickFalse) { destination_image=DestroyImageList(destination_image); break; } channels++; } switch (channel_op) { case ExtractChannelOp: { channel_mask=(ChannelType) (channel_mask | (1 << destination_channel)); destination_channel=(PixelChannel) (destination_channel+1); break; } default: break; } status=SetImageProgress(source_image,ChannelFxImageTag,p-expression, strlen(expression)); if (status == MagickFalse) break; } SetPixelChannelMask(destination_image,channel_mask); if ((channel_op == ExtractChannelOp) && (channels == 1)) (void) SetImageColorspace(destination_image,GRAYColorspace,exception); status=SetImageStorageClass(destination_image,DirectClass,exception); if (status == MagickFalse) { destination_image=GetLastImageInList(destination_image); return((Image *) NULL); } return(GetFirstImageInList(destination_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) { ThreadViewDataSet *pixels_sums; Image *average_image; const Image *last_image; long y; unsigned long row_count=0; double number_scenes; unsigned long number_pixels; MagickPassFail status=MagickPass; /* Ensure the image are the same size. */ assert(image != (Image *) NULL); assert(image->signature == MagickSignature); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); if (image->next == (Image *) NULL) ThrowImageException3(ImageError,ImageSequenceIsRequired, UnableToAverageImage); { const Image *next; for (next=image; next != (Image *) NULL; next=next->next) { if ((next->columns != image->columns) || (next->rows != image->rows)) ThrowImageException3(OptionError,UnableToAverageImageSequence, ImageWidthsOrHeightsDiffer); } } /* Allocate sum accumulation buffer. */ number_pixels=image->columns; pixels_sums=AllocateThreadViewDataArray(image,exception,number_pixels, sizeof(DoublePixelPacket)); if (pixels_sums == (ThreadViewDataSet *) NULL) ThrowImageException3(ResourceLimitError,MemoryAllocationFailed, UnableToAverageImageSequence); /* Initialize average next attributes. */ average_image=CloneImage(image,image->columns,image->rows,True,exception); if (average_image == (Image *) NULL) { DestroyThreadViewDataSet(pixels_sums); return((Image *) NULL); } average_image->storage_class=DirectClass; number_scenes=(double) GetImageListLength(image); last_image=GetLastImageInList(image); #if defined(HAVE_OPENMP) # pragma omp parallel for schedule(dynamic) shared(row_count, status) #endif for (y=0; y < (long) image->rows; y++) { register DoublePixelPacket *pixels_sum; const Image *next; register const PixelPacket *p; register long x; MagickBool thread_status; thread_status=status; if (thread_status == MagickFail) continue; pixels_sum=AccessThreadViewData(pixels_sums); /* Compute sum over each pixel color component. */ for (next=image; next != (Image *) NULL; next=next->next) { ViewInfo *next_view; next_view=OpenCacheView((Image *) next); if (next_view == (ViewInfo *) NULL) thread_status=MagickFail; if (next_view != (ViewInfo *) NULL) { p=AcquireCacheViewPixels(next_view,0,y,next->columns,1,exception); if (p == (const PixelPacket *) NULL) thread_status=MagickFail; if (p != (const PixelPacket *) NULL) { if (next == image) { for (x=0; x < (long) next->columns; x++) { pixels_sum[x].red=p[x].red; pixels_sum[x].green=p[x].green; pixels_sum[x].blue=p[x].blue; pixels_sum[x].opacity=p[x].opacity; } } else { for (x=0; x < (long) next->columns; x++) { pixels_sum[x].red+=p[x].red; pixels_sum[x].green+=p[x].green; pixels_sum[x].blue+=p[x].blue; pixels_sum[x].opacity+=p[x].opacity; } } } CloseCacheView(next_view); } } /* Average next pixels. */ if (thread_status != MagickFail) { register PixelPacket *q; q=SetImagePixelsEx(average_image,0,y,average_image->columns,1,exception); if (q == (PixelPacket *) NULL) thread_status=MagickFail; if (q != (PixelPacket *) NULL) { for (x=0; x < (long) average_image->columns; x++) { q[x].red=(Quantum) (pixels_sum[x].red/number_scenes+0.5); q[x].green=(Quantum) (pixels_sum[x].green/number_scenes+0.5); q[x].blue=(Quantum) (pixels_sum[x].blue/number_scenes+0.5); q[x].opacity=(Quantum) (pixels_sum[x].opacity/number_scenes+0.5); } if (!SyncImagePixelsEx(average_image,exception)) thread_status=MagickFail; } } #if defined(HAVE_OPENMP) # pragma omp critical (GM_AverageImages) #endif { row_count++; if (QuantumTick(row_count,average_image->rows)) if (!MagickMonitorFormatted(row_count,average_image->rows,exception, "[%s,...,%s] Average image sequence...", image->filename,last_image->filename)) thread_status=MagickFail; if (thread_status == MagickFail) status=MagickFail; } } DestroyThreadViewDataSet(pixels_sums); if (status == MagickFail) { DestroyImage(average_image); average_image=(Image *) NULL; } return(average_image); }