Main Page   Class Hierarchy   Compound List   File List   Compound Members  

image_.cpp

00001 /*=============================================================================
00002         File: image.cpp
00003      Purpose:
00004     Revision: $Id: image_.cpp,v 1.4 2003/01/13 19:41:11 philosophil Exp $
00005   Created by: Philippe Lavoie          (18 February 1999)
00006  Modified by: 
00007 
00008  Copyright notice:
00009           Copyright (C) 1996-1997 Philippe Lavoie
00010  
00011           This library is free software; you can redistribute it and/or
00012           modify it under the terms of the GNU Library General Public
00013           License as published by the Free Software Foundation; either
00014           version 2 of the License, or (at your option) any later version.
00015  
00016           This library is distributed in the hope that it will be useful,
00017           but WITHOUT ANY WARRANTY; without even the implied warranty of
00018           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019           Library General Public License for more details.
00020  
00021           You should have received a copy of the GNU Library General Public
00022           License along with this library; if not, write to the Free
00023           Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024 =============================================================================*/
00025 
00026 #ifndef _PLIB_IMAGE_IMAGEMAGICK_SOURCE
00027 #define _PLIB_IMAGE_IMAGEMAGICK_SOURCE
00028 
00029 #include "image.cpp"
00030 
00031 
00032 #ifdef WITH_IMAGE_MAGICK
00033 
00034 
00035 
00036 namespace PLib { 
00037 
00043   void IM_ImageT<Color>::setMatrix(){
00044     Image *image;
00045     ImageInfo image_info;
00046     ExceptionInfo exception;
00047 
00048     InitializeMagick((char *) NULL);
00049     GetExceptionInfo( &exception );
00050     GetImageInfo(&image_info);
00051     strcpy(image_info.filename,file_name.c_str());
00052 
00053     image = ReadImage(&image_info,&exception);
00054     if(image){
00055       SetImageType(image,TrueColorType);
00056       resize(image->rows,image->columns);
00057 
00058       const PixelPacket* pixels = AcquireImagePixels(image,0,0,cols(),rows(),&exception);
00059 
00060       for(int i=0;i<rows();++i)
00061         for(int j=0;j<cols();++j){
00062           elem(i,j).r = pixels->red;
00063           elem(i,j).g = pixels->green;
00064           elem(i,j).b = pixels->blue;
00065           ++pixels;
00066         }
00067 
00068       DestroyImage(image);
00069     }
00070     DestroyExceptionInfo(&exception);
00071     DestroyImageInfo(&image_info);
00072     DestroyMagick();
00073   }
00074 
00075 
00076   void IM_ImageT<unsigned char>::setMatrix(){
00077     Image *image;
00078     ImageInfo image_info;
00079     ExceptionInfo exception;
00080 
00081     InitializeMagick((char *) NULL);
00082     GetExceptionInfo( &exception );
00083     GetImageInfo(&image_info);
00084     strcpy(image_info.filename,file_name.c_str());
00085 
00086     image = ReadImage(&image_info,&exception);
00087     if(image){
00088       SetImageType(image,GrayscaleType);
00089       resize(image->rows,image->columns);
00090 
00091       const PixelPacket* pixels = AcquireImagePixels(image,0,0,cols(),rows(),&exception);
00092 
00093       for(int i=0;i<rows();++i)
00094         for(int j=0;j<cols();++j){
00095           elem(i,j) = pixels->red;
00096           ++pixels;
00097         }
00098 
00099       DestroyImage(image);
00100     }
00101     DestroyExceptionInfo(&exception);
00102     DestroyImageInfo(&image_info);
00103     DestroyMagick();
00104   }
00105 
00106  
00107   void IM_ImageT<Color>::setImage(){
00108     Image *image;
00109     ImageInfo image_info;
00110     ExceptionInfo exception;
00111     char size[MaxTextExtent];
00112 
00113     unsigned char *buffer;
00114     buffer = new unsigned char[cols()*rows()*3];
00115     for(int i=0;i<rows();++i){
00116       for(int j=0;j<cols();++j){
00117         int offset = i*cols()*3+j*3;
00118         buffer[offset]=elem(i,j).r;
00119         buffer[offset+1]=elem(i,j).g;
00120         buffer[offset+2]=elem(i,j).b;
00121       }
00122     }
00123 
00124 
00125     InitializeMagick((char *) NULL);
00126     GetExceptionInfo( &exception );
00127     GetImageInfo(&image_info);
00128 
00129     image = ConstituteImage(cols(),rows(),"RGB",CharPixel,buffer,&exception);
00130     
00131     if(image){
00132       strcpy(image->filename,file_name.c_str());
00133       int result = WriteImage(&image_info,image);
00134     }
00135 
00136     DestroyImage(image);
00137     delete []buffer;
00138     DestroyExceptionInfo(&exception);
00139     DestroyImageInfo(&image_info);
00140     DestroyMagick();
00141   }
00142   
00143   void IM_ImageT<unsigned char>::setImage(){
00144     Image *image;
00145     ImageInfo image_info;
00146     ExceptionInfo exception;
00147     char size[MaxTextExtent];
00148 
00149     unsigned char *buffer;
00150     buffer = new unsigned char[cols()*rows()];
00151     for(int i=0;i<rows();++i){
00152       for(int j=0;j<cols();++j){
00153         int offset = i*cols()+j;
00154         buffer[offset]=elem(i,j);
00155       }
00156     }
00157 
00158 
00159     InitializeMagick((char *) NULL);
00160     GetExceptionInfo( &exception );
00161     GetImageInfo(&image_info);
00162 
00163     image = ConstituteImage(cols(),rows(),"I",CharPixel,buffer,&exception);
00164     
00165     if(image){
00166       strcpy(image->filename,file_name.c_str());
00167       int result = WriteImage(&image_info,image);
00168     }
00169 
00170     DestroyImage(image);
00171     delete []buffer;
00172     DestroyExceptionInfo(&exception);
00173     DestroyImageInfo(&image_info);
00174     DestroyMagick();
00175   }
00176   
00177   
00178   int IM_ImageT<Color>::read(const std::string &filename) {
00179     file_name = filename;    
00180     setMatrix() ;
00181     return 1 ;
00182   }
00183   
00184   int IM_ImageT<unsigned char>::read(const std::string& filename) {
00185     file_name = filename;    
00186     setMatrix() ;
00187     return 1 ;
00188   }
00189 
00190 
00191   int IM_ImageT<Color>::write(const std::string& filename) {
00192     file_name = filename;    
00193     setImage() ;
00194     
00195     return 1;
00196   }
00197   
00198   int IM_ImageT<unsigned char>::write(const std::string& filename) {
00199     file_name = filename;
00200     setImage() ;
00201     
00202     return 1;
00203   }
00204 
00205 #ifdef NO_IMPLICIT_TEMPLATES
00206   template class IM_ImageT<unsigned char> ;
00207   template class IM_ImageT<Color> ;
00208 
00209 #endif
00210 
00211 } // end namespace 
00212 
00213 #endif // WITH_IMAGE_MAGICK
00214 
00215  
00216 
00217 #endif // IMAGE_IMAGEMAGICK_SOURCE

Generated on Tue Jun 24 13:26:55 2003 for NURBS++ by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002