cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % L AAA BBBB EEEEE L % |
| 7 | % L A A B B E L % |
| 8 | % L AAAAA BBBB EEE L % |
| 9 | % L A A B B E L % |
| 10 | % LLLLL A A BBBB EEEEE LLLLL % |
| 11 | % % |
| 12 | % % |
| 13 | % Read ASCII String As An Image. % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 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" |
cristy | 76ce6e1 | 2013-04-05 14:33:38 +0000 | [diff] [blame] | 44 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 45 | #include "MagickCore/blob.h" |
| 46 | #include "MagickCore/blob-private.h" |
| 47 | #include "MagickCore/draw.h" |
| 48 | #include "MagickCore/exception.h" |
| 49 | #include "MagickCore/exception-private.h" |
| 50 | #include "MagickCore/image.h" |
| 51 | #include "MagickCore/image-private.h" |
| 52 | #include "MagickCore/list.h" |
| 53 | #include "MagickCore/magick.h" |
| 54 | #include "MagickCore/memory_.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" |
| 60 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 64 | % % |
| 65 | % % |
| 66 | % % |
| 67 | % R e a d L A B E L I m a g e % |
| 68 | % % |
| 69 | % % |
| 70 | % % |
| 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | % |
| 73 | % ReadLABELImage() reads a LABEL image file and returns it. It |
| 74 | % allocates the memory necessary for the new Image structure and returns a |
| 75 | % pointer to the new image. |
| 76 | % |
| 77 | % The format of the ReadLABELImage method is: |
| 78 | % |
| 79 | % Image *ReadLABELImage(const ImageInfo *image_info, |
| 80 | % ExceptionInfo *exception) |
| 81 | % |
| 82 | % A description of each parameter follows: |
| 83 | % |
| 84 | % o image_info: the image info. |
| 85 | % |
| 86 | % o exception: return any errors or warnings in this structure. |
| 87 | % |
| 88 | */ |
| 89 | static Image *ReadLABELImage(const ImageInfo *image_info, |
| 90 | ExceptionInfo *exception) |
| 91 | { |
| 92 | char |
| 93 | geometry[MaxTextExtent], |
| 94 | *property; |
| 95 | |
| 96 | const char |
| 97 | *label; |
| 98 | |
| 99 | DrawInfo |
| 100 | *draw_info; |
| 101 | |
| 102 | Image |
| 103 | *image; |
| 104 | |
| 105 | MagickBooleanType |
| 106 | status; |
| 107 | |
| 108 | TypeMetric |
| 109 | metrics; |
| 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 | |
| 115 | /* |
| 116 | Initialize Image structure. |
| 117 | */ |
| 118 | assert(image_info != (const ImageInfo *) NULL); |
| 119 | assert(image_info->signature == MagickSignature); |
| 120 | if (image_info->debug != MagickFalse) |
| 121 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 122 | image_info->filename); |
| 123 | assert(exception != (ExceptionInfo *) NULL); |
| 124 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 125 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 126 | (void) ResetImagePage(image,"0x0+0+0"); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 127 | property=InterpretImageProperties(image_info,image,image_info->filename, |
| 128 | exception); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 129 | (void) SetImageProperty(image,"label",property,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 130 | property=DestroyString(property); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 131 | label=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 132 | draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); |
| 133 | draw_info->text=ConstantString(label); |
cristy | a492593 | 2013-04-14 21:14:20 +0000 | [diff] [blame^] | 134 | if (((image->columns == 0) || (image->rows == 0)) && |
| 135 | (fabs(image_info->pointsize) < MagickEpsilon)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | { |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 137 | double |
| 138 | high, |
| 139 | low; |
| 140 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 141 | /* |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 142 | Auto fit text into bounding box. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | */ |
cristy | 555a8a7 | 2013-03-14 00:13:29 +0000 | [diff] [blame] | 144 | for ( ; ; draw_info->pointsize*=2.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | { |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 146 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
| 147 | -metrics.bounds.x1,metrics.ascent); |
| 148 | if (draw_info->gravity == UndefinedGravity) |
| 149 | (void) CloneString(&draw_info->geometry,geometry); |
| 150 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 151 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
anthony | d7c0545 | 2010-09-12 01:02:02 +0000 | [diff] [blame] | 152 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | a492593 | 2013-04-14 21:14:20 +0000 | [diff] [blame^] | 153 | if (((image->columns != 0) && (width >= image->columns)) || |
| 154 | ((image->rows != 0) && (height >= image->rows))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | break; |
cristy | a492593 | 2013-04-14 21:14:20 +0000 | [diff] [blame^] | 156 | if (((image->columns != 0) && (width >= (2*image->columns))) || |
| 157 | ((image->rows != 0) && (height >= (2*image->rows)))) |
cristy | 8b235b2 | 2013-03-01 12:35:09 +0000 | [diff] [blame] | 158 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | } |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 160 | high=draw_info->pointsize/2.0; |
cristy | 6cd2947 | 2013-03-14 13:21:34 +0000 | [diff] [blame] | 161 | for (low=high/2.0; (high-low) > 1.0; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 162 | { |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 163 | draw_info->pointsize=(low+high)/2.0; |
| 164 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
| 165 | -metrics.bounds.x1,metrics.ascent); |
| 166 | if (draw_info->gravity == UndefinedGravity) |
| 167 | (void) CloneString(&draw_info->geometry,geometry); |
| 168 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 169 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
anthony | d7c0545 | 2010-09-12 01:02:02 +0000 | [diff] [blame] | 170 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | a492593 | 2013-04-14 21:14:20 +0000 | [diff] [blame^] | 171 | if (((image->columns != 0) && (width < image->columns)) || |
| 172 | ((image->rows != 0) && (height < image->rows))) |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 173 | low=draw_info->pointsize+1.0; |
| 174 | else |
| 175 | high=draw_info->pointsize-1.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | } |
cristy | 555a8a7 | 2013-03-14 00:13:29 +0000 | [diff] [blame] | 177 | for (draw_info->pointsize=(low+high)/2.0; (high-low) > 1.0; ) |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 178 | { |
| 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,exception); |
| 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); |
cristy | a492593 | 2013-04-14 21:14:20 +0000 | [diff] [blame^] | 186 | if (((image->columns != 0) && (width < image->columns)) || |
| 187 | ((image->rows != 0) && (height < image->rows))) |
cristy | dce9cde | 2012-08-22 12:58:16 +0000 | [diff] [blame] | 188 | break; |
| 189 | draw_info->pointsize--; |
| 190 | } |
cristy | ec0bf4b | 2013-04-01 19:24:36 +0000 | [diff] [blame] | 191 | draw_info->pointsize--; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 193 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | if (status == MagickFalse) |
| 195 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 196 | image=DestroyImageList(image); |
| 197 | return((Image *) NULL); |
| 198 | } |
cristy | a492593 | 2013-04-14 21:14:20 +0000 | [diff] [blame^] | 199 | if (image->columns == 0) |
| 200 | image->columns=(size_t) (metrics.width+draw_info->stroke_width+0.5); |
| 201 | if (image->columns == 0) |
| 202 | image->columns=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5); |
| 203 | if (image->rows == 0) |
| 204 | image->rows=(size_t) (metrics.ascent-metrics.descent+ |
| 205 | draw_info->stroke_width+0.5); |
| 206 | if (image->rows == 0) |
| 207 | image->rows=(size_t) (draw_info->pointsize+draw_info->stroke_width+0.5); |
cristy | a2f09b9 | 2012-05-01 12:07:49 +0000 | [diff] [blame] | 208 | if (draw_info->gravity == UndefinedGravity) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 209 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 210 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 211 | -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ |
| 212 | draw_info->stroke_width/2.0); |
cristy | a2f09b9 | 2012-05-01 12:07:49 +0000 | [diff] [blame] | 213 | (void) CloneString(&draw_info->geometry,geometry); |
| 214 | } |
| 215 | if (draw_info->direction == RightToLeftDirection) |
| 216 | { |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 217 | if (draw_info->direction == RightToLeftDirection) |
| 218 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
| 219 | image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), |
cristy | b93d54f | 2012-07-01 01:32:47 +0000 | [diff] [blame] | 220 | metrics.ascent+draw_info->stroke_width/2.0); |
cristy | a2f09b9 | 2012-05-01 12:07:49 +0000 | [diff] [blame] | 221 | (void) CloneString(&draw_info->geometry,geometry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 222 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 223 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 224 | { |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 225 | image=DestroyImageList(image); |
| 226 | return((Image *) NULL); |
| 227 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 228 | (void) AnnotateImage(image,draw_info,exception); |
cristy | b70a486 | 2010-06-18 01:18:44 +0000 | [diff] [blame] | 229 | if (image_info->pointsize == 0.0) |
| 230 | { |
| 231 | char |
| 232 | pointsize[MaxTextExtent]; |
| 233 | |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 234 | (void) FormatLocaleString(pointsize,MaxTextExtent,"%.20g", |
cristy | b70a486 | 2010-06-18 01:18:44 +0000 | [diff] [blame] | 235 | draw_info->pointsize); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 236 | (void) SetImageProperty(image,"label:pointsize",pointsize,exception); |
cristy | b70a486 | 2010-06-18 01:18:44 +0000 | [diff] [blame] | 237 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | draw_info=DestroyDrawInfo(draw_info); |
| 239 | return(GetFirstImageInList(image)); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 244 | % % |
| 245 | % % |
| 246 | % % |
| 247 | % R e g i s t e r L A B E L I m a g e % |
| 248 | % % |
| 249 | % % |
| 250 | % % |
| 251 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 252 | % |
| 253 | % RegisterLABELImage() adds properties for the LABEL image format to |
| 254 | % the list of supported formats. The properties include the image format |
| 255 | % tag, a method to read and/or write the format, whether the format |
| 256 | % supports the saving of more than one frame to the same file or blob, |
| 257 | % whether the format supports native in-memory I/O, and a brief |
| 258 | % description of the format. |
| 259 | % |
| 260 | % The format of the RegisterLABELImage method is: |
| 261 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 262 | % size_t RegisterLABELImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 263 | % |
| 264 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 265 | ModuleExport size_t RegisterLABELImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 266 | { |
| 267 | MagickInfo |
| 268 | *entry; |
| 269 | |
| 270 | entry=SetMagickInfo("LABEL"); |
| 271 | entry->decoder=(DecodeImageHandler *) ReadLABELImage; |
| 272 | entry->adjoin=MagickFalse; |
cristy | 009d739 | 2010-07-25 22:08:41 +0000 | [diff] [blame] | 273 | entry->format_type=ImplicitFormatType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | entry->description=ConstantString("Image label"); |
| 275 | entry->module=ConstantString("LABEL"); |
| 276 | (void) RegisterMagickInfo(entry); |
| 277 | return(MagickImageCoderSignature); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 282 | % % |
| 283 | % % |
| 284 | % % |
| 285 | % U n r e g i s t e r L A B E L I m a g e % |
| 286 | % % |
| 287 | % % |
| 288 | % % |
| 289 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 290 | % |
| 291 | % UnregisterLABELImage() removes format registrations made by the |
| 292 | % LABEL module from the list of supported formats. |
| 293 | % |
| 294 | % The format of the UnregisterLABELImage method is: |
| 295 | % |
| 296 | % UnregisterLABELImage(void) |
| 297 | % |
| 298 | */ |
| 299 | ModuleExport void UnregisterLABELImage(void) |
| 300 | { |
| 301 | (void) UnregisterMagickInfo("LABEL"); |
| 302 | } |