blob: 197fa7e645bf765eaeabb38984f1a0d9c7e669f5 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristye6ee7492013-03-16 15:04:42 +000013% Render Text Onto A Canvas Image. %
cristy3ed852e2009-09-05 21:47:34 +000014% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy3ed852e2009-09-05 21:47:34 +000070
71/*
72 Forward declarations.
73*/
74static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000075 WriteTXTImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000076
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*/
102static 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
cristyf2faecf2010-05-28 19:19:36 +0000112 unsigned long
cristy3ed852e2009-09-05 21:47:34 +0000113 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
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*/
159static 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
cristy3ed852e2009-09-05 21:47:34 +0000173 MagickBooleanType
174 status;
175
176 PointInfo
177 delta;
178
179 RectangleInfo
180 page;
181
cristyc6da28e2011-04-28 01:41:35 +0000182 ssize_t
183 offset;
184
cristy3ed852e2009-09-05 21:47:34 +0000185 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;
cristy2a11bef2011-10-28 18:33:11 +0000203 if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
cristy3ed852e2009-09-05 21:47:34 +0000204 {
205 GeometryInfo
206 geometry_info;
207
208 MagickStatusType
209 flags;
210
211 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
cristy2a11bef2011-10-28 18:33:11 +0000212 image->resolution.x=geometry_info.rho;
213 image->resolution.y=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000214 if ((flags & SigmaValue) == 0)
cristy2a11bef2011-10-28 18:33:11 +0000215 image->resolution.y=image->resolution.x;
cristy3ed852e2009-09-05 21:47:34 +0000216 }
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 */
cristy2a11bef2011-10-28 18:33:11 +0000226 image->columns=(size_t) floor((((double) page.width*image->resolution.x)/
cristyc6da28e2011-04-28 01:41:35 +0000227 delta.x)+0.5);
cristy2a11bef2011-10-28 18:33:11 +0000228 image->rows=(size_t) floor((((double) page.height*image->resolution.y)/
cristyc6da28e2011-04-28 01:41:35 +0000229 delta.y)+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000230 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 */
cristyea1a8aa2011-10-20 13:24:06 +0000248 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000249 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
250 (void) CloneString(&draw_info->text,image_info->filename);
cristyb51dff52011-05-19 16:55:47 +0000251 (void) FormatLocaleString(geometry,MaxTextExtent,"0x0%+ld%+ld",(long) page.x,
cristyf2faecf2010-05-28 19:19:36 +0000252 (long) page.y);
cristy3ed852e2009-09-05 21:47:34 +0000253 (void) CloneString(&draw_info->geometry,geometry);
cristy5cbc0162011-08-29 00:36:28 +0000254 status=GetTypeMetrics(image,draw_info,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000255 if (status == MagickFalse)
256 ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
cristybb503372010-05-27 20:51:26 +0000257 page.y=(ssize_t) ceil((double) page.y+metrics.ascent-0.5);
cristyb51dff52011-05-19 16:55:47 +0000258 (void) FormatLocaleString(geometry,MaxTextExtent,"0x0%+ld%+ld",(long) page.x,
cristyf2faecf2010-05-28 19:19:36 +0000259 (long) page.y);
cristy3ed852e2009-09-05 21:47:34 +0000260 (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");
cristybb503372010-05-27 20:51:26 +0000272 offset+=(ssize_t) (metrics.ascent-metrics.descent);
cristy3ed852e2009-09-05 21:47:34 +0000273 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);
cristybb503372010-05-27 20:51:26 +0000280 if ((offset < (ssize_t) image->rows) && (p != (char *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000281 continue;
282 if (texture != (Image *) NULL)
283 {
284 MagickProgressMonitor
285 progress_monitor;
286
287 progress_monitor=SetImageProgressMonitor(image,
288 (MagickProgressMonitor) NULL,image->client_data);
cristye941a752011-10-15 01:52:48 +0000289 (void) TextureImage(image,texture,exception);
cristy3ed852e2009-09-05 21:47:34 +0000290 (void) SetImageProgressMonitor(image,progress_monitor,
291 image->client_data);
292 }
cristy5cbc0162011-08-29 00:36:28 +0000293 (void) AnnotateImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000294 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;
cristy9950d572011-10-01 18:22:35 +0000301 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000302 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);
cristyea1a8aa2011-10-20 13:24:06 +0000311 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000312 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);
cristye941a752011-10-15 01:52:48 +0000324 (void) TextureImage(image,texture,exception);
cristy3ed852e2009-09-05 21:47:34 +0000325 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
326 }
cristy5cbc0162011-08-29 00:36:28 +0000327 (void) AnnotateImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000328 if (texture != (Image *) NULL)
329 texture=DestroyImage(texture);
330 draw_info=DestroyDrawInfo(draw_info);
331 (void) CloseBlob(image);
332 return(GetFirstImageInList(image));
333}
334
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*/
361static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
362{
363 char
364 colorspace[MaxTextExtent],
365 text[MaxTextExtent];
366
367 Image
368 *image;
369
cristyf2faecf2010-05-28 19:19:36 +0000370 long
cristy3ed852e2009-09-05 21:47:34 +0000371 type,
cristy9c7e2af2010-05-23 23:13:27 +0000372 x_offset,
373 y,
374 y_offset;
cristy3ed852e2009-09-05 21:47:34 +0000375
cristy101ab702011-10-13 13:06:32 +0000376 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000377 pixel;
378
379 MagickBooleanType
380 status;
381
382 QuantumAny
383 range;
384
cristybb503372010-05-27 20:51:26 +0000385 register ssize_t
cristy9c7e2af2010-05-23 23:13:27 +0000386 i,
387 x;
cristy3ed852e2009-09-05 21:47:34 +0000388
cristy4c08aed2011-07-01 19:47:50 +0000389 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000390 *q;
391
392 ssize_t
393 count;
394
cristyf2faecf2010-05-28 19:19:36 +0000395 unsigned long
cristy3ed852e2009-09-05 21:47:34 +0000396 depth,
cristyf2faecf2010-05-28 19:19:36 +0000397 height,
398 max_value,
399 width;
cristy3ed852e2009-09-05 21:47:34 +0000400
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);
cristy9950d572011-10-01 18:22:35 +0000411 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000412 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));
cristy9c7e2af2010-05-23 23:13:27 +0000422 do
cristy3ed852e2009-09-05 21:47:34 +0000423 {
cristy9c7e2af2010-05-23 23:13:27 +0000424 *colorspace='\0';
cristyf2faecf2010-05-28 19:19:36 +0000425 count=(ssize_t) sscanf(text+32,"%lu,%lu,%lu,%s",&width,&height,&max_value,
426 colorspace);
cristy9c7e2af2010-05-23 23:13:27 +0000427 if (count != 4)
428 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristyf2faecf2010-05-28 19:19:36 +0000429 image->columns=width;
430 image->rows=height;
cristy9c7e2af2010-05-23 23:13:27 +0000431 for (depth=1; (GetQuantumRange(depth)+1) < max_value; depth++) ;
432 image->depth=depth;
433 LocaleLower(colorspace);
cristybb503372010-05-27 20:51:26 +0000434 i=(ssize_t) strlen(colorspace)-1;
cristy8a46d822012-08-28 23:32:39 +0000435 image->alpha_trait=UndefinedPixelTrait;
cristy9c7e2af2010-05-23 23:13:27 +0000436 if ((i > 0) && (colorspace[i] == 'a'))
cristy3ed852e2009-09-05 21:47:34 +0000437 {
cristy9c7e2af2010-05-23 23:13:27 +0000438 colorspace[i]='\0';
cristy8a46d822012-08-28 23:32:39 +0000439 image->alpha_trait=BlendPixelTrait;
cristy9c7e2af2010-05-23 23:13:27 +0000440 }
cristy042ee782011-04-22 18:48:30 +0000441 type=ParseCommandOption(MagickColorspaceOptions,MagickFalse,colorspace);
cristy9c7e2af2010-05-23 23:13:27 +0000442 if (type < 0)
443 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristyea1a8aa2011-10-20 13:24:06 +0000444 (void) SetImageBackgroundColor(image,exception);
cristycaf45802012-06-16 18:28:54 +0000445 (void) SetImageColorspace(image,(ColorspaceType) type,exception);
cristye9139612012-01-03 02:19:10 +0000446 GetPixelInfo(image,&pixel);
cristy9c7e2af2010-05-23 23:13:27 +0000447 range=GetQuantumRange(image->depth);
cristybb503372010-05-27 20:51:26 +0000448 for (y=0; y < (ssize_t) image->rows; y++)
cristy9c7e2af2010-05-23 23:13:27 +0000449 {
cristy9333f752012-10-10 23:49:45 +0000450 double
451 alpha,
452 black,
453 blue,
454 green,
455 red;
456
cristybb503372010-05-27 20:51:26 +0000457 for (x=0; x < (ssize_t) image->columns; x++)
cristy9c7e2af2010-05-23 23:13:27 +0000458 {
459 if (ReadBlobString(image,text) == (char *) NULL)
460 break;
cristye9139612012-01-03 02:19:10 +0000461 switch (image->colorspace)
462 {
463 case GRAYColorspace:
cristy9c7e2af2010-05-23 23:13:27 +0000464 {
cristy8a46d822012-08-28 23:32:39 +0000465 if (image->alpha_trait == BlendPixelTrait)
cristye9139612012-01-03 02:19:10 +0000466 {
467 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf,%lf",&x_offset,
cristy9333f752012-10-10 23:49:45 +0000468 &y_offset,&red,&alpha);
469 green=red;
470 blue=red;
cristye9139612012-01-03 02:19:10 +0000471 break;
472 }
473 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf",&x_offset,&y_offset,
cristy9333f752012-10-10 23:49:45 +0000474 &red);
475 green=red;
476 blue=red;
cristye9139612012-01-03 02:19:10 +0000477 break;
cristy9c7e2af2010-05-23 23:13:27 +0000478 }
cristye9139612012-01-03 02:19:10 +0000479 case CMYKColorspace:
480 {
cristy8a46d822012-08-28 23:32:39 +0000481 if (image->alpha_trait == BlendPixelTrait)
cristye9139612012-01-03 02:19:10 +0000482 {
483 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf,%lf,%lf,%lf,%lf",
cristy9333f752012-10-10 23:49:45 +0000484 &x_offset,&y_offset,&red,&green,&blue,&black,&alpha);
cristye9139612012-01-03 02:19:10 +0000485 break;
486 }
cristy101ab702011-10-13 13:06:32 +0000487 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf,%lf,%lf,%lf",&x_offset,
cristy9333f752012-10-10 23:49:45 +0000488 &y_offset,&red,&green,&blue,&black);
cristye9139612012-01-03 02:19:10 +0000489 break;
490 }
491 default:
492 {
cristy8a46d822012-08-28 23:32:39 +0000493 if (image->alpha_trait == BlendPixelTrait)
cristye9139612012-01-03 02:19:10 +0000494 {
495 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf,%lf,%lf,%lf",
cristy9333f752012-10-10 23:49:45 +0000496 &x_offset,&y_offset,&red,&green,&blue,&alpha);
cristye9139612012-01-03 02:19:10 +0000497 break;
498 }
cristy101ab702011-10-13 13:06:32 +0000499 count=(ssize_t) sscanf(text,"%ld,%ld: (%lf,%lf,%lf",&x_offset,
cristy9333f752012-10-10 23:49:45 +0000500 &y_offset,&red,&green,&blue);
cristye9139612012-01-03 02:19:10 +0000501 break;
502 }
503 }
cristyc5349322012-11-27 02:36:11 +0000504 if (image->colorspace == LabColorspace)
cristy24098612012-11-27 02:18:38 +0000505 {
cristyc5349322012-11-27 02:36:11 +0000506 green+=(range+1)/2.0;
507 blue+=(range+1)/2.0;
cristy24098612012-11-27 02:18:38 +0000508 }
cristy9333f752012-10-10 23:49:45 +0000509 pixel.red=ScaleAnyToQuantum((QuantumAny) (red+0.5),range);
510 pixel.green=ScaleAnyToQuantum((QuantumAny) (green+0.5),range);
511 pixel.blue=ScaleAnyToQuantum((QuantumAny) (blue+0.5),range);
512 pixel.black=ScaleAnyToQuantum((QuantumAny) (black+0.5),range);
513 pixel.alpha=ScaleAnyToQuantum((QuantumAny) (alpha+0.5),range);
cristy9c7e2af2010-05-23 23:13:27 +0000514 q=GetAuthenticPixels(image,x_offset,y_offset,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000515 if (q == (Quantum *) NULL)
cristy9c7e2af2010-05-23 23:13:27 +0000516 continue;
cristye9139612012-01-03 02:19:10 +0000517 SetPixelInfoPixel(image,&pixel,q);
cristy9c7e2af2010-05-23 23:13:27 +0000518 if (SyncAuthenticPixels(image,exception) == MagickFalse)
519 break;
cristy3ed852e2009-09-05 21:47:34 +0000520 }
cristy9c7e2af2010-05-23 23:13:27 +0000521 }
522 (void) ReadBlobString(image,text);
523 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000524 {
cristy9c7e2af2010-05-23 23:13:27 +0000525 /*
526 Allocate next image structure.
527 */
cristy9950d572011-10-01 18:22:35 +0000528 AcquireNextImage(image_info,image,exception);
cristy9c7e2af2010-05-23 23:13:27 +0000529 if (GetNextImageInList(image) == (Image *) NULL)
530 {
531 image=DestroyImageList(image);
532 return((Image *) NULL);
533 }
534 image=SyncNextImageInList(image);
535 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
536 GetBlobSize(image));
537 if (status == MagickFalse)
538 break;
cristy3ed852e2009-09-05 21:47:34 +0000539 }
cristy9c7e2af2010-05-23 23:13:27 +0000540 } while (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) == 0);
541 (void) CloseBlob(image);
cristy3ed852e2009-09-05 21:47:34 +0000542 return(GetFirstImageInList(image));
543}
544
545/*
546%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547% %
548% %
549% %
550% R e g i s t e r T X T I m a g e %
551% %
552% %
553% %
554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555%
556% RegisterTXTImage() adds attributes for the TXT image format to the
557% list of supported formats. The attributes include the image format
558% tag, a method to read and/or write the format, whether the format
559% supports the saving of more than one frame to the same file or blob,
560% whether the format supports native in-memory I/O, and a brief
561% description of the format.
562%
563% The format of the RegisterTXTImage method is:
564%
cristybb503372010-05-27 20:51:26 +0000565% size_t RegisterTXTImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000566%
567*/
cristybb503372010-05-27 20:51:26 +0000568ModuleExport size_t RegisterTXTImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000569{
570 MagickInfo
571 *entry;
572
cristye6ee7492013-03-16 15:04:42 +0000573 entry=SetMagickInfo("SPARSE-COLOR");
574 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
575 entry->raw=MagickTrue;
576 entry->endian_support=MagickTrue;
577 entry->description=ConstantString("Sparse Color");
578 entry->module=ConstantString("TXT");
579 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +0000580 entry=SetMagickInfo("TEXT");
581 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
582 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
583 entry->raw=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000584 entry->endian_support=MagickTrue;
585 entry->description=ConstantString("Text");
586 entry->module=ConstantString("TXT");
587 (void) RegisterMagickInfo(entry);
588 entry=SetMagickInfo("TXT");
589 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
590 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
cristy3ed852e2009-09-05 21:47:34 +0000591 entry->description=ConstantString("Text");
592 entry->magick=(IsImageFormatHandler *) IsTXT;
593 entry->module=ConstantString("TXT");
594 (void) RegisterMagickInfo(entry);
595 return(MagickImageCoderSignature);
596}
597
598/*
599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600% %
601% %
602% %
603% U n r e g i s t e r T X T I m a g e %
604% %
605% %
606% %
607%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
608%
609% UnregisterTXTImage() removes format registrations made by the
610% TXT module from the list of supported format.
611%
612% The format of the UnregisterTXTImage method is:
613%
614% UnregisterTXTImage(void)
615%
616*/
617ModuleExport void UnregisterTXTImage(void)
618{
cristye6ee7492013-03-16 15:04:42 +0000619 (void) UnregisterMagickInfo("SPARSE-COLOR");
cristy3ed852e2009-09-05 21:47:34 +0000620 (void) UnregisterMagickInfo("TEXT");
621 (void) UnregisterMagickInfo("TXT");
622}
623
624/*
625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626% %
627% %
628% %
629% W r i t e T X T I m a g e %
630% %
631% %
632% %
633%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634%
635% WriteTXTImage writes the pixel values as text numbers.
636%
637% The format of the WriteTXTImage method is:
638%
cristy3a37efd2011-08-28 20:31:03 +0000639% MagickBooleanType WriteTXTImage(const ImageInfo *image_info,
640% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000641%
642% A description of each parameter follows.
643%
644% o image_info: the image info.
645%
646% o image: The image.
647%
cristy3a37efd2011-08-28 20:31:03 +0000648% o exception: return any errors or warnings in this structure.
649%
cristy3ed852e2009-09-05 21:47:34 +0000650*/
cristy3a37efd2011-08-28 20:31:03 +0000651static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image,
652 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000653{
654 char
655 buffer[MaxTextExtent],
656 colorspace[MaxTextExtent],
657 tuple[MaxTextExtent];
658
cristy3ed852e2009-09-05 21:47:34 +0000659 MagickBooleanType
660 status;
661
cristy9c7e2af2010-05-23 23:13:27 +0000662 MagickOffsetType
663 scene;
664
cristy4c08aed2011-07-01 19:47:50 +0000665 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000666 pixel;
667
cristy4c08aed2011-07-01 19:47:50 +0000668 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000669 *p;
670
cristybb503372010-05-27 20:51:26 +0000671 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000672 x;
673
cristyc6da28e2011-04-28 01:41:35 +0000674 ssize_t
675 y;
676
cristy3ed852e2009-09-05 21:47:34 +0000677 /*
678 Open output image file.
679 */
680 assert(image_info != (const ImageInfo *) NULL);
681 assert(image_info->signature == MagickSignature);
682 assert(image != (Image *) NULL);
683 assert(image->signature == MagickSignature);
684 if (image->debug != MagickFalse)
685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000686 status=OpenBlob(image_info,image,WriteBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000687 if (status == MagickFalse)
688 return(status);
cristy9c7e2af2010-05-23 23:13:27 +0000689 scene=0;
690 do
cristy3ed852e2009-09-05 21:47:34 +0000691 {
cristy2c992db2013-09-02 02:12:55 +0000692 ComplianceType
693 compliance;
694
cristy042ee782011-04-22 18:48:30 +0000695 (void) CopyMagickString(colorspace,CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +0000696 MagickColorspaceOptions,(ssize_t) image->colorspace),MaxTextExtent);
cristy9c7e2af2010-05-23 23:13:27 +0000697 LocaleLower(colorspace);
698 image->depth=GetImageQuantumDepth(image,MagickTrue);
cristy8a46d822012-08-28 23:32:39 +0000699 if (image->alpha_trait == BlendPixelTrait)
cristy9c7e2af2010-05-23 23:13:27 +0000700 (void) ConcatenateMagickString(colorspace,"a",MaxTextExtent);
cristy2c992db2013-09-02 02:12:55 +0000701 compliance=NoCompliance;
cristye6ee7492013-03-16 15:04:42 +0000702 if (LocaleCompare(image_info->magick,"SPARSE-COLOR") != 0)
703 {
704 (void) FormatLocaleString(buffer,MaxTextExtent,
705 "# ImageMagick pixel enumeration: %.20g,%.20g,%.20g,%s\n",(double)
cristy1a598952013-09-06 13:11:23 +0000706 image->columns,(double) image->rows,(double) ((MagickOffsetType)
707 GetQuantumRange(image->depth)),colorspace);
cristye6ee7492013-03-16 15:04:42 +0000708 (void) WriteBlobString(image,buffer);
cristy2c992db2013-09-02 02:12:55 +0000709 compliance=SVGCompliance;
cristye6ee7492013-03-16 15:04:42 +0000710 }
cristy4c08aed2011-07-01 19:47:50 +0000711 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +0000712 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000713 {
cristy3a37efd2011-08-28 20:31:03 +0000714 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000715 if (p == (const Quantum *) NULL)
cristy9c7e2af2010-05-23 23:13:27 +0000716 break;
cristybb503372010-05-27 20:51:26 +0000717 for (x=0; x < (ssize_t) image->columns; x++)
cristy9c7e2af2010-05-23 23:13:27 +0000718 {
cristy803640d2011-11-17 02:11:32 +0000719 GetPixelInfoPixel(image,p,&pixel);
cristy24098612012-11-27 02:18:38 +0000720 if (pixel.colorspace == LabColorspace)
721 {
722 pixel.green-=(QuantumRange+1)/2.0;
723 pixel.blue-=(QuantumRange+1)/2.0;
724 }
cristye6ee7492013-03-16 15:04:42 +0000725 if (LocaleCompare(image_info->magick,"SPARSE-COLOR") == 0)
726 {
727 /*
728 Sparse-color format.
729 */
730 if (GetPixelAlpha(image,p) == (Quantum) OpaqueAlpha)
731 {
cristy0e339ba2013-03-18 00:20:48 +0000732 GetColorTuple(&pixel,MagickFalse,tuple);
cristye6ee7492013-03-16 15:04:42 +0000733 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g,%.20g,",
734 (double) x,(double) y);
735 (void) WriteBlobString(image,buffer);
736 (void) WriteBlobString(image,tuple);
737 (void) WriteBlobString(image," ");
738 }
739 p+=GetPixelChannels(image);
740 continue;
741 }
cristybbb9b902013-03-13 10:25:06 +0000742 (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g,%.20g: ",(double)
743 x,(double) y);
744 (void) WriteBlobString(image,buffer);
cristy9c7e2af2010-05-23 23:13:27 +0000745 (void) CopyMagickString(tuple,"(",MaxTextExtent);
cristye9139612012-01-03 02:19:10 +0000746 if (pixel.colorspace == GRAYColorspace)
cristy2c992db2013-09-02 02:12:55 +0000747 ConcatenateColorComponent(&pixel,GrayPixelChannel,compliance,
cristye9139612012-01-03 02:19:10 +0000748 tuple);
749 else
750 {
cristy2c992db2013-09-02 02:12:55 +0000751 ConcatenateColorComponent(&pixel,RedPixelChannel,compliance,tuple);
752 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
753 ConcatenateColorComponent(&pixel,GreenPixelChannel,compliance,
cristye9139612012-01-03 02:19:10 +0000754 tuple);
755 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
cristy2c992db2013-09-02 02:12:55 +0000756 ConcatenateColorComponent(&pixel,BluePixelChannel,compliance,tuple);
cristye9139612012-01-03 02:19:10 +0000757 }
cristy9c7e2af2010-05-23 23:13:27 +0000758 if (pixel.colorspace == CMYKColorspace)
759 {
760 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
cristy2c992db2013-09-02 02:12:55 +0000761 ConcatenateColorComponent(&pixel,BlackPixelChannel,compliance,
cristyfa806a72011-07-04 02:06:13 +0000762 tuple);
cristy9c7e2af2010-05-23 23:13:27 +0000763 }
cristy8a46d822012-08-28 23:32:39 +0000764 if (pixel.alpha_trait == BlendPixelTrait)
cristy9c7e2af2010-05-23 23:13:27 +0000765 {
766 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
cristy2c992db2013-09-02 02:12:55 +0000767 ConcatenateColorComponent(&pixel,AlphaPixelChannel,compliance,
cristyfa806a72011-07-04 02:06:13 +0000768 tuple);
cristy9c7e2af2010-05-23 23:13:27 +0000769 }
770 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
771 (void) WriteBlobString(image,tuple);
772 (void) WriteBlobString(image," ");
773 GetColorTuple(&pixel,MagickTrue,tuple);
cristyb51dff52011-05-19 16:55:47 +0000774 (void) FormatLocaleString(buffer,MaxTextExtent,"%s",tuple);
cristy9c7e2af2010-05-23 23:13:27 +0000775 (void) WriteBlobString(image,buffer);
776 (void) WriteBlobString(image," ");
cristy269c9412011-10-13 23:41:15 +0000777 (void) QueryColorname(image,&pixel,SVGCompliance,tuple,exception);
cristy9c7e2af2010-05-23 23:13:27 +0000778 (void) WriteBlobString(image,tuple);
779 (void) WriteBlobString(image,"\n");
cristyed231572011-07-14 02:18:59 +0000780 p+=GetPixelChannels(image);
cristy9c7e2af2010-05-23 23:13:27 +0000781 }
cristycee97112010-05-28 00:44:52 +0000782 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy2aac09a2010-06-06 16:46:43 +0000783 image->rows);
cristy9c7e2af2010-05-23 23:13:27 +0000784 if (status == MagickFalse)
785 break;
cristy3ed852e2009-09-05 21:47:34 +0000786 }
cristy9c7e2af2010-05-23 23:13:27 +0000787 if (GetNextImageInList(image) == (Image *) NULL)
788 break;
789 image=SyncNextImageInList(image);
790 status=SetImageProgress(image,SaveImagesTag,scene++,
791 GetImageListLength(image));
cristy3ed852e2009-09-05 21:47:34 +0000792 if (status == MagickFalse)
793 break;
cristy9c7e2af2010-05-23 23:13:27 +0000794 } while (image_info->adjoin != MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000795 (void) CloseBlob(image);
796 return(MagickTrue);
797}