2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 % M M AAA TTTTT L AAA BBBB %
6 % MM MM A A T L A A B B %
7 % M M M AAAAA T L AAAAA BBBB %
8 % M M A A T L A A B B %
9 % M M A A T LLLLL A A BBBB %
12 % Read MATLAB Image Format %
19 % Permission is hereby granted, free of charge, to any person obtaining a %
20 % copy of this software and associated documentation files ("ImageMagick"), %
21 % to deal in ImageMagick without restriction, including without limitation %
22 % the rights to use, copy, modify, merge, publish, distribute, sublicense, %
23 % and/or sell copies of ImageMagick, and to permit persons to whom the %
24 % ImageMagick is furnished to do so, subject to the following conditions: %
26 % The above copyright notice and this permission notice shall be included in %
27 % all copies or substantial portions of ImageMagick. %
29 % The software is provided "as is", without warranty of any kind, express or %
30 % implied, including but not limited to the warranties of merchantability, %
31 % fitness for a particular purpose and noninfringement. In no event shall %
32 % ImageMagick Studio be liable for any claim, damages or other liability, %
33 % whether in an action of contract, tort or otherwise, arising from, out of %
34 % or in connection with ImageMagick or the use or other dealings in %
37 % Except as contained in this notice, the name of the ImageMagick Studio %
38 % shall not be used in advertising or otherwise to promote the sale, use or %
39 % other dealings in ImageMagick without prior written authorization from the %
40 % ImageMagick Studio. %
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 #include "MagickCore/studio.h"
51 #include "MagickCore/attribute.h"
52 #include "MagickCore/blob.h"
53 #include "MagickCore/blob-private.h"
54 #include "MagickCore/cache.h"
55 #include "MagickCore/color-private.h"
56 #include "MagickCore/colormap.h"
57 #include "MagickCore/colorspace-private.h"
58 #include "MagickCore/distort.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/image.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/magick.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/monitor.h"
67 #include "MagickCore/monitor-private.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/quantum.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/option.h"
72 #include "MagickCore/pixel.h"
73 #include "MagickCore/resource_.h"
74 #include "MagickCore/static.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/module.h"
77 #include "MagickCore/timer-private.h"
78 #include "MagickCore/transform.h"
79 #include "MagickCore/utility-private.h"
80 #if defined(MAGICKCORE_ZLIB_DELEGATE)
87 static MagickBooleanType
88 WriteMATImage(const ImageInfo *,Image *,ExceptionInfo *);
91 /* Auto coloring method, sorry this creates some artefact inside data
92 MinReal+j*MaxComplex = red MaxReal+j*MaxComplex = black
93 MinReal+j*0 = white MaxReal+j*0 = black
94 MinReal+j*MinComplex = blue MaxReal+j*MinComplex = black
100 unsigned short Version;
101 char EndianIndicator[2];
102 unsigned int DataType;
103 unsigned int ObjectSize;
104 unsigned int unknown1;
105 unsigned int unknown2;
107 unsigned short unknown5;
108 unsigned char StructureFlag;
109 unsigned char StructureClass;
110 unsigned int unknown3;
111 unsigned int unknown4;
112 unsigned int DimFlag;
116 unsigned short Flag1;
117 unsigned short NameFlag;
122 MonthsTab[12][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
125 DayOfWTab[7][4] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
129 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
141 miINT8 = 1, /* 8 bit signed */
142 miUINT8, /* 8 bit unsigned */
143 miINT16, /* 16 bit signed */
144 miUINT16, /* 16 bit unsigned */
145 miINT32, /* 32 bit signed */
146 miUINT32, /* 32 bit unsigned */
147 miSINGLE, /* IEEE 754 single precision float */
149 miDOUBLE, /* IEEE 754 double precision float */
152 miINT64, /* 64 bit signed */
153 miUINT64, /* 64 bit unsigned */
154 miMATRIX, /* MATLAB array */
155 miCOMPRESSED, /* Compressed Data */
156 miUTF8, /* Unicode UTF-8 Encoded Character Data */
157 miUTF16, /* Unicode UTF-16 Encoded Character Data */
158 miUTF32 /* Unicode UTF-32 Encoded Character Data */
163 mxCELL_CLASS=1, /* cell array */
164 mxSTRUCT_CLASS, /* structure */
165 mxOBJECT_CLASS, /* object */
166 mxCHAR_CLASS, /* character array */
167 mxSPARSE_CLASS, /* sparse array */
168 mxDOUBLE_CLASS, /* double precision array */
169 mxSINGLE_CLASS, /* single precision floating point */
170 mxINT8_CLASS, /* 8 bit signed integer */
171 mxUINT8_CLASS, /* 8 bit unsigned integer */
172 mxINT16_CLASS, /* 16 bit signed integer */
173 mxUINT16_CLASS, /* 16 bit unsigned integer */
174 mxINT32_CLASS, /* 32 bit signed integer */
175 mxUINT32_CLASS, /* 32 bit unsigned integer */
176 mxINT64_CLASS, /* 64 bit signed integer */
177 mxUINT64_CLASS, /* 64 bit unsigned integer */
178 mxFUNCTION_CLASS /* Function handle */
181 #define FLAG_COMPLEX 0x8
182 #define FLAG_GLOBAL 0x4
183 #define FLAG_LOGICAL 0x2
185 static const QuantumType z2qtype[4] = {GrayQuantum, BlueQuantum, GreenQuantum, RedQuantum};
187 static void InsertComplexDoubleRow(Image *image,double *p,int y,double MinVal,
188 double MaxVal,ExceptionInfo *exception)
199 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
200 if (q == (Quantum *) NULL)
202 for (x = 0; x < (ssize_t) image->columns; x++)
206 f=(*p/MaxVal)*(Quantum) (QuantumRange-GetPixelRed(image,q));
207 if ((f+GetPixelRed(image,q)) >= QuantumRange)
208 SetPixelRed(image,QuantumRange,q);
210 SetPixelRed(image,GetPixelRed(image,q)+ClampToQuantum(f),q);
211 f=GetPixelGreen(image,q)-f/2.0;
214 SetPixelGreen(image,0,q);
215 SetPixelBlue(image,0,q);
219 SetPixelBlue(image,ClampToQuantum(f),q);
220 SetPixelGreen(image,ClampToQuantum(f),q);
225 f=(*p/MinVal)*(Quantum) (QuantumRange-GetPixelBlue(image,q));
226 if ((f+GetPixelBlue(image,q)) >= QuantumRange)
227 SetPixelBlue(image,QuantumRange,q);
229 SetPixelBlue(image,GetPixelBlue(image,q)+ClampToQuantum(f),q);
230 f=GetPixelGreen(image,q)-f/2.0;
233 SetPixelRed(image,0,q);
234 SetPixelGreen(image,0,q);
238 SetPixelRed(image,ClampToQuantum(f),q);
239 SetPixelGreen(image,ClampToQuantum(f),q);
245 if (!SyncAuthenticPixels(image,exception))
250 static void InsertComplexFloatRow(Image *image,float *p,int y,double MinVal,
251 double MaxVal,ExceptionInfo *exception)
262 q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
263 if (q == (Quantum *) NULL)
265 for (x = 0; x < (ssize_t) image->columns; x++)
269 f=(*p/MaxVal)*(Quantum) (QuantumRange-GetPixelRed(image,q));
270 if ((f+GetPixelRed(image,q)) < QuantumRange)
271 SetPixelRed(image,GetPixelRed(image,q)+ClampToQuantum(f),q);
273 SetPixelRed(image,QuantumRange,q);
275 if (f < GetPixelGreen(image,q))
277 SetPixelBlue(image,GetPixelBlue(image,q)-ClampToQuantum(f),q);
278 SetPixelGreen(image,GetPixelBlue(image,q),q);
282 SetPixelGreen(image,0,q);
283 SetPixelBlue(image,0,q);
288 f=(*p/MaxVal)*(Quantum) (QuantumRange-GetPixelBlue(image,q));
289 if ((f+GetPixelBlue(image,q)) < QuantumRange)
290 SetPixelBlue(image,GetPixelBlue(image,q)+ClampToQuantum(f),q);
292 SetPixelBlue(image,QuantumRange,q);
294 if (f < GetPixelGreen(image,q))
296 SetPixelRed(image,GetPixelRed(image,q)-ClampToQuantum(f),q);
297 SetPixelGreen(image,GetPixelRed(image,q),q);
301 SetPixelGreen(image,0,q);
302 SetPixelRed(image,0,q);
308 if (!SyncAuthenticPixels(image,exception))
314 /************** READERS ******************/
316 /* This function reads one block of floats*/
317 static void ReadBlobFloatsLSB(Image * image, size_t len, float *data)
321 *data++ = ReadBlobFloat(image);
322 len -= sizeof(float);
325 (void) SeekBlob(image, len, SEEK_CUR);
328 static void ReadBlobFloatsMSB(Image * image, size_t len, float *data)
332 *data++ = ReadBlobFloat(image);
333 len -= sizeof(float);
336 (void) SeekBlob(image, len, SEEK_CUR);
339 /* This function reads one block of doubles*/
340 static void ReadBlobDoublesLSB(Image * image, size_t len, double *data)
344 *data++ = ReadBlobDouble(image);
345 len -= sizeof(double);
348 (void) SeekBlob(image, len, SEEK_CUR);
351 static void ReadBlobDoublesMSB(Image * image, size_t len, double *data)
355 *data++ = ReadBlobDouble(image);
356 len -= sizeof(double);
359 (void) SeekBlob(image, len, SEEK_CUR);
362 /* Calculate minimum and maximum from a given block of data */
363 static void CalcMinMax(Image *image, int endian_indicator, int SizeX, int SizeY, size_t CellType, unsigned ldblk, void *BImgBuff, double *Min, double *Max)
365 MagickOffsetType filepos;
367 void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
368 void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
372 if (endian_indicator == LSBEndian)
374 ReadBlobDoublesXXX = ReadBlobDoublesLSB;
375 ReadBlobFloatsXXX = ReadBlobFloatsLSB;
379 ReadBlobDoublesXXX = ReadBlobDoublesMSB;
380 ReadBlobFloatsXXX = ReadBlobFloatsMSB;
383 filepos = TellBlob(image); /* Please note that file seeking occurs only in the case of doubles */
384 for (i = 0; i < SizeY; i++)
386 if (CellType==miDOUBLE)
388 ReadBlobDoublesXXX(image, ldblk, (double *)BImgBuff);
389 dblrow = (double *)BImgBuff;
392 *Min = *Max = *dblrow;
394 for (x = 0; x < SizeX; x++)
403 if (CellType==miSINGLE)
405 ReadBlobFloatsXXX(image, ldblk, (float *)BImgBuff);
406 fltrow = (float *)BImgBuff;
409 *Min = *Max = *fltrow;
411 for (x = 0; x < (ssize_t) SizeX; x++)
421 (void) SeekBlob(image, filepos, SEEK_SET);
425 static void FixSignedValues(const Image *image,Quantum *q, int y)
429 /* Please note that negative values will overflow
430 Q=8; QuantumRange=255: <0;127> + 127+1 = <128; 255>
431 <-1;-128> + 127+1 = <0; 127> */
432 SetPixelRed(image,GetPixelRed(image,q)+QuantumRange/2+1,q);
433 SetPixelGreen(image,GetPixelGreen(image,q)+QuantumRange/2+1,q);
434 SetPixelBlue(image,GetPixelBlue(image,q)+QuantumRange/2+1,q);
440 /** Fix whole row of logical/binary data. It means pack it. */
441 static void FixLogical(unsigned char *Buff,int ldblk)
443 unsigned char mask=128;
444 unsigned char *BuffL = Buff;
445 unsigned char val = 0;
464 #if defined(MAGICKCORE_ZLIB_DELEGATE)
465 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
469 return((voidpf) AcquireQuantumMemory(items,size));
472 static void RelinquishZIPMemory(voidpf context,voidpf memory)
475 memory=RelinquishMagickMemory(memory);
479 #if defined(MAGICKCORE_ZLIB_DELEGATE)
480 /** This procedure decompreses an image block for a new MATLAB format. */
481 static Image *decompress_block(Image *orig, unsigned int *Size, ImageInfo *clone_info, ExceptionInfo *exception)
485 void *cache_block, *decompress_block;
494 ssize_t TotalSize = 0;
496 if(clone_info==NULL) return NULL;
497 if(clone_info->file) /* Close file opened from previous transaction. */
499 fclose(clone_info->file);
500 clone_info->file = NULL;
501 (void) remove_utf8(clone_info->filename);
504 cache_block = AcquireQuantumMemory((size_t)(*Size < MagickMinBufferExtent) ? *Size: MagickMinBufferExtent,sizeof(unsigned char *));
505 if(cache_block==NULL) return NULL;
506 decompress_block = AcquireQuantumMemory((size_t)(4096),sizeof(unsigned char *));
507 if(decompress_block==NULL)
509 RelinquishMagickMemory(cache_block);
514 file = AcquireUniqueFileResource(clone_info->filename);
516 mat_file = fdopen(file,"w");
519 RelinquishMagickMemory(cache_block);
520 RelinquishMagickMemory(decompress_block);
521 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Cannot create file stream for decompressed image");
525 zip_info.zalloc=AcquireZIPMemory;
526 zip_info.zfree=RelinquishZIPMemory;
527 zip_info.opaque = (voidpf) NULL;
528 zip_status = inflateInit(&zip_info);
529 if (zip_status != Z_OK)
531 RelinquishMagickMemory(cache_block);
532 RelinquishMagickMemory(decompress_block);
533 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
534 "UnableToUncompressImage","`%s'",clone_info->filename);
535 (void) fclose(mat_file);
536 RelinquishUniqueFileResource(clone_info->filename);
539 /* zip_info.next_out = 8*4;*/
541 zip_info.avail_in = 0;
542 zip_info.total_out = 0;
543 while(*Size>0 && !EOFBlob(orig))
545 magick_size = ReadBlob(orig, (*Size < MagickMinBufferExtent) ? *Size : MagickMinBufferExtent, (unsigned char *) cache_block);
546 if (magick_size == 0)
548 zip_info.next_in = (Bytef *) cache_block;
549 zip_info.avail_in = (uInt) magick_size;
551 while(zip_info.avail_in>0)
553 zip_info.avail_out = 4096;
554 zip_info.next_out = (Bytef *) decompress_block;
555 zip_status = inflate(&zip_info,Z_NO_FLUSH);
556 if ((zip_status != Z_OK) && (zip_status != Z_STREAM_END))
558 extent=fwrite(decompress_block, 4096-zip_info.avail_out, 1, mat_file);
560 TotalSize += 4096-zip_info.avail_out;
562 if(zip_status == Z_STREAM_END) goto DblBreak;
564 if ((zip_status != Z_OK) && (zip_status != Z_STREAM_END))
567 *Size -= (unsigned int) magick_size;
571 inflateEnd(&zip_info);
572 (void)fclose(mat_file);
573 RelinquishMagickMemory(cache_block);
574 RelinquishMagickMemory(decompress_block);
577 if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL) goto UnlinkFile;
578 if( (image2 = AcquireImage(clone_info,exception))==NULL ) goto EraseFile;
579 status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception);
580 if (status == MagickFalse)
582 DeleteImageFromList(&image2);
584 fclose(clone_info->file);
585 clone_info->file = NULL;
587 RelinquishUniqueFileResource(clone_info->filename);
595 static Image *ReadMATImageV4(const ImageInfo *image_info,Image *image,
596 ExceptionInfo *exception)
599 unsigned char Type[4];
603 unsigned int nameLen;
640 quantum_info=(QuantumInfo *) NULL;
641 (void) SeekBlob(image,0,SEEK_SET);
643 while (EOFBlob(image) == MagickFalse)
648 ldblk=ReadBlobLSBLong(image);
649 if(EOFBlob(image)) break;
650 if ((ldblk > 9999) || (ldblk < 0))
652 HDR.Type[3]=ldblk % 10; ldblk /= 10; /* T digit */
653 HDR.Type[2]=ldblk % 10; ldblk /= 10; /* P digit */
654 HDR.Type[1]=ldblk % 10; ldblk /= 10; /* O digit */
655 HDR.Type[0]=ldblk; /* M digit */
656 if (HDR.Type[3] != 0)
657 break; /* Data format */
658 if (HDR.Type[2] != 0)
659 break; /* Always 0 */
660 if (HDR.Type[0] == 0)
662 HDR.nRows=ReadBlobLSBLong(image);
663 HDR.nCols=ReadBlobLSBLong(image);
664 HDR.imagf=ReadBlobLSBLong(image);
665 HDR.nameLen=ReadBlobLSBLong(image);
670 HDR.nRows=ReadBlobMSBLong(image);
671 HDR.nCols=ReadBlobMSBLong(image);
672 HDR.imagf=ReadBlobMSBLong(image);
673 HDR.nameLen=ReadBlobMSBLong(image);
676 if ((HDR.imagf != 0) && (HDR.imagf != 1))
678 if (HDR.nameLen > 0xFFFF)
679 return(DestroyImageList(image));
680 for (i=0; i < (ssize_t) HDR.nameLen; i++)
688 byte=ReadBlobByte(image);
691 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
696 image->columns=(size_t) HDR.nRows;
697 image->rows=(size_t) HDR.nCols;
698 if ((image->columns == 0) || (image->rows == 0))
699 return(DestroyImageList(image));
700 if (image_info->ping != MagickFalse)
702 Swap(image->columns,image->rows);
703 if(HDR.imagf==1) ldblk *= 2;
704 SeekBlob(image, HDR.nCols*ldblk, SEEK_CUR);
705 if ((image->columns == 0) || (image->rows == 0))
706 return(image->previous == (Image *) NULL ? DestroyImageList(image)
708 goto skip_reading_current;
710 status=SetImageExtent(image,image->columns,image->rows,exception);
711 if (status == MagickFalse)
712 return(DestroyImageList(image));
713 (void) SetImageBackgroundColor(image,exception);
714 (void) SetImageColorspace(image,GRAYColorspace,exception);
715 quantum_info=AcquireQuantumInfo(image_info,image);
716 if (quantum_info == (QuantumInfo *) NULL)
717 return(DestroyImageList(image));
721 format_type=FloatingPointQuantumFormat;
725 format_type=FloatingPointQuantumFormat;
729 format_type=UnsignedQuantumFormat;
733 format_type=SignedQuantumFormat;
737 format_type=UnsignedQuantumFormat;
741 format_type=UnsignedQuantumFormat;
746 if (HDR.Type[0] != 0)
747 SetQuantumEndian(image,quantum_info,MSBEndian);
748 status=SetQuantumFormat(image,quantum_info,format_type);
749 status=SetQuantumDepth(image,quantum_info,depth);
750 status=SetQuantumEndian(image,quantum_info,endian);
751 SetQuantumScale(quantum_info,1.0);
752 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
753 for (y=0; y < (ssize_t) image->rows; y++)
758 count=ReadBlob(image,depth/8*image->columns,(char *) pixels);
761 q=QueueAuthenticPixels(image,0,image->rows-y-1,image->columns,1,
763 if (q == (Quantum *) NULL)
765 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
766 GrayQuantum,pixels,exception);
767 if ((HDR.Type[1] == 2) || (HDR.Type[1] == 3))
768 FixSignedValues(image,q,(int) image->columns);
769 if (SyncAuthenticPixels(image,exception) == MagickFalse)
771 if (image->previous == (Image *) NULL)
773 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
775 if (status == MagickFalse)
780 for (y=0; y < (ssize_t) image->rows; y++)
785 count=ReadBlob(image,depth/8*image->columns,(char *) pixels);
788 if (HDR.Type[1] == 0)
789 InsertComplexDoubleRow(image,(double *) pixels,y,0,0,exception);
791 InsertComplexFloatRow(image,(float *) pixels,y,0,0,exception);
793 if (quantum_info != (QuantumInfo *) NULL)
794 quantum_info=DestroyQuantumInfo(quantum_info);
795 if (EOFBlob(image) != MagickFalse)
797 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
801 rotated_image=RotateImage(image,90.0,exception);
802 if (rotated_image != (Image *) NULL)
804 rotated_image->page.x=0;
805 rotated_image->page.y=0;
806 rotated_image->colors = image->colors;
807 DestroyBlob(rotated_image);
808 rotated_image->blob=ReferenceBlob(image->blob);
809 AppendImageToList(&image,rotated_image);
810 DeleteImageFromList(&image);
813 Proceed to next image.
815 if (image_info->number_scenes != 0)
816 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
819 Allocate next image structure.
821 skip_reading_current:
822 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
823 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
825 AcquireNextImage(image_info,image,exception);
826 if (GetNextImageInList(image) == (Image *) NULL)
831 image=SyncNextImageInList(image);
832 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
834 if (status == MagickFalse)
837 (void) CloseBlob(image);
838 if (status == MagickFalse)
839 return(DestroyImageList(image));
840 return(GetFirstImageInList(image));
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848 % R e a d M A T L A B i m a g e %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % ReadMATImage() reads an MAT X image file and returns it. It
855 % allocates the memory necessary for the new Image structure and returns a
856 % pointer to the new image.
858 % The format of the ReadMATImage method is:
860 % Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
862 % A description of each parameter follows:
864 % o image: Method ReadMATImage returns a pointer to the image after
865 % reading. A null image is returned if there is a memory shortage or if
866 % the image cannot be read.
868 % o image_info: Specifies a pointer to a ImageInfo structure.
870 % o exception: return any errors or warnings in this structure.
873 static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
875 Image *image, *image2=NULL,
880 MATHeader MATLAB_HDR;
883 QuantumInfo *quantum_info;
884 ImageInfo *clone_info;
887 unsigned char *BImgBuff = NULL;
888 double MinVal, MaxVal;
893 MagickOffsetType filepos=0x80;
895 unsigned int (*ReadBlobXXXLong)(Image *image);
896 unsigned short (*ReadBlobXXXShort)(Image *image);
897 void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
898 void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
901 assert(image_info != (const ImageInfo *) NULL);
902 assert(image_info->signature == MagickCoreSignature);
903 assert(exception != (ExceptionInfo *) NULL);
904 assert(exception->signature == MagickCoreSignature);
905 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter");
910 image = AcquireImage(image_info,exception);
911 image2 = (Image *) NULL;
913 status = OpenBlob(image_info, image, ReadBinaryBlobMode, exception);
914 if (status == MagickFalse)
916 image=DestroyImageList(image);
917 return((Image *) NULL);
922 quantum_info=(QuantumInfo *) NULL;
923 clone_info=(ImageInfo *) NULL;
924 if (ReadBlob(image,124,(unsigned char *) &MATLAB_HDR.identific) != 124)
925 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
926 if (strncmp(MATLAB_HDR.identific,"MATLAB",6) != 0)
928 image=ReadMATImageV4(image_info,image,exception);
931 if ((image != image2) && (image2 != (Image *) NULL))
932 image2=DestroyImage(image2);
933 if (clone_info != (ImageInfo *) NULL)
934 clone_info=DestroyImageInfo(clone_info);
935 return((Image *) NULL);
939 MATLAB_HDR.Version = ReadBlobLSBShort(image);
940 if(ReadBlob(image,2,(unsigned char *) &MATLAB_HDR.EndianIndicator) != 2)
941 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
944 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Endian %c%c",
945 MATLAB_HDR.EndianIndicator[0],MATLAB_HDR.EndianIndicator[1]);
946 if (!strncmp(MATLAB_HDR.EndianIndicator, "IM", 2))
948 ReadBlobXXXLong = ReadBlobLSBLong;
949 ReadBlobXXXShort = ReadBlobLSBShort;
950 ReadBlobDoublesXXX = ReadBlobDoublesLSB;
951 ReadBlobFloatsXXX = ReadBlobFloatsLSB;
952 image->endian = LSBEndian;
954 else if (!strncmp(MATLAB_HDR.EndianIndicator, "MI", 2))
956 ReadBlobXXXLong = ReadBlobMSBLong;
957 ReadBlobXXXShort = ReadBlobMSBShort;
958 ReadBlobDoublesXXX = ReadBlobDoublesMSB;
959 ReadBlobFloatsXXX = ReadBlobFloatsMSB;
960 image->endian = MSBEndian;
965 if ((image != image2) && (image2 != (Image *) NULL))
966 image2=DestroyImage(image2);
967 if (clone_info != (ImageInfo *) NULL)
968 clone_info=DestroyImageInfo(clone_info);
969 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
972 filepos = TellBlob(image);
973 while(filepos < (MagickOffsetType) GetBlobSize(image) && !EOFBlob(image)) /* object parser loop */
976 if(filepos > (MagickOffsetType) GetBlobSize(image) || filepos < 0)
978 if(SeekBlob(image,filepos,SEEK_SET) != filepos) break;
979 /* printf("pos=%X\n",TellBlob(image)); */
981 MATLAB_HDR.DataType = ReadBlobXXXLong(image);
982 if(EOFBlob(image)) break;
983 MATLAB_HDR.ObjectSize = ReadBlobXXXLong(image);
984 if(EOFBlob(image)) break;
985 if((MagickSizeType) (MATLAB_HDR.ObjectSize+filepos) >= GetBlobSize(image))
987 filepos += (MagickOffsetType) MATLAB_HDR.ObjectSize + 4 + 4;
989 if (clone_info != (ImageInfo *) NULL)
990 clone_info=DestroyImageInfo(clone_info);
991 clone_info=CloneImageInfo(image_info);
992 if ((image != image2) && (image2 != (Image *) NULL))
993 image2=DestroyImage(image2);
995 #if defined(MAGICKCORE_ZLIB_DELEGATE)
996 if(MATLAB_HDR.DataType == miCOMPRESSED)
998 image2 = decompress_block(image,&MATLAB_HDR.ObjectSize,clone_info,exception);
999 if(image2==NULL) continue;
1000 MATLAB_HDR.DataType = ReadBlobXXXLong(image2); /* replace compressed object type. */
1004 if (MATLAB_HDR.DataType != miMATRIX)
1006 clone_info=DestroyImageInfo(clone_info);
1007 #if defined(MAGICKCORE_ZLIB_DELEGATE)
1008 if (image2 != image)
1009 DeleteImageFromList(&image2);
1011 continue; /* skip another objects. */
1014 MATLAB_HDR.unknown1 = ReadBlobXXXLong(image2);
1015 MATLAB_HDR.unknown2 = ReadBlobXXXLong(image2);
1017 MATLAB_HDR.unknown5 = ReadBlobXXXLong(image2);
1018 MATLAB_HDR.StructureClass = MATLAB_HDR.unknown5 & 0xFF;
1019 MATLAB_HDR.StructureFlag = (MATLAB_HDR.unknown5>>8) & 0xFF;
1021 MATLAB_HDR.unknown3 = ReadBlobXXXLong(image2);
1023 MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2); /* ??? don't understand why ?? */
1024 MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2);
1025 MATLAB_HDR.DimFlag = ReadBlobXXXLong(image2);
1026 MATLAB_HDR.SizeX = ReadBlobXXXLong(image2);
1027 MATLAB_HDR.SizeY = ReadBlobXXXLong(image2);
1030 switch(MATLAB_HDR.DimFlag)
1032 case 8: z2=z=1; break; /* 2D matrix*/
1033 case 12: z2=z = ReadBlobXXXLong(image2); /* 3D matrix RGB*/
1034 (void) ReadBlobXXXLong(image2);
1037 if (clone_info != (ImageInfo *) NULL)
1038 clone_info=DestroyImageInfo(clone_info);
1039 if ((image != image2) && (image2 != (Image *) NULL))
1040 image2=DestroyImage(image2);
1041 ThrowReaderException(CoderError,
1042 "MultidimensionalMatricesAreNotSupported");
1045 case 16: z2=z = ReadBlobXXXLong(image2); /* 4D matrix animation */
1048 if (clone_info != (ImageInfo *) NULL)
1049 clone_info=DestroyImageInfo(clone_info);
1050 if ((image != image2) && (image2 != (Image *) NULL))
1051 image2=DestroyImage(image2);
1052 ThrowReaderException(CoderError,
1053 "MultidimensionalMatricesAreNotSupported");
1055 Frames = ReadBlobXXXLong(image2);
1058 if (clone_info != (ImageInfo *) NULL)
1059 clone_info=DestroyImageInfo(clone_info);
1060 if ((image != image2) && (image2 != (Image *) NULL))
1061 image2=DestroyImage(image2);
1062 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1064 if (AcquireMagickResource(ListLengthResource,Frames) == MagickFalse)
1066 if (clone_info != (ImageInfo *) NULL)
1067 clone_info=DestroyImageInfo(clone_info);
1068 if ((image != image2) && (image2 != (Image *) NULL))
1069 image2=DestroyImage(image2);
1070 ThrowReaderException(ResourceLimitError,"ListLengthExceedsLimit");
1074 if (clone_info != (ImageInfo *) NULL)
1075 clone_info=DestroyImageInfo(clone_info);
1076 if ((image != image2) && (image2 != (Image *) NULL))
1077 image2=DestroyImage(image2);
1078 ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
1081 MATLAB_HDR.Flag1 = ReadBlobXXXShort(image2);
1082 MATLAB_HDR.NameFlag = ReadBlobXXXShort(image2);
1084 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1085 "MATLAB_HDR.StructureClass %d",MATLAB_HDR.StructureClass);
1086 if (MATLAB_HDR.StructureClass != mxCHAR_CLASS &&
1087 MATLAB_HDR.StructureClass != mxSINGLE_CLASS && /* float + complex float */
1088 MATLAB_HDR.StructureClass != mxDOUBLE_CLASS && /* double + complex double */
1089 MATLAB_HDR.StructureClass != mxINT8_CLASS &&
1090 MATLAB_HDR.StructureClass != mxUINT8_CLASS && /* uint8 + uint8 3D */
1091 MATLAB_HDR.StructureClass != mxINT16_CLASS &&
1092 MATLAB_HDR.StructureClass != mxUINT16_CLASS && /* uint16 + uint16 3D */
1093 MATLAB_HDR.StructureClass != mxINT32_CLASS &&
1094 MATLAB_HDR.StructureClass != mxUINT32_CLASS && /* uint32 + uint32 3D */
1095 MATLAB_HDR.StructureClass != mxINT64_CLASS &&
1096 MATLAB_HDR.StructureClass != mxUINT64_CLASS) /* uint64 + uint64 3D */
1098 if ((image2 != (Image*) NULL) && (image2 != image))
1101 DeleteImageFromList(&image2);
1103 if (clone_info != (ImageInfo *) NULL)
1104 clone_info=DestroyImageInfo(clone_info);
1105 ThrowReaderException(CoderError,"UnsupportedCellTypeInTheMatrix");
1108 switch (MATLAB_HDR.NameFlag)
1111 size = ReadBlobXXXLong(image2); /* Object name string size */
1112 size = 4 * (((size_t) size + 3 + 1) / 4);
1113 (void) SeekBlob(image2, size, SEEK_CUR);
1119 (void) ReadBlob(image2, 4, (unsigned char *) &size); /* Object name string */
1125 CellType = ReadBlobXXXLong(image2); /* Additional object type */
1127 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1128 "MATLAB_HDR.CellType: %.20g",(double) CellType);
1131 if (ReadBlob(image2, 4, (unsigned char *) &size) != 4)
1140 if(MATLAB_HDR.StructureFlag & FLAG_LOGICAL)
1143 image->depth = 8; /* Byte type cell */
1144 ldblk = (ssize_t) MATLAB_HDR.SizeX;
1149 image->depth = 16; /* Word type cell */
1150 ldblk = (ssize_t) (2 * MATLAB_HDR.SizeX);
1155 image->depth = 32; /* Dword type cell */
1156 ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
1161 image->depth = 64; /* Qword type cell */
1162 ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
1166 image->depth = 32; /* double type cell */
1167 (void) SetImageOption(clone_info,"quantum:format","floating-point");
1168 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
1169 { /* complex float type cell */
1171 ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
1175 image->depth = 64; /* double type cell */
1176 (void) SetImageOption(clone_info,"quantum:format","floating-point");
1177 DisableMSCWarning(4127)
1178 if (sizeof(double) != 8)
1181 if (clone_info != (ImageInfo *) NULL)
1182 clone_info=DestroyImageInfo(clone_info);
1183 if ((image != image2) && (image2 != (Image *) NULL))
1184 image2=DestroyImage(image2);
1185 ThrowReaderException(CoderError, "IncompatibleSizeOfDouble");
1187 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
1188 { /* complex double type cell */
1190 ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
1193 if ((image != image2) && (image2 != (Image *) NULL))
1194 image2=DestroyImage(image2);
1196 clone_info=DestroyImageInfo(clone_info);
1197 ThrowReaderException(CoderError, "UnsupportedCellTypeInTheMatrix");
1200 image->columns = MATLAB_HDR.SizeX;
1201 image->rows = MATLAB_HDR.SizeY;
1202 image->colors = GetQuantumRange(image->depth);
1203 if (image->columns == 0 || image->rows == 0)
1205 if((unsigned int)ldblk*MATLAB_HDR.SizeY > MATLAB_HDR.ObjectSize)
1207 /* Image is gray when no complex flag is set and 2D Matrix */
1208 if ((MATLAB_HDR.DimFlag == 8) &&
1209 ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
1211 image->type=GrayscaleType;
1212 SetImageColorspace(image,GRAYColorspace,exception);
1217 If ping is true, then only set image size and colors without
1218 reading any image data.
1220 if (image_info->ping)
1222 size_t temp = image->columns;
1223 image->columns = image->rows;
1225 goto done_reading; /* !!!!!! BAD !!!! */
1227 status=SetImageExtent(image,image->columns,image->rows,exception);
1228 if (status == MagickFalse)
1230 if (clone_info != (ImageInfo *) NULL)
1231 clone_info=DestroyImageInfo(clone_info);
1232 if ((image != image2) && (image2 != (Image *) NULL))
1233 image2=DestroyImage(image2);
1234 return(DestroyImageList(image));
1236 (void) SetImageBackgroundColor(image,exception);
1237 quantum_info=AcquireQuantumInfo(clone_info,image);
1238 if (quantum_info == (QuantumInfo *) NULL)
1240 if (clone_info != (ImageInfo *) NULL)
1241 clone_info=DestroyImageInfo(clone_info);
1242 if ((image != image2) && (image2 != (Image *) NULL))
1243 image2=DestroyImage(image2);
1244 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1247 /* ----- Load raster data ----- */
1248 BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(double)); /* Ldblk was set in the check phase */
1249 if (BImgBuff == NULL)
1251 if (clone_info != (ImageInfo *) NULL)
1252 clone_info=DestroyImageInfo(clone_info);
1253 if ((image != image2) && (image2 != (Image *) NULL))
1254 image2=DestroyImage(image2);
1255 if (quantum_info != (QuantumInfo *) NULL)
1256 quantum_info=DestroyQuantumInfo(quantum_info);
1257 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1259 (void) memset(BImgBuff,0,ldblk*sizeof(double));
1263 if (CellType==miDOUBLE || CellType==miSINGLE) /* Find Min and Max Values for floats */
1265 CalcMinMax(image2,image_info->endian,MATLAB_HDR.SizeX,MATLAB_HDR.SizeY,
1266 CellType,ldblk,BImgBuff,&quantum_info->minimum,
1267 &quantum_info->maximum);
1270 /* Main loop for reading all scanlines */
1271 if(z==1) z=0; /* read grey scanlines */
1272 /* else read color scanlines */
1275 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
1277 q=GetAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception);
1278 if (q == (Quantum *) NULL)
1280 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1281 " MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1));
1282 goto done_reading; /* Skip image rotation, when cannot set image pixels */
1284 if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk)
1286 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1287 " MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1));
1288 if ((image != image2) && (image2 != (Image *) NULL))
1289 image2=DestroyImage(image2);
1290 if (clone_info != (ImageInfo *) NULL)
1291 clone_info=DestroyImageInfo(clone_info);
1292 if (quantum_info != (QuantumInfo *) NULL)
1293 quantum_info=DestroyQuantumInfo(quantum_info);
1294 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
1295 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
1297 if((CellType==miINT8 || CellType==miUINT8) && (MATLAB_HDR.StructureFlag & FLAG_LOGICAL))
1299 FixLogical((unsigned char *)BImgBuff,ldblk);
1300 if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
1302 ImportQuantumPixelsFailed:
1303 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1304 " MAT failed to ImportQuantumPixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
1310 if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
1311 goto ImportQuantumPixelsFailed;
1314 if (z<=1 && /* fix only during a last pass z==0 || z==1 */
1315 (CellType==miINT8 || CellType==miINT16 || CellType==miINT32 || CellType==miINT64))
1316 FixSignedValues(image,q,MATLAB_HDR.SizeX);
1319 if (!SyncAuthenticPixels(image,exception))
1321 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1322 " MAT failed to sync image pixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
1328 if (i != (long) MATLAB_HDR.SizeY)
1329 goto END_OF_READING;
1331 /* Read complex part of numbers here */
1332 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
1333 { /* Find Min and Max Values for complex parts of floats */
1334 CellType = ReadBlobXXXLong(image2); /* Additional object type */
1335 i = ReadBlobXXXLong(image2); /* size of a complex part - toss away*/
1337 if (CellType==miDOUBLE || CellType==miSINGLE)
1339 CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &MinVal, &MaxVal);
1342 if (CellType==miDOUBLE)
1343 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
1345 ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff);
1346 if (EOFBlob(image) != MagickFalse)
1348 InsertComplexDoubleRow(image, (double *)BImgBuff, i, MinVal, MaxVal,
1352 if (CellType==miSINGLE)
1353 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
1355 ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff);
1356 if (EOFBlob(image) != MagickFalse)
1358 InsertComplexFloatRow(image,(float *)BImgBuff,i,MinVal,MaxVal,
1363 /* Image is gray when no complex flag is set and 2D Matrix AGAIN!!! */
1364 if ((MATLAB_HDR.DimFlag == 8) &&
1365 ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
1366 image->type=GrayscaleType;
1367 if (image->depth == 1)
1368 image->type=BilevelType;
1371 image2 = NULL; /* Remove shadow copy to an image before rotation. */
1374 rotated_image = RotateImage(image, 90.0, exception);
1375 if (rotated_image != (Image *) NULL)
1377 /* Remove page offsets added by RotateImage */
1378 rotated_image->page.x=0;
1379 rotated_image->page.y=0;
1380 rotated_image->colors = image->colors;
1381 DestroyBlob(rotated_image);
1382 rotated_image->blob=ReferenceBlob(image->blob);
1383 AppendImageToList(&image,rotated_image);
1384 DeleteImageFromList(&image);
1392 DeleteImageFromList(&image2);
1395 if(clone_info->file)
1397 fclose(clone_info->file);
1398 clone_info->file = NULL;
1399 (void) remove_utf8(clone_info->filename);
1403 if (EOFBlob(image) != MagickFalse)
1406 /* Allocate next image structure. */
1407 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1408 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1410 AcquireNextImage(image_info,image,exception);
1411 if (image->next == (Image *) NULL) break;
1412 image=SyncNextImageInList(image);
1413 image->columns=image->rows=0;
1416 /* row scan buffer is no longer needed */
1417 RelinquishMagickMemory(BImgBuff);
1419 if (quantum_info != (QuantumInfo *) NULL)
1420 quantum_info=DestroyQuantumInfo(quantum_info);
1425 if(image2==NULL) image2 = image;
1426 if(!EOFBlob(image) && TellBlob(image)<filepos)
1429 if ((image2!=NULL) && (image2!=image)) /* Does shadow temporary decompressed image exist? */
1431 /* CloseBlob(image2); */
1432 DeleteImageFromList(&image2);
1435 if(clone_info->file)
1437 fclose(clone_info->file);
1438 clone_info->file = NULL;
1439 (void) remove_utf8(clone_info->filename);
1445 clone_info=DestroyImageInfo(clone_info);
1449 RelinquishMagickMemory(BImgBuff);
1450 if (quantum_info != (QuantumInfo *) NULL)
1451 quantum_info=DestroyQuantumInfo(quantum_info);
1460 Rewind list, removing any empty images while rewinding.
1464 while (p != (Image *) NULL)
1467 if ((p->rows == 0) || (p->columns == 0)) {
1470 image2=(Image *) NULL;
1471 DeleteImageFromList(&tmp);
1481 for (p=image; p != (Image *) NULL; p=p->next)
1485 if(clone_info != NULL) /* cleanup garbage file from compression */
1487 if(clone_info->file)
1489 fclose(clone_info->file);
1490 clone_info->file = NULL;
1491 (void) remove_utf8(clone_info->filename);
1493 DestroyImageInfo(clone_info);
1496 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),"return");
1497 if ((image != image2) && (image2 != (Image *) NULL))
1498 image2=DestroyImage(image2);
1499 if (image == (Image *) NULL)
1500 ThrowReaderException(CorruptImageError,"ImproperImageHeader")
1505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1509 % R e g i s t e r M A T I m a g e %
1513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1515 % Method RegisterMATImage adds attributes for the MAT image format to
1516 % the list of supported formats. The attributes include the image format
1517 % tag, a method to read and/or write the format, whether the format
1518 % supports the saving of more than one frame to the same file or blob,
1519 % whether the format supports native in-memory I/O, and a brief
1520 % description of the format.
1522 % The format of the RegisterMATImage method is:
1524 % size_t RegisterMATImage(void)
1527 ModuleExport size_t RegisterMATImage(void)
1532 entry=AcquireMagickInfo("MAT","MAT","MATLAB level 5 image format");
1533 entry->decoder=(DecodeImageHandler *) ReadMATImage;
1534 entry->encoder=(EncodeImageHandler *) WriteMATImage;
1535 entry->flags^=CoderBlobSupportFlag;
1536 entry->flags|=CoderDecoderSeekableStreamFlag;
1537 (void) RegisterMagickInfo(entry);
1538 return(MagickImageCoderSignature);
1542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546 % U n r e g i s t e r M A T I m a g e %
1550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1552 % Method UnregisterMATImage removes format registrations made by the
1553 % MAT module from the list of supported formats.
1555 % The format of the UnregisterMATImage method is:
1557 % UnregisterMATImage(void)
1560 ModuleExport void UnregisterMATImage(void)
1562 (void) UnregisterMagickInfo("MAT");
1566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1570 % W r i t e M A T L A B I m a g e %
1574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1576 % Function WriteMATImage writes an Matlab matrix to a file.
1578 % The format of the WriteMATImage method is:
1580 % MagickBooleanType WriteMATImage(const ImageInfo *image_info,
1581 % Image *image,ExceptionInfo *exception)
1583 % A description of each parameter follows.
1585 % o image_info: Specifies a pointer to a ImageInfo structure.
1587 % o image: A pointer to an Image structure.
1589 % o exception: return any errors or warnings in this structure.
1592 static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image,
1593 ExceptionInfo *exception)
1614 Open output image file.
1616 assert(image_info != (const ImageInfo *) NULL);
1617 assert(image_info->signature == MagickCoreSignature);
1618 assert(image != (Image *) NULL);
1619 assert(image->signature == MagickCoreSignature);
1620 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"enter MAT");
1621 assert(exception != (ExceptionInfo *) NULL);
1622 assert(exception->signature == MagickCoreSignature);
1623 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1624 if (status == MagickFalse)
1625 return(MagickFalse);
1628 current_time=GetMagickTime();
1629 GetMagickUTCtime(¤t_time,&utc_time);
1630 (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124));
1631 FormatLocaleString(MATLAB_HDR,sizeof(MATLAB_HDR),
1632 "MATLAB 5.0 MAT-file, Platform: %s, Created on: %s %s %2d %2d:%2d:%2d %d",
1633 OsDesc,DayOfWTab[utc_time.tm_wday],MonthsTab[utc_time.tm_mon],
1634 utc_time.tm_mday,utc_time.tm_hour,utc_time.tm_min,
1635 utc_time.tm_sec,utc_time.tm_year+1900);
1638 MATLAB_HDR[0x7E]='I';
1639 MATLAB_HDR[0x7F]='M';
1640 (void) WriteBlob(image,sizeof(MATLAB_HDR),(unsigned char *) MATLAB_HDR);
1642 imageListLength=GetImageListLength(image);
1663 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1664 is_gray=SetImageGray(image,exception);
1665 z=(is_gray != MagickFalse) ? 0 : 3;
1670 data_size = image->rows * image->columns;
1671 if (is_gray == MagickFalse)
1673 padding=((unsigned char)(data_size-1) & 0x7) ^ 0x7;
1675 (void) WriteBlobLSBLong(image,miMATRIX);
1676 (void) WriteBlobLSBLong(image,(unsigned int) data_size+padding+
1677 ((is_gray != MagickFalse) ? 48 : 56));
1678 (void) WriteBlobLSBLong(image,0x6); /* 0x88 */
1679 (void) WriteBlobLSBLong(image,0x8); /* 0x8C */
1680 (void) WriteBlobLSBLong(image,0x6); /* 0x90 */
1681 (void) WriteBlobLSBLong(image,0);
1682 (void) WriteBlobLSBLong(image,0x5); /* 0x98 */
1683 (void) WriteBlobLSBLong(image,(is_gray != MagickFalse) ? 0x8 : 0xC); /* 0x9C - DimFlag */
1684 (void) WriteBlobLSBLong(image,(unsigned int) image->rows); /* x: 0xA0 */
1685 (void) WriteBlobLSBLong(image,(unsigned int) image->columns); /* y: 0xA4 */
1686 if (is_gray == MagickFalse)
1688 (void) WriteBlobLSBLong(image,3); /* z: 0xA8 */
1689 (void) WriteBlobLSBLong(image,0);
1691 (void) WriteBlobLSBShort(image,1); /* 0xB0 */
1692 (void) WriteBlobLSBShort(image,1); /* 0xB2 */
1693 (void) WriteBlobLSBLong(image,'M'); /* 0xB4 */
1694 (void) WriteBlobLSBLong(image,0x2); /* 0xB8 */
1695 (void) WriteBlobLSBLong(image,(unsigned int) data_size); /* 0xBC */
1700 quantum_info=AcquireQuantumInfo(image_info,image);
1701 if (quantum_info == (QuantumInfo *) NULL)
1702 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1703 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1712 for (y=0; y < (ssize_t) image->columns; y++)
1717 p=GetVirtualPixels(image,y,0,1,image->rows,exception);
1718 if (p == (const Quantum *) NULL)
1720 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1721 z2qtype[z],pixels,exception);
1722 if (length != image->columns)
1724 if (WriteBlob(image,image->rows,pixels) != (ssize_t) image->rows)
1727 if (y < (ssize_t) image->columns)
1729 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1732 while (padding-- > 0)
1733 (void) WriteBlobByte(image,0);
1734 quantum_info=DestroyQuantumInfo(quantum_info);
1735 if (GetNextImageInList(image) == (Image *) NULL)
1737 image=SyncNextImageInList(image);
1738 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1739 if (status == MagickFalse)
1741 } while (image_info->adjoin != MagickFalse);
1742 (void) CloseBlob(image);