]> granicus.if.org Git - imagemagick/blobdiff - coders/cals.c
(no commit message)
[imagemagick] / coders / cals.c
index c18de5f4738e8309de41abd6b12989aff9f03dac..a89a804e73b018e0880e58fead1635ed1ea654c8 100644 (file)
 %                         CCCC  A   A  LLLLL  SSSSS                           %
 %                                                                             %
 %                                                                             %
-%                        Read/Write CALS Image Format                         %
+%                 Read/Write CALS Raster Group 1 Image Format                 %
 %                                                                             %
 %                              Software Design                                %
 %                                John Cristy                                  %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2010 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  %
@@ -51,6 +51,7 @@
 #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/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"
 \f
+/*
+ Forward declarations.
+*/
+static MagickBooleanType
+  WriteCALSImage(const ImageInfo *,Image *);
+\f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
@@ -76,7 +84,7 @@
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 %  IsCALS() returns MagickTrue if the image format type, identified by the
-%  magick string, is CALS.
+%  magick string, is CALS Raster Group 1.
 %
 %  The format of the IsCALS method is:
 %
@@ -93,11 +101,11 @@ static MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
 {
   if (length < 128)
     return(MagickFalse);
-  if (LocaleNCompare((char *) magick,"version: MIL-STD-1840",21) == 0)
+  if (LocaleNCompare((const char *) magick,"version: MIL-STD-1840",21) == 0)
     return(MagickTrue);
-  if (LocaleNCompare((char *) magick,"srcdocid:",9) == 0)
+  if (LocaleNCompare((const char *) magick,"srcdocid:",9) == 0)
     return(MagickTrue);
-  if (LocaleNCompare((char *) magick,"rorient:",8) == 0)
+  if (LocaleNCompare((const char *) magick,"rorient:",8) == 0)
     return(MagickTrue);
   return(MagickFalse);
 }
@@ -113,9 +121,9 @@ static MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  ReadCALSImage() reads an Automated Interchange of Technical Information,
-%  MIL-STD-1840A image file and returns it.  It allocates the memory necessary
-%  for the new Image structure and returns a pointer to the new image.
+%  ReadCALSImage() reads an CALS Raster Group 1 image format image file and
+%  returns it.  It allocates the memory necessary for the new Image structure
+%  and returns a pointer to the new image.
 %
 %  The format of the ReadCALSImage method is:
 %
@@ -129,25 +137,13 @@ static MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
 %    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 *ReadCALSImage(const ImageInfo *image_info,
   ExceptionInfo *exception)
 {
   char
     filename[MaxTextExtent],
-    header[129];
+    header[129],
+    message[MaxTextExtent];
 
   FILE
     *file;
@@ -168,14 +164,7 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
   register long
     i;
 
-  size_t
-    length;
-
-  ssize_t
-    offset,
-    strip_offset;
-
-  unsigned int
+  unsigned long
     density,
     direction,
     height,
@@ -223,17 +212,17 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
       {
         if (LocaleNCompare(header,"rdensty:",8) == 0)
           {
-            (void) sscanf(header+8,"%u",&density);
+            (void) sscanf(header+8,"%lu",&density);
             break;
           }
         if (LocaleNCompare(header,"rpelcnt:",8) == 0)
           {
-            (void) sscanf(header+8,"%u,%u",&width,&height);
+            (void) sscanf(header+8,"%lu,%lu",&width,&height);
             break;
           }
         if (LocaleNCompare(header,"rorient:",8) == 0)
           {
-            (void) sscanf(header+8,"%u,%u",&pel_path,&direction);
+            (void) sscanf(header+8,"%lu,%lu",&pel_path,&direction);
             if (pel_path == 90)
               orientation=5;
             else
@@ -248,71 +237,48 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
           }
         if (LocaleNCompare(header,"rtype:",6) == 0)
           {
-            (void) sscanf(header+6,"%u",&type);
+            (void) sscanf(header+6,"%lu",&type);
             break;
           }
         break;
       }
     }
   }
-  if ((width == 0) || (height == 0) || (type == 0))
-    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
   /*
-    Write CALS facsimile document wrapped as a TIFF image file.
+    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))
-    ThrowReaderException(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,width);
-  length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
-  length=WriteCALSLSBLong(file,height);
-  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,width);
-  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++)
+    ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
+  while ((c=ReadBlobByte(image)) != EOF)
     (void) fputc(c,file);
+  (void) fclose(file);
   (void) CloseBlob(image);
   image=DestroyImage(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",
+  (void) FormatMagickString(read_info->filename,MaxTextExtent,"group4:%.1024s",
     filename);
+  (void) FormatMagickString(message,MaxTextExtent,"%lux%lu",width,height);
+  read_info->size=ConstantString(message);
+  (void) FormatMagickString(message,MaxTextExtent,"%lu",density);
+  read_info->density=ConstantString(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);
-  if (image == (Image *) NULL)
-    return(image);
-  return(GetFirstImageInList(image));
+  return(image);
 }
 \f
 /*
@@ -326,11 +292,12 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  RegisterCALSImage() adds attributes for the CALS X image 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 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.
+%  RegisterCALSImage() adds attributes for the CALS Raster Group 1 image file
+%  image 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 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.
 %
 %  The format of the RegisterCALSImage method is:
 %
@@ -342,12 +309,36 @@ ModuleExport unsigned long RegisterCALSImage(void)
   MagickInfo
     *entry;
 
+  static const char
+    *CALSDescription=
+    {
+      "Continuous Acquisition and Life-cycle Support Type 1 Image"
+    },
+    *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);
@@ -374,5 +365,216 @@ ModuleExport unsigned long RegisterCALSImage(void)
 */
 ModuleExport void UnregisterCALSImage(void)
 {
+  (void) UnregisterMagickInfo("CAL");
   (void) UnregisterMagickInfo("CALS");
 }
+\f
+#if defined(MAGICKCORE_TIFF_DELEGATE)
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   W r i t e C A L S I m a g e                                               %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  WriteCALSImage() writes an image to a file in CALS Raster Group 1 image
+%  format.
+%
+%  The format of the WriteCALSImage method is:
+%
+%      MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
+%        Image *image)
+%
+%  A description of each parameter follows.
+%
+%    o image_info: the image info.
+%
+%    o image:  The image.
+%
+*/
+
+static ssize_t WriteCALSRecord(Image *image,const char *data)
+{
+  char
+    pad[128];
+
+  ssize_t
+    count;
+
+  register const char
+    *p;
+
+  register long
+    i;
+
+  i=0;
+  if (data != (const char *) NULL)
+    {
+      p=data;
+      for (i=0; (i < 128) && (p[i] != '\0'); i++);
+      count=WriteBlob(image,(size_t) i,(const unsigned char *) data);
+    }
+  if (i < 128)
+    {
+      i=128-i;
+      (void) ResetMagickMemory(pad,' ',(const size_t) i);
+      count=WriteBlob(image,(size_t) i,(const unsigned char *) pad);
+    }
+  return(count);
+}
+
+static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
+  Image *image)
+{
+  char
+    header[129];
+
+  Image
+    *group4_image;
+
+  ImageInfo
+    *write_info;
+
+  MagickBooleanType
+    status;
+
+  register long
+    i;
+
+  ssize_t
+    count;
+
+  size_t
+    length;
+
+  unsigned char
+    *group4;
+
+  unsigned long
+    density,
+    orient_x,
+    orient_y;
+
+  /*
+    Open output image 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);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+  if (status == MagickFalse)
+    return(status);
+  /*
+    Create standard CALS header.
+  */
+  count=WriteCALSRecord(image,"srcdocid: NONE");
+  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:
+    {
+      orient_x=180;
+      orient_y=270;
+      break;
+    }
+    case BottomRightOrientation:
+    {
+      orient_x=180;
+      orient_y=90;
+      break;
+    }
+    case BottomLeftOrientation:
+    {
+      orient_y=90;
+      break;
+    }
+    case LeftTopOrientation:
+    {
+      orient_x=270;
+      break;
+    }
+    case RightTopOrientation:
+    {
+      orient_x=270;
+      orient_y=180;
+      break;
+    }
+    case RightBottomOrientation:
+    {
+      orient_x=90;
+      orient_y=180;
+      break;
+    }
+    case LeftBottomOrientation:
+    {
+      orient_x=90;
+      break;
+    }
+    default:
+    {
+      orient_y=270;
+    }
+  }
+  (void) FormatMagickString(header,MaxTextExtent,"rorient: %03ld,%03ld",
+    orient_x,orient_y);
+  count=WriteCALSRecord(image,header);
+  (void) FormatMagickString(header,MaxTextExtent,"rpelcnt: %06lu,%06lu",
+    image->columns,image->rows);
+  count=WriteCALSRecord(image,header);  
+  density=200;
+  if (image_info->density != (char *) NULL)
+    {
+      GeometryInfo
+        geometry_info;
+
+      (void) ParseGeometry(image_info->density,&geometry_info);
+      density=(unsigned long) (geometry_info.rho+0.5);
+    }
+  (void) FormatMagickString(header,MaxTextExtent,"rdensty: %04lu",density);
+  count=WriteCALSRecord(image,header);
+  count=WriteCALSRecord(image,"notes: NONE");
+  (void) ResetMagickMemory(header,' ',128);
+  for (i=0; i < 5; i++)
+    (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,&image->exception);
+  if (group4_image == (Image *) NULL)
+    {
+      (void) CloseBlob(image);
+      return(MagickFalse);
+    }
+  group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
+    &image->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