cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | % 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | */ |
| 88 | static 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 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 108 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 109 | i; |
| 110 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 111 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 112 | height, |
| 113 | width; |
| 114 | |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 115 | TypeMetric |
| 116 | metrics; |
| 117 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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) |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 143 | draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 157 | i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text, |
| 158 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | (void) CloneString(&draw_info->text,text); |
| 160 | text=DestroyString(text); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 161 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 162 | -metrics.bounds.x1,metrics.ascent); |
| 163 | if (draw_info->gravity == UndefinedGravity) |
| 164 | (void) CloneString(&draw_info->geometry,geometry); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 165 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 166 | (void) status; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 167 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 168 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 169 | if ((width > (image->columns+1)) || (height > (image->rows+1))) |
| 170 | break; |
| 171 | draw_info->pointsize*=2.0; |
| 172 | } |
| 173 | draw_info->pointsize/=2.0; |
| 174 | for ( ; ; ) |
| 175 | { |
| 176 | text=AcquireString(caption); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 177 | i=FormatMagickCaption(image,draw_info,MagickFalse,&metrics,&text, |
| 178 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 179 | (void) CloneString(&draw_info->text,text); |
| 180 | text=DestroyString(text); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 181 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | -metrics.bounds.x1,metrics.ascent); |
| 183 | if (draw_info->gravity == UndefinedGravity) |
| 184 | (void) CloneString(&draw_info->geometry,geometry); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 185 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 186 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
cristy | 4e82e51 | 2011-04-24 01:33:42 +0000 | [diff] [blame] | 187 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 188 | if ((width > (image->columns+1)) || (height > (image->rows+1))) |
| 189 | break; |
| 190 | draw_info->pointsize++; |
| 191 | } |
| 192 | draw_info->pointsize--; |
| 193 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 194 | i=FormatMagickCaption(image,draw_info,MagickTrue,&metrics,&caption,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 195 | if (image->rows == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 196 | image->rows=(size_t) ((i+1)*(metrics.ascent-metrics.descent+ |
cristy | cc2fb02 | 2011-04-01 14:08:37 +0000 | [diff] [blame] | 197 | draw_info->interline_spacing+draw_info->stroke_width)+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 198 | if (image->rows == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 199 | image->rows=(size_t) ((i+1)*draw_info->pointsize+ |
cristy | cc2fb02 | 2011-04-01 14:08:37 +0000 | [diff] [blame] | 200 | draw_info->interline_spacing+draw_info->stroke_width+0.5); |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 201 | if (SetImageBackgroundColor(image) == MagickFalse) |
| 202 | { |
| 203 | InheritException(exception,&image->exception); |
| 204 | image=DestroyImageList(image); |
| 205 | return((Image *) NULL); |
| 206 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | /* |
| 208 | Draw caption. |
| 209 | */ |
| 210 | (void) CloneString(&draw_info->text,caption); |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 211 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 212 | if ((draw_info->gravity != UndefinedGravity) && |
| 213 | (draw_info->direction != RightToLeftDirection)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 214 | image->page.x=(ssize_t) (metrics.bounds.x1-draw_info->stroke_width/2.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 215 | else |
| 216 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 217 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ |
| 219 | draw_info->stroke_width/2.0); |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 220 | if (draw_info->direction == RightToLeftDirection) |
| 221 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
| 222 | image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), |
| 223 | metrics.ascent+draw_info->stroke_width/2.0); |
| 224 | draw_info->geometry=AcquireString(geometry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 225 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 226 | (void) AnnotateImage(image,draw_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | draw_info=DestroyDrawInfo(draw_info); |
| 228 | caption=DestroyString(caption); |
| 229 | return(GetFirstImageInList(image)); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 234 | % % |
| 235 | % % |
| 236 | % % |
| 237 | % R e g i s t e r C A P T I O N I m a g e % |
| 238 | % % |
| 239 | % % |
| 240 | % % |
| 241 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 242 | % |
| 243 | % RegisterCAPTIONImage() adds attributes for the CAPTION image format to |
| 244 | % the list of supported formats. The attributes include the image format |
| 245 | % tag, a method to read and/or write the format, whether the format |
| 246 | % supports the saving of more than one frame to the same file or blob, |
| 247 | % whether the format supports native in-memory I/O, and a brief |
| 248 | % description of the format. |
| 249 | % |
| 250 | % The format of the RegisterCAPTIONImage method is: |
| 251 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 252 | % size_t RegisterCAPTIONImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 253 | % |
| 254 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 255 | ModuleExport size_t RegisterCAPTIONImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 256 | { |
| 257 | MagickInfo |
| 258 | *entry; |
| 259 | |
| 260 | entry=SetMagickInfo("CAPTION"); |
| 261 | entry->decoder=(DecodeImageHandler *) ReadCAPTIONImage; |
| 262 | entry->adjoin=MagickFalse; |
cristy | 009d739 | 2010-07-25 22:08:41 +0000 | [diff] [blame] | 263 | entry->format_type=ImplicitFormatType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | entry->description=ConstantString("Image caption"); |
| 265 | entry->module=ConstantString("CAPTION"); |
| 266 | (void) RegisterMagickInfo(entry); |
| 267 | return(MagickImageCoderSignature); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 272 | % % |
| 273 | % % |
| 274 | % % |
| 275 | % U n r e g i s t e r C A P T I O N I m a g e % |
| 276 | % % |
| 277 | % % |
| 278 | % % |
| 279 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 280 | % |
| 281 | % UnregisterCAPTIONImage() removes format registrations made by the |
| 282 | % CAPTION module from the list of supported formats. |
| 283 | % |
| 284 | % The format of the UnregisterCAPTIONImage method is: |
| 285 | % |
| 286 | % UnregisterCAPTIONImage(void) |
| 287 | % |
| 288 | */ |
| 289 | ModuleExport void UnregisterCAPTIONImage(void) |
| 290 | { |
| 291 | (void) UnregisterMagickInfo("CAPTION"); |
| 292 | } |