diff --git a/coders/ghostscript-private.h b/coders/ghostscript-private.h index 836f3f2f52..2d3f0f8f07 100644 --- a/coders/ghostscript-private.h +++ b/coders/ghostscript-private.h @@ -1,12 +1,12 @@ /* Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization dedicated to making software imaging solutions freely available. - + You may not use this file except in compliance with the License. You may obtain a copy of the License at - + https://imagemagick.org/script/license.php - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -31,22 +31,22 @@ static int MagickDLLCall GhostscriptDelegateMessage(void *handle, offset=0; messages=(char **) handle; if (*messages == (char *) NULL) - *messages=(char *) AcquireQuantumMemory(length+1,sizeof(char *)); + *messages=(char *) AcquireQuantumMemory((size_t) length+1,sizeof(char *)); else { - offset=strlen(*messages); - *messages=(char *) ResizeQuantumMemory(*messages,offset+length+1, - sizeof(char *)); + offset=(ssize_t) strlen(*messages); + *messages=(char *) ResizeQuantumMemory(*messages,(size_t) (offset+length+ + 1),sizeof(char *)); } if (*messages == (char *) NULL) return(0); - (void) memcpy(*messages+offset,message,length); + (void) memcpy(*messages+offset,message,(size_t) length); (*messages)[length+offset] ='\0'; return(length); } #endif -static MagickBooleanType InvokeGhostscriptDelegate( +static inline MagickBooleanType InvokeGhostscriptDelegate( const MagickBooleanType verbose,const char *command,char *message, ExceptionInfo *exception) { @@ -127,7 +127,7 @@ static MagickBooleanType InvokeGhostscriptDelegate( #endif if (ghost_info == (GhostInfo *) NULL) ExecuteGhostscriptCommand(command,status); - if ((ghost_info->revision)(&revision,sizeof(revision)) != 0) + if ((ghost_info->revision)(&revision,(int) sizeof(revision)) != 0) revision.revision=0; if (verbose != MagickFalse) { @@ -206,7 +206,7 @@ static MagickBooleanType IsGhostscriptRendered(const char *path) return(MagickFalse); } -static void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer, +static inline void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer, StringInfo **profile) { #define BeginXMPPacket "?xpacket begin=" diff --git a/coders/xps.c b/coders/xps.c index 0fdfbd2dbc..a590b965a6 100644 --- a/coders/xps.c +++ b/coders/xps.c @@ -14,7 +14,7 @@ % % % Software Design % % Cristy % -% January 2008 % +% January 2008 % % % % % % Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization % @@ -41,13 +41,17 @@ */ #include "MagickCore/studio.h" #include "MagickCore/artifact.h" +#include "MagickCore/attribute.h" #include "MagickCore/blob.h" #include "MagickCore/blob-private.h" +#include "MagickCore/cache.h" #include "MagickCore/color.h" #include "MagickCore/color-private.h" #include "MagickCore/colorspace.h" +#include "MagickCore/colorspace-private.h" #include "MagickCore/constitute.h" #include "MagickCore/delegate.h" +#include "MagickCore/delegate-private.h" #include "MagickCore/draw.h" #include "MagickCore/exception.h" #include "MagickCore/exception-private.h" @@ -57,19 +61,46 @@ #include "MagickCore/list.h" #include "MagickCore/magick.h" #include "MagickCore/memory_.h" +#include "MagickCore/module.h" #include "MagickCore/monitor.h" #include "MagickCore/monitor-private.h" +#include "MagickCore/nt-base-private.h" #include "MagickCore/option.h" #include "MagickCore/profile.h" -#include "MagickCore/property.h" #include "MagickCore/resource_.h" +#include "MagickCore/pixel-accessor.h" +#include "MagickCore/property.h" #include "MagickCore/quantum-private.h" #include "MagickCore/static.h" #include "MagickCore/string_.h" -#include "MagickCore/module.h" +#include "MagickCore/string-private.h" +#include "MagickCore/timer-private.h" #include "MagickCore/token.h" #include "MagickCore/transform.h" #include "MagickCore/utility.h" +#include "coders/bytebuffer-private.h" +#include "coders/ghostscript-private.h" + +/* + Typedef declaractions. +*/ +typedef struct _XPSInfo +{ + MagickBooleanType + cmyk; + + SegmentInfo + bounds; + + unsigned long + columns, + rows; + + StringInfo + *icc_profile, + *photoshop_profile, + *xmp_profile; +} XPSInfo; /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -86,9 +117,9 @@ % It allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % -% The format of the ReadXPSImage method is: +% The format of the ReadPSImage method is: % -% Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception) +% Image *ReadPSImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % @@ -97,58 +128,87 @@ % o exception: return any errors or warnings in this structure. % */ + +static inline int ProfileInteger(MagickByteBuffer *buffer,short int *hex_digits) +{ + int + c, + l, + value; + + register ssize_t + i; + + l=0; + value=0; + for (i=0; i < 2; ) + { + c=ReadMagickByteBuffer(buffer); + if ((c == EOF) || ((c == '%') && (l == '%'))) + { + value=(-1); + break; + } + l=c; + c&=0xff; + if (isxdigit(c) == MagickFalse) + continue; + value=(int) ((size_t) value << 4)+hex_digits[c]; + i++; + } + return(value); +} + static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception) { -#define CropBox "CropBox" -#define DeviceCMYK "DeviceCMYK" -#define MediaBox "MediaBox" -#define RenderXPSText " Rendering XPS... " - char command[MagickPathExtent], *density, filename[MagickPathExtent], - geometry[MagickPathExtent], - *options, - input_filename[MagickPathExtent]; + input_filename[MagickPathExtent], + message[MagickPathExtent], + *options; + + const char + *option; const DelegateInfo *delegate_info; + GeometryInfo + geometry_info; + Image *image, - *next_image; + *next, + *postscript_image; ImageInfo *read_info; MagickBooleanType - cmyk, + fitPage, status; + MagickStatusType + flags; + PointInfo - delta; + delta, + resolution; RectangleInfo - bounding_box, page; - register char - *p; - register ssize_t - c; + i; - SegmentInfo - bounds; - - size_t - height, - width; - - ssize_t - count; + unsigned long + scene; + /* + Open image file. + */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickCoreSignature); if (image_info->debug != MagickFalse) @@ -156,9 +216,6 @@ static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception) image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickCoreSignature); - /* - Open image file. - */ image=AcquireImage(image_info,exception); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) @@ -181,132 +238,97 @@ static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception) delta.y=DefaultResolution; if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0)) { - GeometryInfo - geometry_info; - - MagickStatusType - flags; - flags=ParseGeometry(PSDensityGeometry,&geometry_info); image->resolution.x=geometry_info.rho; image->resolution.y=geometry_info.sigma; if ((flags & SigmaValue) == 0) image->resolution.y=image->resolution.x; } - /* - Determine page geometry from the XPS media box. - */ - cmyk=image->colorspace == CMYKColorspace ? MagickTrue : MagickFalse; - count=0; - (void) memset(&bounding_box,0,sizeof(bounding_box)); - (void) memset(&bounds,0,sizeof(bounds)); - (void) memset(&page,0,sizeof(page)); - (void) memset(command,0,sizeof(command)); - p=command; - for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image)) - { - if (image_info->page != (char *) NULL) - continue; - /* - Note XPS elements. - */ - *p++=(char) c; - if ((c != (int) '/') && (c != '\n') && - ((size_t) (p-command) < (MagickPathExtent-1))) - continue; - *p='\0'; - p=command; - /* - Is this a CMYK document? - */ - if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0) - cmyk=MagickTrue; - if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0) - { - /* - Note region defined by crop box. - */ - count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf", - &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); - if (count != 4) - count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf", - &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); - } - if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0) - { - /* - Note region defined by media box. - */ - count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf", - &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); - if (count != 4) - count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf", - &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); - } - if (count != 4) - continue; - /* - Set XPS render geometry. - */ - width=(size_t) (floor(bounds.x2+0.5)-ceil(bounds.x1-0.5)); - height=(size_t) (floor(bounds.y2+0.5)-ceil(bounds.y1-0.5)); - if (width > page.width) - page.width=width; - if (height > page.height) - page.height=height; - } - (void) CloseBlob(image); - /* - Render XPS with the GhostXPS delegate. - */ - if ((page.width == 0) || (page.height == 0)) - (void) ParseAbsoluteGeometry(PSPageGeometry,&page); + if (image_info->density != (char *) NULL) + { + flags=ParseGeometry(image_info->density,&geometry_info); + image->resolution.x=geometry_info.rho; + image->resolution.y=geometry_info.sigma; + if ((flags & SigmaValue) == 0) + image->resolution.y=image->resolution.x; + } + (void) ParseAbsoluteGeometry(PSPageGeometry,&page); if (image_info->page != (char *) NULL) (void) ParseAbsoluteGeometry(image_info->page,&page); - (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g",(double) - page.width,(double) page.height); - if (image_info->monochrome != MagickFalse) - delegate_info=GetDelegateInfo("xps:mono",(char *) NULL,exception); - else - if (cmyk != MagickFalse) - delegate_info=GetDelegateInfo("xps:cmyk",(char *) NULL,exception); - else - delegate_info=GetDelegateInfo("xps:color",(char *) NULL,exception); + resolution=image->resolution; + page.width=(size_t) ((ssize_t) ceil((double) (page.width*resolution.x/ + delta.x)-0.5)); + page.height=(size_t) ((ssize_t) ceil((double) (page.height*resolution.y/ + delta.y)-0.5)); + fitPage=MagickFalse; + option=GetImageOption(image_info,"xps:fit-page"); + if (option != (char *) NULL) + { + char + *page_geometry; + + page_geometry=GetPageGeometry(option); + flags=ParseMetaGeometry(page_geometry,&page.x,&page.y,&page.width, + &page.height); + if (flags == NoValue) + { + (void) ThrowMagickException(exception,GetMagickModule(),OptionError, + "InvalidGeometry","`%s'",option); + page_geometry=DestroyString(page_geometry); + image=DestroyImage(image); + return((Image *) NULL); + } + page.width=(size_t) ((ssize_t) ceil((double) (page.width* + image->resolution.x/delta.x)-0.5)); + page.height=(size_t) ((ssize_t) ceil((double) (page.height* + image->resolution.y/delta.y) -0.5)); + page_geometry=DestroyString(page_geometry); + fitPage=MagickTrue; + } + /* + Render Postscript with the Ghostscript delegate. + */ + delegate_info=GetDelegateInfo("xps:color",(char *) NULL,exception); if (delegate_info == (const DelegateInfo *) NULL) { - image=DestroyImage(image); + image=DestroyImageList(image); return((Image *) NULL); } density=AcquireString(""); options=AcquireString(""); - (void) FormatLocaleString(density,MagickPathExtent,"%gx%g", - image->resolution.x,image->resolution.y); - if ((page.width == 0) || (page.height == 0)) - (void) ParseAbsoluteGeometry(PSPageGeometry,&page); - if (image_info->page != (char *) NULL) - (void) ParseAbsoluteGeometry(image_info->page,&page); - page.width=(size_t) floor(page.width*image->resolution.y/delta.x+0.5); - page.height=(size_t) floor(page.height*image->resolution.y/delta.y+0.5); + (void) FormatLocaleString(density,MagickPathExtent,"%gx%g",resolution.x, + resolution.y); (void) FormatLocaleString(options,MagickPathExtent,"-g%.20gx%.20g ",(double) page.width,(double) page.height); - image=DestroyImage(image); read_info=CloneImageInfo(image_info); *read_info->magick='\0'; if (read_info->number_scenes != 0) { - if (read_info->number_scenes != 1) - (void) FormatLocaleString(options,MagickPathExtent,"-dLastPage=%.20g", - (double) (read_info->scene+read_info->number_scenes)); - else - (void) FormatLocaleString(options,MagickPathExtent, - "-dFirstPage=%.20g -dLastPage=%.20g",(double) read_info->scene+1, - (double) (read_info->scene+read_info->number_scenes)); + char + pages[MagickPathExtent]; + + (void) FormatLocaleString(pages,MagickPathExtent,"-dFirstPage=%.20g " + "-dLastPage=%.20g ",(double) read_info->scene+1,(double) + (read_info->scene+read_info->number_scenes)); + (void) ConcatenateMagickString(options,pages,MagickPathExtent); read_info->number_scenes=0; if (read_info->scenes != (char *) NULL) *read_info->scenes='\0'; } - (void) CopyMagickString(filename,read_info->filename,MagickPathExtent); + if (*image_info->magick == 'E') + { + option=GetImageOption(image_info,"xps:use-cropbox"); + if ((option == (const char *) NULL) || + (IsStringTrue(option) != MagickFalse)) + (void) ConcatenateMagickString(options,"-dEPSCrop ",MagickPathExtent); + if (fitPage != MagickFalse) + (void) ConcatenateMagickString(options,"-dEPSFitPage ", + MagickPathExtent); + } (void) AcquireUniqueFilename(read_info->filename); + (void) RelinquishUniqueFileResource(read_info->filename); + (void) ConcatenateMagickString(read_info->filename,"%d",MagickPathExtent); + (void) CopyMagickString(filename,read_info->filename,MagickPathExtent); (void) FormatLocaleString(command,MagickPathExtent, GetDelegateCommands(delegate_info), read_info->antialias != MagickFalse ? 4 : 1, @@ -314,35 +336,99 @@ static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception) read_info->filename,input_filename); options=DestroyString(options); density=DestroyString(density); + *message='\0'; status=ExternalDelegateCommand(MagickFalse,read_info->verbose,command, (char *) NULL,exception) != 0 ? MagickTrue : MagickFalse; - image=ReadImage(read_info,exception); - (void) RelinquishUniqueFileResource(read_info->filename); (void) RelinquishUniqueFileResource(input_filename); + postscript_image=(Image *) NULL; + if (status == MagickFalse) + for (i=1; ; i++) + { + (void) InterpretImageFilename(image_info,image,filename,(int) i, + read_info->filename,exception); + if (IsGhostscriptRendered(read_info->filename) == MagickFalse) + break; + read_info->blob=NULL; + read_info->length=0; + next=ReadImage(read_info,exception); + (void) RelinquishUniqueFileResource(read_info->filename); + if (next == (Image *) NULL) + break; + AppendImageToList(&postscript_image,next); + } + else + for (i=1; ; i++) + { + (void) InterpretImageFilename(image_info,image,filename,(int) i, + read_info->filename,exception); + if (IsGhostscriptRendered(read_info->filename) == MagickFalse) + break; + read_info->blob=NULL; + read_info->length=0; + next=ReadImage(read_info,exception); + (void) RelinquishUniqueFileResource(read_info->filename); + if (next == (Image *) NULL) + break; + AppendImageToList(&postscript_image,next); + } + (void) RelinquishUniqueFileResource(filename); read_info=DestroyImageInfo(read_info); - if (image == (Image *) NULL) - ThrowReaderException(DelegateError,"XPSDelegateFailed"); - if (LocaleCompare(image->magick,"BMP") == 0) + if (postscript_image == (Image *) NULL) + { + if (*message != '\0') + (void) ThrowMagickException(exception,GetMagickModule(), + DelegateError,"PostscriptDelegateFailed","`%s'",message); + image=DestroyImageList(image); + return((Image *) NULL); + } + if (LocaleCompare(postscript_image->magick,"BMP") == 0) { Image *cmyk_image; - cmyk_image=ConsolidateCMYKImages(image,exception); + cmyk_image=ConsolidateCMYKImages(postscript_image,exception); if (cmyk_image != (Image *) NULL) { - image=DestroyImageList(image); - image=cmyk_image; + postscript_image=DestroyImageList(postscript_image); + postscript_image=cmyk_image; } } + if (image_info->number_scenes != 0) + { + Image + *clone_image; + + /* + Add place holder images to meet the subimage specification requirement. + */ + for (i=0; i < (ssize_t) image_info->scene; i++) + { + clone_image=CloneImage(postscript_image,1,1,MagickTrue,exception); + if (clone_image != (Image *) NULL) + PrependImageToList(&postscript_image,clone_image); + } + } do { - (void) CopyMagickString(image->filename,filename,MagickPathExtent); - image->page=page; - next_image=SyncNextImageInList(image); - if (next_image != (Image *) NULL) - image=next_image; - } while (next_image != (Image *) NULL); - return(GetFirstImageInList(image)); + (void) CopyMagickString(postscript_image->filename,filename, + MagickPathExtent); + (void) CopyMagickString(postscript_image->magick,image->magick, + MagickPathExtent); + postscript_image->page=page; + (void) CloneImageProfiles(postscript_image,image); + (void) CloneImageProperties(postscript_image,image); + next=SyncNextImageInList(postscript_image); + if (next != (Image *) NULL) + postscript_image=next; + } while (next != (Image *) NULL); + image=DestroyImageList(image); + scene=0; + for (next=GetFirstImageInList(postscript_image); next != (Image *) NULL; ) + { + next->scene=scene++; + next=GetNextImageInList(next); + } + return(GetFirstImageInList(postscript_image)); } /* @@ -356,9 +442,9 @@ static Image *ReadXPSImage(const ImageInfo *image_info,ExceptionInfo *exception) % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% RegisterXPSImage() adds attributes for the Microsoft XML Paper Specification -% format to the list of supported formats. The attributes include the image -% format tag, a method to read and/or write the format, whether the format +% RegisterXPSImage() adds properties for the PS image format to +% the list of supported formats. The properties include the image format +% tag, a method to read and/or write the format, whether the format % supports the saving of more than one frame to the same file or blob, % whether the format supports native in-memory I/O, and a brief % description of the format. @@ -375,9 +461,10 @@ ModuleExport size_t RegisterXPSImage(void) entry=AcquireMagickInfo("XPS","XPS","Microsoft XML Paper Specification"); entry->decoder=(DecodeImageHandler *) ReadXPSImage; + entry->flags|=CoderDecoderSeekableStreamFlag; entry->flags^=CoderAdjoinFlag; entry->flags^=CoderBlobSupportFlag; - entry->flags^=CoderDecoderThreadSupportFlag; + entry->mime_type=ConstantString("application/oxps"); (void) RegisterMagickInfo(entry); return(MagickImageCoderSignature); } @@ -393,8 +480,8 @@ ModuleExport size_t RegisterXPSImage(void) % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% UnregisterXPSImage() removes format registrations made by the XPS module -% from the list of supported formats. +% UnregisterXPSImage() removes format registrations made by the +% XPS module from the list of supported formats. % % The format of the UnregisterXPSImage method is: %