1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            TTTTT  X   X  TTTTT                              %
7 %                              T     X X     T                                %
8 %                              T      X      T                                %
9 %                              T     X X     T                                %
10 %                              T    X   X    T                                %
11 %                                                                             %
12 %                                                                             %
13 %                      Render Text Onto A Canvas Image.                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
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 %    https://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 */
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"
70 #include "coders/txt.h"
71 
72 /*
73   Forward declarations.
74 */
75 static MagickBooleanType
76   WriteTXTImage(const ImageInfo *,Image *,ExceptionInfo *);
77 
78 /*
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %   I s T X T                                                                 %
84 %                                                                             %
85 %                                                                             %
86 %                                                                             %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %
89 %  IsTXT() returns MagickTrue if the image format type, identified by the magick
90 %  string, is TXT.
91 %
92 %  The format of the IsTXT method is:
93 %
94 %      MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
95 %
96 %  A description of each parameter follows:
97 %
98 %    o magick: compare image format pattern against these bytes.
99 %
100 %    o length: Specifies the length of the magick string.
101 %
102 */
IsTXT(const unsigned char * magick,const size_t length)103 static MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
104 {
105   char
106     colorspace[MagickPathExtent];
107 
108   ssize_t
109     count;
110 
111   unsigned long
112     columns,
113     depth,
114     rows;
115 
116   if (length < 40)
117     return(MagickFalse);
118   if (LocaleNCompare((const char *) magick,MagickTXTID,
119         strlen(MagickTXTID)) != 0)
120     return(MagickFalse);
121   count=(ssize_t) sscanf((const char *) magick+32,"%lu,%lu,%lu,%32s",&columns,
122     &rows,&depth,colorspace);
123   if (count != 4)
124     return(MagickFalse);
125   return(MagickTrue);
126 }
127 
128 /*
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 %                                                                             %
131 %                                                                             %
132 %                                                                             %
133 %   R e a d T E X T I m a g e                                                 %
134 %                                                                             %
135 %                                                                             %
136 %                                                                             %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 %
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.
142 %
143 %  The format of the ReadTEXTImage method is:
144 %
145 %      Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
146 %        char *text,ExceptionInfo *exception)
147 %
148 %  A description of each parameter follows:
149 %
150 %    o image_info: the image info.
151 %
152 %    o image: the image.
153 %
154 %    o text: the text storage buffer.
155 %
156 %    o exception: return any errors or warnings in this structure.
157 %
158 */
ReadTEXTImage(const ImageInfo * image_info,ExceptionInfo * exception)159 static Image *ReadTEXTImage(const ImageInfo *image_info,
160   ExceptionInfo *exception)
161 {
162   char
163     filename[MagickPathExtent],
164     geometry[MagickPathExtent],
165     *p,
166     text[MagickPathExtent];
167 
168   DrawInfo
169     *draw_info;
170 
171   Image
172     *image,
173     *texture;
174 
175   MagickBooleanType
176     status;
177 
178   PointInfo
179     delta;
180 
181   RectangleInfo
182     page;
183 
184   ssize_t
185     offset;
186 
187   TypeMetric
188     metrics;
189 
190   /*
191     Open image file.
192   */
193   assert(image_info != (const ImageInfo *) NULL);
194   assert(image_info->signature == MagickCoreSignature);
195   if (image_info->debug != MagickFalse)
196     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
197       image_info->filename);
198   assert(exception != (ExceptionInfo *) NULL);
199   assert(exception->signature == MagickCoreSignature);
200   image=AcquireImage(image_info,exception);
201   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
202   if (status == MagickFalse)
203     {
204       image=DestroyImageList(image);
205       return((Image *) NULL);
206     }
207   (void) memset(text,0,sizeof(text));
208   (void) ReadBlobString(image,text);
209   /*
210     Set the page geometry.
211   */
212   delta.x=DefaultResolution;
213   delta.y=DefaultResolution;
214   if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
215     {
216       GeometryInfo
217         geometry_info;
218 
219       MagickStatusType
220         flags;
221 
222       flags=ParseGeometry(PSDensityGeometry,&geometry_info);
223       image->resolution.x=geometry_info.rho;
224       image->resolution.y=geometry_info.sigma;
225       if ((flags & SigmaValue) == 0)
226         image->resolution.y=image->resolution.x;
227     }
228   page.width=612;
229   page.height=792;
230   page.x=43;
231   page.y=43;
232   if (image_info->page != (char *) NULL)
233     (void) ParseAbsoluteGeometry(image_info->page,&page);
234   /*
235     Initialize Image structure.
236   */
237   image->columns=(size_t) floor((((double) page.width*image->resolution.x)/
238     delta.x)+0.5);
239   image->rows=(size_t) floor((((double) page.height*image->resolution.y)/
240     delta.y)+0.5);
241   status=SetImageExtent(image,image->columns,image->rows,exception);
242   if (status != MagickFalse)
243     status=ResetImagePixels(image,exception);
244   if (status == MagickFalse)
245     return(DestroyImageList(image));
246   image->page.x=0;
247   image->page.y=0;
248   texture=(Image *) NULL;
249   if (image_info->texture != (char *) NULL)
250     {
251       ImageInfo
252         *read_info;
253 
254       read_info=CloneImageInfo(image_info);
255       SetImageInfoBlob(read_info,(void *) NULL,0);
256       (void) CopyMagickString(read_info->filename,image_info->texture,
257         MagickPathExtent);
258       texture=ReadImage(read_info,exception);
259       read_info=DestroyImageInfo(read_info);
260     }
261   /*
262     Annotate the text image.
263   */
264   (void) SetImageBackgroundColor(image,exception);
265   draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
266   (void) CloneString(&draw_info->text,image_info->filename);
267   (void) FormatLocaleString(geometry,MagickPathExtent,"%gx%g%+g%+g",(double)
268     image->columns,(double) image->rows,(double) page.x,(double) page.y);
269   (void) CloneString(&draw_info->geometry,geometry);
270   status=GetTypeMetrics(image,draw_info,&metrics,exception);
271   if (status == MagickFalse)
272     {
273       draw_info=DestroyDrawInfo(draw_info);
274       ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
275     }
276   page.y=CastDoubleToLong(ceil((double) page.y+metrics.ascent-0.5));
277   (void) FormatLocaleString(geometry,MagickPathExtent,"%gx%g%+g%+g",(double)
278     image->columns,(double) image->rows,(double) page.x,(double) page.y);
279   (void) CloneString(&draw_info->geometry,geometry);
280   (void) CopyMagickString(filename,image_info->filename,MagickPathExtent);
281   if (*draw_info->text != '\0')
282     *draw_info->text='\0';
283   p=text;
284   for (offset=2*page.y; p != (char *) NULL; )
285   {
286     /*
287       Annotate image with text.
288     */
289     (void) ConcatenateString(&draw_info->text,text);
290     (void) ConcatenateString(&draw_info->text,"\n");
291     offset+=(ssize_t) (metrics.ascent-metrics.descent);
292     if (image->previous == (Image *) NULL)
293       {
294         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) offset,
295           image->rows);
296         if (status == MagickFalse)
297           break;
298       }
299     p=ReadBlobString(image,text);
300     if ((offset < (ssize_t) image->rows) && (p != (char *) NULL))
301       continue;
302     if (texture != (Image *) NULL)
303       {
304         MagickProgressMonitor
305           progress_monitor;
306 
307         progress_monitor=SetImageProgressMonitor(image,
308           (MagickProgressMonitor) NULL,image->client_data);
309         (void) TextureImage(image,texture,exception);
310         (void) SetImageProgressMonitor(image,progress_monitor,
311           image->client_data);
312       }
313     (void) AnnotateImage(image,draw_info,exception);
314     if (p == (char *) NULL)
315       break;
316     /*
317       Page is full-- allocate next image structure.
318     */
319     *draw_info->text='\0';
320     offset=2*page.y;
321     AcquireNextImage(image_info,image,exception);
322     if (GetNextImageInList(image) == (Image *) NULL)
323       {
324         status=MagickFalse;
325         break;
326       }
327     image->next->columns=image->columns;
328     image->next->rows=image->rows;
329     image=SyncNextImageInList(image);
330     (void) CopyMagickString(image->filename,filename,MagickPathExtent);
331     (void) SetImageBackgroundColor(image,exception);
332     status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
333       GetBlobSize(image));
334     if (status == MagickFalse)
335       break;
336   }
337   if (texture != (Image *) NULL)
338     {
339       MagickProgressMonitor
340         progress_monitor;
341 
342       progress_monitor=SetImageProgressMonitor(image,
343         (MagickProgressMonitor) NULL,image->client_data);
344       (void) TextureImage(image,texture,exception);
345       (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
346     }
347   (void) AnnotateImage(image,draw_info,exception);
348   if (texture != (Image *) NULL)
349     texture=DestroyImage(texture);
350   draw_info=DestroyDrawInfo(draw_info);
351   (void) CloseBlob(image);
352   if (status == MagickFalse)
353     return(DestroyImageList(image));
354   return(GetFirstImageInList(image));
355 }
356 
357 /*
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 %                                                                             %
360 %                                                                             %
361 %                                                                             %
362 %   R e a d T X T I m a g e                                                   %
363 %                                                                             %
364 %                                                                             %
365 %                                                                             %
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 %
368 %  ReadTXTImage() reads a text file and returns it as an image.  It allocates
369 %  the memory necessary for the new Image structure and returns a pointer to
370 %  the new image.
371 %
372 %  The format of the ReadTXTImage method is:
373 %
374 %      Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
375 %
376 %  A description of each parameter follows:
377 %
378 %    o image_info: the image info.
379 %
380 %    o exception: return any errors or warnings in this structure.
381 %
382 */
ReadTXTImage(const ImageInfo * image_info,ExceptionInfo * exception)383 static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
384 {
385   char
386     colorspace[MagickPathExtent],
387     text[MagickPathExtent];
388 
389   double
390     max_value,
391     x_offset,
392     y_offset;
393 
394   Image
395     *image;
396 
397   PixelInfo
398     pixel;
399 
400   MagickBooleanType
401     status;
402 
403   QuantumAny
404     range;
405 
406   ssize_t
407     i,
408     x;
409 
410   Quantum
411     *q;
412 
413   ssize_t
414     count,
415     type,
416     y;
417 
418   unsigned long
419     depth,
420     height,
421     width;
422 
423   /*
424     Open image file.
425   */
426   assert(image_info != (const ImageInfo *) NULL);
427   assert(image_info->signature == MagickCoreSignature);
428   if (image_info->debug != MagickFalse)
429     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
430       image_info->filename);
431   assert(exception != (ExceptionInfo *) NULL);
432   assert(exception->signature == MagickCoreSignature);
433   image=AcquireImage(image_info,exception);
434   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
435   if (status == MagickFalse)
436     {
437       image=DestroyImageList(image);
438       return((Image *) NULL);
439     }
440   (void) memset(text,0,sizeof(text));
441   (void) ReadBlobString(image,text);
442   if (LocaleNCompare((char *) text,MagickTXTID,strlen(MagickTXTID)) != 0)
443     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
444   x_offset=(-1.0);
445   y_offset=(-1.0);
446   q=(Quantum *) NULL;
447   do
448   {
449     width=0;
450     height=0;
451     max_value=0.0;
452     *colorspace='\0';
453     count=(ssize_t) sscanf(text+32,"%lu,%lu,%lf,%32s",&width,&height,&max_value,
454       colorspace);
455     if ((count != 4) || (width == 0) || (height == 0) || (max_value == 0.0))
456       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
457     image->columns=width;
458     image->rows=height;
459     if ((max_value == 0.0) || (max_value > 18446744073709551615.0))
460       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
461     for (depth=1; ((double) GetQuantumRange(depth)+1) < max_value; depth++) ;
462     image->depth=depth;
463     status=SetImageExtent(image,image->columns,image->rows,exception);
464     if (status != MagickFalse)
465       status=ResetImagePixels(image,exception);
466     if (status == MagickFalse)
467       return(DestroyImageList(image));
468     LocaleLower(colorspace);
469     i=(ssize_t) strlen(colorspace)-1;
470     image->alpha_trait=UndefinedPixelTrait;
471     if ((i > 0) && (colorspace[i] == 'a'))
472       {
473         colorspace[i]='\0';
474         image->alpha_trait=BlendPixelTrait;
475       }
476     type=ParseCommandOption(MagickColorspaceOptions,MagickFalse,colorspace);
477     if (type < 0)
478       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
479     (void) SetImageColorspace(image,(ColorspaceType) type,exception);
480     (void) SetImageBackgroundColor(image,exception);
481     GetPixelInfo(image,&pixel);
482     range=GetQuantumRange(image->depth);
483     status=MagickTrue;
484     for (y=0; y < (ssize_t) image->rows; y++)
485     {
486       double
487         alpha,
488         black,
489         blue,
490         green,
491         red;
492 
493       if (status == MagickFalse)
494         break;
495       red=0.0;
496       green=0.0;
497       blue=0.0;
498       black=0.0;
499       alpha=0.0;
500       for (x=0; x < (ssize_t) image->columns; x++)
501       {
502         if (ReadBlobString(image,text) == (char *) NULL)
503           {
504             status=MagickFalse;
505             break;
506           }
507         switch (image->colorspace)
508         {
509           case LinearGRAYColorspace:
510           case GRAYColorspace:
511           {
512             if (image->alpha_trait != UndefinedPixelTrait)
513               {
514                 count=(ssize_t) sscanf(text,"%lf,%lf: (%lf%*[%,]%lf%*[%,]",
515                   &x_offset,&y_offset,&red,&alpha);
516                 green=red;
517                 blue=red;
518                 break;
519               }
520             count=(ssize_t) sscanf(text,"%lf,%lf: (%lf%*[%,]",&x_offset,
521               &y_offset,&red);
522             green=red;
523             blue=red;
524             break;
525           }
526           case CMYKColorspace:
527           {
528             if (image->alpha_trait != UndefinedPixelTrait)
529               {
530                 count=(ssize_t) sscanf(text,
531                   "%lf,%lf: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",
532                   &x_offset,&y_offset,&red,&green,&blue,&black,&alpha);
533                 break;
534               }
535             count=(ssize_t) sscanf(text,
536               "%lf,%lf: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",&x_offset,
537               &y_offset,&red,&green,&blue,&black);
538             break;
539           }
540           default:
541           {
542             if (image->alpha_trait != UndefinedPixelTrait)
543               {
544                 count=(ssize_t) sscanf(text,
545                   "%lf,%lf: (%lf%*[%,]%lf%*[%,]%lf%*[%,]%lf%*[%,]",
546                   &x_offset,&y_offset,&red,&green,&blue,&alpha);
547                 break;
548               }
549             count=(ssize_t) sscanf(text,"%lf,%lf: (%lf%*[%,]%lf%*[%,]%lf%*[%,]",
550               &x_offset,&y_offset,&red,&green,&blue);
551             break;
552           }
553         }
554         if (strchr(text,'%') != (char *) NULL)
555           {
556             red*=0.01*range;
557             green*=0.01*range;
558             blue*=0.01*range;
559             black*=0.01*range;
560             alpha*=0.01*range;
561           }
562         if (image->colorspace == LabColorspace)
563           {
564             green+=(range+1)/2.0;
565             blue+=(range+1)/2.0;
566           }
567         pixel.red=(MagickRealType) ScaleAnyToQuantum((QuantumAny)
568           MagickMax(red+0.5,0.0),range);
569         pixel.green=(MagickRealType) ScaleAnyToQuantum((QuantumAny)
570           MagickMax(green+0.5,0.0),range);
571         pixel.blue=(MagickRealType) ScaleAnyToQuantum((QuantumAny)
572           MagickMax(blue+0.5,0.0),range);
573         pixel.black=(MagickRealType) ScaleAnyToQuantum((QuantumAny)
574           MagickMax(black+0.5,0.0),range);
575         pixel.alpha=(MagickRealType) ScaleAnyToQuantum((QuantumAny)
576           MagickMax(alpha+0.5,0.0),range);
577         q=GetAuthenticPixels(image,CastDoubleToLong(x_offset),
578           CastDoubleToLong(y_offset),1,1,exception);
579         if (q == (Quantum *) NULL)
580           {
581             status=MagickFalse;
582             break;
583           }
584         SetPixelViaPixelInfo(image,&pixel,q);
585         if (SyncAuthenticPixels(image,exception) == MagickFalse)
586           {
587             status=MagickFalse;
588             break;
589           }
590       }
591     }
592     if (status == MagickFalse)
593       break;
594     *text='\0';
595     (void) ReadBlobString(image,text);
596     if (LocaleNCompare((char *) text,MagickTXTID,strlen(MagickTXTID)) == 0)
597       {
598         /*
599           Allocate next image structure.
600         */
601         AcquireNextImage(image_info,image,exception);
602         if (GetNextImageInList(image) == (Image *) NULL)
603           {
604             status=MagickFalse;
605             break;
606           }
607         image=SyncNextImageInList(image);
608         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
609           GetBlobSize(image));
610         if (status == MagickFalse)
611           break;
612       }
613   } while (LocaleNCompare((char *) text,MagickTXTID,strlen(MagickTXTID)) == 0);
614   (void) CloseBlob(image);
615   if (q == (Quantum *) NULL)
616     return(DestroyImage(image));
617   return(GetFirstImageInList(image));
618 }
619 
620 /*
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 %                                                                             %
623 %                                                                             %
624 %                                                                             %
625 %   R e g i s t e r T X T I m a g e                                           %
626 %                                                                             %
627 %                                                                             %
628 %                                                                             %
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 %
631 %  RegisterTXTImage() adds attributes for the TXT image format to the
632 %  list of supported formats.  The attributes include the image format
633 %  tag, a method to read and/or write the format, whether the format
634 %  supports the saving of more than one frame to the same file or blob,
635 %  whether the format supports native in-memory I/O, and a brief
636 %  description of the format.
637 %
638 %  The format of the RegisterTXTImage method is:
639 %
640 %      size_t RegisterTXTImage(void)
641 %
642 */
RegisterTXTImage(void)643 ModuleExport size_t RegisterTXTImage(void)
644 {
645   MagickInfo
646     *entry;
647 
648   entry=AcquireMagickInfo("TXT","SPARSE-COLOR","Sparse Color");
649   entry->encoder=(EncodeImageHandler *) WriteTXTImage;
650   entry->flags|=CoderRawSupportFlag;
651   entry->flags|=CoderEndianSupportFlag;
652   (void) RegisterMagickInfo(entry);
653   entry=AcquireMagickInfo("TXT","TEXT","Text");
654   entry->decoder=(DecodeImageHandler *) ReadTEXTImage;
655   entry->format_type=ImplicitFormatType;
656   entry->flags|=CoderRawSupportFlag;
657   entry->flags|=CoderEndianSupportFlag;
658   (void) RegisterMagickInfo(entry);
659   entry=AcquireMagickInfo("TXT","TXT","Text");
660   entry->decoder=(DecodeImageHandler *) ReadTXTImage;
661   entry->encoder=(EncodeImageHandler *) WriteTXTImage;
662   entry->magick=(IsImageFormatHandler *) IsTXT;
663   (void) RegisterMagickInfo(entry);
664   return(MagickImageCoderSignature);
665 }
666 
667 /*
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 %                                                                             %
670 %                                                                             %
671 %                                                                             %
672 %   U n r e g i s t e r T X T I m a g e                                       %
673 %                                                                             %
674 %                                                                             %
675 %                                                                             %
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 %
678 %  UnregisterTXTImage() removes format registrations made by the
679 %  TXT module from the list of supported format.
680 %
681 %  The format of the UnregisterTXTImage method is:
682 %
683 %      UnregisterTXTImage(void)
684 %
685 */
UnregisterTXTImage(void)686 ModuleExport void UnregisterTXTImage(void)
687 {
688   (void) UnregisterMagickInfo("SPARSE-COLOR");
689   (void) UnregisterMagickInfo("TEXT");
690   (void) UnregisterMagickInfo("TXT");
691 }
692 
693 /*
694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695 %                                                                             %
696 %                                                                             %
697 %                                                                             %
698 %   W r i t e T X T I m a g e                                                 %
699 %                                                                             %
700 %                                                                             %
701 %                                                                             %
702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 %
704 %  WriteTXTImage writes the pixel values as text numbers.
705 %
706 %  The format of the WriteTXTImage method is:
707 %
708 %      MagickBooleanType WriteTXTImage(const ImageInfo *image_info,
709 %        Image *image,ExceptionInfo *exception)
710 %
711 %  A description of each parameter follows.
712 %
713 %    o image_info: the image info.
714 %
715 %    o image:  The image.
716 %
717 %    o exception: return any errors or warnings in this structure.
718 %
719 */
WriteTXTImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)720 static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image,
721   ExceptionInfo *exception)
722 {
723   char
724     buffer[MagickPathExtent],
725     colorspace[MagickPathExtent],
726     tuple[MagickPathExtent];
727 
728   MagickBooleanType
729     status;
730 
731   MagickOffsetType
732     scene;
733 
734   PixelInfo
735     pixel;
736 
737   const Quantum
738     *p;
739 
740   ssize_t
741     x;
742 
743   size_t
744     imageListLength;
745 
746   ssize_t
747     y;
748 
749   /*
750     Open output image file.
751   */
752   assert(image_info != (const ImageInfo *) NULL);
753   assert(image_info->signature == MagickCoreSignature);
754   assert(image != (Image *) NULL);
755   assert(image->signature == MagickCoreSignature);
756   if (image->debug != MagickFalse)
757     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
758   status=OpenBlob(image_info,image,WriteBlobMode,exception);
759   if (status == MagickFalse)
760     return(status);
761   scene=0;
762   imageListLength=GetImageListLength(image);
763   do
764   {
765     ComplianceType
766       compliance;
767 
768     const char
769       *value;
770 
771     (void) CopyMagickString(colorspace,CommandOptionToMnemonic(
772       MagickColorspaceOptions,(ssize_t) image->colorspace),MagickPathExtent);
773     LocaleLower(colorspace);
774     image->depth=GetImageQuantumDepth(image,MagickTrue);
775     if (image->alpha_trait != UndefinedPixelTrait)
776       (void) ConcatenateMagickString(colorspace,"a",MagickPathExtent);
777     compliance=NoCompliance;
778     value=GetImageOption(image_info,"txt:compliance");
779     if (value != (char *) NULL)
780       compliance=(ComplianceType) ParseCommandOption(MagickComplianceOptions,
781         MagickFalse,value);
782     if (LocaleCompare(image_info->magick,"SPARSE-COLOR") != 0)
783       {
784         (void) FormatLocaleString(buffer,MagickPathExtent,
785           "# ImageMagick pixel enumeration: %.20g,%.20g,%.20g,%s\n",(double)
786           image->columns,(double) image->rows,(double)
787           GetQuantumRange(image->depth),colorspace);
788         (void) WriteBlobString(image,buffer);
789       }
790     GetPixelInfo(image,&pixel);
791     for (y=0; y < (ssize_t) image->rows; y++)
792     {
793       p=GetVirtualPixels(image,0,y,image->columns,1,exception);
794       if (p == (const Quantum *) NULL)
795         break;
796       for (x=0; x < (ssize_t) image->columns; x++)
797       {
798         GetPixelInfoPixel(image,p,&pixel);
799         if (LocaleCompare(image_info->magick,"SPARSE-COLOR") == 0)
800           {
801             /*
802               Sparse-color format.
803             */
804             if (GetPixelAlpha(image,p) == (Quantum) OpaqueAlpha)
805               {
806                 GetColorTuple(&pixel,MagickFalse,tuple);
807                 (void) FormatLocaleString(buffer,MagickPathExtent,
808                   "%.20g,%.20g,",(double) x,(double) y);
809                 (void) WriteBlobString(image,buffer);
810                 (void) WriteBlobString(image,tuple);
811                 (void) WriteBlobString(image," ");
812               }
813             p+=GetPixelChannels(image);
814             continue;
815           }
816         (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g,%.20g: ",
817           (double) x,(double) y);
818         (void) WriteBlobString(image,buffer);
819         (void) CopyMagickString(tuple,"(",MagickPathExtent);
820         if ((pixel.colorspace == LinearGRAYColorspace) ||
821             (pixel.colorspace == GRAYColorspace))
822           ConcatenateColorComponent(&pixel,GrayPixelChannel,compliance,tuple);
823         else
824           {
825             ConcatenateColorComponent(&pixel,RedPixelChannel,compliance,tuple);
826             (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
827             ConcatenateColorComponent(&pixel,GreenPixelChannel,compliance,
828               tuple);
829             (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
830             ConcatenateColorComponent(&pixel,BluePixelChannel,compliance,
831               tuple);
832           }
833         if (pixel.colorspace == CMYKColorspace)
834           {
835             (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
836             ConcatenateColorComponent(&pixel,BlackPixelChannel,compliance,
837               tuple);
838           }
839         if (pixel.alpha_trait != UndefinedPixelTrait)
840           {
841             (void) ConcatenateMagickString(tuple,",",MagickPathExtent);
842             ConcatenateColorComponent(&pixel,AlphaPixelChannel,compliance,
843               tuple);
844           }
845         (void) ConcatenateMagickString(tuple,")",MagickPathExtent);
846         (void) WriteBlobString(image,tuple);
847         (void) WriteBlobString(image,"  ");
848         GetColorTuple(&pixel,MagickTrue,tuple);
849         (void) FormatLocaleString(buffer,MagickPathExtent,"%s",tuple);
850         (void) WriteBlobString(image,buffer);
851         (void) WriteBlobString(image,"  ");
852         (void) QueryColorname(image,&pixel,SVGCompliance,tuple,exception);
853         (void) WriteBlobString(image,tuple);
854         (void) WriteBlobString(image,"\n");
855         p+=GetPixelChannels(image);
856       }
857       status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
858         image->rows);
859       if (status == MagickFalse)
860         break;
861     }
862     if (GetNextImageInList(image) == (Image *) NULL)
863       break;
864     image=SyncNextImageInList(image);
865     status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
866     if (status == MagickFalse)
867       break;
868   } while (image_info->adjoin != MagickFalse);
869   (void) CloseBlob(image);
870   return(MagickTrue);
871 }
872