2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Render Text Onto A Canvas Image. %
20 % Copyright 1999-2009 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 "magick/studio.h"
43 #include "magick/annotate.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/constitute.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/pixel-private.h"
64 #include "magick/quantum-private.h"
65 #include "magick/static.h"
66 #include "magick/statistic.h"
67 #include "magick/string_.h"
68 #include "magick/module.h"
73 static MagickBooleanType
74 WriteTXTImage(const ImageInfo *,Image *);
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % IsTXT() returns MagickTrue if the image format type, identified by the magick
90 % The format of the IsTXT method is:
92 % MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
94 % A description of each parameter follows:
96 % o magick: compare image format pattern against these bytes.
98 % o length: Specifies the length of the magick string.
101 static MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
103 #define MagickID "# ImageMagick pixel enumeration:"
106 colorspace[MaxTextExtent];
118 if (LocaleNCompare((const char *) magick,MagickID,strlen(MagickID)) != 0)
120 count=(ssize_t) sscanf((const char *) magick+32,"%lu,%lu,%lu,%s",&columns,
121 &rows,&depth,colorspace);
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 % R e a d T E X T I m a g e %
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % ReadTEXTImage() reads a text file and returns it as an image. It
139 % allocates the memory necessary for the new Image structure and returns a
140 % pointer to the new image.
142 % The format of the ReadTEXTImage method is:
144 % Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
145 % char *text,ExceptionInfo *exception)
147 % A description of each parameter follows:
149 % o image_info: the image info.
151 % o image: the image.
153 % o text: the text storage buffer.
155 % o exception: return any errors or warnings in this structure.
158 static Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
159 char *text,ExceptionInfo *exception)
162 filename[MaxTextExtent],
163 geometry[MaxTextExtent],
190 assert(image_info != (const ImageInfo *) NULL);
191 assert(image_info->signature == MagickSignature);
192 if (image_info->debug != MagickFalse)
193 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
194 image_info->filename);
195 assert(exception != (ExceptionInfo *) NULL);
196 assert(exception->signature == MagickSignature);
198 Set the page geometry.
200 delta.x=DefaultResolution;
201 delta.y=DefaultResolution;
202 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
210 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
211 image->x_resolution=geometry_info.rho;
212 image->y_resolution=geometry_info.sigma;
213 if ((flags & SigmaValue) == 0)
214 image->y_resolution=image->x_resolution;
220 if (image_info->page != (char *) NULL)
221 (void) ParseAbsoluteGeometry(image_info->page,&page);
223 Initialize Image structure.
225 image->columns=(unsigned long) (((page.width*image->x_resolution)/
227 image->rows=(unsigned long) (((page.height*image->y_resolution)/delta.y)+0.5);
230 texture=(Image *) NULL;
231 if (image_info->texture != (char *) NULL)
236 read_info=CloneImageInfo(image_info);
237 SetImageInfoBlob(read_info,(void *) NULL,0);
238 (void) CopyMagickString(read_info->filename,image_info->texture,
240 texture=ReadImage(read_info,exception);
241 read_info=DestroyImageInfo(read_info);
244 Annotate the text image.
246 (void) SetImageBackgroundColor(image);
247 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
248 (void) CloneString(&draw_info->text,image_info->filename);
249 (void) FormatMagickString(geometry,MaxTextExtent,"0x0%+ld%+ld",page.x,page.y);
250 (void) CloneString(&draw_info->geometry,geometry);
251 status=GetTypeMetrics(image,draw_info,&metrics);
252 if (status == MagickFalse)
253 ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
254 page.y=(long) (page.y+metrics.ascent+0.5);
255 (void) FormatMagickString(geometry,MaxTextExtent,"0x0%+ld%+ld",page.x,page.y);
256 (void) CloneString(&draw_info->geometry,geometry);
257 (void) CopyMagickString(filename,image_info->filename,MaxTextExtent);
258 if (*draw_info->text != '\0')
259 *draw_info->text='\0';
261 for (offset=2*page.y; p != (char *) NULL; )
264 Annotate image with text.
266 (void) ConcatenateString(&draw_info->text,text);
267 (void) ConcatenateString(&draw_info->text,"\n");
268 offset+=(long) (metrics.ascent-metrics.descent);
269 if (image->previous == (Image *) NULL)
271 status=SetImageProgress(image,LoadImageTag,offset,image->rows);
272 if (status == MagickFalse)
275 p=ReadBlobString(image,text);
276 if ((offset < (long) image->rows) && (p != (char *) NULL))
278 if (texture != (Image *) NULL)
280 MagickProgressMonitor
283 progress_monitor=SetImageProgressMonitor(image,
284 (MagickProgressMonitor) NULL,image->client_data);
285 (void) TextureImage(image,texture);
286 (void) SetImageProgressMonitor(image,progress_monitor,
289 (void) AnnotateImage(image,draw_info);
290 if (p == (char *) NULL)
293 Page is full-- allocate next image structure.
295 *draw_info->text='\0';
297 AcquireNextImage(image_info,image);
298 if (GetNextImageInList(image) == (Image *) NULL)
300 image=DestroyImageList(image);
301 return((Image *) NULL);
303 image->next->columns=image->columns;
304 image->next->rows=image->rows;
305 image=SyncNextImageInList(image);
306 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
307 (void) SetImageBackgroundColor(image);
308 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
310 if (status == MagickFalse)
313 if (texture != (Image *) NULL)
315 MagickProgressMonitor
318 progress_monitor=SetImageProgressMonitor(image,
319 (MagickProgressMonitor) NULL,image->client_data);
320 (void) TextureImage(image,texture);
321 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
323 (void) AnnotateImage(image,draw_info);
324 if (texture != (Image *) NULL)
325 texture=DestroyImage(texture);
326 draw_info=DestroyDrawInfo(draw_info);
327 (void) CloseBlob(image);
328 return(GetFirstImageInList(image));
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % R e a d T X T I m a g e %
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % ReadTXTImage() reads a text file and returns it as an image. It allocates
343 % the memory necessary for the new Image structure and returns a pointer to
346 % The format of the ReadTXTImage method is:
348 % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
350 % A description of each parameter follows:
352 % o image_info: the image info.
354 % o exception: return any errors or warnings in this structure.
357 static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
360 colorspace[MaxTextExtent],
399 assert(image_info != (const ImageInfo *) NULL);
400 assert(image_info->signature == MagickSignature);
401 if (image_info->debug != MagickFalse)
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
403 image_info->filename);
404 assert(exception != (ExceptionInfo *) NULL);
405 assert(exception->signature == MagickSignature);
406 image=AcquireImage(image_info);
407 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
408 if (status == MagickFalse)
410 image=DestroyImageList(image);
411 return((Image *) NULL);
413 (void) ResetMagickMemory(text,0,sizeof(text));
414 (void) ReadBlobString(image,text);
415 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) != 0)
416 return(ReadTEXTImage(image_info,image,text,exception));
418 count=(ssize_t) sscanf(text+32,"%lu,%lu,%lu,%s",&image->columns,
419 &image->rows,&max_value,colorspace);
421 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
422 for (depth=1; (GetQuantumRange(depth)+1) < max_value; depth++) ;
424 LocaleLower(colorspace);
425 i=(long) strlen(colorspace)-1;
426 image->matte=MagickFalse;
427 if ((i > 0) && (colorspace[i] == 'a'))
430 image->matte=MagickTrue;
432 type=ParseMagickOption(MagickColorspaceOptions,MagickFalse,colorspace);
434 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
435 image->colorspace=(ColorspaceType) type;
436 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
437 (void) SetImageBackgroundColor(image);
438 range=GetQuantumRange(image->depth);
439 while (ReadBlobString(image,text) != (char *) NULL)
441 if (image->colorspace == CMYKColorspace)
443 if (image->matte != MagickFalse)
444 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu,%lu",&x,&y,
445 &pixel.red,&pixel.green,&pixel.blue,&pixel.index,&pixel.opacity);
447 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu",&x,&y,
448 &pixel.red,&pixel.green,&pixel.blue,&pixel.index);
451 if (image->matte != MagickFalse)
452 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu",&x,&y,
453 &pixel.red,&pixel.green,&pixel.blue,&pixel.opacity);
455 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu",&x,&y,
456 &pixel.red,&pixel.green,&pixel.blue);
459 q=GetAuthenticPixels(image,x,y,1,1,exception);
460 if (q == (PixelPacket *) NULL)
462 q->red=ScaleAnyToQuantum(pixel.red,range);
463 q->green=ScaleAnyToQuantum(pixel.green,range);
464 q->blue=ScaleAnyToQuantum(pixel.blue,range);
465 if (image->colorspace == CMYKColorspace)
467 indexes=GetAuthenticIndexQueue(image);
468 *indexes=ScaleAnyToQuantum(pixel.index,range);
470 if (image->matte != MagickFalse)
471 q->opacity=(Quantum) (QuantumRange-ScaleAnyToQuantum(pixel.opacity,
473 if (SyncAuthenticPixels(image,exception) == MagickFalse)
476 return(GetFirstImageInList(image));
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % R e g i s t e r T X T I m a g e %
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % RegisterTXTImage() adds attributes for the TXT image format to the
491 % list of supported formats. The attributes include the image format
492 % tag, a method to read and/or write the format, whether the format
493 % supports the saving of more than one frame to the same file or blob,
494 % whether the format supports native in-memory I/O, and a brief
495 % description of the format.
497 % The format of the RegisterTXTImage method is:
499 % unsigned long RegisterTXTImage(void)
502 ModuleExport unsigned long RegisterTXTImage(void)
507 entry=SetMagickInfo("TEXT");
508 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
509 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
510 entry->raw=MagickTrue;
511 entry->adjoin=MagickFalse;
512 entry->endian_support=MagickTrue;
513 entry->description=ConstantString("Text");
514 entry->module=ConstantString("TXT");
515 (void) RegisterMagickInfo(entry);
516 entry=SetMagickInfo("TXT");
517 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
518 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
519 entry->adjoin=MagickFalse;
520 entry->description=ConstantString("Text");
521 entry->magick=(IsImageFormatHandler *) IsTXT;
522 entry->module=ConstantString("TXT");
523 (void) RegisterMagickInfo(entry);
524 return(MagickImageCoderSignature);
528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532 % U n r e g i s t e r T X T I m a g e %
536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 % UnregisterTXTImage() removes format registrations made by the
539 % TXT module from the list of supported format.
541 % The format of the UnregisterTXTImage method is:
543 % UnregisterTXTImage(void)
546 ModuleExport void UnregisterTXTImage(void)
548 (void) UnregisterMagickInfo("TEXT");
549 (void) UnregisterMagickInfo("TXT");
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % W r i t e T X T I m a g e %
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % WriteTXTImage writes the pixel values as text numbers.
565 % The format of the WriteTXTImage method is:
567 % MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image)
569 % A description of each parameter follows.
571 % o image_info: the image info.
573 % o image: The image.
576 static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image)
579 buffer[MaxTextExtent],
580 colorspace[MaxTextExtent],
581 tuple[MaxTextExtent];
592 register const IndexPacket
595 register const PixelPacket
602 Open output image file.
604 assert(image_info != (const ImageInfo *) NULL);
605 assert(image_info->signature == MagickSignature);
606 assert(image != (Image *) NULL);
607 assert(image->signature == MagickSignature);
608 if (image->debug != MagickFalse)
609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
610 status=OpenBlob(image_info,image,WriteBlobMode,&image->exception);
611 if (status == MagickFalse)
613 (void) CopyMagickString(colorspace,MagickOptionToMnemonic(
614 MagickColorspaceOptions,(long) image->colorspace),MaxTextExtent);
615 LocaleLower(colorspace);
616 image->depth=GetImageQuantumDepth(image,MagickTrue);
617 if (image->matte != MagickFalse)
618 (void) ConcatenateMagickString(colorspace,"a",MaxTextExtent);
619 (void) FormatMagickString(buffer,MaxTextExtent,
620 "# ImageMagick pixel enumeration: %lu,%lu,%lu,%s\n",image->columns,
621 image->rows,(unsigned long) GetQuantumRange(image->depth),colorspace);
622 (void) WriteBlobString(image,buffer);
623 GetMagickPixelPacket(image,&pixel);
624 for (y=0; y < (long) image->rows; y++)
626 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
627 if (p == (const PixelPacket *) NULL)
629 indexes=GetVirtualIndexQueue(image);
630 for (x=0; x < (long) image->columns; x++)
632 (void) FormatMagickString(buffer,MaxTextExtent,"%ld,%ld: ",x,y);
633 (void) WriteBlobString(image,buffer);
634 SetMagickPixelPacket(image,p,indexes+x,&pixel);
635 (void) CopyMagickString(tuple,"(",MaxTextExtent);
636 ConcatenateColorComponent(&pixel,RedChannel,X11Compliance,tuple);
637 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
638 ConcatenateColorComponent(&pixel,GreenChannel,X11Compliance,tuple);
639 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
640 ConcatenateColorComponent(&pixel,BlueChannel,X11Compliance,tuple);
641 if (pixel.colorspace == CMYKColorspace)
643 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
644 ConcatenateColorComponent(&pixel,IndexChannel,X11Compliance,tuple);
646 if (pixel.matte != MagickFalse)
648 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
649 ConcatenateColorComponent(&pixel,AlphaChannel,X11Compliance,tuple);
651 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
652 (void) WriteBlobString(image,tuple);
653 (void) WriteBlobString(image," ");
654 GetColorTuple(&pixel,MagickTrue,tuple);
655 (void) FormatMagickString(buffer,MaxTextExtent,"%s",tuple);
656 (void) WriteBlobString(image,buffer);
657 (void) WriteBlobString(image," ");
658 (void) QueryMagickColorname(image,&pixel,SVGCompliance,tuple,
660 (void) WriteBlobString(image,tuple);
661 (void) WriteBlobString(image,"\n");
664 status=SetImageProgress(image,SaveImageTag,y,image->rows);
665 if (status == MagickFalse)
668 (void) CloseBlob(image);