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