2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % IIIIIIIIII PPPPPPPP LL %
13 % IIIIIIIIII PP LLLLLLLL %
17 % Read/Write Scanalytics IPLab Image Format %
22 % Copyright 1999-2007 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 "magick/studio.h"
45 #include "magick/blob.h"
46 #include "magick/blob-private.h"
47 #include "magick/cache.h"
48 #include "magick/colorspace.h"
49 #include "magick/exception.h"
50 #include "magick/exception-private.h"
51 #include "magick/image.h"
52 #include "magick/image-private.h"
53 #include "magick/list.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/option.h"
59 #include "magick/property.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/string_.h"
63 #include "magick/module.h"
69 typedef struct _IPLInfo
83 static MagickBooleanType
84 WriteIPLImage(const ImageInfo *,Image *);
86 void increase (void *pixel, int byteType){
88 case 0:(*((unsigned char *) pixel))++; break;
89 case 1:(*((signed int *) pixel))++; break;
90 case 2:(*((unsigned int *) pixel))++; break;
91 case 3:(*((signed long *) pixel))++; break;
92 default:(*((unsigned int *) pixel))++; break;
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % IsIPL() returns MagickTrue if the image format type, identified by the
108 % magick string, is IPL.
110 % The format of the IsIPL method is:
112 % MagickBooleanType IsIPL(const unsigned char *magick,const size_t length)
114 % A description of each parameter follows:
116 % o magick: compare image format pattern against these bytes.
118 % o length: Specifies the length of the magick string.
121 static MagickBooleanType IsIPL(const unsigned char *magick,const size_t length)
125 if (LocaleNCompare((const char *) magick,"data",4) == 0)
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 % R e a d I P L I m a g e %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % ReadIPLImage() reads a Scanalytics IPLab image file and returns it. It
142 % allocates the memory necessary for the new Image structure and returns a
143 % pointer to the new image.
145 % According to the IPLab spec, the data is blocked out in five dimensions:
146 % { t, z, c, y, x }. When we return the image, the latter three are folded
147 % into the standard "Image" structure. The "scenes" (image_info->scene)
148 % correspond to the order: { {t0,z0}, {t0, z1}, ..., {t1,z0}, {t1,z1}... }
149 % The number of scenes is t*z.
151 % The format of the ReadIPLImage method is:
153 % Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception)
155 % A description of each parameter follows:
157 % o image_info: The image info.
159 % o exception: return any errors or warnings in this structure.
163 void SetHeaderFromIPL(Image *image, IPLInfo *ipl){
164 image->columns = ipl->width;
165 image->rows = ipl->height;
166 image->depth = ipl->depth;
167 image->x_resolution = 1;
168 image->y_resolution = 1;
172 static Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception)
180 MagickBooleanType status;
181 register PixelPacket *q;
182 unsigned char magick[12], *pixels;
200 assert(image_info != (const ImageInfo *) NULL);
201 assert(image_info->signature == MagickSignature);
202 if ( image_info->debug != MagickFalse)
203 (void) LogMagickEvent(TraceEvent, GetMagickModule(), "%s",
204 image_info->filename);
205 assert(exception != (ExceptionInfo *) NULL);
206 assert(exception->signature == MagickSignature);
207 image=AcquireImage(image_info);
208 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
209 if (status == MagickFalse)
211 image=DestroyImageList(image);
212 return((Image *) NULL);
221 If we get back "iiii", we have LSB,"mmmm", MSB
223 count=ReadBlob(image,4,magick);
224 if((LocaleNCompare((char *) magick,"iiii",4) == 0))
225 image->endian=LSBEndian;
227 if((LocaleNCompare((char *) magick,"mmmm",4) == 0))
228 image->endian=MSBEndian;
230 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
233 /* Skip o'er the next 8 bytes (garbage) */
234 count=ReadBlob(image, 8, magick);
236 Excellent, now we read the header unimpeded.
238 count=ReadBlob(image,4,magick);
239 if((LocaleNCompare((char *) magick,"data",4) != 0))
240 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
241 ipl_info.size=ReadBlobLong(image);
242 ipl_info.width=ReadBlobLong(image);
243 ipl_info.height=ReadBlobLong(image);
244 if((ipl_info.width == 0UL) || (ipl_info.height == 0UL))
245 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
246 ipl_info.colors=ReadBlobLong(image);
247 if(ipl_info.colors == 3){ image->colorspace=RGBColorspace;}
248 else { image->colorspace = GRAYColorspace; }
249 ipl_info.z=ReadBlobLong(image);
250 ipl_info.time=ReadBlobLong(image);
252 ipl_info.byteType=ReadBlobLong(image);
255 /* Initialize Quantum Info */
257 switch (ipl_info.byteType) {
260 quantum_format = UnsignedQuantumFormat;
264 quantum_format = SignedQuantumFormat;
268 quantum_format = UnsignedQuantumFormat;
272 quantum_format = SignedQuantumFormat;
274 case 4: ipl_info.depth=32;
275 quantum_format = FloatingPointQuantumFormat;
279 quantum_format = UnsignedQuantumFormat;
283 quantum_format = UnsignedQuantumFormat;
287 quantum_format = FloatingPointQuantumFormat;
291 quantum_format = UnsignedQuantumFormat;
296 Set number of scenes of image
299 SetHeaderFromIPL(image, &ipl_info);
301 /* Thats all we need if we are pinging. */
302 if (image_info->ping != MagickFalse)
304 (void) CloseBlob(image);
305 return(GetFirstImageInList(image));
307 length=image->columns;
308 quantum_type=GetQuantumType(image,exception);
311 SetHeaderFromIPL(image, &ipl_info);
313 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
314 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
317 printf("Length: %.20g, Memory size: %.20g\n", (double) length,(double)
320 quantum_info=AcquireQuantumInfo(image_info,image);
321 if (quantum_info == (QuantumInfo *) NULL)
322 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
323 status=SetQuantumFormat(image,quantum_info,quantum_format);
324 if (status == MagickFalse)
325 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
326 pixels=GetQuantumPixels(quantum_info);
327 if(image->columns != ipl_info.width){
329 printf("Columns not set correctly! Wanted: %.20g, got: %.20g\n",
330 (double) ipl_info.width, (double) image->columns);
335 Covert IPL binary to pixel packets
338 if(ipl_info.colors == 1){
339 for(y = 0; y < (ssize_t) image->rows; y++){
340 (void) ReadBlob(image, length*image->depth/8, pixels);
341 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
342 if (q == (PixelPacket *) NULL)
344 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
345 GrayQuantum,pixels,exception);
346 if (SyncAuthenticPixels(image,exception) == MagickFalse)
351 for(y = 0; y < (ssize_t) image->rows; y++){
352 (void) ReadBlob(image, length*image->depth/8, pixels);
353 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
354 if (q == (PixelPacket *) NULL)
356 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
357 RedQuantum,pixels,exception);
358 if (SyncAuthenticPixels(image,exception) == MagickFalse)
361 for(y = 0; y < (ssize_t) image->rows; y++){
362 (void) ReadBlob(image, length*image->depth/8, pixels);
363 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
364 if (q == (PixelPacket *) NULL)
366 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
367 GreenQuantum,pixels,exception);
368 if (SyncAuthenticPixels(image,exception) == MagickFalse)
371 for(y = 0; y < (ssize_t) image->rows; y++){
372 (void) ReadBlob(image, length*image->depth/8, pixels);
373 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
374 if (q == (PixelPacket *) NULL)
376 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
377 BlueQuantum,pixels,exception);
378 if (SyncAuthenticPixels(image,exception) == MagickFalse)
382 SetQuantumImageType(image,quantum_type);
385 quantum_info = DestroyQuantumInfo(quantum_info);
387 if (EOFBlob(image) != MagickFalse)
389 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
393 if(t_count < ipl_info.z * ipl_info.time){
395 Proceed to next image.
397 AcquireNextImage(image_info, image);
398 if (GetNextImageInList(image) == (Image *) NULL)
400 image=DestroyImageList(image);
401 return((Image *) NULL);
403 image=SyncNextImageInList(image);
404 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
406 if (status == MagickFalse)
409 } while (t_count < ipl_info.z*ipl_info.time);
411 return(GetFirstImageInList(image));
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 % R e g i s t e r I P L I m a g e %
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 % RegisterIPLImage() add attributes for the Scanalytics IPL image format to the
426 % list of supported formats.
430 ModuleExport size_t RegisterIPLImage(void)
435 entry=SetMagickInfo("IPL");
436 entry->decoder=(DecodeImageHandler *) ReadIPLImage;
437 entry->encoder=(EncodeImageHandler *) WriteIPLImage;
438 entry->magick=(IsImageFormatHandler *) IsIPL;
439 entry->adjoin=MagickTrue;
440 entry->description=ConstantString("IPL Image Sequence");
441 entry->module=ConstantString("IPL");
442 entry->endian_support=MagickTrue;
443 (void) RegisterMagickInfo(entry);
444 return(MagickImageCoderSignature);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % U n r e g i s t e r I P L I m a g e %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % UnregisterIPLImage() removes format registrations made by the
459 % IPL module from the list of supported formats.
461 % The format of the UnregisterIPLImage method is:
463 % UnregisterIPLImage(void)
466 ModuleExport void UnregisterIPLImage(void)
468 (void) UnregisterMagickInfo("IPL");
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 % W r i t e I P L I m a g e %
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 % WriteIPLImage() writes an image to a file in Scanalytics IPLabimage format.
484 % The format of the WriteIPLImage method is:
486 % MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
488 % A description of each parameter follows.
490 % o image_info: The image info.
492 % o image: The image.
496 static MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
507 register const PixelPacket
523 Open output image file.
525 assert(image_info != (const ImageInfo *) NULL);
526 assert(image_info->signature == MagickSignature);
527 assert(image != (Image *) NULL);
528 assert(image->signature == MagickSignature);
529 if (image->debug != MagickFalse)
530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
531 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
532 if (status == MagickFalse)
537 quantum_info=AcquireQuantumInfo(image_info, image);
538 if ((quantum_info->format == UndefinedQuantumFormat) &&
539 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
540 SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
541 switch(quantum_info->depth){
543 ipl_info.byteType = 0;
546 if(quantum_info->format == SignedQuantumFormat){
547 ipl_info.byteType = 2;
550 ipl_info.byteType = 1;
554 if(quantum_info->format == FloatingPointQuantumFormat){
555 ipl_info.byteType = 3;
558 ipl_info.byteType = 4;
562 ipl_info.byteType = 10;
565 ipl_info.byteType = 2;
569 ipl_info.z = (unsigned int) GetImageListLength(image);
570 /* There is no current method for detecting whether we have T or Z stacks */
572 ipl_info.width = (unsigned int) image->columns;
573 ipl_info.height = (unsigned int) image->rows;
575 if (image->colorspace != RGBColorspace)
576 (void) TransformImageColorspace(image,RGBColorspace);
578 if(image->colorspace == RGBColorspace) { ipl_info.colors = 3; }
579 else{ ipl_info.colors = 1; }
581 ipl_info.size = (unsigned int) (28 +
582 ((image->depth)/8)*ipl_info.height*ipl_info.width*ipl_info.colors*ipl_info.z);
584 /* Ok! Calculations are done. Lets write this puppy down! */
589 /* Shockingly (maybe not if you have used IPLab), IPLab itself CANNOT read MSBEndian
590 files! The reader above can, but they cannot. For compatability reasons, I will leave
591 the code in here, but it is all but useless if you want to use IPLab. */
593 if(image_info->endian == MSBEndian)
594 (void) WriteBlob(image, 4, (const unsigned char *) "mmmm");
596 image->endian = LSBEndian;
597 (void) WriteBlob(image, 4, (const unsigned char *) "iiii");
599 (void) WriteBlobLong(image, 4);
600 (void) WriteBlob(image, 4, (const unsigned char *) "100f");
601 (void) WriteBlob(image, 4, (const unsigned char *) "data");
602 (void) WriteBlobLong(image, ipl_info.size);
603 (void) WriteBlobLong(image, ipl_info.width);
604 (void) WriteBlobLong(image, ipl_info.height);
605 (void) WriteBlobLong(image, ipl_info.colors);
606 if(image_info->adjoin == MagickFalse)
607 (void) WriteBlobLong(image, 1);
609 (void) WriteBlobLong(image, ipl_info.z);
610 (void) WriteBlobLong(image, ipl_info.time);
611 (void) WriteBlobLong(image, ipl_info.byteType);
613 exception=(&image->exception);
617 Convert MIFF to IPL raster pixels.
619 pixels=GetQuantumPixels(quantum_info);
620 if(ipl_info.colors == 1){
622 for(y = 0; y < (ssize_t) ipl_info.height; y++){
623 p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
624 if (p == (PixelPacket *) NULL)
626 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
627 GrayQuantum, pixels,&image->exception);
628 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
632 if(ipl_info.colors == 3){
634 for(y = 0; y < (ssize_t) ipl_info.height; y++){
635 p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
636 if (p == (PixelPacket *) NULL)
638 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
639 RedQuantum, pixels,&image->exception);
640 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
644 for(y = 0; y < (ssize_t) ipl_info.height; y++){
645 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
646 if (p == (PixelPacket *) NULL)
648 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
649 GreenQuantum, pixels,&image->exception);
650 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
653 for(y = 0; y < (ssize_t) ipl_info.height; y++){
654 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
655 if (p == (PixelPacket *) NULL)
657 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
658 BlueQuantum, pixels,&image->exception);
659 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
660 if (image->previous == (Image *) NULL)
662 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
664 if (status == MagickFalse)
669 quantum_info=DestroyQuantumInfo(quantum_info);
670 if (GetNextImageInList(image) == (Image *) NULL)
672 image=SyncNextImageInList(image);
673 status=SetImageProgress(image,SaveImagesTag,scene++,
674 GetImageListLength(image));
675 if (status == MagickFalse)
677 }while (image_info->adjoin != MagickFalse);
679 (void) WriteBlob(image, 4, (const unsigned char *) "fini");
680 (void) WriteBlobLong(image, 0);