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 | c53413d | 2011-11-17 13:04:26 +0000 | [diff] [blame] | 58 | #include "MagickCore/distort.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 59 | #include "MagickCore/exception.h" |
| 60 | #include "MagickCore/exception-private.h" |
| 61 | #include "MagickCore/image.h" |
| 62 | #include "MagickCore/image-private.h" |
| 63 | #include "MagickCore/list.h" |
| 64 | #include "MagickCore/magick.h" |
| 65 | #include "MagickCore/memory_.h" |
| 66 | #include "MagickCore/monitor.h" |
| 67 | #include "MagickCore/monitor-private.h" |
| 68 | #include "MagickCore/pixel-accessor.h" |
| 69 | #include "MagickCore/quantum-private.h" |
| 70 | #include "MagickCore/option.h" |
| 71 | #include "MagickCore/pixel.h" |
| 72 | #include "MagickCore/resource_.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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 | |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 181 | static void InsertComplexDoubleRow(Image *image,double *p,int y,double MinVal, |
| 182 | double MaxVal,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 183 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | |
| 185 | double f; |
| 186 | int x; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 187 | register Quantum *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 188 | |
| 189 | if (MinVal == 0) |
| 190 | MinVal = -1; |
| 191 | if (MaxVal == 0) |
| 192 | MaxVal = 1; |
| 193 | |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 194 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 195 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 196 | return; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 197 | for (x = 0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 198 | { |
| 199 | if (*p > 0) |
| 200 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 201 | f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q)); |
| 202 | if (f + GetPixelRed(image,q) > QuantumRange) |
| 203 | SetPixelRed(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 204 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 205 | SetPixelRed(image,GetPixelRed(image,q)+(int) f,q); |
| 206 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 207 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 208 | SetPixelGreen(image,0,q); |
| 209 | SetPixelBlue(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 210 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 211 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 212 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 213 | SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q); |
| 214 | SetPixelGreen(image,GetPixelBlue(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 215 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 216 | } |
| 217 | if (*p < 0) |
| 218 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 219 | f = (*p / MaxVal) * (QuantumRange-GetPixelBlue(image,q)); |
| 220 | if (f+GetPixelBlue(image,q) > QuantumRange) |
| 221 | SetPixelBlue(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 222 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 223 | SetPixelBlue(image,GetPixelBlue(image,q)+(int) f,q); |
| 224 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 225 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 226 | SetPixelRed(image,0,q); |
| 227 | SetPixelGreen(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 228 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 229 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 230 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 231 | SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q); |
| 232 | SetPixelGreen(image,GetPixelRed(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 233 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 234 | } |
| 235 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 236 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 237 | } |
| 238 | if (!SyncAuthenticPixels(image,exception)) |
| 239 | return; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 244 | static void InsertComplexFloatRow(Image *image,float *p,int y,double MinVal, |
| 245 | double MaxVal,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 246 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 247 | double f; |
| 248 | int x; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 249 | register Quantum *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 250 | |
| 251 | if (MinVal == 0) |
| 252 | MinVal = -1; |
| 253 | if (MaxVal == 0) |
| 254 | MaxVal = 1; |
| 255 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 256 | q = QueueAuthenticPixels(image, 0, y, image->columns, 1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 257 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | return; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 259 | for (x = 0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 260 | { |
| 261 | if (*p > 0) |
| 262 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 263 | f = (*p / MaxVal) * (QuantumRange-GetPixelRed(image,q)); |
| 264 | if (f+GetPixelRed(image,q) > QuantumRange) |
| 265 | SetPixelRed(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 266 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 267 | SetPixelRed(image,GetPixelRed(image,q)+(int) f,q); |
| 268 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 269 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 270 | SetPixelGreen(image,0,q); |
| 271 | SetPixelBlue(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 272 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 273 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 274 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 275 | SetPixelBlue(image,GetPixelBlue(image,q)-(int) (f/2.0),q); |
| 276 | SetPixelGreen(image,GetPixelBlue(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 277 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 278 | } |
| 279 | if (*p < 0) |
| 280 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 281 | f = (*p / MaxVal) * (QuantumRange - GetPixelBlue(image,q)); |
| 282 | if (f + GetPixelBlue(image,q) > QuantumRange) |
| 283 | SetPixelBlue(image,QuantumRange,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 284 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 285 | SetPixelBlue(image,GetPixelBlue(image,q)+ |
| 286 | (int) f,q); |
| 287 | if ((int) f / 2.0 > GetPixelGreen(image,q)) |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 288 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 289 | SetPixelGreen(image,0,q); |
| 290 | SetPixelRed(image,0,q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 291 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 292 | else |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 293 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 294 | SetPixelRed(image,GetPixelRed(image,q)-(int) (f/2.0),q); |
| 295 | SetPixelGreen(image,GetPixelRed(image,q),q); |
cristy | 524222d | 2011-04-25 00:37:06 +0000 | [diff] [blame] | 296 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 297 | } |
| 298 | p++; |
| 299 | q++; |
| 300 | } |
| 301 | if (!SyncAuthenticPixels(image,exception)) |
| 302 | return; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | return; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /************** READERS ******************/ |
| 308 | |
| 309 | /* This function reads one block of floats*/ |
| 310 | static 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 | |
| 321 | static 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*/ |
| 333 | static 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 | |
| 344 | static 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 */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 356 | 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] | 357 | { |
| 358 | MagickOffsetType filepos; |
| 359 | int i, x; |
| 360 | void (*ReadBlobDoublesXXX)(Image * image, size_t len, double *data); |
| 361 | void (*ReadBlobFloatsXXX)(Image * image, size_t len, float *data); |
| 362 | double *dblrow; |
| 363 | float *fltrow; |
| 364 | |
| 365 | if (endian_indicator == LSBEndian) |
| 366 | { |
| 367 | ReadBlobDoublesXXX = ReadBlobDoublesLSB; |
| 368 | ReadBlobFloatsXXX = ReadBlobFloatsLSB; |
| 369 | } |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 370 | else /* MI */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | { |
| 372 | ReadBlobDoublesXXX = ReadBlobDoublesMSB; |
| 373 | ReadBlobFloatsXXX = ReadBlobFloatsMSB; |
| 374 | } |
| 375 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 376 | 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] | 377 | 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 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 404 | for (x = 0; x < (ssize_t) SizeX; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 405 | { |
| 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 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 418 | static void FixSignedValues(const Image *image,Quantum *q, int y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 419 | { |
| 420 | while(y-->0) |
| 421 | { |
| 422 | /* Please note that negative values will overflow |
| 423 | Q=8; QuantumRange=255: <0;127> + 127+1 = <128; 255> |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 424 | <-1;-128> + 127+1 = <0; 127> */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 425 | 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); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 428 | q++; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** Fix whole row of logical/binary data. It means pack it. */ |
| 434 | static void FixLogical(unsigned char *Buff,int ldblk) |
| 435 | { |
| 436 | unsigned char mask=128; |
| 437 | unsigned char *BuffL = Buff; |
| 438 | unsigned 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) |
| 458 | static voidpf AcquireZIPMemory(voidpf context,unsigned int items, |
| 459 | unsigned int size) |
| 460 | { |
| 461 | (void) context; |
| 462 | return((voidpf) AcquireQuantumMemory(items,size)); |
| 463 | } |
| 464 | |
| 465 | static void RelinquishZIPMemory(voidpf context,voidpf memory) |
| 466 | { |
| 467 | (void) context; |
| 468 | memory=RelinquishMagickMemory(memory); |
| 469 | } |
| 470 | #endif |
| 471 | |
cristy | 0906b14 | 2011-02-21 01:10:00 +0000 | [diff] [blame] | 472 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 473 | /** This procedure decompreses an image block for a new MATLAB format. */ |
| 474 | static Image *DecompressBlock(Image *orig, MagickOffsetType Size, ImageInfo *clone_info, ExceptionInfo *exception) |
| 475 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | |
| 477 | Image *image2; |
| 478 | void *CacheBlock, *DecompressBlock; |
| 479 | z_stream zip_info; |
| 480 | FILE *mat_file; |
| 481 | size_t magick_size; |
cristy | 95236b5 | 2009-12-30 21:56:45 +0000 | [diff] [blame] | 482 | size_t extent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 483 | |
| 484 | int status; |
| 485 | |
| 486 | if(clone_info==NULL) return NULL; |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 487 | if(clone_info->file) /* Close file opened from previous transaction. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | { |
| 489 | fclose(clone_info->file); |
| 490 | clone_info->file = NULL; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 491 | (void) remove_utf8(clone_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 492 | } |
| 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); |
cristy | 95236b5 | 2009-12-30 21:56:45 +0000 | [diff] [blame] | 531 | extent=fwrite(DecompressBlock, 4096-zip_info.avail_out, 1, mat_file); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 532 | (void) extent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 533 | |
| 534 | if(status == Z_STREAM_END) goto DblBreak; |
| 535 | } |
| 536 | |
| 537 | Size -= magick_size; |
| 538 | } |
| 539 | DblBreak: |
| 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; |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 546 | if( (image2 = AcquireImage(clone_info,exception))==NULL ) goto EraseFile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 547 | status = OpenBlob(clone_info,image2,ReadBinaryBlobMode,exception); |
| 548 | if (status == MagickFalse) |
| 549 | { |
| 550 | DeleteImageFromList(&image2); |
| 551 | EraseFile: |
| 552 | fclose(clone_info->file); |
| 553 | clone_info->file = NULL; |
| 554 | UnlinkFile: |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 555 | (void) remove_utf8(clone_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | return image2; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 560 | } |
cristy | 0906b14 | 2011-02-21 01:10:00 +0000 | [diff] [blame] | 561 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | |
| 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 | |
| 594 | static 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 | |
| 601 | static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 602 | { |
| 603 | Image *image, *image2=NULL, |
| 604 | *rotated_image; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 605 | register Quantum *q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 606 | |
| 607 | unsigned int status; |
| 608 | MATHeader MATLAB_HDR; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 609 | size_t size; |
| 610 | size_t CellType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 611 | QuantumInfo *quantum_info; |
| 612 | ImageInfo *clone_info; |
| 613 | int i; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 614 | ssize_t ldblk; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 615 | unsigned char *BImgBuff = NULL; |
| 616 | double MinVal, MaxVal; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 617 | size_t Unknown6; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 618 | unsigned z; |
| 619 | int logging; |
| 620 | int sample_size; |
| 621 | MagickOffsetType filepos=0x80; |
| 622 | BlobInfo *blob; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 623 | size_t one; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 624 | |
| 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 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 640 | image = AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 641 | |
| 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)) |
| 680 | MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 681 | |
| 682 | filepos = TellBlob(image); |
| 683 | while(!EOFBlob(image)) /* object parser loop */ |
| 684 | { |
| 685 | (void) SeekBlob(image,filepos,SEEK_SET); |
| 686 | /* printf("pos=%X\n",TellBlob(image)); */ |
| 687 | |
| 688 | MATLAB_HDR.DataType = ReadBlobXXXLong(image); |
| 689 | if(EOFBlob(image)) break; |
| 690 | MATLAB_HDR.ObjectSize = ReadBlobXXXLong(image); |
| 691 | if(EOFBlob(image)) break; |
| 692 | filepos += MATLAB_HDR.ObjectSize + 4 + 4; |
| 693 | |
| 694 | image2 = image; |
| 695 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 696 | if(MATLAB_HDR.DataType == miCOMPRESSED) |
| 697 | { |
| 698 | image2 = DecompressBlock(image,MATLAB_HDR.ObjectSize,clone_info,exception); |
| 699 | if(image2==NULL) continue; |
| 700 | MATLAB_HDR.DataType = ReadBlobXXXLong(image2); /* replace compressed object type. */ |
| 701 | } |
| 702 | #endif |
| 703 | |
| 704 | if(MATLAB_HDR.DataType!=miMATRIX) continue; /* skip another objects. */ |
| 705 | |
| 706 | MATLAB_HDR.unknown1 = ReadBlobXXXLong(image2); |
| 707 | MATLAB_HDR.unknown2 = ReadBlobXXXLong(image2); |
| 708 | |
| 709 | MATLAB_HDR.unknown5 = ReadBlobXXXLong(image2); |
| 710 | MATLAB_HDR.StructureClass = MATLAB_HDR.unknown5 & 0xFF; |
| 711 | MATLAB_HDR.StructureFlag = (MATLAB_HDR.unknown5>>8) & 0xFF; |
| 712 | |
| 713 | MATLAB_HDR.unknown3 = ReadBlobXXXLong(image2); |
| 714 | if(image!=image2) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 715 | MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2); /* ??? don't understand why ?? */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 716 | MATLAB_HDR.unknown4 = ReadBlobXXXLong(image2); |
| 717 | MATLAB_HDR.DimFlag = ReadBlobXXXLong(image2); |
| 718 | MATLAB_HDR.SizeX = ReadBlobXXXLong(image2); |
| 719 | MATLAB_HDR.SizeY = ReadBlobXXXLong(image2); |
| 720 | |
| 721 | |
| 722 | switch(MATLAB_HDR.DimFlag) |
| 723 | { |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 724 | case 8: z=1; break; /* 2D matrix*/ |
| 725 | case 12: z = ReadBlobXXXLong(image2); /* 3D matrix RGB*/ |
| 726 | Unknown6 = ReadBlobXXXLong(image2); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 727 | (void) Unknown6; |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 728 | if(z!=3) ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported"); |
| 729 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 730 | default: ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported"); |
| 731 | } |
| 732 | |
| 733 | MATLAB_HDR.Flag1 = ReadBlobXXXShort(image2); |
| 734 | MATLAB_HDR.NameFlag = ReadBlobXXXShort(image2); |
| 735 | |
| 736 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 737 | "MATLAB_HDR.StructureClass %d",MATLAB_HDR.StructureClass); |
| 738 | if (MATLAB_HDR.StructureClass != mxCHAR_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 739 | MATLAB_HDR.StructureClass != mxSINGLE_CLASS && /* float + complex float */ |
| 740 | MATLAB_HDR.StructureClass != mxDOUBLE_CLASS && /* double + complex double */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | MATLAB_HDR.StructureClass != mxINT8_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 742 | MATLAB_HDR.StructureClass != mxUINT8_CLASS && /* uint8 + uint8 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 743 | MATLAB_HDR.StructureClass != mxINT16_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 744 | MATLAB_HDR.StructureClass != mxUINT16_CLASS && /* uint16 + uint16 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 745 | MATLAB_HDR.StructureClass != mxINT32_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 746 | MATLAB_HDR.StructureClass != mxUINT32_CLASS && /* uint32 + uint32 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 747 | MATLAB_HDR.StructureClass != mxINT64_CLASS && |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 748 | MATLAB_HDR.StructureClass != mxUINT64_CLASS) /* uint64 + uint64 3D */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | ThrowReaderException(CoderError,"UnsupportedCellTypeInTheMatrix"); |
| 750 | |
| 751 | switch (MATLAB_HDR.NameFlag) |
| 752 | { |
| 753 | case 0: |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 754 | size = ReadBlobXXXLong(image2); /* Object name string size */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 755 | size = 4 * (ssize_t) ((size + 3 + 1) / 4); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 756 | (void) SeekBlob(image2, size, SEEK_CUR); |
| 757 | break; |
| 758 | case 1: |
| 759 | case 2: |
| 760 | case 3: |
| 761 | case 4: |
| 762 | (void) ReadBlob(image2, 4, (unsigned char *) &size); /* Object name string */ |
| 763 | break; |
| 764 | default: |
| 765 | goto MATLAB_KO; |
| 766 | } |
| 767 | |
| 768 | CellType = ReadBlobXXXLong(image2); /* Additional object type */ |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 769 | if (logging) |
| 770 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 771 | "MATLAB_HDR.CellType: %.20g",(double) CellType); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 772 | |
| 773 | (void) ReadBlob(image2, 4, (unsigned char *) &size); /* data size */ |
| 774 | |
| 775 | /* Image is gray when no complex flag is set and 2D Matrix */ |
| 776 | if ((MATLAB_HDR.DimFlag == 8) && |
| 777 | ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0)) |
cristy | db0d99f | 2012-01-01 01:35:45 +0000 | [diff] [blame] | 778 | { |
| 779 | image->type=GrayscaleType; |
| 780 | image->colorspace=GRAYColorspace; |
| 781 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 782 | |
| 783 | switch (CellType) |
| 784 | { |
| 785 | case miINT8: |
| 786 | case miUINT8: |
| 787 | sample_size = 8; |
| 788 | if(MATLAB_HDR.StructureFlag & FLAG_LOGICAL) |
| 789 | image->depth = 1; |
| 790 | else |
| 791 | image->depth = 8; /* Byte type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 792 | ldblk = (ssize_t) MATLAB_HDR.SizeX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 793 | break; |
| 794 | case miINT16: |
| 795 | case miUINT16: |
| 796 | sample_size = 16; |
| 797 | image->depth = 16; /* Word type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 798 | ldblk = (ssize_t) (2 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | break; |
| 800 | case miINT32: |
| 801 | case miUINT32: |
| 802 | sample_size = 32; |
| 803 | image->depth = 32; /* Dword type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 804 | ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | break; |
| 806 | case miINT64: |
| 807 | case miUINT64: |
| 808 | sample_size = 64; |
| 809 | image->depth = 64; /* Qword type cell */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 810 | ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | break; |
| 812 | case miSINGLE: |
| 813 | sample_size = 32; |
| 814 | image->depth = 32; /* double type cell */ |
| 815 | (void) SetImageOption(clone_info,"quantum:format","floating-point"); |
| 816 | if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 817 | { /* complex float type cell */ |
| 818 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 819 | ldblk = (ssize_t) (4 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 820 | break; |
| 821 | case miDOUBLE: |
| 822 | sample_size = 64; |
| 823 | image->depth = 64; /* double type cell */ |
| 824 | (void) SetImageOption(clone_info,"quantum:format","floating-point"); |
| 825 | if (sizeof(double) != 8) |
| 826 | ThrowReaderException(CoderError, "IncompatibleSizeOfDouble"); |
| 827 | if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 828 | { /* complex double type cell */ |
| 829 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 830 | ldblk = (ssize_t) (8 * MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 831 | break; |
| 832 | default: |
| 833 | ThrowReaderException(CoderError, "UnsupportedCellTypeInTheMatrix"); |
| 834 | } |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 835 | (void) sample_size; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 836 | image->columns = MATLAB_HDR.SizeX; |
| 837 | image->rows = MATLAB_HDR.SizeY; |
| 838 | quantum_info=AcquireQuantumInfo(clone_info,image); |
| 839 | if (quantum_info == (QuantumInfo *) NULL) |
| 840 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 841 | one=1; |
| 842 | image->colors = one << image->depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 843 | if (image->columns == 0 || image->rows == 0) |
| 844 | goto MATLAB_KO; |
| 845 | |
| 846 | /* ----- Create gray palette ----- */ |
| 847 | |
| 848 | if (CellType==miUINT8 && z!=3) |
| 849 | { |
| 850 | if(image->colors>256) image->colors = 256; |
| 851 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 852 | if (AcquireImageColormap(image, image->colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | { |
| 854 | NoMemory:ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");} |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | If ping is true, then only set image size and colors without |
| 859 | reading any image data. |
| 860 | */ |
| 861 | if (image_info->ping) |
| 862 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 863 | size_t temp = image->columns; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 864 | image->columns = image->rows; |
| 865 | image->rows = temp; |
| 866 | goto done_reading; /* !!!!!! BAD !!!! */ |
| 867 | } |
| 868 | |
| 869 | /* ----- Load raster data ----- */ |
| 870 | BImgBuff = (unsigned char *) AcquireQuantumMemory((size_t) (ldblk),sizeof(unsigned char *)); /* Ldblk was set in the check phase */ |
| 871 | if (BImgBuff == NULL) |
| 872 | goto NoMemory; |
| 873 | |
| 874 | MinVal = 0; |
| 875 | MaxVal = 0; |
| 876 | if (CellType==miDOUBLE || CellType==miSINGLE) /* Find Min and Max Values for floats */ |
| 877 | { |
| 878 | CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &quantum_info->minimum, &quantum_info->maximum); |
| 879 | } |
| 880 | |
| 881 | /* Main loop for reading all scanlines */ |
| 882 | if(z==1) z=0; /* read grey scanlines */ |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 883 | /* else read color scanlines */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 884 | do |
| 885 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 886 | for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | { |
| 888 | q=QueueAuthenticPixels(image,0,MATLAB_HDR.SizeY-i-1,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 889 | if (q == (Quantum *)NULL) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 890 | { |
| 891 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 892 | " 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] | 893 | goto done_reading; /* Skip image rotation, when cannot set image pixels */ |
| 894 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 895 | if(ReadBlob(image2,ldblk,(unsigned char *)BImgBuff) != (ssize_t) ldblk) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 896 | { |
| 897 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 898 | " 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] | 899 | goto ExitLoop; |
| 900 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 901 | if((CellType==miINT8 || CellType==miUINT8) && (MATLAB_HDR.StructureFlag & FLAG_LOGICAL)) |
| 902 | { |
| 903 | FixLogical((unsigned char *)BImgBuff,ldblk); |
| 904 | if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 905 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | ImportQuantumPixelsFailed: |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 907 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | " 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] | 909 | break; |
| 910 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | } |
| 912 | else |
| 913 | { |
| 914 | if(ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,z2qtype[z],BImgBuff,exception) <= 0) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 915 | goto ImportQuantumPixelsFailed; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 916 | |
| 917 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 918 | if (z<=1 && /* fix only during a last pass z==0 || z==1 */ |
| 919 | (CellType==miINT8 || CellType==miINT16 || CellType==miINT32 || CellType==miINT64)) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 920 | FixSignedValues(image,q,MATLAB_HDR.SizeX); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | if (!SyncAuthenticPixels(image,exception)) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 924 | { |
| 925 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 926 | " 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] | 927 | goto ExitLoop; |
| 928 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 929 | } |
| 930 | } while(z-- >= 2); |
| 931 | ExitLoop: |
| 932 | |
| 933 | |
| 934 | /* Read complex part of numbers here */ |
| 935 | if (MATLAB_HDR.StructureFlag & FLAG_COMPLEX) |
| 936 | { /* Find Min and Max Values for complex parts of floats */ |
| 937 | CellType = ReadBlobXXXLong(image2); /* Additional object type */ |
| 938 | i = ReadBlobXXXLong(image2); /* size of a complex part - toss away*/ |
| 939 | |
| 940 | if (CellType==miDOUBLE || CellType==miSINGLE) |
| 941 | { |
| 942 | CalcMinMax(image2, image_info->endian, MATLAB_HDR.SizeX, MATLAB_HDR.SizeY, CellType, ldblk, BImgBuff, &MinVal, &MaxVal); |
| 943 | } |
| 944 | |
| 945 | if (CellType==miDOUBLE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 946 | for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 947 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | ReadBlobDoublesXXX(image2, ldblk, (double *)BImgBuff); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 949 | InsertComplexDoubleRow(image, (double *)BImgBuff, i, MinVal, MaxVal, |
| 950 | exception); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 951 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 952 | |
| 953 | if (CellType==miSINGLE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 954 | for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 955 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 956 | ReadBlobFloatsXXX(image2, ldblk, (float *)BImgBuff); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 957 | InsertComplexFloatRow(image,(float *)BImgBuff,i,MinVal,MaxVal, |
| 958 | exception); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 959 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* Image is gray when no complex flag is set and 2D Matrix AGAIN!!! */ |
| 963 | if ((MATLAB_HDR.DimFlag == 8) && |
| 964 | ((MATLAB_HDR.StructureFlag & FLAG_COMPLEX) == 0)) |
| 965 | image->type=GrayscaleType; |
| 966 | if (image->depth == 1) |
| 967 | image->type=BilevelType; |
| 968 | |
| 969 | if(image2==image) |
| 970 | image2 = NULL; /* Remove shadow copy to an image before rotation. */ |
| 971 | |
| 972 | /* Rotate image. */ |
| 973 | rotated_image = RotateImage(image, 90.0, exception); |
| 974 | if (rotated_image != (Image *) NULL) |
| 975 | { |
| 976 | /* Remove page offsets added by RotateImage */ |
| 977 | rotated_image->page.x=0; |
| 978 | rotated_image->page.y=0; |
| 979 | |
| 980 | blob = rotated_image->blob; |
| 981 | rotated_image->blob = image->blob; |
| 982 | rotated_image->colors = image->colors; |
| 983 | image->blob = blob; |
| 984 | AppendImageToList(&image,rotated_image); |
| 985 | DeleteImageFromList(&image); |
| 986 | } |
| 987 | |
| 988 | done_reading: |
| 989 | |
| 990 | if(image2!=NULL) |
| 991 | if(image2!=image) |
| 992 | { |
| 993 | DeleteImageFromList(&image2); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 994 | if(clone_info) |
| 995 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 996 | if(clone_info->file) |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 997 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 998 | fclose(clone_info->file); |
| 999 | clone_info->file = NULL; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 1000 | (void) remove_utf8(clone_info->filename); |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 1001 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* Allocate next image structure. */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 1006 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1007 | if (image->next == (Image *) NULL) break; |
| 1008 | image=SyncNextImageInList(image); |
| 1009 | image->columns=image->rows=0; |
| 1010 | image->colors=0; |
| 1011 | |
cristy | a03e799 | 2010-06-25 12:18:06 +0000 | [diff] [blame] | 1012 | /* row scan buffer is no longer needed */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1013 | RelinquishMagickMemory(BImgBuff); |
| 1014 | BImgBuff = NULL; |
| 1015 | } |
| 1016 | clone_info=DestroyImageInfo(clone_info); |
| 1017 | |
| 1018 | RelinquishMagickMemory(BImgBuff); |
| 1019 | CloseBlob(image); |
| 1020 | |
| 1021 | |
| 1022 | { |
| 1023 | Image *p; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1024 | ssize_t scene=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* |
| 1027 | Rewind list, removing any empty images while rewinding. |
| 1028 | */ |
| 1029 | p=image; |
| 1030 | image=NULL; |
| 1031 | while (p != (Image *)NULL) |
| 1032 | { |
| 1033 | Image *tmp=p; |
| 1034 | if ((p->rows == 0) || (p->columns == 0)) { |
| 1035 | p=p->previous; |
| 1036 | DeleteImageFromList(&tmp); |
| 1037 | } else { |
| 1038 | image=p; |
| 1039 | p=p->previous; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | Fix scene numbers |
| 1045 | */ |
| 1046 | for (p=image; p != (Image *) NULL; p=p->next) |
| 1047 | p->scene=scene++; |
| 1048 | } |
| 1049 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 1050 | if(clone_info != NULL) /* cleanup garbage file from compression */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | { |
| 1052 | if(clone_info->file) |
| 1053 | { |
| 1054 | fclose(clone_info->file); |
| 1055 | clone_info->file = NULL; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 1056 | (void) remove_utf8(clone_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | } |
| 1058 | DestroyImageInfo(clone_info); |
| 1059 | clone_info = NULL; |
| 1060 | } |
| 1061 | if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(),"return"); |
| 1062 | if(image==NULL) |
| 1063 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1064 | return (image); |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1069 | % % |
| 1070 | % % |
| 1071 | % % |
| 1072 | % R e g i s t e r M A T I m a g e % |
| 1073 | % % |
| 1074 | % % |
| 1075 | % % |
| 1076 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1077 | % |
| 1078 | % Method RegisterMATImage adds attributes for the MAT image format to |
| 1079 | % the list of supported formats. The attributes include the image format |
| 1080 | % tag, a method to read and/or write the format, whether the format |
| 1081 | % supports the saving of more than one frame to the same file or blob, |
| 1082 | % whether the format supports native in-memory I/O, and a brief |
| 1083 | % description of the format. |
| 1084 | % |
| 1085 | % The format of the RegisterMATImage method is: |
| 1086 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1087 | % size_t RegisterMATImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1088 | % |
| 1089 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1090 | ModuleExport size_t RegisterMATImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1091 | { |
| 1092 | MagickInfo |
| 1093 | *entry; |
| 1094 | |
| 1095 | entry=SetMagickInfo("MAT"); |
| 1096 | entry->decoder=(DecodeImageHandler *) ReadMATImage; |
| 1097 | entry->encoder=(EncodeImageHandler *) WriteMATImage; |
| 1098 | entry->blob_support=MagickFalse; |
| 1099 | entry->seekable_stream=MagickTrue; |
cristy | a7cb431 | 2010-06-26 00:47:03 +0000 | [diff] [blame] | 1100 | entry->description=AcquireString("MATLAB level 5 image format"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1101 | entry->module=AcquireString("MAT"); |
| 1102 | (void) RegisterMagickInfo(entry); |
| 1103 | return(MagickImageCoderSignature); |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1108 | % % |
| 1109 | % % |
| 1110 | % % |
| 1111 | % U n r e g i s t e r M A T I m a g e % |
| 1112 | % % |
| 1113 | % % |
| 1114 | % % |
| 1115 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1116 | % |
| 1117 | % Method UnregisterMATImage removes format registrations made by the |
| 1118 | % MAT module from the list of supported formats. |
| 1119 | % |
| 1120 | % The format of the UnregisterMATImage method is: |
| 1121 | % |
| 1122 | % UnregisterMATImage(void) |
| 1123 | % |
| 1124 | */ |
| 1125 | ModuleExport void UnregisterMATImage(void) |
| 1126 | { |
| 1127 | (void) UnregisterMagickInfo("MAT"); |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1132 | % % |
| 1133 | % % |
| 1134 | % % |
| 1135 | % W r i t e M A T L A B I m a g e % |
| 1136 | % % |
| 1137 | % % |
| 1138 | % % |
| 1139 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1140 | % |
| 1141 | % Function WriteMATImage writes an Matlab matrix to a file. |
| 1142 | % |
| 1143 | % The format of the WriteMATImage method is: |
| 1144 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1145 | % MagickBooleanType WriteMATImage(const ImageInfo *image_info, |
| 1146 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1147 | % |
| 1148 | % A description of each parameter follows. |
| 1149 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1150 | % o image_info: Specifies a pointer to a ImageInfo structure. |
| 1151 | % |
| 1152 | % o image: A pointer to an Image structure. |
| 1153 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1154 | % o exception: return any errors or warnings in this structure. |
| 1155 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1156 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1157 | static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image, |
| 1158 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1159 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1160 | ssize_t y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1161 | unsigned z; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1162 | register const Quantum *p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1163 | |
| 1164 | unsigned int status; |
| 1165 | int logging; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1166 | size_t DataSize; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1167 | char padding; |
| 1168 | char MATLAB_HDR[0x80]; |
| 1169 | time_t current_time; |
| 1170 | struct tm local_time; |
| 1171 | unsigned char *pixels; |
| 1172 | int is_gray; |
| 1173 | |
| 1174 | MagickOffsetType |
| 1175 | scene; |
| 1176 | |
| 1177 | QuantumInfo |
| 1178 | *quantum_info; |
| 1179 | |
| 1180 | /* |
| 1181 | Open output image file. |
| 1182 | */ |
| 1183 | assert(image_info != (const ImageInfo *) NULL); |
| 1184 | assert(image_info->signature == MagickSignature); |
| 1185 | assert(image != (Image *) NULL); |
| 1186 | assert(image->signature == MagickSignature); |
| 1187 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"enter MAT"); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1188 | (void) logging; |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1189 | assert(exception != (ExceptionInfo *) NULL); |
| 1190 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1191 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1192 | if (status == MagickFalse) |
| 1193 | return(MagickFalse); |
| 1194 | image->depth=8; |
| 1195 | |
| 1196 | current_time=time((time_t *) NULL); |
| 1197 | #if defined(MAGICKCORE_HAVE_LOCALTIME_R) |
| 1198 | (void) localtime_r(¤t_time,&local_time); |
| 1199 | #else |
| 1200 | (void) memcpy(&local_time,localtime(¤t_time),sizeof(local_time)); |
| 1201 | #endif |
| 1202 | (void) memset(MATLAB_HDR,' ',MagickMin(sizeof(MATLAB_HDR),124)); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1203 | 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] | 1204 | OsDesc,DayOfWTab[local_time.tm_wday],MonthsTab[local_time.tm_mon], |
| 1205 | local_time.tm_mday,local_time.tm_hour,local_time.tm_min, |
| 1206 | local_time.tm_sec,local_time.tm_year+1900); |
| 1207 | MATLAB_HDR[0x7C]=0; |
| 1208 | MATLAB_HDR[0x7D]=1; |
| 1209 | MATLAB_HDR[0x7E]='I'; |
| 1210 | MATLAB_HDR[0x7F]='M'; |
| 1211 | (void) WriteBlob(image,sizeof(MATLAB_HDR),(unsigned char *) MATLAB_HDR); |
| 1212 | scene=0; |
| 1213 | do |
| 1214 | { |
cristy | 501c559 | 2012-04-18 12:45:09 +0000 | [diff] [blame^] | 1215 | if (IssRGBColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 1216 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1217 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1218 | is_gray = IsImageGray(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1219 | z = is_gray ? 0 : 3; |
| 1220 | |
| 1221 | /* |
| 1222 | Store MAT header. |
| 1223 | */ |
| 1224 | DataSize = image->rows /*Y*/ * image->columns /*X*/; |
| 1225 | if(!is_gray) DataSize *= 3 /*Z*/; |
| 1226 | padding=((unsigned char)(DataSize-1) & 0x7) ^ 0x7; |
| 1227 | |
| 1228 | (void) WriteBlobLSBLong(image, miMATRIX); |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1229 | (void) WriteBlobLSBLong(image, (unsigned int) DataSize+padding+(is_gray ? 48 : 56)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1230 | (void) WriteBlobLSBLong(image, 0x6); /* 0x88 */ |
| 1231 | (void) WriteBlobLSBLong(image, 0x8); /* 0x8C */ |
| 1232 | (void) WriteBlobLSBLong(image, 0x6); /* 0x90 */ |
| 1233 | (void) WriteBlobLSBLong(image, 0); |
| 1234 | (void) WriteBlobLSBLong(image, 0x5); /* 0x98 */ |
| 1235 | (void) WriteBlobLSBLong(image, is_gray ? 0x8 : 0xC); /* 0x9C - DimFlag */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1236 | (void) WriteBlobLSBLong(image, (unsigned int) image->rows); /* x: 0xA0 */ |
| 1237 | (void) WriteBlobLSBLong(image, (unsigned int) image->columns); /* y: 0xA4 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1238 | if(!is_gray) |
| 1239 | { |
| 1240 | (void) WriteBlobLSBLong(image, 3); /* z: 0xA8 */ |
| 1241 | (void) WriteBlobLSBLong(image, 0); |
| 1242 | } |
| 1243 | (void) WriteBlobLSBShort(image, 1); /* 0xB0 */ |
| 1244 | (void) WriteBlobLSBShort(image, 1); /* 0xB2 */ |
| 1245 | (void) WriteBlobLSBLong(image, 'M'); /* 0xB4 */ |
| 1246 | (void) WriteBlobLSBLong(image, 0x2); /* 0xB8 */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1247 | (void) WriteBlobLSBLong(image, (unsigned int) DataSize); /* 0xBC */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1248 | |
| 1249 | /* |
| 1250 | Store image data. |
| 1251 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1252 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 1253 | if (quantum_info == (QuantumInfo *) NULL) |
| 1254 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1255 | pixels=GetQuantumPixels(quantum_info); |
| 1256 | do |
| 1257 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1258 | for (y=0; y < (ssize_t)image->columns; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1259 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1260 | p=GetVirtualPixels(image,y,0,1,image->rows,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1261 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1262 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1263 | (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1264 | z2qtype[z],pixels,exception); |
| 1265 | (void) WriteBlob(image,image->rows,pixels); |
| 1266 | } |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1267 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | } while(z-- >= 2); |
| 1270 | while(padding-->0) (void) WriteBlobByte(image,0); |
| 1271 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 1272 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1273 | break; |
| 1274 | image=SyncNextImageInList(image); |
| 1275 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 1276 | GetImageListLength(image)); |
| 1277 | if (status == MagickFalse) |
| 1278 | break; |
| 1279 | } while (image_info->adjoin != MagickFalse); |
| 1280 | (void) CloseBlob(image); |
| 1281 | return(MagickTrue); |
| 1282 | } |