2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % LLLLL A A BBBB EEEEE LLLLL %
13 % Read ASCII String As An Image. %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/annotate.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/draw.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/property.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/module.h"
60 #include "MagickCore/utility.h"
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 % R e a d L A B E L I m a g e %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % ReadLABELImage() reads a LABEL image file and returns it. It
74 % allocates the memory necessary for the new Image structure and returns a
75 % pointer to the new image.
77 % The format of the ReadLABELImage method is:
79 % Image *ReadLABELImage(const ImageInfo *image_info,
80 % ExceptionInfo *exception)
82 % A description of each parameter follows:
84 % o image_info: the image info.
86 % o exception: return any errors or warnings in this structure.
89 static Image *ReadLABELImage(const ImageInfo *image_info,
90 ExceptionInfo *exception)
93 geometry[MagickPathExtent],
116 Initialize Image structure.
118 assert(image_info != (const ImageInfo *) NULL);
119 assert(image_info->signature == MagickCoreSignature);
120 if (image_info->debug != MagickFalse)
121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
122 image_info->filename);
123 assert(exception != (ExceptionInfo *) NULL);
124 assert(exception->signature == MagickCoreSignature);
125 image=AcquireImage(image_info,exception);
126 (void) ResetImagePage(image,"0x0+0+0");
127 property=InterpretImageProperties((ImageInfo *) image_info,image,
128 image_info->filename,exception);
129 (void) SetImageProperty(image,"label",property,exception);
130 property=DestroyString(property);
131 label=GetImageProperty(image,"label",exception);
132 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
133 draw_info->text=ConstantString(label);
136 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
137 if ((image->columns == 0) && (image->rows == 0))
139 image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
140 image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
143 if ((strlen(label) > 0) &&
144 (((image->columns == 0) || (image->rows == 0)) ||
145 (fabs(image_info->pointsize) < MagickEpsilon)))
152 Auto fit text into bounding box.
154 for ( ; ; draw_info->pointsize*=2.0)
156 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
157 -metrics.bounds.x1,metrics.ascent);
158 if (draw_info->gravity == UndefinedGravity)
159 (void) CloneString(&draw_info->geometry,geometry);
160 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
161 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
162 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
163 if ((image->columns != 0) && (image->rows != 0))
165 if ((width >= image->columns) && (height >= image->rows))
169 if (((image->columns != 0) && (width >= image->columns)) ||
170 ((image->rows != 0) && (height >= image->rows)))
173 high=draw_info->pointsize;
174 for (low=1.0; (high-low) > 0.5; )
176 draw_info->pointsize=(low+high)/2.0;
177 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
178 -metrics.bounds.x1,metrics.ascent);
179 if (draw_info->gravity == UndefinedGravity)
180 (void) CloneString(&draw_info->geometry,geometry);
181 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
182 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
183 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
184 if ((image->columns != 0) && (image->rows != 0))
186 if ((width < image->columns) && (height < image->rows))
187 low=draw_info->pointsize+0.5;
189 high=draw_info->pointsize-0.5;
192 if (((image->columns != 0) && (width < image->columns)) ||
193 ((image->rows != 0) && (height < image->rows)))
194 low=draw_info->pointsize+0.5;
196 high=draw_info->pointsize-0.5;
198 draw_info->pointsize=(low+high)/2.0-0.5;
200 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
201 if (status == MagickFalse)
203 draw_info=DestroyDrawInfo(draw_info);
204 image=DestroyImageList(image);
205 return((Image *) NULL);
207 if (image->columns == 0)
208 image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
209 if (image->columns == 0)
210 image->columns=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
212 if (image->rows == 0)
213 image->rows=(size_t) floor(metrics.ascent-metrics.descent+
214 draw_info->stroke_width+0.5);
215 if (image->rows == 0)
216 image->rows=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
218 status=SetImageExtent(image,image->columns,image->rows,exception);
219 if (status == MagickFalse)
221 draw_info=DestroyDrawInfo(draw_info);
222 return(DestroyImageList(image));
224 if (SetImageBackgroundColor(image,exception) == MagickFalse)
226 draw_info=DestroyDrawInfo(draw_info);
227 image=DestroyImageList(image);
228 return((Image *) NULL);
233 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
234 draw_info->direction == RightToLeftDirection ? image->columns-
235 metrics.bounds.x2 : 0.0,draw_info->gravity == UndefinedGravity ?
236 metrics.ascent : 0.0);
237 (void) CloneString(&draw_info->geometry,geometry);
238 status=AnnotateImage(image,draw_info,exception);
239 if (image_info->pointsize == 0.0)
242 pointsize[MagickPathExtent];
244 (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g",
245 draw_info->pointsize);
246 (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
248 draw_info=DestroyDrawInfo(draw_info);
249 if (status == MagickFalse)
251 image=DestroyImageList(image);
252 return((Image *) NULL);
254 return(GetFirstImageInList(image));
258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % R e g i s t e r L A B E L I m a g e %
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 % RegisterLABELImage() adds properties for the LABEL image format to
269 % the list of supported formats. The properties include the image format
270 % tag, a method to read and/or write the format, whether the format
271 % supports the saving of more than one frame to the same file or blob,
272 % whether the format supports native in-memory I/O, and a brief
273 % description of the format.
275 % The format of the RegisterLABELImage method is:
277 % size_t RegisterLABELImage(void)
280 ModuleExport size_t RegisterLABELImage(void)
285 entry=AcquireMagickInfo("LABEL","LABEL","Image label");
286 entry->decoder=(DecodeImageHandler *) ReadLABELImage;
287 entry->flags^=CoderAdjoinFlag;
288 entry->format_type=ImplicitFormatType;
289 (void) RegisterMagickInfo(entry);
290 return(MagickImageCoderSignature);
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % U n r e g i s t e r L A B E L I m a g e %
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 % UnregisterLABELImage() removes format registrations made by the
305 % LABEL module from the list of supported formats.
307 % The format of the UnregisterLABELImage method is:
309 % UnregisterLABELImage(void)
312 ModuleExport void UnregisterLABELImage(void)
314 (void) UnregisterMagickInfo("LABEL");