2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % IIIIIIIIII PPPPPPPP LL %
13 % IIIIIIIIII PP LLLLLLLL %
17 % Read/Write Scanalytics IPLab Image Format %
22 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
23 % dedicated to making software imaging solutions freely available. %
25 % You may not use this file except in compliance with the License. You may %
26 % obtain a copy of the License at %
28 % http://www.imagemagick.org/script/license.php %
30 % Unless required by applicable law or agreed to in writing, software %
31 % distributed under the License is distributed on an "AS IS" BASIS, %
32 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
33 % See the License for the specific language governing permissions and %
34 % limitations under the License. %
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 #include "MagickCore/studio.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/colorspace.h"
49 #include "MagickCore/colorspace-private.h"
50 #include "MagickCore/exception.h"
51 #include "MagickCore/exception-private.h"
52 #include "MagickCore/image.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/list.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/option.h"
60 #include "MagickCore/property.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
70 typedef struct _IPLInfo
84 static MagickBooleanType
85 WriteIPLImage(const ImageInfo *,Image *,ExceptionInfo *);
88 static void increase (void *pixel, int byteType){
90 case 0:(*((unsigned char *) pixel))++; break;
91 case 1:(*((signed int *) pixel))++; break;
92 case 2:(*((unsigned int *) pixel))++; break;
93 case 3:(*((signed long *) pixel))++; break;
94 default:(*((unsigned int *) pixel))++; break;
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % IsIPL() returns MagickTrue if the image format type, identified by the
111 % magick string, is IPL.
113 % The format of the IsIPL method is:
115 % MagickBooleanType IsIPL(const unsigned char *magick,const size_t length)
117 % A description of each parameter follows:
119 % o magick: compare image format pattern against these bytes.
121 % o length: Specifies the length of the magick string.
124 static MagickBooleanType IsIPL(const unsigned char *magick,const size_t length)
128 if (LocaleNCompare((const char *) magick,"data",4) == 0)
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % R e a d I P L I m a g e %
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % ReadIPLImage() reads a Scanalytics IPLab image file and returns it. It
145 % allocates the memory necessary for the new Image structure and returns a
146 % pointer to the new image.
148 % According to the IPLab spec, the data is blocked out in five dimensions:
149 % { t, z, c, y, x }. When we return the image, the latter three are folded
150 % into the standard "Image" structure. The "scenes" (image_info->scene)
151 % correspond to the order: { {t0,z0}, {t0, z1}, ..., {t1,z0}, {t1,z1}... }
152 % The number of scenes is t*z.
154 % The format of the ReadIPLImage method is:
156 % Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception)
158 % A description of each parameter follows:
160 % o image_info: The image info.
162 % o exception: return any errors or warnings in this structure.
166 static void SetHeaderFromIPL(Image *image, IPLInfo *ipl){
167 image->columns = ipl->width;
168 image->rows = ipl->height;
169 image->depth = ipl->depth;
170 image->resolution.x = 1;
171 image->resolution.y = 1;
175 static Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception)
183 MagickBooleanType status;
185 unsigned char magick[12], *pixels;
203 assert(image_info != (const ImageInfo *) NULL);
204 assert(image_info->signature == MagickSignature);
205 if ( image_info->debug != MagickFalse)
206 (void) LogMagickEvent(TraceEvent, GetMagickModule(), "%s",
207 image_info->filename);
208 assert(exception != (ExceptionInfo *) NULL);
209 assert(exception->signature == MagickSignature);
210 image=AcquireImage(image_info,exception);
211 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
212 if (status == MagickFalse)
214 image=DestroyImageList(image);
215 return((Image *) NULL);
224 If we get back "iiii", we have LSB,"mmmm", MSB
226 count=ReadBlob(image,4,magick);
228 if((LocaleNCompare((char *) magick,"iiii",4) == 0))
229 image->endian=LSBEndian;
231 if((LocaleNCompare((char *) magick,"mmmm",4) == 0))
232 image->endian=MSBEndian;
234 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
237 /* Skip o'er the next 8 bytes (garbage) */
238 count=ReadBlob(image, 8, magick);
240 Excellent, now we read the header unimpeded.
242 count=ReadBlob(image,4,magick);
243 if((LocaleNCompare((char *) magick,"data",4) != 0))
244 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
245 ipl_info.size=ReadBlobLong(image);
246 ipl_info.width=ReadBlobLong(image);
247 ipl_info.height=ReadBlobLong(image);
248 if((ipl_info.width == 0UL) || (ipl_info.height == 0UL))
249 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
250 ipl_info.colors=ReadBlobLong(image);
251 if(ipl_info.colors == 3){ SetImageColorspace(image,sRGBColorspace,exception);}
252 else { image->colorspace = GRAYColorspace; }
253 ipl_info.z=ReadBlobLong(image);
254 ipl_info.time=ReadBlobLong(image);
256 ipl_info.byteType=ReadBlobLong(image);
259 /* Initialize Quantum Info */
261 switch (ipl_info.byteType) {
264 quantum_format = UnsignedQuantumFormat;
268 quantum_format = SignedQuantumFormat;
272 quantum_format = UnsignedQuantumFormat;
276 quantum_format = SignedQuantumFormat;
278 case 4: ipl_info.depth=32;
279 quantum_format = FloatingPointQuantumFormat;
283 quantum_format = UnsignedQuantumFormat;
287 quantum_format = UnsignedQuantumFormat;
291 quantum_format = FloatingPointQuantumFormat;
295 quantum_format = UnsignedQuantumFormat;
300 Set number of scenes of image
303 SetHeaderFromIPL(image, &ipl_info);
305 /* Thats all we need if we are pinging. */
306 if (image_info->ping != MagickFalse)
308 (void) CloseBlob(image);
309 return(GetFirstImageInList(image));
311 length=image->columns;
312 quantum_type=GetQuantumType(image,exception);
315 SetHeaderFromIPL(image, &ipl_info);
317 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
318 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
321 printf("Length: %.20g, Memory size: %.20g\n", (double) length,(double)
324 quantum_info=AcquireQuantumInfo(image_info,image);
325 if (quantum_info == (QuantumInfo *) NULL)
326 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
327 status=SetQuantumFormat(image,quantum_info,quantum_format);
328 if (status == MagickFalse)
329 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
330 pixels=GetQuantumPixels(quantum_info);
331 if(image->columns != ipl_info.width){
333 printf("Columns not set correctly! Wanted: %.20g, got: %.20g\n",
334 (double) ipl_info.width, (double) image->columns);
339 Covert IPL binary to pixel packets
342 if(ipl_info.colors == 1){
343 for(y = 0; y < (ssize_t) image->rows; y++){
344 (void) ReadBlob(image, length*image->depth/8, pixels);
345 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
346 if (q == (Quantum *) NULL)
348 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
349 GrayQuantum,pixels,exception);
350 if (SyncAuthenticPixels(image,exception) == MagickFalse)
355 for(y = 0; y < (ssize_t) image->rows; y++){
356 (void) ReadBlob(image, length*image->depth/8, pixels);
357 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
358 if (q == (Quantum *) NULL)
360 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
361 RedQuantum,pixels,exception);
362 if (SyncAuthenticPixels(image,exception) == MagickFalse)
365 for(y = 0; y < (ssize_t) image->rows; y++){
366 (void) ReadBlob(image, length*image->depth/8, pixels);
367 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
368 if (q == (Quantum *) NULL)
370 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
371 GreenQuantum,pixels,exception);
372 if (SyncAuthenticPixels(image,exception) == MagickFalse)
375 for(y = 0; y < (ssize_t) image->rows; y++){
376 (void) ReadBlob(image, length*image->depth/8, pixels);
377 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
378 if (q == (Quantum *) NULL)
380 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
381 BlueQuantum,pixels,exception);
382 if (SyncAuthenticPixels(image,exception) == MagickFalse)
386 SetQuantumImageType(image,quantum_type);
389 quantum_info = DestroyQuantumInfo(quantum_info);
391 if (EOFBlob(image) != MagickFalse)
393 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
397 if(t_count < ipl_info.z * ipl_info.time){
399 Proceed to next image.
401 AcquireNextImage(image_info,image,exception);
402 if (GetNextImageInList(image) == (Image *) NULL)
404 image=DestroyImageList(image);
405 return((Image *) NULL);
407 image=SyncNextImageInList(image);
408 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
410 if (status == MagickFalse)
413 } while (t_count < ipl_info.z*ipl_info.time);
415 return(GetFirstImageInList(image));
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 % R e g i s t e r I P L I m a g e %
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % RegisterIPLImage() add attributes for the Scanalytics IPL image format to the
430 % list of supported formats.
434 ModuleExport size_t RegisterIPLImage(void)
439 entry=SetMagickInfo("IPL");
440 entry->decoder=(DecodeImageHandler *) ReadIPLImage;
441 entry->encoder=(EncodeImageHandler *) WriteIPLImage;
442 entry->magick=(IsImageFormatHandler *) IsIPL;
443 entry->adjoin=MagickTrue;
444 entry->description=ConstantString("IPL Image Sequence");
445 entry->module=ConstantString("IPL");
446 entry->endian_support=MagickTrue;
447 (void) RegisterMagickInfo(entry);
448 return(MagickImageCoderSignature);
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 % U n r e g i s t e r I P L I m a g e %
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % UnregisterIPLImage() removes format registrations made by the
463 % IPL module from the list of supported formats.
465 % The format of the UnregisterIPLImage method is:
467 % UnregisterIPLImage(void)
470 ModuleExport void UnregisterIPLImage(void)
472 (void) UnregisterMagickInfo("IPL");
476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480 % W r i t e I P L I m a g e %
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 % WriteIPLImage() writes an image to a file in Scanalytics IPLabimage format.
488 % The format of the WriteIPLImage method is:
490 % MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
491 % Image *image,ExceptionInfo *exception)
493 % A description of each parameter follows.
495 % o image_info: The image info.
497 % o image: The image.
499 % o exception: return any errors or warnings in this structure.
502 static MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image,
503 ExceptionInfo *exception)
514 register const Quantum
527 Open output image file.
529 assert(image_info != (const ImageInfo *) NULL);
530 assert(image_info->signature == MagickSignature);
531 assert(image != (Image *) NULL);
532 assert(image->signature == MagickSignature);
533 if (image->debug != MagickFalse)
534 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
535 assert(exception != (ExceptionInfo *) NULL);
536 assert(exception->signature == MagickSignature);
537 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
538 if (status == MagickFalse)
543 quantum_info=AcquireQuantumInfo(image_info, image);
544 if ((quantum_info->format == UndefinedQuantumFormat) &&
545 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
546 SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
547 switch(quantum_info->depth){
549 ipl_info.byteType = 0;
552 if(quantum_info->format == SignedQuantumFormat){
553 ipl_info.byteType = 2;
556 ipl_info.byteType = 1;
560 if(quantum_info->format == FloatingPointQuantumFormat){
561 ipl_info.byteType = 3;
564 ipl_info.byteType = 4;
568 ipl_info.byteType = 10;
571 ipl_info.byteType = 2;
575 ipl_info.z = (unsigned int) GetImageListLength(image);
576 /* There is no current method for detecting whether we have T or Z stacks */
578 ipl_info.width = (unsigned int) image->columns;
579 ipl_info.height = (unsigned int) image->rows;
581 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
582 (void) TransformImageColorspace(image,sRGBColorspace,exception);
584 if(IssRGBCompatibleColorspace(image->colorspace) == MagickTrue) { ipl_info.colors = 3; }
585 else{ ipl_info.colors = 1; }
587 ipl_info.size = (unsigned int) (28 +
588 ((image->depth)/8)*ipl_info.height*ipl_info.width*ipl_info.colors*ipl_info.z);
590 /* Ok! Calculations are done. Lets write this puppy down! */
595 /* Shockingly (maybe not if you have used IPLab), IPLab itself CANNOT read MSBEndian
596 files! The reader above can, but they cannot. For compatability reasons, I will leave
597 the code in here, but it is all but useless if you want to use IPLab. */
599 if(image_info->endian == MSBEndian)
600 (void) WriteBlob(image, 4, (const unsigned char *) "mmmm");
602 image->endian = LSBEndian;
603 (void) WriteBlob(image, 4, (const unsigned char *) "iiii");
605 (void) WriteBlobLong(image, 4);
606 (void) WriteBlob(image, 4, (const unsigned char *) "100f");
607 (void) WriteBlob(image, 4, (const unsigned char *) "data");
608 (void) WriteBlobLong(image, ipl_info.size);
609 (void) WriteBlobLong(image, ipl_info.width);
610 (void) WriteBlobLong(image, ipl_info.height);
611 (void) WriteBlobLong(image, ipl_info.colors);
612 if(image_info->adjoin == MagickFalse)
613 (void) WriteBlobLong(image, 1);
615 (void) WriteBlobLong(image, ipl_info.z);
616 (void) WriteBlobLong(image, ipl_info.time);
617 (void) WriteBlobLong(image, ipl_info.byteType);
622 Convert MIFF to IPL raster pixels.
624 pixels=GetQuantumPixels(quantum_info);
625 if(ipl_info.colors == 1){
627 for(y = 0; y < (ssize_t) ipl_info.height; y++){
628 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
629 if (p == (const Quantum *) NULL)
631 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
632 GrayQuantum, pixels,exception);
633 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
637 if(ipl_info.colors == 3){
639 for(y = 0; y < (ssize_t) ipl_info.height; y++){
640 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
641 if (p == (const Quantum *) NULL)
643 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
644 RedQuantum, pixels,exception);
645 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
649 for(y = 0; y < (ssize_t) ipl_info.height; y++){
650 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
651 if (p == (const Quantum *) NULL)
653 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
654 GreenQuantum, pixels,exception);
655 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
658 for(y = 0; y < (ssize_t) ipl_info.height; y++){
659 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
660 if (p == (const Quantum *) NULL)
662 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
663 BlueQuantum, pixels,exception);
664 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
665 if (image->previous == (Image *) NULL)
667 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
669 if (status == MagickFalse)
674 quantum_info=DestroyQuantumInfo(quantum_info);
675 if (GetNextImageInList(image) == (Image *) NULL)
677 image=SyncNextImageInList(image);
678 status=SetImageProgress(image,SaveImagesTag,scene++,
679 GetImageListLength(image));
680 if (status == MagickFalse)
682 }while (image_info->adjoin != MagickFalse);
684 (void) WriteBlob(image, 4, (const unsigned char *) "fini");
685 (void) WriteBlobLong(image, 0);