blob: c2a9463e2d0079af10cf88cb3ffc4630f613809e [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/annotate.h"
cristy76ce6e12013-04-05 14:33:38 +000044#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000045#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"
cristy3ed852e2009-09-05 21:47:34 +000061
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*/
89static 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
cristybb503372010-05-27 20:51:26 +0000111 size_t
cristy3ed852e2009-09-05 21:47:34 +0000112 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);
cristy9950d572011-10-01 18:22:35 +0000125 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000126 (void) ResetImagePage(image,"0x0+0+0");
cristye67f43f2013-04-29 10:27:50 +0000127 property=InterpretImageProperties((ImageInfo *) image_info,image,
128 image_info->filename,exception);
cristyd15e6592011-10-15 00:13:06 +0000129 (void) SetImageProperty(image,"label",property,exception);
cristy3ed852e2009-09-05 21:47:34 +0000130 property=DestroyString(property);
cristyd15e6592011-10-15 00:13:06 +0000131 label=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000132 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
133 draw_info->text=ConstantString(label);
cristye275ada2013-04-14 22:29:40 +0000134 if ((image->columns == 0) && (image->rows == 0))
135 {
136 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
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 if (((image->columns == 0) || (image->rows == 0)) ||
cristya4925932013-04-14 21:14:20 +0000141 (fabs(image_info->pointsize) < MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +0000142 {
cristydce9cde2012-08-22 12:58:16 +0000143 double
144 high,
145 low;
146
cristy3ed852e2009-09-05 21:47:34 +0000147 /*
cristydce9cde2012-08-22 12:58:16 +0000148 Auto fit text into bounding box.
cristy3ed852e2009-09-05 21:47:34 +0000149 */
cristy555a8a72013-03-14 00:13:29 +0000150 for ( ; ; draw_info->pointsize*=2.0)
cristy3ed852e2009-09-05 21:47:34 +0000151 {
cristydce9cde2012-08-22 12:58:16 +0000152 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
153 -metrics.bounds.x1,metrics.ascent);
154 if (draw_info->gravity == UndefinedGravity)
155 (void) CloneString(&draw_info->geometry,geometry);
156 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000157 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
anthonyd7c05452010-09-12 01:02:02 +0000158 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristya4925932013-04-14 21:14:20 +0000159 if (((image->columns != 0) && (width >= image->columns)) ||
160 ((image->rows != 0) && (height >= image->rows)))
cristy3ed852e2009-09-05 21:47:34 +0000161 break;
cristya4925932013-04-14 21:14:20 +0000162 if (((image->columns != 0) && (width >= (2*image->columns))) ||
163 ((image->rows != 0) && (height >= (2*image->rows))))
cristy8b235b22013-03-01 12:35:09 +0000164 break;
cristy3ed852e2009-09-05 21:47:34 +0000165 }
cristy0839c552013-04-14 21:45:02 +0000166 high=draw_info->pointsize;
cristye275ada2013-04-14 22:29:40 +0000167 for (low=1.0; (high-low) > 1.0; )
cristy3ed852e2009-09-05 21:47:34 +0000168 {
cristydce9cde2012-08-22 12:58:16 +0000169 draw_info->pointsize=(low+high)/2.0;
170 (void) FormatLocaleString(geometry,MaxTextExtent,"%+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);
cristybb503372010-05-27 20:51:26 +0000175 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
anthonyd7c05452010-09-12 01:02:02 +0000176 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristya4925932013-04-14 21:14:20 +0000177 if (((image->columns != 0) && (width < image->columns)) ||
178 ((image->rows != 0) && (height < image->rows)))
cristydce9cde2012-08-22 12:58:16 +0000179 low=draw_info->pointsize+1.0;
180 else
181 high=draw_info->pointsize-1.0;
cristy3ed852e2009-09-05 21:47:34 +0000182 }
cristy555a8a72013-03-14 00:13:29 +0000183 for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
cristydce9cde2012-08-22 12:58:16 +0000184 {
185 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
186 -metrics.bounds.x1,metrics.ascent);
187 if (draw_info->gravity == UndefinedGravity)
188 (void) CloneString(&draw_info->geometry,geometry);
189 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
190 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
191 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristya4925932013-04-14 21:14:20 +0000192 if (((image->columns != 0) && (width < image->columns)) ||
193 ((image->rows != 0) && (height < image->rows)))
cristydce9cde2012-08-22 12:58:16 +0000194 break;
195 draw_info->pointsize--;
196 }
cristyec0bf4b2013-04-01 19:24:36 +0000197 draw_info->pointsize--;
cristy3ed852e2009-09-05 21:47:34 +0000198 }
cristy5cbc0162011-08-29 00:36:28 +0000199 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000200 if (status == MagickFalse)
201 {
cristy3ed852e2009-09-05 21:47:34 +0000202 image=DestroyImageList(image);
203 return((Image *) NULL);
204 }
cristya4925932013-04-14 21:14:20 +0000205 if (image->columns == 0)
206 image->columns=(size_t) (metrics.width+draw_info->stroke_width+0.5);
207 if (image->columns == 0)
208 image->columns=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5);
209 if (image->rows == 0)
210 image->rows=(size_t) (metrics.ascent-metrics.descent+
211 draw_info->stroke_width+0.5);
212 if (image->rows == 0)
213 image->rows=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5);
cristya2f09b92012-05-01 12:07:49 +0000214 if (draw_info->gravity == UndefinedGravity)
cristy3ed852e2009-09-05 21:47:34 +0000215 {
cristyb51dff52011-05-19 16:55:47 +0000216 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
cristy3ed852e2009-09-05 21:47:34 +0000217 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
218 draw_info->stroke_width/2.0);
cristya2f09b92012-05-01 12:07:49 +0000219 (void) CloneString(&draw_info->geometry,geometry);
220 }
221 if (draw_info->direction == RightToLeftDirection)
222 {
cristy024843f2011-06-03 17:52:52 +0000223 if (draw_info->direction == RightToLeftDirection)
224 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
225 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
cristyb93d54f2012-07-01 01:32:47 +0000226 metrics.ascent+draw_info->stroke_width/2.0);
cristya2f09b92012-05-01 12:07:49 +0000227 (void) CloneString(&draw_info->geometry,geometry);
cristy3ed852e2009-09-05 21:47:34 +0000228 }
cristyea1a8aa2011-10-20 13:24:06 +0000229 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000230 {
cristy95524f92010-02-16 18:44:34 +0000231 image=DestroyImageList(image);
232 return((Image *) NULL);
233 }
cristy5cbc0162011-08-29 00:36:28 +0000234 (void) AnnotateImage(image,draw_info,exception);
cristyb70a4862010-06-18 01:18:44 +0000235 if (image_info->pointsize == 0.0)
236 {
237 char
238 pointsize[MaxTextExtent];
239
cristyb51dff52011-05-19 16:55:47 +0000240 (void) FormatLocaleString(pointsize,MaxTextExtent,"%.20g",
cristyb70a4862010-06-18 01:18:44 +0000241 draw_info->pointsize);
cristyd15e6592011-10-15 00:13:06 +0000242 (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
cristyb70a4862010-06-18 01:18:44 +0000243 }
cristy3ed852e2009-09-05 21:47:34 +0000244 draw_info=DestroyDrawInfo(draw_info);
245 return(GetFirstImageInList(image));
246}
247
248/*
249%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250% %
251% %
252% %
253% R e g i s t e r L A B E L I m a g e %
254% %
255% %
256% %
257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258%
259% RegisterLABELImage() adds properties for the LABEL image format to
260% the list of supported formats. The properties include the image format
261% tag, a method to read and/or write the format, whether the format
262% supports the saving of more than one frame to the same file or blob,
263% whether the format supports native in-memory I/O, and a brief
264% description of the format.
265%
266% The format of the RegisterLABELImage method is:
267%
cristybb503372010-05-27 20:51:26 +0000268% size_t RegisterLABELImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000269%
270*/
cristybb503372010-05-27 20:51:26 +0000271ModuleExport size_t RegisterLABELImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000272{
273 MagickInfo
274 *entry;
275
276 entry=SetMagickInfo("LABEL");
277 entry->decoder=(DecodeImageHandler *) ReadLABELImage;
278 entry->adjoin=MagickFalse;
cristy009d7392010-07-25 22:08:41 +0000279 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000280 entry->description=ConstantString("Image label");
281 entry->module=ConstantString("LABEL");
282 (void) RegisterMagickInfo(entry);
283 return(MagickImageCoderSignature);
284}
285
286/*
287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288% %
289% %
290% %
291% U n r e g i s t e r L A B E L I m a g e %
292% %
293% %
294% %
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296%
297% UnregisterLABELImage() removes format registrations made by the
298% LABEL module from the list of supported formats.
299%
300% The format of the UnregisterLABELImage method is:
301%
302% UnregisterLABELImage(void)
303%
304*/
305ModuleExport void UnregisterLABELImage(void)
306{
307 (void) UnregisterMagickInfo("LABEL");
308}