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;
185 unsigned long t_count=0;
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: %lu, Memory size: %lu\n", length,(unsigned long)(image->depth));
319 quantum_info=AcquireQuantumInfo(image_info,image);
320 if (quantum_info == (QuantumInfo *) NULL)
321 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
322 status=SetQuantumFormat(image,quantum_info,quantum_format);
323 if (status == MagickFalse)
324 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
325 pixels=GetQuantumPixels(quantum_info);
326 if(image->columns != ipl_info.width){
328 printf("Columns not set correctly! Wanted: %lu, got: %lu\n",
329 ipl_info.width, image->columns);
334 Covert IPL binary to pixel packets
337 if(ipl_info.colors == 1){
338 for(y = 0; y < (long) image->rows; y++){
339 (void) ReadBlob(image, length*image->depth/8, pixels);
340 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
341 if (q == (PixelPacket *) NULL)
343 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
344 GrayQuantum,pixels,exception);
345 if (SyncAuthenticPixels(image,exception) == MagickFalse)
350 for(y = 0; y < (long) image->rows; y++){
351 (void) ReadBlob(image, length*image->depth/8, pixels);
352 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
353 if (q == (PixelPacket *) NULL)
355 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
356 RedQuantum,pixels,exception);
357 if (SyncAuthenticPixels(image,exception) == MagickFalse)
360 for(y = 0; y < (long) image->rows; y++){
361 (void) ReadBlob(image, length*image->depth/8, pixels);
362 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
363 if (q == (PixelPacket *) NULL)
365 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
366 GreenQuantum,pixels,exception);
367 if (SyncAuthenticPixels(image,exception) == MagickFalse)
370 for(y = 0; y < (long) image->rows; y++){
371 (void) ReadBlob(image, length*image->depth/8, pixels);
372 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
373 if (q == (PixelPacket *) NULL)
375 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
376 BlueQuantum,pixels,exception);
377 if (SyncAuthenticPixels(image,exception) == MagickFalse)
381 SetQuantumImageType(image,quantum_type);
384 quantum_info = DestroyQuantumInfo(quantum_info);
386 if (EOFBlob(image) != MagickFalse)
388 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
392 if(t_count < ipl_info.z * ipl_info.time){
394 Proceed to next image.
396 AcquireNextImage(image_info, image);
397 if (GetNextImageInList(image) == (Image *) NULL)
399 image=DestroyImageList(image);
400 return((Image *) NULL);
402 image=SyncNextImageInList(image);
403 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
405 if (status == MagickFalse)
408 } while (t_count < ipl_info.z*ipl_info.time);
410 return(GetFirstImageInList(image));
414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 % R e g i s t e r I P L I m a g e %
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 % RegisterIPLImage() add attributes for the Scanalytics IPL image format to the
425 % list of supported formats.
429 ModuleExport unsigned long RegisterIPLImage(void)
434 entry=SetMagickInfo("IPL");
435 entry->decoder=(DecodeImageHandler *) ReadIPLImage;
436 entry->encoder=(EncodeImageHandler *) WriteIPLImage;
437 entry->magick=(IsImageFormatHandler *) IsIPL;
438 entry->adjoin=MagickTrue;
439 entry->description=ConstantString("IPL Image Sequence");
440 entry->module=ConstantString("IPL");
441 entry->endian_support=MagickTrue;
442 (void) RegisterMagickInfo(entry);
443 return(MagickImageCoderSignature);
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 % U n r e g i s t e r I P L I m a g e %
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % UnregisterIPLImage() removes format registrations made by the
458 % IPL module from the list of supported formats.
460 % The format of the UnregisterIPLImage method is:
462 % UnregisterIPLImage(void)
465 ModuleExport void UnregisterIPLImage(void)
467 (void) UnregisterMagickInfo("IPL");
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 % W r i t e I P L I m a g e %
479 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 % WriteIPLImage() writes an image to a file in Scanalytics IPLabimage format.
483 % The format of the WriteIPLImage method is:
485 % MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
487 % A description of each parameter follows.
489 % o image_info: The image info.
491 % o image: The image.
495 static MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
506 register const PixelPacket
522 Open output image file.
524 assert(image_info != (const ImageInfo *) NULL);
525 assert(image_info->signature == MagickSignature);
526 assert(image != (Image *) NULL);
527 assert(image->signature == MagickSignature);
528 if (image->debug != MagickFalse)
529 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
530 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
531 if (status == MagickFalse)
536 quantum_info=AcquireQuantumInfo(image_info, image);
537 if ((quantum_info->format == UndefinedQuantumFormat) &&
538 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
539 SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
540 switch(quantum_info->depth){
542 ipl_info.byteType = 0;
545 if(quantum_info->format == SignedQuantumFormat){
546 ipl_info.byteType = 2;
549 ipl_info.byteType = 1;
553 if(quantum_info->format == FloatingPointQuantumFormat){
554 ipl_info.byteType = 3;
557 ipl_info.byteType = 4;
561 ipl_info.byteType = 10;
564 ipl_info.byteType = 2;
568 ipl_info.z = GetImageListLength(image);
569 /* There is no current method for detecting whether we have T or Z stacks */
571 ipl_info.width = image->columns;
572 ipl_info.height = image->rows;
574 if (image->colorspace != RGBColorspace)
575 (void) TransformImageColorspace(image,RGBColorspace);
577 if(image->colorspace == RGBColorspace) { ipl_info.colors = 3; }
578 else{ ipl_info.colors = 1; }
581 ((image->depth)/8)*ipl_info.height*ipl_info.width*ipl_info.colors*ipl_info.z;
583 /* Ok! Calculations are done. Lets write this puppy down! */
588 /* Shockingly (maybe not if you have used IPLab), IPLab itself CANNOT read MSBEndian
589 files! The reader above can, but they cannot. For compatability reasons, I will leave
590 the code in here, but it is all but useless if you want to use IPLab. */
592 if(image_info->endian == MSBEndian)
593 (void) WriteBlob(image, 4, (const unsigned char *) "mmmm");
595 image->endian = LSBEndian;
596 (void) WriteBlob(image, 4, (const unsigned char *) "iiii");
598 (void) WriteBlobLong(image, 4);
599 (void) WriteBlob(image, 4, (const unsigned char *) "100f");
600 (void) WriteBlob(image, 4, (const unsigned char *) "data");
601 (void) WriteBlobLong(image, ipl_info.size);
602 (void) WriteBlobLong(image, ipl_info.width);
603 (void) WriteBlobLong(image, ipl_info.height);
604 (void) WriteBlobLong(image, ipl_info.colors);
605 if(image_info->adjoin == MagickFalse)
606 (void) WriteBlobLong(image, 1);
608 (void) WriteBlobLong(image, ipl_info.z);
609 (void) WriteBlobLong(image, ipl_info.time);
610 (void) WriteBlobLong(image, ipl_info.byteType);
612 exception=(&image->exception);
616 Convert MIFF to IPL raster pixels.
618 pixels=GetQuantumPixels(quantum_info);
619 if(ipl_info.colors == 1){
621 for(y = 0; y < (long) ipl_info.height; y++){
622 p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
623 if (p == (PixelPacket *) NULL)
625 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
626 GrayQuantum, pixels,&image->exception);
627 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
631 if(ipl_info.colors == 3){
633 for(y = 0; y < (long) ipl_info.height; y++){
634 p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
635 if (p == (PixelPacket *) NULL)
637 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
638 RedQuantum, pixels,&image->exception);
639 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
643 for(y = 0; y < (long) ipl_info.height; y++){
644 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
645 if (p == (PixelPacket *) NULL)
647 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
648 GreenQuantum, pixels,&image->exception);
649 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
652 for(y = 0; y < (long) ipl_info.height; y++){
653 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
654 if (p == (PixelPacket *) NULL)
656 (void) ExportQuantumPixels(image,(const CacheView *) NULL, quantum_info,
657 BlueQuantum, pixels,&image->exception);
658 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
659 if (image->previous == (Image *) NULL)
661 status=SetImageProgress(image,SaveImageTag,y,image->rows);
662 if (status == MagickFalse)
667 quantum_info=DestroyQuantumInfo(quantum_info);
668 if (GetNextImageInList(image) == (Image *) NULL)
670 image=SyncNextImageInList(image);
671 status=SetImageProgress(image,SaveImagesTag,scene++,
672 GetImageListLength(image));
673 if (status == MagickFalse)
675 }while (image_info->adjoin != MagickFalse);
677 (void) WriteBlob(image, 4, (const unsigned char *) "fini");
678 (void) WriteBlobLong(image, 0);