IO.cpp

Go to the documentation of this file.
00001 //Copyright (c) 2004-2005, Baris Sumengen
00002 //All rights reserved.
00003 //
00004 // CIMPL Matrix Performance Library
00005 //
00006 //Redistribution and use in source and binary
00007 //forms, with or without modification, are
00008 //permitted provided that the following
00009 //conditions are met:
00010 //
00011 //    * No commercial use is allowed. 
00012 //    This software can only be used
00013 //    for non-commercial purposes. This 
00014 //    distribution is mainly intended for
00015 //    academic research and teaching.
00016 //    * Redistributions of source code must
00017 //    retain the above copyright notice, this
00018 //    list of conditions and the following
00019 //    disclaimer.
00020 //    * Redistributions of binary form must
00021 //    mention the above copyright notice, this
00022 //    list of conditions and the following
00023 //    disclaimer in a clearly visible part 
00024 //    in associated product manual, 
00025 //    readme, and web site of the redistributed 
00026 //    software.
00027 //    * Redistributions in binary form must
00028 //    reproduce the above copyright notice,
00029 //    this list of conditions and the
00030 //    following disclaimer in the
00031 //    documentation and/or other materials
00032 //    provided with the distribution.
00033 //    * The name of Baris Sumengen may not be
00034 //    used to endorse or promote products
00035 //    derived from this software without
00036 //    specific prior written permission.
00037 //
00038 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
00039 //HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00040 //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
00041 //NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00042 //MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00043 //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00044 //CONTRIBUTORS BE LIABLE FOR ANY
00045 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00046 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00047 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00048 //OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00049 //DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00050 //HOWEVER CAUSED AND ON ANY THEORY OF
00051 //LIABILITY, WHETHER IN CONTRACT, STRICT
00052 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00053 //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00054 //OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00055 //POSSIBILITY OF SUCH DAMAGE.
00056 
00057 
00058 
00059 #include "./IO.h"
00060 //#include "./ImageMagick.h"
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                 //int variable = (int)(rand()/(double)RAND_MAX*1000);
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                 //int variable = (int)(rand()/(double)RAND_MAX*1000);
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                 //|| imType == BilevelType
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                 //bool imageList = false;
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                 //else if(Image.Planes() > 0)
00305                 //{
00306                 //      map = "I";
00307                 //      imageList = true;
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                 //if(imageList)
00320                 //{
00321                 //      for(int p=0;p<Image.Planes();p++)
00322                 //      {
00323                 //              MagickWand *temp_wand;
00324                 //              temp_wand = NewMagickWand();  
00325                 //              int arrayLength = Image.Rows()*Image.Columns();
00326                 //              unsigned char *pixels = new unsigned char[arrayLength];
00327                 //              int z=0;
00328                 //              for(int i=0; i<Image.Rows(); i++)
00329                 //              {
00330                 //                      for(int j=0; j<Image.Columns(); j++)
00331                 //                      {
00332                 //                              pixels[z] = Image[p].ElemNC(i,j);
00333                 //                              z++;
00334                 //                      }
00335                 //              }
00336 
00337                 //              status = MagickConstituteImage(temp_wand, Image.Columns(), Image.Rows(), map, CharPixel, pixels);
00338                 //              if (status == MagickFalse)
00339                 //              {
00340                 //                      ThrowWandException(temp_wand);
00341                 //              }
00342                 //              status = MagickAddImage(magick_wand, temp_wand);
00343                 //              if (status == MagickFalse)
00344                 //              {
00345                 //                      ThrowWandException(magick_wand);
00346                 //              }
00347                 //              temp_wand = DestroyMagickWand(temp_wand);
00348                 //      }
00349                 //      status = MagickWriteImages(magick_wand, filename, MagickTrue);
00350                 //      if (status == MagickFalse)
00351                 //      {
00352                 //              ThrowWandException(magick_wand);
00353                 //      }
00354                 //}
00355                 //else
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 

Generated on Thu Jan 20 08:43:42 2005 for CIMPL by  doxygen 1.3.9.1