2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % Read Text Caption. %
20 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/composite-private.h"
48 #include "MagickCore/draw.h"
49 #include "MagickCore/draw-private.h"
50 #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"
57 #include "MagickCore/module.h"
58 #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"
63 #include "MagickCore/string-private.h"
64 #include "MagickCore/utility.h"
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 % R e a d C A P T I O N I m a g e %
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
81 % The format of the ReadCAPTIONImage method is:
83 % Image *ReadCAPTIONImage(const ImageInfo *image_info,
84 % ExceptionInfo *exception)
86 % A description of each parameter follows:
88 % o image_info: the image info.
90 % o exception: return any errors or warnings in this structure.
93 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
94 ExceptionInfo *exception)
98 geometry[MaxTextExtent],
126 Initialize Image structure.
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);
135 image=AcquireImage(image_info,exception);
136 (void) ResetImagePage(image,"0x0+0+0");
140 option=GetImageArtifact(image,"filename");
141 if (option == (const char *) NULL)
142 property=InterpretImageProperties(image_info,image,image_info->filename,
145 if (LocaleNCompare(option,"caption:",8) == 0)
146 property=InterpretImageProperties(image_info,image,option+8,exception);
148 property=InterpretImageProperties(image_info,image,option,exception);
149 (void) SetImageProperty(image,"caption",property,exception);
150 property=DestroyString(property);
151 caption=ConstantString(GetImageProperty(image,"caption",exception));
152 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
153 (void) CloneString(&draw_info->text,caption);
154 gravity=GetImageArtifact(image,"gravity");
155 if (gravity != (char *) NULL)
156 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
157 MagickFalse,gravity);
158 if (image->columns == 0)
160 for ( ; ; draw_info->pointsize*=2.0)
162 text=AcquireString(caption);
163 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
165 (void) CloneString(&draw_info->text,text);
166 text=DestroyString(text);
167 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
168 -metrics.bounds.x1,metrics.ascent);
169 if (draw_info->gravity == UndefinedGravity)
170 (void) CloneString(&draw_info->geometry,geometry);
171 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
172 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
173 if ((height >= image->rows) || (image_info->pointsize != 0.0))
176 draw_info->pointsize/=2.0;
177 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
178 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
179 image->columns=width;
181 if (image->rows == 0)
183 for ( ; ; draw_info->pointsize*=2.0)
185 text=AcquireString(caption);
186 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
188 (void) CloneString(&draw_info->text,text);
189 text=DestroyString(text);
190 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
191 -metrics.bounds.x1,metrics.ascent);
192 if (draw_info->gravity == UndefinedGravity)
193 (void) CloneString(&draw_info->geometry,geometry);
194 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
195 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
196 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
197 if ((width >= image->columns) || (image_info->pointsize != 0.0))
200 draw_info->pointsize/=2.0;
201 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
202 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
203 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
205 if (fabs(image_info->pointsize) < MagickEpsilon)
212 Auto fit text into bounding box.
214 for ( ; ; draw_info->pointsize*=2.0)
216 text=AcquireString(caption);
217 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
219 (void) CloneString(&draw_info->text,text);
220 text=DestroyString(text);
221 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
222 -metrics.bounds.x1,metrics.ascent);
223 if (draw_info->gravity == UndefinedGravity)
224 (void) CloneString(&draw_info->geometry,geometry);
225 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
226 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
227 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
228 if ((width >= image->columns) && (height >= image->rows))
230 if ((width >= (image->columns << 1)) || (height >= (image->rows << 1)))
233 high=draw_info->pointsize/2.0;
234 for (low=high/2.0; (high-low) > 1.0; )
236 draw_info->pointsize=(low+high)/2.0;
237 text=AcquireString(caption);
238 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
240 (void) CloneString(&draw_info->text,text);
241 text=DestroyString(text);
242 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
243 -metrics.bounds.x1,metrics.ascent);
244 if (draw_info->gravity == UndefinedGravity)
245 (void) CloneString(&draw_info->geometry,geometry);
246 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
247 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
248 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
249 if ((width <= image->columns) && (height <= image->rows))
250 low=draw_info->pointsize+1.0;
252 high=draw_info->pointsize-1.0;
254 for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; )
256 text=AcquireString(caption);
257 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text,
259 (void) CloneString(&draw_info->text,text);
260 text=DestroyString(text);
261 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
262 -metrics.bounds.x1,metrics.ascent);
263 if (draw_info->gravity == UndefinedGravity)
264 (void) CloneString(&draw_info->geometry,geometry);
265 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
266 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
267 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
268 if ((width <= image->columns) && (height <= image->rows))
270 draw_info->pointsize--;
272 draw_info->pointsize--;
274 (void) CloneString(&draw_info->text,caption);
275 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&caption,
277 if (SetImageBackgroundColor(image,exception) == MagickFalse)
279 image=DestroyImageList(image);
280 return((Image *) NULL);
285 (void) CloneString(&draw_info->text,caption);
286 status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception);
287 if ((draw_info->gravity != UndefinedGravity) &&
288 (draw_info->direction != RightToLeftDirection))
289 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
292 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
293 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
294 draw_info->stroke_width/2.0);
295 if (draw_info->direction == RightToLeftDirection)
296 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
297 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
298 metrics.ascent+draw_info->stroke_width/2.0);
299 draw_info->geometry=AcquireString(geometry);
301 status=AnnotateImage(image,draw_info,exception);
302 draw_info=DestroyDrawInfo(draw_info);
303 caption=DestroyString(caption);
304 if (status == MagickFalse)
306 image=DestroyImageList(image);
307 return((Image *) NULL);
309 return(GetFirstImageInList(image));
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 % R e g i s t e r C A P T I O N I m a g e %
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % RegisterCAPTIONImage() adds attributes for the CAPTION image format to
324 % the list of supported formats. The attributes include the image format
325 % tag, a method to read and/or write the format, whether the format
326 % supports the saving of more than one frame to the same file or blob,
327 % whether the format supports native in-memory I/O, and a brief
328 % description of the format.
330 % The format of the RegisterCAPTIONImage method is:
332 % size_t RegisterCAPTIONImage(void)
335 ModuleExport size_t RegisterCAPTIONImage(void)
340 entry=SetMagickInfo("CAPTION");
341 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
342 entry->description=ConstantString("Caption");
343 entry->adjoin=MagickFalse;
344 entry->module=ConstantString("CAPTION");
345 (void) RegisterMagickInfo(entry);
346 return(MagickImageCoderSignature);
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % U n r e g i s t e r C A P T I O N I m a g e %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % UnregisterCAPTIONImage() removes format registrations made by the
361 % CAPTION module from the list of supported formats.
363 % The format of the UnregisterCAPTIONImage method is:
365 % UnregisterCAPTIONImage(void)
368 ModuleExport void UnregisterCAPTIONImage(void)
370 (void) UnregisterMagickInfo("CAPTION");