2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Render Text Onto A Canvas Image. %
20 % Copyright 1999-2015 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 "MagickCore/studio.h"
43 #include "MagickCore/annotate.h"
44 #include "MagickCore/attribute.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/constitute.h"
52 #include "MagickCore/draw.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/statistic.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/module.h"
74 static MagickBooleanType
75 WriteTXTImage(const ImageInfo *,Image *,ExceptionInfo *);
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % IsTXT() returns MagickTrue if the image format type, identified by the magick
91 % The format of the IsTXT method is:
93 % MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
95 % A description of each parameter follows:
97 % o magick: compare image format pattern against these bytes.
99 % o length: Specifies the length of the magick string.
102 static MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
104 #define MagickID "# ImageMagick pixel enumeration:"
107 colorspace[MaxTextExtent];
119 if (LocaleNCompare((const char *) magick,MagickID,strlen(MagickID)) != 0)
121 count=(ssize_t) sscanf((const char *) magick+32,"%lu,%lu,%lu,%s",&columns,
122 &rows,&depth,colorspace);
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % R e a d T E X T I m a g e %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % ReadTEXTImage() reads a text file and returns it as an image. It
140 % allocates the memory necessary for the new Image structure and returns a
141 % pointer to the new image.
143 % The format of the ReadTEXTImage method is:
145 % Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
146 % char *text,ExceptionInfo *exception)
148 % A description of each parameter follows:
150 % o image_info: the image info.
152 % o image: the image.
154 % o text: the text storage buffer.
156 % o exception: return any errors or warnings in this structure.
159 static Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
160 char *text,ExceptionInfo *exception)
163 filename[MaxTextExtent],
164 geometry[MaxTextExtent],
191 assert(image_info != (const ImageInfo *) NULL);
192 assert(image_info->signature == MagickSignature);
193 if (image_info->debug != MagickFalse)
194 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
195 image_info->filename);
196 assert(exception != (ExceptionInfo *) NULL);
197 assert(exception->signature == MagickSignature);
199 Set the page geometry.
201 delta.x=DefaultResolution;
202 delta.y=DefaultResolution;
203 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
211 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
212 image->resolution.x=geometry_info.rho;
213 image->resolution.y=geometry_info.sigma;
214 if ((flags & SigmaValue) == 0)
215 image->resolution.y=image->resolution.x;
221 if (image_info->page != (char *) NULL)
222 (void) ParseAbsoluteGeometry(image_info->page,&page);
224 Initialize Image structure.
226 image->columns=(size_t) floor((((double) page.width*image->resolution.x)/
228 image->rows=(size_t) floor((((double) page.height*image->resolution.y)/
230 status=SetImageExtent(image,image->columns,image->rows,exception);
231 if (status == MagickFalse)
232 return(DestroyImageList(image));
235 texture=(Image *) NULL;
236 if (image_info->texture != (char *) NULL)
241 read_info=CloneImageInfo(image_info);
242 SetImageInfoBlob(read_info,(void *) NULL,0);
243 (void) CopyMagickString(read_info->filename,image_info->texture,
245 texture=ReadImage(read_info,exception);
246 read_info=DestroyImageInfo(read_info);
249 Annotate the text image.
251 (void) SetImageBackgroundColor(image,exception);
252 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
253 (void) CloneString(&draw_info->text,image_info->filename);
254 (void) FormatLocaleString(geometry,MaxTextExtent,"0x0%+ld%+ld",(long) page.x,
256 (void) CloneString(&draw_info->geometry,geometry);
257 status=GetTypeMetrics(image,draw_info,&metrics,exception);
258 if (status == MagickFalse)
259 ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
260 page.y=(ssize_t) ceil((double) page.y+metrics.ascent-0.5);
261 (void) FormatLocaleString(geometry,MaxTextExtent,"0x0%+ld%+ld",(long) page.x,
263 (void) CloneString(&draw_info->geometry,geometry);
264 (void) CopyMagickString(filename,image_info->filename,MaxTextExtent);
265 if (*draw_info->text != '\0')
266 *draw_info->text='\0';
268 for (offset=2*page.y; p != (char *) NULL; )
271 Annotate image with text.
273 (void) ConcatenateString(&draw_info->text,text);
274 (void) ConcatenateString(&draw_info->text,"\n");
275 offset+=(ssize_t) (metrics.ascent-metrics.descent);
276 if (image->previous == (Image *) NULL)
278 status=SetImageProgress(image,LoadImageTag,offset,image->rows);
279 if (status == MagickFalse)
282 p=ReadBlobString(image,text);
283 if ((offset < (ssize_t) image->rows) && (p != (char *) NULL))
285 if (texture != (Image *) NULL)
287 MagickProgressMonitor
290 progress_monitor=SetImageProgressMonitor(image,
291 (MagickProgressMonitor) NULL,image->client_data);
292 (void) TextureImage(image,texture,exception);
293 (void) SetImageProgressMonitor(image,progress_monitor,
296 (void) AnnotateImage(image,draw_info,exception);
297 if (p == (char *) NULL)
300 Page is full-- allocate next image structure.
302 *draw_info->text='\0';
304 AcquireNextImage(image_info,image,exception);
305 if (GetNextImageInList(image) == (Image *) NULL)
307 image=DestroyImageList(image);
308 return((Image *) NULL);
310 image->next->columns=image->columns;
311 image->next->rows=image->rows;
312 image=SyncNextImageInList(image);
313 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
314 (void) SetImageBackgroundColor(image,exception);
315 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
317 if (status == MagickFalse)
320 if (texture != (Image *) NULL)
322 MagickProgressMonitor
325 progress_monitor=SetImageProgressMonitor(image,
326 (MagickProgressMonitor) NULL,image->client_data);
327 (void) TextureImage(image,texture,exception);
328 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
330 (void) AnnotateImage(image,draw_info,exception);
331 if (texture != (Image *) NULL)
332 texture=DestroyImage(texture);
333 draw_info=DestroyDrawInfo(draw_info);
334 (void) CloseBlob(image);
335 return(GetFirstImageInList(image));
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % R e a d T X T I m a g e %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % ReadTXTImage() reads a text file and returns it as an image. It allocates
350 % the memory necessary for the new Image structure and returns a pointer to
353 % The format of the ReadTXTImage method is:
355 % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
357 % A description of each parameter follows:
359 % o image_info: the image info.
361 % o exception: return any errors or warnings in this structure.
364 static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
367 colorspace[MaxTextExtent],
407 assert(image_info != (const ImageInfo *) NULL);
408 assert(image_info->signature == MagickSignature);
409 if (image_info->debug != MagickFalse)
410 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
411 image_info->filename);
412 assert(exception != (ExceptionInfo *) NULL);
413 assert(exception->signature == MagickSignature);
414 image=AcquireImage(image_info,exception);
415 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
416 if (status == MagickFalse)
418 image=DestroyImageList(image);
419 return((Image *) NULL);
421 (void) ResetMagickMemory(text,0,sizeof(text));
422 (void) ReadBlobString(image,text);
423 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) != 0)
424 return(ReadTEXTImage(image_info,image,text,exception));
431 count=(ssize_t) sscanf(text+32,"%lu,%lu,%lu,%s",&width,&height,&max_value,
433 if ((count != 4) || (width == 0) || (height == 0) || (max_value == 0))
434 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
435 image->columns=width;
437 for (depth=1; (GetQuantumRange(depth)+1) < max_value; depth++) ;
439 status=SetImageExtent(image,image->columns,image->rows,exception);
440 if (status == MagickFalse)
441 return(DestroyImageList(image));
442 LocaleLower(colorspace);
443 i=(ssize_t) strlen(colorspace)-1;
444 image->alpha_trait=UndefinedPixelTrait;
445 if ((i > 0) && (colorspace[i] == 'a'))
448 image->alpha_trait=BlendPixelTrait;
450 type=ParseCommandOption(MagickColorspaceOptions,MagickFalse,colorspace);
452 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
453 (void) SetImageBackgroundColor(image,exception);
454 (void) SetImageColorspace(image,(ColorspaceType) type,exception);
455 GetPixelInfo(image,&pixel);
456 range=GetQuantumRange(image->depth);
457 for (y=0; y < (ssize_t) image->rows; y++)
471 for (x=0; x < (ssize_t) image->columns; x++)
473 if (ReadBlobString(image,text) == (char *) NULL)
475 switch (image->colorspace)
479 if (image->alpha_trait != UndefinedPixelTrait)
481 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf%*[%,]%lf%*[%,]",
482 &x_offset,&y_offset,&red,&alpha);
487 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf%*[%,]",&x_offset,
495 if (image->alpha_trait != UndefinedPixelTrait)
497 count=(ssize_t) sscanf(text,
498 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",
499 &x_offset,&y_offset,&red,&green,&blue,&black,&alpha);
502 count=(ssize_t) sscanf(text,
503 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",&x_offset,
504 &y_offset,&red,&green,&blue,&black);
509 if (image->alpha_trait != UndefinedPixelTrait)
511 count=(ssize_t) sscanf(text,
512 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",
513 &x_offset,&y_offset,&red,&green,&blue,&alpha);
516 count=(ssize_t) sscanf(text,
517 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]",&x_offset,
518 &y_offset,&red,&green,&blue);
522 if (strchr(text,'%') != (char *) NULL)
530 if (image->colorspace == LabColorspace)
532 green+=(range+1)/2.0;
535 pixel.red=ScaleAnyToQuantum((QuantumAny) (red+0.5),range);
536 pixel.green=ScaleAnyToQuantum((QuantumAny) (green+0.5),range);
537 pixel.blue=ScaleAnyToQuantum((QuantumAny) (blue+0.5),range);
538 pixel.black=ScaleAnyToQuantum((QuantumAny) (black+0.5),range);
539 pixel.alpha=ScaleAnyToQuantum((QuantumAny) (alpha+0.5),range);
540 q=GetAuthenticPixels(image,x_offset,y_offset,1,1,exception);
541 if (q == (Quantum *) NULL)
543 SetPixelViaPixelInfo(image,&pixel,q);
544 if (SyncAuthenticPixels(image,exception) == MagickFalse)
548 (void) ReadBlobString(image,text);
549 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) == 0)
552 Allocate next image structure.
554 AcquireNextImage(image_info,image,exception);
555 if (GetNextImageInList(image) == (Image *) NULL)
557 image=DestroyImageList(image);
558 return((Image *) NULL);
560 image=SyncNextImageInList(image);
561 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
563 if (status == MagickFalse)
566 } while (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) == 0);
567 (void) CloseBlob(image);
568 return(GetFirstImageInList(image));
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % R e g i s t e r T X T I m a g e %
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 % RegisterTXTImage() adds attributes for the TXT image format to the
583 % list of supported formats. The attributes include the image format
584 % tag, a method to read and/or write the format, whether the format
585 % supports the saving of more than one frame to the same file or blob,
586 % whether the format supports native in-memory I/O, and a brief
587 % description of the format.
589 % The format of the RegisterTXTImage method is:
591 % size_t RegisterTXTImage(void)
594 ModuleExport size_t RegisterTXTImage(void)
599 entry=SetMagickInfo("SPARSE-COLOR");
600 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
601 entry->raw=MagickTrue;
602 entry->endian_support=MagickTrue;
603 entry->description=ConstantString("Sparse Color");
604 entry->module=ConstantString("TXT");
605 (void) RegisterMagickInfo(entry);
606 entry=SetMagickInfo("TEXT");
607 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
608 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
609 entry->raw=MagickTrue;
610 entry->endian_support=MagickTrue;
611 entry->description=ConstantString("Text");
612 entry->module=ConstantString("TXT");
613 (void) RegisterMagickInfo(entry);
614 entry=SetMagickInfo("TXT");
615 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
616 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
617 entry->description=ConstantString("Text");
618 entry->magick=(IsImageFormatHandler *) IsTXT;
619 entry->module=ConstantString("TXT");
620 (void) RegisterMagickInfo(entry);
621 return(MagickImageCoderSignature);
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % U n r e g i s t e r T X T I m a g e %
633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 % UnregisterTXTImage() removes format registrations made by the
636 % TXT module from the list of supported format.
638 % The format of the UnregisterTXTImage method is:
640 % UnregisterTXTImage(void)
643 ModuleExport void UnregisterTXTImage(void)
645 (void) UnregisterMagickInfo("SPARSE-COLOR");
646 (void) UnregisterMagickInfo("TEXT");
647 (void) UnregisterMagickInfo("TXT");
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % W r i t e T X T I m a g e %
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661 % WriteTXTImage writes the pixel values as text numbers.
663 % The format of the WriteTXTImage method is:
665 % MagickBooleanType WriteTXTImage(const ImageInfo *image_info,
666 % Image *image,ExceptionInfo *exception)
668 % A description of each parameter follows.
670 % o image_info: the image info.
672 % o image: The image.
674 % o exception: return any errors or warnings in this structure.
677 static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image,
678 ExceptionInfo *exception)
681 buffer[MaxTextExtent],
682 colorspace[MaxTextExtent],
683 tuple[MaxTextExtent];
694 register const Quantum
704 Open output image file.
706 assert(image_info != (const ImageInfo *) NULL);
707 assert(image_info->signature == MagickSignature);
708 assert(image != (Image *) NULL);
709 assert(image->signature == MagickSignature);
710 if (image->debug != MagickFalse)
711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
712 status=OpenBlob(image_info,image,WriteBlobMode,exception);
713 if (status == MagickFalse)
721 (void) CopyMagickString(colorspace,CommandOptionToMnemonic(
722 MagickColorspaceOptions,(ssize_t) image->colorspace),MaxTextExtent);
723 LocaleLower(colorspace);
724 image->depth=GetImageQuantumDepth(image,MagickTrue);
725 if (image->alpha_trait != UndefinedPixelTrait)
726 (void) ConcatenateMagickString(colorspace,"a",MaxTextExtent);
727 compliance=NoCompliance;
728 if (LocaleCompare(image_info->magick,"SPARSE-COLOR") != 0)
730 (void) FormatLocaleString(buffer,MaxTextExtent,
731 "# ImageMagick pixel enumeration: %.20g,%.20g,%.20g,%s\n",(double)
732 image->columns,(double) image->rows,(double) ((MagickOffsetType)
733 GetQuantumRange(image->depth)),colorspace);
734 (void) WriteBlobString(image,buffer);
735 compliance=SVGCompliance;
737 GetPixelInfo(image,&pixel);
738 for (y=0; y < (ssize_t) image->rows; y++)
740 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
741 if (p == (const Quantum *) NULL)
743 for (x=0; x < (ssize_t) image->columns; x++)
745 GetPixelInfoPixel(image,p,&pixel);
746 if (pixel.colorspace == LabColorspace)
748 pixel.green-=(QuantumRange+1)/2.0;
749 pixel.blue-=(QuantumRange+1)/2.0;
751 if (LocaleCompare(image_info->magick,"SPARSE-COLOR") == 0)
756 if (GetPixelAlpha(image,p) == (Quantum) OpaqueAlpha)
758 GetColorTuple(&pixel,MagickFalse,tuple);
759 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g,%.20g,",
760 (double) x,(double) y);
761 (void) WriteBlobString(image,buffer);
762 (void) WriteBlobString(image,tuple);
763 (void) WriteBlobString(image," ");
765 p+=GetPixelChannels(image);
768 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g,%.20g: ",(double)
770 (void) WriteBlobString(image,buffer);
771 (void) CopyMagickString(tuple,"(",MaxTextExtent);
772 if (pixel.colorspace == GRAYColorspace)
773 ConcatenateColorComponent(&pixel,GrayPixelChannel,compliance,
777 ConcatenateColorComponent(&pixel,RedPixelChannel,compliance,tuple);
778 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
779 ConcatenateColorComponent(&pixel,GreenPixelChannel,compliance,
781 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
782 ConcatenateColorComponent(&pixel,BluePixelChannel,compliance,tuple);
784 if (pixel.colorspace == CMYKColorspace)
786 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
787 ConcatenateColorComponent(&pixel,BlackPixelChannel,compliance,
790 if (pixel.alpha_trait != UndefinedPixelTrait)
792 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
793 ConcatenateColorComponent(&pixel,AlphaPixelChannel,compliance,
796 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
797 (void) WriteBlobString(image,tuple);
798 (void) WriteBlobString(image," ");
799 GetColorTuple(&pixel,MagickTrue,tuple);
800 (void) FormatLocaleString(buffer,MaxTextExtent,"%s",tuple);
801 (void) WriteBlobString(image,buffer);
802 (void) WriteBlobString(image," ");
803 (void) QueryColorname(image,&pixel,SVGCompliance,tuple,exception);
804 (void) WriteBlobString(image,tuple);
805 (void) WriteBlobString(image,"\n");
806 p+=GetPixelChannels(image);
808 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
810 if (status == MagickFalse)
813 if (GetNextImageInList(image) == (Image *) NULL)
815 image=SyncNextImageInList(image);
816 status=SetImageProgress(image,SaveImagesTag,scene++,
817 GetImageListLength(image));
818 if (status == MagickFalse)
820 } while (image_info->adjoin != MagickFalse);
821 (void) CloseBlob(image);