2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % CCCC A A LLLLL SSSSS %
13 % Read/Write CALS Raster Group 1 Image Format %
20 % Copyright 1999-2010 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 "magick/studio.h"
48 #include "magick/blob.h"
49 #include "magick/blob-private.h"
50 #include "magick/cache.h"
51 #include "magick/colorspace.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/quantum-private.h"
64 #include "magick/resource_.h"
65 #include "magick/static.h"
66 #include "magick/string_.h"
67 #include "magick/module.h"
72 static MagickBooleanType
73 WriteCALSImage(const ImageInfo *,Image *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsCALS() returns MagickTrue if the image format type, identified by the
87 % magick string, is CALS Raster Group 1.
89 % The format of the IsCALS method is:
91 % MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o length: Specifies the length of the magick string.
100 static MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
104 if (LocaleNCompare((const char *) magick,"version: MIL-STD-1840",21) == 0)
106 if (LocaleNCompare((const char *) magick,"srcdocid:",9) == 0)
108 if (LocaleNCompare((const char *) magick,"rorient:",8) == 0)
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % R e a d C A L S I m a g e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % ReadCALSImage() reads an CALS Raster Group 1 image format image file and
125 % returns it. It allocates the memory necessary for the new Image structure
126 % and returns a pointer to the new image.
128 % The format of the ReadCALSImage method is:
130 % Image *ReadCALSImage(const ImageInfo *image_info,
131 % ExceptionInfo *exception)
133 % A description of each parameter follows:
135 % o image_info: the image info.
137 % o exception: return any errors or warnings in this structure.
140 static Image *ReadCALSImage(const ImageInfo *image_info,
141 ExceptionInfo *exception)
144 filename[MaxTextExtent],
146 message[MaxTextExtent];
179 assert(image_info != (const ImageInfo *) NULL);
180 assert(image_info->signature == MagickSignature);
181 if (image_info->debug != MagickFalse)
182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
183 image_info->filename);
184 assert(exception != (ExceptionInfo *) NULL);
185 assert(exception->signature == MagickSignature);
186 image=AcquireImage(image_info);
187 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
188 if (status == MagickFalse)
190 image=DestroyImageList(image);
191 return((Image *) NULL);
196 (void) ResetMagickMemory(header,0,sizeof(header));
204 for (i=0; i < 16; i++)
206 if (ReadBlob(image,128,(unsigned char *) header) != 128)
213 if (LocaleNCompare(header,"rdensty:",8) == 0)
215 (void) sscanf(header+8,"%lu",&density);
218 if (LocaleNCompare(header,"rpelcnt:",8) == 0)
220 (void) sscanf(header+8,"%lu,%lu",&width,&height);
223 if (LocaleNCompare(header,"rorient:",8) == 0)
225 (void) sscanf(header+8,"%lu,%lu",&pel_path,&direction);
238 if (LocaleNCompare(header,"rtype:",6) == 0)
240 (void) sscanf(header+6,"%lu",&type);
251 unique_file=AcquireUniqueFileResource(filename);
252 if (unique_file != -1)
253 file=fdopen(unique_file,"wb");
254 if ((unique_file == -1) || (file == (FILE *) NULL))
255 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
256 while ((c=ReadBlobByte(image)) != EOF)
257 (void) fputc(c,file);
259 (void) CloseBlob(image);
260 image=DestroyImage(image);
261 read_info=CloneImageInfo(image_info);
262 SetImageInfoBlob(read_info,(void *) NULL,0);
263 (void) FormatMagickString(read_info->filename,MaxTextExtent,"group4:%.1024s",
265 (void) FormatMagickString(message,MaxTextExtent,"%lux%lu",width,height);
266 read_info->size=ConstantString(message);
267 (void) FormatMagickString(message,MaxTextExtent,"%lu",density);
268 read_info->density=ConstantString(message);
269 read_info->orientation=(OrientationType) orientation;
270 image=ReadImage(read_info,exception);
271 if (image != (Image *) NULL)
273 (void) CopyMagickString(image->filename,image_info->filename,
275 (void) CopyMagickString(image->magick_filename,image_info->filename,
277 (void) CopyMagickString(image->magick,"CALS",MaxTextExtent);
279 read_info=DestroyImageInfo(read_info);
280 (void) RelinquishUniqueFileResource(filename);
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 % R e g i s t e r C A L S I m a g e %
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % RegisterCALSImage() adds attributes for the CALS Raster Group 1 image file
296 % image format to the list of supported formats. The attributes include the
297 % image format tag, a method to read and/or write the format, whether the
298 % format supports the saving of more than one frame to the same file or blob,
299 % whether the format supports native in-memory I/O, and a brief description
302 % The format of the RegisterCALSImage method is:
304 % unsigned long RegisterCALSImage(void)
307 ModuleExport unsigned long RegisterCALSImage(void)
315 "Continuous Acquisition and Life-cycle Support Type 1"
319 "Specified in MIL-R-28002 and MIL-PRF-28002"
322 entry=SetMagickInfo("CAL");
323 entry->decoder=(DecodeImageHandler *) ReadCALSImage;
324 #if defined(MAGICKCORE_TIFF_DELEGATE)
325 entry->encoder=(EncodeImageHandler *) WriteCALSImage;
327 entry->adjoin=MagickFalse;
328 entry->magick=(IsImageFormatHandler *) IsCALS;
329 entry->description=ConstantString(CALSDescription);
330 entry->note=ConstantString(CALSNote);
331 entry->module=ConstantString("CALS");
332 (void) RegisterMagickInfo(entry);
333 entry=SetMagickInfo("CALS");
334 entry->decoder=(DecodeImageHandler *) ReadCALSImage;
335 #if defined(MAGICKCORE_TIFF_DELEGATE)
336 entry->encoder=(EncodeImageHandler *) WriteCALSImage;
338 entry->adjoin=MagickFalse;
339 entry->magick=(IsImageFormatHandler *) IsCALS;
340 entry->description=ConstantString(CALSDescription);
341 entry->note=ConstantString(CALSNote);
342 entry->module=ConstantString("CALS");
343 (void) RegisterMagickInfo(entry);
344 return(MagickImageCoderSignature);
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % U n r e g i s t e r C A L S I m a g e %
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % UnregisterCALSImage() removes format registrations made by the
359 % CALS module from the list of supported formats.
361 % The format of the UnregisterCALSImage method is:
363 % UnregisterCALSImage(void)
366 ModuleExport void UnregisterCALSImage(void)
368 (void) UnregisterMagickInfo("CAL");
369 (void) UnregisterMagickInfo("CALS");
372 #if defined(MAGICKCORE_TIFF_DELEGATE)
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 % W r i t e C A L S I m a g e %
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 % WriteCALSImage() writes an image to a file in CALS Raster Group 1 image
387 % The format of the WriteCALSImage method is:
389 % MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
392 % A description of each parameter follows.
394 % o image_info: the image info.
396 % o image: The image.
400 static ssize_t WriteCALSRecord(Image *image,const char *data)
415 if (data != (const char *) NULL)
418 for (i=0; (i < 128) && (p[i] != '\0'); i++);
419 count=WriteBlob(image,(size_t) i,(const unsigned char *) data);
424 (void) ResetMagickMemory(pad,' ',(const size_t) i);
425 count=WriteBlob(image,(size_t) i,(const unsigned char *) pad);
430 static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
463 Open output image file.
465 assert(image_info != (const ImageInfo *) NULL);
466 assert(image_info->signature == MagickSignature);
467 assert(image != (Image *) NULL);
468 assert(image->signature == MagickSignature);
469 if (image->debug != MagickFalse)
470 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
471 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
472 if (status == MagickFalse)
475 Create standard CALS header.
477 count=WriteCALSRecord(image,"srcdocid: NONE");
478 count=WriteCALSRecord(image,"dstdocid: NONE");
479 count=WriteCALSRecord(image,"txtfilid: NONE");
480 count=WriteCALSRecord(image,"figid: NONE");
481 count=WriteCALSRecord(image,"srcgph: NONE");
482 count=WriteCALSRecord(image,"docls: NONE");
483 count=WriteCALSRecord(image,"rtype: 1");
486 switch (image->orientation)
488 case TopRightOrientation:
494 case BottomRightOrientation:
500 case BottomLeftOrientation:
505 case LeftTopOrientation:
510 case RightTopOrientation:
516 case RightBottomOrientation:
522 case LeftBottomOrientation:
532 (void) FormatMagickString(header,MaxTextExtent,"rorient: %03ld,%03ld",
534 count=WriteCALSRecord(image,header);
535 (void) FormatMagickString(header,MaxTextExtent,"rpelcnt: %06lu,%06lu",
536 image->columns,image->rows);
537 count=WriteCALSRecord(image,header);
539 if (image_info->density != (char *) NULL)
544 (void) ParseGeometry(image_info->density,&geometry_info);
545 density=(unsigned long) (geometry_info.rho+0.5);
547 (void) FormatMagickString(header,MaxTextExtent,"rdensty: %04lu",density);
548 count=WriteCALSRecord(image,header);
549 count=WriteCALSRecord(image,"notes: NONE");
550 (void) ResetMagickMemory(header,' ',128);
551 for (i=0; i < 5; i++)
552 (void) WriteBlob(image,128,(unsigned char *) header);
556 write_info=CloneImageInfo(image_info);
557 (void) CopyMagickString(write_info->filename,"GROUP4:",MaxTextExtent);
558 (void) CopyMagickString(write_info->magick,"GROUP4",MaxTextExtent);
559 group4_image=CloneImage(image,0,0,MagickTrue,&image->exception);
560 if (group4_image == (Image *) NULL)
562 (void) CloseBlob(image);
565 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
567 group4_image=DestroyImage(group4_image);
568 if (group4 == (unsigned char *) NULL)
570 (void) CloseBlob(image);
573 write_info=DestroyImageInfo(write_info);
574 if (WriteBlob(image,length,group4) != (ssize_t) length)
576 group4=(unsigned char *) RelinquishMagickMemory(group4);
577 (void) CloseBlob(image);