blob: ff89b775c2c29110c0bba03d89fdd99e857a19f3 [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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
42#include "magick/studio.h"
43#include "magick/annotate.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/draw.h"
47#include "magick/exception.h"
48#include "magick/exception-private.h"
49#include "magick/image.h"
50#include "magick/image-private.h"
51#include "magick/list.h"
52#include "magick/magick.h"
53#include "magick/memory_.h"
54#include "magick/option.h"
55#include "magick/property.h"
56#include "magick/quantum-private.h"
57#include "magick/static.h"
58#include "magick/string_.h"
59#include "magick/module.h"
60
61/*
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63% %
64% %
65% %
66% R e a d C A P T I O N I m a g e %
67% %
68% %
69% %
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71%
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.
75%
76% The format of the ReadCAPTIONImage method is:
77%
78% Image *ReadCAPTIONImage(const ImageInfo *image_info,
79% ExceptionInfo *exception)
80%
81% A description of each parameter follows:
82%
83% o image_info: the image info.
84%
85% o exception: return any errors or warnings in this structure.
86%
87*/
88static Image *ReadCAPTIONImage(const ImageInfo *image_info,
89 ExceptionInfo *exception)
90{
91 char
92 *caption,
93 geometry[MaxTextExtent],
94 *property;
95
96 const char
97 *gravity;
98
99 DrawInfo
100 *draw_info;
101
102 Image
103 *image;
104
105 MagickBooleanType
106 status;
107
cristybb503372010-05-27 20:51:26 +0000108 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000109 i;
110
111 TypeMetric
112 metrics;
113
cristybb503372010-05-27 20:51:26 +0000114 size_t
cristy3ed852e2009-09-05 21:47:34 +0000115 height,
116 width;
117
118 /*
119 Initialize Image structure.
120 */
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");
132 /*
133 Format caption.
134 */
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) ParseMagickOption(MagickGravityOptions,
144 MagickFalse,gravity);
145 if ((*caption != '\0') && (image->rows != 0) &&
146 (image_info->pointsize == 0.0))
147 {
148 char
149 *text;
150
151 /*
152 Scale text to fit bounding box.
153 */
154 for ( ; ; )
155 {
156 text=AcquireString(caption);
cristy6b1d05e2010-09-22 19:17:27 +0000157 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text);
cristy3ed852e2009-09-05 21:47:34 +0000158 (void) CloneString(&draw_info->text,text);
159 text=DestroyString(text);
160 (void) FormatMagickString(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);
cristybb503372010-05-27 20:51:26 +0000165 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
166 height=(size_t) floor(metrics.height+draw_info->stroke_width+
cristy06609ee2010-03-17 20:21:27 +0000167 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000168 if ((width > (image->columns+1)) || (height > (image->rows+1)))
169 break;
170 draw_info->pointsize*=2.0;
171 }
172 draw_info->pointsize/=2.0;
173 for ( ; ; )
174 {
175 text=AcquireString(caption);
cristy6b1d05e2010-09-22 19:17:27 +0000176 i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text);
cristy3ed852e2009-09-05 21:47:34 +0000177 (void) CloneString(&draw_info->text,text);
178 text=DestroyString(text);
179 (void) FormatMagickString(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);
cristybb503372010-05-27 20:51:26 +0000184 width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5);
185 height=(size_t) floor(metrics.height+draw_info->stroke_width+
cristy06609ee2010-03-17 20:21:27 +0000186 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000187 if ((width > (image->columns+1)) || (height > (image->rows+1)))
188 break;
189 draw_info->pointsize++;
190 }
191 draw_info->pointsize--;
192 }
cristy6b1d05e2010-09-22 19:17:27 +0000193 i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption);
cristy3ed852e2009-09-05 21:47:34 +0000194 if (image->rows == 0)
cristybb503372010-05-27 20:51:26 +0000195 image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+
cristy3ed852e2009-09-05 21:47:34 +0000196 draw_info->stroke_width)+0.5);
197 if (image->rows == 0)
cristybb503372010-05-27 20:51:26 +0000198 image->rows=(size_t) ((i+1)*draw_info->pointsize+
cristy3ed852e2009-09-05 21:47:34 +0000199 draw_info->stroke_width+0.5);
cristy95524f92010-02-16 18:44:34 +0000200 if (SetImageBackgroundColor(image) == MagickFalse)
201 {
202 InheritException(exception,&image->exception);
203 image=DestroyImageList(image);
204 return((Image *) NULL);
205 }
cristy3ed852e2009-09-05 21:47:34 +0000206 /*
207 Draw caption.
208 */
209 (void) CloneString(&draw_info->text,caption);
210 status=GetMultilineTypeMetrics(image,draw_info,&metrics);
211 if (draw_info->gravity != UndefinedGravity)
cristybb503372010-05-27 20:51:26 +0000212 image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0);
cristy3ed852e2009-09-05 21:47:34 +0000213 else
214 {
215 (void) FormatMagickString(geometry,MaxTextExtent,"%+g%+g",
216 -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+
217 draw_info->stroke_width/2.0);
218 (void) CloneString(&draw_info->geometry,geometry);
219 }
220 (void) AnnotateImage(image,draw_info);
221 draw_info=DestroyDrawInfo(draw_info);
222 caption=DestroyString(caption);
223 return(GetFirstImageInList(image));
224}
225
226/*
227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228% %
229% %
230% %
231% R e g i s t e r C A P T I O N I m a g e %
232% %
233% %
234% %
235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236%
237% RegisterCAPTIONImage() adds attributes for the CAPTION image format to
238% the list of supported formats. The attributes include the image format
239% tag, a method to read and/or write the format, whether the format
240% supports the saving of more than one frame to the same file or blob,
241% whether the format supports native in-memory I/O, and a brief
242% description of the format.
243%
244% The format of the RegisterCAPTIONImage method is:
245%
cristybb503372010-05-27 20:51:26 +0000246% size_t RegisterCAPTIONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000247%
248*/
cristybb503372010-05-27 20:51:26 +0000249ModuleExport size_t RegisterCAPTIONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000250{
251 MagickInfo
252 *entry;
253
254 entry=SetMagickInfo("CAPTION");
255 entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage;
256 entry->adjoin=MagickFalse;
cristy009d7392010-07-25 22:08:41 +0000257 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000258 entry->description=ConstantString("Image caption");
259 entry->module=ConstantString("CAPTION");
260 (void) RegisterMagickInfo(entry);
261 return(MagickImageCoderSignature);
262}
263
264/*
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266% %
267% %
268% %
269% U n r e g i s t e r C A P T I O N I m a g e %
270% %
271% %
272% %
273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274%
275% UnregisterCAPTIONImage() removes format registrations made by the
276% CAPTION module from the list of supported formats.
277%
278% The format of the UnregisterCAPTIONImage method is:
279%
280% UnregisterCAPTIONImage(void)
281%
282*/
283ModuleExport void UnregisterCAPTIONImage(void)
284{
285 (void) UnregisterMagickInfo("CAPTION");
286}