blob: e67fbaf0c070e40273c20a6e59c605e3a4fa81e3 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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
cristy151b66d2015-04-15 10:50:31 +000093 geometry[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +000094 *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);
cristye1c94d92015-06-28 12:16:33 +0000119 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000120 if (image_info->debug != MagickFalse)
121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
122 image_info->filename);
123 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000124 assert(exception->signature == MagickCoreSignature);
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);
cristyf2a82ee2014-05-26 17:49:54 +0000134 metrics.width=0;
cristyb05c5342015-02-19 16:01:46 +0000135 metrics.ascent=0.0;
cristy0a6c4332013-05-25 18:07:07 +0000136 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristye275ada2013-04-14 22:29:40 +0000137 if ((image->columns == 0) && (image->rows == 0))
138 {
cristye275ada2013-04-14 22:29:40 +0000139 image->columns=(size_t) (metrics.width+draw_info->stroke_width+0.5);
140 image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
141 }
cristyd15dfd12013-05-13 11:21:49 +0000142 else
143 if (((image->columns == 0) || (image->rows == 0)) ||
144 (fabs(image_info->pointsize) < MagickEpsilon))
145 {
146 double
147 high,
148 low;
cristydce9cde2012-08-22 12:58:16 +0000149
cristyd15dfd12013-05-13 11:21:49 +0000150 /*
151 Auto fit text into bounding box.
152 */
153 for ( ; ; draw_info->pointsize*=2.0)
154 {
cristy151b66d2015-04-15 10:50:31 +0000155 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
cristyb05c5342015-02-19 16:01:46 +0000156 -metrics.bounds.x1,metrics.ascent);
cristyd15dfd12013-05-13 11:21:49 +0000157 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 ((image->columns != 0) && (image->rows != 0))
163 {
cristycae9c062013-05-13 11:46:22 +0000164 if ((width >= image->columns) && (height >= image->rows))
cristyd15dfd12013-05-13 11:21:49 +0000165 break;
166 }
167 else
cristycae9c062013-05-13 11:46:22 +0000168 if (((image->columns != 0) && (width >= image->columns)) ||
169 ((image->rows != 0) && (height >= image->rows)))
cristy071c2e22013-05-11 01:05:45 +0000170 break;
cristyd15dfd12013-05-13 11:21:49 +0000171 }
172 high=draw_info->pointsize;
cristy74263582013-05-30 23:47:06 +0000173 for (low=1.0; (high-low) > 0.5; )
cristyd15dfd12013-05-13 11:21:49 +0000174 {
175 draw_info->pointsize=(low+high)/2.0;
cristy151b66d2015-04-15 10:50:31 +0000176 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
cristyb05c5342015-02-19 16:01:46 +0000177 -metrics.bounds.x1,metrics.ascent);
cristyd15dfd12013-05-13 11:21:49 +0000178 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 ((image->columns != 0) && (image->rows != 0))
184 {
cristycae9c062013-05-13 11:46:22 +0000185 if ((width < image->columns) && (height < image->rows))
cristy74263582013-05-30 23:47:06 +0000186 low=draw_info->pointsize+0.5;
cristyd15dfd12013-05-13 11:21:49 +0000187 else
cristy74263582013-05-30 23:47:06 +0000188 high=draw_info->pointsize-0.5;
cristyd15dfd12013-05-13 11:21:49 +0000189 }
190 else
191 if (((image->columns != 0) && (width < image->columns)) ||
192 ((image->rows != 0) && (height < image->rows)))
cristy74263582013-05-30 23:47:06 +0000193 low=draw_info->pointsize+0.5;
cristy071c2e22013-05-11 01:05:45 +0000194 else
cristy74263582013-05-30 23:47:06 +0000195 high=draw_info->pointsize-0.5;
cristyd15dfd12013-05-13 11:21:49 +0000196 }
cristy74263582013-05-30 23:47:06 +0000197 draw_info->pointsize=(low+high)/2.0-0.5;
cristy3ed852e2009-09-05 21:47:34 +0000198 }
cristy176d1cd2015-02-20 17:44:44 +0000199 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
200 if (status == MagickFalse)
201 {
202 draw_info=DestroyDrawInfo(draw_info);
203 image=DestroyImageList(image);
204 return((Image *) NULL);
205 }
cristya4925932013-04-14 21:14:20 +0000206 if (image->columns == 0)
207 image->columns=(size_t) (metrics.width+draw_info->stroke_width+0.5);
208 if (image->columns == 0)
209 image->columns=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5);
210 if (image->rows == 0)
cristyb05c5342015-02-19 16:01:46 +0000211 image->rows=(size_t) (metrics.ascent-metrics.descent+
cristya4925932013-04-14 21:14:20 +0000212 draw_info->stroke_width+0.5);
213 if (image->rows == 0)
214 image->rows=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5);
cristyacabb842014-12-14 23:36:33 +0000215 status=SetImageExtent(image,image->columns,image->rows,exception);
216 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000217 {
cristy176d1cd2015-02-20 17:44:44 +0000218 draw_info=DestroyDrawInfo(draw_info);
219 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000220 }
cristyea1a8aa2011-10-20 13:24:06 +0000221 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000222 {
cristy176d1cd2015-02-20 17:44:44 +0000223 draw_info=DestroyDrawInfo(draw_info);
cristy95524f92010-02-16 18:44:34 +0000224 image=DestroyImageList(image);
225 return((Image *) NULL);
226 }
cristy176d1cd2015-02-20 17:44:44 +0000227 /*
228 Draw label.
229 */
cristy151b66d2015-04-15 10:50:31 +0000230 (void) FormatLocaleString(geometry,MagickPathExtent,"%+g%+g",
cristy176d1cd2015-02-20 17:44:44 +0000231 draw_info->direction == RightToLeftDirection ? image->columns-
232 metrics.bounds.x2 : 0.0,draw_info->gravity == UndefinedGravity ?
233 metrics.ascent : 0.0);
234 draw_info->geometry=AcquireString(geometry);
235 status=AnnotateImage(image,draw_info,exception);
cristyb70a4862010-06-18 01:18:44 +0000236 if (image_info->pointsize == 0.0)
237 {
238 char
cristy151b66d2015-04-15 10:50:31 +0000239 pointsize[MagickPathExtent];
cristyb70a4862010-06-18 01:18:44 +0000240
cristy151b66d2015-04-15 10:50:31 +0000241 (void) FormatLocaleString(pointsize,MagickPathExtent,"%.20g",
cristyb70a4862010-06-18 01:18:44 +0000242 draw_info->pointsize);
cristyd15e6592011-10-15 00:13:06 +0000243 (void) SetImageProperty(image,"label:pointsize",pointsize,exception);
cristyb70a4862010-06-18 01:18:44 +0000244 }
cristy3ed852e2009-09-05 21:47:34 +0000245 draw_info=DestroyDrawInfo(draw_info);
cristy176d1cd2015-02-20 17:44:44 +0000246 if (status == MagickFalse)
247 {
248 image=DestroyImageList(image);
249 return((Image *) NULL);
250 }
cristy3ed852e2009-09-05 21:47:34 +0000251 return(GetFirstImageInList(image));
252}
253
254/*
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256% %
257% %
258% %
259% R e g i s t e r L A B E L I m a g e %
260% %
261% %
262% %
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264%
265% RegisterLABELImage() adds properties for the LABEL image format to
266% the list of supported formats. The properties include the image format
267% tag, a method to read and/or write the format, whether the format
268% supports the saving of more than one frame to the same file or blob,
269% whether the format supports native in-memory I/O, and a brief
270% description of the format.
271%
272% The format of the RegisterLABELImage method is:
273%
cristybb503372010-05-27 20:51:26 +0000274% size_t RegisterLABELImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000275%
276*/
cristybb503372010-05-27 20:51:26 +0000277ModuleExport size_t RegisterLABELImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000278{
279 MagickInfo
280 *entry;
281
dirk06b627a2015-04-06 18:59:17 +0000282 entry=AcquireMagickInfo("LABEL","LABEL","Image label");
cristy3ed852e2009-09-05 21:47:34 +0000283 entry->decoder=(DecodeImageHandler *) ReadLABELImage;
dirk08e9a112015-02-22 01:51:41 +0000284 entry->flags^=CoderAdjoinFlag;
cristy009d7392010-07-25 22:08:41 +0000285 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000286 (void) RegisterMagickInfo(entry);
287 return(MagickImageCoderSignature);
288}
289
290/*
291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292% %
293% %
294% %
295% U n r e g i s t e r L A B E L I m a g e %
296% %
297% %
298% %
299%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300%
301% UnregisterLABELImage() removes format registrations made by the
302% LABEL module from the list of supported formats.
303%
304% The format of the UnregisterLABELImage method is:
305%
306% UnregisterLABELImage(void)
307%
308*/
309ModuleExport void UnregisterLABELImage(void)
310{
311 (void) UnregisterMagickInfo("LABEL");
312}