cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % M M AAA TTTTT L AAA BBBB % |
| 6 | % MM MM A A T L A A B B % |
| 7 | % M M M AAAAA T L AAAAA BBBB % |
| 8 | % M M A A T L A A B B % |
| 9 | % M M A A T LLLLL A A BBBB % |
| 10 | % % |
| 11 | % % |
| 12 | % Read MATLAB Image Format % |
| 13 | % % |
| 14 | % Software Design % |
| 15 | % Jaroslav Fojtik % |
| 16 | % 2001-2008 % |
| 17 | % % |
| 18 | % % |
| 19 | % Permission is hereby granted, free of charge, to any person obtaining a % |
| 20 | % copy of this software and associated documentation files ("ImageMagick"), % |
| 21 | % to deal in ImageMagick without restriction, including without limitation % |
| 22 | % the rights to use, copy, modify, merge, publish, distribute, sublicense, % |
| 23 | % and/or sell copies of ImageMagick, and to permit persons to whom the % |
| 24 | % ImageMagick is furnished to do so, subject to the following conditions: % |
| 25 | % % |
| 26 | % The above copyright notice and this permission notice shall be included in % |
| 27 | % all copies or substantial portions of ImageMagick. % |
| 28 | % % |
| 29 | % The software is provided "as is", without warranty of any kind, express or % |
| 30 | % implied, including but not limited to the warranties of merchantability, % |
| 31 | % fitness for a particular purpose and noninfringement. In no event shall % |
| 32 | % ImageMagick Studio be liable for any claim, damages or other liability, % |
| 33 | % whether in an action of contract, tort or otherwise, arising from, out of % |
| 34 | % or in connection with ImageMagick or the use or other dealings in % |
| 35 | % ImageMagick. % |
| 36 | % % |
| 37 | % Except as contained in this notice, the name of the ImageMagick Studio % |
| 38 | % shall not be used in advertising or otherwise to promote the sale, use or % |
| 39 | % other dealings in ImageMagick without prior written authorization from the % |
| 40 | % ImageMagick Studio. % |
| 41 | % % |
| 42 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 43 | % |
| 44 | % |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | Include declarations. |
| 49 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 50 | #include "MagickCore/studio.h" |
| 51 | #include "MagickCore/attribute.h" |
| 52 | #include "MagickCore/blob.h" |
| 53 | #include "MagickCore/blob-private.h" |
| 54 | #include "MagickCore/cache.h" |
| 55 | #include "MagickCore/color-private.h" |
| 56 | #include "MagickCore/colormap.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 57 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 58 | #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" |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 77 | #include "MagickCore/utility-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 78 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 79 | #include "zlib.h" |
| 80 | #endif |
| 81 | |
| 82 | /* |
| 83 | Forward declaration. |
| 84 | */ |
| 85 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 86 | WriteMATImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | /* Auto coloring method, sorry this creates some artefact inside data |
| 90 | MinReal+j*MaxComplex = red MaxReal+j*MaxComplex = black |
| 91 | MinReal+j*0 = white MaxReal+j*0 = black |
| 92 | MinReal+j*MinComplex = blue MaxReal+j*MinComplex = black |
| 93 | */ |
| 94 | |
| 95 | typedef struct |
| 96 | { |
| 97 | char identific[124]; |
| 98 | unsigned short Version; |
| 99 | char EndianIndicator[2]; |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 100 | unsigned long DataType; |
| 101 | unsigned long ObjectSize; |
| 102 | unsigned long unknown1; |
| 103 | unsigned long unknown2; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 104 | |
| 105 | unsigned short unknown5; |
| 106 | unsigned char StructureFlag; |
| 107 | unsigned char StructureClass; |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 108 | unsigned long unknown3; |
| 109 | unsigned long unknown4; |
| 110 | unsigned long DimFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 111 | |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 112 | unsigned long SizeX; |
| 113 | unsigned long SizeY; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 114 | unsigned short Flag1; |
| 115 | unsigned short NameFlag; |
| 116 | } |
| 117 | MATHeader; |
| 118 | |
| 119 | static const char *MonthsTab[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; |
| 120 | static const char *DayOfWTab[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
| 121 | static const char *OsDesc= |
| 122 | #ifdef __WIN32__ |
| 123 | "PCWIN"; |
| 124 | #else |
| 125 | #ifdef __APPLE__ |
| 126 | "MAC"; |
| 127 | #else |
| 128 | "LNX86"; |
| 129 | #endif |
| 130 | #endif |
| 131 | |
| 132 | typedef enum |
| 133 | { |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 134 | 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 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 141 | miRESERVE1, |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 142 | miDOUBLE, /* IEEE 754 double precision float */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | miRESERVE2, |
| 144 | miRESERVE3, |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 145 | 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 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 152 | } mat5_data_type; |
| 153 | |
| 154 | typedef enum |
| 155 | { |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 156 | 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 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | mxFUNCTION_CLASS /* Function handle */ |
| 172 | } arrayclasstype; |
| 173 | |
| 174 | #define FLAG_COMPLEX 0x8 |
| 175 | #define FLAG_GLOBAL 0x4 |
| 176 | #define FLAG_LOGICAL 0x2 |
| 177 | |
| 178 | static const QuantumType z2qtype[4] = {GrayQuantum, BlueQuantum, GreenQuantum, RedQuantum}; |
| 179 | |
| 180 | |
| 181 | static 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; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 189 | register Quantum *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 190 | |
| 191 | if (MinVal == 0) |
| 192 | MinVal = -1; |
| 193 | if (MaxVal == 0) |
| 194 | MaxVal = 1; |
| 195 | |
| 196 | exception=(&image->exception); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 197 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 198 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | return; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 200 | for (x = 0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | { |
| 202 | if (*p > 0) |
| 203 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 204 | f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q)); |
| 205 | if (f + GetPixelRed(image,q) > QuantumRange) |
| 206 | SetPixelRed(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 208 | SetPixelRed(image,GetPixelRed(image,q)+(int) f,q); |
| 209 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 210 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 211 | SetPixelGreen(image,0,q); |
| 212 | SetPixelBlue(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 213 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 214 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 215 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 216 | SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q); |
| 217 | SetPixelGreen(image,GetPixelBlue(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 218 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 219 | } |
| 220 | if (*p < 0) |
| 221 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 222 | f = (*p / MaxVal) * (QuantumRange-GetPixelBlue(image,q)); |
| 223 | if (f+GetPixelBlue(image,q) > QuantumRange) |
| 224 | SetPixelBlue(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 225 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 226 | SetPixelBlue(image,GetPixelBlue(image,q)+(int) f,q); |
| 227 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 228 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 229 | SetPixelRed(image,0,q); |
| 230 | SetPixelGreen(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 231 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 232 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 233 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 234 | SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q); |
| 235 | SetPixelGreen(image,GetPixelRed(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 236 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 237 | } |
| 238 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 239 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | } |
| 241 | if (!SyncAuthenticPixels(image,exception)) |
| 242 | return; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | static 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; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 255 | register Quantum *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 256 | |
| 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); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 264 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | return; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 266 | for (x = 0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | { |
| 268 | if (*p > 0) |
| 269 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 270 | f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q)); |
| 271 | if (f+GetPixelRed(image,q) > QuantumRange) |
| 272 | SetPixelRed(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 273 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 274 | SetPixelRed(image,GetPixelRed(image,q)+(int) f,q); |
| 275 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 276 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 277 | SetPixelGreen(image,0,q); |
| 278 | SetPixelBlue(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 279 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 281 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 282 | SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q); |
| 283 | SetPixelGreen(image,GetPixelBlue(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 284 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 285 | } |
| 286 | if (*p < 0) |
| 287 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 288 | f = (*p / MaxVal) * (QuantumRange - GetPixelBlue(image,q)); |
| 289 | if (f + GetPixelBlue(image,q) > QuantumRange) |
| 290 | SetPixelBlue(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 291 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 292 | SetPixelBlue(image,GetPixelBlue(image,q)+ |
| 293 | (int) f,q); |
| 294 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 295 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 296 | SetPixelGreen(image,0,q); |
| 297 | SetPixelRed(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 298 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 299 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 300 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 301 | SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q); |
| 302 | SetPixelGreen(image,GetPixelRed(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 303 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 304 | } |
| 305 | p++; |
| 306 | q++; |
| 307 | } |
| 308 | if (!SyncAuthenticPixels(image,exception)) |
| 309 | return; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /************** READERS ******************/ |
| 315 | |
| 316 | /* This function reads one block of floats*/ |
| 317 | static 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 | |
| 328 | static 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*/ |
| 340 | static 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 | |
| 351 | static 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 */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 363 | static void CalcMinMax(Image *image, int endian_indicator, int SizeX, int SizeY, size_t CellType, unsigned ldblk, void *BImgBuff, double *Min, double *Max) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 364 | { |
| 365 | MagickOffsetType filepos; |
| 366 | int i, x; |
| 367 | void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data); |
| 368 | void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data); |
| 369 | double *dblrow; |
| 370 | float *fltrow; |
| 371 | |
| 372 | if (endian_indicator == LSBEndian) |
| 373 | { |
| 374 | ReadBlobDoublesXXX = ReadBlobDoublesLSB; |
| 375 | ReadBlobFloatsXXX = ReadBlobFloatsLSB; |
| 376 | } |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 377 | else /* MI */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 378 | { |
| 379 | ReadBlobDoublesXXX = ReadBlobDoublesMSB; |
| 380 | ReadBlobFloatsXXX = ReadBlobFloatsMSB; |
| 381 | } |
| 382 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 383 | filepos = TellBlob(image); /* Please note that file seeking occurs only in the case of doubles */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 384 | 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 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 411 | for (x = 0; x < (ssize_t) SizeX; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 412 | { |
| 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 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 425 | static void FixSignedValues(const Image *image,Quantum *q, int y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 426 | { |
| 427 | while(y-->0) |
| 428 | { |
| 429 | /* Please note that negative values will overflow |
| 430 | Q=8; QuantumRange=255: <0;127> + 127+1 = <128; 255> |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 431 | <-1;-128> + 127+1 = <0; 127> */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 432 | SetPixelRed(image,GetPixelRed(image,q)+QuantumRange/2+1,q); |
| 433 | SetPixelGreen(image,GetPixelGreen(image,q)+QuantumRange/2+1,q); |
| 434 | SetPixelBlue(image,GetPixelBlue(image,q)+QuantumRange/2+1,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 435 | q++; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** Fix whole row of logical/binary data. It means pack it. */ |
| 441 | static void FixLogical(unsigned char *Buff,int ldblk) |
| 442 | { |
| 443 | unsigned char mask=128; |
| 444 | unsigned char *BuffL = Buff; |
| 445 | unsigned 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) |
| 465 | static voidpf AcquireZIPMemory(voidpf context,unsigned int items, |
| 466 | unsigned int size) |
| 467 | { |
| 468 | (void) context; |
| 469 | return((voidpf) AcquireQuantumMemory(items,size)); |
| 470 | } |
| 471 | |
| 472 | static void RelinquishZIPMemory(voidpf context,voidpf memory) |
| 473 | { |
| 474 | (void) context; |
| 475 | memory=RelinquishMagickMemory(memory); |
| 476 | } |
| 477 | #endif |
| 478 | |
cristy | 0906b14 | 2011-02-21 01:10:00 +0000 | [diff] [blame] | 479 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 480 | /** This procedure decompreses an image block for a new MATLAB format. */ |
| 481 | static Image *DecompressBlock(Image *orig, MagickOffsetType Size, ImageInfo *clone_info, ExceptionInfo *exception) |
| 482 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 483 | |
| 484 | Image *image2; |
| 485 | void *CacheBlock, *DecompressBlock; |
| 486 | z_stream zip_info; |
| 487 | FILE *mat_file; |
| 488 | size_t magick_size; |
cristy | 95236b5 | 2009-12-30 21:56:45 +0000 | [diff] [blame] | 489 | size_t extent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 490 | |
| 491 | int status; |
| 492 | |
| 493 | if(clone_info==NULL) return NULL; |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 494 | if(clone_info->file) /* Close file opened from previous transaction. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | { |
| 496 | fclose(clone_info->file); |
| 497 | clone_info->file = NULL; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 498 | (void) remove_utf8(clone_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | } |
| 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); |
cristy | 95236b5 | 2009-12-30 21:56:45 +0000 | [diff] [blame] | 538 | extent=fwrite(DecompressBlock, 4096-zip_info.avail_out, 1, mat_file); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 539 | (void) extent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 540 | |
| 541 | if(status == Z_STREAM_END) goto DblBreak; |
| 542 | } |
| 543 | |
| 544 | Size -= magick_size; |
| 545 | } |
| 546 | DblBreak: |
| 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; |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 553 | if( (image2 = AcquireImage(clone_info,exception))==NULL ) goto EraseFile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 554 | status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception); |
| 555 | if (status == MagickFalse) |
| 556 | { |
| 557 | DeleteImageFromList(&image2); |
| 558 | EraseFile: |
| 559 | fclose(clone_info->file); |
| 560 | clone_info->file = NULL; |
| 561 | UnlinkFile: |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 562 | (void) remove_utf8(clone_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | return image2; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 567 | } |
cristy | 0906b14 | 2011-02-21 01:10:00 +0000 | [diff] [blame] | 568 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | |
| 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 | |
| 601 | static 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 | |
| 608 | static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 609 | { |
| 610 | Image *image, *image2=NULL, |
| 611 | *rotated_image; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 612 | register Quantum *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 613 | |
| 614 | unsigned int status; |
| 615 | MATHeader MATLAB_HDR; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 616 | size_t size; |
| 617 | size_t CellType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 618 | QuantumInfo *quantum_info; |
| 619 | ImageInfo *clone_info; |
| 620 | int i; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 621 | ssize_t ldblk; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 622 | unsigned char *BImgBuff = NULL; |
| 623 | double MinVal, MaxVal; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 624 | size_t Unknown6; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 625 | unsigned z; |
| 626 | int logging; |
| 627 | int sample_size; |
| 628 | MagickOffsetType filepos=0x80; |
| 629 | BlobInfo *blob; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 630 | size_t one; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 631 | |
| 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 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 647 | image = AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | |
| 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)) |
| 687 | MATLAB_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) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 722 | MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2); /* ??? don't understand why ?? */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | 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 | { |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 731 | case 8: z=1; break; /* 2D matrix*/ |
| 732 | case 12: z = ReadBlobXXXLong(image2); /* 3D matrix RGB*/ |
| 733 | Unknown6 = ReadBlobXXXLong(image2); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 734 | (void) Unknown6; |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 735 | if(z!=3) ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported"); |
| 736 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 737 | 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 && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 746 | MATLAB_HDR.StructureClass != mxSINGLE_CLASS && /* float + complex float */ |
| 747 | MATLAB_HDR.StructureClass != mxDOUBLE_CLASS && /* double + complex double */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 748 | MATLAB_HDR.StructureClass != mxINT8_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 749 | MATLAB_HDR.StructureClass != mxUINT8_CLASS && /* uint8 + uint8 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 750 | MATLAB_HDR.StructureClass != mxINT16_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 751 | MATLAB_HDR.StructureClass != mxUINT16_CLASS && /* uint16 + uint16 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 752 | MATLAB_HDR.StructureClass != mxINT32_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 753 | MATLAB_HDR.StructureClass != mxUINT32_CLASS && /* uint32 + uint32 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 754 | MATLAB_HDR.StructureClass != mxINT64_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 755 | MATLAB_HDR.StructureClass != mxUINT64_CLASS) /* uint64 + uint64 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 756 | ThrowReaderException(CoderError,"UnsupportedCellTypeInTheMatrix"); |
| 757 | |
| 758 | switch (MATLAB_HDR.NameFlag) |
| 759 | { |
| 760 | case 0: |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 761 | size = ReadBlobXXXLong(image2); /* Object name string size */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 762 | size = 4 * (ssize_t) ((size + 3 + 1) / 4); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | (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 */ |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 776 | if (logging) |
| 777 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 778 | "MATLAB_HDR.CellType: %.20g",(double) CellType); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 779 | |
| 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 */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 796 | ldblk = (ssize_t) MATLAB_HDR.SizeX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | break; |
| 798 | case miINT16: |
| 799 | case miUINT16: |
| 800 | sample_size = 16; |
| 801 | image->depth = 16; /* Word type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 802 | ldblk = (ssize_t) (2 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 803 | break; |
| 804 | case miINT32: |
| 805 | case miUINT32: |
| 806 | sample_size = 32; |
| 807 | image->depth = 32; /* Dword type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 808 | ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 809 | break; |
| 810 | case miINT64: |
| 811 | case miUINT64: |
| 812 | sample_size = 64; |
| 813 | image->depth = 64; /* Qword type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 814 | ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 815 | 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) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 821 | { /* complex float type cell */ |
| 822 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 823 | ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | 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) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 832 | { /* complex double type cell */ |
| 833 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 834 | ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 835 | break; |
| 836 | default: |
| 837 | ThrowReaderException(CoderError, "UnsupportedCellTypeInTheMatrix"); |
| 838 | } |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 839 | (void) sample_size; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 840 | 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"); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 845 | one=1; |
| 846 | image->colors = one << image->depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 847 | 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 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 856 | if (AcquireImageColormap(image, image->colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 857 | { |
| 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 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 867 | size_t temp = image->columns; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 868 | 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 */ |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 887 | /* else read color scanlines */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 888 | do |
| 889 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 890 | for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 891 | { |
| 892 | q=QueueAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 893 | if (q == (Quantum *)NULL) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 894 | { |
| 895 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | " MAT set image pixels returns unexpected NULL on a row %u.", (unsigned)(MATLAB_HDR.SizeY-i-1)); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 897 | goto done_reading; /* Skip image rotation, when cannot set image pixels */ |
| 898 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 899 | if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 900 | { |
| 901 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 902 | " MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1)); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 903 | goto ExitLoop; |
| 904 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 905 | 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) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 909 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 910 | ImportQuantumPixelsFailed: |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 911 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 912 | " MAT failed to ImportQuantumPixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1)); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 913 | break; |
| 914 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 915 | } |
| 916 | else |
| 917 | { |
| 918 | if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 919 | goto ImportQuantumPixelsFailed; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 920 | |
| 921 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 922 | if (z<=1 && /* fix only during a last pass z==0 || z==1 */ |
| 923 | (CellType==miINT8 || CellType==miINT16 || CellType==miINT32 || CellType==miINT64)) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 924 | FixSignedValues(image,q,MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | if (!SyncAuthenticPixels(image,exception)) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 928 | { |
| 929 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | " MAT failed to sync image pixels for a row %u", (unsigned)(MATLAB_HDR.SizeY-i-1)); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 931 | goto ExitLoop; |
| 932 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 933 | } |
| 934 | } while(z-- >= 2); |
| 935 | ExitLoop: |
| 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) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 950 | for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 951 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 952 | ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff); |
| 953 | InsertComplexDoubleRow((double *)BImgBuff, i, image, MinVal, MaxVal); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 954 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 955 | |
| 956 | if (CellType==miSINGLE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 957 | for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 958 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 959 | ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff); |
| 960 | InsertComplexFloatRow((float *)BImgBuff, i, image, MinVal, MaxVal); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 961 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | } |
| 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 | |
| 990 | done_reading: |
| 991 | |
| 992 | if(image2!=NULL) |
| 993 | if(image2!=image) |
| 994 | { |
| 995 | DeleteImageFromList(&image2); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 996 | if(clone_info) |
| 997 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 998 | if(clone_info->file) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 999 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1000 | fclose(clone_info->file); |
| 1001 | clone_info->file = NULL; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 1002 | (void) remove_utf8(clone_info->filename); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 1003 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* Allocate next image structure. */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 1008 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | if (image->next == (Image *) NULL) break; |
| 1010 | image=SyncNextImageInList(image); |
| 1011 | image->columns=image->rows=0; |
| 1012 | image->colors=0; |
| 1013 | |
cristy | a03e799 | 2010-06-25 12:18:06 +0000 | [diff] [blame] | 1014 | /* row scan buffer is no longer needed */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1015 | 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; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1026 | ssize_t scene=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1027 | |
| 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 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 1052 | if(clone_info != NULL) /* cleanup garbage file from compression */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1053 | { |
| 1054 | if(clone_info->file) |
| 1055 | { |
| 1056 | fclose(clone_info->file); |
| 1057 | clone_info->file = NULL; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 1058 | (void) remove_utf8(clone_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1059 | } |
| 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 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1089 | % size_t RegisterMATImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1090 | % |
| 1091 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1092 | ModuleExport size_t RegisterMATImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1093 | { |
| 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; |
cristy | a7cb431 | 2010-06-26 00:47:03 +0000 | [diff] [blame] | 1102 | entry->description=AcquireString("MATLAB level 5 image format"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1103 | 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 | */ |
| 1127 | ModuleExport 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 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1147 | % MagickBooleanType WriteMATImage(const ImageInfo *image_info, |
| 1148 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1149 | % |
| 1150 | % A description of each parameter follows. |
| 1151 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1152 | % o image_info: Specifies a pointer to a ImageInfo structure. |
| 1153 | % |
| 1154 | % o image: A pointer to an Image structure. |
| 1155 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1156 | % o exception: return any errors or warnings in this structure. |
| 1157 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1158 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1159 | static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image, |
| 1160 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1161 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1162 | ssize_t y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1163 | unsigned z; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1164 | register const Quantum *p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1165 | |
| 1166 | unsigned int status; |
| 1167 | int logging; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1168 | size_t DataSize; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1169 | 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"); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1190 | (void) logging; |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1191 | assert(exception != (ExceptionInfo *) NULL); |
| 1192 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1193 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1194 | 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(¤t_time,&local_time); |
| 1201 | #else |
| 1202 | (void) memcpy(&local_time,localtime(¤t_time),sizeof(local_time)); |
| 1203 | #endif |
| 1204 | (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124)); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1205 | FormatLocaleString(MATLAB_HDR,MaxTextExtent,"MATLAB 5.0 MAT-file, Platform: %s, Created on: %s %s %2d %2d:%2d:%2d %d", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1206 | 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 | { |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 1217 | if (IsRGBColorspace(image->colorspace) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1218 | (void) TransformImageColorspace(image,RGBColorspace); |
| 1219 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1220 | is_gray = IsImageGray(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1221 | 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); |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1231 | (void) WriteBlobLSBLong(image, (unsigned int) DataSize+padding+(is_gray ? 48 : 56)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1232 | (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 */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1238 | (void) WriteBlobLSBLong(image, (unsigned int) image->rows); /* x: 0xA0 */ |
| 1239 | (void) WriteBlobLSBLong(image, (unsigned int) image->columns); /* y: 0xA4 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1240 | 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 */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1249 | (void) WriteBlobLSBLong(image, (unsigned int) DataSize); /* 0xBC */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1250 | |
| 1251 | /* |
| 1252 | Store image data. |
| 1253 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1254 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 1255 | if (quantum_info == (QuantumInfo *) NULL) |
| 1256 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1257 | pixels=GetQuantumPixels(quantum_info); |
| 1258 | do |
| 1259 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1260 | for (y=0; y < (ssize_t)image->columns; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1261 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1262 | p=GetVirtualPixels(image,y,0,1,image->rows,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1263 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1264 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1265 | (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1266 | z2qtype[z],pixels,exception); |
| 1267 | (void) WriteBlob(image,image->rows,pixels); |
| 1268 | } |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1269 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1270 | 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 | } |