blob: 555bc0f530992d053b5307474b70349f19bb0e0e [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% CCCC AAA PPPP TTTTT IIIII OOO N N %
7% C A A P P T I O O NN N %
8% C AAAAA PPPP T I O O N N N %
9% C A A P T I O O N NN %
10% CCCC A A P T IIIII OOO N N %
11% %
12% %
13% Read Text Caption. %
14% %
15% Software Design %
16% John Cristy %
17% February 2002 %
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"
cristy8a3ce7f2011-10-30 02:28:03 +000047#include "MagickCore/composite-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/draw.h"
cristy8a3ce7f2011-10-30 02:28:03 +000049#include "MagickCore/draw-private.h"
cristy4c08aed2011-07-01 19:47:50 +000050#include "MagickCore/exception.h"
51#include "MagickCore/exception-private.h"
52#include "MagickCore/image.h"
53#include "MagickCore/image-private.h"
54#include "MagickCore/list.h"
55#include "MagickCore/magick.h"
56#include "MagickCore/memory_.h"
cristy8a3ce7f2011-10-30 02:28:03 +000057#include "MagickCore/module.h"
cristy4c08aed2011-07-01 19:47:50 +000058#include "MagickCore/option.h"
59#include "MagickCore/property.h"
60#include "MagickCore/quantum-private.h"
61#include "MagickCore/static.h"
62#include "MagickCore/string_.h"
cristy8a3ce7f2011-10-30 02:28:03 +000063#include "MagickCore/string-private.h"
64#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000065
66/*
67%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68% %
69% %
70% %
71% R e a d C A P T I O N I m a g e %
72% %
73% %
74% %
75%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76%
77% ReadCAPTIONImage() reads a CAPTION image file and returns it. It
78% allocates the memory necessary for the new Image structure and returns a
79% pointer to the new image.
80%
81% The format of the ReadCAPTIONImage method is:
82%
83% Image *ReadCAPTIONImage(const ImageInfo *image_info,
84% ExceptionInfo *exception)
85%
86% A description of each parameter follows:
87%
88% o image_info: the image info.
89%
90% o exception: return any errors or warnings in this structure.
91%
92*/
93static Image *ReadCAPTIONImage(const ImageInfo *image_info,
94 ExceptionInfo *exception)
95{
96 char
97 *caption,
98 geometry[MaxTextExtent],
cristy54ec4272013-03-14 14:24:14 +000099 *property,
100 *text;
cristy3ed852e2009-09-05 21:47:34 +0000101
102 const char
cristyab272ac2012-03-04 22:08:07 +0000103 *gravity,
104 *option;
cristy3ed852e2009-09-05 21:47:34 +0000105
106 DrawInfo
107 *draw_info;
108
109 Image
110 *image;
111
112 MagickBooleanType
113 status;
114
cristybb503372010-05-27 20:51:26 +0000115 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000116 i;
117
cristybb503372010-05-27 20:51:26 +0000118 size_t
cristy3ed852e2009-09-05 21:47:34 +0000119 height,
120 width;
121
cristy4e82e512011-04-24 01:33:42 +0000122 TypeMetric
123 metrics;
124
cristy3ed852e2009-09-05 21:47:34 +0000125 /*
126 Initialize Image structure.
127 */
128 assert(image_info != (const ImageInfo *) NULL);
129 assert(image_info->signature == MagickSignature);
130 if (image_info->debug != MagickFalse)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132 image_info->filename);
133 assert(exception != (ExceptionInfo *) NULL);
134 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000135 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000136 (void) ResetImagePage(image,"0x0+0+0");
137 /*
138 Format caption.
139 */
cristy092ec8d2013-04-26 13:46:22 +0000140 option=GetImageOption(image_info,"filename");
cristyab272ac2012-03-04 22:08:07 +0000141 if (option == (const char *) NULL)
142 property=InterpretImageProperties(image_info,image,image_info->filename,
143 exception);
144 else
145 if (LocaleNCompare(option,"caption:",8) == 0)
146 property=InterpretImageProperties(image_info,image,option+8,exception);
147 else
148 property=InterpretImageProperties(image_info,image,option,exception);
cristyd15e6592011-10-15 00:13:06 +0000149 (void) SetImageProperty(image,"caption",property,exception);
cristy3ed852e2009-09-05 21:47:34 +0000150 property=DestroyString(property);
cristyd15e6592011-10-15 00:13:06 +0000151 caption=ConstantString(GetImageProperty(image,"caption",exception));
cristy3ed852e2009-09-05 21:47:34 +0000152 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
cristy6fc39292011-10-23 14:26:30 +0000153 (void) CloneString(&draw_info->text,caption);
cristy092ec8d2013-04-26 13:46:22 +0000154 gravity=GetImageOption(image_info,"gravity");
cristy3ed852e2009-09-05 21:47:34 +0000155 if (gravity != (char *) NULL)
cristy042ee782011-04-22 18:48:30 +0000156 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
cristy3ed852e2009-09-05 21:47:34 +0000157 MagickFalse,gravity);
cristy8db9ca42013-03-14 13:35:37 +0000158 if (image->columns == 0)
159 {
cristye275ada2013-04-14 22:29:40 +0000160 text=AcquireString(caption);
161 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
162 exception);
163 (void) CloneString(&draw_info->text,text);
164 text=DestroyString(text);
165 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
166 -metrics.bounds.x1,metrics.ascent);
167 if (draw_info->gravity == UndefinedGravity)
168 (void) CloneString(&draw_info->geometry,geometry);
cristyb712c6e2013-04-14 21:43:41 +0000169 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristya4925932013-04-14 21:14:20 +0000170 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
cristy8db9ca42013-03-14 13:35:37 +0000171 image->columns=width;
172 }
173 if (image->rows == 0)
174 {
cristye275ada2013-04-14 22:29:40 +0000175 text=AcquireString(caption);
176 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
177 exception);
178 (void) CloneString(&draw_info->text,text);
179 text=DestroyString(text);
180 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
181 -metrics.bounds.x1,metrics.ascent);
182 if (draw_info->gravity == UndefinedGravity)
183 (void) CloneString(&draw_info->geometry,geometry);
cristya4925932013-04-14 21:14:20 +0000184 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristy54ec4272013-03-14 14:24:14 +0000185 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
186 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
cristy8db9ca42013-03-14 13:35:37 +0000187 }
cristya4925932013-04-14 21:14:20 +0000188 if (fabs(image_info->pointsize) < MagickEpsilon)
cristy3ed852e2009-09-05 21:47:34 +0000189 {
cristybdf24b22012-05-24 12:20:45 +0000190 double
191 high,
192 low;
193
cristy3ed852e2009-09-05 21:47:34 +0000194 /*
cristybdf24b22012-05-24 12:20:45 +0000195 Auto fit text into bounding box.
cristy3ed852e2009-09-05 21:47:34 +0000196 */
cristy8592b052013-02-07 01:24:54 +0000197 for ( ; ; draw_info->pointsize*=2.0)
cristy3ed852e2009-09-05 21:47:34 +0000198 {
199 text=AcquireString(caption);
cristy9f92e3c2013-03-20 17:28:14 +0000200 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
cristy5cbc0162011-08-29 00:36:28 +0000201 exception);
cristy3ed852e2009-09-05 21:47:34 +0000202 (void) CloneString(&draw_info->text,text);
203 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +0000204 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
cristy3ed852e2009-09-05 21:47:34 +0000205 -metrics.bounds.x1,metrics.ascent);
206 if (draw_info->gravity == UndefinedGravity)
207 (void) CloneString(&draw_info->geometry,geometry);
cristy5cbc0162011-08-29 00:36:28 +0000208 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000209 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
cristy4e82e512011-04-24 01:33:42 +0000210 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristy54ec4272013-03-14 14:24:14 +0000211 if ((width >= image->columns) && (height >= image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000212 break;
cristy54ec4272013-03-14 14:24:14 +0000213 if ((width >= (image->columns << 1)) || (height >= (image->rows << 1)))
cristy8b235b22013-03-01 12:35:09 +0000214 break;
cristy3ed852e2009-09-05 21:47:34 +0000215 }
cristy0839c552013-04-14 21:45:02 +0000216 high=draw_info->pointsize;
cristye275ada2013-04-14 22:29:40 +0000217 for (low=1.0; (high-low) > 1.0; )
cristy3ed852e2009-09-05 21:47:34 +0000218 {
cristybdf24b22012-05-24 12:20:45 +0000219 draw_info->pointsize=(low+high)/2.0;
cristy3ed852e2009-09-05 21:47:34 +0000220 text=AcquireString(caption);
cristy9f92e3c2013-03-20 17:28:14 +0000221 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
cristy5cbc0162011-08-29 00:36:28 +0000222 exception);
cristy3ed852e2009-09-05 21:47:34 +0000223 (void) CloneString(&draw_info->text,text);
224 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +0000225 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
cristy3ed852e2009-09-05 21:47:34 +0000226 -metrics.bounds.x1,metrics.ascent);
227 if (draw_info->gravity == UndefinedGravity)
228 (void) CloneString(&draw_info->geometry,geometry);
cristy5cbc0162011-08-29 00:36:28 +0000229 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000230 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
cristy4e82e512011-04-24 01:33:42 +0000231 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
cristy42f5a6a2012-05-24 01:52:18 +0000232 if ((width <= image->columns) && (height <= image->rows))
cristybdf24b22012-05-24 12:20:45 +0000233 low=draw_info->pointsize+1.0;
234 else
235 high=draw_info->pointsize-1.0;
cristy3ed852e2009-09-05 21:47:34 +0000236 }
cristy6cd29472013-03-14 13:21:34 +0000237 for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
cristy17386ff2012-05-28 11:43:37 +0000238 {
239 text=AcquireString(caption);
cristy9f92e3c2013-03-20 17:28:14 +0000240 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
cristy17386ff2012-05-28 11:43:37 +0000241 exception);
242 (void) CloneString(&draw_info->text,text);
243 text=DestroyString(text);
244 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
245 -metrics.bounds.x1,metrics.ascent);
246 if (draw_info->gravity == UndefinedGravity)
247 (void) CloneString(&draw_info->geometry,geometry);
248 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
249 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
250 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
251 if ((width <= image->columns) && (height <= image->rows))
252 break;
cristy6cd29472013-03-14 13:21:34 +0000253 draw_info->pointsize--;
cristy17386ff2012-05-28 11:43:37 +0000254 }
cristyec0bf4b2013-04-01 19:24:36 +0000255 draw_info->pointsize--;
cristy3ed852e2009-09-05 21:47:34 +0000256 }
cristya4925932013-04-14 21:14:20 +0000257 (void) CloneString(&draw_info->text,caption);
258 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&caption,
259 exception);
cristyea1a8aa2011-10-20 13:24:06 +0000260 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000261 {
cristy95524f92010-02-16 18:44:34 +0000262 image=DestroyImageList(image);
263 return((Image *) NULL);
264 }
cristy3ed852e2009-09-05 21:47:34 +0000265 /*
266 Draw caption.
267 */
268 (void) CloneString(&draw_info->text,caption);
cristy5cbc0162011-08-29 00:36:28 +0000269 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
cristy024843f2011-06-03 17:52:52 +0000270 if ((draw_info->gravity != UndefinedGravity) &&
271 (draw_info->direction != RightToLeftDirection))
cristybb503372010-05-27 20:51:26 +0000272 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
cristy3ed852e2009-09-05 21:47:34 +0000273 else
274 {
cristyb51dff52011-05-19 16:55:47 +0000275 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
cristy3ed852e2009-09-05 21:47:34 +0000276 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
277 draw_info->stroke_width/2.0);
cristy024843f2011-06-03 17:52:52 +0000278 if (draw_info->direction == RightToLeftDirection)
279 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
280 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
281 metrics.ascent+draw_info->stroke_width/2.0);
282 draw_info->geometry=AcquireString(geometry);
cristy3ed852e2009-09-05 21:47:34 +0000283 }
cristyab272ac2012-03-04 22:08:07 +0000284 status=AnnotateImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000285 draw_info=DestroyDrawInfo(draw_info);
286 caption=DestroyString(caption);
cristyab272ac2012-03-04 22:08:07 +0000287 if (status == MagickFalse)
288 {
289 image=DestroyImageList(image);
290 return((Image *) NULL);
291 }
cristy3ed852e2009-09-05 21:47:34 +0000292 return(GetFirstImageInList(image));
293}
294
295/*
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297% %
298% %
299% %
300% R e g i s t e r C A P T I O N I m a g e %
301% %
302% %
303% %
304%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305%
306% RegisterCAPTIONImage() adds attributes for the CAPTION image format to
307% the list of supported formats. The attributes include the image format
308% tag, a method to read and/or write the format, whether the format
309% supports the saving of more than one frame to the same file or blob,
310% whether the format supports native in-memory I/O, and a brief
311% description of the format.
312%
313% The format of the RegisterCAPTIONImage method is:
314%
cristybb503372010-05-27 20:51:26 +0000315% size_t RegisterCAPTIONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000316%
317*/
cristybb503372010-05-27 20:51:26 +0000318ModuleExport size_t RegisterCAPTIONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000319{
320 MagickInfo
321 *entry;
322
323 entry=SetMagickInfo("CAPTION");
324 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
cristy8a3ce7f2011-10-30 02:28:03 +0000325 entry->description=ConstantString("Caption");
cristy3ed852e2009-09-05 21:47:34 +0000326 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000327 entry->module=ConstantString("CAPTION");
328 (void) RegisterMagickInfo(entry);
329 return(MagickImageCoderSignature);
330}
331
332/*
333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334% %
335% %
336% %
337% U n r e g i s t e r C A P T I O N I m a g e %
338% %
339% %
340% %
341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342%
343% UnregisterCAPTIONImage() removes format registrations made by the
344% CAPTION module from the list of supported formats.
345%
346% The format of the UnregisterCAPTIONImage method is:
347%
348% UnregisterCAPTIONImage(void)
349%
350*/
351ModuleExport void UnregisterCAPTIONImage(void)
352{
353 (void) UnregisterMagickInfo("CAPTION");
354}