2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % CCCC A A LLLLL SSSSS %
13 % Read/Write CALS Raster Group 1 Image Format %
20 % Copyright 1999-2011 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % The CALS raster format is a standard developed by the Computer Aided
37 % Acquisition and Logistics Support (CALS) office of the United States
38 % Department of Defense to standardize graphics data interchange for
39 % electronic publishing, especially in the areas of technical graphics,
40 % CAD/CAM, and image processing applications.
47 #include "MagickCore/studio.h"
48 #include "MagickCore/blob.h"
49 #include "MagickCore/blob-private.h"
50 #include "MagickCore/cache.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/resource_.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/module.h"
69 #if defined(MAGICKCORE_TIFF_DELEGATE)
73 static MagickBooleanType
74 WriteCALSImage(const ImageInfo *,Image *,ExceptionInfo *);
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % IsCALS() returns MagickTrue if the image format type, identified by the
89 % magick string, is CALS Raster Group 1.
91 % The format of the IsCALS method is:
93 % MagickBooleanType IsCALS(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 IsCALS(const unsigned char *magick,const size_t length)
106 if (LocaleNCompare((const char *) magick,"version: MIL-STD-1840",21) == 0)
108 if (LocaleNCompare((const char *) magick,"srcdocid:",9) == 0)
110 if (LocaleNCompare((const char *) magick,"rorient:",8) == 0)
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % R e a d C A L S I m a g e %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % ReadCALSImage() reads an CALS Raster Group 1 image format image file and
127 % returns it. It allocates the memory necessary for the new Image structure
128 % and returns a pointer to the new image.
130 % The format of the ReadCALSImage method is:
132 % Image *ReadCALSImage(const ImageInfo *image_info,
133 % ExceptionInfo *exception)
135 % A description of each parameter follows:
137 % o image_info: the image info.
139 % o exception: return any errors or warnings in this structure.
142 static Image *ReadCALSImage(const ImageInfo *image_info,
143 ExceptionInfo *exception)
146 filename[MaxTextExtent],
148 message[MaxTextExtent];
181 assert(image_info != (const ImageInfo *) NULL);
182 assert(image_info->signature == MagickSignature);
183 if (image_info->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
185 image_info->filename);
186 assert(exception != (ExceptionInfo *) NULL);
187 assert(exception->signature == MagickSignature);
188 image=AcquireImage(image_info,exception);
189 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
190 if (status == MagickFalse)
192 image=DestroyImageList(image);
193 return((Image *) NULL);
198 (void) ResetMagickMemory(header,0,sizeof(header));
206 for (i=0; i < 16; i++)
208 if (ReadBlob(image,128,(unsigned char *) header) != 128)
215 if (LocaleNCompare(header,"rdensty:",8) == 0)
217 (void) sscanf(header+8,"%lu",&density);
220 if (LocaleNCompare(header,"rpelcnt:",8) == 0)
222 (void) sscanf(header+8,"%lu,%lu",&width,&height);
225 if (LocaleNCompare(header,"rorient:",8) == 0)
227 (void) sscanf(header+8,"%lu,%lu",&pel_path,&direction);
240 if (LocaleNCompare(header,"rtype:",6) == 0)
242 (void) sscanf(header+6,"%lu",&type);
253 unique_file=AcquireUniqueFileResource(filename);
254 if (unique_file != -1)
255 file=fdopen(unique_file,"wb");
256 if ((unique_file == -1) || (file == (FILE *) NULL))
257 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
258 while ((c=ReadBlobByte(image)) != EOF)
259 (void) fputc(c,file);
261 (void) CloseBlob(image);
262 image=DestroyImage(image);
263 read_info=CloneImageInfo(image_info);
264 SetImageInfoBlob(read_info,(void *) NULL,0);
265 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"group4:%s",
267 (void) FormatLocaleString(message,MaxTextExtent,"%lux%lu",width,height);
268 read_info->size=ConstantString(message);
269 (void) FormatLocaleString(message,MaxTextExtent,"%lu",density);
270 read_info->density=ConstantString(message);
271 read_info->orientation=(OrientationType) orientation;
272 image=ReadImage(read_info,exception);
273 if (image != (Image *) NULL)
275 (void) CopyMagickString(image->filename,image_info->filename,
277 (void) CopyMagickString(image->magick_filename,image_info->filename,
279 (void) CopyMagickString(image->magick,"CALS",MaxTextExtent);
281 read_info=DestroyImageInfo(read_info);
282 (void) RelinquishUniqueFileResource(filename);
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % R e g i s t e r C A L S I m a g e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % RegisterCALSImage() adds attributes for the CALS Raster Group 1 image file
298 % image format to the list of supported formats. The attributes include the
299 % image format tag, a method to read and/or write the format, whether the
300 % format supports the saving of more than one frame to the same file or blob,
301 % whether the format supports native in-memory I/O, and a brief description
304 % The format of the RegisterCALSImage method is:
306 % size_t RegisterCALSImage(void)
309 ModuleExport size_t RegisterCALSImage(void)
317 "Continuous Acquisition and Life-cycle Support Type 1"
321 "Specified in MIL-R-28002 and MIL-PRF-28002"
324 entry=SetMagickInfo("CAL");
325 entry->decoder=(DecodeImageHandler *) ReadCALSImage;
326 #if defined(MAGICKCORE_TIFF_DELEGATE)
327 entry->encoder=(EncodeImageHandler *) WriteCALSImage;
329 entry->adjoin=MagickFalse;
330 entry->magick=(IsImageFormatHandler *) IsCALS;
331 entry->description=ConstantString(CALSDescription);
332 entry->note=ConstantString(CALSNote);
333 entry->module=ConstantString("CALS");
334 (void) RegisterMagickInfo(entry);
335 entry=SetMagickInfo("CALS");
336 entry->decoder=(DecodeImageHandler *) ReadCALSImage;
337 #if defined(MAGICKCORE_TIFF_DELEGATE)
338 entry->encoder=(EncodeImageHandler *) WriteCALSImage;
340 entry->adjoin=MagickFalse;
341 entry->magick=(IsImageFormatHandler *) IsCALS;
342 entry->description=ConstantString(CALSDescription);
343 entry->note=ConstantString(CALSNote);
344 entry->module=ConstantString("CALS");
345 (void) RegisterMagickInfo(entry);
346 return(MagickImageCoderSignature);
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % U n r e g i s t e r C A L S I m a g e %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % UnregisterCALSImage() removes format registrations made by the
361 % CALS module from the list of supported formats.
363 % The format of the UnregisterCALSImage method is:
365 % UnregisterCALSImage(void)
368 ModuleExport void UnregisterCALSImage(void)
370 (void) UnregisterMagickInfo("CAL");
371 (void) UnregisterMagickInfo("CALS");
374 #if defined(MAGICKCORE_TIFF_DELEGATE)
376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 % W r i t e C A L S I m a g e %
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 % WriteCALSImage() writes an image to a file in CALS Raster Group 1 image
389 % The format of the WriteCALSImage method is:
391 % MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
392 % Image *image,ExceptionInfo *exception)
394 % A description of each parameter follows.
396 % o image_info: the image info.
398 % o image: The image.
400 % o exception: return any errors or warnings in this structure.
404 static ssize_t WriteCALSRecord(Image *image,const char *data)
419 if (data != (const char *) NULL)
422 for (i=0; (i < 128) && (p[i] != '\0'); i++);
423 count=WriteBlob(image,(size_t) i,(const unsigned char *) data);
428 (void) ResetMagickMemory(pad,' ',(size_t) i);
429 count=WriteBlob(image,(size_t) i,(const unsigned char *) pad);
434 static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
435 Image *image,ExceptionInfo *exception)
465 Open output image file.
467 assert(image_info != (const ImageInfo *) NULL);
468 assert(image_info->signature == MagickSignature);
469 assert(image != (Image *) NULL);
470 assert(image->signature == MagickSignature);
471 if (image->debug != MagickFalse)
472 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
473 assert(exception != (ExceptionInfo *) NULL);
474 assert(exception->signature == MagickSignature);
475 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
476 if (status == MagickFalse)
479 Create standard CALS header.
481 count=WriteCALSRecord(image,"srcdocid: NONE");
483 count=WriteCALSRecord(image,"dstdocid: NONE");
484 count=WriteCALSRecord(image,"txtfilid: NONE");
485 count=WriteCALSRecord(image,"figid: NONE");
486 count=WriteCALSRecord(image,"srcgph: NONE");
487 count=WriteCALSRecord(image,"docls: NONE");
488 count=WriteCALSRecord(image,"rtype: 1");
491 switch (image->orientation)
493 case TopRightOrientation:
499 case BottomRightOrientation:
505 case BottomLeftOrientation:
510 case LeftTopOrientation:
515 case RightTopOrientation:
521 case RightBottomOrientation:
527 case LeftBottomOrientation:
537 (void) FormatLocaleString(header,MaxTextExtent,"rorient: %03ld,%03ld",
538 (long) orient_x,(long) orient_y);
539 count=WriteCALSRecord(image,header);
540 (void) FormatLocaleString(header,MaxTextExtent,"rpelcnt: %06lu,%06lu",
541 (unsigned long) image->columns,(unsigned long) image->rows);
542 count=WriteCALSRecord(image,header);
544 if (image_info->density != (char *) NULL)
549 (void) ParseGeometry(image_info->density,&geometry_info);
550 density=(size_t) floor(geometry_info.rho+0.5);
552 (void) FormatLocaleString(header,MaxTextExtent,"rdensty: %04lu",
553 (unsigned long) density);
554 count=WriteCALSRecord(image,header);
555 count=WriteCALSRecord(image,"notes: NONE");
556 (void) ResetMagickMemory(header,' ',128);
557 for (i=0; i < 5; i++)
558 (void) WriteBlob(image,128,(unsigned char *) header);
562 write_info=CloneImageInfo(image_info);
563 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
564 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
565 group4_image=CloneImage(image,0,0,MagickTrue,exception);
566 if (group4_image == (Image *) NULL)
568 (void) CloseBlob(image);
571 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
573 group4_image=DestroyImage(group4_image);
574 if (group4 == (unsigned char *) NULL)
576 (void) CloseBlob(image);
579 write_info=DestroyImageInfo(write_info);
580 if (WriteBlob(image,length,group4) != (ssize_t) length)
582 group4=(unsigned char *) RelinquishMagickMemory(group4);
583 (void) CloseBlob(image);