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-2011 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/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/option.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"
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 % R e a d C A P T I O N I m a g e %
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % ReadCAPTIONImage() reads a CAPTION 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.
76 % The format of the ReadCAPTIONImage method is:
78 % Image *ReadCAPTIONImage(const ImageInfo *image_info,
79 % ExceptionInfo *exception)
81 % A description of each parameter follows:
83 % o image_info: the image info.
85 % o exception: return any errors or warnings in this structure.
88 static Image *ReadCAPTIONImage(const ImageInfo *image_info,
89 ExceptionInfo *exception)
93 geometry[MaxTextExtent],
119 Initialize Image structure.
121 assert(image_info != (const ImageInfo *) NULL);
122 assert(image_info->signature == MagickSignature);
123 if (image_info->debug != MagickFalse)
124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
125 image_info->filename);
126 assert(exception != (ExceptionInfo *) NULL);
127 assert(exception->signature == MagickSignature);
128 image=AcquireImage(image_info);
129 if (image->columns == 0)
130 ThrowReaderException(OptionError,"MustSpecifyImageSize");
131 (void) ResetImagePage(image,"0x0+0+0");
135 property=InterpretImageProperties(image_info,image,image_info->filename);
136 (void) SetImageProperty(image,"caption",property);
137 property=DestroyString(property);
138 caption=ConstantString(GetImageProperty(image,"caption"));
139 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
140 draw_info->text=ConstantString(caption);
141 gravity=GetImageOption(image_info,"gravity");
142 if (gravity != (char *) NULL)
143 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
144 MagickFalse,gravity);
145 if ((*caption != '\0') && (image->rows != 0) &&
146 (image_info->pointsize == 0.0))
152 Scale text to fit bounding box.
156 text=AcquireString(caption);
157 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text);
158 (void) CloneString(&draw_info->text,text);
159 text=DestroyString(text);
160 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
161 -metrics.bounds.x1,metrics.ascent);
162 if (draw_info->gravity == UndefinedGravity)
163 (void) CloneString(&draw_info->geometry,geometry);
164 status=GetMultilineTypeMetrics(image,draw_info,&metrics);
166 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
167 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
168 if ((width > (image->columns+1)) || (height > (image->rows+1)))
170 draw_info->pointsize*=2.0;
172 draw_info->pointsize/=2.0;
175 text=AcquireString(caption);
176 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text);
177 (void) CloneString(&draw_info->text,text);
178 text=DestroyString(text);
179 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
180 -metrics.bounds.x1,metrics.ascent);
181 if (draw_info->gravity == UndefinedGravity)
182 (void) CloneString(&draw_info->geometry,geometry);
183 status=GetMultilineTypeMetrics(image,draw_info,&metrics);
184 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
185 height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5);
186 if ((width > (image->columns+1)) || (height > (image->rows+1)))
188 draw_info->pointsize++;
190 draw_info->pointsize--;
192 i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption);
193 if (image->rows == 0)
194 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
195 draw_info->interline_spacing+draw_info->stroke_width)+0.5);
196 if (image->rows == 0)
197 image->rows=(size_t) ((i+1)*draw_info->pointsize+
198 draw_info->interline_spacing+draw_info->stroke_width+0.5);
199 if (SetImageBackgroundColor(image) == MagickFalse)
201 InheritException(exception,&image->exception);
202 image=DestroyImageList(image);
203 return((Image *) NULL);
208 (void) CloneString(&draw_info->text,caption);
209 status=GetMultilineTypeMetrics(image,draw_info,&metrics);
210 if ((draw_info->gravity != UndefinedGravity) &&
211 (draw_info->direction != RightToLeftDirection))
212 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
215 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
216 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
217 draw_info->stroke_width/2.0);
218 if (draw_info->direction == RightToLeftDirection)
219 (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g",
220 image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0),
221 metrics.ascent+draw_info->stroke_width/2.0);
222 draw_info->geometry=AcquireString(geometry);
224 (void) AnnotateImage(image,draw_info);
225 draw_info=DestroyDrawInfo(draw_info);
226 caption=DestroyString(caption);
227 return(GetFirstImageInList(image));
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 % R e g i s t e r C A P T I O N I m a g e %
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % RegisterCAPTIONImage() adds attributes for the CAPTION image format to
242 % the list of supported formats. The attributes include the image format
243 % tag, a method to read and/or write the format, whether the format
244 % supports the saving of more than one frame to the same file or blob,
245 % whether the format supports native in-memory I/O, and a brief
246 % description of the format.
248 % The format of the RegisterCAPTIONImage method is:
250 % size_t RegisterCAPTIONImage(void)
253 ModuleExport size_t RegisterCAPTIONImage(void)
258 entry=SetMagickInfo("CAPTION");
259 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
260 entry->adjoin=MagickFalse;
261 entry->format_type=ImplicitFormatType;
262 entry->description=ConstantString("Image caption");
263 entry->module=ConstantString("CAPTION");
264 (void) RegisterMagickInfo(entry);
265 return(MagickImageCoderSignature);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % U n r e g i s t e r C A P T I O N I m a g e %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % UnregisterCAPTIONImage() removes format registrations made by the
280 % CAPTION module from the list of supported formats.
282 % The format of the UnregisterCAPTIONImage method is:
284 % UnregisterCAPTIONImage(void)
287 ModuleExport void UnregisterCAPTIONImage(void)
289 (void) UnregisterMagickInfo("CAPTION");