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