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