blob: f0779562600b24dac2e33c781297d5162f7b0511 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
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"
cristy3ed852e2009-09-05 21:47:34 +000060
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*/
88static 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
cristybb503372010-05-27 20:51:26 +0000110 size_t
cristy3ed852e2009-09-05 21:47:34 +0000111 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);
cristy9950d572011-10-01 18:22:35 +0000124 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000125 (void) ResetImagePage(image,"0x0+0+0");
cristy018f07f2011-09-04 21:15:19 +0000126 property=InterpretImageProperties(image_info,image,image_info->filename,
127 exception);
cristyd15e6592011-10-15 00:13:06 +0000128 (void) SetImageProperty(image,"label",property,exception);
cristy3ed852e2009-09-05 21:47:34 +0000129 property=DestroyString(property);
cristyd15e6592011-10-15 00:13:06 +0000130 label=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +0000131 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
132 draw_info->text=ConstantString(label);
cristydce9cde2012-08-22 12:58:16 +0000133 if ((*label != '\0') && (image->rows != 0) && (image_info->pointsize == 0.0))
cristy3ed852e2009-09-05 21:47:34 +0000134 {
cristydce9cde2012-08-22 12:58:16 +0000135 double
136 high,
137 low;
138
cristy3ed852e2009-09-05 21:47:34 +0000139 /*
cristydce9cde2012-08-22 12:58:16 +0000140 Auto fit text into bounding box.
cristy3ed852e2009-09-05 21:47:34 +0000141 */
cristyb2df2f52012-09-26 13:16:30 +0000142 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristydce9cde2012-08-22 12:58:16 +0000143 for ( ; ; )
cristy3ed852e2009-09-05 21:47:34 +0000144 {
cristydce9cde2012-08-22 12:58:16 +0000145 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
146 -metrics.bounds.x1,metrics.ascent);
147 if (draw_info->gravity == UndefinedGravity)
148 (void) CloneString(&draw_info->geometry,geometry);
149 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
150 (void) status;
cristybb503372010-05-27 20:51:26 +0000151 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
anthonyd7c05452010-09-12 01:02:02 +0000152 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristydce9cde2012-08-22 12:58:16 +0000153 if ((width > image->columns) && (height > image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000154 break;
cristydce9cde2012-08-22 12:58:16 +0000155 draw_info->pointsize*=2.0;
cristy3ed852e2009-09-05 21:47:34 +0000156 }
cristydce9cde2012-08-22 12:58:16 +0000157 high=draw_info->pointsize/2.0;
158 low=high/2.0;
159 while ((high-low) > 1.0)
cristy3ed852e2009-09-05 21:47:34 +0000160 {
cristydce9cde2012-08-22 12:58:16 +0000161 draw_info->pointsize=(low+high)/2.0;
162 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
163 -metrics.bounds.x1,metrics.ascent);
164 if (draw_info->gravity == UndefinedGravity)
165 (void) CloneString(&draw_info->geometry,geometry);
166 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000167 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
anthonyd7c05452010-09-12 01:02:02 +0000168 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristydce9cde2012-08-22 12:58:16 +0000169 if ((width <= image->columns) && (height <= image->rows))
170 low=draw_info->pointsize+1.0;
171 else
172 high=draw_info->pointsize-1.0;
cristy3ed852e2009-09-05 21:47:34 +0000173 }
cristydce9cde2012-08-22 12:58:16 +0000174 for (draw_info->pointsize=(low+high)/2.0; ; )
175 {
176 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
177 -metrics.bounds.x1,metrics.ascent);
178 if (draw_info->gravity == UndefinedGravity)
179 (void) CloneString(&draw_info->geometry,geometry);
180 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
181 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
182 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
183 if ((width <= image->columns) && (height <= image->rows))
184 break;
185 draw_info->pointsize--;
186 }
187 draw_info->pointsize=floor(draw_info->pointsize);
cristy3ed852e2009-09-05 21:47:34 +0000188 }
cristy5cbc0162011-08-29 00:36:28 +0000189 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000190 if (status == MagickFalse)
191 {
cristy3ed852e2009-09-05 21:47:34 +0000192 image=DestroyImageList(image);
193 return((Image *) NULL);
194 }
195 if (image->columns == 0)
cristybb503372010-05-27 20:51:26 +0000196 image->columns=(size_t) (metrics.width+draw_info->stroke_width+1.5);
cristy3ed852e2009-09-05 21:47:34 +0000197 if (image->columns == 0)
cristy024843f2011-06-03 17:52:52 +0000198 image->columns=(size_t) (draw_info->pointsize+draw_info->stroke_width+1.5);
cristya2f09b92012-05-01 12:07:49 +0000199 if (draw_info->gravity == UndefinedGravity)
cristy3ed852e2009-09-05 21:47:34 +0000200 {
cristyb51dff52011-05-19 16:55:47 +0000201 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
cristy3ed852e2009-09-05 21:47:34 +0000202 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
203 draw_info->stroke_width/2.0);
cristya2f09b92012-05-01 12:07:49 +0000204 (void) CloneString(&draw_info->geometry,geometry);
205 }
206 if (draw_info->direction == RightToLeftDirection)
207 {
cristy024843f2011-06-03 17:52:52 +0000208 if (draw_info->direction == RightToLeftDirection)
209 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
210 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
cristyb93d54f2012-07-01 01:32:47 +0000211 metrics.ascent+draw_info->stroke_width/2.0);
cristya2f09b92012-05-01 12:07:49 +0000212 (void) CloneString(&draw_info->geometry,geometry);
cristy3ed852e2009-09-05 21:47:34 +0000213 }
214 if (image->rows == 0)
cristy524222d2011-04-25 00:37:06 +0000215 image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristy06609ee2010-03-17 20:21:27 +0000216 if (image->rows == 0)
cristy524222d2011-04-25 00:37:06 +0000217 image->rows=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+
218 0.5);
cristyea1a8aa2011-10-20 13:24:06 +0000219 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000220 {
cristy95524f92010-02-16 18:44:34 +0000221 image=DestroyImageList(image);
222 return((Image *) NULL);
223 }
cristy5cbc0162011-08-29 00:36:28 +0000224 (void) AnnotateImage(image,draw_info,exception);
cristyb70a4862010-06-18 01:18:44 +0000225 if (image_info->pointsize == 0.0)
226 {
227 char
228 pointsize[MaxTextExtent];
229
cristyb51dff52011-05-19 16:55:47 +0000230 (void) FormatLocaleString(pointsize,MaxTextExtent,"%.20g",
cristyb70a4862010-06-18 01:18:44 +0000231 draw_info->pointsize);
cristyd15e6592011-10-15 00:13:06 +0000232 (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
cristyb70a4862010-06-18 01:18:44 +0000233 }
cristy3ed852e2009-09-05 21:47:34 +0000234 draw_info=DestroyDrawInfo(draw_info);
235 return(GetFirstImageInList(image));
236}
237
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
243% R e g i s t e r L A B E L I m a g e %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% RegisterLABELImage() adds properties for the LABEL image format to
250% the list of supported formats. The properties include the image format
251% tag, a method to read and/or write the format, whether the format
252% supports the saving of more than one frame to the same file or blob,
253% whether the format supports native in-memory I/O, and a brief
254% description of the format.
255%
256% The format of the RegisterLABELImage method is:
257%
cristybb503372010-05-27 20:51:26 +0000258% size_t RegisterLABELImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000259%
260*/
cristybb503372010-05-27 20:51:26 +0000261ModuleExport size_t RegisterLABELImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000262{
263 MagickInfo
264 *entry;
265
266 entry=SetMagickInfo("LABEL");
267 entry->decoder=(DecodeImageHandler *) ReadLABELImage;
268 entry->adjoin=MagickFalse;
cristy009d7392010-07-25 22:08:41 +0000269 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000270 entry->description=ConstantString("Image label");
271 entry->module=ConstantString("LABEL");
272 (void) RegisterMagickInfo(entry);
273 return(MagickImageCoderSignature);
274}
275
276/*
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278% %
279% %
280% %
281% U n r e g i s t e r L A B E L I m a g e %
282% %
283% %
284% %
285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286%
287% UnregisterLABELImage() removes format registrations made by the
288% LABEL module from the list of supported formats.
289%
290% The format of the UnregisterLABELImage method is:
291%
292% UnregisterLABELImage(void)
293%
294*/
295ModuleExport void UnregisterLABELImage(void)
296{
297 (void) UnregisterMagickInfo("LABEL");
298}