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