WandExport MagickBooleanType IdentifyImageCommand(ImageInfo *image_info, int argc,char **argv,char **metadata,ExceptionInfo *exception) { #define DestroyIdentify() \ { \ DestroyImageStack(); \ for (i=0; i < (ssize_t) argc; i++) \ argv[i]=DestroyString(argv[i]); \ argv=(char **) RelinquishMagickMemory(argv); \ } #define ThrowIdentifyException(asperity,tag,option) \ { \ (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag,"`%s'", \ option); \ DestroyIdentify(); \ return(MagickFalse); \ } #define ThrowIdentifyInvalidArgumentException(option,argument) \ { \ (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \ "InvalidArgument","`%s': %s",option,argument); \ DestroyIdentify(); \ return(MagickFalse); \ } const char *format, *option; Image *image; ImageStack image_stack[MaxImageStackDepth+1]; MagickBooleanType fire, pend, respect_parenthesis; MagickStatusType status; register ssize_t i; size_t count; ssize_t j, k; /* Set defaults. */ assert(image_info != (ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); assert(exception != (ExceptionInfo *) NULL); if (argc == 2) { option=argv[1]; if ((LocaleCompare("version",option+1) == 0) || (LocaleCompare("-version",option+1) == 0)) { (void) FormatLocaleFile(stdout,"Version: %s\n", GetMagickVersion((size_t *) NULL)); (void) FormatLocaleFile(stdout,"Copyright: %s\n", GetMagickCopyright()); (void) FormatLocaleFile(stdout,"Features: %s\n\n", GetMagickFeatures()); return(MagickFalse); } } if (argc < 2) return(IdentifyUsage()); count=0; format=NULL; j=1; k=0; NewImageStack(); option=(char *) NULL; pend=MagickFalse; respect_parenthesis=MagickFalse; status=MagickTrue; /* Identify an image. */ ReadCommandlLine(argc,&argv); status=ExpandFilenames(&argc,&argv); if (status == MagickFalse) ThrowIdentifyException(ResourceLimitError,"MemoryAllocationFailed", GetExceptionMessage(errno)); image_info->ping=MagickTrue; for (i=1; i < (ssize_t) argc; i++) { option=argv[i]; if (LocaleCompare(option,"(") == 0) { FireImageStack(MagickFalse,MagickTrue,pend); if (k == MaxImageStackDepth) ThrowIdentifyException(OptionError,"ParenthesisNestedTooDeeply", option); PushImageStack(); continue; } if (LocaleCompare(option,")") == 0) { FireImageStack(MagickFalse,MagickTrue,MagickTrue); if (k == 0) ThrowIdentifyException(OptionError,"UnableToParseExpression",option); PopImageStack(); continue; } if (IsCommandOption(option) == MagickFalse) { char *filename; Image *images; ImageInfo *identify_info; /* Read input image. */ FireImageStack(MagickFalse,MagickFalse,pend); identify_info=CloneImageInfo(image_info); identify_info->verbose=MagickFalse; filename=argv[i]; if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1))) filename=argv[++i]; (void) SetImageOption(image_info,"filename",filename); (void) CopyMagickString(identify_info->filename,filename,MaxTextExtent); if (identify_info->ping != MagickFalse) images=PingImages(identify_info,exception); else images=ReadImages(identify_info,exception); identify_info=DestroyImageInfo(identify_info); status&=(images != (Image *) NULL) && (exception->severity < ErrorException); if (images == (Image *) NULL) continue; AppendImageStack(images); FinalizeImageSettings(image_info,image,MagickFalse); for ( ; image != (Image *) NULL; image=GetNextImageInList(image)) { if (image->scene == 0) image->scene=count++; if (format == (char *) NULL) { (void) IdentifyImage(image,stdout,image_info->verbose); continue; } if (metadata != (char **) NULL) { char *text; text=InterpretImageProperties(image_info,image,format); if (text == (char *) NULL) ThrowIdentifyException(ResourceLimitError, "MemoryAllocationFailed",GetExceptionMessage(errno)); (void) ConcatenateString(&(*metadata),text); text=DestroyString(text); if (LocaleCompare(format,"%n") == 0) break; } } RemoveAllImageStack(); continue; } pend=image != (Image *) NULL ? MagickTrue : MagickFalse; switch (*(option+1)) { case 'a': { if (LocaleCompare("alpha",option+1) == 0) { ssize_t type; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); type=ParseCommandOption(MagickAlphaOptions,MagickFalse,argv[i]); if (type < 0) ThrowIdentifyException(OptionError,"UnrecognizedAlphaChannelType", argv[i]); break; } if (LocaleCompare("antialias",option+1) == 0) break; if (LocaleCompare("authenticate",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'c': { if (LocaleCompare("cache",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("channel",option+1) == 0) { ssize_t channel; if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); channel=ParseChannelOption(argv[i]); if (channel < 0) ThrowIdentifyException(OptionError,"UnrecognizedChannelType", argv[i]); break; } if (LocaleCompare("colorspace",option+1) == 0) { ssize_t colorspace; if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); colorspace=ParseCommandOption(MagickColorspaceOptions, MagickFalse,argv[i]); if (colorspace < 0) ThrowIdentifyException(OptionError,"UnrecognizedColorspace", argv[i]); break; } if (LocaleCompare("crop",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); image_info->ping=MagickFalse; break; } if (LocaleCompare("concurrent",option+1) == 0) break; ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'd': { if (LocaleCompare("debug",option+1) == 0) { ssize_t event; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); event=ParseCommandOption(MagickLogEventOptions,MagickFalse,argv[i]); if (event < 0) ThrowIdentifyException(OptionError,"UnrecognizedEventType", argv[i]); (void) SetLogEventMask(argv[i]); break; } if (LocaleCompare("define",option+1) == 0) { i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (*option == '+') { const char *define; define=GetImageOption(image_info,argv[i]); if (define == (const char *) NULL) ThrowIdentifyException(OptionError,"NoSuchOption",argv[i]); break; } break; } if (LocaleCompare("density",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("depth",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("duration",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'f': { if (LocaleCompare("features",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("format",option+1) == 0) { format=(char *) NULL; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); format=argv[i]; break; } if (LocaleCompare("fuzz",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'g': { if (LocaleCompare("gamma",option+1) == 0) { i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'h': { if ((LocaleCompare("help",option+1) == 0) || (LocaleCompare("-help",option+1) == 0)) return(IdentifyUsage()); ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'i': { if (LocaleCompare("interlace",option+1) == 0) { ssize_t interlace; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); interlace=ParseCommandOption(MagickInterlaceOptions,MagickFalse, argv[i]); if (interlace < 0) ThrowIdentifyException(OptionError, "UnrecognizedInterlaceType",argv[i]); break; } if (LocaleCompare("interpolate",option+1) == 0) { ssize_t interpolate; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); interpolate=ParseCommandOption(MagickInterpolateOptions,MagickFalse, argv[i]); if (interpolate < 0) ThrowIdentifyException(OptionError, "UnrecognizedInterpolateMethod",argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'l': { if (LocaleCompare("limit",option+1) == 0) { char *p; double value; ssize_t resource; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); resource=ParseCommandOption(MagickResourceOptions,MagickFalse, argv[i]); if (resource < 0) ThrowIdentifyException(OptionError,"UnrecognizedResourceType", argv[i]); i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); value=StringToDouble(argv[i],&p); (void) value; if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0)) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("list",option+1) == 0) { ssize_t list; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); list=ParseCommandOption(MagickListOptions,MagickFalse,argv[i]); if (list < 0) ThrowIdentifyException(OptionError,"UnrecognizedListType", argv[i]); status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **) argv+j,exception); DestroyIdentify(); return(status != 0 ? MagickFalse : MagickTrue); } if (LocaleCompare("log",option+1) == 0) { if (*option == '+') break; i++; if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL)) ThrowIdentifyException(OptionError,"MissingArgument",option); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'm': { if (LocaleCompare("matte",option+1) == 0) break; if (LocaleCompare("monitor",option+1) == 0) break; ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'n': { if (LocaleCompare("negate",option+1) == 0) break; ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'p': { if (LocaleCompare("ping",option+1) == 0) break; ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'q': { if (LocaleCompare("quiet",option+1) == 0) break; ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'r': { if (LocaleCompare("regard-warnings",option+1) == 0) break; if (LocaleNCompare("respect-parentheses",option+1,17) == 0) { respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse; break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 's': { if (LocaleCompare("sampling-factor",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("seed",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("set",option+1) == 0) { i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); break; } if (LocaleCompare("size",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("strip",option+1) == 0) break; if (LocaleCompare("support",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowIdentifyInvalidArgumentException(option,argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'u': { if (LocaleCompare("unique",option+1) == 0) break; if (LocaleCompare("units",option+1) == 0) { ssize_t units; if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); units=ParseCommandOption(MagickResolutionOptions,MagickFalse, argv[i]); if (units < 0) ThrowIdentifyException(OptionError,"UnrecognizedUnitsType", argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case 'v': { if (LocaleCompare("verbose",option+1) == 0) break; if (LocaleCompare("virtual-pixel",option+1) == 0) { ssize_t method; if (*option == '+') break; i++; if (i == (ssize_t) (argc-1)) ThrowIdentifyException(OptionError,"MissingArgument",option); method=ParseCommandOption(MagickVirtualPixelOptions,MagickFalse, argv[i]); if (method < 0) ThrowIdentifyException(OptionError, "UnrecognizedVirtualPixelMethod",argv[i]); break; } ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } case '?': break; default: ThrowIdentifyException(OptionError,"UnrecognizedOption",option) } fire=(GetCommandOptionFlags(MagickCommandOptions,MagickFalse,option) & FireOptionFlag) == 0 ? MagickFalse : MagickTrue; if (fire != MagickFalse) FireImageStack(MagickFalse,MagickTrue,MagickTrue); } if (k != 0) ThrowIdentifyException(OptionError,"UnbalancedParenthesis",argv[i]); if (i != (ssize_t) argc) ThrowIdentifyException(OptionError,"MissingAnImageFilename",argv[i]); DestroyIdentify(); return(status != 0 ? MagickTrue : MagickFalse); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d P A N G O I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadPANGOImage() reads an image in the Pango Markup Language Format. % % The format of the ReadPANGOImage method is: % % Image *ReadPANGOImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadPANGOImage(const ImageInfo *image_info, ExceptionInfo *exception) { cairo_font_options_t *font_options; cairo_surface_t *surface; char *caption, *property; cairo_t *cairo_image; const char *option; DrawInfo *draw_info; Image *image; MagickBooleanType status; PangoAlignment align; PangoContext *context; PangoFontMap *fontmap; PangoGravity gravity; PangoLayout *layout; PangoRectangle extent; PixelInfo fill_color; RectangleInfo page; register unsigned char *p; size_t stride; ssize_t y; unsigned char *pixels; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info,exception); (void) ResetImagePage(image,"0x0+0+0"); /* Format caption. */ option=GetImageArtifact(image,"filename"); if (option == (const char *) NULL) property=InterpretImageProperties(image_info,image,image_info->filename, exception); else if (LocaleNCompare(option,"pango:",6) == 0) property=InterpretImageProperties(image_info,image,option+6,exception); else property=InterpretImageProperties(image_info,image,option,exception); (void) SetImageProperty(image,"caption",property,exception); property=DestroyString(property); caption=ConstantString(GetImageProperty(image,"caption",exception)); /* Get context. */ fontmap=pango_cairo_font_map_new(); pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(fontmap), image->resolution.x == 0.0 ? 90.0 : image->resolution.x); font_options=cairo_font_options_create(); option=GetImageArtifact(image,"pango:hinting"); if (option != (const char *) NULL) { if (LocaleCompare(option,"none") != 0) cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_NONE); if (LocaleCompare(option,"full") != 0) cairo_font_options_set_hint_style(font_options,CAIRO_HINT_STYLE_FULL); } context=pango_font_map_create_context(fontmap); pango_cairo_context_set_font_options(context,font_options); cairo_font_options_destroy(font_options); option=GetImageArtifact(image,"pango:language"); if (option != (const char *) NULL) pango_context_set_language(context,pango_language_from_string(option)); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); pango_context_set_base_dir(context,draw_info->direction == RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR); switch (draw_info->gravity) { case NorthGravity: { gravity=PANGO_GRAVITY_NORTH; break; } case NorthWestGravity: case WestGravity: case SouthWestGravity: { gravity=PANGO_GRAVITY_WEST; break; } case NorthEastGravity: case EastGravity: case SouthEastGravity: { gravity=PANGO_GRAVITY_EAST; break; } case SouthGravity: { gravity=PANGO_GRAVITY_SOUTH; break; } default: { gravity=PANGO_GRAVITY_AUTO; break; } } pango_context_set_base_gravity(context,gravity); option=GetImageArtifact(image,"pango:gravity-hint"); if (option != (const char *) NULL) { if (LocaleCompare(option,"line") == 0) pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE); if (LocaleCompare(option,"natural") == 0) pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL); if (LocaleCompare(option,"strong") == 0) pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG); } /* Configure layout. */ layout=pango_layout_new(context); option=GetImageArtifact(image,"pango:auto-dir"); if (option != (const char *) NULL) pango_layout_set_auto_dir(layout,1); option=GetImageArtifact(image,"pango:ellipsize"); if (option != (const char *) NULL) { if (LocaleCompare(option,"end") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END); if (LocaleCompare(option,"middle") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE); if (LocaleCompare(option,"none") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE); if (LocaleCompare(option,"start") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START); } option=GetImageArtifact(image,"pango:justify"); if (IfMagickTrue(IsStringTrue(option))) pango_layout_set_justify(layout,1); option=GetImageArtifact(image,"pango:single-paragraph"); if (IfMagickTrue(IsStringTrue(option))) pango_layout_set_single_paragraph_mode(layout,1); option=GetImageArtifact(image,"pango:wrap"); if (option != (const char *) NULL) { if (LocaleCompare(option,"char") == 0) pango_layout_set_wrap(layout,PANGO_WRAP_CHAR); if (LocaleCompare(option,"word") == 0) pango_layout_set_wrap(layout,PANGO_WRAP_WORD); if (LocaleCompare(option,"word-char") == 0) pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR); } option=GetImageArtifact(image,"pango:indent"); if (option != (const char *) NULL) pango_layout_set_indent(layout,(int) ((StringToLong(option)* (image->resolution.x == 0.0 ? 90.0 : image->resolution.x)*PANGO_SCALE+36)/ 90.0+0.5)); switch (draw_info->align) { case CenterAlign: align=PANGO_ALIGN_CENTER; break; case RightAlign: align=PANGO_ALIGN_RIGHT; break; case LeftAlign: align=PANGO_ALIGN_LEFT; break; default: { if (draw_info->gravity == CenterGravity) { align=PANGO_ALIGN_CENTER; break; } align=PANGO_ALIGN_LEFT; break; } } if ((align != PANGO_ALIGN_CENTER) && (draw_info->direction == RightToLeftDirection)) align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align); pango_layout_set_alignment(layout,align); if (draw_info->font != (char *) NULL) { PangoFontDescription *description; /* Set font. */ description=pango_font_description_from_string(draw_info->font); pango_font_description_set_size(description,(int) (PANGO_SCALE* draw_info->pointsize+0.5)); pango_layout_set_font_description(layout,description); pango_font_description_free(description); } option=GetImageArtifact(image,"pango:markup"); if ((option != (const char *) NULL) && (IsStringTrue(option) == MagickFalse)) pango_layout_set_text(layout,caption,-1); else { GError *error; error=(GError *) NULL; if (pango_parse_markup(caption,-1,0,NULL,NULL,NULL,&error) == 0) (void) ThrowMagickException(exception,GetMagickModule(),CoderError, error->message,"`%s'",image_info->filename); pango_layout_set_markup(layout,caption,-1); } pango_layout_context_changed(layout); page.x=0; page.y=0; if (image_info->page != (char *) NULL) (void) ParseAbsoluteGeometry(image_info->page,&page); if (image->columns == 0) { pango_layout_get_extents(layout,NULL,&extent); image->columns=(extent.x+extent.width+PANGO_SCALE/2)/PANGO_SCALE+2*page.x; } else { image->columns-=2*page.x; pango_layout_set_width(layout,(int) ((PANGO_SCALE*image->columns* (image->resolution.x == 0.0 ? 90.0 : image->resolution.x)+45.0)/90.0+ 0.5)); } if (image->rows == 0) { pango_layout_get_extents(layout,NULL,&extent); image->rows=(extent.y+extent.height+PANGO_SCALE/2)/PANGO_SCALE+2*page.y; } else { image->rows-=2*page.y; pango_layout_set_height(layout,(int) ((PANGO_SCALE*image->rows* (image->resolution.y == 0.0 ? 90.0 : image->resolution.y)+45.0)/90.0+ 0.5)); } /* Render markup. */ stride=(size_t) cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, image->columns); pixels=(unsigned char *) AcquireQuantumMemory(image->rows,stride* sizeof(*pixels)); if (pixels == (unsigned char *) NULL) { draw_info=DestroyDrawInfo(draw_info); caption=DestroyString(caption); ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } surface=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32, image->columns,image->rows,stride); cairo_image=cairo_create(surface); cairo_set_operator(cairo_image,CAIRO_OPERATOR_CLEAR); cairo_paint(cairo_image); cairo_set_operator(cairo_image,CAIRO_OPERATOR_OVER); cairo_translate(cairo_image,page.x,page.y); pango_cairo_show_layout(cairo_image,layout); cairo_destroy(cairo_image); cairo_surface_destroy(surface); g_object_unref(layout); g_object_unref(fontmap); /* Convert surface to image. */ (void) SetImageBackgroundColor(image,exception); p=pixels; GetPixelInfo(image,&fill_color); for (y=0; y < (ssize_t) image->rows; y++) { register Quantum *q; register ssize_t x; q=GetAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (Quantum *) NULL) break; for (x=0; x < (ssize_t) image->columns; x++) { double gamma; fill_color.blue=(double) ScaleCharToQuantum(*p++); fill_color.green=(double) ScaleCharToQuantum(*p++); fill_color.red=(double) ScaleCharToQuantum(*p++); fill_color.alpha=(double) ScaleCharToQuantum(*p++); /* Disassociate alpha. */ gamma=1.0-QuantumScale*fill_color.alpha; gamma=PerceptibleReciprocal(gamma); fill_color.blue*=gamma; fill_color.green*=gamma; fill_color.red*=gamma; CompositePixelOver(image,&fill_color,fill_color.alpha,q,(double) GetPixelAlpha(image,q),q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) break; if (image->previous == (Image *) NULL) { status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, image->rows); if (status == MagickFalse) break; } } /* Relinquish resources. */ pixels=(unsigned char *) RelinquishMagickMemory(pixels); draw_info=DestroyDrawInfo(draw_info); caption=DestroyString(caption); return(GetFirstImageInList(image)); }
WandExport MagickBooleanType CompareImagesCommand(ImageInfo *image_info, int argc,char **argv,char **metadata,ExceptionInfo *exception) { #define CompareEpsilon (1.0e-06) #define DefaultDissimilarityThreshold 0.31830988618379067154 #define DefaultSimilarityThreshold (-1.0) #define DestroyCompare() \ { \ if (similarity_image != (Image *) NULL) \ similarity_image=DestroyImageList(similarity_image); \ if (difference_image != (Image *) NULL) \ difference_image=DestroyImageList(difference_image); \ DestroyImageStack(); \ for (i=0; i < (ssize_t) argc; i++) \ argv[i]=DestroyString(argv[i]); \ argv=(char **) RelinquishMagickMemory(argv); \ } #define ThrowCompareException(asperity,tag,option) \ { \ if (exception->severity < (asperity)) \ (void) ThrowMagickException(exception,GetMagickModule(),asperity,tag, \ "`%s'",option); \ DestroyCompare(); \ return(MagickFalse); \ } #define ThrowCompareInvalidArgumentException(option,argument) \ { \ (void) ThrowMagickException(exception,GetMagickModule(),OptionError, \ "InvalidArgument","'%s': %s",option,argument); \ DestroyCompare(); \ return(MagickFalse); \ } char *filename, *option; const char *format; double dissimilarity_threshold, distortion, similarity_metric, similarity_threshold; Image *difference_image, *image, *reconstruct_image, *similarity_image; ImageStack image_stack[MaxImageStackDepth+1]; MagickBooleanType fire, pend, respect_parenthesis, subimage_search; MagickStatusType status; MetricType metric; RectangleInfo offset; register ssize_t i; ssize_t j, k; /* Set defaults. */ assert(image_info != (ImageInfo *) NULL); assert(image_info->signature == MagickCoreSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); assert(exception != (ExceptionInfo *) NULL); if (argc == 2) { option=argv[1]; if ((LocaleCompare("version",option+1) == 0) || (LocaleCompare("-version",option+1) == 0)) { ListMagickVersion(stdout); return(MagickFalse); } } if (argc < 3) return(CompareUsage()); difference_image=NewImageList(); similarity_image=NewImageList(); dissimilarity_threshold=DefaultDissimilarityThreshold; similarity_threshold=DefaultSimilarityThreshold; distortion=0.0; format=(char *) NULL; j=1; k=0; metric=UndefinedErrorMetric; NewImageStack(); option=(char *) NULL; pend=MagickFalse; reconstruct_image=NewImageList(); respect_parenthesis=MagickFalse; status=MagickTrue; subimage_search=MagickFalse; /* Compare an image. */ ReadCommandlLine(argc,&argv); status=ExpandFilenames(&argc,&argv); if (status == MagickFalse) ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed", GetExceptionMessage(errno)); for (i=1; i < (ssize_t) (argc-1); i++) { option=argv[i]; if (LocaleCompare(option,"(") == 0) { FireImageStack(MagickTrue,MagickTrue,pend); if (k == MaxImageStackDepth) ThrowCompareException(OptionError,"ParenthesisNestedTooDeeply", option); PushImageStack(); continue; } if (LocaleCompare(option,")") == 0) { FireImageStack(MagickTrue,MagickTrue,MagickTrue); if (k == 0) ThrowCompareException(OptionError,"UnableToParseExpression",option); PopImageStack(); continue; } if (IsCommandOption(option) == MagickFalse) { Image *images; /* Read input image. */ FireImageStack(MagickFalse,MagickFalse,pend); filename=argv[i]; if ((LocaleCompare(filename,"--") == 0) && (i < (ssize_t) (argc-1))) filename=argv[++i]; images=ReadImages(image_info,filename,exception); status&=(images != (Image *) NULL) && (exception->severity < ErrorException); if (images == (Image *) NULL) continue; AppendImageStack(images); continue; } pend=image != (Image *) NULL ? MagickTrue : MagickFalse; switch (*(option+1)) { case 'a': { if (LocaleCompare("alpha",option+1) == 0) { ssize_t type; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); type=ParseCommandOption(MagickAlphaChannelOptions,MagickFalse,argv[i]); if (type < 0) ThrowCompareException(OptionError,"UnrecognizedAlphaChannelOption", argv[i]); break; } if (LocaleCompare("authenticate",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option); } case 'c': { if (LocaleCompare("cache",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("channel",option+1) == 0) { ssize_t channel; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); channel=ParseChannelOption(argv[i]); if (channel < 0) ThrowCompareException(OptionError,"UnrecognizedChannelType", argv[i]); (void) SetPixelChannelMask(image,(ChannelType) channel); break; } if (LocaleCompare("colorspace",option+1) == 0) { ssize_t colorspace; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); colorspace=ParseCommandOption(MagickColorspaceOptions,MagickFalse, argv[i]); if (colorspace < 0) ThrowCompareException(OptionError,"UnrecognizedColorspace", argv[i]); break; } if (LocaleCompare("compose",option+1) == 0) { ssize_t compose; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); compose=ParseCommandOption(MagickComposeOptions,MagickFalse, argv[i]); if (compose < 0) ThrowCompareException(OptionError,"UnrecognizedComposeOperator", argv[i]); break; } if (LocaleCompare("compress",option+1) == 0) { ssize_t compress; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); compress=ParseCommandOption(MagickCompressOptions,MagickFalse, argv[i]); if (compress < 0) ThrowCompareException(OptionError,"UnrecognizedImageCompression", argv[i]); break; } if (LocaleCompare("concurrent",option+1) == 0) break; ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'd': { if (LocaleCompare("debug",option+1) == 0) { LogEventType event_mask; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); event_mask=SetLogEventMask(argv[i]); if (event_mask == UndefinedEvents) ThrowCompareException(OptionError,"UnrecognizedEventType", argv[i]); break; } if (LocaleCompare("decipher",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } if (LocaleCompare("define",option+1) == 0) { i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (*option == '+') { const char *define; define=GetImageOption(image_info,argv[i]); if (define == (const char *) NULL) ThrowCompareException(OptionError,"NoSuchOption",argv[i]); break; } break; } if (LocaleCompare("density",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("depth",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("dissimilarity-threshold",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); if (*option == '+') dissimilarity_threshold=DefaultDissimilarityThreshold; else dissimilarity_threshold=StringToDouble(argv[i],(char **) NULL); break; } if (LocaleCompare("duration",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'e': { if (LocaleCompare("encipher",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } if (LocaleCompare("extract",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'f': { if (LocaleCompare("format",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); format=argv[i]; break; } if (LocaleCompare("fuzz",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'h': { if ((LocaleCompare("help",option+1) == 0) || (LocaleCompare("-help",option+1) == 0)) return(CompareUsage()); if (LocaleCompare("highlight-color",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'i': { if (LocaleCompare("identify",option+1) == 0) break; if (LocaleCompare("interlace",option+1) == 0) { ssize_t interlace; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); interlace=ParseCommandOption(MagickInterlaceOptions,MagickFalse, argv[i]); if (interlace < 0) ThrowCompareException(OptionError,"UnrecognizedInterlaceType", argv[i]); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'l': { if (LocaleCompare("limit",option+1) == 0) { char *p; double value; ssize_t resource; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); resource=ParseCommandOption(MagickResourceOptions,MagickFalse, argv[i]); if (resource < 0) ThrowCompareException(OptionError,"UnrecognizedResourceType", argv[i]); i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); value=StringToDouble(argv[i],&p); (void) value; if ((p == argv[i]) && (LocaleCompare("unlimited",argv[i]) != 0)) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("list",option+1) == 0) { ssize_t list; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); list=ParseCommandOption(MagickListOptions,MagickFalse,argv[i]); if (list < 0) ThrowCompareException(OptionError,"UnrecognizedListType",argv[i]); status=MogrifyImageInfo(image_info,(int) (i-j+1),(const char **) argv+j,exception); DestroyCompare(); return(status == 0 ? MagickTrue : MagickFalse); } if (LocaleCompare("log",option+1) == 0) { if (*option == '+') break; i++; if ((i == (ssize_t) argc) || (strchr(argv[i],'%') == (char *) NULL)) ThrowCompareException(OptionError,"MissingArgument",option); break; } if (LocaleCompare("lowlight-color",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'm': { if (LocaleCompare("matte",option+1) == 0) break; if (LocaleCompare("metric",option+1) == 0) { ssize_t type; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); type=ParseCommandOption(MagickMetricOptions,MagickTrue,argv[i]); if (type < 0) ThrowCompareException(OptionError,"UnrecognizedMetricType", argv[i]); metric=(MetricType) type; break; } if (LocaleCompare("monitor",option+1) == 0) break; ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'p': { if (LocaleCompare("profile",option+1) == 0) { i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'q': { if (LocaleCompare("quality",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("quantize",option+1) == 0) { ssize_t colorspace; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); colorspace=ParseCommandOption(MagickColorspaceOptions, MagickFalse,argv[i]); if (colorspace < 0) ThrowCompareException(OptionError,"UnrecognizedColorspace", argv[i]); break; } if (LocaleCompare("quiet",option+1) == 0) break; ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'r': { if (LocaleCompare("regard-warnings",option+1) == 0) break; if (LocaleNCompare("respect-parentheses",option+1,17) == 0) { respect_parenthesis=(*option == '-') ? MagickTrue : MagickFalse; break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 's': { if (LocaleCompare("sampling-factor",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("seed",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("set",option+1) == 0) { i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } if (LocaleCompare("similarity-threshold",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); if (*option == '+') similarity_threshold=DefaultSimilarityThreshold; else similarity_threshold=StringToDouble(argv[i],(char **) NULL); break; } if (LocaleCompare("size",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); if (IsGeometry(argv[i]) == MagickFalse) ThrowCompareInvalidArgumentException(option,argv[i]); break; } if (LocaleCompare("subimage-search",option+1) == 0) { if (*option == '+') { subimage_search=MagickFalse; break; } subimage_search=MagickTrue; break; } if (LocaleCompare("synchronize",option+1) == 0) break; ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 't': { if (LocaleCompare("taint",option+1) == 0) break; if (LocaleCompare("transparent-color",option+1) == 0) { if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); break; } if (LocaleCompare("type",option+1) == 0) { ssize_t type; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); type=ParseCommandOption(MagickTypeOptions,MagickFalse,argv[i]); if (type < 0) ThrowCompareException(OptionError,"UnrecognizedImageType", argv[i]); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case 'v': { if (LocaleCompare("verbose",option+1) == 0) break; if ((LocaleCompare("version",option+1) == 0) || (LocaleCompare("-version",option+1) == 0)) { ListMagickVersion(stdout); break; } if (LocaleCompare("virtual-pixel",option+1) == 0) { ssize_t method; if (*option == '+') break; i++; if (i == (ssize_t) argc) ThrowCompareException(OptionError,"MissingArgument",option); method=ParseCommandOption(MagickVirtualPixelOptions,MagickFalse, argv[i]); if (method < 0) ThrowCompareException(OptionError, "UnrecognizedVirtualPixelMethod",argv[i]); break; } ThrowCompareException(OptionError,"UnrecognizedOption",option) } case '?': break; default: ThrowCompareException(OptionError,"UnrecognizedOption",option) } fire=(GetCommandOptionFlags(MagickCommandOptions,MagickFalse,option) & FireOptionFlag) == 0 ? MagickFalse : MagickTrue; if (fire != MagickFalse) FireImageStack(MagickTrue,MagickTrue,MagickTrue); } if (k != 0) ThrowCompareException(OptionError,"UnbalancedParenthesis",argv[i]); if (i-- != (ssize_t) (argc-1)) ThrowCompareException(OptionError,"MissingAnImageFilename",argv[i]); if ((image == (Image *) NULL) || (GetImageListLength(image) < 2)) ThrowCompareException(OptionError,"MissingAnImageFilename",argv[i]); FinalizeImageSettings(image_info,image,MagickTrue); if ((image == (Image *) NULL) || (GetImageListLength(image) < 2)) ThrowCompareException(OptionError,"MissingAnImageFilename",argv[i]); image=GetImageFromList(image,0); reconstruct_image=GetImageFromList(image,1); offset.x=0; offset.y=0; if (subimage_search != MagickFalse) { similarity_image=SimilarityImage(image,reconstruct_image,metric, similarity_threshold,&offset,&similarity_metric,exception); if (similarity_metric > dissimilarity_threshold) ThrowCompareException(ImageError,"ImagesTooDissimilar",image->filename); } if ((reconstruct_image->columns == image->columns) && (reconstruct_image->rows == image->rows)) difference_image=CompareImages(image,reconstruct_image,metric,&distortion, exception); else if (similarity_image == (Image *) NULL) { if (metric == PerceptualHashErrorMetric) difference_image=CompareImages(image,reconstruct_image,metric, &distortion,exception); else ThrowCompareException(OptionError,"ImageWidthsOrHeightsDiffer", image->filename); } else { Image *composite_image; /* Determine if reconstructed image is a subimage of the image. */ composite_image=CloneImage(image,0,0,MagickTrue,exception); if (composite_image == (Image *) NULL) difference_image=CompareImages(image,reconstruct_image,metric, &distortion,exception); else { Image *distort_image; RectangleInfo page; (void) CompositeImage(composite_image,reconstruct_image, CopyCompositeOp,MagickTrue,offset.x,offset.y,exception); difference_image=CompareImages(image,composite_image,metric, &distortion,exception); if (difference_image != (Image *) NULL) { difference_image->page.x=offset.x; difference_image->page.y=offset.y; } composite_image=DestroyImage(composite_image); page.width=reconstruct_image->columns; page.height=reconstruct_image->rows; page.x=offset.x; page.y=offset.y; distort_image=CropImage(image,&page,exception); if (distort_image != (Image *) NULL) { Image *sans_image; sans_image=CompareImages(distort_image,reconstruct_image,metric, &distortion,exception); distort_image=DestroyImage(distort_image); if (sans_image != (Image *) NULL) sans_image=DestroyImage(sans_image); } } if (difference_image != (Image *) NULL) { AppendImageToList(&difference_image,similarity_image); similarity_image=(Image *) NULL; } } if (difference_image == (Image *) NULL) status=0; else { if (image_info->verbose != MagickFalse) (void) SetImageColorMetric(image,reconstruct_image,exception); if (*difference_image->magick == '\0') (void) CopyMagickString(difference_image->magick,image->magick, MagickPathExtent); if (image_info->verbose == MagickFalse) { switch (metric) { case FuzzErrorMetric: case MeanAbsoluteErrorMetric: case MeanSquaredErrorMetric: case PeakAbsoluteErrorMetric: case RootMeanSquaredErrorMetric: { (void) FormatLocaleFile(stderr,"%g (%g)",QuantumRange*distortion, (double) distortion); break; } case AbsoluteErrorMetric: case NormalizedCrossCorrelationErrorMetric: case PeakSignalToNoiseRatioErrorMetric: case PerceptualHashErrorMetric: { (void) FormatLocaleFile(stderr,"%g",distortion); break; } case MeanErrorPerPixelErrorMetric: { (void) FormatLocaleFile(stderr,"%g (%g, %g)",distortion, image->error.normalized_mean_error, image->error.normalized_maximum_error); break; } case UndefinedErrorMetric: break; } if (subimage_search != MagickFalse) (void) FormatLocaleFile(stderr," @ %.20g,%.20g",(double) difference_image->page.x,(double) difference_image->page.y); } else { double *channel_distortion; channel_distortion=GetImageDistortions(image,reconstruct_image, metric,exception); (void) FormatLocaleFile(stderr,"Image: %s\n",image->filename); if ((reconstruct_image->columns != image->columns) || (reconstruct_image->rows != image->rows)) (void) FormatLocaleFile(stderr,"Offset: %.20g,%.20g\n",(double) difference_image->page.x,(double) difference_image->page.y); (void) FormatLocaleFile(stderr," Channel distortion: %s\n", CommandOptionToMnemonic(MagickMetricOptions,(ssize_t) metric)); switch (metric) { case FuzzErrorMetric: case MeanAbsoluteErrorMetric: case MeanSquaredErrorMetric: case PeakAbsoluteErrorMetric: case RootMeanSquaredErrorMetric: { switch (image->colorspace) { case RGBColorspace: default: { (void) FormatLocaleFile(stderr," red: %g (%g)\n", QuantumRange*channel_distortion[RedPixelChannel], channel_distortion[RedPixelChannel]); (void) FormatLocaleFile(stderr," green: %g (%g)\n", QuantumRange*channel_distortion[GreenPixelChannel], channel_distortion[GreenPixelChannel]); (void) FormatLocaleFile(stderr," blue: %g (%g)\n", QuantumRange*channel_distortion[BluePixelChannel], channel_distortion[BluePixelChannel]); if (image->alpha_trait != UndefinedPixelTrait) (void) FormatLocaleFile(stderr," alpha: %g (%g)\n", QuantumRange*channel_distortion[AlphaPixelChannel], channel_distortion[AlphaPixelChannel]); break; } case CMYKColorspace: { (void) FormatLocaleFile(stderr," cyan: %g (%g)\n", QuantumRange*channel_distortion[CyanPixelChannel], channel_distortion[CyanPixelChannel]); (void) FormatLocaleFile(stderr," magenta: %g (%g)\n", QuantumRange*channel_distortion[MagentaPixelChannel], channel_distortion[MagentaPixelChannel]); (void) FormatLocaleFile(stderr," yellow: %g (%g)\n", QuantumRange*channel_distortion[YellowPixelChannel], channel_distortion[YellowPixelChannel]); (void) FormatLocaleFile(stderr," black: %g (%g)\n", QuantumRange*channel_distortion[BlackPixelChannel], channel_distortion[BlackPixelChannel]); if (image->alpha_trait != UndefinedPixelTrait) (void) FormatLocaleFile(stderr," alpha: %g (%g)\n", QuantumRange*channel_distortion[AlphaPixelChannel], channel_distortion[AlphaPixelChannel]); break; } case GRAYColorspace: { (void) FormatLocaleFile(stderr," gray: %g (%g)\n", QuantumRange*channel_distortion[GrayPixelChannel], channel_distortion[GrayPixelChannel]); if (image->alpha_trait != UndefinedPixelTrait) (void) FormatLocaleFile(stderr," alpha: %g (%g)\n", QuantumRange*channel_distortion[AlphaPixelChannel], channel_distortion[AlphaPixelChannel]); break; } } (void) FormatLocaleFile(stderr," all: %g (%g)\n", QuantumRange*channel_distortion[MaxPixelChannels], channel_distortion[MaxPixelChannels]); break; } case AbsoluteErrorMetric: case NormalizedCrossCorrelationErrorMetric: case PeakSignalToNoiseRatioErrorMetric: case PerceptualHashErrorMetric: { switch (image->colorspace) { case RGBColorspace: default: { (void) FormatLocaleFile(stderr," red: %g\n", channel_distortion[RedPixelChannel]); (void) FormatLocaleFile(stderr," green: %g\n", channel_distortion[GreenPixelChannel]); (void) FormatLocaleFile(stderr," blue: %g\n", channel_distortion[BluePixelChannel]); if (image->alpha_trait != UndefinedPixelTrait) (void) FormatLocaleFile(stderr," alpha: %g\n", channel_distortion[AlphaPixelChannel]); break; } case CMYKColorspace: { (void) FormatLocaleFile(stderr," cyan: %g\n", channel_distortion[CyanPixelChannel]); (void) FormatLocaleFile(stderr," magenta: %g\n", channel_distortion[MagentaPixelChannel]); (void) FormatLocaleFile(stderr," yellow: %g\n", channel_distortion[YellowPixelChannel]); (void) FormatLocaleFile(stderr," black: %g\n", channel_distortion[BlackPixelChannel]); if (image->alpha_trait != UndefinedPixelTrait) (void) FormatLocaleFile(stderr," alpha: %g\n", channel_distortion[AlphaPixelChannel]); break; } case GRAYColorspace: { (void) FormatLocaleFile(stderr," gray: %g\n", channel_distortion[GrayPixelChannel]); if (image->alpha_trait != UndefinedPixelTrait) (void) FormatLocaleFile(stderr," alpha: %g\n", channel_distortion[AlphaPixelChannel]); break; } } (void) FormatLocaleFile(stderr," all: %g\n", channel_distortion[MaxPixelChannels]); break; } case MeanErrorPerPixelErrorMetric: { (void) FormatLocaleFile(stderr," %g (%g, %g)\n", channel_distortion[MaxPixelChannels], image->error.normalized_mean_error, image->error.normalized_maximum_error); break; } case UndefinedErrorMetric: break; } channel_distortion=(double *) RelinquishMagickMemory( channel_distortion); if (subimage_search != MagickFalse) (void) FormatLocaleFile(stderr," Offset: %.20g,%.20g\n",(double) difference_image->page.x,(double) difference_image->page.y); } status&=WriteImages(image_info,difference_image,argv[argc-1],exception); if ((metadata != (char **) NULL) && (format != (char *) NULL)) { char *text; text=InterpretImageProperties(image_info,difference_image,format, exception); if (text == (char *) NULL) ThrowCompareException(ResourceLimitError,"MemoryAllocationFailed", GetExceptionMessage(errno)); (void) ConcatenateString(&(*metadata),text); text=DestroyString(text); } difference_image=DestroyImageList(difference_image); } DestroyCompare(); if ((metric == NormalizedCrossCorrelationErrorMetric) || (metric == UndefinedErrorMetric)) { if (fabs(distortion-1.0) > CompareEpsilon) (void) SetImageOption(image_info,"compare:dissimilar","true"); } else if (fabs(distortion) > CompareEpsilon) (void) SetImageOption(image_info,"compare:dissimilar","true"); return(status != 0 ? MagickTrue : MagickFalse); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d L A B E L I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadLABELImage() reads a LABEL image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadLABELImage method is: % % Image *ReadLABELImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadLABELImage(const ImageInfo *image_info, ExceptionInfo *exception) { char geometry[MaxTextExtent], *property; const char *label; DrawInfo *draw_info; Image *image; MagickBooleanType status; TypeMetric metrics; unsigned long height, width; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info); (void) ResetImagePage(image,"0x0+0+0"); property=InterpretImageProperties(image_info,image,image_info->filename); (void) SetImageProperty(image,"label",property); property=DestroyString(property); label=GetImageProperty(image,"label"); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); draw_info->text=ConstantString(label); if (((image->columns != 0) || (image->rows != 0)) && (image_info->pointsize == 0.0)) { /* Fit label to canvas size. */ status=GetMultilineTypeMetrics(image,draw_info,&metrics); for ( ; status != MagickFalse; draw_info->pointsize*=2.0) { width=(unsigned long) (metrics.width+draw_info->stroke_width+0.5); height=(unsigned long) (metrics.height+draw_info->stroke_width+0.5); if (((image->columns != 0) && (width > (image->columns+1))) || ((image->rows != 0) && (height > (image->rows+1)))) break; status=GetMultilineTypeMetrics(image,draw_info,&metrics); } for ( ; status != MagickFalse; draw_info->pointsize--) { width=(unsigned long) (metrics.width+draw_info->stroke_width+0.5); height=(unsigned long) (metrics.height+draw_info->stroke_width+0.5); if ((image->columns != 0) && (width <= (image->columns+1)) && ((image->rows == 0) || (height <= (image->rows+1)))) break; if ((image->rows != 0) && (height <= (image->rows+1)) && ((image->columns == 0) || (width <= (image->columns+1)))) break; if (draw_info->pointsize < 2.0) break; status=GetMultilineTypeMetrics(image,draw_info,&metrics); } } status=GetMultilineTypeMetrics(image,draw_info,&metrics); if (status == MagickFalse) { InheritException(exception,&image->exception); image=DestroyImageList(image); return((Image *) NULL); } if (image->columns == 0) image->columns=(unsigned long) (metrics.width+draw_info->stroke_width+1.5); if (image->columns == 0) image->columns=(unsigned long) (draw_info->pointsize+ draw_info->stroke_width+1.5); if (draw_info->gravity == UndefinedGravity) { (void) FormatMagickString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ draw_info->stroke_width/2.0); draw_info->geometry=AcquireString(geometry); } if (image->rows == 0) image->rows=(unsigned long) (metrics.height+draw_info->stroke_width+0.5); if (image->rows == 0) image->rows=(unsigned long) (draw_info->pointsize+draw_info->stroke_width+ 0.5); if (SetImageBackgroundColor(image) == MagickFalse) { InheritException(exception,&image->exception); image=DestroyImageList(image); return((Image *) NULL); } (void) AnnotateImage(image,draw_info); draw_info=DestroyDrawInfo(draw_info); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadImage() reads an image or image sequence from a file or file handle. % The method returns a NULL if there is a memory shortage or if the image % cannot be read. On failure, a NULL image is returned and exception % describes the reason for the failure. % % The format of the ReadImage method is: % % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: Read the image defined by the file or filename members of % this structure. % % o exception: return any errors or warnings in this structure. % */ MagickExport Image *ReadImage(const ImageInfo *image_info, ExceptionInfo *exception) { char filename[MaxTextExtent], magick[MaxTextExtent], magick_filename[MaxTextExtent]; const char *value; const DelegateInfo *delegate_info; const MagickInfo *magick_info; ExceptionInfo *sans_exception; GeometryInfo geometry_info; Image *image, *next; ImageInfo *read_info; MagickStatusType flags, thread_support; PolicyDomain domain; PolicyRights rights; /* Determine image type from filename prefix or suffix (e.g. image.jpg). */ assert(image_info != (ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image_info->filename != (char *) NULL); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); read_info=CloneImageInfo(image_info); (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent); (void) SetImageInfo(read_info,MagickFalse,exception); (void) CopyMagickString(filename,read_info->filename,MaxTextExtent); (void) CopyMagickString(magick,read_info->magick,MaxTextExtent); domain=CoderPolicyDomain; rights=ReadPolicyRights; if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse) { errno=EPERM; (void) ThrowMagickException(exception,GetMagickModule(),PolicyError, "NotAuthorized","`%s'",read_info->filename); return((Image *) NULL); } /* Call appropriate image reader based on image type. */ sans_exception=AcquireExceptionInfo(); magick_info=GetMagickInfo(read_info->magick,sans_exception); sans_exception=DestroyExceptionInfo(sans_exception); if (magick_info != (const MagickInfo *) NULL) { if (GetMagickEndianSupport(magick_info) == MagickFalse) read_info->endian=UndefinedEndian; else if ((image_info->endian == UndefinedEndian) && (GetMagickRawSupport(magick_info) != MagickFalse)) { unsigned long lsb_first; lsb_first=1; read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian; } } if ((magick_info != (const MagickInfo *) NULL) && (GetMagickSeekableStream(magick_info) != MagickFalse)) { MagickBooleanType status; image=AcquireImage(read_info); (void) CopyMagickString(image->filename,read_info->filename, MaxTextExtent); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { read_info=DestroyImageInfo(read_info); image=DestroyImage(image); return((Image *) NULL); } if (IsBlobSeekable(image) == MagickFalse) { /* Coder requires a seekable stream. */ *read_info->filename='\0'; status=ImageToFile(image,read_info->filename,exception); if (status == MagickFalse) { (void) CloseBlob(image); read_info=DestroyImageInfo(read_info); image=DestroyImage(image); return((Image *) NULL); } read_info->temporary=MagickTrue; } (void) CloseBlob(image); image=DestroyImage(image); } image=NewImageList(); if (constitute_semaphore == (SemaphoreInfo *) NULL) AcquireSemaphoreInfo(&constitute_semaphore); if ((magick_info != (const MagickInfo *) NULL) && (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL)) { thread_support=GetMagickThreadSupport(magick_info); if ((thread_support & DecoderThreadSupport) == 0) LockSemaphoreInfo(constitute_semaphore); image=GetImageDecoder(magick_info)(read_info,exception); if ((thread_support & DecoderThreadSupport) == 0) UnlockSemaphoreInfo(constitute_semaphore); } else { delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception); if (delegate_info == (const DelegateInfo *) NULL) { if (IsPathAccessible(read_info->filename) != MagickFalse) (void) ThrowMagickException(exception,GetMagickModule(), MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'", read_info->filename); if (read_info->temporary != MagickFalse) (void) RelinquishUniqueFileResource(read_info->filename); read_info=DestroyImageInfo(read_info); return((Image *) NULL); } /* Let our decoding delegate process the image. */ image=AcquireImage(read_info); if (image == (Image *) NULL) { read_info=DestroyImageInfo(read_info); return((Image *) NULL); } (void) CopyMagickString(image->filename,read_info->filename, MaxTextExtent); *read_info->filename='\0'; if (GetDelegateThreadSupport(delegate_info) == MagickFalse) LockSemaphoreInfo(constitute_semaphore); (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL, exception); if (GetDelegateThreadSupport(delegate_info) == MagickFalse) UnlockSemaphoreInfo(constitute_semaphore); image=DestroyImageList(image); read_info->temporary=MagickTrue; (void) SetImageInfo(read_info,MagickFalse,exception); magick_info=GetMagickInfo(read_info->magick,exception); if ((magick_info == (const MagickInfo *) NULL) || (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL)) { if (IsPathAccessible(read_info->filename) != MagickFalse) (void) ThrowMagickException(exception,GetMagickModule(), MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'", read_info->filename); else ThrowFileException(exception,FileOpenError,"UnableToOpenFile", read_info->filename); read_info=DestroyImageInfo(read_info); return((Image *) NULL); } thread_support=GetMagickThreadSupport(magick_info); if ((thread_support & DecoderThreadSupport) == 0) LockSemaphoreInfo(constitute_semaphore); image=(Image *) (GetImageDecoder(magick_info))(read_info,exception); if ((thread_support & DecoderThreadSupport) == 0) UnlockSemaphoreInfo(constitute_semaphore); } if (read_info->temporary != MagickFalse) { (void) RelinquishUniqueFileResource(read_info->filename); read_info->temporary=MagickFalse; if (image != (Image *) NULL) (void) CopyMagickString(image->filename,filename,MaxTextExtent); } if (image == (Image *) NULL) { read_info=DestroyImageInfo(read_info); return(image); } if (exception->severity >= ErrorException) (void) LogMagickEvent(ExceptionEvent,GetMagickModule(), "Coder (%s) generated an image despite an error (%d), " "notify the developers",image->magick,exception->severity); if (IsBlobTemporary(image) != MagickFalse) (void) RelinquishUniqueFileResource(read_info->filename); if ((GetNextImageInList(image) != (Image *) NULL) && (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse)) { Image *clones; clones=CloneImages(image,read_info->scenes,exception); if (clones == (Image *) NULL) (void) ThrowMagickException(exception,GetMagickModule(),OptionError, "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename); else { image=DestroyImageList(image); image=GetFirstImageInList(clones); } } if (GetBlobError(image) != MagickFalse) { ThrowFileException(exception,FileOpenError, "AnErrorHasOccurredReadingFromFile",read_info->filename); image=DestroyImageList(image); read_info=DestroyImageInfo(read_info); return((Image *) NULL); } for (next=image; next != (Image *) NULL; next=GetNextImageInList(next)) { char timestamp[MaxTextExtent]; const StringInfo *profile; next->taint=MagickFalse; if (next->magick_columns == 0) next->magick_columns=next->columns; if (next->magick_rows == 0) next->magick_rows=next->rows; if ((LocaleCompare(magick,"HTTP") != 0) && (LocaleCompare(magick,"FTP") != 0)) (void) CopyMagickString(next->magick,magick,MaxTextExtent); (void) CopyMagickString(next->magick_filename,magick_filename, MaxTextExtent); if (IsBlobTemporary(image) != MagickFalse) (void) CopyMagickString(next->filename,filename,MaxTextExtent); value=GetImageProperty(next,"tiff:Orientation"); if (value == (char *) NULL) value=GetImageProperty(next,"exif:Orientation"); if (value != (char *) NULL) { next->orientation=(OrientationType) StringToLong(value); (void) DeleteImageProperty(next,"tiff:Orientation"); (void) DeleteImageProperty(next,"exif:Orientation"); } value=GetImageProperty(next,"tiff:XResolution"); if (value == (char *) NULL) value=GetImageProperty(next,"exif:XResolution"); if (value != (char *) NULL) { geometry_info.rho=next->x_resolution; geometry_info.sigma=1.0; flags=ParseGeometry(value,&geometry_info); if (geometry_info.sigma != 0) next->x_resolution=geometry_info.rho/geometry_info.sigma; (void) DeleteImageProperty(next,"exif:XResolution"); (void) DeleteImageProperty(next,"tiff:XResolution"); } value=GetImageProperty(next,"tiff:YResolution"); if (value == (char *) NULL) value=GetImageProperty(next,"exif:YResolution"); if (value != (char *) NULL) { geometry_info.rho=next->y_resolution; geometry_info.sigma=1.0; flags=ParseGeometry(value,&geometry_info); if (geometry_info.sigma != 0) next->y_resolution=geometry_info.rho/geometry_info.sigma; (void) DeleteImageProperty(next,"exif:YResolution"); (void) DeleteImageProperty(next,"tiff:YResolution"); } value=GetImageProperty(next,"tiff:ResolutionUnit"); if (value == (char *) NULL) value=GetImageProperty(next,"exif:ResolutionUnit"); if (value != (char *) NULL) { next->units=(ResolutionType) (StringToLong(value)-1); (void) DeleteImageProperty(next,"exif:ResolutionUnit"); (void) DeleteImageProperty(next,"tiff:ResolutionUnit"); } if (next->page.width == 0) next->page.width=next->columns; if (next->page.height == 0) next->page.height=next->rows; if (*read_info->filename != '\0') { char *property; const char *option; option=GetImageOption(read_info,"caption"); if (option != (const char *) NULL) { property=InterpretImageProperties(read_info,next,option); (void) SetImageProperty(next,"caption",property); property=DestroyString(property); } option=GetImageOption(read_info,"comment"); if (option != (const char *) NULL) { property=InterpretImageProperties(read_info,next,option); (void) SetImageProperty(next,"comment",property); property=DestroyString(property); } option=GetImageOption(read_info,"label"); if (option != (const char *) NULL) { property=InterpretImageProperties(read_info,next,option); (void) SetImageProperty(next,"label",property); property=DestroyString(property); } } if (LocaleCompare(next->magick,"TEXT") == 0) (void) ParseAbsoluteGeometry("0x0+0+0",&next->page); if ((read_info->extract != (char *) NULL) && (read_info->stream == (StreamHandler) NULL)) { RectangleInfo geometry; flags=ParseAbsoluteGeometry(read_info->extract,&geometry); if ((next->columns != geometry.width) || (next->rows != geometry.height)) { if (((flags & XValue) != 0) || ((flags & YValue) != 0)) { Image *crop_image; crop_image=CropImage(next,&geometry,exception); if (crop_image != (Image *) NULL) ReplaceImageInList(&next,crop_image); } else if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0)) { Image *size_image; flags=ParseRegionGeometry(next,read_info->extract,&geometry, exception); size_image=ResizeImage(next,geometry.width,geometry.height, next->filter,next->blur,exception); if (size_image != (Image *) NULL) ReplaceImageInList(&next,size_image); } } } profile=GetImageProfile(next,"icc"); if (profile == (const StringInfo *) NULL) profile=GetImageProfile(next,"icm"); if (profile != (const StringInfo *) NULL) { next->color_profile.length=GetStringInfoLength(profile); next->color_profile.info=GetStringInfoDatum(profile); } profile=GetImageProfile(next,"iptc"); if (profile == (const StringInfo *) NULL) profile=GetImageProfile(next,"8bim"); if (profile != (const StringInfo *) NULL) { next->iptc_profile.length=GetStringInfoLength(profile); next->iptc_profile.info=GetStringInfoDatum(profile); } (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent, timestamp); (void) SetImageProperty(next,"date:modify",timestamp); (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent, timestamp); (void) SetImageProperty(next,"date:create",timestamp); if (read_info->verbose != MagickFalse) (void) IdentifyImage(next,stdout,MagickFalse); image=next; } read_info=DestroyImageInfo(read_info); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e I N F O I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteINFOImage writes the pixel values as text numbers. % % The format of the WriteINFOImage method is: % % MagickBooleanType WriteINFOImage(const ImageInfo *image_info, % Image *image) % % A description of each parameter follows. % % o image_info: the image info. % % o image: The image. % */ static MagickBooleanType WriteINFOImage(const ImageInfo *image_info, Image *image) { const char *format; MagickBooleanType status; MagickOffsetType scene; /* Open output image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); if (image->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBlobMode,&image->exception); if (status == MagickFalse) return(status); scene=0; do { format=GetImageOption(image_info,"format"); if (format == (char *) NULL) { (void) CopyMagickString(image->filename,image->magick_filename, MaxTextExtent); image->magick_columns=image->columns; image->magick_rows=image->rows; (void) IdentifyImage(image,GetBlobFileHandle(image), image_info->verbose); } else { char *text; text=InterpretImageProperties(image_info,image,format); if (text != (char *) NULL) { (void) WriteBlobString(image,text); (void) WriteBlobString(image,"\n"); text=DestroyString(text); } } if (GetNextImageInList(image) == (Image *) NULL) break; image=SyncNextImageInList(image); status=SetImageProgress(image,SaveImagesTag,scene++, GetImageListLength(image)); if (status == MagickFalse) break; } while (image_info->adjoin != MagickFalse); (void) CloseBlob(image); return(MagickTrue); }
MagickExport Image *MontageImageList(const ImageInfo *image_info, const MontageInfo *montage_info,const Image *images,ExceptionInfo *exception) { #define MontageImageTag "Montage/Image" #define TileImageTag "Tile/Image" char tile_geometry[MagickPathExtent], *title; const char *value; DrawInfo *draw_info; FrameInfo frame_info; Image *image, **image_list, **master_list, *montage, *texture, *tile_image, *thumbnail; ImageInfo *clone_info; MagickBooleanType concatenate, proceed, status; MagickOffsetType tiles; MagickProgressMonitor progress_monitor; MagickStatusType flags; register ssize_t i; RectangleInfo bounds, geometry, extract_info; size_t bevel_width, border_width, extent, height, images_per_page, max_height, number_images, number_lines, sans, tiles_per_column, tiles_per_page, tiles_per_row, title_offset, total_tiles, width; ssize_t tile, x, x_offset, y, y_offset; TypeMetric metrics; /* Create image tiles. */ assert(images != (Image *) NULL); assert(images->signature == MagickCoreSignature); if (images->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); assert(montage_info != (MontageInfo *) NULL); assert(montage_info->signature == MagickCoreSignature); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickCoreSignature); number_images=GetImageListLength(images); master_list=ImageListToArray(images,exception); image_list=master_list; image=image_list[0]; if (master_list == (Image **) NULL) ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); thumbnail=NewImageList(); for (i=0; i < (ssize_t) number_images; i++) { image=CloneImage(image_list[i],0,0,MagickTrue,exception); if (image == (Image *) NULL) break; (void) ParseAbsoluteGeometry("0x0+0+0",&image->page); progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL, image->client_data); flags=ParseRegionGeometry(image,montage_info->geometry,&geometry,exception); thumbnail=ThumbnailImage(image,geometry.width,geometry.height,exception); if (thumbnail == (Image *) NULL) break; image_list[i]=thumbnail; (void) SetImageProgressMonitor(image,progress_monitor,image->client_data); proceed=SetImageProgress(image,TileImageTag,(MagickOffsetType) i, number_images); if (proceed == MagickFalse) break; image=DestroyImage(image); } if (i < (ssize_t) number_images) { if (thumbnail == (Image *) NULL) i--; for (tile=0; (ssize_t) tile <= i; tile++) if (image_list[tile] != (Image *) NULL) image_list[tile]=DestroyImage(image_list[tile]); master_list=(Image **) RelinquishMagickMemory(master_list); return((Image *) NULL); } /* Sort image list by increasing tile number. */ for (i=0; i < (ssize_t) number_images; i++) if (image_list[i]->scene == 0) break; if (i == (ssize_t) number_images) qsort((void *) image_list,(size_t) number_images,sizeof(*image_list), SceneCompare); /* Determine tiles per row and column. */ tiles_per_column=(size_t) sqrt((double) number_images); tiles_per_row=(size_t) ceil((double) number_images/tiles_per_column); x_offset=0; y_offset=0; if (montage_info->tile != (char *) NULL) GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset, &tiles_per_column,&tiles_per_row); /* Determine tile sizes. */ concatenate=MagickFalse; SetGeometry(image_list[0],&extract_info); extract_info.x=(ssize_t) montage_info->border_width; extract_info.y=(ssize_t) montage_info->border_width; if (montage_info->geometry != (char *) NULL) { /* Initialize tile geometry. */ flags=GetGeometry(montage_info->geometry,&extract_info.x,&extract_info.y, &extract_info.width,&extract_info.height); concatenate=((flags & RhoValue) == 0) && ((flags & SigmaValue) == 0) ? MagickTrue : MagickFalse; } border_width=montage_info->border_width; bevel_width=0; (void) ResetMagickMemory(&frame_info,0,sizeof(frame_info)); if (montage_info->frame != (char *) NULL) { char absolute_geometry[MagickPathExtent]; frame_info.width=extract_info.width; frame_info.height=extract_info.height; (void) FormatLocaleString(absolute_geometry,MagickPathExtent,"%s!", montage_info->frame); flags=ParseMetaGeometry(absolute_geometry,&frame_info.outer_bevel, &frame_info.inner_bevel,&frame_info.width,&frame_info.height); if ((flags & HeightValue) == 0) frame_info.height=frame_info.width; if ((flags & XiValue) == 0) frame_info.outer_bevel=(ssize_t) frame_info.width/2; if ((flags & PsiValue) == 0) frame_info.inner_bevel=frame_info.outer_bevel; frame_info.x=(ssize_t) frame_info.width; frame_info.y=(ssize_t) frame_info.height; bevel_width=(size_t) MagickMax(frame_info.inner_bevel, frame_info.outer_bevel); border_width=(size_t) MagickMax((ssize_t) frame_info.width, (ssize_t) frame_info.height); } for (i=0; i < (ssize_t) number_images; i++) { if (image_list[i]->columns > extract_info.width) extract_info.width=image_list[i]->columns; if (image_list[i]->rows > extract_info.height) extract_info.height=image_list[i]->rows; } /* Initialize draw attributes. */ clone_info=CloneImageInfo(image_info); clone_info->background_color=montage_info->background_color; clone_info->border_color=montage_info->border_color; draw_info=CloneDrawInfo(clone_info,(DrawInfo *) NULL); if (montage_info->font != (char *) NULL) (void) CloneString(&draw_info->font,montage_info->font); if (montage_info->pointsize != 0.0) draw_info->pointsize=montage_info->pointsize; draw_info->gravity=CenterGravity; draw_info->stroke=montage_info->stroke; draw_info->fill=montage_info->fill; draw_info->text=AcquireString(""); (void) GetTypeMetrics(image_list[0],draw_info,&metrics,exception); texture=NewImageList(); if (montage_info->texture != (char *) NULL) { (void) CopyMagickString(clone_info->filename,montage_info->texture, MagickPathExtent); texture=ReadImage(clone_info,exception); } /* Determine the number of lines in an next label. */ title=InterpretImageProperties(clone_info,image_list[0],montage_info->title, exception); title_offset=0; if (montage_info->title != (char *) NULL) title_offset=(size_t) (2*(metrics.ascent-metrics.descent)* MultilineCensus(title)+2*extract_info.y); number_lines=0; for (i=0; i < (ssize_t) number_images; i++) { value=GetImageProperty(image_list[i],"label",exception); if (value == (const char *) NULL) continue; if (MultilineCensus(value) > number_lines) number_lines=MultilineCensus(value); } /* Allocate next structure. */ tile_image=AcquireImage((ImageInfo *) NULL,exception); montage=AcquireImage(clone_info,exception); montage->background_color=montage_info->background_color; montage->scene=0; images_per_page=(number_images-1)/(tiles_per_row*tiles_per_column)+1; tiles=0; total_tiles=(size_t) number_images; for (i=0; i < (ssize_t) images_per_page; i++) { /* Determine bounding box. */ tiles_per_page=tiles_per_row*tiles_per_column; x_offset=0; y_offset=0; if (montage_info->tile != (char *) NULL) GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset, &sans,&sans); tiles_per_page=tiles_per_row*tiles_per_column; y_offset+=(ssize_t) title_offset; max_height=0; bounds.width=0; bounds.height=0; width=0; for (tile=0; tile < (ssize_t) tiles_per_page; tile++) { if (tile < (ssize_t) number_images) { width=concatenate != MagickFalse ? image_list[tile]->columns : extract_info.width; if (image_list[tile]->rows > max_height) max_height=image_list[tile]->rows; } x_offset+=(ssize_t) (width+2*(extract_info.x+border_width)); if (x_offset > (ssize_t) bounds.width) bounds.width=(size_t) x_offset; if (((tile+1) == (ssize_t) tiles_per_page) || (((tile+1) % tiles_per_row) == 0)) { x_offset=0; if (montage_info->tile != (char *) NULL) GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y, &sans,&sans); height=concatenate != MagickFalse ? max_height : extract_info.height; y_offset+=(ssize_t) (height+(extract_info.y+(ssize_t) border_width)*2+ (metrics.ascent-metrics.descent+4)*number_lines+ (montage_info->shadow != MagickFalse ? 4 : 0)); if (y_offset > (ssize_t) bounds.height) bounds.height=(size_t) y_offset; max_height=0; } } if (montage_info->shadow != MagickFalse) bounds.width+=4; /* Initialize montage image. */ (void) CopyMagickString(montage->filename,montage_info->filename, MagickPathExtent); montage->columns=(size_t) MagickMax((ssize_t) bounds.width,1); montage->rows=(size_t) MagickMax((ssize_t) bounds.height,1); (void) SetImageBackgroundColor(montage,exception); /* Set montage geometry. */ montage->montage=AcquireString((char *) NULL); tile=0; extent=1; while (tile < MagickMin((ssize_t) tiles_per_page,(ssize_t) number_images)) { extent+=strlen(image_list[tile]->filename)+1; tile++; } montage->directory=(char *) AcquireQuantumMemory(extent, sizeof(*montage->directory)); if ((montage->montage == (char *) NULL) || (montage->directory == (char *) NULL)) ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); x_offset=0; y_offset=0; if (montage_info->tile != (char *) NULL) GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset, &sans,&sans); y_offset+=(ssize_t) title_offset; (void) FormatLocaleString(montage->montage,MagickPathExtent, "%.20gx%.20g%+.20g%+.20g",(double) (extract_info.width+ (extract_info.x+border_width)*2),(double) (extract_info.height+ (extract_info.y+border_width)*2+(double) ((metrics.ascent- metrics.descent+4)*number_lines+(montage_info->shadow != MagickFalse ? 4 : 0))),(double) x_offset,(double) y_offset); *montage->directory='\0'; tile=0; while (tile < MagickMin((ssize_t) tiles_per_page,(ssize_t) number_images)) { (void) ConcatenateMagickString(montage->directory, image_list[tile]->filename,extent); (void) ConcatenateMagickString(montage->directory,"\n",extent); tile++; } progress_monitor=SetImageProgressMonitor(montage,(MagickProgressMonitor) NULL,montage->client_data); if (texture != (Image *) NULL) (void) TextureImage(montage,texture,exception); if (montage_info->title != (char *) NULL) { char geometry[MagickPathExtent]; DrawInfo *clone_info; TypeMetric metrics; /* Annotate composite image with title. */ clone_info=CloneDrawInfo(image_info,draw_info); clone_info->gravity=CenterGravity; clone_info->pointsize*=2.0; (void) GetTypeMetrics(image_list[0],clone_info,&metrics,exception); (void) FormatLocaleString(geometry,MagickPathExtent, "%.20gx%.20g%+.20g%+.20g",(double) montage->columns,(double) (metrics.ascent-metrics.descent),0.0,(double) extract_info.y+4); (void) CloneString(&clone_info->geometry,geometry); (void) CloneString(&clone_info->text,title); (void) AnnotateImage(montage,clone_info,exception); clone_info=DestroyDrawInfo(clone_info); } (void) SetImageProgressMonitor(montage,progress_monitor, montage->client_data); /* Copy tile to the composite. */ x_offset=0; y_offset=0; if (montage_info->tile != (char *) NULL) GetMontageGeometry(montage_info->tile,number_images,&x_offset,&y_offset, &sans,&sans); x_offset+=extract_info.x; y_offset+=(ssize_t) title_offset+extract_info.y; max_height=0; status=MagickTrue; for (tile=0; tile < MagickMin((ssize_t) tiles_per_page,(ssize_t) number_images); tile++) { /* Copy this tile to the composite. */ image=CloneImage(image_list[tile],0,0,MagickTrue,exception); progress_monitor=SetImageProgressMonitor(image, (MagickProgressMonitor) NULL,image->client_data); width=concatenate != MagickFalse ? image->columns : extract_info.width; if (image->rows > max_height) max_height=image->rows; height=concatenate != MagickFalse ? max_height : extract_info.height; if (border_width != 0) { Image *border_image; RectangleInfo border_info; /* Put a border around the image. */ border_info.width=border_width; border_info.height=border_width; if (montage_info->frame != (char *) NULL) { border_info.width=(width-image->columns+1)/2; border_info.height=(height-image->rows+1)/2; } border_image=BorderImage(image,&border_info,image->compose,exception); if (border_image != (Image *) NULL) { image=DestroyImage(image); image=border_image; } if ((montage_info->frame != (char *) NULL) && (image->compose == DstOutCompositeOp)) { (void) SetPixelChannelMask(image,AlphaChannel); (void) NegateImage(image,MagickFalse,exception); (void) SetPixelChannelMask(image,DefaultChannels); } } /* Gravitate as specified by the tile gravity. */ tile_image->columns=width; tile_image->rows=height; tile_image->gravity=montage_info->gravity; if (image->gravity != UndefinedGravity) tile_image->gravity=image->gravity; (void) FormatLocaleString(tile_geometry,MagickPathExtent,"%.20gx%.20g+0+0", (double) image->columns,(double) image->rows); flags=ParseGravityGeometry(tile_image,tile_geometry,&geometry,exception); x=(ssize_t) (geometry.x+border_width); y=(ssize_t) (geometry.y+border_width); if ((montage_info->frame != (char *) NULL) && (bevel_width != 0)) { FrameInfo extract_info; Image *frame_image; /* Put an ornamental border around this tile. */ extract_info=frame_info; extract_info.width=width+2*frame_info.width; extract_info.height=height+2*frame_info.height; value=GetImageProperty(image,"label",exception); if (value != (const char *) NULL) extract_info.height+=(size_t) ((metrics.ascent-metrics.descent+4)* MultilineCensus(value)); frame_image=FrameImage(image,&extract_info,image->compose,exception); if (frame_image != (Image *) NULL) { image=DestroyImage(image); image=frame_image; } x=0; y=0; } if (LocaleCompare(image->magick,"NULL") != 0) { /* Composite background with tile. */ if (montage_info->shadow != MagickFalse) { Image *shadow_image; /* Shadow image. */ (void) QueryColorCompliance("#0000",AllCompliance, &image->background_color,exception); shadow_image=ShadowImage(image,80.0,2.0,5,5,exception); if (shadow_image != (Image *) NULL) { (void) CompositeImage(shadow_image,image,OverCompositeOp, MagickTrue,0,0,exception); image=DestroyImage(image); image=shadow_image; } } (void) CompositeImage(montage,image,image->compose,MagickTrue, x_offset+x,y_offset+y,exception); value=GetImageProperty(image,"label",exception); if (value != (const char *) NULL) { char geometry[MagickPathExtent]; /* Annotate composite tile with label. */ (void) FormatLocaleString(geometry,MagickPathExtent, "%.20gx%.20g%+.20g%+.20g",(double) ((montage_info->frame ? image->columns : width)-2*border_width),(double) (metrics.ascent-metrics.descent+4)*MultilineCensus(value), (double) (x_offset+border_width),(double) ((montage_info->frame ? y_offset+height+border_width+4 : y_offset+extract_info.height+border_width+ (montage_info->shadow != MagickFalse ? 4 : 0))+bevel_width)); (void) CloneString(&draw_info->geometry,geometry); (void) CloneString(&draw_info->text,value); (void) AnnotateImage(montage,draw_info,exception); } } x_offset+=(ssize_t) (width+2*(extract_info.x+border_width)); if (((tile+1) == (ssize_t) tiles_per_page) || (((tile+1) % tiles_per_row) == 0)) { x_offset=extract_info.x; y_offset+=(ssize_t) (height+(extract_info.y+border_width)*2+ (metrics.ascent-metrics.descent+4)*number_lines+ (montage_info->shadow != MagickFalse ? 4 : 0)); max_height=0; } if (images->progress_monitor != (MagickProgressMonitor) NULL) { MagickBooleanType proceed; proceed=SetImageProgress(image,MontageImageTag,tiles,total_tiles); if (proceed == MagickFalse) status=MagickFalse; } image_list[tile]=DestroyImage(image_list[tile]); image=DestroyImage(image); tiles++; } (void) status; if ((i+1) < (ssize_t) images_per_page) { /* Allocate next image structure. */ AcquireNextImage(clone_info,montage,exception); if (GetNextImageInList(montage) == (Image *) NULL) { montage=DestroyImageList(montage); return((Image *) NULL); } montage=GetNextImageInList(montage); montage->background_color=montage_info->background_color; image_list+=tiles_per_page; number_images-=tiles_per_page; } } tile_image=DestroyImage(tile_image); if (texture != (Image *) NULL) texture=DestroyImage(texture); title=DestroyString(title); master_list=(Image **) RelinquishMagickMemory(master_list); draw_info=DestroyDrawInfo(draw_info); clone_info=DestroyImageInfo(clone_info); return(GetFirstImageInList(montage)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d C A P T I O N I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadCAPTIONImage() reads a CAPTION image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadCAPTIONImage method is: % % Image *ReadCAPTIONImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadCAPTIONImage(const ImageInfo *image_info, ExceptionInfo *exception) { char *caption, geometry[MaxTextExtent], *property, *text; const char *gravity, *option; DrawInfo *draw_info; Image *image; MagickBooleanType split, status; register ssize_t i; size_t height, width; TypeMetric metrics; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info); (void) ResetImagePage(image,"0x0+0+0"); /* Format caption. */ option=GetImageOption(image_info,"filename"); if (option == (const char *) NULL) property=InterpretImageProperties(image_info,image,image_info->filename); else if (LocaleNCompare(option,"caption:",8) == 0) property=InterpretImageProperties(image_info,image,option+8); else property=InterpretImageProperties(image_info,image,option); (void) SetImageProperty(image,"caption",property); property=DestroyString(property); caption=ConstantString(GetImageProperty(image,"caption")); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); (void) CloneString(&draw_info->text,caption); gravity=GetImageOption(image_info,"gravity"); if (gravity != (char *) NULL) draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions, MagickFalse,gravity); split=MagickFalse; if (image->columns == 0) { text=AcquireString(caption); i=FormatMagickCaption(image,draw_info,split,&metrics,&text); (void) CloneString(&draw_info->text,text); text=DestroyString(text); (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); image->columns=width; } if (image->rows == 0) { split=MagickTrue; text=AcquireString(caption); i=FormatMagickCaption(image,draw_info,split,&metrics,&text); (void) CloneString(&draw_info->text,text); text=DestroyString(text); (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+ draw_info->interline_spacing+draw_info->stroke_width)+0.5); } if (fabs(image_info->pointsize) < MagickEpsilon) { double high, low; /* Auto fit text into bounding box. */ for ( ; ; draw_info->pointsize*=2.0) { text=AcquireString(caption); i=FormatMagickCaption(image,draw_info,split,&metrics,&text); (void) CloneString(&draw_info->text,text); text=DestroyString(text); (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); (void) status; width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((image->columns != 0) && (image->rows != 0)) { if ((width >= image->columns) && (height >= image->rows)) break; } else if (((image->columns != 0) && (width >= image->columns)) || ((image->rows != 0) && (height >= image->rows))) break; } high=draw_info->pointsize; for (low=1.0; (high-low) > 0.5; ) { draw_info->pointsize=(low+high)/2.0; text=AcquireString(caption); i=FormatMagickCaption(image,draw_info,split,&metrics,&text); (void) CloneString(&draw_info->text,text); text=DestroyString(text); (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((image->columns != 0) && (image->rows != 0)) { if ((width < image->columns) && (height < image->rows)) low=draw_info->pointsize+0.5; else high=draw_info->pointsize-0.5; } else if (((image->columns != 0) && (width < image->columns)) || ((image->rows != 0) && (height < image->rows))) low=draw_info->pointsize+0.5; else high=draw_info->pointsize-0.5; } draw_info->pointsize=(low+high)/2.0-0.5; } (void) CloneString(&draw_info->text,caption); i=FormatMagickCaption(image,draw_info,split,&metrics,&caption); if (SetImageBackgroundColor(image) == MagickFalse) { InheritException(exception,&image->exception); image=DestroyImageList(image); return((Image *) NULL); } /* Draw caption. */ (void) CloneString(&draw_info->text,caption); status=GetMultilineTypeMetrics(image,draw_info,&metrics); if ((draw_info->gravity != UndefinedGravity) && (draw_info->direction != RightToLeftDirection)) image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0); else { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ draw_info->stroke_width/2.0); if (draw_info->direction == RightToLeftDirection) (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), metrics.ascent+draw_info->stroke_width/2.0); draw_info->geometry=AcquireString(geometry); } status=AnnotateImage(image,draw_info); draw_info=DestroyDrawInfo(draw_info); caption=DestroyString(caption); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d L A B E L I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadLABELImage() reads a LABEL image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadLABELImage method is: % % Image *ReadLABELImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadLABELImage(const ImageInfo *image_info, ExceptionInfo *exception) { char geometry[MaxTextExtent], *property; const char *label; DrawInfo *draw_info; Image *image; MagickBooleanType status; TypeMetric metrics; size_t height, width; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info); (void) ResetImagePage(image,"0x0+0+0"); property=InterpretImageProperties(image_info,image,image_info->filename); (void) SetImageProperty(image,"label",property); property=DestroyString(property); label=GetImageProperty(image,"label"); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); draw_info->text=ConstantString(label); status=GetMultilineTypeMetrics(image,draw_info,&metrics); if (image->columns == 0) { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); (void) status; image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); } if (image->rows == 0) { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); } if (image_info->pointsize == 0.0) { double high, low; /* Auto fit text into bounding box. */ for ( ; ; draw_info->pointsize*=2.0) { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); (void) status; width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((width >= image->columns) && (height >= image->rows)) break; if ((width >= (image->columns << 1)) || (height >= (image->rows << 1))) break; } high=draw_info->pointsize/2.0; for (low=high/2.0; (high-low) > 1.0; ) { draw_info->pointsize=(low+high)/2.0; (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((width <= image->columns) && (height <= image->rows)) low=draw_info->pointsize+1.0; else high=draw_info->pointsize-1.0; } for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; ) { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((width <= image->columns) && (height <= image->rows)) break; draw_info->pointsize--; } draw_info->pointsize=floor(draw_info->pointsize+0.5); } status=GetMultilineTypeMetrics(image,draw_info,&metrics); if (status == MagickFalse) { InheritException(exception,&image->exception); image=DestroyImageList(image); return((Image *) NULL); } if (draw_info->gravity == UndefinedGravity) { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ draw_info->stroke_width/2.0); (void) CloneString(&draw_info->geometry,geometry); } if (draw_info->direction == RightToLeftDirection) { if (draw_info->direction == RightToLeftDirection) (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), metrics.ascent+draw_info->stroke_width/2.0); (void) CloneString(&draw_info->geometry,geometry); } if (SetImageBackgroundColor(image) == MagickFalse) { InheritException(exception,&image->exception); image=DestroyImageList(image); return((Image *) NULL); } (void) AnnotateImage(image,draw_info); if (image_info->pointsize == 0.0) { char pointsize[MaxTextExtent]; (void) FormatLocaleString(pointsize,MaxTextExtent,"%.20g", draw_info->pointsize); (void) SetImageProperty(image,"label:pointsize",pointsize); } draw_info=DestroyDrawInfo(draw_info); return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d L A B E L I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadLABELImage() reads a LABEL image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadLABELImage method is: % % Image *ReadLABELImage(const ImageInfo *image_info, % ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadLABELImage(const ImageInfo *image_info, ExceptionInfo *exception) { char geometry[MagickPathExtent], *property; const char *label; DrawInfo *draw_info; Image *image; MagickBooleanType status; TypeMetric metrics; size_t height, width; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickCoreSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickCoreSignature); image=AcquireImage(image_info,exception); (void) ResetImagePage(image,"0x0+0+0"); property=InterpretImageProperties((ImageInfo *) image_info,image, image_info->filename,exception); (void) SetImageProperty(image,"label",property,exception); property=DestroyString(property); label=GetImageProperty(image,"label",exception); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); draw_info->text=ConstantString(label); metrics.width=0; metrics.ascent=0.0; status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); if ((image->columns == 0) && (image->rows == 0)) { image->columns=(size_t) (metrics.width+draw_info->stroke_width+0.5); image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); } else if (((image->columns == 0) || (image->rows == 0)) || (fabs(image_info->pointsize) < MagickEpsilon)) { double high, low; /* Auto fit text into bounding box. */ for ( ; ; draw_info->pointsize*=2.0) { (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((image->columns != 0) && (image->rows != 0)) { if ((width >= image->columns) && (height >= image->rows)) break; } else if (((image->columns != 0) && (width >= image->columns)) || ((image->rows != 0) && (height >= image->rows))) break; } high=draw_info->pointsize; for (low=1.0; (high-low) > 0.5; ) { draw_info->pointsize=(low+high)/2.0; (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((image->columns != 0) && (image->rows != 0)) { if ((width < image->columns) && (height < image->rows)) low=draw_info->pointsize+0.5; else high=draw_info->pointsize-0.5; } else if (((image->columns != 0) && (width < image->columns)) || ((image->rows != 0) && (height < image->rows))) low=draw_info->pointsize+0.5; else high=draw_info->pointsize-0.5; } draw_info->pointsize=(low+high)/2.0-0.5; } status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); if (status == MagickFalse) { draw_info=DestroyDrawInfo(draw_info); image=DestroyImageList(image); return((Image *) NULL); } if (image->columns == 0) image->columns=(size_t) (metrics.width+draw_info->stroke_width+0.5); if (image->columns == 0) image->columns=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5); if (image->rows == 0) image->rows=(size_t) (metrics.ascent-metrics.descent+ draw_info->stroke_width+0.5); if (image->rows == 0) image->rows=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5); status=SetImageExtent(image,image->columns,image->rows,exception); if (status == MagickFalse) { draw_info=DestroyDrawInfo(draw_info); return(DestroyImageList(image)); } if (SetImageBackgroundColor(image,exception) == MagickFalse) { draw_info=DestroyDrawInfo(draw_info); image=DestroyImageList(image); return((Image *) NULL); } /* Draw label. */ (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g", draw_info->direction == RightToLeftDirection ? image->columns- metrics.bounds.x2 : 0.0,draw_info->gravity == UndefinedGravity ? metrics.ascent : 0.0); draw_info->geometry=AcquireString(geometry); status=AnnotateImage(image,draw_info,exception); if (image_info->pointsize == 0.0) { char pointsize[MagickPathExtent]; (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g", draw_info->pointsize); (void) SetImageProperty(image,"label:pointsize",pointsize,exception); } draw_info=DestroyDrawInfo(draw_info); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } return(GetFirstImageInList(image)); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d V I D I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadVIDImage reads one of more images and creates a Visual Image % Directory file. It allocates the memory necessary for the new Image % structure and returns a pointer to the new image. % % The format of the ReadVIDImage method is: % % Image *ReadVIDImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadVIDImage(const ImageInfo *image_info,ExceptionInfo *exception) { #define ClientName "montage" char **filelist, *label; Image *image, *images, *montage_image, *next_image, *thumbnail_image; ImageInfo *read_info; int number_files; MagickBooleanType status; MontageInfo *montage_info; RectangleInfo geometry; register ssize_t i; /* Expand the filename. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info); filelist=(char **) AcquireMagickMemory(sizeof(*filelist)); if (filelist == (char **) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); filelist[0]=ConstantString(image_info->filename); number_files=1; status=ExpandFilenames(&number_files,&filelist); if ((status == MagickFalse) || (number_files == 0)) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); image=DestroyImage(image); /* Read each image and convert them to a tile. */ images=NewImageList(); read_info=CloneImageInfo(image_info); SetImageInfoBlob(read_info,(void *) NULL,0); (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL, (void *) NULL); if (read_info->size == (char *) NULL) (void) CloneString(&read_info->size,DefaultTileGeometry); for (i=0; i < (ssize_t) number_files; i++) { if (image_info->debug != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(),"name: %s", filelist[i]); (void) CopyMagickString(read_info->filename,filelist[i],MaxTextExtent); filelist[i]=DestroyString(filelist[i]); *read_info->magick='\0'; next_image=ReadImage(read_info,exception); CatchException(exception); if (next_image == (Image *) NULL) break; label=InterpretImageProperties(image_info,next_image,DefaultTileLabel); (void) SetImageProperty(next_image,"label",label); label=DestroyString(label); if (image_info->debug != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "geometry: %.20gx%.20g",(double) next_image->columns,(double) next_image->rows); SetGeometry(next_image,&geometry); (void) ParseMetaGeometry(read_info->size,&geometry.x,&geometry.y, &geometry.width,&geometry.height); thumbnail_image=ThumbnailImage(next_image,geometry.width,geometry.height, exception); if (thumbnail_image != (Image *) NULL) { next_image=DestroyImage(next_image); next_image=thumbnail_image; } if (image_info->debug != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(), "thumbnail geometry: %.20gx%.20g",(double) next_image->columns,(double) next_image->rows); AppendImageToList(&images,next_image); status=SetImageProgress(images,LoadImagesTag,i,number_files); if (status == MagickFalse) break; } read_info=DestroyImageInfo(read_info); filelist=(char **) RelinquishMagickMemory(filelist); if (images == (Image *) NULL) ThrowReaderException(CorruptImageError, "ImageFileDoesNotContainAnyImageData"); /* Create the visual image directory. */ montage_info=CloneMontageInfo(image_info,(MontageInfo *) NULL); if (image_info->debug != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(),"creating montage"); montage_image=MontageImageList(image_info,montage_info, GetFirstImageInList(images),exception); montage_info=DestroyMontageInfo(montage_info); images=DestroyImageList(images); return(montage_image); }
WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info,int argc, char **argv,char **metadata,ExceptionInfo *exception) { MagickCLI *cli_wand; size_t len; assert(image_info != (ImageInfo *) NULL); /* For specific OS command line requirements */ ReadCommandlLine(argc,&argv); /* Initialize special "CLI Wand" to hold images and settings (empty) */ cli_wand=AcquireMagickCLI(image_info,exception); cli_wand->location="Initializing"; cli_wand->filename=argv[0]; cli_wand->line=1; if (cli_wand->wand.debug != MagickFalse) (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(), "\"%s\"",argv[0]); GetPathComponent(argv[0],TailPath,cli_wand->wand.name); SetClientName(cli_wand->wand.name); ConcatenateMagickString(cli_wand->wand.name,"-CLI",MagickPathExtent); len=strlen(argv[0]); /* precaution */ /* "convert" command - give a "deprecated" warning" */ if (len>=7 && LocaleCompare("convert",argv[0]+len-7) == 0) { cli_wand->process_flags = ConvertCommandOptionFlags; (void) FormatLocaleFile(stderr,"WARNING: %s\n", "The convert command is deprecated in IMv7, use \"magick\"\n"); } /* Special Case: If command name ends with "script" implied "-script" */ if (len>=6 && LocaleCompare("script",argv[0]+len-6) == 0) { if (argc >= 2 && ( (*(argv[1]) != '-') || (strlen(argv[1]) == 1) )) { GetPathComponent(argv[1],TailPath,cli_wand->wand.name); ProcessScriptOptions(cli_wand,argv[1],argc,argv,2); goto Magick_Command_Cleanup; } } /* Special Case: Version Information and Abort */ if (argc == 2) { if ((LocaleCompare("-version",argv[1]) == 0) || /* GNU standard option */ (LocaleCompare("--version",argv[1]) == 0) ) { /* just version */ CLIOption(cli_wand, "-version"); goto Magick_Command_Exit; } if ((LocaleCompare("-help",argv[1]) == 0) || /* GNU standard option */ (LocaleCompare("--help",argv[1]) == 0) ) { /* just a brief summary */ if (cli_wand->wand.debug != MagickFalse) (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(), "- Special Option \"%s\"", argv[1]); MagickUsage(MagickFalse); goto Magick_Command_Exit; } if (LocaleCompare("-usage",argv[1]) == 0) { /* both version & usage */ if (cli_wand->wand.debug != MagickFalse) (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(), "- Special Option \"%s\"", argv[1]); CLIOption(cli_wand, "-version" ); MagickUsage(MagickTrue); goto Magick_Command_Exit; } } /* not enough arguments -- including -help */ if (argc < 3) { (void) FormatLocaleFile(stderr, "Error: Invalid argument or not enough arguments\n\n"); MagickUsage(MagickFalse); goto Magick_Command_Exit; } /* Special "concatenate option (hidden) for delegate usage */ if (LocaleCompare("-concatenate",argv[1]) == 0) { if (cli_wand->wand.debug != MagickFalse) (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(), "- Special Option \"%s\"", argv[1]); ConcatenateImages(argc,argv,exception); goto Magick_Command_Exit; } /* List Information and Abort */ if (argc == 3 && LocaleCompare("-list",argv[1]) == 0) { CLIOption(cli_wand, argv[1], argv[2]); goto Magick_Command_Exit; } /* ------------- */ /* The Main Call */ if (LocaleCompare("-script",argv[1]) == 0) { /* Start processing directly from script, no pre-script options Replace wand command name with script name First argument in the argv array is the script name to read. */ GetPathComponent(argv[2],TailPath,cli_wand->wand.name); ProcessScriptOptions(cli_wand,argv[2],argc,argv,3); } else { /* Normal Command Line, assumes output file as last option */ ProcessCommandOptions(cli_wand,argc,argv,1); } /* ------------- */ Magick_Command_Cleanup: cli_wand->location="Cleanup"; cli_wand->filename=argv[0]; if (cli_wand->wand.debug != MagickFalse) (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(), "\"%s\"",argv[0]); /* recover original image_info and clean up stacks FUTURE: "-reset stacks" option */ while ((cli_wand->image_list_stack != (Stack *) NULL) && (cli_wand->image_list_stack->next != (Stack *) NULL)) CLIOption(cli_wand,")"); while ((cli_wand->image_info_stack != (Stack *) NULL) && (cli_wand->image_info_stack->next != (Stack *) NULL)) CLIOption(cli_wand,"}"); /* assert we have recovered the original structures */ assert(cli_wand->wand.image_info == image_info); assert(cli_wand->wand.exception == exception); /* Handle metadata for ImageMagickObject COM object for Windows VBS */ if (metadata != (char **) NULL) { const char *format; char *text; format="%w,%h,%m"; // Get this from image_info Option splaytree text=InterpretImageProperties(image_info,cli_wand->wand.images,format, exception); if (text == (char *) NULL) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError, "MemoryAllocationFailed","`%s'", GetExceptionMessage(errno)); else { (void) ConcatenateString(&(*metadata),text); text=DestroyString(text); } } Magick_Command_Exit: cli_wand->location="Exiting"; cli_wand->filename=argv[0]; if (cli_wand->wand.debug != MagickFalse) (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(), "\"%s\"",argv[0]); /* Destroy the special CLI Wand */ cli_wand->wand.image_info = (ImageInfo *) NULL; /* not these */ cli_wand->wand.exception = (ExceptionInfo *) NULL; cli_wand=DestroyMagickCLI(cli_wand); return(exception->severity < ErrorException ? MagickTrue : MagickFalse); }
static Image *ReadCAPTIONImage(const ImageInfo *image_info, ExceptionInfo *exception) { char *caption, geometry[MaxTextExtent], *property; const char *gravity; DrawInfo *draw_info; Image *image; MagickBooleanType status; register ssize_t i; size_t height, width; TypeMetric metrics; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info); if (image->columns == 0) ThrowReaderException(OptionError,"MustSpecifyImageSize"); (void) ResetImagePage(image,"0x0+0+0"); /* Format caption. */ property=InterpretImageProperties(image_info,image,image_info->filename); (void) SetImageProperty(image,"caption",property); property=DestroyString(property); caption=ConstantString(GetImageProperty(image,"caption")); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); draw_info->text=ConstantString(caption); gravity=GetImageOption(image_info,"gravity"); if (gravity != (char *) NULL) draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions, MagickFalse,gravity); if ((*caption != '\0') && (image->rows != 0) && (image_info->pointsize == 0.0)) { char *text; /* Scale text to fit bounding box. */ for ( ; ; ) { text=AcquireString(caption); i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text); (void) CloneString(&draw_info->text,text); text=DestroyString(text); (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); (void) status; width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((width > (image->columns+1)) || (height > (image->rows+1))) break; draw_info->pointsize*=2.0; } draw_info->pointsize/=2.0; for ( ; ; ) { text=AcquireString(caption); i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text); (void) CloneString(&draw_info->text,text); text=DestroyString(text); (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1,metrics.ascent); if (draw_info->gravity == UndefinedGravity) (void) CloneString(&draw_info->geometry,geometry); status=GetMultilineTypeMetrics(image,draw_info,&metrics); width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); if ((width > (image->columns+1)) || (height > (image->rows+1))) break; draw_info->pointsize++; } draw_info->pointsize--; } i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption); if (image->rows == 0) image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+ draw_info->interline_spacing+draw_info->stroke_width)+0.5); if (image->rows == 0) image->rows=(size_t) ((i+1)*draw_info->pointsize+ draw_info->interline_spacing+draw_info->stroke_width+0.5); if (SetImageBackgroundColor(image) == MagickFalse) { InheritException(exception,&image->exception); image=DestroyImageList(image); return((Image *) NULL); } /* Draw caption. */ (void) CloneString(&draw_info->text,caption); status=GetMultilineTypeMetrics(image,draw_info,&metrics); if ((draw_info->gravity != UndefinedGravity) && (draw_info->direction != RightToLeftDirection)) image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0); else { (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ draw_info->stroke_width/2.0); if (draw_info->direction == RightToLeftDirection) (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), metrics.ascent+draw_info->stroke_width/2.0); draw_info->geometry=AcquireString(geometry); } (void) AnnotateImage(image,draw_info); draw_info=DestroyDrawInfo(draw_info); caption=DestroyString(caption); return(GetFirstImageInList(image)); }
static Image *ReadCAPTIONImage(const ImageInfo *image_info, ExceptionInfo *exception) { char *caption, *property; const char *option; DrawInfo *draw_info; FT_Bitmap *canvas; Image *image; PangoAlignment align; PangoContext *context; PangoFontDescription *description; PangoFontMap *fontmap; PangoGravity gravity; PangoLayout *layout; PangoRectangle extent; PixelPacket fill_color; RectangleInfo page; register PixelPacket *q; register unsigned char *p; ssize_t y; /* Initialize Image structure. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info); (void) ResetImagePage(image,"0x0+0+0"); /* Get context. */ fontmap=(PangoFontMap *) pango_ft2_font_map_new(); pango_ft2_font_map_set_resolution((PangoFT2FontMap *) fontmap, image->x_resolution,image->y_resolution); option=GetImageOption(image_info,"caption:hinting"); pango_ft2_font_map_set_default_substitute((PangoFT2FontMap *) fontmap, PangoSubstitute,(char *) option,NULL); context=pango_font_map_create_context(fontmap); option=GetImageOption(image_info,"caption:language"); if (option != (const char *) NULL) pango_context_set_language(context,pango_language_from_string(option)); draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); pango_context_set_base_dir(context,draw_info->direction == RightToLeftDirection ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR); switch (draw_info->gravity) { case NorthGravity: gravity=PANGO_GRAVITY_NORTH; break; case WestGravity: gravity=PANGO_GRAVITY_WEST; break; case EastGravity: gravity=PANGO_GRAVITY_EAST; break; case SouthGravity: gravity=PANGO_GRAVITY_SOUTH; break; default: gravity=PANGO_GRAVITY_AUTO; break; } pango_context_set_base_gravity(context,gravity); option=GetImageOption(image_info,"caption:gravity-hint"); if (option != (const char *) NULL) { if (LocaleCompare(option,"line") == 0) pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_LINE); if (LocaleCompare(option,"natural") == 0) pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_NATURAL); if (LocaleCompare(option,"strong") == 0) pango_context_set_gravity_hint(context,PANGO_GRAVITY_HINT_STRONG); } /* Configure layout. */ layout=pango_layout_new(context); option=GetImageOption(image_info,"caption:auto-dir"); if (option != (const char *) NULL) pango_layout_set_auto_dir(layout,1); option=GetImageOption(image_info,"caption:ellipsize"); if (option != (const char *) NULL) { if (LocaleCompare(option,"end") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_END); if (LocaleCompare(option,"middle") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_MIDDLE); if (LocaleCompare(option,"none") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_NONE); if (LocaleCompare(option,"start") == 0) pango_layout_set_ellipsize(layout,PANGO_ELLIPSIZE_START); } option=GetImageOption(image_info,"caption:justify"); if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) pango_layout_set_justify(layout,1); option=GetImageOption(image_info,"caption:single-paragraph"); if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) pango_layout_set_single_paragraph_mode(layout,1); option=GetImageOption(image_info,"caption:wrap"); if (option != (const char *) NULL) { if (LocaleCompare(option,"char") == 0) pango_layout_set_wrap(layout,PANGO_WRAP_CHAR); if (LocaleCompare(option,"word") == 0) pango_layout_set_wrap(layout,PANGO_WRAP_WORD); if (LocaleCompare(option,"word-char") == 0) pango_layout_set_wrap(layout,PANGO_WRAP_WORD_CHAR); } option=GetImageOption(image_info,"caption:indent"); if (option != (const char *) NULL) pango_layout_set_indent(layout,(StringToLong(option)*image->x_resolution* PANGO_SCALE+36)/72); switch (draw_info->align) { case CenterAlign: align=PANGO_ALIGN_CENTER; break; case RightAlign: align=PANGO_ALIGN_RIGHT; break; case LeftAlign: default: align=PANGO_ALIGN_LEFT; break; } if ((align != PANGO_ALIGN_CENTER) && (draw_info->direction == RightToLeftDirection)) align=(PangoAlignment) (PANGO_ALIGN_LEFT+PANGO_ALIGN_RIGHT-align); pango_layout_set_alignment(layout,align); description=pango_font_description_from_string(draw_info->font == (char *) NULL ? "helvetica" : draw_info->font); pango_font_description_set_size(description,PANGO_SCALE*draw_info->pointsize); pango_layout_set_font_description(layout,description); pango_font_description_free(description); property=InterpretImageProperties(image_info,image,image_info->filename); (void) SetImageProperty(image,"caption",property); property=DestroyString(property); caption=ConstantString(GetImageProperty(image,"caption")); /* Render caption. */ option=GetImageOption(image_info,"caption:markup"); if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) pango_layout_set_markup(layout,caption,-1); else pango_layout_set_text(layout,caption,-1); pango_layout_context_changed(layout); page.x=0; page.y=0; if (image_info->page != (char *) NULL) (void) ParseAbsoluteGeometry(image_info->page,&page); if (image->columns == 0) { pango_layout_get_pixel_extents(layout,NULL,&extent); image->columns=extent.x+extent.width; } else { image->columns-=2*page.x; pango_layout_set_width(layout,(PANGO_SCALE*image->columns* image->x_resolution+36.0)/72.0); } if (image->rows == 0) { pango_layout_get_pixel_extents(layout,NULL,&extent); image->rows=extent.y+extent.height; } else { image->rows-=2*page.y; pango_layout_set_height(layout,(PANGO_SCALE*image->rows* image->y_resolution+36.0)/72.0); } /* Create canvas. */ canvas=(FT_Bitmap *) AcquireMagickMemory(sizeof(*canvas)); if (canvas == (FT_Bitmap *) NULL) { draw_info=DestroyDrawInfo(draw_info); ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } canvas->width=image->columns; canvas->pitch=(canvas->width+3) & ~3; canvas->rows=image->rows; canvas->buffer=(unsigned char *) AcquireQuantumMemory(canvas->pitch, canvas->rows*sizeof(*canvas->buffer)); if (canvas->buffer == (unsigned char *) NULL) { draw_info=DestroyDrawInfo(draw_info); canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } canvas->num_grays=256; canvas->pixel_mode=ft_pixel_mode_grays; ResetMagickMemory(canvas->buffer,0x00,canvas->pitch*canvas->rows); pango_ft2_render_layout(canvas,layout,0,0); /* Convert caption to image. */ image->columns+=2*page.x; image->rows+=2*page.y; if (SetImageBackgroundColor(image) == MagickFalse) { draw_info=DestroyDrawInfo(draw_info); canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer); canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); caption=DestroyString(caption); image=DestroyImageList(image); return((Image *) NULL); } p=canvas->buffer; for (y=page.y; y < (ssize_t) (image->rows-page.y); y++) { register ssize_t x; q=GetAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (PixelPacket *) NULL) break; q+=page.x; for (x=page.x; x < (ssize_t) (image->columns-page.x); x++) { MagickRealType fill_opacity; (void) GetFillColor(draw_info,x,y,&fill_color); fill_opacity=QuantumRange-(*p)/canvas->num_grays*(QuantumRange- fill_color.opacity); if (draw_info->text_antialias == MagickFalse) fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0; MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q); p++; q++; } for ( ; x < (ssize_t) ((canvas->width+3) & ~3); x++) p++; } /* Relinquish resources. */ draw_info=DestroyDrawInfo(draw_info); canvas->buffer=(unsigned char *) RelinquishMagickMemory(canvas->buffer); canvas=(FT_Bitmap *) RelinquishMagickMemory(canvas); caption=DestroyString(caption); return(GetFirstImageInList(image)); }