2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Render Text Onto A Canvas Image. %
20 % Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/annotate.h"
44 #include "magick/attribute.h"
45 #include "magick/blob.h"
46 #include "magick/blob-private.h"
47 #include "magick/cache.h"
48 #include "magick/color.h"
49 #include "magick/color-private.h"
50 #include "magick/colorspace.h"
51 #include "magick/constitute.h"
52 #include "magick/draw.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/geometry.h"
56 #include "magick/image.h"
57 #include "magick/image-private.h"
58 #include "magick/list.h"
59 #include "magick/magick.h"
60 #include "magick/memory_.h"
61 #include "magick/monitor.h"
62 #include "magick/monitor-private.h"
63 #include "magick/option.h"
64 #include "magick/pixel-private.h"
65 #include "magick/quantum-private.h"
66 #include "magick/static.h"
67 #include "magick/statistic.h"
68 #include "magick/string_.h"
69 #include "magick/module.h"
74 static MagickBooleanType
75 WriteTXTImage(const ImageInfo *,Image *);
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % IsTXT() returns MagickTrue if the image format type, identified by the magick
91 % The format of the IsTXT method is:
93 % MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
95 % A description of each parameter follows:
97 % o magick: compare image format pattern against these bytes.
99 % o length: Specifies the length of the magick string.
102 static MagickBooleanType IsTXT(const unsigned char *magick,const size_t length)
104 #define MagickID "# ImageMagick pixel enumeration:"
107 colorspace[MaxTextExtent];
119 if (LocaleNCompare((const char *) magick,MagickID,strlen(MagickID)) != 0)
121 count=(ssize_t) sscanf((const char *) magick+32,"%lu,%lu,%lu,%s",&columns,
122 &rows,&depth,colorspace);
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % R e a d T E X T I m a g e %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
143 % The format of the ReadTEXTImage method is:
145 % Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
146 % char *text,ExceptionInfo *exception)
148 % A description of each parameter follows:
150 % o image_info: the image info.
152 % o image: the image.
154 % o text: the text storage buffer.
156 % o exception: return any errors or warnings in this structure.
159 static Image *ReadTEXTImage(const ImageInfo *image_info,Image *image,
160 char *text,ExceptionInfo *exception)
163 filename[MaxTextExtent],
164 geometry[MaxTextExtent],
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);
199 Set the page geometry.
201 delta.x=DefaultResolution;
202 delta.y=DefaultResolution;
203 if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
211 flags=ParseGeometry(PSDensityGeometry,&geometry_info);
212 image->x_resolution=geometry_info.rho;
213 image->y_resolution=geometry_info.sigma;
214 if ((flags & SigmaValue) == 0)
215 image->y_resolution=image->x_resolution;
221 if (image_info->page != (char *) NULL)
222 (void) ParseAbsoluteGeometry(image_info->page,&page);
224 Initialize Image structure.
226 image->columns=(unsigned long) (((page.width*image->x_resolution)/
228 image->rows=(unsigned long) (((page.height*image->y_resolution)/delta.y)+0.5);
231 texture=(Image *) NULL;
232 if (image_info->texture != (char *) NULL)
237 read_info=CloneImageInfo(image_info);
238 SetImageInfoBlob(read_info,(void *) NULL,0);
239 (void) CopyMagickString(read_info->filename,image_info->texture,
241 texture=ReadImage(read_info,exception);
242 read_info=DestroyImageInfo(read_info);
245 Annotate the text image.
247 (void) SetImageBackgroundColor(image);
248 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
249 (void) CloneString(&draw_info->text,image_info->filename);
250 (void) FormatMagickString(geometry,MaxTextExtent,"0x0%+ld%+ld",page.x,page.y);
251 (void) CloneString(&draw_info->geometry,geometry);
252 status=GetTypeMetrics(image,draw_info,&metrics);
253 if (status == MagickFalse)
254 ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
255 page.y=(long) (page.y+metrics.ascent+0.5);
256 (void) FormatMagickString(geometry,MaxTextExtent,"0x0%+ld%+ld",page.x,page.y);
257 (void) CloneString(&draw_info->geometry,geometry);
258 (void) CopyMagickString(filename,image_info->filename,MaxTextExtent);
259 if (*draw_info->text != '\0')
260 *draw_info->text='\0';
262 for (offset=2*page.y; p != (char *) NULL; )
265 Annotate image with text.
267 (void) ConcatenateString(&draw_info->text,text);
268 (void) ConcatenateString(&draw_info->text,"\n");
269 offset+=(long) (metrics.ascent-metrics.descent);
270 if (image->previous == (Image *) NULL)
272 status=SetImageProgress(image,LoadImageTag,offset,image->rows);
273 if (status == MagickFalse)
276 p=ReadBlobString(image,text);
277 if ((offset < (long) image->rows) && (p != (char *) NULL))
279 if (texture != (Image *) NULL)
281 MagickProgressMonitor
284 progress_monitor=SetImageProgressMonitor(image,
285 (MagickProgressMonitor) NULL,image->client_data);
286 (void) TextureImage(image,texture);
287 (void) SetImageProgressMonitor(image,progress_monitor,
290 (void) AnnotateImage(image,draw_info);
291 if (p == (char *) NULL)
294 Page is full-- allocate next image structure.
296 *draw_info->text='\0';
298 AcquireNextImage(image_info,image);
299 if (GetNextImageInList(image) == (Image *) NULL)
301 image=DestroyImageList(image);
302 return((Image *) NULL);
304 image->next->columns=image->columns;
305 image->next->rows=image->rows;
306 image=SyncNextImageInList(image);
307 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
308 (void) SetImageBackgroundColor(image);
309 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
311 if (status == MagickFalse)
314 if (texture != (Image *) NULL)
316 MagickProgressMonitor
319 progress_monitor=SetImageProgressMonitor(image,
320 (MagickProgressMonitor) NULL,image->client_data);
321 (void) TextureImage(image,texture);
322 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
324 (void) AnnotateImage(image,draw_info);
325 if (texture != (Image *) NULL)
326 texture=DestroyImage(texture);
327 draw_info=DestroyDrawInfo(draw_info);
328 (void) CloseBlob(image);
329 return(GetFirstImageInList(image));
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 % R e a d T X T I m a g e %
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % ReadTXTImage() reads a text file and returns it as an image. It allocates
344 % the memory necessary for the new Image structure and returns a pointer to
347 % The format of the ReadTXTImage method is:
349 % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
351 % A description of each parameter follows:
353 % o image_info: the image info.
355 % o exception: return any errors or warnings in this structure.
358 static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
361 colorspace[MaxTextExtent],
400 assert(image_info != (const ImageInfo *) NULL);
401 assert(image_info->signature == MagickSignature);
402 if (image_info->debug != MagickFalse)
403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
404 image_info->filename);
405 assert(exception != (ExceptionInfo *) NULL);
406 assert(exception->signature == MagickSignature);
407 image=AcquireImage(image_info);
408 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
409 if (status == MagickFalse)
411 image=DestroyImageList(image);
412 return((Image *) NULL);
414 (void) ResetMagickMemory(text,0,sizeof(text));
415 (void) ReadBlobString(image,text);
416 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) != 0)
417 return(ReadTEXTImage(image_info,image,text,exception));
419 count=(ssize_t) sscanf(text+32,"%lu,%lu,%lu,%s",&image->columns,
420 &image->rows,&max_value,colorspace);
422 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
423 for (depth=1; (GetQuantumRange(depth)+1) < max_value; depth++) ;
425 LocaleLower(colorspace);
426 i=(long) strlen(colorspace)-1;
427 image->matte=MagickFalse;
428 if ((i > 0) && (colorspace[i] == 'a'))
431 image->matte=MagickTrue;
433 type=ParseMagickOption(MagickColorspaceOptions,MagickFalse,colorspace);
435 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
436 image->colorspace=(ColorspaceType) type;
437 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
438 (void) SetImageBackgroundColor(image);
439 range=GetQuantumRange(image->depth);
440 while (ReadBlobString(image,text) != (char *) NULL)
442 if (image->colorspace == CMYKColorspace)
444 if (image->matte != MagickFalse)
445 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu,%lu",&x,&y,
446 &pixel.red,&pixel.green,&pixel.blue,&pixel.index,&pixel.opacity);
448 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu",&x,&y,
449 &pixel.red,&pixel.green,&pixel.blue,&pixel.index);
452 if (image->matte != MagickFalse)
453 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu",&x,&y,
454 &pixel.red,&pixel.green,&pixel.blue,&pixel.opacity);
456 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu",&x,&y,
457 &pixel.red,&pixel.green,&pixel.blue);
460 q=GetAuthenticPixels(image,x,y,1,1,exception);
461 if (q == (PixelPacket *) NULL)
463 q->red=ScaleAnyToQuantum(pixel.red,range);
464 q->green=ScaleAnyToQuantum(pixel.green,range);
465 q->blue=ScaleAnyToQuantum(pixel.blue,range);
466 if (image->colorspace == CMYKColorspace)
468 indexes=GetAuthenticIndexQueue(image);
469 *indexes=ScaleAnyToQuantum(pixel.index,range);
471 if (image->matte != MagickFalse)
472 q->opacity=(Quantum) (QuantumRange-ScaleAnyToQuantum(pixel.opacity,
474 if (SyncAuthenticPixels(image,exception) == MagickFalse)
477 return(GetFirstImageInList(image));
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 % R e g i s t e r T X T I m a g e %
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 % RegisterTXTImage() adds attributes for the TXT image format to the
492 % list of supported formats. The attributes include the image format
493 % tag, a method to read and/or write the format, whether the format
494 % supports the saving of more than one frame to the same file or blob,
495 % whether the format supports native in-memory I/O, and a brief
496 % description of the format.
498 % The format of the RegisterTXTImage method is:
500 % unsigned long RegisterTXTImage(void)
503 ModuleExport unsigned long RegisterTXTImage(void)
508 entry=SetMagickInfo("TEXT");
509 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
510 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
511 entry->raw=MagickTrue;
512 entry->adjoin=MagickFalse;
513 entry->endian_support=MagickTrue;
514 entry->description=ConstantString("Text");
515 entry->module=ConstantString("TXT");
516 (void) RegisterMagickInfo(entry);
517 entry=SetMagickInfo("TXT");
518 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
519 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
520 entry->adjoin=MagickFalse;
521 entry->description=ConstantString("Text");
522 entry->magick=(IsImageFormatHandler *) IsTXT;
523 entry->module=ConstantString("TXT");
524 (void) RegisterMagickInfo(entry);
525 return(MagickImageCoderSignature);
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 % U n r e g i s t e r T X T I m a g e %
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 % UnregisterTXTImage() removes format registrations made by the
540 % TXT module from the list of supported format.
542 % The format of the UnregisterTXTImage method is:
544 % UnregisterTXTImage(void)
547 ModuleExport void UnregisterTXTImage(void)
549 (void) UnregisterMagickInfo("TEXT");
550 (void) UnregisterMagickInfo("TXT");
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % W r i t e T X T I m a g e %
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % WriteTXTImage writes the pixel values as text numbers.
566 % The format of the WriteTXTImage method is:
568 % MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image)
570 % A description of each parameter follows.
572 % o image_info: the image info.
574 % o image: The image.
577 static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image)
580 buffer[MaxTextExtent],
581 colorspace[MaxTextExtent],
582 tuple[MaxTextExtent];
593 register const IndexPacket
596 register const PixelPacket
603 Open output image file.
605 assert(image_info != (const ImageInfo *) NULL);
606 assert(image_info->signature == MagickSignature);
607 assert(image != (Image *) NULL);
608 assert(image->signature == MagickSignature);
609 if (image->debug != MagickFalse)
610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
611 status=OpenBlob(image_info,image,WriteBlobMode,&image->exception);
612 if (status == MagickFalse)
614 (void) CopyMagickString(colorspace,MagickOptionToMnemonic(
615 MagickColorspaceOptions,(long) image->colorspace),MaxTextExtent);
616 LocaleLower(colorspace);
617 image->depth=GetImageQuantumDepth(image,MagickTrue);
618 if (image->matte != MagickFalse)
619 (void) ConcatenateMagickString(colorspace,"a",MaxTextExtent);
620 (void) FormatMagickString(buffer,MaxTextExtent,
621 "# ImageMagick pixel enumeration: %lu,%lu,%lu,%s\n",image->columns,
622 image->rows,(unsigned long) GetQuantumRange(image->depth),colorspace);
623 (void) WriteBlobString(image,buffer);
624 GetMagickPixelPacket(image,&pixel);
625 for (y=0; y < (long) image->rows; y++)
627 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
628 if (p == (const PixelPacket *) NULL)
630 indexes=GetVirtualIndexQueue(image);
631 for (x=0; x < (long) image->columns; x++)
633 (void) FormatMagickString(buffer,MaxTextExtent,"%ld,%ld: ",x,y);
634 (void) WriteBlobString(image,buffer);
635 SetMagickPixelPacket(image,p,indexes+x,&pixel);
636 (void) CopyMagickString(tuple,"(",MaxTextExtent);
637 ConcatenateColorComponent(&pixel,RedChannel,X11Compliance,tuple);
638 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
639 ConcatenateColorComponent(&pixel,GreenChannel,X11Compliance,tuple);
640 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
641 ConcatenateColorComponent(&pixel,BlueChannel,X11Compliance,tuple);
642 if (pixel.colorspace == CMYKColorspace)
644 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
645 ConcatenateColorComponent(&pixel,IndexChannel,X11Compliance,tuple);
647 if (pixel.matte != MagickFalse)
649 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
650 ConcatenateColorComponent(&pixel,AlphaChannel,X11Compliance,tuple);
652 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
653 (void) WriteBlobString(image,tuple);
654 (void) WriteBlobString(image," ");
655 GetColorTuple(&pixel,MagickTrue,tuple);
656 (void) FormatMagickString(buffer,MaxTextExtent,"%s",tuple);
657 (void) WriteBlobString(image,buffer);
658 (void) WriteBlobString(image," ");
659 (void) QueryMagickColorname(image,&pixel,SVGCompliance,tuple,
661 (void) WriteBlobString(image,tuple);
662 (void) WriteBlobString(image,"\n");
665 status=SetImageProgress(image,SaveImageTag,y,image->rows);
666 if (status == MagickFalse)
669 (void) CloseBlob(image);