This source file includes following definitions.
- IsPCL
- ReadPCLImage
- RegisterPCLImage
- UnregisterPCLImage
- PCLDeltaCompressImage
- PCLPackbitsCompressImage
- WritePCLImage
#include "magick/studio.h"
#include "magick/artifact.h"
#include "magick/attribute.h"
#include "magick/blob.h"
#include "magick/blob-private.h"
#include "magick/cache.h"
#include "magick/color.h"
#include "magick/color-private.h"
#include "magick/colorspace.h"
#include "magick/colorspace-private.h"
#include "magick/constitute.h"
#include "magick/delegate.h"
#include "magick/draw.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/geometry.h"
#include "magick/image.h"
#include "magick/image-private.h"
#include "magick/list.h"
#include "magick/magick.h"
#include "magick/memory_.h"
#include "magick/monitor.h"
#include "magick/monitor-private.h"
#include "magick/option.h"
#include "magick/pixel-accessor.h"
#include "magick/profile.h"
#include "magick/property.h"
#include "magick/quantum-private.h"
#include "magick/resource_.h"
#include "magick/static.h"
#include "magick/string_.h"
#include "magick/module.h"
#include "magick/token.h"
#include "magick/transform.h"
#include "magick/utility.h"
static MagickBooleanType
WritePCLImage(const ImageInfo *,Image *);
static MagickBooleanType IsPCL(const unsigned char *magick,const size_t length)
{
if (length < 4)
return(MagickFalse);
if (memcmp(magick,"\033E\033&",4) == 0)
return(MagickFalse);
if (memcmp(magick,"\033E\033",3) == 0)
return(MagickTrue);
return(MagickFalse);
}
static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
#define CropBox "CropBox"
#define DeviceCMYK "DeviceCMYK"
#define MediaBox "MediaBox"
#define RenderPCLText " Rendering PCL... "
char
command[MaxTextExtent],
*density,
filename[MaxTextExtent],
geometry[MaxTextExtent],
*options,
input_filename[MaxTextExtent];
const DelegateInfo
*delegate_info;
Image
*image,
*next_image;
ImageInfo
*read_info;
int
c;
MagickBooleanType
cmyk,
status;
PointInfo
delta;
RectangleInfo
bounding_box,
page;
register char
*p;
SegmentInfo
bounds;
size_t
height,
width;
ssize_t
count;
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
if (image_info->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
image=AcquireImage(image_info);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
image=DestroyImageList(image);
return((Image *) NULL);
}
status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
if (status == MagickFalse)
{
ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
image_info->filename);
image=DestroyImageList(image);
return((Image *) NULL);
}
delta.x=DefaultResolution;
delta.y=DefaultResolution;
if ((image->x_resolution == 0.0) || (image->y_resolution == 0.0))
{
GeometryInfo
geometry_info;
MagickStatusType
flags;
flags=ParseGeometry(PSDensityGeometry,&geometry_info);
image->x_resolution=geometry_info.rho;
image->y_resolution=geometry_info.sigma;
if ((flags & SigmaValue) == 0)
image->y_resolution=image->x_resolution;
}
cmyk=image->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
count=0;
(void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box));
(void) ResetMagickMemory(&bounds,0,sizeof(bounds));
(void) ResetMagickMemory(&page,0,sizeof(page));
(void) ResetMagickMemory(command,0,sizeof(command));
p=command;
for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
{
if (image_info->page != (char *) NULL)
continue;
*p++=(char) c;
if ((c != (int) '/') && (c != '\n') &&
((size_t) (p-command) < (MaxTextExtent-1)))
continue;
*p='\0';
p=command;
if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0)
cmyk=MagickTrue;
if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0)
{
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)
{
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;
width=(size_t) floor(bounds.x2-bounds.x1+0.5);
height=(size_t) floor(bounds.y2-bounds.y1+0.5);
if (width > page.width)
page.width=width;
if (height > page.height)
page.height=height;
}
(void) CloseBlob(image);
if ((page.width == 0) || (page.height == 0))
(void) ParseAbsoluteGeometry(PSPageGeometry,&page);
if (image_info->page != (char *) NULL)
(void) ParseAbsoluteGeometry(image_info->page,&page);
(void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g",(double)
page.width,(double) page.height);
if (image_info->monochrome != MagickFalse)
delegate_info=GetDelegateInfo("pcl:mono",(char *) NULL,exception);
else
if (cmyk != MagickFalse)
delegate_info=GetDelegateInfo("pcl:cmyk",(char *) NULL,exception);
else
delegate_info=GetDelegateInfo("pcl:color",(char *) NULL,exception);
if (delegate_info == (const DelegateInfo *) NULL)
return((Image *) NULL);
if ((page.width == 0) || (page.height == 0))
(void) ParseAbsoluteGeometry(PSPageGeometry,&page);
if (image_info->page != (char *) NULL)
(void) ParseAbsoluteGeometry(image_info->page,&page);
density=AcquireString("");
options=AcquireString("");
(void) FormatLocaleString(density,MaxTextExtent,"%gx%g",
image->x_resolution,image->y_resolution);
page.width=(size_t) floor((double) page.width*image->x_resolution/delta.x+
0.5);
page.height=(size_t) floor((double) page.height*image->y_resolution/delta.y+
0.5);
(void) FormatLocaleString(options,MaxTextExtent,"-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,MaxTextExtent,"-dLastPage=%.20g",
(double) (read_info->scene+read_info->number_scenes));
else
(void) FormatLocaleString(options,MaxTextExtent,
"-dFirstPage=%.20g -dLastPage=%.20g",(double) read_info->scene+1,
(double) (read_info->scene+read_info->number_scenes));
read_info->number_scenes=0;
if (read_info->scenes != (char *) NULL)
*read_info->scenes='\0';
}
if (read_info->authenticate != (char *) NULL)
(void) FormatLocaleString(options+strlen(options),MaxTextExtent,
" -sPCLPassword=%s",read_info->authenticate);
(void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
(void) AcquireUniqueFilename(read_info->filename);
(void) FormatLocaleString(command,MaxTextExtent,
GetDelegateCommands(delegate_info),
read_info->antialias != MagickFalse ? 4 : 1,
read_info->antialias != MagickFalse ? 4 : 1,density,options,
read_info->filename,input_filename);
options=DestroyString(options);
density=DestroyString(density);
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);
read_info=DestroyImageInfo(read_info);
if (image == (Image *) NULL)
ThrowReaderException(DelegateError,"PCLDelegateFailed");
if (LocaleCompare(image->magick,"BMP") == 0)
{
Image
*cmyk_image;
cmyk_image=ConsolidateCMYKImages(image,&image->exception);
if (cmyk_image != (Image *) NULL)
{
image=DestroyImageList(image);
image=cmyk_image;
}
}
do
{
(void) CopyMagickString(image->filename,filename,MaxTextExtent);
image->page=page;
next_image=SyncNextImageInList(image);
if (next_image != (Image *) NULL)
image=next_image;
} while (next_image != (Image *) NULL);
return(GetFirstImageInList(image));
}
ModuleExport size_t RegisterPCLImage(void)
{
MagickInfo
*entry;
entry=SetMagickInfo("PCL");
entry->decoder=(DecodeImageHandler *) ReadPCLImage;
entry->encoder=(EncodeImageHandler *) WritePCLImage;
entry->magick=(IsImageFormatHandler *) IsPCL;
entry->blob_support=MagickFalse;
entry->seekable_stream=MagickTrue;
entry->thread_support=EncoderThreadSupport;
entry->description=ConstantString("Printer Control Language");
entry->module=ConstantString("PCL");
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
}
ModuleExport void UnregisterPCLImage(void)
{
(void) UnregisterMagickInfo("PCL");
}
static size_t PCLDeltaCompressImage(const size_t length,
const unsigned char *previous_pixels,const unsigned char *pixels,
unsigned char *compress_pixels)
{
int
delta,
j,
replacement;
register ssize_t
i,
x;
register unsigned char
*q;
q=compress_pixels;
for (x=0; x < (ssize_t) length; )
{
j=0;
for (i=0; x < (ssize_t) length; x++)
{
if (*pixels++ != *previous_pixels++)
{
i=1;
break;
}
j++;
}
while (x < (ssize_t) length)
{
x++;
if (*pixels == *previous_pixels)
break;
i++;
previous_pixels++;
pixels++;
}
if (i == 0)
break;
replacement=j >= 31 ? 31 : j;
j-=replacement;
delta=i >= 8 ? 8 : i;
*q++=(unsigned char) (((delta-1) << 5) | replacement);
if (replacement == 31)
{
for (replacement=255; j != 0; )
{
if (replacement > j)
replacement=j;
*q++=(unsigned char) replacement;
j-=replacement;
}
if (replacement == 255)
*q++='\0';
}
for (pixels-=i; i != 0; )
{
for (i-=delta; delta != 0; delta--)
*q++=(*pixels++);
if (i == 0)
break;
delta=(int) i;
if (i >= 8)
delta=8;
*q++=(unsigned char) ((delta-1) << 5);
}
}
return((size_t) (q-compress_pixels));
}
static size_t PCLPackbitsCompressImage(const size_t length,
const unsigned char *pixels,unsigned char *compress_pixels)
{
int
count;
register ssize_t
x;
register unsigned char
*q;
ssize_t
j;
unsigned char
packbits[128];
q=compress_pixels;
for (x=(ssize_t) length; x != 0; )
{
switch (x)
{
case 1:
{
x--;
*q++=0;
*q++=(*pixels);
break;
}
case 2:
{
x-=2;
*q++=1;
*q++=(*pixels);
*q++=pixels[1];
break;
}
case 3:
{
x-=3;
if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
{
*q++=(unsigned char) ((256-3)+1);
*q++=(*pixels);
break;
}
*q++=2;
*q++=(*pixels);
*q++=pixels[1];
*q++=pixels[2];
break;
}
default:
{
if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
{
count=3;
while (((ssize_t) count < x) && (*pixels == *(pixels+count)))
{
count++;
if (count >= 127)
break;
}
x-=count;
*q++=(unsigned char) ((256-count)+1);
*q++=(*pixels);
pixels+=count;
break;
}
count=0;
while ((*(pixels+count) != *(pixels+count+1)) ||
(*(pixels+count+1) != *(pixels+count+2)))
{
packbits[count+1]=pixels[count];
count++;
if (((ssize_t) count >= (x-3)) || (count >= 127))
break;
}
x-=count;
*packbits=(unsigned char) (count-1);
for (j=0; j <= (ssize_t) count; j++)
*q++=packbits[j];
pixels+=count;
break;
}
}
}
*q++=128;
return((size_t) (q-compress_pixels));
}
static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image)
{
char
buffer[MaxTextExtent];
CompressionType
compression;
const char
*option;
MagickBooleanType
status;
MagickOffsetType
scene;
register const IndexPacket
*indexes;
register const PixelPacket *p; register ssize_t i, x; register unsigned char *q; size_t density, length,
one,
packets;
ssize_t
y;
unsigned char
bits_per_pixel,
*compress_pixels,
*pixels,
*previous_pixels;
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickSignature);
assert(image != (Image *) NULL);
assert(image->signature == MagickSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
if (status == MagickFalse)
return(status);
density=75;
if (image_info->density != (char *) NULL)
{
GeometryInfo
geometry;
(void) ParseGeometry(image_info->density,&geometry);
density=(size_t) geometry.rho;
}
scene=0;
one=1;
do
{
(void) TransformImageColorspace(image,sRGBColorspace);
(void) WriteBlobString(image,"\033E");
(void) WriteBlobString(image,"\033*r3F");
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*r%.20gs%.20gT",
(double) image->columns,(double) image->rows);
(void) WriteBlobString(image,buffer);
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*t%.20gR",(double)
density);
(void) WriteBlobString(image,buffer);
(void) WriteBlobString(image,"\033&l0E");
if (SetImageMonochrome(image,&image->exception) != MagickFalse)
{
bits_per_pixel=1;
}
else
if (image->storage_class == DirectClass)
{
bits_per_pixel=24;
(void) WriteBlobString(image,"\033*v6W");
(void) WriteBlobByte(image,0);
(void) WriteBlobByte(image,3);
(void) WriteBlobByte(image,0);
(void) WriteBlobByte(image,8);
(void) WriteBlobByte(image,8);
(void) WriteBlobByte(image,8);
}
else
{
bits_per_pixel=8;
(void) WriteBlobString(image,"\033*v6W");
(void) WriteBlobByte(image,0);
(void) WriteBlobByte(image,1);
(void) WriteBlobByte(image,bits_per_pixel);
(void) WriteBlobByte(image,8);
(void) WriteBlobByte(image,8);
(void) WriteBlobByte(image,8);
for (i=0; i < (ssize_t) image->colors; i++)
{
(void) FormatLocaleString(buffer,MaxTextExtent,
"\033*v%da%db%dc%.20gI",
ScaleQuantumToChar(image->colormap[i].red),
ScaleQuantumToChar(image->colormap[i].green),
ScaleQuantumToChar(image->colormap[i].blue),(double) i);
(void) WriteBlobString(image,buffer);
}
for (one=1; i < (ssize_t) (one << bits_per_pixel); i++)
{
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*v%.20gI",
(double) i);
(void) WriteBlobString(image,buffer);
}
}
option=GetImageOption(image_info,"pcl:fit-to-page");
if ((option != (const char *) NULL) &&
(IsMagickTrue(option) != MagickFalse))
(void) WriteBlobString(image,"\033*r3A");
else
(void) WriteBlobString(image,"\033*r1A");
(void) WriteBlobString(image,"\033*b0Y");
length=(image->columns*bits_per_pixel+7)/8;
pixels=(unsigned char *) AcquireQuantumMemory(length+1,sizeof(*pixels));
if (pixels == (unsigned char *) NULL)
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
(void) ResetMagickMemory(pixels,0,(length+1)*sizeof(*pixels));
compress_pixels=(unsigned char *) NULL;
previous_pixels=(unsigned char *) NULL;
compression=UndefinedCompression;
if (image_info->compression != UndefinedCompression)
compression=image_info->compression;
switch (compression)
{
case NoCompression:
{
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*b0M");
(void) WriteBlobString(image,buffer);
break;
}
case RLECompression:
{
compress_pixels=(unsigned char *) AcquireQuantumMemory(length+256,
sizeof(*compress_pixels));
if (compress_pixels == (unsigned char *) NULL)
{
pixels=(unsigned char *) RelinquishMagickMemory(pixels);
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
}
(void) ResetMagickMemory(compress_pixels,0,(length+256)*
sizeof(*compress_pixels));
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*b2M");
(void) WriteBlobString(image,buffer);
break;
}
default:
{
compress_pixels=(unsigned char *) AcquireQuantumMemory(3*length+256,
sizeof(*compress_pixels));
if (compress_pixels == (unsigned char *) NULL)
{
pixels=(unsigned char *) RelinquishMagickMemory(pixels);
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
}
(void) ResetMagickMemory(compress_pixels,0,(3*length+256)*
sizeof(*compress_pixels));
previous_pixels=(unsigned char *) AcquireQuantumMemory(length+1,
sizeof(*previous_pixels));
if (previous_pixels == (unsigned char *) NULL)
{
compress_pixels=(unsigned char *) RelinquishMagickMemory(
compress_pixels);
pixels=(unsigned char *) RelinquishMagickMemory(pixels);
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
}
(void) ResetMagickMemory(previous_pixels,0,(length+1)*
sizeof(*previous_pixels));
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*b3M");
(void) WriteBlobString(image,buffer);
break;
}
}
for (y=0; y < (ssize_t) image->rows; y++)
{
p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
if (p == (const PixelPacket *) NULL)
break;
indexes=GetAuthenticIndexQueue(image);
q=pixels;
switch (bits_per_pixel)
{
case 1:
{
register unsigned char
bit,
byte;
bit=0;
byte=0;
for (x=0; x < (ssize_t) image->columns; x++)
{
byte<<=1;
if (GetPixelLuma(image,p) < (QuantumRange/2.0))
byte|=0x01;
bit++;
if (bit == 8)
{
*q++=byte;
bit=0;
byte=0;
}
p++;
}
if (bit != 0)
*q++=byte << (8-bit);
break;
}
case 8:
{
for (x=0; x < (ssize_t) image->columns; x++)
*q++=(unsigned char) GetPixelIndex(indexes+x);
break;
}
case 24:
case 32:
{
for (x=0; x < (ssize_t) image->columns; x++)
{
*q++=ScaleQuantumToChar(GetPixelRed(p));
*q++=ScaleQuantumToChar(GetPixelGreen(p));
*q++=ScaleQuantumToChar(GetPixelBlue(p));
p++;
}
break;
}
}
switch (compression)
{
case NoCompression:
{
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*b%.20gW",
(double) length);
(void) WriteBlobString(image,buffer);
(void) WriteBlob(image,length,pixels);
break;
}
case RLECompression:
{
packets=PCLPackbitsCompressImage(length,pixels,compress_pixels);
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*b%.20gW",
(double) packets);
(void) WriteBlobString(image,buffer);
(void) WriteBlob(image,packets,compress_pixels);
break;
}
default:
{
if (y == 0)
for (i=0; i < (ssize_t) length; i++)
previous_pixels[i]=(~pixels[i]);
packets=PCLDeltaCompressImage(length,previous_pixels,pixels,
compress_pixels);
(void) FormatLocaleString(buffer,MaxTextExtent,"\033*b%.20gW",
(double) packets);
(void) WriteBlobString(image,buffer);
(void) WriteBlob(image,packets,compress_pixels);
(void) CopyMagickMemory(previous_pixels,pixels,length*
sizeof(*pixels));
break;
}
}
}
(void) WriteBlobString(image,"\033*rB");
switch (compression)
{
case NoCompression:
break;
case RLECompression:
{
compress_pixels=(unsigned char *) RelinquishMagickMemory(
compress_pixels);
break;
}
default:
{
previous_pixels=(unsigned char *) RelinquishMagickMemory(
previous_pixels);
compress_pixels=(unsigned char *) RelinquishMagickMemory(
compress_pixels);
break;
}
}
pixels=(unsigned char *) RelinquishMagickMemory(pixels);
if (GetNextImageInList(image) == (Image *) NULL)
break;
image=SyncNextImageInList(image);
status=SetImageProgress(image,SaveImagesTag,scene++,
GetImageListLength(image));
if (status == MagickFalse)
break;
} while (image_info->adjoin != MagickFalse);
(void) WriteBlobString(image,"\033E");
(void) CloseBlob(image);
return(MagickTrue);
}