Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages   Examples  

Kernel.cpp

00001 /* Copyright (c) 2001 C. Grigorescu */
00002 
00003 #include <fstream.h>
00004 #include <string.h>
00005 #include "Kernel.h"
00006 
00007 template class Kernel< byte >;
00008 template class Kernel< int >;
00009 template class Kernel< float >;
00010 
00011 template< class T >
00012 void Kernel< T >::readKernelCoeffs(char *name)
00013 {
00014   if (typeid(T) == typeid(byte)) kernel_type = KERNEL_BYTE_UNKNOWN;
00015   if (typeid(T) == typeid(int)) kernel_type = KERNEL_INT_UNKNOWN;
00016   if (typeid(T) == typeid(float)) kernel_type = KERNEL_FLOAT_UNKNOWN;
00017 
00018   int w, h;
00019   ifstream in;
00020   char *comment[1024];
00021   in.open(name,ios::in);
00022   if (in == NULL) {
00023     cout << "\nError: Cannot read kernel coefficients from file: \"" << name 
00024          <<"\"" << endl; 
00025     assert(0);
00026     return;
00027   }
00028   char c;
00029   in.get(c);
00030 
00031   while (c == '#') {
00032     in.unget();
00033     in.gets(comment);
00034     c = in.get();
00035   }
00036   in.unget();
00037 
00038   in >> h;                                                                        
00039   in >> w;
00040   makeImage(w,h);
00041   
00042   for (int j=0;j<height;j++)
00043     for (int i=0;i<width;i++)
00044       in >> pixels[j][i] ;
00045   
00046   in.close();                                                                       
00047 }
00048 
00049 template< class T >
00050 void Kernel< T >::writeKernelCoeffs(char *name, char *comment)
00051 {
00052   if (!pixels)
00053     throw Fatal_error ("Empty buffer to write to file!");
00054 
00055   ofstream out;
00056   out.open(name,ios::out);
00057   if (out == NULL)
00058     cout << "\nError: Cannot write kernel coefficients to file: \"" << name 
00059          <<"\"" << endl;
00060   
00061   out << "# " << comment << endl;
00062   out << height << " ";
00063   out << width << endl;
00064   
00065   for (int j=0;j<height;j++) {
00066     for (int i=0;i<width;i++) 
00067       out << pixels[j][i] << " ";
00068     out << endl;
00069   }
00070   out.close(); 
00071 }
00072 
00073 
00074 
00075