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);
225 if((LocaleNCompare((char *) magick,"iiii",4) == 0))
226 image->endian=LSBEndian;
228 if((LocaleNCompare((char *) magick,"mmmm",4) == 0))
229 image->endian=MSBEndian;
231 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
234 /* Skip o'er the next 8 bytes (garbage) */
235 count=ReadBlob(image, 8, magick);
237 Excellent, now we read the header unimpeded.
239 count=ReadBlob(image,4,magick);
240 if((LocaleNCompare((char *) magick,"data",4) != 0))
241 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
242 ipl_info.size=ReadBlobLong(image);
243 ipl_info.width=ReadBlobLong(image);
244 ipl_info.height=ReadBlobLong(image);
245 if((ipl_info.width == 0UL) || (ipl_info.height == 0UL))
246 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
247 ipl_info.colors=ReadBlobLong(image);
248 if(ipl_info.colors == 3){ image->colorspace=RGBColorspace;}
249 else { image->colorspace = GRAYColorspace; }
250 ipl_info.z=ReadBlobLong(image);
251 ipl_info.time=ReadBlobLong(image);
253 ipl_info.byteType=ReadBlobLong(image);
256 /* Initialize Quantum Info */
258 switch (ipl_info.byteType) {
261 quantum_format = UnsignedQuantumFormat;
265 quantum_format = SignedQuantumFormat;
269 quantum_format = UnsignedQuantumFormat;
273 quantum_format = SignedQuantumFormat;
275 case 4: ipl_info.depth=32;
276 quantum_format = FloatingPointQuantumFormat;
280 quantum_format = UnsignedQuantumFormat;
284 quantum_format = UnsignedQuantumFormat;
288 quantum_format = FloatingPointQuantumFormat;
292 quantum_format = UnsignedQuantumFormat;
297 Set number of scenes of image
300 SetHeaderFromIPL(image, &ipl_info);
302 /* Thats all we need if we are pinging. */
303 if (image_info->ping != MagickFalse)
305 (void) CloseBlob(image);
306 return(GetFirstImageInList(image));
308 length=image->columns;
309 quantum_type=GetQuantumType(image,exception);
312 SetHeaderFromIPL(image, &ipl_info);
314 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
315 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
318 printf("Length: %.20g, Memory size: %.20g\n", (double) length,(double)
321 quantum_info=AcquireQuantumInfo(image_info,image);
322 if (quantum_info == (QuantumInfo *) NULL)
323 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
324 status=SetQuantumFormat(image,quantum_info,quantum_format);
325 if (status == MagickFalse)
326 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
327 pixels=GetQuantumPixels(quantum_info);
328 if(image->columns != ipl_info.width){
330 printf("Columns not set correctly! Wanted: %.20g, got: %.20g\n",
331 (double) ipl_info.width, (double) image->columns);
336 Covert IPL binary to pixel packets
339 if(ipl_info.colors == 1){
340 for(y = 0; y < (ssize_t) image->rows; y++){
341 (void) ReadBlob(image, length*image->depth/8, pixels);
342 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
343 if (q == (PixelPacket *) NULL)
345 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
346 GrayQuantum,pixels,exception);
347 if (SyncAuthenticPixels(image,exception) == MagickFalse)
352 for(y = 0; y < (ssize_t) image->rows; y++){
353 (void) ReadBlob(image, length*image->depth/8, pixels);
354 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
355 if (q == (PixelPacket *) NULL)
357 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
358 RedQuantum,pixels,exception);
359 if (SyncAuthenticPixels(image,exception) == MagickFalse)
362 for(y = 0; y < (ssize_t) image->rows; y++){
363 (void) ReadBlob(image, length*image->depth/8, pixels);
364 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
365 if (q == (PixelPacket *) NULL)
367 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
368 GreenQuantum,pixels,exception);
369 if (SyncAuthenticPixels(image,exception) == MagickFalse)
372 for(y = 0; y < (ssize_t) image->rows; y++){
373 (void) ReadBlob(image, length*image->depth/8, pixels);
374 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
375 if (q == (PixelPacket *) NULL)
377 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
378 BlueQuantum,pixels,exception);
379 if (SyncAuthenticPixels(image,exception) == MagickFalse)
383 SetQuantumImageType(image,quantum_type);
386 quantum_info = DestroyQuantumInfo(quantum_info);
388 if (EOFBlob(image) != MagickFalse)
390 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
394 if(t_count < ipl_info.z * ipl_info.time){
396 Proceed to next image.
398 AcquireNextImage(image_info, image);
399 if (GetNextImageInList(image) == (Image *) NULL)
401 image=DestroyImageList(image);
402 return((Image *) NULL);
404 image=SyncNextImageInList(image);
405 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
407 if (status == MagickFalse)
410 } while (t_count < ipl_info.z*ipl_info.time);
412 return(GetFirstImageInList(image));
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 % R e g i s t e r I P L I m a g e %
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 % RegisterIPLImage() add attributes for the Scanalytics IPL image format to the
427 % list of supported formats.
431 ModuleExport size_t RegisterIPLImage(void)
436 entry=SetMagickInfo("IPL");
437 entry->decoder=(DecodeImageHandler *) ReadIPLImage;
438 entry->encoder=(EncodeImageHandler *) WriteIPLImage;
439 entry->magick=(IsImageFormatHandler *) IsIPL;
440 entry->adjoin=MagickTrue;
441 entry->description=ConstantString("IPL Image Sequence");
442 entry->module=ConstantString("IPL");
443 entry->endian_support=MagickTrue;
444 (void) RegisterMagickInfo(entry);
445 return(MagickImageCoderSignature);
449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 % U n r e g i s t e r I P L I m a g e %
457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 % UnregisterIPLImage() removes format registrations made by the
460 % IPL module from the list of supported formats.
462 % The format of the UnregisterIPLImage method is:
464 % UnregisterIPLImage(void)
467 ModuleExport void UnregisterIPLImage(void)
469 (void) UnregisterMagickInfo("IPL");
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % W r i t e I P L I m a g e %
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483 % WriteIPLImage() writes an image to a file in Scanalytics IPLabimage format.
485 % The format of the WriteIPLImage method is:
487 % MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
489 % A description of each parameter follows.
491 % o image_info: The image info.
493 % o image: The image.
497 static MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
508 register const PixelPacket
524 Open output image file.
526 assert(image_info != (const ImageInfo *) NULL);
527 assert(image_info->signature == MagickSignature);
528 assert(image != (Image *) NULL);
529 assert(image->signature == MagickSignature);
530 if (image->debug != MagickFalse)
531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
532 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
533 if (status == MagickFalse)
538 quantum_info=AcquireQuantumInfo(image_info, image);
539 if ((quantum_info->format == UndefinedQuantumFormat) &&
540 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
541 SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
542 switch(quantum_info->depth){
544 ipl_info.byteType = 0;
547 if(quantum_info->format == SignedQuantumFormat){
548 ipl_info.byteType = 2;
551 ipl_info.byteType = 1;
555 if(quantum_info->format == FloatingPointQuantumFormat){
556 ipl_info.byteType = 3;
559 ipl_info.byteType = 4;
563 ipl_info.byteType = 10;
566 ipl_info.byteType = 2;
570 ipl_info.z = (unsigned int) GetImageListLength(image);
571 /* There is no current method for detecting whether we have T or Z stacks */
573 ipl_info.width = (unsigned int) image->columns;
574 ipl_info.height = (unsigned int) image->rows;
576 if (image->colorspace != RGBColorspace)
577 (void) TransformImageColorspace(image,RGBColorspace);
579 if(image->colorspace == RGBColorspace) { ipl_info.colors = 3; }
580 else{ ipl_info.colors = 1; }
582 ipl_info.size = (unsigned int) (28 +
583 ((image->depth)/8)*ipl_info.height*ipl_info.width*ipl_info.colors*ipl_info.z);
585 /* Ok! Calculations are done. Lets write this puppy down! */
590 /* Shockingly (maybe not if you have used IPLab), IPLab itself CANNOT read MSBEndian
591 files! The reader above can, but they cannot. For compatability reasons, I will leave
592 the code in here, but it is all but useless if you want to use IPLab. */
594 if(image_info->endian == MSBEndian)
595 (void) WriteBlob(image, 4, (const unsigned char *) "mmmm");
597 image->endian = LSBEndian;
598 (void) WriteBlob(image, 4, (const unsigned char *) "iiii");
600 (void) WriteBlobLong(image, 4);
601 (void) WriteBlob(image, 4, (const unsigned char *) "100f");
602 (void) WriteBlob(image, 4, (const unsigned char *) "data");
603 (void) WriteBlobLong(image, ipl_info.size);
604 (void) WriteBlobLong(image, ipl_info.width);
605 (void) WriteBlobLong(image, ipl_info.height);
606 (void) WriteBlobLong(image, ipl_info.colors);
607 if(image_info->adjoin == MagickFalse)
608 (void) WriteBlobLong(image, 1);
610 (void) WriteBlobLong(image, ipl_info.z);
611 (void) WriteBlobLong(image, ipl_info.time);
612 (void) WriteBlobLong(image, ipl_info.byteType);
614 exception=(&image->exception);
618 Convert MIFF to IPL raster pixels.
620 pixels=GetQuantumPixels(quantum_info);
621 if(ipl_info.colors == 1){
623 for(y = 0; y < (ssize_t) ipl_info.height; y++){
624 p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
625 if (p == (PixelPacket *) NULL)
627 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
628 GrayQuantum, pixels,&image->exception);
629 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
633 if(ipl_info.colors == 3){
635 for(y = 0; y < (ssize_t) ipl_info.height; y++){
636 p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
637 if (p == (PixelPacket *) NULL)
639 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
640 RedQuantum, pixels,&image->exception);
641 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
645 for(y = 0; y < (ssize_t) ipl_info.height; y++){
646 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
647 if (p == (PixelPacket *) NULL)
649 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
650 GreenQuantum, pixels,&image->exception);
651 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
654 for(y = 0; y < (ssize_t) ipl_info.height; y++){
655 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
656 if (p == (PixelPacket *) NULL)
658 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
659 BlueQuantum, pixels,&image->exception);
660 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
661 if (image->previous == (Image *) NULL)
663 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
665 if (status == MagickFalse)
670 quantum_info=DestroyQuantumInfo(quantum_info);
671 if (GetNextImageInList(image) == (Image *) NULL)
673 image=SyncNextImageInList(image);
674 status=SetImageProgress(image,SaveImagesTag,scene++,
675 GetImageListLength(image));
676 if (status == MagickFalse)
678 }while (image_info->adjoin != MagickFalse);
680 (void) WriteBlob(image, 4, (const unsigned char *) "fini");
681 (void) WriteBlobLong(image, 0);