1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                     L       AAA   BBBB   EEEEE  L                           %
7 %                     L      A   A  B   B  E      L                           %
8 %                     L      AAAAA  BBBB   EEE    L                           %
9 %                     L      A   A  B   B  E      L                           %
10 %                     LLLLL  A   A  BBBB   EEEEE  LLLLL                       %
11 %                                                                             %
12 %                                                                             %
13 %                      Read ASCII String As An Image.                         %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://imagemagick.org/script/license.php                               %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 
39 /*
40   Include declarations.
41 */
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"
62 
63 /*
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %                                                                             %
66 %                                                                             %
67 %                                                                             %
68 %   R e a d L A B E L I m a g e                                               %
69 %                                                                             %
70 %                                                                             %
71 %                                                                             %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %
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.
77 %
78 %  The format of the ReadLABELImage method is:
79 %
80 %      Image *ReadLABELImage(const ImageInfo *image_info,
81 %        ExceptionInfo *exception)
82 %
83 %  A description of each parameter follows:
84 %
85 %    o image_info: the image info.
86 %
87 %    o exception: return any errors or warnings in this structure.
88 %
89 */
ReadLABELImage(const ImageInfo * image_info,ExceptionInfo * exception)90 static Image *ReadLABELImage(const ImageInfo *image_info,
91   ExceptionInfo *exception)
92 {
93   char
94     geometry[MagickPathExtent],
95     *label;
96 
97   DrawInfo
98     *draw_info;
99 
100   Image
101     *image;
102 
103   MagickBooleanType
104     status;
105 
106   TypeMetric
107     metrics;
108 
109   size_t
110     height,
111     width;
112 
113   /*
114     Initialize Image structure.
115   */
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     {
127       status=SetImageExtent(image,image->columns,image->rows,exception);
128       if (status == MagickFalse)
129         return(DestroyImageList(image));
130       (void) SetImageBackgroundColor(image,exception);
131     }
132   label=InterpretImageProperties((ImageInfo *) image_info,image,
133     image_info->filename,exception);
134   if (label == (char *) NULL)
135     return(DestroyImageList(image));
136   (void) SetImageProperty(image,"label",label,exception);
137   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
138   width=(size_t) floor(draw_info->pointsize*strlen(label)+0.5);
139   if (AcquireMagickResource(WidthResource,width) == MagickFalse)
140     {
141       label=DestroyString(label);
142       draw_info=DestroyDrawInfo(draw_info);
143       ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
144     }
145   draw_info->text=ConstantString(label);
146   (void) memset(&metrics,0,sizeof(metrics));
147   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
148   if ((image->columns == 0) && (image->rows == 0))
149     {
150       image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
151       image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
152     }
153   else
154     if ((status != MagickFalse) && (strlen(label) > 0) &&
155         (((image->columns == 0) || (image->rows == 0)) ||
156          (fabs(image_info->pointsize) < MagickEpsilon)))
157       {
158         double
159           high,
160           low;
161 
162         ssize_t
163           n;
164 
165         /*
166           Auto fit text into bounding box.
167         */
168         for (n=0; n < 32; n++, draw_info->pointsize*=2.0)
169         {
170           (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
171             -metrics.bounds.x1,metrics.ascent);
172           if (draw_info->gravity == UndefinedGravity)
173             (void) CloneString(&draw_info->geometry,geometry);
174           status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
175           if (status == MagickFalse)
176             break;
177           width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
178           height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
179           if ((image->columns != 0) && (image->rows != 0))
180             {
181               if ((width >= image->columns) && (height >= image->rows))
182                 break;
183             }
184           else
185             if (((image->columns != 0) && (width >= image->columns)) ||
186                 ((image->rows != 0) && (height >= image->rows)))
187               break;
188         }
189         if (status == MagickFalse)
190           {
191             label=DestroyString(label);
192             draw_info=DestroyDrawInfo(draw_info);
193             image=DestroyImageList(image);
194             return((Image *) NULL);
195           }
196         high=draw_info->pointsize;
197         for (low=1.0; (high-low) > 0.5; )
198         {
199           draw_info->pointsize=(low+high)/2.0;
200           (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
201             -metrics.bounds.x1,metrics.ascent);
202           if (draw_info->gravity == UndefinedGravity)
203             (void) CloneString(&draw_info->geometry,geometry);
204           status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
205           if (status == MagickFalse)
206             break;
207           width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
208           height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
209           if ((image->columns != 0) && (image->rows != 0))
210             {
211               if ((width < image->columns) && (height < image->rows))
212                 low=draw_info->pointsize+0.5;
213               else
214                 high=draw_info->pointsize-0.5;
215             }
216           else
217             if (((image->columns != 0) && (width < image->columns)) ||
218                 ((image->rows != 0) && (height < image->rows)))
219               low=draw_info->pointsize+0.5;
220             else
221               high=draw_info->pointsize-0.5;
222         }
223         if (status != MagickFalse)
224           {
225             draw_info->pointsize=floor((low+high)/2.0-0.5);
226             status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
227           }
228       }
229    label=DestroyString(label);
230    if (status == MagickFalse)
231      {
232        draw_info=DestroyDrawInfo(draw_info);
233        image=DestroyImageList(image);
234        return((Image *) NULL);
235      }
236   if (image->columns == 0)
237     image->columns=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
238   if (image->columns == 0)
239     image->columns=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
240       0.5);
241   if (image->rows == 0)
242     image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
243   if (image->rows == 0)
244     image->rows=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
245       0.5);
246   status=SetImageExtent(image,image->columns,image->rows,exception);
247   if (status == MagickFalse)
248     {
249       draw_info=DestroyDrawInfo(draw_info);
250       return(DestroyImageList(image));
251     }
252   if (SetImageBackgroundColor(image,exception) == MagickFalse)
253     {
254       draw_info=DestroyDrawInfo(draw_info);
255       image=DestroyImageList(image);
256       return((Image *) NULL);
257     }
258   /*
259     Draw label.
260   */
261   (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
262     (draw_info->direction == RightToLeftDirection ? (double) image->columns-
263     metrics.bounds.x2 : 0.0),(draw_info->gravity == UndefinedGravity ?
264     MagickMax(metrics.ascent,metrics.bounds.y2) : 0.0));
265   (void) CloneString(&draw_info->geometry,geometry);
266   status=AnnotateImage(image,draw_info,exception);
267   if (image_info->pointsize == 0.0)
268     (void) FormatImageProperty(image,"label:pointsize","%.20g",
269       draw_info->pointsize);
270   draw_info=DestroyDrawInfo(draw_info);
271   if (status == MagickFalse)
272     {
273       image=DestroyImageList(image);
274       return((Image *) NULL);
275     }
276   return(GetFirstImageInList(image));
277 }
278 
279 /*
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 %                                                                             %
282 %                                                                             %
283 %                                                                             %
284 %   R e g i s t e r L A B E L I m a g e                                       %
285 %                                                                             %
286 %                                                                             %
287 %                                                                             %
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 %
290 %  RegisterLABELImage() adds properties for the LABEL image format to
291 %  the list of supported formats.  The properties include the image format
292 %  tag, a method to read and/or write the format, whether the format
293 %  supports the saving of more than one frame to the same file or blob,
294 %  whether the format supports native in-memory I/O, and a brief
295 %  description of the format.
296 %
297 %  The format of the RegisterLABELImage method is:
298 %
299 %      size_t RegisterLABELImage(void)
300 %
301 */
RegisterLABELImage(void)302 ModuleExport size_t RegisterLABELImage(void)
303 {
304   MagickInfo
305     *entry;
306 
307   entry=AcquireMagickInfo("LABEL","LABEL","Image label");
308   entry->decoder=(DecodeImageHandler *) ReadLABELImage;
309   entry->flags^=CoderAdjoinFlag;
310   entry->format_type=ImplicitFormatType;
311   (void) RegisterMagickInfo(entry);
312   return(MagickImageCoderSignature);
313 }
314 
315 /*
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 %                                                                             %
318 %                                                                             %
319 %                                                                             %
320 %   U n r e g i s t e r L A B E L I m a g e                                   %
321 %                                                                             %
322 %                                                                             %
323 %                                                                             %
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 %
326 %  UnregisterLABELImage() removes format registrations made by the
327 %  LABEL module from the list of supported formats.
328 %
329 %  The format of the UnregisterLABELImage method is:
330 %
331 %      UnregisterLABELImage(void)
332 %
333 */
UnregisterLABELImage(void)334 ModuleExport void UnregisterLABELImage(void)
335 {
336   (void) UnregisterMagickInfo("LABEL");
337 }
338