00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include "./IO.h"
00060
00061 #include "wand/magick_wand.h"
00062
00063 #define ThrowWandException(wand) \
00064 { \
00065 char \
00066 *description; \
00067 \
00068 ExceptionType \
00069 severity; \
00070 \
00071 description=MagickGetException(wand,&severity); \
00072 (void) fprintf(stderr,"%s %s %ld %s\n",GetMagickModule(),description); \
00073 description=(char *) MagickRelinquishMemory(description); \
00074 exit(-1); \
00075 }
00076
00077 namespace IO
00078 {
00079
00080 void WriteMFile(Matrix<ComplexFloat>& m, string variable, string filename)
00081 {
00082 Matrix<float> re = Real(m);
00083 Matrix<float> im = Imag(m);
00084
00085 ofstream mfile(filename.c_str(), ios::out);
00086 if(!RandomGen::Initialized())
00087 {
00088 RandomGen::Initialize();
00089 }
00090
00091
00092 mfile << variable << " = [ " << endl;
00093
00094 for(int i=0; i<m.Rows(); i++)
00095 {
00096 int c = 0;
00097 for(int j=0; j<m.Columns(); j++)
00098 {
00099 mfile << re.ElemNC(i,j) << " + " << im.ElemNC(i,j) << "i" << " ";
00100 c++;
00101 if(c==500)
00102 {
00103 mfile << " ..." << endl;
00104 }
00105 }
00106 mfile << endl;
00107 }
00108 mfile << "];" << endl;
00109 }
00110
00111
00112 void WriteMFile(Vector<ComplexFloat>& m, string variable, string filename)
00113 {
00114 WriteMFile((Matrix<ComplexFloat>)m, variable, filename);
00115 }
00116
00117 void WriteMFile(Matrix<ComplexDouble>& m, string variable, string filename)
00118 {
00119 Matrix<double> re = Real(m);
00120 Matrix<double> im = Imag(m);
00121
00122 ofstream mfile(filename.c_str(), ios::out);
00123 if(!RandomGen::Initialized())
00124 {
00125 RandomGen::Initialize();
00126 }
00127
00128
00129 mfile << variable << " = [ " << endl;
00130
00131 for(int i=0; i<m.Rows(); i++)
00132 {
00133 int c = 0;
00134 for(int j=0; j<m.Columns(); j++)
00135 {
00136 mfile << re.ElemNC(i,j) << " + " << im.ElemNC(i,j) << "i" << " ";
00137 c++;
00138 if(c==500)
00139 {
00140 mfile << " ..." << endl;
00141 }
00142 }
00143 mfile << endl;
00144 }
00145 mfile << "];" << endl;
00146 }
00147
00148 void WriteMFile(Vector<ComplexDouble>& m, string variable, string filename)
00149 {
00150 WriteMFile((Matrix<ComplexDouble>)m, variable, filename);
00151 }
00152
00153
00154
00155
00156
00157 MatrixList<unsigned char> ImRead(char *filename)
00158 {
00159 MagickBooleanType status;
00160 MagickWand *magick_wand;
00161 magick_wand = NewMagickWand();
00162
00163 status = MagickReadImage(magick_wand, filename);
00164 if (status == MagickFalse)
00165 {
00166 ThrowWandException(magick_wand);
00167 }
00168
00169 ImageType imType = MagickGetImageType(magick_wand);
00170 char *map;
00171 int isRGB;
00172
00173 if(imType == GrayscaleType || imType == BilevelType)
00174 {
00175 map = "I";
00176 isRGB = 1;
00177 }
00178 else if(imType == TrueColorMatteType || imType == PaletteMatteType)
00179 {
00180 map = "RGBA";
00181 isRGB = 3;
00182 }
00183 else
00184 {
00185 map = "RGB";
00186 isRGB = 2;
00187 }
00188
00189 int hei = (int)MagickGetImageHeight(magick_wand);
00190 int wid = (int)MagickGetImageWidth(magick_wand);
00191
00192 int l;
00193 if(isRGB == 2)
00194 {
00195 l = 3*wid*hei;
00196 }
00197 else if(isRGB == 1)
00198 {
00199 l = wid*hei;
00200 }
00201 else if(isRGB == 3)
00202 {
00203 l = 4*wid*hei;
00204 }
00205 unsigned char *pixels = new (std::nothrow) unsigned char[l];
00206 Utility::CheckPointer(pixels);
00207 status = MagickGetImagePixels( magick_wand, 0, 0, wid, hei, map, CharPixel, pixels );
00208 if (status == MagickFalse)
00209 {
00210 ThrowWandException(magick_wand);
00211 }
00212
00213 MatrixList<unsigned char> temp;
00214 if(isRGB == 2)
00215 {
00216 temp = MatrixList<unsigned char>(3,hei,wid);
00217 }
00218 else if(isRGB == 1)
00219 {
00220 temp = MatrixList<unsigned char>(1,hei,wid);
00221 }
00222 else if(isRGB == 3)
00223 {
00224 temp = MatrixList<unsigned char>(4,hei,wid);
00225 }
00226
00227 int z=0;
00228 for(int i=0; i<temp.Rows(); i++)
00229 {
00230 for(int j=0; j<temp.Columns(); j++)
00231 {
00232 for(int k=0; k<temp.Planes(); k++)
00233 {
00234 temp[k].ElemNC(i,j) = pixels[z];
00235 z++;
00236 }
00237 }
00238 }
00239
00240 delete [] pixels;
00241
00242 magick_wand = DestroyMagickWand(magick_wand);
00243
00244 return temp;
00245 }
00246
00247
00248
00249
00250
00251 void ImWrite(Matrix<unsigned char>& Image, char *filename)
00252 {
00253 char *map = "I";
00254 MagickBooleanType status;
00255 MagickWand *magick_wand;
00256 magick_wand = NewMagickWand();
00257
00258 unsigned char *pixels = new (std::nothrow) unsigned char[Image.Length()];
00259 Utility::CheckPointer(pixels);
00260 int z = 0;
00261 for(int i=0; i<Image.Rows(); i++)
00262 {
00263 for(int j=0; j<Image.Columns(); j++)
00264 {
00265 pixels[z] = Image.ElemNC(i,j);
00266 z++;
00267 }
00268 }
00269
00270 status = MagickConstituteImage(magick_wand, Image.Columns(), Image.Rows(), map, CharPixel, pixels);
00271 if (status == MagickFalse)
00272 {
00273 ThrowWandException(magick_wand);
00274 }
00275 status = MagickWriteImage(magick_wand, filename);
00276 if (status == MagickFalse)
00277 {
00278 ThrowWandException(magick_wand);
00279 }
00280
00281 delete [] pixels;
00282 magick_wand = DestroyMagickWand(magick_wand);
00283
00284 }
00285
00286
00287
00288 void ImWrite(MatrixList<unsigned char>& Image, char *filename)
00289 {
00290
00291 char *map;
00292 if(Image.Planes() == 1)
00293 {
00294 map = "I";
00295 }
00296 else if(Image.Planes() == 3)
00297 {
00298 map = "RGB";
00299 }
00300 else if(Image.Planes() == 4)
00301 {
00302 map = "RGBA";
00303 }
00304
00305
00306
00307
00308
00309 else
00310 {
00311 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00312 Utility::RunTimeError("MatrixList should consist either 1 (gray scale), 3 (RGB) or 4 (RGBA) planes!");
00313 }
00314
00315 MagickBooleanType status;
00316 MagickWand *magick_wand;
00317 magick_wand = NewMagickWand();
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 int arrayLength = Image.Rows()*Image.Columns()*Image.Planes();
00359 unsigned char *pixels = new (std::nothrow) unsigned char[arrayLength];
00360 Utility::CheckPointer(pixels);
00361 int z=0;
00362 for(int i=0; i<Image.Rows(); i++)
00363 {
00364 for(int j=0; j<Image.Columns(); j++)
00365 {
00366 for(int k=0; k<Image.Planes(); k++)
00367 {
00368 pixels[z] = Image[k].ElemNC(i,j);
00369 z++;
00370 }
00371 }
00372 }
00373
00374 status = MagickConstituteImage(magick_wand, Image.Columns(), Image.Rows(), map, CharPixel, pixels);
00375 if (status == MagickFalse)
00376 {
00377 ThrowWandException(magick_wand);
00378 }
00379 status = MagickWriteImage(magick_wand, filename);
00380 if (status == MagickFalse)
00381 {
00382 ThrowWandException(magick_wand);
00383 }
00384
00385
00386
00387
00388 delete [] pixels;
00389 magick_wand = DestroyMagickWand(magick_wand);
00390
00391 }
00392
00393
00394
00395
00396
00397 void ImWrite(Matrix<int>& Image, char *filename)
00398 {
00399 Matrix<unsigned char> temp(Image.Rows(), Image.Columns());
00400 int max = Maximum(Image(":"));
00401 int min = Minimum(Image(":"));
00402
00403 if((max-min) == 0)
00404 {
00405 Utility::Warning("Image is constant. Setting its values to zero (black)!");
00406 temp.Init(0);
00407 }
00408 else
00409 {
00410 for(int i=0; i<Image.Length(); i++)
00411 {
00412 temp.ElemNC(i) = (unsigned char)((Image.ElemNC(i)-min)*255.0/(max-min)+0.5);
00413 }
00414 }
00415
00416 ImWrite(temp, filename);
00417 }
00418
00419
00420
00421
00422
00423 void ImWrite(MatrixList<int>& Image, char *filename)
00424 {
00425 MatrixList<unsigned char> temp(Image.Planes(), Image.Rows(), Image.Columns());
00426 int max = Image[0].ElemNC(0);
00427 int min = Image[0].ElemNC(0);
00428 for(int z=0; z<Image.Planes(); z++)
00429 {
00430 for(int i=0; i<Image[z].Length(); i++)
00431 {
00432 max = max >= Image[z].ElemNC(i) ? max : Image[z].ElemNC(i);
00433 min = min <= Image[z].ElemNC(i) ? min : Image[z].ElemNC(i);
00434 }
00435 }
00436 if((max-min) == 0)
00437 {
00438 Utility::Warning("Image is constant. Setting its values to zero (black)!");
00439 for(int z=0; z<Image.Planes(); z++)
00440 {
00441 temp[z].Init(0);
00442 }
00443 }
00444 else
00445 {
00446 for(int z=0; z<Image.Planes(); z++)
00447 {
00448 for(int i=0; i<Image[z].Length(); i++)
00449 {
00450 temp[z].ElemNC(i) = (unsigned char)((Image[z].ElemNC(i)-min)*255.0/(max-min)+0.5);
00451 }
00452 }
00453 }
00454
00455 ImWrite(temp, filename);
00456 }
00457
00458
00459
00460
00461 void ImWrite(Matrix<float>& Image, char *filename)
00462 {
00463 Matrix<unsigned char> temp(Image.Rows(), Image.Columns());
00464 float max = Maximum(Image(":"));
00465 float min = Minimum(Image(":"));
00466
00467 if((max-min) < 0.000000001)
00468 {
00469 Utility::Warning("Image is (almost) constant. Setting its values to zero (black)!");
00470 temp.Init(0);
00471 }
00472 else
00473 {
00474 for(int i=0; i<Image.Length(); i++)
00475 {
00476 temp.ElemNC(i) = (unsigned char)((Image.ElemNC(i)-min)*255.0/(max-min)+0.5);
00477 }
00478 }
00479
00480 ImWrite(temp, filename);
00481 }
00482
00483
00484 void ImWrite(MatrixList<float>& Image, char *filename)
00485 {
00486 MatrixList<unsigned char> temp(Image.Planes(), Image.Rows(), Image.Columns());
00487 float max = Image[0].ElemNC(0);
00488 float min = Image[0].ElemNC(0);
00489 for(int z=0; z<Image.Planes(); z++)
00490 {
00491 for(int i=0; i<Image[z].Length(); i++)
00492 {
00493 max = max >= Image[z].ElemNC(i) ? max : Image[z].ElemNC(i);
00494 min = min <= Image[z].ElemNC(i) ? min : Image[z].ElemNC(i);
00495 }
00496 }
00497 if((max-min) < 0.000000001)
00498 {
00499 Utility::Warning("Image is constant. Setting its values to zero (black)!");
00500 for(int z=0; z<Image.Planes(); z++)
00501 {
00502 temp[z].Init(0);
00503 }
00504 }
00505 else
00506 {
00507 for(int z=0; z<Image.Planes(); z++)
00508 {
00509 for(int i=0; i<Image[z].Length(); i++)
00510 {
00511 temp[z].ElemNC(i) = (unsigned char)((Image[z].ElemNC(i)-min)*255.0/(max-min)+0.5);
00512 }
00513 }
00514 }
00515
00516 ImWrite(temp, filename);
00517 }
00518
00519
00520
00521 void ImWrite(Matrix<double>& Image, char *filename)
00522 {
00523 Matrix<unsigned char> temp(Image.Rows(), Image.Columns());
00524 double max = Maximum(Image(":"));
00525 double min = Minimum(Image(":"));
00526
00527 if((max-min) < 0.00000000000001)
00528 {
00529 Utility::Warning("Image is (almost) constant. Setting its values to zero (black)!");
00530 temp.Init(0);
00531 }
00532 else
00533 {
00534 for(int i=0; i<Image.Length(); i++)
00535 {
00536 temp.ElemNC(i) = (unsigned char)((Image.ElemNC(i)-min)*255.0/(max-min)+0.5);
00537 }
00538 }
00539
00540 ImWrite(temp, filename);
00541 }
00542
00543
00544 void ImWrite(MatrixList<double>& Image, char *filename)
00545 {
00546 MatrixList<unsigned char> temp(Image.Planes(), Image.Rows(), Image.Columns());
00547 double max = Image[0].ElemNC(0);
00548 double min = Image[0].ElemNC(0);
00549 for(int z=0; z<Image.Planes(); z++)
00550 {
00551 for(int i=0; i<Image[z].Length(); i++)
00552 {
00553 max = max >= Image[z].ElemNC(i) ? max : Image[z].ElemNC(i);
00554 min = min <= Image[z].ElemNC(i) ? min : Image[z].ElemNC(i);
00555 }
00556 }
00557 if((max-min) < 0.00000000000001)
00558 {
00559 Utility::Warning("Image is constant. Setting its values to zero (black)!");
00560 for(int z=0; z<Image.Planes(); z++)
00561 {
00562 temp[z].Init(0);
00563 }
00564 }
00565 else
00566 {
00567 for(int z=0; z<Image.Planes(); z++)
00568 {
00569 for(int i=0; i<Image[z].Length(); i++)
00570 {
00571 temp[z].ElemNC(i) = (unsigned char)((Image[z].ElemNC(i)-min)*255.0/(max-min)+0.5);
00572 }
00573 }
00574 }
00575
00576 ImWrite(temp, filename);
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596 };
00597