2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % IIIIIIIIII PPPPPPPP LL %
13 % IIIIIIIIII PP LLLLLLLL %
17 % Read/Write Scanalytics IPLab Image Format %
22 % Copyright 1999-2012 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 *);
87 void increase (void *pixel, int byteType){
89 case 0:(*((unsigned char *) pixel))++; break;
90 case 1:(*((signed int *) pixel))++; break;
91 case 2:(*((unsigned int *) pixel))++; break;
92 case 3:(*((signed long *) pixel))++; break;
93 default:(*((unsigned int *) pixel))++; break;
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % IsIPL() returns MagickTrue if the image format type, identified by the
109 % magick string, is IPL.
111 % The format of the IsIPL method is:
113 % MagickBooleanType IsIPL(const unsigned char *magick,const size_t length)
115 % A description of each parameter follows:
117 % o magick: compare image format pattern against these bytes.
119 % o length: Specifies the length of the magick string.
122 static MagickBooleanType IsIPL(const unsigned char *magick,const size_t length)
126 if (LocaleNCompare((const char *) magick,"data",4) == 0)
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 % R e a d I P L I m a g e %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % ReadIPLImage() reads a Scanalytics IPLab image file and returns it. It
143 % allocates the memory necessary for the new Image structure and returns a
144 % pointer to the new image.
146 % According to the IPLab spec, the data is blocked out in five dimensions:
147 % { t, z, c, y, x }. When we return the image, the latter three are folded
148 % into the standard "Image" structure. The "scenes" (image_info->scene)
149 % correspond to the order: { {t0,z0}, {t0, z1}, ..., {t1,z0}, {t1,z1}... }
150 % The number of scenes is t*z.
152 % The format of the ReadIPLImage method is:
154 % Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception)
156 % A description of each parameter follows:
158 % o image_info: The image info.
160 % o exception: return any errors or warnings in this structure.
164 void SetHeaderFromIPL(Image *image, IPLInfo *ipl){
165 image->columns = ipl->width;
166 image->rows = ipl->height;
167 image->depth = ipl->depth;
168 image->resolution.x = 1;
169 image->resolution.y = 1;
173 static Image *ReadIPLImage(const ImageInfo *image_info,ExceptionInfo *exception)
181 MagickBooleanType status;
183 unsigned char magick[12], *pixels;
201 assert(image_info != (const ImageInfo *) NULL);
202 assert(image_info->signature == MagickSignature);
203 if ( image_info->debug != MagickFalse)
204 (void) LogMagickEvent(TraceEvent, GetMagickModule(), "%s",
205 image_info->filename);
206 assert(exception != (ExceptionInfo *) NULL);
207 assert(exception->signature == MagickSignature);
208 image=AcquireImage(image_info,exception);
209 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
210 if (status == MagickFalse)
212 image=DestroyImageList(image);
213 return((Image *) NULL);
222 If we get back "iiii", we have LSB,"mmmm", MSB
224 count=ReadBlob(image,4,magick);
226 if((LocaleNCompare((char *) magick,"iiii",4) == 0))
227 image->endian=LSBEndian;
229 if((LocaleNCompare((char *) magick,"mmmm",4) == 0))
230 image->endian=MSBEndian;
232 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
235 /* Skip o'er the next 8 bytes (garbage) */
236 count=ReadBlob(image, 8, magick);
238 Excellent, now we read the header unimpeded.
240 count=ReadBlob(image,4,magick);
241 if((LocaleNCompare((char *) magick,"data",4) != 0))
242 ThrowReaderException(CorruptImageError, "ImproperImageHeader");
243 ipl_info.size=ReadBlobLong(image);
244 ipl_info.width=ReadBlobLong(image);
245 ipl_info.height=ReadBlobLong(image);
246 if((ipl_info.width == 0UL) || (ipl_info.height == 0UL))
247 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
248 ipl_info.colors=ReadBlobLong(image);
249 if(ipl_info.colors == 3){ image->colorspace=RGBColorspace;}
250 else { image->colorspace = GRAYColorspace; }
251 ipl_info.z=ReadBlobLong(image);
252 ipl_info.time=ReadBlobLong(image);
254 ipl_info.byteType=ReadBlobLong(image);
257 /* Initialize Quantum Info */
259 switch (ipl_info.byteType) {
262 quantum_format = UnsignedQuantumFormat;
266 quantum_format = SignedQuantumFormat;
270 quantum_format = UnsignedQuantumFormat;
274 quantum_format = SignedQuantumFormat;
276 case 4: ipl_info.depth=32;
277 quantum_format = FloatingPointQuantumFormat;
281 quantum_format = UnsignedQuantumFormat;
285 quantum_format = UnsignedQuantumFormat;
289 quantum_format = FloatingPointQuantumFormat;
293 quantum_format = UnsignedQuantumFormat;
298 Set number of scenes of image
301 SetHeaderFromIPL(image, &ipl_info);
303 /* Thats all we need if we are pinging. */
304 if (image_info->ping != MagickFalse)
306 (void) CloseBlob(image);
307 return(GetFirstImageInList(image));
309 length=image->columns;
310 quantum_type=GetQuantumType(image,exception);
313 SetHeaderFromIPL(image, &ipl_info);
315 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
316 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
319 printf("Length: %.20g, Memory size: %.20g\n", (double) length,(double)
322 quantum_info=AcquireQuantumInfo(image_info,image);
323 if (quantum_info == (QuantumInfo *) NULL)
324 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
325 status=SetQuantumFormat(image,quantum_info,quantum_format);
326 if (status == MagickFalse)
327 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
328 pixels=GetQuantumPixels(quantum_info);
329 if(image->columns != ipl_info.width){
331 printf("Columns not set correctly! Wanted: %.20g, got: %.20g\n",
332 (double) ipl_info.width, (double) image->columns);
337 Covert IPL binary to pixel packets
340 if(ipl_info.colors == 1){
341 for(y = 0; y < (ssize_t) image->rows; y++){
342 (void) ReadBlob(image, length*image->depth/8, pixels);
343 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
344 if (q == (Quantum *) NULL)
346 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
347 GrayQuantum,pixels,exception);
348 if (SyncAuthenticPixels(image,exception) == MagickFalse)
353 for(y = 0; y < (ssize_t) image->rows; y++){
354 (void) ReadBlob(image, length*image->depth/8, pixels);
355 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
356 if (q == (Quantum *) NULL)
358 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
359 RedQuantum,pixels,exception);
360 if (SyncAuthenticPixels(image,exception) == MagickFalse)
363 for(y = 0; y < (ssize_t) image->rows; y++){
364 (void) ReadBlob(image, length*image->depth/8, pixels);
365 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
366 if (q == (Quantum *) NULL)
368 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
369 GreenQuantum,pixels,exception);
370 if (SyncAuthenticPixels(image,exception) == MagickFalse)
373 for(y = 0; y < (ssize_t) image->rows; y++){
374 (void) ReadBlob(image, length*image->depth/8, pixels);
375 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
376 if (q == (Quantum *) NULL)
378 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
379 BlueQuantum,pixels,exception);
380 if (SyncAuthenticPixels(image,exception) == MagickFalse)
384 SetQuantumImageType(image,quantum_type);
387 quantum_info = DestroyQuantumInfo(quantum_info);
389 if (EOFBlob(image) != MagickFalse)
391 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
395 if(t_count < ipl_info.z * ipl_info.time){
397 Proceed to next image.
399 AcquireNextImage(image_info,image,exception);
400 if (GetNextImageInList(image) == (Image *) NULL)
402 image=DestroyImageList(image);
403 return((Image *) NULL);
405 image=SyncNextImageInList(image);
406 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
408 if (status == MagickFalse)
411 } while (t_count < ipl_info.z*ipl_info.time);
413 return(GetFirstImageInList(image));
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 % R e g i s t e r I P L I m a g e %
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % RegisterIPLImage() add attributes for the Scanalytics IPL image format to the
428 % list of supported formats.
432 ModuleExport size_t RegisterIPLImage(void)
437 entry=SetMagickInfo("IPL");
438 entry->decoder=(DecodeImageHandler *) ReadIPLImage;
439 entry->encoder=(EncodeImageHandler *) WriteIPLImage;
440 entry->magick=(IsImageFormatHandler *) IsIPL;
441 entry->adjoin=MagickTrue;
442 entry->description=ConstantString("IPL Image Sequence");
443 entry->module=ConstantString("IPL");
444 entry->endian_support=MagickTrue;
445 (void) RegisterMagickInfo(entry);
446 return(MagickImageCoderSignature);
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % U n r e g i s t e r I P L I m a g e %
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % UnregisterIPLImage() removes format registrations made by the
461 % IPL module from the list of supported formats.
463 % The format of the UnregisterIPLImage method is:
465 % UnregisterIPLImage(void)
468 ModuleExport void UnregisterIPLImage(void)
470 (void) UnregisterMagickInfo("IPL");
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 % W r i t e I P L I m a g e %
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % WriteIPLImage() writes an image to a file in Scanalytics IPLabimage format.
486 % The format of the WriteIPLImage method is:
488 % MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image)
489 % Image *image,ExceptionInfo *exception)
491 % A description of each parameter follows.
493 % o image_info: The image info.
495 % o image: The image.
497 % o exception: return any errors or warnings in this structure.
500 static MagickBooleanType WriteIPLImage(const ImageInfo *image_info,Image *image,
501 ExceptionInfo *exception)
512 register const Quantum
525 Open output image file.
527 assert(image_info != (const ImageInfo *) NULL);
528 assert(image_info->signature == MagickSignature);
529 assert(image != (Image *) NULL);
530 assert(image->signature == MagickSignature);
531 if (image->debug != MagickFalse)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
533 assert(exception != (ExceptionInfo *) NULL);
534 assert(exception->signature == MagickSignature);
535 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
536 if (status == MagickFalse)
541 quantum_info=AcquireQuantumInfo(image_info, image);
542 if ((quantum_info->format == UndefinedQuantumFormat) &&
543 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
544 SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
545 switch(quantum_info->depth){
547 ipl_info.byteType = 0;
550 if(quantum_info->format == SignedQuantumFormat){
551 ipl_info.byteType = 2;
554 ipl_info.byteType = 1;
558 if(quantum_info->format == FloatingPointQuantumFormat){
559 ipl_info.byteType = 3;
562 ipl_info.byteType = 4;
566 ipl_info.byteType = 10;
569 ipl_info.byteType = 2;
573 ipl_info.z = (unsigned int) GetImageListLength(image);
574 /* There is no current method for detecting whether we have T or Z stacks */
576 ipl_info.width = (unsigned int) image->columns;
577 ipl_info.height = (unsigned int) image->rows;
579 if (IsRGBColorspace(image->colorspace) == MagickFalse)
580 (void) TransformImageColorspace(image,RGBColorspace,exception);
582 if(IsRGBColorspace(image->colorspace) == MagickTrue) { ipl_info.colors = 3; }
583 else{ ipl_info.colors = 1; }
585 ipl_info.size = (unsigned int) (28 +
586 ((image->depth)/8)*ipl_info.height*ipl_info.width*ipl_info.colors*ipl_info.z);
588 /* Ok! Calculations are done. Lets write this puppy down! */
593 /* Shockingly (maybe not if you have used IPLab), IPLab itself CANNOT read MSBEndian
594 files! The reader above can, but they cannot. For compatability reasons, I will leave
595 the code in here, but it is all but useless if you want to use IPLab. */
597 if(image_info->endian == MSBEndian)
598 (void) WriteBlob(image, 4, (const unsigned char *) "mmmm");
600 image->endian = LSBEndian;
601 (void) WriteBlob(image, 4, (const unsigned char *) "iiii");
603 (void) WriteBlobLong(image, 4);
604 (void) WriteBlob(image, 4, (const unsigned char *) "100f");
605 (void) WriteBlob(image, 4, (const unsigned char *) "data");
606 (void) WriteBlobLong(image, ipl_info.size);
607 (void) WriteBlobLong(image, ipl_info.width);
608 (void) WriteBlobLong(image, ipl_info.height);
609 (void) WriteBlobLong(image, ipl_info.colors);
610 if(image_info->adjoin == MagickFalse)
611 (void) WriteBlobLong(image, 1);
613 (void) WriteBlobLong(image, ipl_info.z);
614 (void) WriteBlobLong(image, ipl_info.time);
615 (void) WriteBlobLong(image, ipl_info.byteType);
620 Convert MIFF to IPL raster pixels.
622 pixels=GetQuantumPixels(quantum_info);
623 if(ipl_info.colors == 1){
625 for(y = 0; y < (ssize_t) ipl_info.height; y++){
626 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
627 if (p == (const Quantum *) NULL)
629 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
630 GrayQuantum, pixels,exception);
631 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
635 if(ipl_info.colors == 3){
637 for(y = 0; y < (ssize_t) ipl_info.height; y++){
638 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
639 if (p == (const Quantum *) NULL)
641 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
642 RedQuantum, pixels,exception);
643 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
647 for(y = 0; y < (ssize_t) ipl_info.height; y++){
648 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
649 if (p == (const Quantum *) NULL)
651 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
652 GreenQuantum, pixels,exception);
653 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
656 for(y = 0; y < (ssize_t) ipl_info.height; y++){
657 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
658 if (p == (const Quantum *) NULL)
660 (void) ExportQuantumPixels(image,(CacheView *) NULL, quantum_info,
661 BlueQuantum, pixels,exception);
662 (void) WriteBlob(image, image->columns*image->depth/8, pixels);
663 if (image->previous == (Image *) NULL)
665 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
667 if (status == MagickFalse)
672 quantum_info=DestroyQuantumInfo(quantum_info);
673 if (GetNextImageInList(image) == (Image *) NULL)
675 image=SyncNextImageInList(image);
676 status=SetImageProgress(image,SaveImagesTag,scene++,
677 GetImageListLength(image));
678 if (status == MagickFalse)
680 }while (image_info->adjoin != MagickFalse);
682 (void) WriteBlob(image, 4, (const unsigned char *) "fini");
683 (void) WriteBlobLong(image, 0);