2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Render Text Onto A Canvas Image. %
20 % Copyright 1999-2014 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)/
232 texture=(Image *) NULL;
233 if (image_info->texture != (char *) NULL)
238 read_info=CloneImageInfo(image_info);
239 SetImageInfoBlob(read_info,(void *) NULL,0);
240 (void) CopyMagickString(read_info->filename,image_info->texture,
242 texture=ReadImage(read_info,exception);
243 read_info=DestroyImageInfo(read_info);
246 Annotate the text image.
248 (void) SetImageBackgroundColor(image,exception);
249 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
250 (void) CloneString(&draw_info->text,image_info->filename);
251 (void) FormatLocaleString(geometry,MaxTextExtent,"0x0%+ld%+ld",(long) page.x,
253 (void) CloneString(&draw_info->geometry,geometry);
254 status=GetTypeMetrics(image,draw_info,&metrics,exception);
255 if (status == MagickFalse)
256 ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
257 page.y=(ssize_t) ceil((double) page.y+metrics.ascent-0.5);
258 (void) FormatLocaleString(geometry,MaxTextExtent,"0x0%+ld%+ld",(long) page.x,
260 (void) CloneString(&draw_info->geometry,geometry);
261 (void) CopyMagickString(filename,image_info->filename,MaxTextExtent);
262 if (*draw_info->text != '\0')
263 *draw_info->text='\0';
265 for (offset=2*page.y; p != (char *) NULL; )
268 Annotate image with text.
270 (void) ConcatenateString(&draw_info->text,text);
271 (void) ConcatenateString(&draw_info->text,"\n");
272 offset+=(ssize_t) (metrics.ascent-metrics.descent);
273 if (image->previous == (Image *) NULL)
275 status=SetImageProgress(image,LoadImageTag,offset,image->rows);
276 if (status == MagickFalse)
279 p=ReadBlobString(image,text);
280 if ((offset < (ssize_t) image->rows) && (p != (char *) NULL))
282 if (texture != (Image *) NULL)
284 MagickProgressMonitor
287 progress_monitor=SetImageProgressMonitor(image,
288 (MagickProgressMonitor) NULL,image->client_data);
289 (void) TextureImage(image,texture,exception);
290 (void) SetImageProgressMonitor(image,progress_monitor,
293 (void) AnnotateImage(image,draw_info,exception);
294 if (p == (char *) NULL)
297 Page is full-- allocate next image structure.
299 *draw_info->text='\0';
301 AcquireNextImage(image_info,image,exception);
302 if (GetNextImageInList(image) == (Image *) NULL)
304 image=DestroyImageList(image);
305 return((Image *) NULL);
307 image->next->columns=image->columns;
308 image->next->rows=image->rows;
309 image=SyncNextImageInList(image);
310 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
311 (void) SetImageBackgroundColor(image,exception);
312 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
314 if (status == MagickFalse)
317 if (texture != (Image *) NULL)
319 MagickProgressMonitor
322 progress_monitor=SetImageProgressMonitor(image,
323 (MagickProgressMonitor) NULL,image->client_data);
324 (void) TextureImage(image,texture,exception);
325 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
327 (void) AnnotateImage(image,draw_info,exception);
328 if (texture != (Image *) NULL)
329 texture=DestroyImage(texture);
330 draw_info=DestroyDrawInfo(draw_info);
331 (void) CloseBlob(image);
332 return(GetFirstImageInList(image));
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 % R e a d T X T I m a g e %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % ReadTXTImage() reads a text file and returns it as an image. It allocates
347 % the memory necessary for the new Image structure and returns a pointer to
350 % The format of the ReadTXTImage method is:
352 % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
354 % A description of each parameter follows:
356 % o image_info: the image info.
358 % o exception: return any errors or warnings in this structure.
361 static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
364 colorspace[MaxTextExtent],
404 assert(image_info != (const ImageInfo *) NULL);
405 assert(image_info->signature == MagickSignature);
406 if (image_info->debug != MagickFalse)
407 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
408 image_info->filename);
409 assert(exception != (ExceptionInfo *) NULL);
410 assert(exception->signature == MagickSignature);
411 image=AcquireImage(image_info,exception);
412 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
413 if (status == MagickFalse)
415 image=DestroyImageList(image);
416 return((Image *) NULL);
418 (void) ResetMagickMemory(text,0,sizeof(text));
419 (void) ReadBlobString(image,text);
420 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) != 0)
421 return(ReadTEXTImage(image_info,image,text,exception));
428 count=(ssize_t) sscanf(text+32,"%lu,%lu,%lu,%s",&width,&height,&max_value,
430 if ((count != 4) || (width == 0) || (height == 0) || (max_value == 0))
431 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
432 image->columns=width;
434 for (depth=1; (GetQuantumRange(depth)+1) < max_value; depth++) ;
436 LocaleLower(colorspace);
437 i=(ssize_t) strlen(colorspace)-1;
438 image->alpha_trait=UndefinedPixelTrait;
439 if ((i > 0) && (colorspace[i] == 'a'))
442 image->alpha_trait=BlendPixelTrait;
444 type=ParseCommandOption(MagickColorspaceOptions,MagickFalse,colorspace);
446 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
447 (void) SetImageBackgroundColor(image,exception);
448 (void) SetImageColorspace(image,(ColorspaceType) type,exception);
449 GetPixelInfo(image,&pixel);
450 range=GetQuantumRange(image->depth);
451 for (y=0; y < (ssize_t) image->rows; y++)
465 for (x=0; x < (ssize_t) image->columns; x++)
467 if (ReadBlobString(image,text) == (char *) NULL)
469 switch (image->colorspace)
473 if (image->alpha_trait == BlendPixelTrait)
475 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf%*[%,]%lf%*[%,]",
476 &x_offset,&y_offset,&red,&alpha);
481 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf%*[%,]",&x_offset,
489 if (image->alpha_trait == BlendPixelTrait)
491 count=(ssize_t) sscanf(text,
492 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",
493 &x_offset,&y_offset,&red,&green,&blue,&black,&alpha);
496 count=(ssize_t) sscanf(text,
497 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",&x_offset,
498 &y_offset,&red,&green,&blue,&black);
503 if (image->alpha_trait == BlendPixelTrait)
505 count=(ssize_t) sscanf(text,
506 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",
507 &x_offset,&y_offset,&red,&green,&blue,&alpha);
510 count=(ssize_t) sscanf(text,
511 "%ld,%ld: (%lf%*[%,]%lf%*[%,]%lf%*[%,]",&x_offset,
512 &y_offset,&red,&green,&blue);
516 if (strchr(text,'%') != (char *) NULL)
524 if (image->colorspace == LabColorspace)
526 green+=(range+1)/2.0;
529 pixel.red=ScaleAnyToQuantum((QuantumAny) (red+0.5),range);
530 pixel.green=ScaleAnyToQuantum((QuantumAny) (green+0.5),range);
531 pixel.blue=ScaleAnyToQuantum((QuantumAny) (blue+0.5),range);
532 pixel.black=ScaleAnyToQuantum((QuantumAny) (black+0.5),range);
533 pixel.alpha=ScaleAnyToQuantum((QuantumAny) (alpha+0.5),range);
534 q=GetAuthenticPixels(image,x_offset,y_offset,1,1,exception);
535 if (q == (Quantum *) NULL)
537 SetPixelInfoPixel(image,&pixel,q);
538 if (SyncAuthenticPixels(image,exception) == MagickFalse)
542 (void) ReadBlobString(image,text);
543 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) == 0)
546 Allocate next image structure.
548 AcquireNextImage(image_info,image,exception);
549 if (GetNextImageInList(image) == (Image *) NULL)
551 image=DestroyImageList(image);
552 return((Image *) NULL);
554 image=SyncNextImageInList(image);
555 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
557 if (status == MagickFalse)
560 } while (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) == 0);
561 (void) CloseBlob(image);
562 return(GetFirstImageInList(image));
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % R e g i s t e r T X T I m a g e %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % RegisterTXTImage() adds attributes for the TXT image format to the
577 % list of supported formats. The attributes include the image format
578 % tag, a method to read and/or write the format, whether the format
579 % supports the saving of more than one frame to the same file or blob,
580 % whether the format supports native in-memory I/O, and a brief
581 % description of the format.
583 % The format of the RegisterTXTImage method is:
585 % size_t RegisterTXTImage(void)
588 ModuleExport size_t RegisterTXTImage(void)
593 entry=SetMagickInfo("SPARSE-COLOR");
594 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
595 entry->raw=MagickTrue;
596 entry->endian_support=MagickTrue;
597 entry->description=ConstantString("Sparse Color");
598 entry->module=ConstantString("TXT");
599 (void) RegisterMagickInfo(entry);
600 entry=SetMagickInfo("TEXT");
601 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
602 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
603 entry->raw=MagickTrue;
604 entry->endian_support=MagickTrue;
605 entry->description=ConstantString("Text");
606 entry->module=ConstantString("TXT");
607 (void) RegisterMagickInfo(entry);
608 entry=SetMagickInfo("TXT");
609 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
610 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
611 entry->description=ConstantString("Text");
612 entry->magick=(IsImageFormatHandler *) IsTXT;
613 entry->module=ConstantString("TXT");
614 (void) RegisterMagickInfo(entry);
615 return(MagickImageCoderSignature);
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % U n r e g i s t e r T X T I m a g e %
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % UnregisterTXTImage() removes format registrations made by the
630 % TXT module from the list of supported format.
632 % The format of the UnregisterTXTImage method is:
634 % UnregisterTXTImage(void)
637 ModuleExport void UnregisterTXTImage(void)
639 (void) UnregisterMagickInfo("SPARSE-COLOR");
640 (void) UnregisterMagickInfo("TEXT");
641 (void) UnregisterMagickInfo("TXT");
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % W r i t e T X T I m a g e %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 % WriteTXTImage writes the pixel values as text numbers.
657 % The format of the WriteTXTImage method is:
659 % MagickBooleanType WriteTXTImage(const ImageInfo *image_info,
660 % Image *image,ExceptionInfo *exception)
662 % A description of each parameter follows.
664 % o image_info: the image info.
666 % o image: The image.
668 % o exception: return any errors or warnings in this structure.
671 static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image,
672 ExceptionInfo *exception)
675 buffer[MaxTextExtent],
676 colorspace[MaxTextExtent],
677 tuple[MaxTextExtent];
688 register const Quantum
698 Open output image file.
700 assert(image_info != (const ImageInfo *) NULL);
701 assert(image_info->signature == MagickSignature);
702 assert(image != (Image *) NULL);
703 assert(image->signature == MagickSignature);
704 if (image->debug != MagickFalse)
705 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
706 status=OpenBlob(image_info,image,WriteBlobMode,exception);
707 if (status == MagickFalse)
715 (void) CopyMagickString(colorspace,CommandOptionToMnemonic(
716 MagickColorspaceOptions,(ssize_t) image->colorspace),MaxTextExtent);
717 LocaleLower(colorspace);
718 image->depth=GetImageQuantumDepth(image,MagickTrue);
719 if (image->alpha_trait == BlendPixelTrait)
720 (void) ConcatenateMagickString(colorspace,"a",MaxTextExtent);
721 compliance=NoCompliance;
722 if (LocaleCompare(image_info->magick,"SPARSE-COLOR") != 0)
724 (void) FormatLocaleString(buffer,MaxTextExtent,
725 "# ImageMagick pixel enumeration: %.20g,%.20g,%.20g,%s\n",(double)
726 image->columns,(double) image->rows,(double) ((MagickOffsetType)
727 GetQuantumRange(image->depth)),colorspace);
728 (void) WriteBlobString(image,buffer);
729 compliance=SVGCompliance;
731 GetPixelInfo(image,&pixel);
732 for (y=0; y < (ssize_t) image->rows; y++)
734 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
735 if (p == (const Quantum *) NULL)
737 for (x=0; x < (ssize_t) image->columns; x++)
739 GetPixelInfoPixel(image,p,&pixel);
740 if (pixel.colorspace == LabColorspace)
742 pixel.green-=(QuantumRange+1)/2.0;
743 pixel.blue-=(QuantumRange+1)/2.0;
745 if (LocaleCompare(image_info->magick,"SPARSE-COLOR") == 0)
750 if (GetPixelAlpha(image,p) == (Quantum) OpaqueAlpha)
752 GetColorTuple(&pixel,MagickFalse,tuple);
753 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g,%.20g,",
754 (double) x,(double) y);
755 (void) WriteBlobString(image,buffer);
756 (void) WriteBlobString(image,tuple);
757 (void) WriteBlobString(image," ");
759 p+=GetPixelChannels(image);
762 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g,%.20g: ",(double)
764 (void) WriteBlobString(image,buffer);
765 (void) CopyMagickString(tuple,"(",MaxTextExtent);
766 if (pixel.colorspace == GRAYColorspace)
767 ConcatenateColorComponent(&pixel,GrayPixelChannel,compliance,
771 ConcatenateColorComponent(&pixel,RedPixelChannel,compliance,tuple);
772 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
773 ConcatenateColorComponent(&pixel,GreenPixelChannel,compliance,
775 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
776 ConcatenateColorComponent(&pixel,BluePixelChannel,compliance,tuple);
778 if (pixel.colorspace == CMYKColorspace)
780 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
781 ConcatenateColorComponent(&pixel,BlackPixelChannel,compliance,
784 if (pixel.alpha_trait == BlendPixelTrait)
786 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
787 ConcatenateColorComponent(&pixel,AlphaPixelChannel,compliance,
790 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
791 (void) WriteBlobString(image,tuple);
792 (void) WriteBlobString(image," ");
793 GetColorTuple(&pixel,MagickTrue,tuple);
794 (void) FormatLocaleString(buffer,MaxTextExtent,"%s",tuple);
795 (void) WriteBlobString(image,buffer);
796 (void) WriteBlobString(image," ");
797 (void) QueryColorname(image,&pixel,SVGCompliance,tuple,exception);
798 (void) WriteBlobString(image,tuple);
799 (void) WriteBlobString(image,"\n");
800 p+=GetPixelChannels(image);
802 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
804 if (status == MagickFalse)
807 if (GetNextImageInList(image) == (Image *) NULL)
809 image=SyncNextImageInList(image);
810 status=SetImageProgress(image,SaveImagesTag,scene++,
811 GetImageListLength(image));
812 if (status == MagickFalse)
814 } while (image_info->adjoin != MagickFalse);
815 (void) CloseBlob(image);