blob: 5e94865470fb4529a5a093c03b1dad64e6f7fd9d [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"
cristyc53413d2011-11-17 13:04:26 +000058#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000059#include "MagickCore/exception.h"
60#include "MagickCore/exception-private.h"
61#include "MagickCore/image.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/magick.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/quantum-private.h"
70#include "MagickCore/option.h"
71#include "MagickCore/pixel.h"
72#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +000073#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
cristyc82a27b2011-10-21 01:07:16 +0000181static void InsertComplexDoubleRow(Image *image,double *p,int y,double MinVal,
182 double MaxVal,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000183{
cristy3ed852e2009-09-05 21:47:34 +0000184
185 double f;
186 int x;
cristy4c08aed2011-07-01 19:47:50 +0000187 register Quantum *q;
cristy3ed852e2009-09-05 21:47:34 +0000188
189 if (MinVal == 0)
190 MinVal = -1;
191 if (MaxVal == 0)
192 MaxVal = 1;
193
cristy524222d2011-04-25 00:37:06 +0000194 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000195 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000196 return;
cristybb503372010-05-27 20:51:26 +0000197 for (x = 0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000198 {
199 if (*p > 0)
200 {
cristy4c08aed2011-07-01 19:47:50 +0000201 f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q));
202 if (f + GetPixelRed(image,q) > QuantumRange)
203 SetPixelRed(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000204 else
cristy4c08aed2011-07-01 19:47:50 +0000205 SetPixelRed(image,GetPixelRed(image,q)+(int) f,q);
206 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000207 {
cristy4c08aed2011-07-01 19:47:50 +0000208 SetPixelGreen(image,0,q);
209 SetPixelBlue(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000210 }
cristy3ed852e2009-09-05 21:47:34 +0000211 else
cristy524222d2011-04-25 00:37:06 +0000212 {
cristy4c08aed2011-07-01 19:47:50 +0000213 SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q);
214 SetPixelGreen(image,GetPixelBlue(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000215 }
cristy3ed852e2009-09-05 21:47:34 +0000216 }
217 if (*p < 0)
218 {
cristy4c08aed2011-07-01 19:47:50 +0000219 f = (*p / MaxVal) * (QuantumRange-GetPixelBlue(image,q));
220 if (f+GetPixelBlue(image,q) > QuantumRange)
221 SetPixelBlue(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000222 else
cristy4c08aed2011-07-01 19:47:50 +0000223 SetPixelBlue(image,GetPixelBlue(image,q)+(int) f,q);
224 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000225 {
cristy4c08aed2011-07-01 19:47:50 +0000226 SetPixelRed(image,0,q);
227 SetPixelGreen(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000228 }
cristy3ed852e2009-09-05 21:47:34 +0000229 else
cristy524222d2011-04-25 00:37:06 +0000230 {
cristy4c08aed2011-07-01 19:47:50 +0000231 SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q);
232 SetPixelGreen(image,GetPixelRed(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000233 }
cristy3ed852e2009-09-05 21:47:34 +0000234 }
235 p++;
cristyed231572011-07-14 02:18:59 +0000236 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000237 }
238 if (!SyncAuthenticPixels(image,exception))
239 return;
cristy3ed852e2009-09-05 21:47:34 +0000240 return;
241}
242
243
cristyc82a27b2011-10-21 01:07:16 +0000244static void InsertComplexFloatRow(Image *image,float *p,int y,double MinVal,
245 double MaxVal,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000246{
cristy3ed852e2009-09-05 21:47:34 +0000247 double f;
248 int x;
cristy4c08aed2011-07-01 19:47:50 +0000249 register Quantum *q;
cristy3ed852e2009-09-05 21:47:34 +0000250
251 if (MinVal == 0)
252 MinVal = -1;
253 if (MaxVal == 0)
254 MaxVal = 1;
255
cristy3ed852e2009-09-05 21:47:34 +0000256 q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000257 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000258 return;
cristybb503372010-05-27 20:51:26 +0000259 for (x = 0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000260 {
261 if (*p > 0)
262 {
cristy4c08aed2011-07-01 19:47:50 +0000263 f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q));
264 if (f+GetPixelRed(image,q) > QuantumRange)
265 SetPixelRed(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000266 else
cristy4c08aed2011-07-01 19:47:50 +0000267 SetPixelRed(image,GetPixelRed(image,q)+(int) f,q);
268 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000269 {
cristy4c08aed2011-07-01 19:47:50 +0000270 SetPixelGreen(image,0,q);
271 SetPixelBlue(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000272 }
cristy3ed852e2009-09-05 21:47:34 +0000273 else
cristy524222d2011-04-25 00:37:06 +0000274 {
cristy4c08aed2011-07-01 19:47:50 +0000275 SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q);
276 SetPixelGreen(image,GetPixelBlue(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000277 }
cristy3ed852e2009-09-05 21:47:34 +0000278 }
279 if (*p < 0)
280 {
cristy4c08aed2011-07-01 19:47:50 +0000281 f = (*p / MaxVal) * (QuantumRange - GetPixelBlue(image,q));
282 if (f + GetPixelBlue(image,q) > QuantumRange)
283 SetPixelBlue(image,QuantumRange,q);
cristy3ed852e2009-09-05 21:47:34 +0000284 else
cristy4c08aed2011-07-01 19:47:50 +0000285 SetPixelBlue(image,GetPixelBlue(image,q)+
286 (int) f,q);
287 if ((int) f / 2.0 > GetPixelGreen(image,q))
cristy524222d2011-04-25 00:37:06 +0000288 {
cristy4c08aed2011-07-01 19:47:50 +0000289 SetPixelGreen(image,0,q);
290 SetPixelRed(image,0,q);
cristy524222d2011-04-25 00:37:06 +0000291 }
cristy3ed852e2009-09-05 21:47:34 +0000292 else
cristy524222d2011-04-25 00:37:06 +0000293 {
cristy4c08aed2011-07-01 19:47:50 +0000294 SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q);
295 SetPixelGreen(image,GetPixelRed(image,q),q);
cristy524222d2011-04-25 00:37:06 +0000296 }
cristy3ed852e2009-09-05 21:47:34 +0000297 }
298 p++;
299 q++;
300 }
301 if (!SyncAuthenticPixels(image,exception))
302 return;
cristy3ed852e2009-09-05 21:47:34 +0000303 return;
304}
305
306
307/************** READERS ******************/
308
309/* This function reads one block of floats*/
310static void ReadBlobFloatsLSB(Image * image, size_t len, float *data)
311{
312 while (len >= 4)
313 {
314 *data++ = ReadBlobFloat(image);
315 len -= sizeof(float);
316 }
317 if (len > 0)
318 (void) SeekBlob(image, len, SEEK_CUR);
319}
320
321static void ReadBlobFloatsMSB(Image * image, size_t len, float *data)
322{
323 while (len >= 4)
324 {
325 *data++ = ReadBlobFloat(image);
326 len -= sizeof(float);
327 }
328 if (len > 0)
329 (void) SeekBlob(image, len, SEEK_CUR);
330}
331
332/* This function reads one block of doubles*/
333static void ReadBlobDoublesLSB(Image * image, size_t len, double *data)
334{
335 while (len >= 8)
336 {
337 *data++ = ReadBlobDouble(image);
338 len -= sizeof(double);
339 }
340 if (len > 0)
341 (void) SeekBlob(image, len, SEEK_CUR);
342}
343
344static void ReadBlobDoublesMSB(Image * image, size_t len, double *data)
345{
346 while (len >= 8)
347 {
348 *data++ = ReadBlobDouble(image);
349 len -= sizeof(double);
350 }
351 if (len > 0)
352 (void) SeekBlob(image, len, SEEK_CUR);
353}
354
355/* Calculate minimum and maximum from a given block of data */
cristybb503372010-05-27 20:51:26 +0000356static 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 +0000357{
358MagickOffsetType filepos;
359int i, x;
360void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
361void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
362double *dblrow;
363float *fltrow;
364
365 if (endian_indicator == LSBEndian)
366 {
367 ReadBlobDoublesXXX = ReadBlobDoublesLSB;
368 ReadBlobFloatsXXX = ReadBlobFloatsLSB;
369 }
cristyc5de6992009-10-06 19:19:48 +0000370 else /* MI */
cristy3ed852e2009-09-05 21:47:34 +0000371 {
372 ReadBlobDoublesXXX = ReadBlobDoublesMSB;
373 ReadBlobFloatsXXX = ReadBlobFloatsMSB;
374 }
375
cristyc5de6992009-10-06 19:19:48 +0000376 filepos = TellBlob(image); /* Please note that file seeking occurs only in the case of doubles */
cristy3ed852e2009-09-05 21:47:34 +0000377 for (i = 0; i < SizeY; i++)
378 {
379 if (CellType==miDOUBLE)
380 {
381 ReadBlobDoublesXXX(image, ldblk, (double *)BImgBuff);
382 dblrow = (double *)BImgBuff;
383 if (i == 0)
384 {
385 *Min = *Max = *dblrow;
386 }
387 for (x = 0; x < SizeX; x++)
388 {
389 if (*Min > *dblrow)
390 *Min = *dblrow;
391 if (*Max < *dblrow)
392 *Max = *dblrow;
393 dblrow++;
394 }
395 }
396 if (CellType==miSINGLE)
397 {
398 ReadBlobFloatsXXX(image, ldblk, (float *)BImgBuff);
399 fltrow = (float *)BImgBuff;
400 if (i == 0)
401 {
402 *Min = *Max = *fltrow;
403 }
cristybb503372010-05-27 20:51:26 +0000404 for (x = 0; x < (ssize_t) SizeX; x++)
cristy3ed852e2009-09-05 21:47:34 +0000405 {
406 if (*Min > *fltrow)
407 *Min = *fltrow;
408 if (*Max < *fltrow)
409 *Max = *fltrow;
410 fltrow++;
411 }
412 }
413 }
414 (void) SeekBlob(image, filepos, SEEK_SET);
415}
416
417
cristy4c08aed2011-07-01 19:47:50 +0000418static void FixSignedValues(const Image *image,Quantum *q, int y)
cristy3ed852e2009-09-05 21:47:34 +0000419{
420 while(y-->0)
421 {
422 /* Please note that negative values will overflow
423 Q=8; QuantumRange=255: <0;127> + 127+1 = <128; 255>
cristyc5de6992009-10-06 19:19:48 +0000424 <-1;-128> + 127+1 = <0; 127> */
cristy4c08aed2011-07-01 19:47:50 +0000425 SetPixelRed(image,GetPixelRed(image,q)+QuantumRange/2+1,q);
426 SetPixelGreen(image,GetPixelGreen(image,q)+QuantumRange/2+1,q);
427 SetPixelBlue(image,GetPixelBlue(image,q)+QuantumRange/2+1,q);
cristy3ed852e2009-09-05 21:47:34 +0000428 q++;
429 }
430}
431
432
433/** Fix whole row of logical/binary data. It means pack it. */
434static void FixLogical(unsigned char *Buff,int ldblk)
435{
436unsigned char mask=128;
437unsigned char *BuffL = Buff;
438unsigned char val = 0;
439
440 while(ldblk-->0)
441 {
442 if(*Buff++ != 0)
443 val |= mask;
444
445 mask >>= 1;
446 if(mask==0)
447 {
448 *BuffL++ = val;
449 val = 0;
450 mask = 128;
451 }
452
453 }
454 *BuffL = val;
455}
456
457#if defined(MAGICKCORE_ZLIB_DELEGATE)
458static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
459 unsigned int size)
460{
461 (void) context;
462 return((voidpf) AcquireQuantumMemory(items,size));
463}
464
465static void RelinquishZIPMemory(voidpf context,voidpf memory)
466{
467 (void) context;
468 memory=RelinquishMagickMemory(memory);
469}
470#endif
471
cristy0906b142011-02-21 01:10:00 +0000472#if defined(MAGICKCORE_ZLIB_DELEGATE)
cristy3ed852e2009-09-05 21:47:34 +0000473/** This procedure decompreses an image block for a new MATLAB format. */
474static Image *DecompressBlock(Image *orig, MagickOffsetType Size, ImageInfo *clone_info, ExceptionInfo *exception)
475{
cristy3ed852e2009-09-05 21:47:34 +0000476
477Image *image2;
478void *CacheBlock, *DecompressBlock;
479z_stream zip_info;
480FILE *mat_file;
481size_t magick_size;
cristy95236b52009-12-30 21:56:45 +0000482size_t extent;
cristy3ed852e2009-09-05 21:47:34 +0000483
484int status;
485
486 if(clone_info==NULL) return NULL;
cristyc5de6992009-10-06 19:19:48 +0000487 if(clone_info->file) /* Close file opened from previous transaction. */
cristy3ed852e2009-09-05 21:47:34 +0000488 {
489 fclose(clone_info->file);
490 clone_info->file = NULL;
cristy18c6c272011-09-23 14:40:37 +0000491 (void) remove_utf8(clone_info->filename);
cristy3ed852e2009-09-05 21:47:34 +0000492 }
493
494 CacheBlock = AcquireQuantumMemory((size_t)((Size<16384)?Size:16384),sizeof(unsigned char *));
495 if(CacheBlock==NULL) return NULL;
496 DecompressBlock = AcquireQuantumMemory((size_t)(4096),sizeof(unsigned char *));
497 if(DecompressBlock==NULL)
498 {
499 RelinquishMagickMemory(CacheBlock);
500 return NULL;
501 }
502
503 mat_file = fdopen(AcquireUniqueFileResource(clone_info->filename),"w");
504 if(!mat_file)
505 {
506 RelinquishMagickMemory(CacheBlock);
507 RelinquishMagickMemory(DecompressBlock);
508 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Gannot create file stream for PS image");
509 return NULL;
510 }
511
512 zip_info.zalloc=AcquireZIPMemory;
513 zip_info.zfree=RelinquishZIPMemory;
514 zip_info.opaque = (voidpf) NULL;
515 inflateInit(&zip_info);
516 /* zip_info.next_out = 8*4;*/
517
518 zip_info.avail_in = 0;
519 zip_info.total_out = 0;
520 while(Size>0 && !EOFBlob(orig))
521 {
522 magick_size = ReadBlob(orig, (Size<16384)?Size:16384, (unsigned char *) CacheBlock);
523 zip_info.next_in = (Bytef *) CacheBlock;
524 zip_info.avail_in = (uInt) magick_size;
525
526 while(zip_info.avail_in>0)
527 {
528 zip_info.avail_out = 4096;
529 zip_info.next_out = (Bytef *) DecompressBlock;
530 status = inflate(&zip_info,Z_NO_FLUSH);
cristy95236b52009-12-30 21:56:45 +0000531 extent=fwrite(DecompressBlock, 4096-zip_info.avail_out, 1, mat_file);
cristyda16f162011-02-19 23:52:17 +0000532 (void) extent;
cristy3ed852e2009-09-05 21:47:34 +0000533
534 if(status == Z_STREAM_END) goto DblBreak;
535 }
536
537 Size -= magick_size;
538 }
539DblBreak:
540
541 (void)fclose(mat_file);
542 RelinquishMagickMemory(CacheBlock);
543 RelinquishMagickMemory(DecompressBlock);
544
545 if((clone_info->file=fopen(clone_info->filename,"rb"))==NULL) goto UnlinkFile;
cristy9950d572011-10-01 18:22:35 +0000546 if( (image2 = AcquireImage(clone_info,exception))==NULL ) goto EraseFile;
cristy3ed852e2009-09-05 21:47:34 +0000547 status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception);
548 if (status == MagickFalse)
549 {
550 DeleteImageFromList(&image2);
551EraseFile:
552 fclose(clone_info->file);
553 clone_info->file = NULL;
554UnlinkFile:
cristy18c6c272011-09-23 14:40:37 +0000555 (void) remove_utf8(clone_info->filename);
cristy3ed852e2009-09-05 21:47:34 +0000556 return NULL;
557 }
558
559 return image2;
cristy3ed852e2009-09-05 21:47:34 +0000560}
cristy0906b142011-02-21 01:10:00 +0000561#endif
cristy3ed852e2009-09-05 21:47:34 +0000562
563/*
564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565% %
566% %
567% %
568% R e a d M A T L A B i m a g e %
569% %
570% %
571% %
572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573%
574% ReadMATImage() reads an MAT X image file and returns it. It
575% allocates the memory necessary for the new Image structure and returns a
576% pointer to the new image.
577%
578% The format of the ReadMATImage method is:
579%
580% Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
581%
582% A description of each parameter follows:
583%
584% o image: Method ReadMATImage returns a pointer to the image after
585% reading. A null image is returned if there is a memory shortage or if
586% the image cannot be read.
587%
588% o image_info: Specifies a pointer to a ImageInfo structure.
589%
590% o exception: return any errors or warnings in this structure.
591%
592*/
593
594static inline size_t MagickMin(const size_t x,const size_t y)
595{
596 if (x < y)
597 return(x);
598 return(y);
599}
600
601static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception)
602{
603 Image *image, *image2=NULL,
604 *rotated_image;
cristy4c08aed2011-07-01 19:47:50 +0000605 register Quantum *q;
cristy3ed852e2009-09-05 21:47:34 +0000606
607 unsigned int status;
608 MATHeader MATLAB_HDR;
cristybb503372010-05-27 20:51:26 +0000609 size_t size;
610 size_t CellType;
cristy3ed852e2009-09-05 21:47:34 +0000611 QuantumInfo *quantum_info;
612 ImageInfo *clone_info;
613 int i;
cristybb503372010-05-27 20:51:26 +0000614 ssize_t ldblk;
cristy3ed852e2009-09-05 21:47:34 +0000615 unsigned char *BImgBuff = NULL;
616 double MinVal, MaxVal;
cristyf5e4a812012-05-01 16:40:19 +0000617 unsigned z, z2;
618 unsigned Frames;
cristy3ed852e2009-09-05 21:47:34 +0000619 int logging;
620 int sample_size;
621 MagickOffsetType filepos=0x80;
622 BlobInfo *blob;
cristyeaedf062010-05-29 22:36:02 +0000623 size_t one;
cristy3ed852e2009-09-05 21:47:34 +0000624
625 unsigned int (*ReadBlobXXXLong)(Image *image);
626 unsigned short (*ReadBlobXXXShort)(Image *image);
627 void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data);
628 void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data);
629
630
631 assert(image_info != (const ImageInfo *) NULL);
632 assert(image_info->signature == MagickSignature);
633 assert(exception != (ExceptionInfo *) NULL);
634 assert(exception->signature == MagickSignature);
635 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter");
636
637 /*
638 Open image file.
639 */
cristy9950d572011-10-01 18:22:35 +0000640 image = AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000641
642 status = OpenBlob(image_info, image, ReadBinaryBlobMode, exception);
643 if (status == MagickFalse)
644 {
645 image=DestroyImageList(image);
646 return((Image *) NULL);
647 }
648 /*
649 Read MATLAB image.
650 */
651 clone_info=CloneImageInfo(image_info);
652 if(ReadBlob(image,124,(unsigned char *) &MATLAB_HDR.identific) != 124)
653 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
654 MATLAB_HDR.Version = ReadBlobLSBShort(image);
655 if(ReadBlob(image,2,(unsigned char *) &MATLAB_HDR.EndianIndicator) != 2)
656 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
657
658 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule()," Endian %c%c",
659 MATLAB_HDR.EndianIndicator[0],MATLAB_HDR.EndianIndicator[1]);
660 if (!strncmp(MATLAB_HDR.EndianIndicator, "IM", 2))
661 {
662 ReadBlobXXXLong = ReadBlobLSBLong;
663 ReadBlobXXXShort = ReadBlobLSBShort;
664 ReadBlobDoublesXXX = ReadBlobDoublesLSB;
665 ReadBlobFloatsXXX = ReadBlobFloatsLSB;
666 image->endian = LSBEndian;
667 }
668 else if (!strncmp(MATLAB_HDR.EndianIndicator, "MI", 2))
669 {
670 ReadBlobXXXLong = ReadBlobMSBLong;
671 ReadBlobXXXShort = ReadBlobMSBShort;
672 ReadBlobDoublesXXX = ReadBlobDoublesMSB;
673 ReadBlobFloatsXXX = ReadBlobFloatsMSB;
674 image->endian = MSBEndian;
675 }
676 else
677 goto MATLAB_KO; /* unsupported endian */
678
679 if (strncmp(MATLAB_HDR.identific, "MATLAB", 6))
680MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
681
682 filepos = TellBlob(image);
683 while(!EOFBlob(image)) /* object parser loop */
684 {
cristyf5e4a812012-05-01 16:40:19 +0000685 Frames = 1;
cristy3ed852e2009-09-05 21:47:34 +0000686 (void) SeekBlob(image,filepos,SEEK_SET);
687 /* printf("pos=%X\n",TellBlob(image)); */
688
689 MATLAB_HDR.DataType = ReadBlobXXXLong(image);
690 if(EOFBlob(image)) break;
691 MATLAB_HDR.ObjectSize = ReadBlobXXXLong(image);
692 if(EOFBlob(image)) break;
693 filepos += MATLAB_HDR.ObjectSize + 4 + 4;
694
695 image2 = image;
696#if defined(MAGICKCORE_ZLIB_DELEGATE)
697 if(MATLAB_HDR.DataType == miCOMPRESSED)
698 {
699 image2 = DecompressBlock(image,MATLAB_HDR.ObjectSize,clone_info,exception);
700 if(image2==NULL) continue;
701 MATLAB_HDR.DataType = ReadBlobXXXLong(image2); /* replace compressed object type. */
702 }
703#endif
704
705 if(MATLAB_HDR.DataType!=miMATRIX) continue; /* skip another objects. */
706
707 MATLAB_HDR.unknown1 = ReadBlobXXXLong(image2);
708 MATLAB_HDR.unknown2 = ReadBlobXXXLong(image2);
709
710 MATLAB_HDR.unknown5 = ReadBlobXXXLong(image2);
711 MATLAB_HDR.StructureClass = MATLAB_HDR.unknown5 & 0xFF;
712 MATLAB_HDR.StructureFlag = (MATLAB_HDR.unknown5>>8) & 0xFF;
713
714 MATLAB_HDR.unknown3 = ReadBlobXXXLong(image2);
715 if(image!=image2)
cristyc5de6992009-10-06 19:19:48 +0000716 MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2); /* ??? don't understand why ?? */
cristy3ed852e2009-09-05 21:47:34 +0000717 MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2);
718 MATLAB_HDR.DimFlag = ReadBlobXXXLong(image2);
719 MATLAB_HDR.SizeX = ReadBlobXXXLong(image2);
720 MATLAB_HDR.SizeY = ReadBlobXXXLong(image2);
721
722
723 switch(MATLAB_HDR.DimFlag)
724 {
cristyf5e4a812012-05-01 16:40:19 +0000725 case 8: z2=z=1; break; /* 2D matrix*/
726 case 12: z2=z = ReadBlobXXXLong(image2); /* 3D matrix RGB*/
727 (void) ReadBlobXXXLong(image2);
cristyc5de6992009-10-06 19:19:48 +0000728 if(z!=3) ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
729 break;
cristyf5e4a812012-05-01 16:40:19 +0000730 case 16: z2=z = ReadBlobXXXLong(image2); /* 4D matrix animation */
731 if(z!=3 && z!=1)
732 ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
733 Frames = ReadBlobXXXLong(image2);
734 break;
cristy3ed852e2009-09-05 21:47:34 +0000735 default: ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
736 }
737
738 MATLAB_HDR.Flag1 = ReadBlobXXXShort(image2);
739 MATLAB_HDR.NameFlag = ReadBlobXXXShort(image2);
740
741 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
742 "MATLAB_HDR.StructureClass %d",MATLAB_HDR.StructureClass);
743 if (MATLAB_HDR.StructureClass != mxCHAR_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000744 MATLAB_HDR.StructureClass != mxSINGLE_CLASS && /* float + complex float */
745 MATLAB_HDR.StructureClass != mxDOUBLE_CLASS && /* double + complex double */
cristy3ed852e2009-09-05 21:47:34 +0000746 MATLAB_HDR.StructureClass != mxINT8_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000747 MATLAB_HDR.StructureClass != mxUINT8_CLASS && /* uint8 + uint8 3D */
cristy3ed852e2009-09-05 21:47:34 +0000748 MATLAB_HDR.StructureClass != mxINT16_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000749 MATLAB_HDR.StructureClass != mxUINT16_CLASS && /* uint16 + uint16 3D */
cristy3ed852e2009-09-05 21:47:34 +0000750 MATLAB_HDR.StructureClass != mxINT32_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000751 MATLAB_HDR.StructureClass != mxUINT32_CLASS && /* uint32 + uint32 3D */
cristy3ed852e2009-09-05 21:47:34 +0000752 MATLAB_HDR.StructureClass != mxINT64_CLASS &&
cristyc5de6992009-10-06 19:19:48 +0000753 MATLAB_HDR.StructureClass != mxUINT64_CLASS) /* uint64 + uint64 3D */
cristy3ed852e2009-09-05 21:47:34 +0000754 ThrowReaderException(CoderError,"UnsupportedCellTypeInTheMatrix");
755
756 switch (MATLAB_HDR.NameFlag)
757 {
758 case 0:
cristyc5de6992009-10-06 19:19:48 +0000759 size = ReadBlobXXXLong(image2); /* Object name string size */
cristybb503372010-05-27 20:51:26 +0000760 size = 4 * (ssize_t) ((size + 3 + 1) / 4);
cristy3ed852e2009-09-05 21:47:34 +0000761 (void) SeekBlob(image2, size, SEEK_CUR);
762 break;
763 case 1:
764 case 2:
765 case 3:
766 case 4:
767 (void) ReadBlob(image2, 4, (unsigned char *) &size); /* Object name string */
768 break;
769 default:
770 goto MATLAB_KO;
771 }
772
773 CellType = ReadBlobXXXLong(image2); /* Additional object type */
cristyf2faecf2010-05-28 19:19:36 +0000774 if (logging)
775 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000776 "MATLAB_HDR.CellType: %.20g",(double) CellType);
cristy3ed852e2009-09-05 21:47:34 +0000777
778 (void) ReadBlob(image2, 4, (unsigned char *) &size); /* data size */
779
cristyf5e4a812012-05-01 16:40:19 +0000780 NEXT_FRAME:
cristy3ed852e2009-09-05 21:47:34 +0000781 switch (CellType)
782 {
783 case miINT8:
784 case miUINT8:
785 sample_size = 8;
786 if(MATLAB_HDR.StructureFlag & FLAG_LOGICAL)
787 image->depth = 1;
788 else
789 image->depth = 8; /* Byte type cell */
cristybb503372010-05-27 20:51:26 +0000790 ldblk = (ssize_t) MATLAB_HDR.SizeX;
cristy3ed852e2009-09-05 21:47:34 +0000791 break;
792 case miINT16:
793 case miUINT16:
794 sample_size = 16;
795 image->depth = 16; /* Word type cell */
cristybb503372010-05-27 20:51:26 +0000796 ldblk = (ssize_t) (2 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000797 break;
798 case miINT32:
799 case miUINT32:
800 sample_size = 32;
801 image->depth = 32; /* Dword type cell */
cristybb503372010-05-27 20:51:26 +0000802 ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000803 break;
804 case miINT64:
805 case miUINT64:
806 sample_size = 64;
807 image->depth = 64; /* Qword type cell */
cristybb503372010-05-27 20:51:26 +0000808 ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000809 break;
810 case miSINGLE:
811 sample_size = 32;
812 image->depth = 32; /* double type cell */
813 (void) SetImageOption(clone_info,"quantum:format","floating-point");
814 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
cristyc5de6992009-10-06 19:19:48 +0000815 { /* complex float type cell */
816 }
cristybb503372010-05-27 20:51:26 +0000817 ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000818 break;
819 case miDOUBLE:
820 sample_size = 64;
821 image->depth = 64; /* double type cell */
822 (void) SetImageOption(clone_info,"quantum:format","floating-point");
823 if (sizeof(double) != 8)
824 ThrowReaderException(CoderError, "IncompatibleSizeOfDouble");
825 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
cristyc5de6992009-10-06 19:19:48 +0000826 { /* complex double type cell */
827 }
cristybb503372010-05-27 20:51:26 +0000828 ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000829 break;
830 default:
831 ThrowReaderException(CoderError, "UnsupportedCellTypeInTheMatrix");
832 }
cristyda16f162011-02-19 23:52:17 +0000833 (void) sample_size;
cristy3ed852e2009-09-05 21:47:34 +0000834 image->columns = MATLAB_HDR.SizeX;
835 image->rows = MATLAB_HDR.SizeY;
836 quantum_info=AcquireQuantumInfo(clone_info,image);
837 if (quantum_info == (QuantumInfo *) NULL)
838 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristyeaedf062010-05-29 22:36:02 +0000839 one=1;
840 image->colors = one << image->depth;
cristy3ed852e2009-09-05 21:47:34 +0000841 if (image->columns == 0 || image->rows == 0)
842 goto MATLAB_KO;
cristydfc9c532012-05-12 23:45:48 +0000843 /* Image is gray when no complex flag is set and 2D Matrix */
844 if ((MATLAB_HDR.DimFlag == 8) &&
845 ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
846 {
847 image->type=GrayscaleType;
848 SetImageColorspace(image,GRAYColorspace,exception);
849 }
850
cristy3ed852e2009-09-05 21:47:34 +0000851
852 /* ----- Create gray palette ----- */
853
854 if (CellType==miUINT8 && z!=3)
855 {
cristy9caad062012-05-07 18:58:56 +0000856 if(image->colors > 256) image->colors = 256;
cristy3ed852e2009-09-05 21:47:34 +0000857
cristy018f07f2011-09-04 21:15:19 +0000858 if (AcquireImageColormap(image, image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000859 {
860 NoMemory:ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");}
861 }
862
863 /*
864 If ping is true, then only set image size and colors without
865 reading any image data.
866 */
867 if (image_info->ping)
868 {
cristybb503372010-05-27 20:51:26 +0000869 size_t temp = image->columns;
cristy3ed852e2009-09-05 21:47:34 +0000870 image->columns = image->rows;
871 image->rows = temp;
872 goto done_reading; /* !!!!!! BAD !!!! */
873 }
874
875 /* ----- Load raster data ----- */
876 BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(unsigned char *)); /* Ldblk was set in the check phase */
877 if (BImgBuff == NULL)
878 goto NoMemory;
879
880 MinVal = 0;
881 MaxVal = 0;
882 if (CellType==miDOUBLE || CellType==miSINGLE) /* Find Min and Max Values for floats */
883 {
884 CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &quantum_info->minimum, &quantum_info->maximum);
885 }
886
887 /* Main loop for reading all scanlines */
888 if(z==1) z=0; /* read grey scanlines */
cristyc5de6992009-10-06 19:19:48 +0000889 /* else read color scanlines */
cristy3ed852e2009-09-05 21:47:34 +0000890 do
891 {
cristybb503372010-05-27 20:51:26 +0000892 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
cristy3ed852e2009-09-05 21:47:34 +0000893 {
894 q=QueueAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000895 if (q == (Quantum *)NULL)
cristyc5de6992009-10-06 19:19:48 +0000896 {
897 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000898 " MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000899 goto done_reading; /* Skip image rotation, when cannot set image pixels */
900 }
cristy3ed852e2009-09-05 21:47:34 +0000901 if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk)
cristyc5de6992009-10-06 19:19:48 +0000902 {
903 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000904 " MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000905 goto ExitLoop;
906 }
cristy3ed852e2009-09-05 21:47:34 +0000907 if((CellType==miINT8 || CellType==miUINT8) && (MATLAB_HDR.StructureFlag & FLAG_LOGICAL))
908 {
909 FixLogical((unsigned char *)BImgBuff,ldblk);
910 if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
cristyc5de6992009-10-06 19:19:48 +0000911 {
cristy3ed852e2009-09-05 21:47:34 +0000912ImportQuantumPixelsFailed:
cristyc5de6992009-10-06 19:19:48 +0000913 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000914 " MAT failed to ImportQuantumPixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000915 break;
916 }
cristy3ed852e2009-09-05 21:47:34 +0000917 }
918 else
919 {
920 if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0)
cristyc5de6992009-10-06 19:19:48 +0000921 goto ImportQuantumPixelsFailed;
cristy3ed852e2009-09-05 21:47:34 +0000922
923
cristyc5de6992009-10-06 19:19:48 +0000924 if (z<=1 && /* fix only during a last pass z==0 || z==1 */
925 (CellType==miINT8 || CellType==miINT16 || CellType==miINT32 || CellType==miINT64))
cristy4c08aed2011-07-01 19:47:50 +0000926 FixSignedValues(image,q,MATLAB_HDR.SizeX);
cristy3ed852e2009-09-05 21:47:34 +0000927 }
928
929 if (!SyncAuthenticPixels(image,exception))
cristyc5de6992009-10-06 19:19:48 +0000930 {
931 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000932 " MAT failed to sync image pixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1));
cristyc5de6992009-10-06 19:19:48 +0000933 goto ExitLoop;
934 }
cristy3ed852e2009-09-05 21:47:34 +0000935 }
936 } while(z-- >= 2);
937ExitLoop:
938
939
940 /* Read complex part of numbers here */
941 if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX)
942 { /* Find Min and Max Values for complex parts of floats */
943 CellType = ReadBlobXXXLong(image2); /* Additional object type */
944 i = ReadBlobXXXLong(image2); /* size of a complex part - toss away*/
945
946 if (CellType==miDOUBLE || CellType==miSINGLE)
947 {
948 CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &MinVal, &MaxVal);
949 }
950
951 if (CellType==miDOUBLE)
cristybb503372010-05-27 20:51:26 +0000952 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
cristyc5de6992009-10-06 19:19:48 +0000953 {
cristy3ed852e2009-09-05 21:47:34 +0000954 ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff);
cristyc82a27b2011-10-21 01:07:16 +0000955 InsertComplexDoubleRow(image, (double *)BImgBuff, i, MinVal, MaxVal,
956 exception);
cristyc5de6992009-10-06 19:19:48 +0000957 }
cristy3ed852e2009-09-05 21:47:34 +0000958
959 if (CellType==miSINGLE)
cristybb503372010-05-27 20:51:26 +0000960 for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
cristyc5de6992009-10-06 19:19:48 +0000961 {
cristy3ed852e2009-09-05 21:47:34 +0000962 ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff);
cristyc82a27b2011-10-21 01:07:16 +0000963 InsertComplexFloatRow(image,(float *)BImgBuff,i,MinVal,MaxVal,
964 exception);
cristyc5de6992009-10-06 19:19:48 +0000965 }
cristy3ed852e2009-09-05 21:47:34 +0000966 }
967
968 /* Image is gray when no complex flag is set and 2D Matrix AGAIN!!! */
969 if ((MATLAB_HDR.DimFlag == 8) &&
970 ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0))
971 image->type=GrayscaleType;
972 if (image->depth == 1)
973 image->type=BilevelType;
974
975 if(image2==image)
976 image2 = NULL; /* Remove shadow copy to an image before rotation. */
977
978 /* Rotate image. */
979 rotated_image = RotateImage(image, 90.0, exception);
980 if (rotated_image != (Image *) NULL)
981 {
982 /* Remove page offsets added by RotateImage */
983 rotated_image->page.x=0;
984 rotated_image->page.y=0;
985
986 blob = rotated_image->blob;
987 rotated_image->blob = image->blob;
988 rotated_image->colors = image->colors;
989 image->blob = blob;
990 AppendImageToList(&image,rotated_image);
991 DeleteImageFromList(&image);
992 }
993
994done_reading:
995
996 if(image2!=NULL)
997 if(image2!=image)
998 {
999 DeleteImageFromList(&image2);
cristyc5de6992009-10-06 19:19:48 +00001000 if(clone_info)
1001 {
cristy3ed852e2009-09-05 21:47:34 +00001002 if(clone_info->file)
cristyc5de6992009-10-06 19:19:48 +00001003 {
cristy3ed852e2009-09-05 21:47:34 +00001004 fclose(clone_info->file);
1005 clone_info->file = NULL;
cristy18c6c272011-09-23 14:40:37 +00001006 (void) remove_utf8(clone_info->filename);
cristyc5de6992009-10-06 19:19:48 +00001007 }
cristy3ed852e2009-09-05 21:47:34 +00001008 }
1009 }
1010
1011 /* Allocate next image structure. */
cristy9950d572011-10-01 18:22:35 +00001012 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001013 if (image->next == (Image *) NULL) break;
1014 image=SyncNextImageInList(image);
1015 image->columns=image->rows=0;
1016 image->colors=0;
1017
cristya03e7992010-06-25 12:18:06 +00001018 /* row scan buffer is no longer needed */
cristy3ed852e2009-09-05 21:47:34 +00001019 RelinquishMagickMemory(BImgBuff);
1020 BImgBuff = NULL;
cristyf5e4a812012-05-01 16:40:19 +00001021
1022 if(--Frames>0)
1023 {
1024 z = z2;
1025 if(image2==NULL) image2 = image;
1026 goto NEXT_FRAME;
1027 }
1028 if ((image2!=NULL) && (image2!=image)) /* Does shadow temporary decompressed image exist? */
1029 {
1030/* CloseBlob(image2); */
1031 DeleteImageFromList(&image2);
1032 if(clone_info)
1033 {
1034 if(clone_info->file)
1035 {
1036 fclose(clone_info->file);
1037 clone_info->file = NULL;
1038 (void) remove_utf8(clone_info->filename);
1039 }
1040 }
1041 }
cristyf5e4a812012-05-01 16:40:19 +00001042 }
cristy3ed852e2009-09-05 21:47:34 +00001043
cristy8473f9e2012-05-01 16:46:04 +00001044 clone_info=DestroyImageInfo(clone_info);
cristy3ed852e2009-09-05 21:47:34 +00001045 RelinquishMagickMemory(BImgBuff);
1046 CloseBlob(image);
1047
1048
1049 {
1050 Image *p;
cristybb503372010-05-27 20:51:26 +00001051 ssize_t scene=0;
cristy3ed852e2009-09-05 21:47:34 +00001052
1053 /*
1054 Rewind list, removing any empty images while rewinding.
1055 */
1056 p=image;
1057 image=NULL;
1058 while (p != (Image *)NULL)
1059 {
1060 Image *tmp=p;
1061 if ((p->rows == 0) || (p->columns == 0)) {
1062 p=p->previous;
1063 DeleteImageFromList(&tmp);
1064 } else {
1065 image=p;
1066 p=p->previous;
1067 }
1068 }
1069
1070 /*
1071 Fix scene numbers
1072 */
1073 for (p=image; p != (Image *) NULL; p=p->next)
1074 p->scene=scene++;
1075 }
1076
cristyc5de6992009-10-06 19:19:48 +00001077 if(clone_info != NULL) /* cleanup garbage file from compression */
cristy3ed852e2009-09-05 21:47:34 +00001078 {
1079 if(clone_info->file)
1080 {
1081 fclose(clone_info->file);
1082 clone_info->file = NULL;
cristy18c6c272011-09-23 14:40:37 +00001083 (void) remove_utf8(clone_info->filename);
cristy3ed852e2009-09-05 21:47:34 +00001084 }
1085 DestroyImageInfo(clone_info);
1086 clone_info = NULL;
1087 }
1088 if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),"return");
1089 if(image==NULL)
1090 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1091 return (image);
1092}
1093
1094/*
1095%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1096% %
1097% %
1098% %
1099% R e g i s t e r M A T I m a g e %
1100% %
1101% %
1102% %
1103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1104%
1105% Method RegisterMATImage adds attributes for the MAT image format to
1106% the list of supported formats. The attributes include the image format
1107% tag, a method to read and/or write the format, whether the format
1108% supports the saving of more than one frame to the same file or blob,
1109% whether the format supports native in-memory I/O, and a brief
1110% description of the format.
1111%
1112% The format of the RegisterMATImage method is:
1113%
cristybb503372010-05-27 20:51:26 +00001114% size_t RegisterMATImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001115%
1116*/
cristybb503372010-05-27 20:51:26 +00001117ModuleExport size_t RegisterMATImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001118{
1119 MagickInfo
1120 *entry;
1121
1122 entry=SetMagickInfo("MAT");
1123 entry->decoder=(DecodeImageHandler *) ReadMATImage;
1124 entry->encoder=(EncodeImageHandler *) WriteMATImage;
1125 entry->blob_support=MagickFalse;
1126 entry->seekable_stream=MagickTrue;
cristya7cb4312010-06-26 00:47:03 +00001127 entry->description=AcquireString("MATLAB level 5 image format");
cristy3ed852e2009-09-05 21:47:34 +00001128 entry->module=AcquireString("MAT");
1129 (void) RegisterMagickInfo(entry);
1130 return(MagickImageCoderSignature);
1131}
1132
1133/*
1134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1135% %
1136% %
1137% %
1138% U n r e g i s t e r M A T I m a g e %
1139% %
1140% %
1141% %
1142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1143%
1144% Method UnregisterMATImage removes format registrations made by the
1145% MAT module from the list of supported formats.
1146%
1147% The format of the UnregisterMATImage method is:
1148%
1149% UnregisterMATImage(void)
1150%
1151*/
1152ModuleExport void UnregisterMATImage(void)
1153{
1154 (void) UnregisterMagickInfo("MAT");
1155}
1156
1157/*
1158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1159% %
1160% %
1161% %
1162% W r i t e M A T L A B I m a g e %
1163% %
1164% %
1165% %
1166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1167%
1168% Function WriteMATImage writes an Matlab matrix to a file.
1169%
1170% The format of the WriteMATImage method is:
1171%
cristy1e178e72011-08-28 19:44:34 +00001172% MagickBooleanType WriteMATImage(const ImageInfo *image_info,
1173% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001174%
1175% A description of each parameter follows.
1176%
cristy3ed852e2009-09-05 21:47:34 +00001177% o image_info: Specifies a pointer to a ImageInfo structure.
1178%
1179% o image: A pointer to an Image structure.
1180%
cristy1e178e72011-08-28 19:44:34 +00001181% o exception: return any errors or warnings in this structure.
1182%
cristy3ed852e2009-09-05 21:47:34 +00001183*/
cristy1e178e72011-08-28 19:44:34 +00001184static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image,
1185 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001186{
cristybb503372010-05-27 20:51:26 +00001187 ssize_t y;
cristy3ed852e2009-09-05 21:47:34 +00001188 unsigned z;
cristy4c08aed2011-07-01 19:47:50 +00001189 register const Quantum *p;
cristy3ed852e2009-09-05 21:47:34 +00001190
1191 unsigned int status;
1192 int logging;
cristybb503372010-05-27 20:51:26 +00001193 size_t DataSize;
cristy3ed852e2009-09-05 21:47:34 +00001194 char padding;
1195 char MATLAB_HDR[0x80];
1196 time_t current_time;
1197 struct tm local_time;
1198 unsigned char *pixels;
1199 int is_gray;
1200
1201 MagickOffsetType
1202 scene;
1203
1204 QuantumInfo
1205 *quantum_info;
1206
1207 /*
1208 Open output image file.
1209 */
1210 assert(image_info != (const ImageInfo *) NULL);
1211 assert(image_info->signature == MagickSignature);
1212 assert(image != (Image *) NULL);
1213 assert(image->signature == MagickSignature);
1214 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"enter MAT");
cristyda16f162011-02-19 23:52:17 +00001215 (void) logging;
cristy3a37efd2011-08-28 20:31:03 +00001216 assert(exception != (ExceptionInfo *) NULL);
1217 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001218 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001219 if (status == MagickFalse)
1220 return(MagickFalse);
1221 image->depth=8;
1222
1223 current_time=time((time_t *) NULL);
1224#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1225 (void) localtime_r(&current_time,&local_time);
1226#else
1227 (void) memcpy(&local_time,localtime(&current_time),sizeof(local_time));
1228#endif
1229 (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124));
cristyb51dff52011-05-19 16:55:47 +00001230 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 +00001231 OsDesc,DayOfWTab[local_time.tm_wday],MonthsTab[local_time.tm_mon],
1232 local_time.tm_mday,local_time.tm_hour,local_time.tm_min,
1233 local_time.tm_sec,local_time.tm_year+1900);
1234 MATLAB_HDR[0x7C]=0;
1235 MATLAB_HDR[0x7D]=1;
1236 MATLAB_HDR[0x7E]='I';
1237 MATLAB_HDR[0x7F]='M';
1238 (void) WriteBlob(image,sizeof(MATLAB_HDR),(unsigned char *) MATLAB_HDR);
1239 scene=0;
1240 do
1241 {
cristyd9ecd042012-06-17 18:26:12 +00001242 if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
1243 (IsImageGray(image,exception) == MagickFalse))
1244 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy1e178e72011-08-28 19:44:34 +00001245 is_gray = IsImageGray(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001246 z = is_gray ? 0 : 3;
1247
1248 /*
1249 Store MAT header.
1250 */
1251 DataSize = image->rows /*Y*/ * image->columns /*X*/;
1252 if(!is_gray) DataSize *= 3 /*Z*/;
1253 padding=((unsigned char)(DataSize-1) & 0x7) ^ 0x7;
1254
1255 (void) WriteBlobLSBLong(image, miMATRIX);
cristyf6fe0a12010-05-30 00:44:47 +00001256 (void) WriteBlobLSBLong(image, (unsigned int) DataSize+padding+(is_gray ? 48 : 56));
cristy3ed852e2009-09-05 21:47:34 +00001257 (void) WriteBlobLSBLong(image, 0x6); /* 0x88 */
1258 (void) WriteBlobLSBLong(image, 0x8); /* 0x8C */
1259 (void) WriteBlobLSBLong(image, 0x6); /* 0x90 */
1260 (void) WriteBlobLSBLong(image, 0);
1261 (void) WriteBlobLSBLong(image, 0x5); /* 0x98 */
1262 (void) WriteBlobLSBLong(image, is_gray ? 0x8 : 0xC); /* 0x9C - DimFlag */
cristyf6fe0a12010-05-30 00:44:47 +00001263 (void) WriteBlobLSBLong(image, (unsigned int) image->rows); /* x: 0xA0 */
1264 (void) WriteBlobLSBLong(image, (unsigned int) image->columns); /* y: 0xA4 */
cristy3ed852e2009-09-05 21:47:34 +00001265 if(!is_gray)
1266 {
1267 (void) WriteBlobLSBLong(image, 3); /* z: 0xA8 */
1268 (void) WriteBlobLSBLong(image, 0);
1269 }
1270 (void) WriteBlobLSBShort(image, 1); /* 0xB0 */
1271 (void) WriteBlobLSBShort(image, 1); /* 0xB2 */
1272 (void) WriteBlobLSBLong(image, 'M'); /* 0xB4 */
1273 (void) WriteBlobLSBLong(image, 0x2); /* 0xB8 */
cristyf6fe0a12010-05-30 00:44:47 +00001274 (void) WriteBlobLSBLong(image, (unsigned int) DataSize); /* 0xBC */
cristy3ed852e2009-09-05 21:47:34 +00001275
1276 /*
1277 Store image data.
1278 */
cristy3ed852e2009-09-05 21:47:34 +00001279 quantum_info=AcquireQuantumInfo(image_info,image);
1280 if (quantum_info == (QuantumInfo *) NULL)
1281 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1282 pixels=GetQuantumPixels(quantum_info);
1283 do
1284 {
cristybb503372010-05-27 20:51:26 +00001285 for (y=0; y < (ssize_t)image->columns; y++)
cristy3ed852e2009-09-05 21:47:34 +00001286 {
cristy1e178e72011-08-28 19:44:34 +00001287 p=GetVirtualPixels(image,y,0,1,image->rows,exception);
cristy4c08aed2011-07-01 19:47:50 +00001288 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001289 break;
cristy4c08aed2011-07-01 19:47:50 +00001290 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +00001291 z2qtype[z],pixels,exception);
1292 (void) WriteBlob(image,image->rows,pixels);
1293 }
cristy1e178e72011-08-28 19:44:34 +00001294 if (SyncAuthenticPixels(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001295 break;
1296 } while(z-- >= 2);
1297 while(padding-->0) (void) WriteBlobByte(image,0);
1298 quantum_info=DestroyQuantumInfo(quantum_info);
1299 if (GetNextImageInList(image) == (Image *) NULL)
1300 break;
1301 image=SyncNextImageInList(image);
1302 status=SetImageProgress(image,SaveImagesTag,scene++,
1303 GetImageListLength(image));
1304 if (status == MagickFalse)
1305 break;
1306 } while (image_info->adjoin != MagickFalse);
1307 (void) CloseBlob(image);
1308 return(MagickTrue);
1309}