Coverage Report

Created: 2026-09-28 06:47

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