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 #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 }
00212
00213 #endif // WITH_IMAGE_MAGICK
00214
00215
00216
00217 #endif // IMAGE_IMAGEMAGICK_SOURCE