43#include "MagickCore/studio.h"
44#include "MagickCore/annotate.h"
45#include "MagickCore/client.h"
46#include "MagickCore/color.h"
47#include "MagickCore/composite.h"
48#include "MagickCore/constitute.h"
49#include "MagickCore/decorate.h"
50#include "MagickCore/draw.h"
51#include "MagickCore/effect.h"
52#include "MagickCore/enhance.h"
53#include "MagickCore/exception.h"
54#include "MagickCore/exception-private.h"
55#include "MagickCore/gem.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/memory_.h"
61#include "MagickCore/memory-private.h"
62#include "MagickCore/monitor.h"
63#include "MagickCore/monitor-private.h"
64#include "MagickCore/montage.h"
65#include "MagickCore/option.h"
66#include "MagickCore/pixel.h"
67#include "MagickCore/quantize.h"
68#include "MagickCore/property.h"
69#include "MagickCore/resize.h"
70#include "MagickCore/resource_.h"
71#include "MagickCore/string_.h"
72#include "MagickCore/utility.h"
73#include "MagickCore/utility-private.h"
74#include "MagickCore/version.h"
75#include "MagickCore/visual-effects.h"
110 clone_info=(
MontageInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
111 GetMontageInfo(image_info,clone_info);
114 if (montage_info->geometry != (
char *) NULL)
115 clone_info->geometry=AcquireString(montage_info->geometry);
116 if (montage_info->tile != (
char *) NULL)
117 clone_info->tile=AcquireString(montage_info->tile);
118 if (montage_info->title != (
char *) NULL)
119 clone_info->title=AcquireString(montage_info->title);
120 if (montage_info->frame != (
char *) NULL)
121 clone_info->frame=AcquireString(montage_info->frame);
122 if (montage_info->texture != (
char *) NULL)
123 clone_info->texture=AcquireString(montage_info->texture);
124 if (montage_info->font != (
char *) NULL)
125 clone_info->font=AcquireString(montage_info->font);
126 clone_info->pointsize=montage_info->pointsize;
127 clone_info->border_width=montage_info->border_width;
128 clone_info->shadow=montage_info->shadow;
129 clone_info->fill=montage_info->fill;
130 clone_info->stroke=montage_info->stroke;
131 clone_info->matte_color=montage_info->matte_color;
132 clone_info->background_color=montage_info->background_color;
133 clone_info->border_color=montage_info->border_color;
134 clone_info->gravity=montage_info->gravity;
135 (void) CopyMagickString(clone_info->filename,montage_info->filename,
137 clone_info->debug=IsEventLogging();
167 assert(montage_info->signature == MagickCoreSignature);
168 if (IsEventLogging() != MagickFalse)
169 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
170 if (montage_info->geometry != (
char *) NULL)
171 montage_info->geometry=(
char *)
172 RelinquishMagickMemory(montage_info->geometry);
173 if (montage_info->tile != (
char *) NULL)
174 montage_info->tile=DestroyString(montage_info->tile);
175 if (montage_info->title != (
char *) NULL)
176 montage_info->title=DestroyString(montage_info->title);
177 if (montage_info->frame != (
char *) NULL)
178 montage_info->frame=DestroyString(montage_info->frame);
179 if (montage_info->texture != (
char *) NULL)
180 montage_info->texture=(
char *) RelinquishMagickMemory(
181 montage_info->texture);
182 if (montage_info->font != (
char *) NULL)
183 montage_info->font=DestroyString(montage_info->font);
184 montage_info->signature=(~MagickCoreSignature);
185 montage_info=(
MontageInfo *) RelinquishMagickMemory(montage_info);
186 return(montage_info);
214MagickExport
void GetMontageInfo(
const ImageInfo *image_info,
217 assert(image_info != (
const ImageInfo *) NULL);
218 assert(image_info->signature == MagickCoreSignature);
220 if (IsEventLogging() != MagickFalse)
221 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",
222 image_info->filename);
223 (void) memset(montage_info,0,
sizeof(*montage_info));
224 (void) CopyMagickString(montage_info->filename,image_info->filename,
226 montage_info->geometry=AcquireString(DefaultTileGeometry);
227 if (image_info->font != (
char *) NULL)
228 montage_info->font=AcquireString(image_info->font);
229 montage_info->gravity=CenterGravity;
230 montage_info->pointsize=image_info->pointsize;
231 montage_info->fill.alpha=(MagickRealType) OpaqueAlpha;
232 montage_info->stroke.alpha=(MagickRealType) TransparentAlpha;
233 montage_info->matte_color=image_info->matte_color;
234 montage_info->background_color=image_info->background_color;
235 montage_info->border_color=image_info->border_color;
236 montage_info->debug=IsEventLogging();
237 montage_info->signature=MagickCoreSignature;
272static void GetMontageGeometry(
char *geometry,
const size_t number_images,
273 ssize_t *x_offset,ssize_t *y_offset,
size_t *tiles_per_column,
274 size_t *tiles_per_row)
278 (void) GetGeometry(geometry,x_offset,y_offset,tiles_per_row,tiles_per_column);
279 if ((*tiles_per_column == 0) && (*tiles_per_row == 0))
280 *tiles_per_column=(size_t) sqrt((
double) number_images);
281 if ((*tiles_per_column == 0) && (*tiles_per_row != 0))
282 *tiles_per_column=(size_t) ceil((
double) number_images/(*tiles_per_row));
283 if ((*tiles_per_row == 0) && (*tiles_per_column != 0))
284 *tiles_per_row=(size_t) ceil((
double) number_images/(*tiles_per_column));
287#if defined(__cplusplus) || defined(c_plusplus)
291static int SceneCompare(
const void *x,
const void *y)
297 image_1=(
Image **) x;
298 image_2=(
Image **) y;
299 return((
int) ((*image_1)->scene-(*image_2)->scene));
302#if defined(__cplusplus) || defined(c_plusplus)
306MagickExport
Image *MontageImages(
const Image *images,
315 image_info=AcquireImageInfo();
316 montage_image=MontageImageList(image_info,montage_info,images,exception);
317 image_info=DestroyImageInfo(image_info);
318 return(montage_image);
324#define MontageImageTag "Montage/Image"
325#define TileImageTag "Tile/Image"
328 tile_geometry[MagickPathExtent],
360 MagickProgressMonitor
404 assert(images != (
Image *) NULL);
405 assert(images->signature == MagickCoreSignature);
407 assert(montage_info->signature == MagickCoreSignature);
409 assert(exception->signature == MagickCoreSignature);
410 if (IsEventLogging() != MagickFalse)
411 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",images->filename);
412 number_images=GetImageListLength(images);
413 primary_list=ImageListToArray(images,exception);
414 if (primary_list == (
Image **) NULL)
415 return((
Image *) NULL);
416 image_list=primary_list;
418 thumbnail=NewImageList();
419 for (i=0; i < (ssize_t) number_images; i++)
421 image=CloneImage(image_list[i],0,0,MagickTrue,exception);
422 if (image == (
Image *) NULL)
424 (void) ParseAbsoluteGeometry(
"0x0+0+0",&image->page);
425 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL,
427 flags=ParseRegionGeometry(image,montage_info->geometry,&geometry,exception);
428 thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception);
429 if (thumbnail == (
Image *) NULL)
431 image_list[i]=thumbnail;
432 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
433 proceed=SetImageProgress(image,TileImageTag,(MagickOffsetType) i,
435 if (proceed == MagickFalse)
437 image=DestroyImage(image);
439 if (i < (ssize_t) number_images)
441 if (image != (
Image *) NULL)
442 image=DestroyImage(image);
443 if (thumbnail == (
Image *) NULL)
445 for (tile=0; (ssize_t) tile <= i; tile++)
446 if (image_list[tile] != (
Image *) NULL)
447 image_list[tile]=DestroyImage(image_list[tile]);
448 primary_list=(
Image **) RelinquishMagickMemory(primary_list);
449 return((
Image *) NULL);
454 for (i=0; i < (ssize_t) number_images; i++)
455 if (image_list[i]->scene == 0)
457 if (i == (ssize_t) number_images)
458 qsort((
void *) image_list,(
size_t) number_images,
sizeof(*image_list),
463 tiles_per_column=(size_t) sqrt((
double) number_images);
464 tiles_per_row=(size_t) ceil((
double) number_images/tiles_per_column);
467 if (montage_info->tile != (
char *) NULL)
468 GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset,
469 &tiles_per_column,&tiles_per_row);
473 concatenate=MagickFalse;
474 SetGeometry(image_list[0],&extract_info);
475 extract_info.x=(ssize_t) montage_info->border_width;
476 extract_info.y=(ssize_t) montage_info->border_width;
477 if (montage_info->geometry != (
char *) NULL)
482 flags=GetGeometry(montage_info->geometry,&extract_info.x,&extract_info.y,
483 &extract_info.width,&extract_info.height);
484 concatenate=((flags & RhoValue) == 0) && ((flags & SigmaValue) == 0) ?
485 MagickTrue : MagickFalse;
487 border_width=montage_info->border_width;
489 (void) memset(&frame_info,0,
sizeof(frame_info));
490 if (montage_info->frame != (
char *) NULL)
493 absolute_geometry[MagickPathExtent];
495 frame_info.width=extract_info.width;
496 frame_info.height=extract_info.height;
497 (void) FormatLocaleString(absolute_geometry,MagickPathExtent,
"%s!",
498 montage_info->frame);
499 flags=ParseMetaGeometry(absolute_geometry,&frame_info.outer_bevel,
500 &frame_info.inner_bevel,&frame_info.width,&frame_info.height);
501 if ((flags & HeightValue) == 0)
502 frame_info.height=frame_info.width;
503 if ((flags & XiValue) == 0)
504 frame_info.outer_bevel=(ssize_t) frame_info.width/2-1;
505 if ((flags & PsiValue) == 0)
506 frame_info.inner_bevel=frame_info.outer_bevel;
507 frame_info.x=(ssize_t) frame_info.width;
508 frame_info.y=(ssize_t) frame_info.height;
509 bevel_width=(ssize_t) MagickMax(frame_info.inner_bevel,
510 frame_info.outer_bevel);
511 border_width=(size_t) MagickMax((ssize_t) frame_info.width,
512 (ssize_t) frame_info.height);
514 for (i=0; i < (ssize_t) number_images; i++)
516 if (image_list[i]->columns > extract_info.width)
517 extract_info.width=image_list[i]->columns;
518 if (image_list[i]->rows > extract_info.height)
519 extract_info.height=image_list[i]->rows;
524 clone_info=CloneImageInfo(image_info);
525 clone_info->background_color=montage_info->background_color;
526 clone_info->border_color=montage_info->border_color;
527 draw_info=CloneDrawInfo(clone_info,(
DrawInfo *) NULL);
528 if (montage_info->font != (
char *) NULL)
529 (void) CloneString(&draw_info->font,montage_info->font);
530 if (montage_info->pointsize != 0.0)
531 draw_info->pointsize=montage_info->pointsize;
532 draw_info->gravity=CenterGravity;
533 draw_info->stroke=montage_info->stroke;
534 draw_info->fill=montage_info->fill;
535 draw_info->text=AcquireString(
"");
536 (void) GetTypeMetrics(image_list[0],draw_info,&metrics,exception);
537 texture=NewImageList();
538 if (montage_info->texture != (
char *) NULL)
540 (void) CopyMagickString(clone_info->filename,montage_info->texture,
542 texture=ReadImage(clone_info,exception);
547 title=InterpretImageProperties(clone_info,image_list[0],montage_info->title,
550 if (montage_info->title != (
char *) NULL)
551 title_offset=(size_t) (2*(metrics.ascent-metrics.descent)*
552 MultilineCensus(title)+2*extract_info.y);
554 for (i=0; i < (ssize_t) number_images; i++)
556 value=GetImageProperty(image_list[i],
"label",exception);
557 if (value == (
const char *) NULL)
559 if (MultilineCensus(value) > number_lines)
560 number_lines=MultilineCensus(value);
565 tile_image=AcquireImage((
ImageInfo *) NULL,exception);
566 montage=AcquireImage(clone_info,exception);
567 montage->background_color=montage_info->background_color;
569 images_per_page=(number_images-1)/(tiles_per_row*tiles_per_column)+1;
571 total_tiles=(size_t) number_images;
572 for (i=0; i < (ssize_t) images_per_page; i++)
577 tiles_per_page=tiles_per_row*tiles_per_column;
580 if (montage_info->tile != (
char *) NULL)
581 GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset,
583 tiles_per_page=tiles_per_row*tiles_per_column;
584 y_offset+=(ssize_t) title_offset;
589 for (tile=0; tile < (ssize_t) tiles_per_page; tile++)
591 if (tile < (ssize_t) number_images)
593 width=concatenate != MagickFalse ? image_list[tile]->columns :
595 if (image_list[tile]->rows > max_height)
596 max_height=image_list[tile]->rows;
598 x_offset+=((ssize_t) width+2*(extract_info.x+(ssize_t) border_width));
599 if (x_offset > (ssize_t) bounds.width)
600 bounds.width=(size_t) x_offset;
601 if (((tile+1) == (ssize_t) tiles_per_page) ||
602 (((tile+1) % (ssize_t) tiles_per_row) == 0))
605 if (montage_info->tile != (
char *) NULL)
606 GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y,
608 height=concatenate != MagickFalse ? max_height : extract_info.height;
609 y_offset+=((ssize_t) height+(extract_info.y+(ssize_t) border_width)*2+
610 (metrics.ascent-metrics.descent+4)*(ssize_t) number_lines+
611 (montage_info->shadow != MagickFalse ? 4 : 0));
612 if (y_offset > (ssize_t) bounds.height)
613 bounds.height=(size_t) y_offset;
617 if (montage_info->shadow != MagickFalse)
622 (void) CopyMagickString(montage->filename,montage_info->filename,
624 montage->columns=(size_t) MagickMax((ssize_t) bounds.width,1);
625 montage->rows=(size_t) MagickMax((ssize_t) bounds.height,1);
626 (void) SetImageBackgroundColor(montage,exception);
630 montage->montage=AcquireString((
char *) NULL);
633 while (tile < MagickMin((ssize_t) tiles_per_page,(ssize_t) number_images))
635 extent+=strlen(image_list[tile]->filename)+1;
638 montage->directory=(
char *) AcquireQuantumMemory(extent,
639 sizeof(*montage->directory));
640 if ((montage->montage == (
char *) NULL) ||
641 (montage->directory == (
char *) NULL))
643 if (montage->montage != (
char *) NULL)
644 montage->montage=(
char *) RelinquishMagickMemory(montage->montage);
645 if (montage->directory != (
char *) NULL)
646 montage->directory=(
char *) RelinquishMagickMemory(
648 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
652 if (montage_info->tile != (
char *) NULL)
653 GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset,
655 y_offset+=(ssize_t) title_offset;
656 (void) FormatLocaleString(montage->montage,MagickPathExtent,
657 "%.20gx%.20g%+.20g%+.20g",(
double) ((ssize_t) extract_info.width+
658 (extract_info.x+(ssize_t) border_width)*2),(
double) ((ssize_t)
659 extract_info.height+(extract_info.y+(ssize_t) border_width)*2+(
double)
660 ((metrics.ascent-metrics.descent+4)*number_lines+
661 (montage_info->shadow != MagickFalse ? 4 : 0))),(
double) x_offset,
663 *montage->directory=
'\0';
665 while (tile < MagickMin((ssize_t) tiles_per_page,(ssize_t) number_images))
667 if (strchr(image_list[tile]->filename,(
int)
'\xff') == (
char *) NULL)
668 (void) ConcatenateMagickString(montage->directory,
669 image_list[tile]->filename,extent);
671 (
void) ThrowMagickException(exception,GetMagickModule(),OptionError,
672 "InvalidArgument",
"'%s'",image_list[tile]->filename);
673 (void) ConcatenateMagickString(montage->directory,
"\xff",extent);
676 progress_monitor=SetImageProgressMonitor(montage,(MagickProgressMonitor)
677 NULL,montage->client_data);
678 if (texture != (
Image *) NULL)
679 (void) TextureImage(montage,texture,exception);
680 if (montage_info->title != (
char *) NULL)
691 draw_clone_info=CloneDrawInfo(image_info,draw_info);
692 draw_clone_info->gravity=CenterGravity;
693 draw_clone_info->pointsize*=2.0;
694 (void) GetTypeMetrics(image_list[0],draw_clone_info,&tile_metrics,
696 (void) FormatLocaleString(tile_geometry,MagickPathExtent,
697 "%.20gx%.20g%+.20g%+.20g",(
double) montage->columns,(
double)
698 (tile_metrics.ascent-tile_metrics.descent),0.0,
699 (
double) extract_info.y+4);
700 (void) CloneString(&draw_clone_info->geometry,tile_geometry);
701 (void) CloneString(&draw_clone_info->text,title);
702 (void) AnnotateImage(montage,draw_clone_info,exception);
703 draw_clone_info=DestroyDrawInfo(draw_clone_info);
705 (void) SetImageProgressMonitor(montage,progress_monitor,
706 montage->client_data);
712 if (montage_info->tile != (
char *) NULL)
713 GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset,
715 x_offset+=extract_info.x;
716 y_offset+=(ssize_t) title_offset+extract_info.y;
719 for (tile=0; tile < MagickMin((ssize_t) tiles_per_page,(ssize_t) number_images); tile++)
724 image=CloneImage(image_list[tile],0,0,MagickTrue,exception);
725 if (image == (
Image *) NULL)
726 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
727 progress_monitor=SetImageProgressMonitor(image,
728 (MagickProgressMonitor) NULL,image->client_data);
729 width=concatenate != MagickFalse ? image->columns : extract_info.width;
730 if (image->rows > max_height)
731 max_height=image->rows;
732 height=concatenate != MagickFalse ? max_height : extract_info.height;
733 if (border_width != 0)
744 border_info.width=border_width;
745 border_info.height=border_width;
746 if (montage_info->frame != (
char *) NULL)
748 border_info.width=(width-image->columns+1)/2;
749 border_info.height=(height-image->rows+1)/2;
751 border_image=BorderImage(image,&border_info,image->compose,exception);
752 if (border_image != (
Image *) NULL)
754 image=DestroyImage(image);
757 if ((montage_info->frame != (
char *) NULL) &&
758 (image->compose == DstOutCompositeOp))
760 (void) SetPixelChannelMask(image,AlphaChannel);
761 (void) NegateImage(image,MagickFalse,exception);
762 (void) SetPixelChannelMask(image,DefaultChannels);
768 tile_image->columns=width;
769 tile_image->rows=height;
770 tile_image->gravity=montage_info->gravity;
771 if (image->gravity != UndefinedGravity)
772 tile_image->gravity=image->gravity;
773 (void) FormatLocaleString(tile_geometry,MagickPathExtent,
774 "%.20gx%.20g+0+0",(
double) image->columns,(
double) image->rows);
775 flags=ParseGravityGeometry(tile_image,tile_geometry,&geometry,exception);
776 x=geometry.x+(ssize_t) border_width;
777 y=geometry.y+(ssize_t) border_width;
778 if ((montage_info->frame != (
char *) NULL) && (bevel_width > 0))
789 frame_clone=frame_info;
790 frame_clone.width=width+2*frame_info.width;
791 frame_clone.height=height+2*frame_info.height;
792 value=GetImageProperty(image,
"label",exception);
793 if (value != (
const char *) NULL)
794 frame_clone.height+=(size_t) ((metrics.ascent-metrics.descent+4)*
795 MultilineCensus(value));
796 frame_image=FrameImage(image,&frame_clone,image->compose,exception);
797 if (frame_image != (
Image *) NULL)
799 image=DestroyImage(image);
805 if (LocaleCompare(image->magick,
"NULL") != 0)
810 if (montage_info->shadow != MagickFalse)
818 (void) QueryColorCompliance(
"#0000",AllCompliance,
819 &image->background_color,exception);
820 shadow_image=ShadowImage(image,30.0,5.0,5,5,exception);
821 if (shadow_image != (
Image *) NULL)
823 (void) CompositeImage(shadow_image,image,OverCompositeOp,
824 MagickTrue,0,0,exception);
825 image=DestroyImage(image);
829 (void) CompositeImage(montage,image,image->compose,MagickTrue,
830 x_offset+x,y_offset+y,exception);
831 value=GetImageProperty(image,
"label",exception);
832 if (value != (
const char *) NULL)
837 (void) FormatLocaleString(tile_geometry,MagickPathExtent,
838 "%.20gx%.20g%+.20g%+.20g",(
double) ((montage_info->frame ?
839 image->columns : width)-2*border_width),(
double)
840 (metrics.ascent-metrics.descent+4)*MultilineCensus(value),
841 (
double) (x_offset+(ssize_t) border_width),(
double)
842 ((montage_info->frame ? y_offset+(ssize_t) height+(ssize_t)
843 border_width+4 : y_offset+(ssize_t) extract_info.height+
844 (ssize_t) border_width+
845 (montage_info->shadow != MagickFalse ? 4 : 0))+bevel_width));
846 (void) CloneString(&draw_info->geometry,tile_geometry);
847 (void) CloneString(&draw_info->text,value);
848 (void) AnnotateImage(montage,draw_info,exception);
851 x_offset+=(ssize_t) width+2*(extract_info.x+(ssize_t) border_width);
852 if (((tile+1) == (ssize_t) tiles_per_page) ||
853 (((tile+1) % (ssize_t) tiles_per_row) == 0))
855 x_offset=extract_info.x;
856 y_offset+=((ssize_t) height+(extract_info.y+(ssize_t) border_width)*2+
857 (metrics.ascent-metrics.descent+4)*number_lines+
858 (montage_info->shadow != MagickFalse ? 4 : 0));
861 if (images->progress_monitor != (MagickProgressMonitor) NULL)
863 proceed=SetImageProgress(image,MontageImageTag,tiles,total_tiles);
864 if (proceed == MagickFalse)
867 image_list[tile]=DestroyImage(image_list[tile]);
868 image=DestroyImage(image);
872 if ((i+1) < (ssize_t) images_per_page)
877 AcquireNextImage(clone_info,montage,exception);
878 if (GetNextImageInList(montage) == (
Image *) NULL)
880 montage=DestroyImageList(montage);
881 return((
Image *) NULL);
883 montage=GetNextImageInList(montage);
884 montage->background_color=montage_info->background_color;
885 image_list+=tiles_per_page;
886 number_images-=tiles_per_page;
889 tile_image=DestroyImage(tile_image);
890 if (texture != (
Image *) NULL)
891 texture=DestroyImage(texture);
892 title=DestroyString(title);
893 primary_list=(
Image **) RelinquishMagickMemory(primary_list);
894 draw_info=DestroyDrawInfo(draw_info);
895 clone_info=DestroyImageInfo(clone_info);
896 return(GetFirstImageInList(montage));