% July 1992 %
% %
% %
-% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2012 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 %
/*
Include declarations.
*/
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/cache.h"
-#include "magick/colorspace.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/quantum-private.h"
-#include "magick/resource_.h"
-#include "magick/static.h"
-#include "magick/string_.h"
-#include "magick/module.h"
-#if defined(MAGICKCORE_TIFF_DELEGATE)
-#if defined(MAGICKCORE_HAVE_TIFFCONF_H)
-#include "tiffconf.h"
-#endif
-#include "tiffio.h"
-#define CCITTParam "-1"
-#else
-#define CCITTParam "0"
-#endif
+#include "MagickCore/studio.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/cache.h"
+#include "MagickCore/colorspace.h"
+#include "MagickCore/constitute.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/geometry.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
+#include "MagickCore/option.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/resource_.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/module.h"
\f
+#if defined(MAGICKCORE_TIFF_DELEGATE)
/*
Forward declarations.
*/
static MagickBooleanType
- WriteCALSImage(const ImageInfo *,Image *);
+ WriteCALSImage(const ImageInfo *,Image *,ExceptionInfo *);
+#endif
\f
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% o exception: return any errors or warnings in this structure.
%
*/
-
-static inline size_t WriteCALSLSBLong(FILE *file,const unsigned int value)
-{
- unsigned char
- buffer[4];
-
- buffer[0]=(unsigned char) value;
- buffer[1]=(unsigned char) (value >> 8);
- buffer[2]=(unsigned char) (value >> 16);
- buffer[3]=(unsigned char) (value >> 24);
- return(fwrite(buffer,1,4,file));
-}
-
-static Image *Huffman2DDecodeImage(const ImageInfo *image_info,Image *image,
- const unsigned long density,const unsigned long orientation,
+static Image *ReadCALSImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
char
- filename[MaxTextExtent];
+ filename[MaxTextExtent],
+ header[129],
+ message[MaxTextExtent];
FILE
*file;
Image
- *huffman_image;
+ *image;
ImageInfo
*read_info;
c,
unique_file;
- size_t
- length;
-
- ssize_t
- offset,
- strip_offset;
-
- /*
- Write CALS facsimile document wrapped as a TIFF image file.
- */
- file=(FILE *) NULL;
- unique_file=AcquireUniqueFileResource(filename);
- if (unique_file != -1)
- file=fdopen(unique_file,"wb");
- if ((unique_file == -1) || (file == (FILE *) NULL))
- ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
- length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
- length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
- length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
- length=WriteCALSLSBLong(file,image->columns);
- length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
- length=WriteCALSLSBLong(file,image->rows);
- length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
- length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
- length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
- length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
- strip_offset=10+(12*14)+4+8;
- length=WriteCALSLSBLong(file,(unsigned int) strip_offset);
- length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
- length=WriteCALSLSBLong(file,orientation);
- length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
- length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
- length=WriteCALSLSBLong(file,image->columns);
- length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
- offset=(ssize_t) ftell(file)-4;
- length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
- length=WriteCALSLSBLong(file,(unsigned int) (strip_offset-8));
- length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
- length=WriteCALSLSBLong(file,(unsigned int) (strip_offset-8));
- length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
- length=fwrite("\000\000\000\000",1,4,file);
- length=WriteCALSLSBLong(file,density);
- length=WriteCALSLSBLong(file,1);
- for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
- (void) fputc(c,file);
- (void) CloseBlob(image);
- offset=(ssize_t) fseek(file,(long) offset,SEEK_SET);
- length=WriteCALSLSBLong(file,(unsigned int) length);
- (void) fclose(file);
- /*
- Read TIFF image.
- */
- read_info=CloneImageInfo(image_info);
- SetImageInfoBlob(read_info,(void *) NULL,0);
- (void) FormatMagickString(read_info->filename,MaxTextExtent,"tiff:%.1024s",
- filename);
- huffman_image=ReadImage(read_info,exception);
- read_info=DestroyImageInfo(read_info);
- (void) RelinquishUniqueFileResource(filename);
- return(huffman_image);
-}
-
-static Image *ReadCALSImage(const ImageInfo *image_info,
- ExceptionInfo *exception)
-{
- char
- header[129];
-
- Image
- *huffman_image,
- *image;
-
MagickBooleanType
status;
- register long
+ register ssize_t
i;
unsigned long
image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
- image=AcquireImage(image_info);
+ image=AcquireImage(image_info,exception);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
if (pel_path == 90)
orientation=5;
else
- if (pel_path == 90)
+ if (pel_path == 180)
orientation=3;
else
if (pel_path == 270)
}
}
}
- if ((width == 0) || (height == 0) || (type == 0))
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
- image->columns=width;
- image->rows=height;
- huffman_image=Huffman2DDecodeImage(image_info,image,density,orientation,
- exception);
+ /*
+ Read CALS pixels.
+ */
+ file=(FILE *) NULL;
+ unique_file=AcquireUniqueFileResource(filename);
+ if (unique_file != -1)
+ file=fdopen(unique_file,"wb");
+ if ((unique_file == -1) || (file == (FILE *) NULL))
+ ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
+ while ((c=ReadBlobByte(image)) != EOF)
+ (void) fputc(c,file);
+ (void) fclose(file);
+ (void) CloseBlob(image);
image=DestroyImage(image);
- (void) CloseBlob(huffman_image);
- if (huffman_image == (Image *) NULL)
- return(huffman_image);
- return(GetFirstImageInList(huffman_image));
+ read_info=CloneImageInfo(image_info);
+ SetImageInfoBlob(read_info,(void *) NULL,0);
+ (void) FormatLocaleString(read_info->filename,MaxTextExtent,"group4:%s",
+ filename);
+ (void) FormatLocaleString(message,MaxTextExtent,"%lux%lu",width,height);
+ (void) CloneString(&read_info->size,message);
+ (void) FormatLocaleString(message,MaxTextExtent,"%lu",density);
+ (void) CloneString(&read_info->density,message);
+ read_info->orientation=(OrientationType) orientation;
+ image=ReadImage(read_info,exception);
+ if (image != (Image *) NULL)
+ {
+ (void) CopyMagickString(image->filename,image_info->filename,
+ MaxTextExtent);
+ (void) CopyMagickString(image->magick_filename,image_info->filename,
+ MaxTextExtent);
+ (void) CopyMagickString(image->magick,"CALS",MaxTextExtent);
+ }
+ read_info=DestroyImageInfo(read_info);
+ (void) RelinquishUniqueFileResource(filename);
+ return(image);
}
\f
/*
%
% The format of the RegisterCALSImage method is:
%
-% unsigned long RegisterCALSImage(void)
+% size_t RegisterCALSImage(void)
%
*/
-ModuleExport unsigned long RegisterCALSImage(void)
+ModuleExport size_t RegisterCALSImage(void)
{
MagickInfo
*entry;
+ static const char
+ *CALSDescription=
+ {
+ "Continuous Acquisition and Life-cycle Support Type 1"
+ },
+ *CALSNote=
+ {
+ "Specified in MIL-R-28002 and MIL-PRF-28002"
+ };
+
+ entry=SetMagickInfo("CAL");
+ entry->decoder=(DecodeImageHandler *) ReadCALSImage;
+#if defined(MAGICKCORE_TIFF_DELEGATE)
+ entry->encoder=(EncodeImageHandler *) WriteCALSImage;
+#endif
+ entry->adjoin=MagickFalse;
+ entry->magick=(IsImageFormatHandler *) IsCALS;
+ entry->description=ConstantString(CALSDescription);
+ entry->note=ConstantString(CALSNote);
+ entry->module=ConstantString("CALS");
+ (void) RegisterMagickInfo(entry);
entry=SetMagickInfo("CALS");
entry->decoder=(DecodeImageHandler *) ReadCALSImage;
+#if defined(MAGICKCORE_TIFF_DELEGATE)
entry->encoder=(EncodeImageHandler *) WriteCALSImage;
+#endif
entry->adjoin=MagickFalse;
entry->magick=(IsImageFormatHandler *) IsCALS;
- entry->description=ConstantString("Automated Interchange of Technical "
- "Information, MIL-STD-1840A");
+ entry->description=ConstantString(CALSDescription);
+ entry->note=ConstantString(CALSNote);
entry->module=ConstantString("CALS");
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
*/
ModuleExport void UnregisterCALSImage(void)
{
+ (void) UnregisterMagickInfo("CAL");
(void) UnregisterMagickInfo("CALS");
}
\f
+#if defined(MAGICKCORE_TIFF_DELEGATE)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% The format of the WriteCALSImage method is:
%
% MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
-% Image *image)
+% Image *image,ExceptionInfo *exception)
%
% A description of each parameter follows.
%
%
% o image: The image.
%
+% o exception: return any errors or warnings in this structure.
+%
*/
-static MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
- Image *image,Image *inject_image)
-{
- char
- filename[MaxTextExtent];
-
- FILE
- *file;
-
- Image
- *huffman_image;
-
- ImageInfo
- *write_info;
-
- int
- unique_file;
-
- MagickBooleanType
- status;
-
- register long
- i;
-
- ssize_t
- count;
-
- TIFF
- *tiff;
-
- uint16
- fillorder;
-
- uint32
- *byte_count,
- strip_size;
-
- unsigned char
- *buffer;
-
- /*
- Write image as CCITTFax4 TIFF image to a temporary file.
- */
- 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);
- assert(inject_image != (Image *) NULL);
- assert(inject_image->signature == MagickSignature);
- huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
- if (huffman_image == (Image *) NULL)
- return(MagickFalse);
- file=(FILE *) NULL;
- unique_file=AcquireUniqueFileResource(filename);
- if (unique_file != -1)
- file=fdopen(unique_file,"wb");
- if ((unique_file == -1) || (file == (FILE *) NULL))
- {
- ThrowFileException(&image->exception,FileOpenError,
- "UnableToCreateTemporaryFile",filename);
- return(MagickFalse);
- }
- (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
- filename);
- write_info=CloneImageInfo(image_info);
- SetImageInfoFile(write_info,file);
- write_info->compression=Group4Compression;
- (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
- status=WriteImage(write_info,huffman_image);
- (void) fflush(file);
- write_info=DestroyImageInfo(write_info);
- if (status == MagickFalse)
- return(MagickFalse);
- tiff=TIFFOpen(filename,"rb");
- if (tiff == (TIFF *) NULL)
- {
- huffman_image=DestroyImage(huffman_image);
- (void) fclose(file);
- (void) RelinquishUniqueFileResource(filename);
- ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
- image_info->filename);
- return(MagickFalse);
- }
- /*
- Allocate raw strip buffer.
- */
- byte_count=0;
- (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count);
- strip_size=byte_count[0];
- for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
- if (byte_count[i] > strip_size)
- strip_size=byte_count[i];
- buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
- sizeof(*buffer));
- if (buffer == (unsigned char *) NULL)
- {
- TIFFClose(tiff);
- huffman_image=DestroyImage(huffman_image);
- (void) fclose(file);
- (void) RelinquishUniqueFileResource(filename);
- ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
- image_info->filename);
- }
- /*
- Compress runlength encoded to 2D Huffman pixels.
- */
- fillorder=FILLORDER_LSB2MSB;
- (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder);
- for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
- {
- count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long)
- byte_count[i]);
- if (fillorder == FILLORDER_LSB2MSB)
- TIFFReverseBits(buffer,(unsigned long) count);
- (void) WriteBlob(image,(size_t) count,buffer);
- }
- buffer=(unsigned char *) RelinquishMagickMemory(buffer);
- TIFFClose(tiff);
- huffman_image=DestroyImage(huffman_image);
- (void) fclose(file);
- (void) RelinquishUniqueFileResource(filename);
- return(MagickTrue);
-}
-
static ssize_t WriteCALSRecord(Image *image,const char *data)
{
char
pad[128];
- ssize_t
- count;
-
register const char
*p;
- register long
+ register ssize_t
i;
+ ssize_t
+ count;
+
i=0;
+ count=0;
if (data != (const char *) NULL)
{
p=data;
if (i < 128)
{
i=128-i;
- (void) ResetMagickMemory(pad,' ',(const size_t) i);
+ (void) ResetMagickMemory(pad,' ',(size_t) i);
count=WriteBlob(image,(size_t) i,(const unsigned char *) pad);
}
return(count);
}
static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
- Image *image)
+ Image *image,ExceptionInfo *exception)
{
char
- buffer[129];
+ header[129];
+
+ Image
+ *group4_image;
+
+ ImageInfo
+ *write_info;
MagickBooleanType
status;
- register long
+ register ssize_t
i;
- ssize_t
- count;
-
- unsigned long
+ size_t
density,
+ length,
orient_x,
orient_y;
+ ssize_t
+ count;
+
+ unsigned char
+ *group4;
+
/*
Open output image file.
*/
assert(image->signature == MagickSignature);
if (image->debug != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+ assert(exception != (ExceptionInfo *) NULL);
+ assert(exception->signature == MagickSignature);
+ status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
if (status == MagickFalse)
return(status);
/*
Create standard CALS header.
*/
count=WriteCALSRecord(image,"srcdocid: NONE");
+ (void) count;
count=WriteCALSRecord(image,"dstdocid: NONE");
count=WriteCALSRecord(image,"txtfilid: NONE");
count=WriteCALSRecord(image,"figid: NONE");
count=WriteCALSRecord(image,"srcgph: NONE");
count=WriteCALSRecord(image,"docls: NONE");
count=WriteCALSRecord(image,"rtype: 1");
+ orient_x=0;
+ orient_y=0;
switch (image->orientation)
{
case TopRightOrientation:
break;
}
case BottomLeftOrientation:
- orient_x=0;
+ {
orient_y=90;
break;
+ }
case LeftTopOrientation:
{
orient_x=270;
- orient_y=0;
break;
}
case RightTopOrientation:
case LeftBottomOrientation:
{
orient_x=90;
- orient_y=0;
break;
}
default:
{
- orient_x=0;
orient_y=270;
}
}
- (void) FormatMagickString(buffer,MaxTextExtent,"rorient: %03ld,%03ld",
- orient_x,orient_y);
- count=WriteCALSRecord(image,buffer);
- (void) FormatMagickString(buffer,MaxTextExtent,"rpelcnt: %06lu,%06lu",
- image->columns,image->rows);
- count=WriteCALSRecord(image,buffer);
+ (void) FormatLocaleString(header,MaxTextExtent,"rorient: %03ld,%03ld",
+ (long) orient_x,(long) orient_y);
+ count=WriteCALSRecord(image,header);
+ (void) FormatLocaleString(header,MaxTextExtent,"rpelcnt: %06lu,%06lu",
+ (unsigned long) image->columns,(unsigned long) image->rows);
+ count=WriteCALSRecord(image,header);
density=200;
if (image_info->density != (char *) NULL)
{
geometry_info;
(void) ParseGeometry(image_info->density,&geometry_info);
- density=(unsigned long) (geometry_info.rho+0.5);
+ density=(size_t) floor(geometry_info.rho+0.5);
}
- (void) FormatMagickString(buffer,MaxTextExtent,"rdensty: %04lu",density);
- count=WriteCALSRecord(image,buffer);
+ (void) FormatLocaleString(header,MaxTextExtent,"rdensty: %04lu",
+ (unsigned long) density);
+ count=WriteCALSRecord(image,header);
count=WriteCALSRecord(image,"notes: NONE");
- (void) ResetMagickMemory(buffer,' ',128);
+ (void) ResetMagickMemory(header,' ',128);
for (i=0; i < 5; i++)
- (void) WriteBlob(image,128,(unsigned char *) buffer);
- status=Huffman2DEncodeImage(image_info,image,image);
+ (void) WriteBlob(image,128,(unsigned char *) header);
+ /*
+ Write CALS pixels.
+ */
+ write_info=CloneImageInfo(image_info);
+ (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
+ (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
+ group4_image=CloneImage(image,0,0,MagickTrue,exception);
+ if (group4_image == (Image *) NULL)
+ {
+ (void) CloseBlob(image);
+ return(MagickFalse);
+ }
+ group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
+ exception);
+ group4_image=DestroyImage(group4_image);
+ if (group4 == (unsigned char *) NULL)
+ {
+ (void) CloseBlob(image);
+ return(MagickFalse);
+ }
+ write_info=DestroyImageInfo(write_info);
+ if (WriteBlob(image,length,group4) != (ssize_t) length)
+ status=MagickFalse;
+ group4=(unsigned char *) RelinquishMagickMemory(group4);
(void) CloseBlob(image);
return(status);
}
+#endif