2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % CCCC A A LLLLL SSSSS %
13 % Read/Write CALS Raster Group 1 Image Format %
20 % Copyright 1999-2017 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 % https://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/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/geometry.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/resource_.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
70 #if defined(MAGICKCORE_TIFF_DELEGATE)
74 static MagickBooleanType
75 WriteCALSImage(const ImageInfo *,Image *,ExceptionInfo *);
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % IsCALS() returns MagickTrue if the image format type, identified by the
90 % magick string, is CALS Raster Group 1.
92 % The format of the IsCALS method is:
94 % MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
96 % A description of each parameter follows:
98 % o magick: compare image format pattern against these bytes.
100 % o length: Specifies the length of the magick string.
103 static MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
107 if (LocaleNCompare((const char *) magick,"version: MIL-STD-1840",21) == 0)
109 if (LocaleNCompare((const char *) magick,"srcdocid:",9) == 0)
111 if (LocaleNCompare((const char *) magick,"rorient:",8) == 0)
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % R e a d C A L S I m a g e %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % ReadCALSImage() reads an CALS Raster Group 1 image format image file and
128 % returns it. It allocates the memory necessary for the new Image structure
129 % and returns a pointer to the new image.
131 % The format of the ReadCALSImage method is:
133 % Image *ReadCALSImage(const ImageInfo *image_info,
134 % ExceptionInfo *exception)
136 % A description of each parameter follows:
138 % o image_info: the image info.
140 % o exception: return any errors or warnings in this structure.
143 static Image *ReadCALSImage(const ImageInfo *image_info,
144 ExceptionInfo *exception)
147 filename[MagickPathExtent],
148 header[MagickPathExtent],
149 message[MagickPathExtent];
182 assert(image_info != (const ImageInfo *) NULL);
183 assert(image_info->signature == MagickCoreSignature);
184 if (image_info->debug != MagickFalse)
185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
186 image_info->filename);
187 assert(exception != (ExceptionInfo *) NULL);
188 assert(exception->signature == MagickCoreSignature);
189 image=AcquireImage(image_info,exception);
190 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
191 if (status == MagickFalse)
193 image=DestroyImageList(image);
194 return((Image *) NULL);
199 (void) ResetMagickMemory(header,0,sizeof(header));
207 for (i=0; i < 16; i++)
209 if (ReadBlob(image,128,(unsigned char *) header) != 128)
216 if (LocaleNCompare(header,"rdensty:",8) == 0)
218 (void) sscanf(header+8,"%lu",&density);
221 if (LocaleNCompare(header,"rpelcnt:",8) == 0)
223 (void) sscanf(header+8,"%lu,%lu",&width,&height);
226 if (LocaleNCompare(header,"rorient:",8) == 0)
228 (void) sscanf(header+8,"%lu,%lu",&pel_path,&direction);
241 if (LocaleNCompare(header,"rtype:",6) == 0)
243 (void) sscanf(header+6,"%lu",&type);
254 unique_file=AcquireUniqueFileResource(filename);
255 if (unique_file != -1)
256 file=fdopen(unique_file,"wb");
257 if ((unique_file == -1) || (file == (FILE *) NULL))
258 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
259 while ((c=ReadBlobByte(image)) != EOF)
260 (void) fputc(c,file);
262 (void) CloseBlob(image);
263 image=DestroyImage(image);
264 read_info=CloneImageInfo(image_info);
265 SetImageInfoBlob(read_info,(void *) NULL,0);
266 (void) FormatLocaleString(read_info->filename,MagickPathExtent,"group4:%s",
268 (void) FormatLocaleString(message,MagickPathExtent,"%lux%lu",width,height);
269 (void) CloneString(&read_info->size,message);
270 (void) FormatLocaleString(message,MagickPathExtent,"%lu",density);
271 (void) CloneString(&read_info->density,message);
272 read_info->orientation=(OrientationType) orientation;
273 image=ReadImage(read_info,exception);
274 if (image != (Image *) NULL)
276 (void) CopyMagickString(image->filename,image_info->filename,
278 (void) CopyMagickString(image->magick_filename,image_info->filename,
280 (void) CopyMagickString(image->magick,"CALS",MagickPathExtent);
282 read_info=DestroyImageInfo(read_info);
283 (void) RelinquishUniqueFileResource(filename);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % R e g i s t e r C A L S I m a g e %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % RegisterCALSImage() adds attributes for the CALS Raster Group 1 image file
299 % image format to the list of supported formats. The attributes include the
300 % image format tag, a method to read and/or write the format, whether the
301 % format supports the saving of more than one frame to the same file or blob,
302 % whether the format supports native in-memory I/O, and a brief description
305 % The format of the RegisterCALSImage method is:
307 % size_t RegisterCALSImage(void)
310 ModuleExport size_t RegisterCALSImage(void)
312 #define CALSDescription "Continuous Acquisition and Life-cycle Support Type 1"
313 #define CALSNote "Specified in MIL-R-28002 and MIL-PRF-28002"
318 entry=AcquireMagickInfo("CALS","CAL",CALSDescription);
319 entry->decoder=(DecodeImageHandler *) ReadCALSImage;
320 #if defined(MAGICKCORE_TIFF_DELEGATE)
321 entry->encoder=(EncodeImageHandler *) WriteCALSImage;
323 entry->flags^=CoderAdjoinFlag;
324 entry->magick=(IsImageFormatHandler *) IsCALS;
325 entry->note=ConstantString(CALSNote);
326 (void) RegisterMagickInfo(entry);
327 entry=AcquireMagickInfo("CALS","CALS",CALSDescription);
328 entry->decoder=(DecodeImageHandler *) ReadCALSImage;
329 #if defined(MAGICKCORE_TIFF_DELEGATE)
330 entry->encoder=(EncodeImageHandler *) WriteCALSImage;
332 entry->flags^=CoderAdjoinFlag;
333 entry->magick=(IsImageFormatHandler *) IsCALS;
334 entry->note=ConstantString(CALSNote);
335 (void) RegisterMagickInfo(entry);
336 return(MagickImageCoderSignature);
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % U n r e g i s t e r C A L S I m a g e %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 % UnregisterCALSImage() removes format registrations made by the
351 % CALS module from the list of supported formats.
353 % The format of the UnregisterCALSImage method is:
355 % UnregisterCALSImage(void)
358 ModuleExport void UnregisterCALSImage(void)
360 (void) UnregisterMagickInfo("CAL");
361 (void) UnregisterMagickInfo("CALS");
364 #if defined(MAGICKCORE_TIFF_DELEGATE)
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 % W r i t e C A L S I m a g e %
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % WriteCALSImage() writes an image to a file in CALS Raster Group 1 image
379 % The format of the WriteCALSImage method is:
381 % MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
382 % Image *image,ExceptionInfo *exception)
384 % A description of each parameter follows.
386 % o image_info: the image info.
388 % o image: The image.
390 % o exception: return any errors or warnings in this structure.
394 static ssize_t WriteCALSRecord(Image *image,const char *data)
410 if (data != (const char *) NULL)
413 for (i=0; (i < 128) && (p[i] != '\0'); i++);
414 count=WriteBlob(image,(size_t) i,(const unsigned char *) data);
419 (void) ResetMagickMemory(pad,' ',(size_t) i);
420 count=WriteBlob(image,(size_t) i,(const unsigned char *) pad);
425 static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
426 Image *image,ExceptionInfo *exception)
456 Open output image file.
458 assert(image_info != (const ImageInfo *) NULL);
459 assert(image_info->signature == MagickCoreSignature);
460 assert(image != (Image *) NULL);
461 assert(image->signature == MagickCoreSignature);
462 if (image->debug != MagickFalse)
463 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
464 assert(exception != (ExceptionInfo *) NULL);
465 assert(exception->signature == MagickCoreSignature);
466 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
467 if (status == MagickFalse)
470 Create standard CALS header.
472 count=WriteCALSRecord(image,"srcdocid: NONE");
474 count=WriteCALSRecord(image,"dstdocid: NONE");
475 count=WriteCALSRecord(image,"txtfilid: NONE");
476 count=WriteCALSRecord(image,"figid: NONE");
477 count=WriteCALSRecord(image,"srcgph: NONE");
478 count=WriteCALSRecord(image,"doccls: NONE");
479 count=WriteCALSRecord(image,"rtype: 1");
482 switch (image->orientation)
484 case TopRightOrientation:
490 case BottomRightOrientation:
496 case BottomLeftOrientation:
501 case LeftTopOrientation:
506 case RightTopOrientation:
512 case RightBottomOrientation:
518 case LeftBottomOrientation:
529 (void) FormatLocaleString(header,sizeof(header),"rorient: %03ld,%03ld",
530 (long) orient_x,(long) orient_y);
531 count=WriteCALSRecord(image,header);
532 (void) FormatLocaleString(header,sizeof(header),"rpelcnt: %06lu,%06lu",
533 (unsigned long) image->columns,(unsigned long) image->rows);
534 count=WriteCALSRecord(image,header);
536 if (image_info->density != (char *) NULL)
541 (void) ParseGeometry(image_info->density,&geometry_info);
542 density=(size_t) floor(geometry_info.rho+0.5);
544 (void) FormatLocaleString(header,sizeof(header),"rdensty: %04lu",
545 (unsigned long) density);
546 count=WriteCALSRecord(image,header);
547 count=WriteCALSRecord(image,"notes: NONE");
548 (void) ResetMagickMemory(header,' ',128);
549 for (i=0; i < 5; i++)
550 (void) WriteBlob(image,128,(unsigned char *) header);
554 write_info=CloneImageInfo(image_info);
555 (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
556 (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
557 group4_image=CloneImage(image,0,0,MagickTrue,exception);
558 if (group4_image == (Image *) NULL)
560 write_info=DestroyImageInfo(write_info);
561 (void) CloseBlob(image);
564 group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
566 group4_image=DestroyImage(group4_image);
567 if (group4 == (unsigned char *) NULL)
569 write_info=DestroyImageInfo(write_info);
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);