]> granicus.if.org Git - imagemagick/blob - coders/label.c
https://github.com/ImageMagick/ImageMagick/issues/1286
[imagemagick] / coders / label.c
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-2018 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://www.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 \f
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 \f
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 */
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     (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)
135     {
136       label=DestroyString(label);
137       draw_info=DestroyDrawInfo(draw_info);
138       ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
139     }
140   draw_info->text=ConstantString(label);
141   metrics.width=0.0;
142   metrics.height=0.0;
143   metrics.ascent=0.0;
144   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
145   if ((image->columns == 0) && (image->rows == 0))
146     {
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);
149     }
150   else
151     if ((status != MagickFalse) && (strlen(label) > 0) &&
152         (((image->columns == 0) || (image->rows == 0)) ||
153          (fabs(image_info->pointsize) < MagickEpsilon)))
154       {
155         double
156           high,
157           low;
158
159         ssize_t
160           n;
161
162         /*
163           Auto fit text into bounding box.
164         */
165         for (n=0; n < 32; n++, draw_info->pointsize*=2.0)
166         {
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)
173             break;
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))
177             {
178               if ((width >= image->columns) && (height >= image->rows))
179                 break;
180             }
181           else
182             if (((image->columns != 0) && (width >= image->columns)) ||
183                 ((image->rows != 0) && (height >= image->rows)))
184               break;
185         }
186         if (status == MagickFalse)
187           {
188             label=DestroyString(label);
189             draw_info=DestroyDrawInfo(draw_info);
190             image=DestroyImageList(image);
191             return((Image *) NULL);
192           }
193         high=draw_info->pointsize;
194         for (low=1.0; (high-low) > 0.5; )
195         {
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)
203             break;
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))
207             {
208               if ((width < image->columns) && (height < image->rows))
209                 low=draw_info->pointsize+0.5;
210               else
211                 high=draw_info->pointsize-0.5;
212             }
213           else
214             if (((image->columns != 0) && (width < image->columns)) ||
215                 ((image->rows != 0) && (height < image->rows)))
216               low=draw_info->pointsize+0.5;
217             else
218               high=draw_info->pointsize-0.5;
219         }
220         if (status != MagickFalse)
221           {
222             draw_info->pointsize=(low+high)/2.0-0.5;
223             status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
224           }
225       }
226    label=DestroyString(label);
227    if (status == MagickFalse)
228      {
229        draw_info=DestroyDrawInfo(draw_info);
230        image=DestroyImageList(image);
231        return((Image *) NULL);
232      }
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+
237       0.5);
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+
243       0.5);
244   status=SetImageExtent(image,image->columns,image->rows,exception);
245   if (status == MagickFalse)
246     {
247       draw_info=DestroyDrawInfo(draw_info);
248       return(DestroyImageList(image));
249     }
250   if (SetImageBackgroundColor(image,exception) == MagickFalse)
251     {
252       draw_info=DestroyDrawInfo(draw_info);
253       image=DestroyImageList(image);
254       return((Image *) NULL);
255     }
256   /*
257     Draw label.
258   */
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)
266     {
267       char
268         pointsize[MagickPathExtent];
269
270       (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g",
271         draw_info->pointsize);
272       (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
273     }
274   draw_info=DestroyDrawInfo(draw_info);
275   if (status == MagickFalse)
276     {
277       image=DestroyImageList(image);
278       return((Image *) NULL);
279     }
280   return(GetFirstImageInList(image));
281 }
282 \f
283 /*
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 %                                                                             %
286 %                                                                             %
287 %                                                                             %
288 %   R e g i s t e r L A B E L I m a g e                                       %
289 %                                                                             %
290 %                                                                             %
291 %                                                                             %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 %
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.
300 %
301 %  The format of the RegisterLABELImage method is:
302 %
303 %      size_t RegisterLABELImage(void)
304 %
305 */
306 ModuleExport size_t RegisterLABELImage(void)
307 {
308   MagickInfo
309     *entry;
310
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);
317 }
318 \f
319 /*
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 %                                                                             %
322 %                                                                             %
323 %                                                                             %
324 %   U n r e g i s t e r L A B E L I m a g e                                   %
325 %                                                                             %
326 %                                                                             %
327 %                                                                             %
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 %
330 %  UnregisterLABELImage() removes format registrations made by the
331 %  LABEL module from the list of supported formats.
332 %
333 %  The format of the UnregisterLABELImage method is:
334 %
335 %      UnregisterLABELImage(void)
336 %
337 */
338 ModuleExport void UnregisterLABELImage(void)
339 {
340   (void) UnregisterMagickInfo("LABEL");
341 }