2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % LLLLL A A BBBB EEEEE LLLLL %
13 % Read ASCII String As An Image. %
20 % Copyright 1999-2018 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/resource_.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #include "MagickCore/utility.h"
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 % R e a d L A B E L I m a g e %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % ReadLABELImage() reads a LABEL image file and returns it. It
75 % allocates the memory necessary for the new Image structure and returns a
76 % pointer to the new image.
78 % The format of the ReadLABELImage method is:
80 % Image *ReadLABELImage(const ImageInfo *image_info,
81 % ExceptionInfo *exception)
83 % A description of each parameter follows:
85 % o image_info: the image info.
87 % o exception: return any errors or warnings in this structure.
90 static Image *ReadLABELImage(const ImageInfo *image_info,
91 ExceptionInfo *exception)
94 geometry[MagickPathExtent],
114 Initialize Image structure.
116 assert(image_info != (const ImageInfo *) NULL);
117 assert(image_info->signature == MagickCoreSignature);
118 if (image_info->debug != MagickFalse)
119 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
120 image_info->filename);
121 assert(exception != (ExceptionInfo *) NULL);
122 assert(exception->signature == MagickCoreSignature);
123 image=AcquireImage(image_info,exception);
124 (void) ResetImagePage(image,"0x0+0+0");
125 if ((image->columns != 0) && (image->rows != 0))
126 (void) SetImageBackgroundColor(image,exception);
127 label=InterpretImageProperties((ImageInfo *) image_info,image,
128 image_info->filename,exception);
129 if (label == (char *) NULL)
130 return(DestroyImageList(image));
131 (void) SetImageProperty(image,"label",label,exception);
132 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
133 width=(size_t) floor(draw_info->pointsize*strlen(label)+0.5);
134 if (AcquireMagickResource(WidthResource,width) == MagickFalse)
136 label=DestroyString(label);
137 draw_info=DestroyDrawInfo(draw_info);
138 ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
140 draw_info->text=ConstantString(label);
144 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
145 if ((image->columns == 0) && (image->rows == 0))
147 image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
148 image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
151 if ((status != MagickFalse) && (strlen(label) > 0) &&
152 (((image->columns == 0) || (image->rows == 0)) ||
153 (fabs(image_info->pointsize) < MagickEpsilon)))
163 Auto fit text into bounding box.
165 for (n=0; n < 32; n++, draw_info->pointsize*=2.0)
167 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
168 -metrics.bounds.x1,metrics.ascent);
169 if (draw_info->gravity == UndefinedGravity)
170 (void) CloneString(&draw_info->geometry,geometry);
171 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
172 if (status == MagickFalse)
174 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
175 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
176 if ((image->columns != 0) && (image->rows != 0))
178 if ((width >= image->columns) && (height >= image->rows))
182 if (((image->columns != 0) && (width >= image->columns)) ||
183 ((image->rows != 0) && (height >= image->rows)))
186 if (status == MagickFalse)
188 label=DestroyString(label);
189 draw_info=DestroyDrawInfo(draw_info);
190 image=DestroyImageList(image);
191 return((Image *) NULL);
193 high=draw_info->pointsize;
194 for (low=1.0; (high-low) > 0.5; )
196 draw_info->pointsize=(low+high)/2.0;
197 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
198 -metrics.bounds.x1,metrics.ascent);
199 if (draw_info->gravity == UndefinedGravity)
200 (void) CloneString(&draw_info->geometry,geometry);
201 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
202 if (status == MagickFalse)
204 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
205 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
206 if ((image->columns != 0) && (image->rows != 0))
208 if ((width < image->columns) && (height < image->rows))
209 low=draw_info->pointsize+0.5;
211 high=draw_info->pointsize-0.5;
214 if (((image->columns != 0) && (width < image->columns)) ||
215 ((image->rows != 0) && (height < image->rows)))
216 low=draw_info->pointsize+0.5;
218 high=draw_info->pointsize-0.5;
220 if (status != MagickFalse)
222 draw_info->pointsize=(low+high)/2.0-0.5;
223 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
226 label=DestroyString(label);
227 if (status == MagickFalse)
229 draw_info=DestroyDrawInfo(draw_info);
230 image=DestroyImageList(image);
231 return((Image *) NULL);
233 if (image->columns == 0)
234 image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
235 if (image->columns == 0)
236 image->columns=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
238 if (image->rows == 0)
239 image->rows=(size_t) floor(metrics.ascent-metrics.descent+
240 draw_info->stroke_width+0.5);
241 if (image->rows == 0)
242 image->rows=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
244 status=SetImageExtent(image,image->columns,image->rows,exception);
245 if (status == MagickFalse)
247 draw_info=DestroyDrawInfo(draw_info);
248 return(DestroyImageList(image));
250 if (SetImageBackgroundColor(image,exception) == MagickFalse)
252 draw_info=DestroyDrawInfo(draw_info);
253 image=DestroyImageList(image);
254 return((Image *) NULL);
259 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
260 draw_info->direction == RightToLeftDirection ? (double) image->columns-
261 metrics.bounds.x2 : 0.0,draw_info->gravity == UndefinedGravity ?
262 MagickMax(metrics.ascent,metrics.bounds.y2) : 0.0);
263 (void) CloneString(&draw_info->geometry,geometry);
264 status=AnnotateImage(image,draw_info,exception);
265 if (image_info->pointsize == 0.0)
268 pointsize[MagickPathExtent];
270 (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g",
271 draw_info->pointsize);
272 (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
274 draw_info=DestroyDrawInfo(draw_info);
275 if (status == MagickFalse)
277 image=DestroyImageList(image);
278 return((Image *) NULL);
280 return(GetFirstImageInList(image));
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % R e g i s t e r L A B E L I m a g e %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % RegisterLABELImage() adds properties for the LABEL image format to
295 % the list of supported formats. The properties include the image format
296 % tag, a method to read and/or write the format, whether the format
297 % supports the saving of more than one frame to the same file or blob,
298 % whether the format supports native in-memory I/O, and a brief
299 % description of the format.
301 % The format of the RegisterLABELImage method is:
303 % size_t RegisterLABELImage(void)
306 ModuleExport size_t RegisterLABELImage(void)
311 entry=AcquireMagickInfo("LABEL","LABEL","Image label");
312 entry->decoder=(DecodeImageHandler *) ReadLABELImage;
313 entry->flags^=CoderAdjoinFlag;
314 entry->format_type=ImplicitFormatType;
315 (void) RegisterMagickInfo(entry);
316 return(MagickImageCoderSignature);
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 % U n r e g i s t e r L A B E L I m a g e %
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % UnregisterLABELImage() removes format registrations made by the
331 % LABEL module from the list of supported formats.
333 % The format of the UnregisterLABELImage method is:
335 % UnregisterLABELImage(void)
338 ModuleExport void UnregisterLABELImage(void)
340 (void) UnregisterMagickInfo("LABEL");