]> granicus.if.org Git - imagemagick/blob - coders/mat.c
(no commit message)
[imagemagick] / coders / mat.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
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                    %
10 %                                                                             %
11 %                                                                             %
12 %                        Read MATLAB Image Format                             %
13 %                                                                             %
14 %                              Software Design                                %
15 %                              Jaroslav Fojtik                                %
16 %                                2001-2008                                    %
17 %                                                                             %
18 %                                                                             %
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:    %
25 %                                                                             %
26 %  The above copyright notice and this permission notice shall be included in %
27 %  all copies or substantial portions of ImageMagick.                         %
28 %                                                                             %
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          %
35 %  ImageMagick.                                                               %
36 %                                                                             %
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.                                                        %
41 %                                                                             %
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 %
44 %
45 */
46 \f
47 /*
48   Include declarations.
49 */
50 #include "magick/studio.h"
51 #include "magick/blob.h"
52 #include "magick/blob-private.h"
53 #include "magick/cache.h"
54 #include "magick/color-private.h"
55 #include "magick/colormap.h"
56 #include "magick/exception.h"
57 #include "magick/exception-private.h"
58 #include "magick/image.h"
59 #include "magick/image-private.h"
60 #include "magick/list.h"
61 #include "magick/magick.h"
62 #include "magick/memory_.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/quantum-private.h"
66 #include "magick/option.h"
67 #include "magick/resource_.h"
68 #include "magick/shear.h"
69 #include "magick/static.h"
70 #include "magick/string_.h"
71 #include "magick/module.h"
72 #include "magick/transform.h"
73 #if defined(MAGICKCORE_ZLIB_DELEGATE)
74  #include "zlib.h"
75 #endif
76 \f
77 /*
78   Forward declaration.
79 */
80 static MagickBooleanType
81   WriteMATImage(const ImageInfo *,Image *);
82
83
84 /* Auto coloring method, sorry this creates some artefact inside data
85 MinReal+j*MaxComplex = red  MaxReal+j*MaxComplex = black
86 MinReal+j*0 = white          MaxReal+j*0 = black
87 MinReal+j*MinComplex = blue  MaxReal+j*MinComplex = black
88 */
89
90 typedef struct
91 {
92   char identific[124];
93   unsigned short Version;
94   char EndianIndicator[2];
95   unsigned long DataType;
96   unsigned long ObjectSize;
97   unsigned long unknown1;
98   unsigned long unknown2;
99
100   unsigned short unknown5;
101   unsigned char StructureFlag;
102   unsigned char StructureClass;
103   unsigned long unknown3;
104   unsigned long unknown4;
105   unsigned long DimFlag;
106
107   unsigned long SizeX;
108   unsigned long SizeY;
109   unsigned short Flag1;
110   unsigned short NameFlag;
111 }
112 MATHeader;
113
114 static const char *MonthsTab[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
115 static const char *DayOfWTab[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
116 static const char *OsDesc=
117 #ifdef __WIN32__
118     "PCWIN";
119 #else
120  #ifdef __APPLE__
121     "MAC";
122  #else
123     "LNX86";
124  #endif
125 #endif
126
127 typedef enum
128   {
129     miINT8 = 1,      /* 8 bit signed */
130     miUINT8,      /* 8 bit unsigned */
131     miINT16,      /* 16 bit signed */
132     miUINT16,      /* 16 bit unsigned */
133     miINT32,      /* 32 bit signed */
134     miUINT32,      /* 32 bit unsigned */
135     miSINGLE,      /* IEEE 754 single precision float */
136     miRESERVE1,
137     miDOUBLE,      /* IEEE 754 double precision float */
138     miRESERVE2,
139     miRESERVE3,
140     miINT64,      /* 64 bit signed */
141     miUINT64,      /* 64 bit unsigned */
142     miMATRIX,            /* MATLAB array */
143     miCOMPRESSED,          /* Compressed Data */
144     miUTF8,            /* Unicode UTF-8 Encoded Character Data */
145     miUTF16,            /* Unicode UTF-16 Encoded Character Data */
146     miUTF32      /* Unicode UTF-32 Encoded Character Data */
147   } mat5_data_type;
148
149 typedef enum
150   {
151     mxCELL_CLASS=1,    /* cell array */
152     mxSTRUCT_CLASS,    /* structure */
153     mxOBJECT_CLASS,    /* object */
154     mxCHAR_CLASS,    /* character array */
155     mxSPARSE_CLASS,    /* sparse array */
156     mxDOUBLE_CLASS,    /* double precision array */
157     mxSINGLE_CLASS,    /* single precision floating point */
158     mxINT8_CLASS,    /* 8 bit signed integer */
159     mxUINT8_CLASS,    /* 8 bit unsigned integer */
160     mxINT16_CLASS,    /* 16 bit signed integer */
161     mxUINT16_CLASS,    /* 16 bit unsigned integer */
162     mxINT32_CLASS,    /* 32 bit signed integer */
163     mxUINT32_CLASS,    /* 32 bit unsigned integer */
164     mxINT64_CLASS,    /* 64 bit signed integer */
165     mxUINT64_CLASS,    /* 64 bit unsigned integer */
166     mxFUNCTION_CLASS            /* Function handle */
167   } arrayclasstype;
168
169 #define FLAG_COMPLEX 0x8
170 #define FLAG_GLOBAL  0x4
171 #define FLAG_LOGICAL 0x2
172
173 static const QuantumType z2qtype[4] = {GrayQuantum, BlueQuantum, GreenQuantum, RedQuantum};
174
175
176 static void InsertComplexDoubleRow(double *p, int y, Image * image, double MinVal,
177                                   double MaxVal)
178 {
179   ExceptionInfo
180     *exception;
181
182   double f;
183   int x;
184   register PixelPacket *q;
185
186   if (MinVal == 0)
187     MinVal = -1;
188   if (MaxVal == 0)
189     MaxVal = 1;
190
191   exception=(&image->exception);
192   q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
193   if (q == (PixelPacket *) NULL)
194     return;
195   for (x = 0; x < (long) image->columns; x++)
196   {
197     if (*p > 0)
198     {
199       f = (*p / MaxVal) * (QuantumRange - q->red);
200       if (f + q->red > QuantumRange)
201         q->red = QuantumRange;
202       else
203         q->red += (int) f;
204       if ((int) f / 2.0 > q->green)
205         q->green = q->blue = 0;
206       else
207         q->green = q->blue -= (int) (f / 2.0);
208     }
209     if (*p < 0)
210     {
211       f = (*p / MaxVal) * (QuantumRange - q->blue);
212       if (f + q->blue > QuantumRange)
213         q->blue = QuantumRange;
214       else
215         q->blue += (int) f;
216       if ((int) f / 2.0 > q->green)
217         q->green = q->red = 0;
218       else
219         q->green = q->red -= (int) (f / 2.0);
220     }
221     p++;
222     q++;
223   }
224   if (!SyncAuthenticPixels(image,exception))
225     return;
226   return;
227 }
228
229
230 static void InsertComplexFloatRow(float *p, int y, Image * image, double MinVal,
231                                   double MaxVal)
232 {
233   ExceptionInfo
234     *exception;
235
236   double f;
237   int x;
238   register PixelPacket *q;
239
240   if (MinVal == 0)
241     MinVal = -1;
242   if (MaxVal == 0)
243     MaxVal = 1;
244
245   exception=(&image->exception);
246   q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
247   if (q == (PixelPacket *) NULL)
248     return;
249   for (x = 0; x < (long) image->columns; x++)
250   {
251     if (*p > 0)
252     {
253       f = (*p / MaxVal) * (QuantumRange - q->red);
254       if (f + q->red > QuantumRange)
255         q->red = QuantumRange;
256       else
257         q->red += (int) f;
258       if ((int) f / 2.0 > q->green)
259         q->green = q->blue = 0;
260       else
261         q->green = q->blue -= (int) (f / 2.0);
262     }
263     if (*p < 0)
264     {
265       f = (*p / MaxVal) * (QuantumRange - q->blue);
266       if (f + q->blue > QuantumRange)
267         q->blue = QuantumRange;
268       else
269         q->blue += (int) f;
270       if ((int) f / 2.0 > q->green)
271         q->green = q->red = 0;
272       else
273         q->green = q->red -= (int) (f / 2.0);
274     }
275     p++;
276     q++;
277   }
278   if (!SyncAuthenticPixels(image,exception))
279     return;
280   return;
281 }
282
283
284 /************** READERS ******************/
285
286 /* This function reads one block of floats*/
287 static void ReadBlobFloatsLSB(Image * image, size_t len, float *data)
288 {
289   while (len >= 4)
290   {
291     *data++ = ReadBlobFloat(image);
292     len -= sizeof(float);
293   }
294   if (len > 0)
295     (void) SeekBlob(image, len, SEEK_CUR);
296 }
297
298 static void ReadBlobFloatsMSB(Image * image, size_t len, float *data)
299 {
300   while (len >= 4)
301   {
302     *data++ = ReadBlobFloat(image);
303     len -= sizeof(float);
304   }
305   if (len > 0)
306     (void) SeekBlob(image, len, SEEK_CUR);
307 }
308
309 /* This function reads one block of doubles*/
310 static void ReadBlobDoublesLSB(Image * image, size_t len, double *data)
311 {
312   while (len >= 8)
313   {
314     *data++ = ReadBlobDouble(image);
315     len -= sizeof(double);
316   }
317   if (len > 0)
318     (void) SeekBlob(image, len, SEEK_CUR);
319 }
320
321 static void ReadBlobDoublesMSB(Image * image, size_t len, double *data)
322 {
323   while (len >= 8)
324   {
325     *data++ = ReadBlobDouble(image);
326     len -= sizeof(double);
327   }
328   if (len > 0)
329     (void) SeekBlob(image, len, SEEK_CUR);
330 }
331
332 /* Calculate minimum and maximum from a given block of data */
333 static void CalcMinMax(Image *image, int endian_indicator, int SizeX, int SizeY, unsigned long CellType, unsigned ldblk, void *BImgBuff, double *Min, double *Max)
334 {
335 MagickOffsetType filepos;
336 int i, x;
337 void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
338 void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
339 double *dblrow;
340 float *fltrow;
341
342   if (endian_indicator == LSBEndian)
343   {    
344     ReadBlobDoublesXXX = ReadBlobDoublesLSB;
345     ReadBlobFloatsXXX = ReadBlobFloatsLSB;   
346   } 
347   else    /* MI */
348   {    
349     ReadBlobDoublesXXX = ReadBlobDoublesMSB;
350     ReadBlobFloatsXXX = ReadBlobFloatsMSB;   
351   }
352
353   filepos = TellBlob(image);     /* Please note that file seeking occurs only in the case of doubles */
354   for (i = 0; i < SizeY; i++)
355   {
356     if (CellType==miDOUBLE)
357     {
358       ReadBlobDoublesXXX(image, ldblk, (double *)BImgBuff);
359       dblrow = (double *)BImgBuff;
360       if (i == 0)
361       {
362         *Min = *Max = *dblrow;
363       }
364       for (x = 0; x < SizeX; x++)
365       {
366         if (*Min > *dblrow)
367           *Min = *dblrow;
368         if (*Max < *dblrow)
369           *Max = *dblrow;
370         dblrow++;
371       }
372     }
373     if (CellType==miSINGLE)
374     {
375       ReadBlobFloatsXXX(image, ldblk, (float *)BImgBuff);
376       fltrow = (float *)BImgBuff;
377       if (i == 0)
378       {
379         *Min = *Max = *fltrow;
380       }
381     for (x = 0; x < (long) SizeX; x++)
382       {
383         if (*Min > *fltrow)
384           *Min = *fltrow;
385         if (*Max < *fltrow)
386           *Max = *fltrow;
387         fltrow++;
388       }
389     }
390   }
391   (void) SeekBlob(image, filepos, SEEK_SET);
392 }
393
394
395 static void FixSignedValues(PixelPacket *q, int y)
396 {
397   while(y-->0)
398   {
399      /* Please note that negative values will overflow
400         Q=8; QuantumRange=255: <0;127> + 127+1 = <128; 255> 
401            <-1;-128> + 127+1 = <0; 127> */
402     q->red += QuantumRange/2 + 1;
403     q->green += QuantumRange/ + 1;
404     q->blue += QuantumRange/ + 1;
405     q++;
406   }
407 }
408
409
410 /** Fix whole row of logical/binary data. It means pack it. */
411 static void FixLogical(unsigned char *Buff,int ldblk)
412 {
413 unsigned char mask=128;
414 unsigned char *BuffL = Buff;
415 unsigned char val = 0;
416
417   while(ldblk-->0)
418   {
419     if(*Buff++ != 0)
420       val |= mask;    
421
422     mask >>= 1;
423     if(mask==0)
424     {
425       *BuffL++ = val;
426       val = 0;
427       mask = 128;
428     }   
429       
430   }
431   *BuffL = val;
432 }
433
434 #if defined(MAGICKCORE_ZLIB_DELEGATE)
435 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
436   unsigned int size)
437 {
438   (void) context;
439   return((voidpf) AcquireQuantumMemory(items,size));
440 }
441
442 static void RelinquishZIPMemory(voidpf context,voidpf memory)
443 {
444   (void) context;
445   memory=RelinquishMagickMemory(memory);
446 }
447 #endif
448
449 /** This procedure decompreses an image block for a new MATLAB format. */
450 static Image *DecompressBlock(Image *orig, MagickOffsetType Size, ImageInfo *clone_info, ExceptionInfo *exception)
451 {
452 #if defined(MAGICKCORE_ZLIB_DELEGATE)
453
454 Image *image2;
455 void *CacheBlock, *DecompressBlock;
456 z_stream zip_info;
457 FILE *mat_file;
458 size_t magick_size;
459 size_t extent;
460
461 int status;
462
463   if(clone_info==NULL) return NULL;
464   if(clone_info->file)    /* Close file opened from previous transaction. */
465   {
466     fclose(clone_info->file);
467     clone_info->file = NULL;
468     (void) unlink(clone_info->filename);
469   }
470
471   CacheBlock = AcquireQuantumMemory((size_t)((Size<16384)?Size:16384),sizeof(unsigned char *));
472   if(CacheBlock==NULL) return NULL;
473   DecompressBlock = AcquireQuantumMemory((size_t)(4096),sizeof(unsigned char *));
474   if(DecompressBlock==NULL) 
475   {
476     RelinquishMagickMemory(CacheBlock);    
477     return NULL;
478   }
479
480   mat_file = fdopen(AcquireUniqueFileResource(clone_info->filename),"w");
481   if(!mat_file)
482   {
483     RelinquishMagickMemory(CacheBlock);
484     RelinquishMagickMemory(DecompressBlock);
485     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Gannot create file stream for PS image");
486     return NULL;
487   }
488
489   zip_info.zalloc=AcquireZIPMemory;
490   zip_info.zfree=RelinquishZIPMemory;
491   zip_info.opaque = (voidpf) NULL;
492   inflateInit(&zip_info);
493   /* zip_info.next_out = 8*4;*/
494
495   zip_info.avail_in = 0;
496   zip_info.total_out = 0;
497   while(Size>0 && !EOFBlob(orig))
498   {    
499     magick_size = ReadBlob(orig, (Size<16384)?Size:16384, (unsigned char *) CacheBlock);
500     zip_info.next_in = (Bytef *) CacheBlock;
501     zip_info.avail_in = (uInt) magick_size;    
502
503     while(zip_info.avail_in>0)
504     {
505       zip_info.avail_out = 4096;    
506       zip_info.next_out = (Bytef *) DecompressBlock;
507       status = inflate(&zip_info,Z_NO_FLUSH);      
508       extent=fwrite(DecompressBlock, 4096-zip_info.avail_out, 1, mat_file);
509
510       if(status == Z_STREAM_END) goto DblBreak;
511     }
512
513     Size -= magick_size;
514   }
515 DblBreak:
516  
517   (void)fclose(mat_file);
518   RelinquishMagickMemory(CacheBlock);
519   RelinquishMagickMemory(DecompressBlock);
520
521   if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL) goto UnlinkFile;
522   if( (image2 = AcquireImage(clone_info))==NULL ) goto EraseFile;  
523   status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception);
524   if (status == MagickFalse)
525   {
526     DeleteImageFromList(&image2);    
527 EraseFile:
528     fclose(clone_info->file);
529     clone_info->file = NULL;
530 UnlinkFile:
531     (void) unlink(clone_info->filename);
532     return NULL; 
533   }
534
535   return image2;
536 #else
537   (void) orig;
538   (void) Size;
539   (void) clone_info;
540   (void) exception;
541   return NULL;
542 #endif
543 }
544 \f
545 /*
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 %                                                                             %
548 %                                                                             %
549 %                                                                             %
550 %   R e a d M A T L A B i m a g e                                             %
551 %                                                                             %
552 %                                                                             %
553 %                                                                             %
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 %
556 %  ReadMATImage() reads an MAT X image file and returns it.  It
557 %  allocates the memory necessary for the new Image structure and returns a
558 %  pointer to the new image.
559 %
560 %  The format of the ReadMATImage method is:
561 %
562 %      Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
563 %
564 %  A description of each parameter follows:
565 %
566 %    o image:  Method ReadMATImage returns a pointer to the image after
567 %      reading. A null image is returned if there is a memory shortage or if
568 %      the image cannot be read.
569 %
570 %    o image_info: Specifies a pointer to a ImageInfo structure.
571 %
572 %    o exception: return any errors or warnings in this structure.
573 %
574 */
575
576 static inline size_t MagickMin(const size_t x,const size_t y)
577 {
578   if (x < y)
579     return(x);
580   return(y);
581 }
582
583 static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
584 {
585   Image *image, *image2=NULL,
586    *rotated_image;
587   PixelPacket *q;
588
589   unsigned int status;
590   MATHeader MATLAB_HDR;
591   unsigned long size;  
592   unsigned long CellType;
593   QuantumInfo *quantum_info;
594   ImageInfo *clone_info;
595   int i;
596   long ldblk;
597   unsigned char *BImgBuff = NULL;
598   double MinVal, MaxVal;
599   unsigned long Unknown6;
600   unsigned z;
601   int logging;
602   int sample_size;
603   MagickOffsetType filepos=0x80;
604   BlobInfo *blob;
605   
606   unsigned int (*ReadBlobXXXLong)(Image *image);
607   unsigned short (*ReadBlobXXXShort)(Image *image);
608   void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
609   void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
610
611
612   assert(image_info != (const ImageInfo *) NULL);
613   assert(image_info->signature == MagickSignature);
614   assert(exception != (ExceptionInfo *) NULL);
615   assert(exception->signature == MagickSignature);
616   logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter"); 
617
618   /*
619      Open image file.
620    */
621   image = AcquireImage(image_info);
622
623   status = OpenBlob(image_info, image, ReadBinaryBlobMode, exception);
624   if (status == MagickFalse)
625     {
626       image=DestroyImageList(image);
627       return((Image *) NULL);
628     }
629   /*
630      Read MATLAB image.
631    */
632   clone_info=CloneImageInfo(image_info);
633   if(ReadBlob(image,124,(unsigned char *) &MATLAB_HDR.identific) != 124)
634     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
635   MATLAB_HDR.Version = ReadBlobLSBShort(image);
636   if(ReadBlob(image,2,(unsigned char *) &MATLAB_HDR.EndianIndicator) != 2)
637     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
638
639   if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),"  Endian %c%c",
640         MATLAB_HDR.EndianIndicator[0],MATLAB_HDR.EndianIndicator[1]);
641   if (!strncmp(MATLAB_HDR.EndianIndicator, "IM", 2))
642   {
643     ReadBlobXXXLong = ReadBlobLSBLong;
644     ReadBlobXXXShort = ReadBlobLSBShort;
645     ReadBlobDoublesXXX = ReadBlobDoublesLSB;
646     ReadBlobFloatsXXX = ReadBlobFloatsLSB;
647     image->endian = LSBEndian;
648   } 
649   else if (!strncmp(MATLAB_HDR.EndianIndicator, "MI", 2))
650   {
651     ReadBlobXXXLong = ReadBlobMSBLong;
652     ReadBlobXXXShort = ReadBlobMSBShort;
653     ReadBlobDoublesXXX = ReadBlobDoublesMSB;
654     ReadBlobFloatsXXX = ReadBlobFloatsMSB;
655     image->endian = MSBEndian;
656   }
657   else 
658     goto MATLAB_KO;    /* unsupported endian */
659
660   if (strncmp(MATLAB_HDR.identific, "MATLAB", 6))
661 MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
662
663   filepos = TellBlob(image);
664   while(!EOFBlob(image)) /* object parser loop */
665   {
666     (void) SeekBlob(image,filepos,SEEK_SET);
667     /* printf("pos=%X\n",TellBlob(image)); */
668
669     MATLAB_HDR.DataType = ReadBlobXXXLong(image);
670     if(EOFBlob(image)) break;
671     MATLAB_HDR.ObjectSize = ReadBlobXXXLong(image);
672     if(EOFBlob(image)) break;
673     filepos += MATLAB_HDR.ObjectSize + 4 + 4;
674
675     image2 = image;
676 #if defined(MAGICKCORE_ZLIB_DELEGATE)
677     if(MATLAB_HDR.DataType == miCOMPRESSED)
678     {
679       image2 = DecompressBlock(image,MATLAB_HDR.ObjectSize,clone_info,exception);
680       if(image2==NULL) continue;
681       MATLAB_HDR.DataType = ReadBlobXXXLong(image2); /* replace compressed object type. */
682     }
683 #endif    
684
685     if(MATLAB_HDR.DataType!=miMATRIX) continue;  /* skip another objects. */
686  
687     MATLAB_HDR.unknown1 = ReadBlobXXXLong(image2);
688     MATLAB_HDR.unknown2 = ReadBlobXXXLong(image2);  
689
690     MATLAB_HDR.unknown5 = ReadBlobXXXLong(image2);
691     MATLAB_HDR.StructureClass = MATLAB_HDR.unknown5 & 0xFF;
692     MATLAB_HDR.StructureFlag = (MATLAB_HDR.unknown5>>8) & 0xFF;  
693
694     MATLAB_HDR.unknown3 = ReadBlobXXXLong(image2);
695     if(image!=image2)
696       MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2);  /* ??? don't understand why ?? */
697     MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2);
698     MATLAB_HDR.DimFlag = ReadBlobXXXLong(image2);
699     MATLAB_HDR.SizeX = ReadBlobXXXLong(image2);
700     MATLAB_HDR.SizeY = ReadBlobXXXLong(image2);  
701    
702
703     switch(MATLAB_HDR.DimFlag)
704     {     
705       case  8: z=1; break;      /* 2D matrix*/
706       case 12: z = ReadBlobXXXLong(image2);  /* 3D matrix RGB*/
707            Unknown6 = ReadBlobXXXLong(image2);
708          if(z!=3) ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
709          break;
710       default: ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
711     }  
712
713     MATLAB_HDR.Flag1 = ReadBlobXXXShort(image2);
714     MATLAB_HDR.NameFlag = ReadBlobXXXShort(image2);
715
716     if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
717           "MATLAB_HDR.StructureClass %d",MATLAB_HDR.StructureClass);
718     if (MATLAB_HDR.StructureClass != mxCHAR_CLASS && 
719         MATLAB_HDR.StructureClass != mxSINGLE_CLASS &&    /* float + complex float */
720         MATLAB_HDR.StructureClass != mxDOUBLE_CLASS &&    /* double + complex double */
721         MATLAB_HDR.StructureClass != mxINT8_CLASS &&
722         MATLAB_HDR.StructureClass != mxUINT8_CLASS &&    /* uint8 + uint8 3D */
723         MATLAB_HDR.StructureClass != mxINT16_CLASS &&
724         MATLAB_HDR.StructureClass != mxUINT16_CLASS &&    /* uint16 + uint16 3D */
725         MATLAB_HDR.StructureClass != mxINT32_CLASS &&
726         MATLAB_HDR.StructureClass != mxUINT32_CLASS &&    /* uint32 + uint32 3D */
727         MATLAB_HDR.StructureClass != mxINT64_CLASS &&
728         MATLAB_HDR.StructureClass != mxUINT64_CLASS)    /* uint64 + uint64 3D */
729       ThrowReaderException(CoderError,"UnsupportedCellTypeInTheMatrix");
730
731     switch (MATLAB_HDR.NameFlag)
732     {
733       case 0:
734         size = ReadBlobXXXLong(image2);  /* Object name string size */
735         size = 4 * (long) ((size + 3 + 1) / 4);
736         (void) SeekBlob(image2, size, SEEK_CUR);
737         break;
738       case 1:
739       case 2:
740       case 3:
741       case 4:
742         (void) ReadBlob(image2, 4, (unsigned char *) &size); /* Object name string */
743         break;
744       default:
745         goto MATLAB_KO;
746     }
747
748     CellType = ReadBlobXXXLong(image2);    /* Additional object type */
749     if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
750           "MATLAB_HDR.CellType: %ld",CellType);
751   
752     (void) ReadBlob(image2, 4, (unsigned char *) &size);     /* data size */
753
754       /* Image is gray when no complex flag is set and 2D Matrix */
755     if ((MATLAB_HDR.DimFlag == 8) &&
756         ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
757       image->type=GrayscaleType;
758
759     switch (CellType)
760     {
761       case miINT8:
762       case miUINT8:
763         sample_size = 8;
764         if(MATLAB_HDR.StructureFlag & FLAG_LOGICAL) 
765           image->depth = 1;
766         else
767           image->depth = 8;         /* Byte type cell */
768         ldblk = (long) MATLAB_HDR.SizeX;      
769         break;
770       case miINT16:
771       case miUINT16:
772         sample_size = 16;
773         image->depth = 16;        /* Word type cell */
774         ldblk = (long) (2 * MATLAB_HDR.SizeX);
775         break;
776       case miINT32:
777       case miUINT32:
778         sample_size = 32;
779         image->depth = 32;        /* Dword type cell */
780         ldblk = (long) (4 * MATLAB_HDR.SizeX);      
781         break;
782       case miINT64:
783       case miUINT64:
784         sample_size = 64;
785         image->depth = 64;        /* Qword type cell */
786         ldblk = (long) (8 * MATLAB_HDR.SizeX);      
787         break;   
788       case miSINGLE:
789         sample_size = 32;
790         image->depth = 32;        /* double type cell */
791         (void) SetImageOption(clone_info,"quantum:format","floating-point");
792         if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
793   {              /* complex float type cell */
794   }
795         ldblk = (long) (4 * MATLAB_HDR.SizeX);
796         break;
797       case miDOUBLE:
798         sample_size = 64; 
799         image->depth = 64;        /* double type cell */
800         (void) SetImageOption(clone_info,"quantum:format","floating-point");
801         if (sizeof(double) != 8)
802           ThrowReaderException(CoderError, "IncompatibleSizeOfDouble");
803         if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
804   {                         /* complex double type cell */        
805   }
806         ldblk = (long) (8 * MATLAB_HDR.SizeX);
807         break;
808       default:
809         ThrowReaderException(CoderError, "UnsupportedCellTypeInTheMatrix");
810     }
811     image->columns = MATLAB_HDR.SizeX;
812     image->rows = MATLAB_HDR.SizeY;    
813     quantum_info=AcquireQuantumInfo(clone_info,image);
814     if (quantum_info == (QuantumInfo *) NULL)
815       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
816     image->colors = 1l << image->depth;
817     if (image->columns == 0 || image->rows == 0)
818       goto MATLAB_KO;
819
820     /* ----- Create gray palette ----- */
821
822     if (CellType==miUINT8 && z!=3)
823     {
824       if(image->colors>256) image->colors = 256;
825
826       if (!AcquireImageColormap(image, image->colors))
827       {
828  NoMemory:ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");}
829     }
830
831     /*
832       If ping is true, then only set image size and colors without
833       reading any image data.
834     */
835     if (image_info->ping)
836     {
837       unsigned long temp = image->columns;
838       image->columns = image->rows;
839       image->rows = temp;
840       goto done_reading; /* !!!!!! BAD  !!!! */
841     }  
842
843   /* ----- Load raster data ----- */
844     BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(unsigned char *));    /* Ldblk was set in the check phase */
845     if (BImgBuff == NULL)
846       goto NoMemory;
847
848     MinVal = 0;
849     MaxVal = 0;
850     if (CellType==miDOUBLE || CellType==miSINGLE)        /* Find Min and Max Values for floats */
851     {
852       CalcMinMax(image2, image_info->endian,  MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &quantum_info->minimum, &quantum_info->maximum);
853     }
854
855     /* Main loop for reading all scanlines */
856     if(z==1) z=0; /* read grey scanlines */
857     /* else read color scanlines */
858     do
859     {
860       for (i = 0; i < (long) MATLAB_HDR.SizeY; i++)
861       {
862         q=QueueAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception);
863         if (q == (PixelPacket *)NULL)
864   {
865     if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
866               "  MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1));
867     goto done_reading;    /* Skip image rotation, when cannot set image pixels    */
868   }
869         if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk)
870   {
871     if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
872              "  MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1));
873     goto ExitLoop;
874   }
875         if((CellType==miINT8 || CellType==miUINT8) && (MATLAB_HDR.StructureFlag & FLAG_LOGICAL))
876         {
877           FixLogical((unsigned char *)BImgBuff,ldblk);
878           if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
879     {
880 ImportQuantumPixelsFailed:
881       if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
882               "  MAT failed to ImportQuantumPixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
883       break;
884     }
885         }
886         else
887         {
888           if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
889       goto ImportQuantumPixelsFailed;
890
891
892           if (z<=1 &&       /* fix only during a last pass z==0 || z==1 */
893           (CellType==miINT8 || CellType==miINT16 || CellType==miINT32 || CellType==miINT64))
894       FixSignedValues(q,MATLAB_HDR.SizeX);
895         }
896
897         if (!SyncAuthenticPixels(image,exception))
898   {
899     if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
900             "  MAT failed to sync image pixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
901     goto ExitLoop;
902   }
903       }
904     } while(z-- >= 2);
905 ExitLoop:
906
907
908     /* Read complex part of numbers here */
909     if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
910     {        /* Find Min and Max Values for complex parts of floats */
911       CellType = ReadBlobXXXLong(image2);    /* Additional object type */
912       i = ReadBlobXXXLong(image2);           /* size of a complex part - toss away*/
913
914       if (CellType==miDOUBLE || CellType==miSINGLE)
915       {
916         CalcMinMax(image2,  image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &MinVal, &MaxVal);      
917       }
918
919       if (CellType==miDOUBLE)
920         for (i = 0; i < (long) MATLAB_HDR.SizeY; i++)
921   {
922           ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff);
923           InsertComplexDoubleRow((double *)BImgBuff, i, image, MinVal, MaxVal);
924   }
925
926       if (CellType==miSINGLE)
927         for (i = 0; i < (long) MATLAB_HDR.SizeY; i++)
928   {
929           ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff);
930           InsertComplexFloatRow((float *)BImgBuff, i, image, MinVal, MaxVal);
931   }    
932     }
933
934       /* Image is gray when no complex flag is set and 2D Matrix AGAIN!!! */
935     if ((MATLAB_HDR.DimFlag == 8) &&
936         ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
937       image->type=GrayscaleType;
938     if (image->depth == 1)
939       image->type=BilevelType;
940
941     if(image2==image)
942         image2 = NULL;    /* Remove shadow copy to an image before rotation. */
943
944       /*  Rotate image. */
945     rotated_image = RotateImage(image, 90.0, exception);
946     if (rotated_image != (Image *) NULL)
947     {
948         /* Remove page offsets added by RotateImage */
949       rotated_image->page.x=0;
950       rotated_image->page.y=0;
951
952       blob = rotated_image->blob;
953       rotated_image->blob = image->blob;
954       rotated_image->colors = image->colors;
955       image->blob = blob;
956       AppendImageToList(&image,rotated_image);      
957       DeleteImageFromList(&image);      
958     }
959
960 done_reading:
961
962     if(image2!=NULL)
963       if(image2!=image)
964       {
965         DeleteImageFromList(&image2); 
966   if(clone_info)
967   {
968           if(clone_info->file)
969     {
970             fclose(clone_info->file);
971             clone_info->file = NULL;
972             (void) unlink(clone_info->filename);
973     }
974         }    
975       }
976
977       /* Allocate next image structure. */    
978     AcquireNextImage(image_info,image);
979     if (image->next == (Image *) NULL) break;                
980     image=SyncNextImageInList(image);
981     image->columns=image->rows=0;
982     image->colors=0;    
983
984       /* row scan buffer is no longer needed */
985     RelinquishMagickMemory(BImgBuff);
986     BImgBuff = NULL;
987   }
988     clone_info=DestroyImageInfo(clone_info);
989
990   RelinquishMagickMemory(BImgBuff);
991   CloseBlob(image);
992
993
994   {
995     Image *p;    
996     long scene=0;
997     
998     /*
999       Rewind list, removing any empty images while rewinding.
1000     */
1001     p=image;
1002     image=NULL;
1003     while (p != (Image *)NULL)
1004       {
1005         Image *tmp=p;
1006         if ((p->rows == 0) || (p->columns == 0)) {
1007           p=p->previous;
1008           DeleteImageFromList(&tmp);
1009         } else {
1010           image=p;
1011           p=p->previous;
1012         }
1013       }
1014     
1015     /*
1016       Fix scene numbers
1017     */
1018     for (p=image; p != (Image *) NULL; p=p->next)
1019       p->scene=scene++;
1020   }
1021
1022   if(clone_info != NULL)  /* cleanup garbage file from compression */
1023   {
1024     if(clone_info->file)
1025     {
1026       fclose(clone_info->file);
1027       clone_info->file = NULL;
1028       (void) unlink(clone_info->filename);
1029     }
1030     DestroyImageInfo(clone_info);
1031     clone_info = NULL;
1032   }
1033   if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),"return");
1034   if(image==NULL)
1035     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1036   return (image);
1037 }
1038 \f
1039 /*
1040 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1041 %                                                                             %
1042 %                                                                             %
1043 %                                                                             %
1044 %   R e g i s t e r M A T I m a g e                                           %
1045 %                                                                             %
1046 %                                                                             %
1047 %                                                                             %
1048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1049 %
1050 %  Method RegisterMATImage adds attributes for the MAT image format to
1051 %  the list of supported formats.  The attributes include the image format
1052 %  tag, a method to read and/or write the format, whether the format
1053 %  supports the saving of more than one frame to the same file or blob,
1054 %  whether the format supports native in-memory I/O, and a brief
1055 %  description of the format.
1056 %
1057 %  The format of the RegisterMATImage method is:
1058 %
1059 %      unsigned long RegisterMATImage(void)
1060 %
1061 */
1062 ModuleExport unsigned long RegisterMATImage(void)
1063 {
1064   MagickInfo
1065     *entry;
1066
1067   entry=SetMagickInfo("MAT");
1068   entry->decoder=(DecodeImageHandler *) ReadMATImage;
1069   entry->encoder=(EncodeImageHandler *) WriteMATImage;
1070   entry->blob_support=MagickFalse;
1071   entry->seekable_stream=MagickTrue;
1072   entry->description=AcquireString("MATLAB image format");
1073   entry->module=AcquireString("MAT");
1074   (void) RegisterMagickInfo(entry);
1075   return(MagickImageCoderSignature);
1076 }
1077 \f
1078 /*
1079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1080 %                                                                             %
1081 %                                                                             %
1082 %                                                                             %
1083 %   U n r e g i s t e r M A T I m a g e                                       %
1084 %                                                                             %
1085 %                                                                             %
1086 %                                                                             %
1087 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1088 %
1089 %  Method UnregisterMATImage removes format registrations made by the
1090 %  MAT module from the list of supported formats.
1091 %
1092 %  The format of the UnregisterMATImage method is:
1093 %
1094 %      UnregisterMATImage(void)
1095 %
1096 */
1097 ModuleExport void UnregisterMATImage(void)
1098 {
1099   (void) UnregisterMagickInfo("MAT");
1100 }
1101 \f
1102 /*
1103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1104 %                                                                             %
1105 %                                                                             %
1106 %                                                                             %
1107 %   W r i t e M A T L A B I m a g e                                           %
1108 %                                                                             %
1109 %                                                                             %
1110 %                                                                             %
1111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1112 %
1113 %  Function WriteMATImage writes an Matlab matrix to a file.  
1114 %
1115 %  The format of the WriteMATImage method is:
1116 %
1117 %      unsigned int WriteMATImage(const ImageInfo *image_info,Image *image)
1118 %
1119 %  A description of each parameter follows.
1120 %
1121 %    o status: Function WriteMATImage return True if the image is written.
1122 %      False is returned is there is a memory shortage or if the image file
1123 %      fails to write.
1124 %
1125 %    o image_info: Specifies a pointer to a ImageInfo structure.
1126 %
1127 %    o image:  A pointer to an Image structure.
1128 %
1129 */
1130 static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image)
1131 {
1132   ExceptionInfo
1133     *exception;
1134
1135   long y;
1136   unsigned z;
1137   const PixelPacket *p;
1138
1139   unsigned int status;
1140   int logging;
1141   unsigned long DataSize;
1142   char padding;
1143   char MATLAB_HDR[0x80];
1144   time_t current_time;
1145   struct tm local_time;
1146   unsigned char *pixels;
1147   int is_gray;
1148
1149   MagickOffsetType
1150     scene;
1151
1152   QuantumInfo
1153     *quantum_info;
1154
1155   /*
1156     Open output image file.
1157   */
1158   assert(image_info != (const ImageInfo *) NULL);
1159   assert(image_info->signature == MagickSignature);
1160   assert(image != (Image *) NULL);
1161   assert(image->signature == MagickSignature);
1162   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"enter MAT");
1163   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1164   if (status == MagickFalse)
1165     return(MagickFalse);
1166   image->depth=8;
1167
1168   current_time=time((time_t *) NULL);
1169 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1170   (void) localtime_r(&current_time,&local_time);
1171 #else
1172   (void) memcpy(&local_time,localtime(&current_time),sizeof(local_time));
1173 #endif
1174   (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124));
1175   FormatMagickString(MATLAB_HDR,MaxTextExtent,"MATLAB 5.0 MAT-file, Platform: %s, Created on: %s %s %2d %2d:%2d:%2d %d",
1176     OsDesc,DayOfWTab[local_time.tm_wday],MonthsTab[local_time.tm_mon],
1177     local_time.tm_mday,local_time.tm_hour,local_time.tm_min,
1178     local_time.tm_sec,local_time.tm_year+1900);
1179   MATLAB_HDR[0x7C]=0;
1180   MATLAB_HDR[0x7D]=1;
1181   MATLAB_HDR[0x7E]='I';
1182   MATLAB_HDR[0x7F]='M';
1183   (void) WriteBlob(image,sizeof(MATLAB_HDR),(unsigned char *) MATLAB_HDR);
1184   scene=0;
1185   do
1186   {
1187     if (image->colorspace != RGBColorspace)
1188       (void) TransformImageColorspace(image,RGBColorspace);
1189
1190     is_gray = IsGrayImage(image,&image->exception);
1191     z = is_gray ? 0 : 3;
1192
1193     /*
1194       Store MAT header.
1195     */
1196     DataSize = image->rows /*Y*/ * image->columns /*X*/;
1197     if(!is_gray) DataSize *= 3 /*Z*/;
1198     padding=((unsigned char)(DataSize-1) & 0x7) ^ 0x7;
1199
1200     (void) WriteBlobLSBLong(image, miMATRIX);
1201     (void) WriteBlobLSBLong(image, DataSize+padding+(is_gray ? 48l : 56l));
1202     (void) WriteBlobLSBLong(image, 0x6); /* 0x88 */
1203     (void) WriteBlobLSBLong(image, 0x8); /* 0x8C */
1204     (void) WriteBlobLSBLong(image, 0x6); /* 0x90 */  
1205     (void) WriteBlobLSBLong(image, 0);   
1206     (void) WriteBlobLSBLong(image, 0x5); /* 0x98 */
1207     (void) WriteBlobLSBLong(image, is_gray ? 0x8 : 0xC); /* 0x9C - DimFlag */
1208     (void) WriteBlobLSBLong(image, image->rows);    /* x: 0xA0 */  
1209     (void) WriteBlobLSBLong(image, image->columns); /* y: 0xA4 */  
1210     if(!is_gray)
1211     {
1212       (void) WriteBlobLSBLong(image, 3); /* z: 0xA8 */  
1213       (void) WriteBlobLSBLong(image, 0);
1214     }
1215     (void) WriteBlobLSBShort(image, 1);  /* 0xB0 */  
1216     (void) WriteBlobLSBShort(image, 1);  /* 0xB2 */
1217     (void) WriteBlobLSBLong(image, 'M'); /* 0xB4 */
1218     (void) WriteBlobLSBLong(image, 0x2); /* 0xB8 */  
1219     (void) WriteBlobLSBLong(image, DataSize); /* 0xBC */
1220
1221     /*
1222       Store image data.
1223     */
1224     exception=(&image->exception);
1225     quantum_info=AcquireQuantumInfo(image_info,image);
1226     if (quantum_info == (QuantumInfo *) NULL)
1227       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1228     pixels=GetQuantumPixels(quantum_info);
1229     do
1230     {
1231       for (y=0; y < (long)image->columns; y++)
1232       {
1233         p=GetVirtualPixels(image,y,0,1,image->rows,&image->exception);
1234         if (p == (const PixelPacket *) NULL)
1235           break;
1236         (void) ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
1237           z2qtype[z],pixels,exception);
1238         (void) WriteBlob(image,image->rows,pixels);
1239       }    
1240       if (!SyncAuthenticPixels(image,exception))
1241         break;
1242     } while(z-- >= 2);
1243     while(padding-->0) (void) WriteBlobByte(image,0);
1244     quantum_info=DestroyQuantumInfo(quantum_info);
1245     if (GetNextImageInList(image) == (Image *) NULL)
1246       break;
1247     image=SyncNextImageInList(image);
1248     status=SetImageProgress(image,SaveImagesTag,scene++,
1249       GetImageListLength(image));
1250     if (status == MagickFalse)
1251       break;
1252   } while (image_info->adjoin != MagickFalse);
1253   (void) CloseBlob(image);
1254   return(MagickTrue);
1255 }