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