2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Render Text Onto A Canvas Image. %
20 % Copyright 1999-2010 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) floor(((page.width*image->x_resolution)/
228 image->rows=(unsigned long) floor(((page.height*image->y_resolution)/
232 texture=(Image *) NULL;
233 if (image_info->texture != (char *) NULL)
238 read_info=CloneImageInfo(image_info);
239 SetImageInfoBlob(read_info,(void *) NULL,0);
240 (void) CopyMagickString(read_info->filename,image_info->texture,
242 texture=ReadImage(read_info,exception);
243 read_info=DestroyImageInfo(read_info);
246 Annotate the text image.
248 (void) SetImageBackgroundColor(image);
249 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
250 (void) CloneString(&draw_info->text,image_info->filename);
251 (void) FormatMagickString(geometry,MaxTextExtent,"0x0%+ld%+ld",page.x,page.y);
252 (void) CloneString(&draw_info->geometry,geometry);
253 status=GetTypeMetrics(image,draw_info,&metrics);
254 if (status == MagickFalse)
255 ThrowReaderException(TypeError,"UnableToGetTypeMetrics");
256 page.y=(long) ceil(page.y+metrics.ascent-0.5);
257 (void) FormatMagickString(geometry,MaxTextExtent,"0x0%+ld%+ld",page.x,page.y);
258 (void) CloneString(&draw_info->geometry,geometry);
259 (void) CopyMagickString(filename,image_info->filename,MaxTextExtent);
260 if (*draw_info->text != '\0')
261 *draw_info->text='\0';
263 for (offset=2*page.y; p != (char *) NULL; )
266 Annotate image with text.
268 (void) ConcatenateString(&draw_info->text,text);
269 (void) ConcatenateString(&draw_info->text,"\n");
270 offset+=(long) (metrics.ascent-metrics.descent);
271 if (image->previous == (Image *) NULL)
273 status=SetImageProgress(image,LoadImageTag,offset,image->rows);
274 if (status == MagickFalse)
277 p=ReadBlobString(image,text);
278 if ((offset < (long) image->rows) && (p != (char *) NULL))
280 if (texture != (Image *) NULL)
282 MagickProgressMonitor
285 progress_monitor=SetImageProgressMonitor(image,
286 (MagickProgressMonitor) NULL,image->client_data);
287 (void) TextureImage(image,texture);
288 (void) SetImageProgressMonitor(image,progress_monitor,
291 (void) AnnotateImage(image,draw_info);
292 if (p == (char *) NULL)
295 Page is full-- allocate next image structure.
297 *draw_info->text='\0';
299 AcquireNextImage(image_info,image);
300 if (GetNextImageInList(image) == (Image *) NULL)
302 image=DestroyImageList(image);
303 return((Image *) NULL);
305 image->next->columns=image->columns;
306 image->next->rows=image->rows;
307 image=SyncNextImageInList(image);
308 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
309 (void) SetImageBackgroundColor(image);
310 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
312 if (status == MagickFalse)
315 if (texture != (Image *) NULL)
317 MagickProgressMonitor
320 progress_monitor=SetImageProgressMonitor(image,
321 (MagickProgressMonitor) NULL,image->client_data);
322 (void) TextureImage(image,texture);
323 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
325 (void) AnnotateImage(image,draw_info);
326 if (texture != (Image *) NULL)
327 texture=DestroyImage(texture);
328 draw_info=DestroyDrawInfo(draw_info);
329 (void) CloseBlob(image);
330 return(GetFirstImageInList(image));
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 % R e a d T X T I m a g e %
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % ReadTXTImage() reads a text file and returns it as an image. It allocates
345 % the memory necessary for the new Image structure and returns a pointer to
348 % The format of the ReadTXTImage method is:
350 % Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
352 % A description of each parameter follows:
354 % o image_info: the image info.
356 % o exception: return any errors or warnings in this structure.
359 static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception)
362 colorspace[MaxTextExtent],
401 assert(image_info != (const ImageInfo *) NULL);
402 assert(image_info->signature == MagickSignature);
403 if (image_info->debug != MagickFalse)
404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
405 image_info->filename);
406 assert(exception != (ExceptionInfo *) NULL);
407 assert(exception->signature == MagickSignature);
408 image=AcquireImage(image_info);
409 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
410 if (status == MagickFalse)
412 image=DestroyImageList(image);
413 return((Image *) NULL);
415 (void) ResetMagickMemory(text,0,sizeof(text));
416 (void) ReadBlobString(image,text);
417 if (LocaleNCompare((char *) text,MagickID,strlen(MagickID)) != 0)
418 return(ReadTEXTImage(image_info,image,text,exception));
420 count=(ssize_t) sscanf(text+32,"%lu,%lu,%lu,%s",&image->columns,
421 &image->rows,&max_value,colorspace);
423 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
424 for (depth=1; (GetQuantumRange(depth)+1) < max_value; depth++) ;
426 LocaleLower(colorspace);
427 i=(long) strlen(colorspace)-1;
428 image->matte=MagickFalse;
429 if ((i > 0) && (colorspace[i] == 'a'))
432 image->matte=MagickTrue;
434 type=ParseMagickOption(MagickColorspaceOptions,MagickFalse,colorspace);
436 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
437 image->colorspace=(ColorspaceType) type;
438 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
439 (void) SetImageBackgroundColor(image);
440 range=GetQuantumRange(image->depth);
441 while (ReadBlobString(image,text) != (char *) NULL)
443 if (image->colorspace == CMYKColorspace)
445 if (image->matte != MagickFalse)
446 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu,%lu",&x,&y,
447 &pixel.red,&pixel.green,&pixel.blue,&pixel.index,&pixel.opacity);
449 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu",&x,&y,
450 &pixel.red,&pixel.green,&pixel.blue,&pixel.index);
453 if (image->matte != MagickFalse)
454 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu,%lu",&x,&y,
455 &pixel.red,&pixel.green,&pixel.blue,&pixel.opacity);
457 count=(ssize_t) sscanf(text,"%ld,%ld: (%lu,%lu,%lu",&x,&y,
458 &pixel.red,&pixel.green,&pixel.blue);
461 q=GetAuthenticPixels(image,x,y,1,1,exception);
462 if (q == (PixelPacket *) NULL)
464 q->red=ScaleAnyToQuantum(pixel.red,range);
465 q->green=ScaleAnyToQuantum(pixel.green,range);
466 q->blue=ScaleAnyToQuantum(pixel.blue,range);
467 if (image->colorspace == CMYKColorspace)
469 indexes=GetAuthenticIndexQueue(image);
470 *indexes=ScaleAnyToQuantum(pixel.index,range);
472 if (image->matte != MagickFalse)
473 q->opacity=(Quantum) (QuantumRange-ScaleAnyToQuantum(pixel.opacity,
475 if (SyncAuthenticPixels(image,exception) == MagickFalse)
478 return(GetFirstImageInList(image));
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 % R e g i s t e r T X T I m a g e %
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 % RegisterTXTImage() adds attributes for the TXT image format to the
493 % list of supported formats. The attributes include the image format
494 % tag, a method to read and/or write the format, whether the format
495 % supports the saving of more than one frame to the same file or blob,
496 % whether the format supports native in-memory I/O, and a brief
497 % description of the format.
499 % The format of the RegisterTXTImage method is:
501 % unsigned long RegisterTXTImage(void)
504 ModuleExport unsigned long RegisterTXTImage(void)
509 entry=SetMagickInfo("TEXT");
510 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
511 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
512 entry->raw=MagickTrue;
513 entry->adjoin=MagickFalse;
514 entry->endian_support=MagickTrue;
515 entry->description=ConstantString("Text");
516 entry->module=ConstantString("TXT");
517 (void) RegisterMagickInfo(entry);
518 entry=SetMagickInfo("TXT");
519 entry->decoder=(DecodeImageHandler *) ReadTXTImage;
520 entry->encoder=(EncodeImageHandler *) WriteTXTImage;
521 entry->adjoin=MagickFalse;
522 entry->description=ConstantString("Text");
523 entry->magick=(IsImageFormatHandler *) IsTXT;
524 entry->module=ConstantString("TXT");
525 (void) RegisterMagickInfo(entry);
526 return(MagickImageCoderSignature);
530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % U n r e g i s t e r T X T I m a g e %
538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
540 % UnregisterTXTImage() removes format registrations made by the
541 % TXT module from the list of supported format.
543 % The format of the UnregisterTXTImage method is:
545 % UnregisterTXTImage(void)
548 ModuleExport void UnregisterTXTImage(void)
550 (void) UnregisterMagickInfo("TEXT");
551 (void) UnregisterMagickInfo("TXT");
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % W r i t e T X T I m a g e %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % WriteTXTImage writes the pixel values as text numbers.
567 % The format of the WriteTXTImage method is:
569 % MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image)
571 % A description of each parameter follows.
573 % o image_info: the image info.
575 % o image: The image.
578 static MagickBooleanType WriteTXTImage(const ImageInfo *image_info,Image *image)
581 buffer[MaxTextExtent],
582 colorspace[MaxTextExtent],
583 tuple[MaxTextExtent];
594 register const IndexPacket
597 register const PixelPacket
604 Open output image file.
606 assert(image_info != (const ImageInfo *) NULL);
607 assert(image_info->signature == MagickSignature);
608 assert(image != (Image *) NULL);
609 assert(image->signature == MagickSignature);
610 if (image->debug != MagickFalse)
611 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
612 status=OpenBlob(image_info,image,WriteBlobMode,&image->exception);
613 if (status == MagickFalse)
615 (void) CopyMagickString(colorspace,MagickOptionToMnemonic(
616 MagickColorspaceOptions,(long) image->colorspace),MaxTextExtent);
617 LocaleLower(colorspace);
618 image->depth=GetImageQuantumDepth(image,MagickTrue);
619 if (image->matte != MagickFalse)
620 (void) ConcatenateMagickString(colorspace,"a",MaxTextExtent);
621 (void) FormatMagickString(buffer,MaxTextExtent,
622 "# ImageMagick pixel enumeration: %lu,%lu,%lu,%s\n",image->columns,
623 image->rows,(unsigned long) GetQuantumRange(image->depth),colorspace);
624 (void) WriteBlobString(image,buffer);
625 GetMagickPixelPacket(image,&pixel);
626 for (y=0; y < (long) image->rows; y++)
628 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
629 if (p == (const PixelPacket *) NULL)
631 indexes=GetVirtualIndexQueue(image);
632 for (x=0; x < (long) image->columns; x++)
634 (void) FormatMagickString(buffer,MaxTextExtent,"%ld,%ld: ",x,y);
635 (void) WriteBlobString(image,buffer);
636 SetMagickPixelPacket(image,p,indexes+x,&pixel);
637 (void) CopyMagickString(tuple,"(",MaxTextExtent);
638 ConcatenateColorComponent(&pixel,RedChannel,X11Compliance,tuple);
639 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
640 ConcatenateColorComponent(&pixel,GreenChannel,X11Compliance,tuple);
641 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
642 ConcatenateColorComponent(&pixel,BlueChannel,X11Compliance,tuple);
643 if (pixel.colorspace == CMYKColorspace)
645 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
646 ConcatenateColorComponent(&pixel,IndexChannel,X11Compliance,tuple);
648 if (pixel.matte != MagickFalse)
650 (void) ConcatenateMagickString(tuple,",",MaxTextExtent);
651 ConcatenateColorComponent(&pixel,AlphaChannel,X11Compliance,tuple);
653 (void) ConcatenateMagickString(tuple,")",MaxTextExtent);
654 (void) WriteBlobString(image,tuple);
655 (void) WriteBlobString(image," ");
656 GetColorTuple(&pixel,MagickTrue,tuple);
657 (void) FormatMagickString(buffer,MaxTextExtent,"%s",tuple);
658 (void) WriteBlobString(image,buffer);
659 (void) WriteBlobString(image," ");
660 (void) QueryMagickColorname(image,&pixel,SVGCompliance,tuple,
662 (void) WriteBlobString(image,tuple);
663 (void) WriteBlobString(image,"\n");
666 status=SetImageProgress(image,SaveImageTag,y,image->rows);
667 if (status == MagickFalse)
670 (void) CloseBlob(image);