]> granicus.if.org Git - imagemagick/blob - coders/label.c
(no commit message)
[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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 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 %    http://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/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/draw.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/property.h"
55 #include "MagickCore/quantum-private.h"
56 #include "MagickCore/static.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/module.h"
59 #include "MagickCore/utility.h"
60 \f
61 /*
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 %                                                                             %
64 %                                                                             %
65 %                                                                             %
66 %   R e a d L A B E L I m a g e                                               %
67 %                                                                             %
68 %                                                                             %
69 %                                                                             %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %
72 %  ReadLABELImage() reads a LABEL image file and returns it.  It
73 %  allocates the memory necessary for the new Image structure and returns a
74 %  pointer to the new image.
75 %
76 %  The format of the ReadLABELImage method is:
77 %
78 %      Image *ReadLABELImage(const ImageInfo *image_info,
79 %        ExceptionInfo *exception)
80 %
81 %  A description of each parameter follows:
82 %
83 %    o image_info: the image info.
84 %
85 %    o exception: return any errors or warnings in this structure.
86 %
87 */
88 static Image *ReadLABELImage(const ImageInfo *image_info,
89   ExceptionInfo *exception)
90 {
91   char
92     geometry[MaxTextExtent],
93     *property;
94
95   const char
96     *label;
97
98   DrawInfo
99     *draw_info;
100
101   Image
102     *image;
103
104   MagickBooleanType
105     status;
106
107   TypeMetric
108     metrics;
109
110   size_t
111     height,
112     width;
113
114   /*
115     Initialize Image structure.
116   */
117   assert(image_info != (const ImageInfo *) NULL);
118   assert(image_info->signature == MagickSignature);
119   if (image_info->debug != MagickFalse)
120     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
121       image_info->filename);
122   assert(exception != (ExceptionInfo *) NULL);
123   assert(exception->signature == MagickSignature);
124   image=AcquireImage(image_info,exception);
125   (void) ResetImagePage(image,"0x0+0+0");
126   property=InterpretImageProperties(image_info,image,image_info->filename,
127     exception);
128   (void) SetImageProperty(image,"label",property,exception);
129   property=DestroyString(property);
130   label=GetImageProperty(image,"label",exception);
131   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
132   draw_info->text=ConstantString(label);
133   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
134   if (image->columns == 0)
135     {
136       for ( ; ; draw_info->pointsize*=2.0)
137       {
138         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
139           -metrics.bounds.x1,metrics.ascent);
140         if (draw_info->gravity == UndefinedGravity)
141           (void) CloneString(&draw_info->geometry,geometry);
142         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
143         (void) status;
144         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
145         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
146         if ((height > image->rows) || (image_info->pointsize != 0.0))
147           break;
148       }
149       image->columns=width;
150     }
151   if (image->rows == 0)
152     {
153       for ( ; ; draw_info->pointsize*=2.0)
154       {
155         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
156           -metrics.bounds.x1,metrics.ascent);
157         if (draw_info->gravity == UndefinedGravity)
158           (void) CloneString(&draw_info->geometry,geometry);
159         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
160         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
161         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
162         if ((width > image->columns) || (image_info->pointsize != 0.0))
163           break;
164       }
165       image->rows=height;
166     }
167   if (image_info->pointsize == 0.0)
168     {
169       double
170         high,
171         low;
172
173       /*
174         Auto fit text into bounding box.
175       */
176       for ( ; ; draw_info->pointsize*=2.0)
177       {
178         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
179           -metrics.bounds.x1,metrics.ascent);
180         if (draw_info->gravity == UndefinedGravity)
181           (void) CloneString(&draw_info->geometry,geometry);
182         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
183         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
184         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
185         if ((width > image->columns) && (height > image->rows))
186           break;
187         if ((width > (image->columns << 1)) || (height > (image->rows << 1)))
188           break;
189       }
190       high=draw_info->pointsize/2.0;
191       for (low=high/2.0; (high-low) > 1.0; )
192       {
193         draw_info->pointsize=(low+high)/2.0;
194         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
195           -metrics.bounds.x1,metrics.ascent);
196         if (draw_info->gravity == UndefinedGravity)
197           (void) CloneString(&draw_info->geometry,geometry);
198         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
199         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
200         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
201         if ((width <= image->columns) && (height <= image->rows))
202           low=draw_info->pointsize+1.0;
203         else
204           high=draw_info->pointsize-1.0;
205       }
206       for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
207       {
208         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
209           -metrics.bounds.x1,metrics.ascent);
210         if (draw_info->gravity == UndefinedGravity)
211           (void) CloneString(&draw_info->geometry,geometry);
212         status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
213         width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
214         height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
215         if ((width <= image->columns) && (height <= image->rows))
216           break;
217         draw_info->pointsize--;
218       }
219       draw_info->pointsize=floor(draw_info->pointsize+0.5);
220     }
221   status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
222   if (status == MagickFalse)
223     {
224       image=DestroyImageList(image);
225       return((Image *) NULL);
226     }
227   if (draw_info->gravity == UndefinedGravity)
228     {
229       (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
230         -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
231         draw_info->stroke_width/2.0);
232       (void) CloneString(&draw_info->geometry,geometry);
233     }
234   if (draw_info->direction == RightToLeftDirection)
235     {
236       if (draw_info->direction == RightToLeftDirection)
237         (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
238           image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
239           metrics.ascent+draw_info->stroke_width/2.0);
240       (void) CloneString(&draw_info->geometry,geometry);
241     }
242   if (SetImageBackgroundColor(image,exception) == MagickFalse)
243     {
244       image=DestroyImageList(image);
245       return((Image *) NULL);
246     }
247   (void) AnnotateImage(image,draw_info,exception);
248   if (image_info->pointsize == 0.0)
249     {
250       char
251         pointsize[MaxTextExtent];
252
253       (void) FormatLocaleString(pointsize,MaxTextExtent,"%.20g",
254         draw_info->pointsize);
255       (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
256     }
257   draw_info=DestroyDrawInfo(draw_info);
258   return(GetFirstImageInList(image));
259 }
260 \f
261 /*
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 %                                                                             %
264 %                                                                             %
265 %                                                                             %
266 %   R e g i s t e r L A B E L I m a g e                                       %
267 %                                                                             %
268 %                                                                             %
269 %                                                                             %
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 %
272 %  RegisterLABELImage() adds properties for the LABEL image format to
273 %  the list of supported formats.  The properties include the image format
274 %  tag, a method to read and/or write the format, whether the format
275 %  supports the saving of more than one frame to the same file or blob,
276 %  whether the format supports native in-memory I/O, and a brief
277 %  description of the format.
278 %
279 %  The format of the RegisterLABELImage method is:
280 %
281 %      size_t RegisterLABELImage(void)
282 %
283 */
284 ModuleExport size_t RegisterLABELImage(void)
285 {
286   MagickInfo
287     *entry;
288
289   entry=SetMagickInfo("LABEL");
290   entry->decoder=(DecodeImageHandler *) ReadLABELImage;
291   entry->adjoin=MagickFalse;
292   entry->format_type=ImplicitFormatType;
293   entry->description=ConstantString("Image label");
294   entry->module=ConstantString("LABEL");
295   (void) RegisterMagickInfo(entry);
296   return(MagickImageCoderSignature);
297 }
298 \f
299 /*
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 %                                                                             %
302 %                                                                             %
303 %                                                                             %
304 %   U n r e g i s t e r L A B E L I m a g e                                   %
305 %                                                                             %
306 %                                                                             %
307 %                                                                             %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %
310 %  UnregisterLABELImage() removes format registrations made by the
311 %  LABEL module from the list of supported formats.
312 %
313 %  The format of the UnregisterLABELImage method is:
314 %
315 %      UnregisterLABELImage(void)
316 %
317 */
318 ModuleExport void UnregisterLABELImage(void)
319 {
320   (void) UnregisterMagickInfo("LABEL");
321 }