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