blob: f5a852b0ac96da8cfd88fb8a275cc86316b51063 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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
47/*
48 Include declarations.
49*/
cristy4c08aed2011-07-01 19:47:50 +000050#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"
cristy510d06a2011-07-06 23:43:54 +000057#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000058#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/image.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/list.h"
63#include "MagickCore/magick.h"
64#include "MagickCore/memory_.h"
65#include "MagickCore/monitor.h"
66#include "MagickCore/monitor-private.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/quantum-private.h"
69#include "MagickCore/option.h"
70#include "MagickCore/pixel.h"
71#include "MagickCore/resource_.h"
72#include "MagickCore/shear.h"
73#include "MagickCore/static.h"
74#include "MagickCore/string_.h"
75#include "MagickCore/module.h"
76#include "MagickCore/transform.h"
cristy18c6c272011-09-23 14:40:37 +000077#include "MagickCore/utility-private.h"
cristy3ed852e2009-09-05 21:47:34 +000078#if defined(MAGICKCORE_ZLIB_DELEGATE)
79 #include "zlib.h"
80#endif
81
82/*
83 Forward declaration.
84*/
85static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000086 WriteMATImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000087
88
89/* Auto coloring method, sorry this creates some artefact inside data
90MinReal+j*MaxComplex = red MaxReal+j*MaxComplex = black
91MinReal+j*0 = white MaxReal+j*0 = black
92MinReal+j*MinComplex = blue MaxReal+j*MinComplex = black
93*/
94
95typedef struct
96{
97 char identific[124];
98 unsigned short Version;
99 char EndianIndicator[2];
cristyf6fe0a12010-05-30 00:44:47 +0000100 unsigned long DataType;
101 unsigned long ObjectSize;
102 unsigned long unknown1;
103 unsigned long unknown2;
cristy3ed852e2009-09-05 21:47:34 +0000104
105 unsigned short unknown5;
106 unsigned char StructureFlag;
107 unsigned char StructureClass;
cristyf6fe0a12010-05-30 00:44:47 +0000108 unsigned long unknown3;
109 unsigned long unknown4;
110 unsigned long DimFlag;
cristy3ed852e2009-09-05 21:47:34 +0000111
cristyf6fe0a12010-05-30 00:44:47 +0000112 unsigned long SizeX;
113 unsigned long SizeY;
cristy3ed852e2009-09-05 21:47:34 +0000114 unsigned short Flag1;
115 unsigned short NameFlag;
116}
117MATHeader;
118
119static const char *MonthsTab[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
120static const char *DayOfWTab[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
121static const char *OsDesc=
122#ifdef __WIN32__
123 "PCWIN";
124#else
125 #ifdef __APPLE__
126 "MAC";
127 #else
128 "LNX86";
129 #endif
130#endif
131
132typedef enum
133 {
cristyc5de6992009-10-06 19:19:48 +0000134 miINT8 = 1, /* 8 bit signed */
135 miUINT8, /* 8 bit unsigned */
136 miINT16, /* 16 bit signed */
137 miUINT16, /* 16 bit unsigned */
138 miINT32, /* 32 bit signed */
139 miUINT32, /* 32 bit unsigned */
140 miSINGLE, /* IEEE 754 single precision float */
cristy3ed852e2009-09-05 21:47:34 +0000141 miRESERVE1,
cristyc5de6992009-10-06 19:19:48 +0000142 miDOUBLE, /* IEEE 754 double precision float */
cristy3ed852e2009-09-05 21:47:34 +0000143 miRESERVE2,
144 miRESERVE3,
cristyc5de6992009-10-06 19:19:48 +0000145 miINT64, /* 64 bit signed */
146 miUINT64, /* 64 bit unsigned */
147 miMATRIX, /* MATLAB array */
148 miCOMPRESSED, /* Compressed Data */
149 miUTF8, /* Unicode UTF-8 Encoded Character Data */
150 miUTF16, /* Unicode UTF-16 Encoded Character Data */
151 miUTF32 /* Unicode UTF-32 Encoded Character Data */
cristy3ed852e2009-09-05 21:47:34 +0000152 } mat5_data_type;
153
154typedef enum
155 {
cristyc5de6992009-10-06 19:19:48 +0000156 mxCELL_CLASS=1, /* cell array */
157 mxSTRUCT_CLASS, /* structure */
158 mxOBJECT_CLASS, /* object */
159 mxCHAR_CLASS, /* character array */
160 mxSPARSE_CLASS, /* sparse array */
161 mxDOUBLE_CLASS, /* double precision array */
162 mxSINGLE_CLASS, /* single precision floating point */
163 mxINT8_CLASS, /* 8 bit signed integer */
164 mxUINT8_CLASS, /* 8 bit unsigned integer */
165 mxINT16_CLASS, /* 16 bit signed integer */
166 mxUINT16_CLASS, /* 16 bit unsigned integer */
167 mxINT32_CLASS, /* 32 bit signed integer */
168 mxUINT32_CLASS, /* 32 bit unsigned integer */
169 mxINT64_CLASS, /* 64 bit signed integer */
170 mxUINT64_CLASS, /* 64 bit unsigned integer */
cristy3ed852e2009-09-05 21:47:34 +0000171 mxFUNCTION_CLASS /* Function handle */
172 } arrayclasstype;
173
174#define FLAG_COMPLEX 0x8
175#define FLAG_GLOBAL 0x4
176#define FLAG_LOGICAL 0x2
177
178static const QuantumType z2qtype[4] = {GrayQuantum, BlueQuantum, GreenQuantum, RedQuantum};
179
180
181static void InsertComplexDoubleRow(double *p, int y, Image * image, double MinVal,
182 double MaxVal)
183{
184 ExceptionInfo
185 *exception;
186
187 double f;
188 int x;
cristy4c08aed2011-07-01 19:47:50 +0000189 register Quantum *q;
cristy3ed852e2009-09-05 21:47:34 +0000190
191 if (MinVal == 0)
192 MinVal = -1;
193 if (MaxVal == 0)
194 MaxVal = 1;
195
196 exception=(&image->exception);
cristy524222d2011-04-25 00:37:06 +0000197 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000198 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000199 return;
cristybb503372010-05-27 20:51:26 +0000200 for (x = 0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000201 {
202 if (*p > 0)
203 {
cristy4c08aed2011-07-01 19:47:50 +0000204 f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q));
205 if (f + GetPixelRed(image,q) > QuantumRange)
206 SetPixelRed(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000207 else
cristy4c08aed2011-07-01 19:47:50 +0000208 SetPixelRed(image,GetPixelRed(image,q)+(int) f,q);
209 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000210 {
cristy4c08aed2011-07-01 19:47:50 +0000211 SetPixelGreen(image,0,q);
212 SetPixelBlue(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000213 }
cristy3ed852e2009-09-05 21:47:34 +0000214 else
cristy524222d2011-04-25 00:37:06 +0000215 {
cristy4c08aed2011-07-01 19:47:50 +0000216 SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q);
217 SetPixelGreen(image,GetPixelBlue(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000218 }
cristy3ed852e2009-09-05 21:47:34 +0000219 }
220 if (*p < 0)
221 {
cristy4c08aed2011-07-01 19:47:50 +0000222 f = (*p / MaxVal) * (QuantumRange-GetPixelBlue(image,q));
223 if (f+GetPixelBlue(image,q) > QuantumRange)
224 SetPixelBlue(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000225 else
cristy4c08aed2011-07-01 19:47:50 +0000226 SetPixelBlue(image,GetPixelBlue(image,q)+(int) f,q);
227 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000228 {
cristy4c08aed2011-07-01 19:47:50 +0000229 SetPixelRed(image,0,q);
230 SetPixelGreen(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000231 }
cristy3ed852e2009-09-05 21:47:34 +0000232 else
cristy524222d2011-04-25 00:37:06 +0000233 {
cristy4c08aed2011-07-01 19:47:50 +0000234 SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q);
235 SetPixelGreen(image,GetPixelRed(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000236 }
cristy3ed852e2009-09-05 21:47:34 +0000237 }
238 p++;
cristyed231572011-07-14 02:18:59 +0000239 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000240 }
241 if (!SyncAuthenticPixels(image,exception))
242 return;
cristy3ed852e2009-09-05 21:47:34 +0000243 return;
244}
245
246
247static void InsertComplexFloatRow(float *p, int y, Image * image, double MinVal,
248 double MaxVal)
249{
250 ExceptionInfo
251 *exception;
252
253 double f;
254 int x;
cristy4c08aed2011-07-01 19:47:50 +0000255 register Quantum *q;
cristy3ed852e2009-09-05 21:47:34 +0000256
257 if (MinVal == 0)
258 MinVal = -1;
259 if (MaxVal == 0)
260 MaxVal = 1;
261
262 exception=(&image->exception);
263 q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000264 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000265 return;
cristybb503372010-05-27 20:51:26 +0000266 for (x = 0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000267 {
268 if (*p > 0)
269 {
cristy4c08aed2011-07-01 19:47:50 +0000270 f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q));
271 if (f+GetPixelRed(image,q) > QuantumRange)
272 SetPixelRed(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000273 else
cristy4c08aed2011-07-01 19:47:50 +0000274 SetPixelRed(image,GetPixelRed(image,q)+(int) f,q);
275 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000276 {
cristy4c08aed2011-07-01 19:47:50 +0000277 SetPixelGreen(image,0,q);
278 SetPixelBlue(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000279 }
cristy3ed852e2009-09-05 21:47:34 +0000280 else
cristy524222d2011-04-25 00:37:06 +0000281 {
cristy4c08aed2011-07-01 19:47:50 +0000282 SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q);
283 SetPixelGreen(image,GetPixelBlue(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000284 }
cristy3ed852e2009-09-05 21:47:34 +0000285 }
286 if (*p < 0)
287 {
cristy4c08aed2011-07-01 19:47:50 +0000288 f = (*p / MaxVal) * (QuantumRange - GetPixelBlue(image,q));
289 if (f + GetPixelBlue(image,q) > QuantumRange)
290 SetPixelBlue(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000291 else
cristy4c08aed2011-07-01 19:47:50 +0000292 SetPixelBlue(image,GetPixelBlue(image,q)+
293 (int) f,q);
294 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000295 {
cristy4c08aed2011-07-01 19:47:50 +0000296 SetPixelGreen(image,0,q);
297 SetPixelRed(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000298 }
cristy3ed852e2009-09-05 21:47:34 +0000299 else
cristy524222d2011-04-25 00:37:06 +0000300 {
cristy4c08aed2011-07-01 19:47:50 +0000301 SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q);
302 SetPixelGreen(image,GetPixelRed(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000303 }
cristy3ed852e2009-09-05 21:47:34 +0000304 }
305 p++;
306 q++;
307 }
308 if (!SyncAuthenticPixels(image,exception))
309 return;
cristy3ed852e2009-09-05 21:47:34 +0000310 return;
311}
312
313
314/************** READERS ******************/
315
316/* This function reads one block of floats*/
317static void ReadBlobFloatsLSB(Image * image, size_t len, float *data)
318{
319 while (len >= 4)
320 {
321 *data++ = ReadBlobFloat(image);
322 len -= sizeof(float);
323 }
324 if (len > 0)
325 (void) SeekBlob(image, len, SEEK_CUR);
326}
327
328static void ReadBlobFloatsMSB(Image * image, size_t len, float *data)
329{
330 while (len >= 4)
331 {
332 *data++ = ReadBlobFloat(image);
333 len -= sizeof(float);
334 }
335 if (len > 0)
336 (void) SeekBlob(image, len, SEEK_CUR);
337}
338
339/* This function reads one block of doubles*/
340static void ReadBlobDoublesLSB(Image * image, size_t len, double *data)
341{
342 while (len >= 8)
343 {
344 *data++ = ReadBlobDouble(image);
345 len -= sizeof(double);
346 }
347 if (len > 0)
348 (void) SeekBlob(image, len, SEEK_CUR);
349}
350
351static void ReadBlobDoublesMSB(Image * image, size_t len, double *data)
352{
353 while (len >= 8)
354 {
355 *data++ = ReadBlobDouble(image);
356 len -= sizeof(double);
357 }
358 if (len > 0)
359 (void) SeekBlob(image, len, SEEK_CUR);
360}
361
362/* Calculate minimum and maximum from a given block of data */
cristybb503372010-05-27 20:51:26 +0000363static void CalcMinMax(Image *image, int endian_indicator, int SizeX, int SizeY, size_t CellType, unsigned ldblk, void *BImgBuff, double *Min, double *Max)
cristy3ed852e2009-09-05 21:47:34 +0000364{
365MagickOffsetType filepos;
366int i, x;
367void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
368void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
369double *dblrow;
370float *fltrow;
371
372 if (endian_indicator == LSBEndian)
373 {
374 ReadBlobDoublesXXX = ReadBlobDoublesLSB;
375 ReadBlobFloatsXXX = ReadBlobFloatsLSB;
376 }
cristyc5de6992009-10-06 19:19:48 +0000377 else /* MI */
cristy3ed852e2009-09-05 21:47:34 +0000378 {
379 ReadBlobDoublesXXX = ReadBlobDoublesMSB;
380 ReadBlobFloatsXXX = ReadBlobFloatsMSB;
381 }
382
cristyc5de6992009-10-06 19:19:48 +0000383 filepos = TellBlob(image); /* Please note that file seeking occurs only in the case of doubles */
cristy3ed852e2009-09-05 21:47:34 +0000384 for (i = 0; i < SizeY; i++)
385 {
386 if (CellType==miDOUBLE)
387 {
388 ReadBlobDoublesXXX(image, ldblk, (double *)BImgBuff);
389 dblrow = (double *)BImgBuff;
390 if (i == 0)
391 {
392 *Min = *Max = *dblrow;
393 }
394 for (x = 0; x < SizeX; x++)
395 {
396 if (*Min > *dblrow)
397 *Min = *dblrow;
398 if (*Max < *dblrow)
399 *Max = *dblrow;
400 dblrow++;
401 }
402 }
403 if (CellType==miSINGLE)
404 {
405 ReadBlobFloatsXXX(image, ldblk, (float *)BImgBuff);
406 fltrow = (float *)BImgBuff;
407 if (i == 0)
408 {
409 *Min = *Max = *fltrow;
410 }
cristybb503372010-05-27 20:51:26 +0000411 for (x = 0; x < (ssize_t) SizeX; x++)
cristy3ed852e2009-09-05 21:47:34 +0000412 {
413 if (*Min > *fltrow)
414 *Min = *fltrow;
415 if (*Max < *fltrow)
416 *Max = *fltrow;
417 fltrow++;
418 }
419 }
420 }
421 (void) SeekBlob(image, filepos, SEEK_SET);
422}
423
424
cristy4c08aed2011-07-01 19:47:50 +0000425static void FixSignedValues(const Image *image,Quantum *q, int y)
cristy3ed852e2009-09-05 21:47:34 +0000426{
427 while(y-->0)
428 {
429 /* Please note that negative values will overflow
430 Q=8; QuantumRange=255: <0;127> + 127+1 = <128; 255>
cristyc5de6992009-10-06 19:19:48 +0000431 <-1;-128> + 127+1 = <0; 127> */
cristy4c08aed2011-07-01 19:47:50 +0000432 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);
cristy3ed852e2009-09-05 21:47:34 +0000435 q++;
436 }
437}
438
439
440/** Fix whole row of logical/binary data. It means pack it. */
441static void FixLogical(unsigned char *Buff,int ldblk)
442{
443unsigned char mask=128;
444unsigned char *BuffL = Buff;
445unsigned char val = 0;
446
447 while(ldblk-->0)
448 {
449 if(*Buff++ != 0)
450 val |= mask;
451
452 mask >>= 1;
453 if(mask==0)
454 {
455 *BuffL++ = val;
456 val = 0;
457 mask = 128;
458 }
459
460 }
461 *BuffL = val;
462}
463
464#if defined(MAGICKCORE_ZLIB_DELEGATE)
465static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
466 unsigned int size)
467{
468 (void) context;
469 return((voidpf) AcquireQuantumMemory(items,size));
470}
471
472static void RelinquishZIPMemory(voidpf context,voidpf memory)
473{
474 (void) context;
475 memory=RelinquishMagickMemory(memory);
476}
477#endif
478
cristy0906b142011-02-21 01:10:00 +0000479#if defined(MAGICKCORE_ZLIB_DELEGATE)
cristy3ed852e2009-09-05 21:47:34 +0000480/** This procedure decompreses an image block for a new MATLAB format. */
481static Image *DecompressBlock(Image *orig, MagickOffsetType Size, ImageInfo *clone_info, ExceptionInfo *exception)
482{
cristy3ed852e2009-09-05 21:47:34 +0000483
484Image *image2;
485void *CacheBlock, *DecompressBlock;
486z_stream zip_info;
487FILE *mat_file;
488size_t magick_size;
cristy95236b52009-12-30 21:56:45 +0000489size_t extent;
cristy3ed852e2009-09-05 21:47:34 +0000490
491int status;
492
493 if(clone_info==NULL) return NULL;
cristyc5de6992009-10-06 19:19:48 +0000494 if(clone_info->file) /* Close file opened from previous transaction. */
cristy3ed852e2009-09-05 21:47:34 +0000495 {
496 fclose(clone_info->file);
497 clone_info->file = NULL;
cristy18c6c272011-09-23 14:40:37 +0000498 (void) remove_utf8(clone_info->filename);
cristy3ed852e2009-09-05 21:47:34 +0000499 }
500
501 CacheBlock = AcquireQuantumMemory((size_t)((Size<16384)?Size:16384),sizeof(unsigned char *));
502 if(CacheBlock==NULL) return NULL;
503 DecompressBlock = AcquireQuantumMemory((size_t)(4096),sizeof(unsigned char *));
504 if(DecompressBlock==NULL)
505 {
506 RelinquishMagickMemory(CacheBlock);
507 return NULL;
508 }
509
510 mat_file = fdopen(AcquireUniqueFileResource(clone_info->filename),"w");
511 if(!mat_file)
512 {
513 RelinquishMagickMemory(CacheBlock);
514 RelinquishMagickMemory(DecompressBlock);
515 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Gannot create file stream for PS image");
516 return NULL;
517 }
518
519 zip_info.zalloc=AcquireZIPMemory;
520 zip_info.zfree=RelinquishZIPMemory;
521 zip_info.opaque = (voidpf) NULL;
522 inflateInit(&zip_info);
523 /* zip_info.next_out = 8*4;*/
524
525 zip_info.avail_in = 0;
526 zip_info.total_out = 0;
527 while(Size>0 && !EOFBlob(orig))
528 {
529 magick_size = ReadBlob(orig, (Size<16384)?Size:16384, (unsigned char *) CacheBlock);
530 zip_info.next_in = (Bytef *) CacheBlock;
531 zip_info.avail_in = (uInt) magick_size;
532
533 while(zip_info.avail_in>0)
534 {
535 zip_info.avail_out = 4096;
536 zip_info.next_out = (Bytef *) DecompressBlock;
537 status = inflate(&zip_info,Z_NO_FLUSH);
cristy95236b52009-12-30 21:56:45 +0000538 extent=fwrite(DecompressBlock, 4096-zip_info.avail_out, 1, mat_file);
cristyda16f162011-02-19 23:52:17 +0000539 (void) extent;
cristy3ed852e2009-09-05 21:47:34 +0000540
541 if(status == Z_STREAM_END) goto DblBreak;
542 }
543
544 Size -= magick_size;
545 }
546DblBreak:
547
548 (void)fclose(mat_file);
549 RelinquishMagickMemory(CacheBlock);
550 RelinquishMagickMemory(DecompressBlock);
551
552 if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL) goto UnlinkFile;
cristy9950d572011-10-01 18:22:35 +0000553 if( (image2 = AcquireImage(clone_info,exception))==NULL ) goto EraseFile;
cristy3ed852e2009-09-05 21:47:34 +0000554 status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception);
555 if (status == MagickFalse)
556 {
557 DeleteImageFromList(&image2);
558EraseFile:
559 fclose(clone_info->file);
560 clone_info->file = NULL;
561UnlinkFile:
cristy18c6c272011-09-23 14:40:37 +0000562 (void) remove_utf8(clone_info->filename);
cristy3ed852e2009-09-05 21:47:34 +0000563 return NULL;
564 }
565
566 return image2;
cristy3ed852e2009-09-05 21:47:34 +0000567}
cristy0906b142011-02-21 01:10:00 +0000568#endif
cristy3ed852e2009-09-05 21:47:34 +0000569
570/*
571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572% %
573% %
574% %
575% R e a d M A T L A B i m a g e %
576% %
577% %
578% %
579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580%
581% ReadMATImage() reads an MAT X image file and returns it. It
582% allocates the memory necessary for the new Image structure and returns a
583% pointer to the new image.
584%
585% The format of the ReadMATImage method is:
586%
587% Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
588%
589% A description of each parameter follows:
590%
591% o image: Method ReadMATImage returns a pointer to the image after
592% reading. A null image is returned if there is a memory shortage or if
593% the image cannot be read.
594%
595% o image_info: Specifies a pointer to a ImageInfo structure.
596%
597% o exception: return any errors or warnings in this structure.
598%
599*/
600
601static inline size_t MagickMin(const size_t x,const size_t y)
602{
603 if (x < y)
604 return(x);
605 return(y);
606}
607
608static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
609{
610 Image *image, *image2=NULL,
611 *rotated_image;
cristy4c08aed2011-07-01 19:47:50 +0000612 register Quantum *q;
cristy3ed852e2009-09-05 21:47:34 +0000613
614 unsigned int status;
615 MATHeader MATLAB_HDR;
cristybb503372010-05-27 20:51:26 +0000616 size_t size;
617 size_t CellType;
cristy3ed852e2009-09-05 21:47:34 +0000618 QuantumInfo *quantum_info;
619 ImageInfo *clone_info;
620 int i;
cristybb503372010-05-27 20:51:26 +0000621 ssize_t ldblk;
cristy3ed852e2009-09-05 21:47:34 +0000622 unsigned char *BImgBuff = NULL;
623 double MinVal, MaxVal;
cristybb503372010-05-27 20:51:26 +0000624 size_t Unknown6;
cristy3ed852e2009-09-05 21:47:34 +0000625 unsigned z;
626 int logging;
627 int sample_size;
628 MagickOffsetType filepos=0x80;
629 BlobInfo *blob;
cristyeaedf062010-05-29 22:36:02 +0000630 size_t one;
cristy3ed852e2009-09-05 21:47:34 +0000631
632 unsigned int (*ReadBlobXXXLong)(Image *image);
633 unsigned short (*ReadBlobXXXShort)(Image *image);
634 void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
635 void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
636
637
638 assert(image_info != (const ImageInfo *) NULL);
639 assert(image_info->signature == MagickSignature);
640 assert(exception != (ExceptionInfo *) NULL);
641 assert(exception->signature == MagickSignature);
642 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter");
643
644 /*
645 Open image file.
646 */
cristy9950d572011-10-01 18:22:35 +0000647 image = AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000648
649 status = OpenBlob(image_info, image, ReadBinaryBlobMode, exception);
650 if (status == MagickFalse)
651 {
652 image=DestroyImageList(image);
653 return((Image *) NULL);
654 }
655 /*
656 Read MATLAB image.
657 */
658 clone_info=CloneImageInfo(image_info);
659 if(ReadBlob(image,124,(unsigned char *) &MATLAB_HDR.identific) != 124)
660 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
661 MATLAB_HDR.Version = ReadBlobLSBShort(image);
662 if(ReadBlob(image,2,(unsigned char *) &MATLAB_HDR.EndianIndicator) != 2)
663 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
664
665 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule()," Endian %c%c",
666 MATLAB_HDR.EndianIndicator[0],MATLAB_HDR.EndianIndicator[1]);
667 if (!strncmp(MATLAB_HDR.EndianIndicator, "IM", 2))
668 {
669 ReadBlobXXXLong = ReadBlobLSBLong;
670 ReadBlobXXXShort = ReadBlobLSBShort;
671 ReadBlobDoublesXXX = ReadBlobDoublesLSB;
672 ReadBlobFloatsXXX = ReadBlobFloatsLSB;
673 image->endian = LSBEndian;
674 }
675 else if (!strncmp(MATLAB_HDR.EndianIndicator, "MI", 2))
676 {
677 ReadBlobXXXLong = ReadBlobMSBLong;
678 ReadBlobXXXShort = ReadBlobMSBShort;
679 ReadBlobDoublesXXX = ReadBlobDoublesMSB;
680 ReadBlobFloatsXXX = ReadBlobFloatsMSB;
681 image->endian = MSBEndian;
682 }
683 else
684 goto MATLAB_KO; /* unsupported endian */
685
686 if (strncmp(MATLAB_HDR.identific, "MATLAB", 6))
687MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
688
689 filepos = TellBlob(image);
690 while(!EOFBlob(image)) /* object parser loop */
691 {
692 (void) SeekBlob(image,filepos,SEEK_SET);
693 /* printf("pos=%X\n",TellBlob(image)); */
694
695 MATLAB_HDR.DataType = ReadBlobXXXLong(image);
696 if(EOFBlob(image)) break;
697 MATLAB_HDR.ObjectSize = ReadBlobXXXLong(image);
698 if(EOFBlob(image)) break;
699 filepos += MATLAB_HDR.ObjectSize + 4 + 4;
700
701 image2 = image;
702#if defined(MAGICKCORE_ZLIB_DELEGATE)
703 if(MATLAB_HDR.DataType == miCOMPRESSED)
704 {
705 image2 = DecompressBlock(image,MATLAB_HDR.ObjectSize,clone_info,exception);
706 if(image2==NULL) continue;
707 MATLAB_HDR.DataType = ReadBlobXXXLong(image2); /* replace compressed object type. */
708 }
709#endif
710
711 if(MATLAB_HDR.DataType!=miMATRIX) continue; /* skip another objects. */
712
713 MATLAB_HDR.unknown1 = ReadBlobXXXLong(image2);
714 MATLAB_HDR.unknown2 = ReadBlobXXXLong(image2);
715
716 MATLAB_HDR.unknown5 = ReadBlobXXXLong(image2);
717 MATLAB_HDR.StructureClass = MATLAB_HDR.unknown5 & 0xFF;
718 MATLAB_HDR.StructureFlag = (MATLAB_HDR.unknown5>>8) & 0xFF;
719
720 MATLAB_HDR.unknown3 = ReadBlobXXXLong(image2);
721 if(image!=image2)
cristyc5de6992009-10-06 19:19:48 +0000722 MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2); /* ??? don't understand why ?? */
cristy3ed852e2009-09-05 21:47:34 +0000723 MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2);
724 MATLAB_HDR.DimFlag = ReadBlobXXXLong(image2);
725 MATLAB_HDR.SizeX = ReadBlobXXXLong(image2);
726 MATLAB_HDR.SizeY = ReadBlobXXXLong(image2);
727
728
729 switch(MATLAB_HDR.DimFlag)
730 {
cristyc5de6992009-10-06 19:19:48 +0000731 case 8: z=1; break; /* 2D matrix*/
732 case 12: z = ReadBlobXXXLong(image2); /* 3D matrix RGB*/
733 Unknown6 = ReadBlobXXXLong(image2);
cristyda16f162011-02-19 23:52:17 +0000734 (void) Unknown6;
cristyc5de6992009-10-06 19:19:48 +0000735 if(z!=3) ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
736 break;
cristy3ed852e2009-09-05 21:47:34 +0000737 default: ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
738 }
739
740 MATLAB_HDR.Flag1 = ReadBlobXXXShort(image2);
741 MATLAB_HDR.NameFlag = ReadBlobXXXShort(image2);
742
743 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
744 "MATLAB_HDR.StructureClass %d",MATLAB_HDR.StructureClass);
745 if (MATLAB_HDR.StructureClass != mxCHAR_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000746 MATLAB_HDR.StructureClass != mxSINGLE_CLASS && /* float + complex float */
747 MATLAB_HDR.StructureClass != mxDOUBLE_CLASS && /* double + complex double */
cristy3ed852e2009-09-05 21:47:34 +0000748 MATLAB_HDR.StructureClass != mxINT8_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000749 MATLAB_HDR.StructureClass != mxUINT8_CLASS && /* uint8 + uint8 3D */
cristy3ed852e2009-09-05 21:47:34 +0000750 MATLAB_HDR.StructureClass != mxINT16_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000751 MATLAB_HDR.StructureClass != mxUINT16_CLASS && /* uint16 + uint16 3D */
cristy3ed852e2009-09-05 21:47:34 +0000752 MATLAB_HDR.StructureClass != mxINT32_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000753 MATLAB_HDR.StructureClass != mxUINT32_CLASS && /* uint32 + uint32 3D */
cristy3ed852e2009-09-05 21:47:34 +0000754 MATLAB_HDR.StructureClass != mxINT64_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000755 MATLAB_HDR.StructureClass != mxUINT64_CLASS) /* uint64 + uint64 3D */
cristy3ed852e2009-09-05 21:47:34 +0000756 ThrowReaderException(CoderError,"UnsupportedCellTypeInTheMatrix");
757
758 switch (MATLAB_HDR.NameFlag)
759 {
760 case 0:
cristyc5de6992009-10-06 19:19:48 +0000761 size = ReadBlobXXXLong(image2); /* Object name string size */
cristybb503372010-05-27 20:51:26 +0000762 size = 4 * (ssize_t) ((size + 3 + 1) / 4);
cristy3ed852e2009-09-05 21:47:34 +0000763 (void) SeekBlob(image2, size, SEEK_CUR);
764 break;
765 case 1:
766 case 2:
767 case 3:
768 case 4:
769 (void) ReadBlob(image2, 4, (unsigned char *) &size); /* Object name string */
770 break;
771 default:
772 goto MATLAB_KO;
773 }
774
775 CellType = ReadBlobXXXLong(image2); /* Additional object type */
cristyf2faecf2010-05-28 19:19:36 +0000776 if (logging)
777 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000778 "MATLAB_HDR.CellType: %.20g",(double) CellType);
cristy3ed852e2009-09-05 21:47:34 +0000779
780 (void) ReadBlob(image2, 4, (unsigned char *) &size); /* data size */
781
782 /* Image is gray when no complex flag is set and 2D Matrix */
783 if ((MATLAB_HDR.DimFlag == 8) &&
784 ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
785 image->type=GrayscaleType;
786
787 switch (CellType)
788 {
789 case miINT8:
790 case miUINT8:
791 sample_size = 8;
792 if(MATLAB_HDR.StructureFlag & FLAG_LOGICAL)
793 image->depth = 1;
794 else
795 image->depth = 8; /* Byte type cell */
cristybb503372010-05-27 20:51:26 +0000796 ldblk = (ssize_t) MATLAB_HDR.SizeX;
cristy3ed852e2009-09-05 21:47:34 +0000797 break;
798 case miINT16:
799 case miUINT16:
800 sample_size = 16;
801 image->depth = 16; /* Word type cell */
cristybb503372010-05-27 20:51:26 +0000802 ldblk = (ssize_t) (2 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000803 break;
804 case miINT32:
805 case miUINT32:
806 sample_size = 32;
807 image->depth = 32; /* Dword type cell */
cristybb503372010-05-27 20:51:26 +0000808 ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000809 break;
810 case miINT64:
811 case miUINT64:
812 sample_size = 64;
813 image->depth = 64; /* Qword type cell */
cristybb503372010-05-27 20:51:26 +0000814 ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000815 break;
816 case miSINGLE:
817 sample_size = 32;
818 image->depth = 32; /* double type cell */
819 (void) SetImageOption(clone_info,"quantum:format","floating-point");
820 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
cristyc5de6992009-10-06 19:19:48 +0000821 { /* complex float type cell */
822 }
cristybb503372010-05-27 20:51:26 +0000823 ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000824 break;
825 case miDOUBLE:
826 sample_size = 64;
827 image->depth = 64; /* double type cell */
828 (void) SetImageOption(clone_info,"quantum:format","floating-point");
829 if (sizeof(double) != 8)
830 ThrowReaderException(CoderError, "IncompatibleSizeOfDouble");
831 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
cristyc5de6992009-10-06 19:19:48 +0000832 { /* complex double type cell */
833 }
cristybb503372010-05-27 20:51:26 +0000834 ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000835 break;
836 default:
837 ThrowReaderException(CoderError, "UnsupportedCellTypeInTheMatrix");
838 }
cristyda16f162011-02-19 23:52:17 +0000839 (void) sample_size;
cristy3ed852e2009-09-05 21:47:34 +0000840 image->columns = MATLAB_HDR.SizeX;
841 image->rows = MATLAB_HDR.SizeY;
842 quantum_info=AcquireQuantumInfo(clone_info,image);
843 if (quantum_info == (QuantumInfo *) NULL)
844 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristyeaedf062010-05-29 22:36:02 +0000845 one=1;
846 image->colors = one << image->depth;
cristy3ed852e2009-09-05 21:47:34 +0000847 if (image->columns == 0 || image->rows == 0)
848 goto MATLAB_KO;
849
850 /* ----- Create gray palette ----- */
851
852 if (CellType==miUINT8 && z!=3)
853 {
854 if(image->colors>256) image->colors = 256;
855
cristy018f07f2011-09-04 21:15:19 +0000856 if (AcquireImageColormap(image, image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000857 {
858 NoMemory:ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");}
859 }
860
861 /*
862 If ping is true, then only set image size and colors without
863 reading any image data.
864 */
865 if (image_info->ping)
866 {
cristybb503372010-05-27 20:51:26 +0000867 size_t temp = image->columns;
cristy3ed852e2009-09-05 21:47:34 +0000868 image->columns = image->rows;
869 image->rows = temp;
870 goto done_reading; /* !!!!!! BAD !!!! */
871 }
872
873 /* ----- Load raster data ----- */
874 BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(unsigned char *)); /* Ldblk was set in the check phase */
875 if (BImgBuff == NULL)
876 goto NoMemory;
877
878 MinVal = 0;
879 MaxVal = 0;
880 if (CellType==miDOUBLE || CellType==miSINGLE) /* Find Min and Max Values for floats */
881 {
882 CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &quantum_info->minimum, &quantum_info->maximum);
883 }
884
885 /* Main loop for reading all scanlines */
886 if(z==1) z=0; /* read grey scanlines */
cristyc5de6992009-10-06 19:19:48 +0000887 /* else read color scanlines */
cristy3ed852e2009-09-05 21:47:34 +0000888 do
889 {
cristybb503372010-05-27 20:51:26 +0000890 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
cristy3ed852e2009-09-05 21:47:34 +0000891 {
892 q=QueueAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000893 if (q == (Quantum *)NULL)
cristyc5de6992009-10-06 19:19:48 +0000894 {
895 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000896 " MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000897 goto done_reading; /* Skip image rotation, when cannot set image pixels */
898 }
cristy3ed852e2009-09-05 21:47:34 +0000899 if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk)
cristyc5de6992009-10-06 19:19:48 +0000900 {
901 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000902 " MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000903 goto ExitLoop;
904 }
cristy3ed852e2009-09-05 21:47:34 +0000905 if((CellType==miINT8 || CellType==miUINT8) && (MATLAB_HDR.StructureFlag & FLAG_LOGICAL))
906 {
907 FixLogical((unsigned char *)BImgBuff,ldblk);
908 if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
cristyc5de6992009-10-06 19:19:48 +0000909 {
cristy3ed852e2009-09-05 21:47:34 +0000910ImportQuantumPixelsFailed:
cristyc5de6992009-10-06 19:19:48 +0000911 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000912 " MAT failed to ImportQuantumPixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000913 break;
914 }
cristy3ed852e2009-09-05 21:47:34 +0000915 }
916 else
917 {
918 if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
cristyc5de6992009-10-06 19:19:48 +0000919 goto ImportQuantumPixelsFailed;
cristy3ed852e2009-09-05 21:47:34 +0000920
921
cristyc5de6992009-10-06 19:19:48 +0000922 if (z<=1 && /* fix only during a last pass z==0 || z==1 */
923 (CellType==miINT8 || CellType==miINT16 || CellType==miINT32 || CellType==miINT64))
cristy4c08aed2011-07-01 19:47:50 +0000924 FixSignedValues(image,q,MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000925 }
926
927 if (!SyncAuthenticPixels(image,exception))
cristyc5de6992009-10-06 19:19:48 +0000928 {
929 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000930 " MAT failed to sync image pixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000931 goto ExitLoop;
932 }
cristy3ed852e2009-09-05 21:47:34 +0000933 }
934 } while(z-- >= 2);
935ExitLoop:
936
937
938 /* Read complex part of numbers here */
939 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
940 { /* Find Min and Max Values for complex parts of floats */
941 CellType = ReadBlobXXXLong(image2); /* Additional object type */
942 i = ReadBlobXXXLong(image2); /* size of a complex part - toss away*/
943
944 if (CellType==miDOUBLE || CellType==miSINGLE)
945 {
946 CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &MinVal, &MaxVal);
947 }
948
949 if (CellType==miDOUBLE)
cristybb503372010-05-27 20:51:26 +0000950 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
cristyc5de6992009-10-06 19:19:48 +0000951 {
cristy3ed852e2009-09-05 21:47:34 +0000952 ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff);
953 InsertComplexDoubleRow((double *)BImgBuff, i, image, MinVal, MaxVal);
cristyc5de6992009-10-06 19:19:48 +0000954 }
cristy3ed852e2009-09-05 21:47:34 +0000955
956 if (CellType==miSINGLE)
cristybb503372010-05-27 20:51:26 +0000957 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
cristyc5de6992009-10-06 19:19:48 +0000958 {
cristy3ed852e2009-09-05 21:47:34 +0000959 ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff);
960 InsertComplexFloatRow((float *)BImgBuff, i, image, MinVal, MaxVal);
cristyc5de6992009-10-06 19:19:48 +0000961 }
cristy3ed852e2009-09-05 21:47:34 +0000962 }
963
964 /* Image is gray when no complex flag is set and 2D Matrix AGAIN!!! */
965 if ((MATLAB_HDR.DimFlag == 8) &&
966 ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
967 image->type=GrayscaleType;
968 if (image->depth == 1)
969 image->type=BilevelType;
970
971 if(image2==image)
972 image2 = NULL; /* Remove shadow copy to an image before rotation. */
973
974 /* Rotate image. */
975 rotated_image = RotateImage(image, 90.0, exception);
976 if (rotated_image != (Image *) NULL)
977 {
978 /* Remove page offsets added by RotateImage */
979 rotated_image->page.x=0;
980 rotated_image->page.y=0;
981
982 blob = rotated_image->blob;
983 rotated_image->blob = image->blob;
984 rotated_image->colors = image->colors;
985 image->blob = blob;
986 AppendImageToList(&image,rotated_image);
987 DeleteImageFromList(&image);
988 }
989
990done_reading:
991
992 if(image2!=NULL)
993 if(image2!=image)
994 {
995 DeleteImageFromList(&image2);
cristyc5de6992009-10-06 19:19:48 +0000996 if(clone_info)
997 {
cristy3ed852e2009-09-05 21:47:34 +0000998 if(clone_info->file)
cristyc5de6992009-10-06 19:19:48 +0000999 {
cristy3ed852e2009-09-05 21:47:34 +00001000 fclose(clone_info->file);
1001 clone_info->file = NULL;
cristy18c6c272011-09-23 14:40:37 +00001002 (void) remove_utf8(clone_info->filename);
cristyc5de6992009-10-06 19:19:48 +00001003 }
cristy3ed852e2009-09-05 21:47:34 +00001004 }
1005 }
1006
1007 /* Allocate next image structure. */
cristy9950d572011-10-01 18:22:35 +00001008 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001009 if (image->next == (Image *) NULL) break;
1010 image=SyncNextImageInList(image);
1011 image->columns=image->rows=0;
1012 image->colors=0;
1013
cristya03e7992010-06-25 12:18:06 +00001014 /* row scan buffer is no longer needed */
cristy3ed852e2009-09-05 21:47:34 +00001015 RelinquishMagickMemory(BImgBuff);
1016 BImgBuff = NULL;
1017 }
1018 clone_info=DestroyImageInfo(clone_info);
1019
1020 RelinquishMagickMemory(BImgBuff);
1021 CloseBlob(image);
1022
1023
1024 {
1025 Image *p;
cristybb503372010-05-27 20:51:26 +00001026 ssize_t scene=0;
cristy3ed852e2009-09-05 21:47:34 +00001027
1028 /*
1029 Rewind list, removing any empty images while rewinding.
1030 */
1031 p=image;
1032 image=NULL;
1033 while (p != (Image *)NULL)
1034 {
1035 Image *tmp=p;
1036 if ((p->rows == 0) || (p->columns == 0)) {
1037 p=p->previous;
1038 DeleteImageFromList(&tmp);
1039 } else {
1040 image=p;
1041 p=p->previous;
1042 }
1043 }
1044
1045 /*
1046 Fix scene numbers
1047 */
1048 for (p=image; p != (Image *) NULL; p=p->next)
1049 p->scene=scene++;
1050 }
1051
cristyc5de6992009-10-06 19:19:48 +00001052 if(clone_info != NULL) /* cleanup garbage file from compression */
cristy3ed852e2009-09-05 21:47:34 +00001053 {
1054 if(clone_info->file)
1055 {
1056 fclose(clone_info->file);
1057 clone_info->file = NULL;
cristy18c6c272011-09-23 14:40:37 +00001058 (void) remove_utf8(clone_info->filename);
cristy3ed852e2009-09-05 21:47:34 +00001059 }
1060 DestroyImageInfo(clone_info);
1061 clone_info = NULL;
1062 }
1063 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),"return");
1064 if(image==NULL)
1065 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1066 return (image);
1067}
1068
1069/*
1070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071% %
1072% %
1073% %
1074% R e g i s t e r M A T I m a g e %
1075% %
1076% %
1077% %
1078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079%
1080% Method RegisterMATImage adds attributes for the MAT image format to
1081% the list of supported formats. The attributes include the image format
1082% tag, a method to read and/or write the format, whether the format
1083% supports the saving of more than one frame to the same file or blob,
1084% whether the format supports native in-memory I/O, and a brief
1085% description of the format.
1086%
1087% The format of the RegisterMATImage method is:
1088%
cristybb503372010-05-27 20:51:26 +00001089% size_t RegisterMATImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001090%
1091*/
cristybb503372010-05-27 20:51:26 +00001092ModuleExport size_t RegisterMATImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001093{
1094 MagickInfo
1095 *entry;
1096
1097 entry=SetMagickInfo("MAT");
1098 entry->decoder=(DecodeImageHandler *) ReadMATImage;
1099 entry->encoder=(EncodeImageHandler *) WriteMATImage;
1100 entry->blob_support=MagickFalse;
1101 entry->seekable_stream=MagickTrue;
cristya7cb4312010-06-26 00:47:03 +00001102 entry->description=AcquireString("MATLAB level 5 image format");
cristy3ed852e2009-09-05 21:47:34 +00001103 entry->module=AcquireString("MAT");
1104 (void) RegisterMagickInfo(entry);
1105 return(MagickImageCoderSignature);
1106}
1107
1108/*
1109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1110% %
1111% %
1112% %
1113% U n r e g i s t e r M A T I m a g e %
1114% %
1115% %
1116% %
1117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1118%
1119% Method UnregisterMATImage removes format registrations made by the
1120% MAT module from the list of supported formats.
1121%
1122% The format of the UnregisterMATImage method is:
1123%
1124% UnregisterMATImage(void)
1125%
1126*/
1127ModuleExport void UnregisterMATImage(void)
1128{
1129 (void) UnregisterMagickInfo("MAT");
1130}
1131
1132/*
1133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1134% %
1135% %
1136% %
1137% W r i t e M A T L A B I m a g e %
1138% %
1139% %
1140% %
1141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1142%
1143% Function WriteMATImage writes an Matlab matrix to a file.
1144%
1145% The format of the WriteMATImage method is:
1146%
cristy1e178e72011-08-28 19:44:34 +00001147% MagickBooleanType WriteMATImage(const ImageInfo *image_info,
1148% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001149%
1150% A description of each parameter follows.
1151%
cristy3ed852e2009-09-05 21:47:34 +00001152% o image_info: Specifies a pointer to a ImageInfo structure.
1153%
1154% o image: A pointer to an Image structure.
1155%
cristy1e178e72011-08-28 19:44:34 +00001156% o exception: return any errors or warnings in this structure.
1157%
cristy3ed852e2009-09-05 21:47:34 +00001158*/
cristy1e178e72011-08-28 19:44:34 +00001159static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image,
1160 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001161{
cristybb503372010-05-27 20:51:26 +00001162 ssize_t y;
cristy3ed852e2009-09-05 21:47:34 +00001163 unsigned z;
cristy4c08aed2011-07-01 19:47:50 +00001164 register const Quantum *p;
cristy3ed852e2009-09-05 21:47:34 +00001165
1166 unsigned int status;
1167 int logging;
cristybb503372010-05-27 20:51:26 +00001168 size_t DataSize;
cristy3ed852e2009-09-05 21:47:34 +00001169 char padding;
1170 char MATLAB_HDR[0x80];
1171 time_t current_time;
1172 struct tm local_time;
1173 unsigned char *pixels;
1174 int is_gray;
1175
1176 MagickOffsetType
1177 scene;
1178
1179 QuantumInfo
1180 *quantum_info;
1181
1182 /*
1183 Open output image file.
1184 */
1185 assert(image_info != (const ImageInfo *) NULL);
1186 assert(image_info->signature == MagickSignature);
1187 assert(image != (Image *) NULL);
1188 assert(image->signature == MagickSignature);
1189 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"enter MAT");
cristyda16f162011-02-19 23:52:17 +00001190 (void) logging;
cristy3a37efd2011-08-28 20:31:03 +00001191 assert(exception != (ExceptionInfo *) NULL);
1192 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001193 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001194 if (status == MagickFalse)
1195 return(MagickFalse);
1196 image->depth=8;
1197
1198 current_time=time((time_t *) NULL);
1199#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1200 (void) localtime_r(&current_time,&local_time);
1201#else
1202 (void) memcpy(&local_time,localtime(&current_time),sizeof(local_time));
1203#endif
1204 (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124));
cristyb51dff52011-05-19 16:55:47 +00001205 FormatLocaleString(MATLAB_HDR,MaxTextExtent,"MATLAB 5.0 MAT-file, Platform: %s, Created on: %s %s %2d %2d:%2d:%2d %d",
cristy3ed852e2009-09-05 21:47:34 +00001206 OsDesc,DayOfWTab[local_time.tm_wday],MonthsTab[local_time.tm_mon],
1207 local_time.tm_mday,local_time.tm_hour,local_time.tm_min,
1208 local_time.tm_sec,local_time.tm_year+1900);
1209 MATLAB_HDR[0x7C]=0;
1210 MATLAB_HDR[0x7D]=1;
1211 MATLAB_HDR[0x7E]='I';
1212 MATLAB_HDR[0x7F]='M';
1213 (void) WriteBlob(image,sizeof(MATLAB_HDR),(unsigned char *) MATLAB_HDR);
1214 scene=0;
1215 do
1216 {
cristy510d06a2011-07-06 23:43:54 +00001217 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001218 (void) TransformImageColorspace(image,RGBColorspace);
1219
cristy1e178e72011-08-28 19:44:34 +00001220 is_gray = IsImageGray(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001221 z = is_gray ? 0 : 3;
1222
1223 /*
1224 Store MAT header.
1225 */
1226 DataSize = image->rows /*Y*/ * image->columns /*X*/;
1227 if(!is_gray) DataSize *= 3 /*Z*/;
1228 padding=((unsigned char)(DataSize-1) & 0x7) ^ 0x7;
1229
1230 (void) WriteBlobLSBLong(image, miMATRIX);
cristyf6fe0a12010-05-30 00:44:47 +00001231 (void) WriteBlobLSBLong(image, (unsigned int) DataSize+padding+(is_gray ? 48 : 56));
cristy3ed852e2009-09-05 21:47:34 +00001232 (void) WriteBlobLSBLong(image, 0x6); /* 0x88 */
1233 (void) WriteBlobLSBLong(image, 0x8); /* 0x8C */
1234 (void) WriteBlobLSBLong(image, 0x6); /* 0x90 */
1235 (void) WriteBlobLSBLong(image, 0);
1236 (void) WriteBlobLSBLong(image, 0x5); /* 0x98 */
1237 (void) WriteBlobLSBLong(image, is_gray ? 0x8 : 0xC); /* 0x9C - DimFlag */
cristyf6fe0a12010-05-30 00:44:47 +00001238 (void) WriteBlobLSBLong(image, (unsigned int) image->rows); /* x: 0xA0 */
1239 (void) WriteBlobLSBLong(image, (unsigned int) image->columns); /* y: 0xA4 */
cristy3ed852e2009-09-05 21:47:34 +00001240 if(!is_gray)
1241 {
1242 (void) WriteBlobLSBLong(image, 3); /* z: 0xA8 */
1243 (void) WriteBlobLSBLong(image, 0);
1244 }
1245 (void) WriteBlobLSBShort(image, 1); /* 0xB0 */
1246 (void) WriteBlobLSBShort(image, 1); /* 0xB2 */
1247 (void) WriteBlobLSBLong(image, 'M'); /* 0xB4 */
1248 (void) WriteBlobLSBLong(image, 0x2); /* 0xB8 */
cristyf6fe0a12010-05-30 00:44:47 +00001249 (void) WriteBlobLSBLong(image, (unsigned int) DataSize); /* 0xBC */
cristy3ed852e2009-09-05 21:47:34 +00001250
1251 /*
1252 Store image data.
1253 */
cristy3ed852e2009-09-05 21:47:34 +00001254 quantum_info=AcquireQuantumInfo(image_info,image);
1255 if (quantum_info == (QuantumInfo *) NULL)
1256 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1257 pixels=GetQuantumPixels(quantum_info);
1258 do
1259 {
cristybb503372010-05-27 20:51:26 +00001260 for (y=0; y < (ssize_t)image->columns; y++)
cristy3ed852e2009-09-05 21:47:34 +00001261 {
cristy1e178e72011-08-28 19:44:34 +00001262 p=GetVirtualPixels(image,y,0,1,image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001263 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001264 break;
cristy4c08aed2011-07-01 19:47:50 +00001265 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +00001266 z2qtype[z],pixels,exception);
1267 (void) WriteBlob(image,image->rows,pixels);
1268 }
cristy1e178e72011-08-28 19:44:34 +00001269 if (SyncAuthenticPixels(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001270 break;
1271 } while(z-- >= 2);
1272 while(padding-->0) (void) WriteBlobByte(image,0);
1273 quantum_info=DestroyQuantumInfo(quantum_info);
1274 if (GetNextImageInList(image) == (Image *) NULL)
1275 break;
1276 image=SyncNextImageInList(image);
1277 status=SetImageProgress(image,SaveImagesTag,scene++,
1278 GetImageListLength(image));
1279 if (status == MagickFalse)
1280 break;
1281 } while (image_info->adjoin != MagickFalse);
1282 (void) CloseBlob(image);
1283 return(MagickTrue);
1284}