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_SOURCE
00027 #define _PLIB_IMAGE_SOURCE
00028
00029 #include "image.h"
00030 #include <stdio.h>
00031
00034 namespace PLib {
00035
00057 template <class T>
00058 void MatrixImage<T>::drawLine(int i1, int j1, int i2, int j2, T color){
00059 int i,j ;
00060 double mx,b ;
00061 if(i1<0 || j1<0 || i1>rows() || j1>=cols() ){
00062 #ifdef USE_EXCEPTION
00063 throw OutOfBound2D(i1,j1,0,rows()-1,0,cols()-1) ;
00064 #else
00065 Error error("MatrixImage<T>::drawLine") ;
00066 error << "Error in drawing line\n Invalid index ("<< i1 << ", " << j1 << ") to ( " << i2 << ", " << j2 << ") \n" ;
00067 error.warning() ;
00068 #endif
00069 return ;
00070 }
00071 if(i2 <0 || j2<0 || i2>rows() || j2>=cols() ){
00072 #ifdef USE_EXCEPTION
00073 throw OutOfBound2D(i2,j2,0,rows()-1,0,cols()-1) ;
00074 #else
00075 Error error("MatrixImage<T>::drawLine") ;
00076 error << "Error in drawing line\n Invalid index ("<< i1 << ", " << j1 << ") to ( " << i2 << ", " << j2 << ") \n" ;
00077 error.warning() ;
00078 #endif
00079 return ;
00080 }
00081
00082
00083 if(j1==j2){
00084 for(i=minimum(i1,i2);i<=maximum(i1,i2);i++)
00085 operator()(i,j1) = color ;
00086 return ;
00087 }
00088 mx = (double)(i1-i2)/(double)(j1-j2) ;
00089 b = (double)i1 - mx*j1 ;
00090 if(absolute(i1-i2)>absolute(j1-j2)){
00091 if(i1>i2){
00092 for(i=i1;i>=i2;i--){
00093 j = int(((double)i-b)/mx) ;
00094 operator()(i,j) = color ;
00095 }
00096 }
00097 else{
00098 for(i=i1;i<=i2;i++){
00099 j = (int)((i-b)/mx) ;
00100 operator()(i,j) = color ;
00101 }
00102 }
00103 }
00104 else{
00105 if(j1>j2){
00106 for(j=j1;j>=j2;j--){
00107 i = (int)(mx*j+b) ;
00108 operator()(i,j) = color ;
00109 }
00110 }
00111 else{
00112 for(j=j1;j<=j2;j++){
00113 i = (int)(mx*j+b) ;
00114 operator()(i,j) = color ;
00115 }
00116 }
00117 }
00118
00119 }
00120
00135 template <class T>
00136 void MatrixImage<T>::drawPoint(int i, int j, double r , T color){
00137 for(int y=i-int(ceil(r)) ; y<i+int(ceil(r)) ; y++)
00138 for(int x = j-int(ceil(r)) ; x<j+int(ceil(r)) ; x++){
00139 if(y>=0 && y<rows() && x>=0 && x<cols()){
00140 if( ((y-i)*(y-i)+(x-j)*(x-j))<= r*r)
00141 operator()(y,x) = color ;
00142 }
00143 }
00144 }
00145
00146
00157 template <class T>
00158 void MatrixImage<T>::store(Matrix<T>& a){
00159 if(a.rows() != rows() || a.cols() != cols()) {
00160 a.resize(rows(),cols()) ;
00161 }
00162 T *aptr, *bptr ;
00163 int size,i ;
00164 aptr = &a(0,0)-1 ;
00165 bptr = m-1 ;
00166 size = cols()*rows() ;
00167 for(i=0;i<size;i++)
00168 *(++aptr) = *(++bptr) ;
00169 }
00170
00171 #ifdef WITH_IMAGE_MAGICK
00172
00182 template <class T>
00183 IM_ImageT<T>::IM_ImageT(): MatrixImage<T>(){
00184 autoSave = 0 ;
00185 }
00186
00208 template <class T>
00209 IM_ImageT<T>::IM_ImageT(const std::string &filename, int save): MatrixImage<T>(){
00210 autoSave = save ;
00211
00212 file_name=filename;
00213 }
00214
00227 template <class T>
00228 IM_ImageT<T>::IM_ImageT(const int r, const int c): MatrixImage<T>(r,c){
00229 autoSave = 0 ;
00230 }
00231
00247 template <class T>
00248 int IM_ImageT<T>::read(const std::string& filename) {
00249 #ifdef USE_EXCEPTION
00250 throw MatrixErr() ;
00251 #else
00252 Error error("IM_ImageT<T>::read") ;
00253 error << "An image of this type is NOT supported!\n" ;
00254 error.fatal() ;
00255 #endif
00256 return 0;
00257 }
00258
00274 template <class T>
00275 int IM_ImageT<T>::write(const std::string& filename) {
00276 #ifdef USE_EXCEPTION
00277 throw MatrixErr() ;
00278 #else
00279 Error error("IM_ImageT<T>::write") ;
00280 error << "An image of this type is NOT supported!\n" ;
00281 error.fatal() ;
00282 #endif
00283 return 0 ;
00284 }
00285
00302 template <class T>
00303 void IM_ImageT<T>::setMatrix(){
00304 #ifdef USE_EXCEPTION
00305 throw MatrixErr() ;
00306 #else
00307 Error error("IM_ImageT<T>::setImage") ;
00308 error << "An image of this type is NOT supported!\n" ;
00309 error.fatal() ;
00310 #endif
00311 }
00312
00313
00329 template <class T>
00330 void IM_ImageT<T>::setImage(){
00331 #ifdef USE_EXCEPTION
00332 throw MatrixErr() ;
00333 #else
00334 Error error("IM_ImageT<T>::setImage") ;
00335 error << "An image of this type is NOT supported!\n" ;
00336 error.fatal() ;
00337 #endif
00338 }
00339
00350 template <class T>
00351 IM_ImageT<T>::~IM_ImageT(){
00352 if(autoSave && file_name.length()>0){
00353
00354 }
00355 }
00356
00357 #endif
00358
00359
00360 #ifdef NO_IMPLICIT_TEMPLATES
00361 template class MatrixImage<int> ;
00362 template class MatrixImage<float> ;
00363 template class MatrixImage<double> ;
00364 template class MatrixImage<char> ;
00365 template class MatrixImage<unsigned char> ;
00366 template class MatrixImage<Color> ;
00367 #endif
00368
00369
00370 }
00371
00372
00373 #endif