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 | 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/property.h" |
| 55 | #include "MagickCore/quantum-private.h" |
| 56 | #include "MagickCore/static.h" |
| 57 | #include "MagickCore/string_.h" |
| 58 | #include "MagickCore/module.h" |
| 59 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 63 | % % |
| 64 | % % |
| 65 | % % |
| 66 | % R e a d L A B E L I m a g e % |
| 67 | % % |
| 68 | % % |
| 69 | % % |
| 70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 71 | % |
| 72 | % ReadLABELImage() reads a LABEL 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 ReadLABELImage method is: |
| 77 | % |
| 78 | % Image *ReadLABELImage(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 *ReadLABELImage(const ImageInfo *image_info, |
| 89 | ExceptionInfo *exception) |
| 90 | { |
| 91 | char |
| 92 | geometry[MaxTextExtent], |
| 93 | *property; |
| 94 | |
| 95 | const char |
| 96 | *label; |
| 97 | |
| 98 | DrawInfo |
| 99 | *draw_info; |
| 100 | |
| 101 | Image |
| 102 | *image; |
| 103 | |
| 104 | MagickBooleanType |
| 105 | status; |
| 106 | |
| 107 | TypeMetric |
| 108 | metrics; |
| 109 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 110 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 111 | height, |
| 112 | width; |
| 113 | |
| 114 | /* |
| 115 | Initialize Image structure. |
| 116 | */ |
| 117 | assert(image_info != (const ImageInfo *) NULL); |
| 118 | assert(image_info->signature == MagickSignature); |
| 119 | if (image_info->debug != MagickFalse) |
| 120 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 121 | image_info->filename); |
| 122 | assert(exception != (ExceptionInfo *) NULL); |
| 123 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 124 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 125 | (void) ResetImagePage(image,"0x0+0+0"); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 126 | property=InterpretImageProperties(image_info,image,image_info->filename, |
| 127 | exception); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 128 | (void) SetImageProperty(image,"label",property,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | property=DestroyString(property); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 130 | label=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL); |
| 132 | draw_info->text=ConstantString(label); |
| 133 | if (((image->columns != 0) || (image->rows != 0)) && |
| 134 | (image_info->pointsize == 0.0)) |
| 135 | { |
| 136 | /* |
| 137 | Fit label to canvas size. |
| 138 | */ |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 139 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 140 | for ( ; status != MagickFalse; draw_info->pointsize*=2.0) |
| 141 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 142 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
anthony | d7c0545 | 2010-09-12 01:02:02 +0000 | [diff] [blame] | 143 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 144 | if (((image->columns != 0) && (width >= image->columns)) || |
| 145 | ((image->rows != 0) && (height >= image->rows))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 146 | break; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 147 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 148 | } |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 149 | draw_info->pointsize/=2.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | for ( ; status != MagickFalse; draw_info->pointsize--) |
| 151 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 152 | width=(size_t) floor(metrics.width+draw_info->stroke_width+0.5); |
anthony | d7c0545 | 2010-09-12 01:02:02 +0000 | [diff] [blame] | 153 | height=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 154 | if ((image->columns != 0) && (width < image->columns) && |
| 155 | ((image->rows == 0) || (height < image->rows))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 156 | break; |
cristy | e2c81ad | 2011-08-20 22:54:14 +0000 | [diff] [blame] | 157 | if ((image->rows != 0) && (height < image->rows) && |
| 158 | ((image->columns == 0) || (width < image->columns))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | break; |
| 160 | if (draw_info->pointsize < 2.0) |
| 161 | break; |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 162 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 165 | status=GetMultilineTypeMetrics(image,draw_info,&metrics,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 166 | if (status == MagickFalse) |
| 167 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 168 | image=DestroyImageList(image); |
| 169 | return((Image *) NULL); |
| 170 | } |
| 171 | if (image->columns == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 172 | image->columns=(size_t) (metrics.width+draw_info->stroke_width+1.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | if (image->columns == 0) |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 174 | image->columns=(size_t) (draw_info->pointsize+draw_info->stroke_width+1.5); |
| 175 | if ((draw_info->gravity == UndefinedGravity) || |
| 176 | (draw_info->direction == RightToLeftDirection)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 178 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 179 | -metrics.bounds.x1+draw_info->stroke_width/2.0,metrics.ascent+ |
| 180 | draw_info->stroke_width/2.0); |
cristy | 024843f | 2011-06-03 17:52:52 +0000 | [diff] [blame] | 181 | if (draw_info->direction == RightToLeftDirection) |
| 182 | (void) FormatLocaleString(geometry,MaxTextExtent,"%+g%+g", |
| 183 | image->columns-(metrics.bounds.x2+draw_info->stroke_width/2.0), |
| 184 | metrics.ascent+draw_info->stroke_width/2.0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 185 | draw_info->geometry=AcquireString(geometry); |
| 186 | } |
| 187 | if (image->rows == 0) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 188 | image->rows=(size_t) floor(metrics.height+draw_info->stroke_width+0.5); |
cristy | 06609ee | 2010-03-17 20:21:27 +0000 | [diff] [blame] | 189 | if (image->rows == 0) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 190 | image->rows=(size_t) floor(draw_info->pointsize+draw_info->stroke_width+ |
| 191 | 0.5); |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 192 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 193 | { |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 194 | image=DestroyImageList(image); |
| 195 | return((Image *) NULL); |
| 196 | } |
cristy | 5cbc016 | 2011-08-29 00:36:28 +0000 | [diff] [blame] | 197 | (void) AnnotateImage(image,draw_info,exception); |
cristy | b70a486 | 2010-06-18 01:18:44 +0000 | [diff] [blame] | 198 | if (image_info->pointsize == 0.0) |
| 199 | { |
| 200 | char |
| 201 | pointsize[MaxTextExtent]; |
| 202 | |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 203 | (void) FormatLocaleString(pointsize,MaxTextExtent,"%.20g", |
cristy | b70a486 | 2010-06-18 01:18:44 +0000 | [diff] [blame] | 204 | draw_info->pointsize); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 205 | (void) SetImageProperty(image,"label:pointsize",pointsize,exception); |
cristy | b70a486 | 2010-06-18 01:18:44 +0000 | [diff] [blame] | 206 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | draw_info=DestroyDrawInfo(draw_info); |
| 208 | return(GetFirstImageInList(image)); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 213 | % % |
| 214 | % % |
| 215 | % % |
| 216 | % R e g i s t e r L A B E L I m a g e % |
| 217 | % % |
| 218 | % % |
| 219 | % % |
| 220 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 221 | % |
| 222 | % RegisterLABELImage() adds properties for the LABEL image format to |
| 223 | % the list of supported formats. The properties include the image format |
| 224 | % tag, a method to read and/or write the format, whether the format |
| 225 | % supports the saving of more than one frame to the same file or blob, |
| 226 | % whether the format supports native in-memory I/O, and a brief |
| 227 | % description of the format. |
| 228 | % |
| 229 | % The format of the RegisterLABELImage method is: |
| 230 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 231 | % size_t RegisterLABELImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 232 | % |
| 233 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 234 | ModuleExport size_t RegisterLABELImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 235 | { |
| 236 | MagickInfo |
| 237 | *entry; |
| 238 | |
| 239 | entry=SetMagickInfo("LABEL"); |
| 240 | entry->decoder=(DecodeImageHandler *) ReadLABELImage; |
| 241 | entry->adjoin=MagickFalse; |
cristy | 009d739 | 2010-07-25 22:08:41 +0000 | [diff] [blame] | 242 | entry->format_type=ImplicitFormatType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 243 | entry->description=ConstantString("Image label"); |
| 244 | entry->module=ConstantString("LABEL"); |
| 245 | (void) RegisterMagickInfo(entry); |
| 246 | return(MagickImageCoderSignature); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 251 | % % |
| 252 | % % |
| 253 | % % |
| 254 | % U n r e g i s t e r L A B E L I m a g e % |
| 255 | % % |
| 256 | % % |
| 257 | % % |
| 258 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 259 | % |
| 260 | % UnregisterLABELImage() removes format registrations made by the |
| 261 | % LABEL module from the list of supported formats. |
| 262 | % |
| 263 | % The format of the UnregisterLABELImage method is: |
| 264 | % |
| 265 | % UnregisterLABELImage(void) |
| 266 | % |
| 267 | */ |
| 268 | ModuleExport void UnregisterLABELImage(void) |
| 269 | { |
| 270 | (void) UnregisterMagickInfo("LABEL"); |
| 271 | } |