Select_ImageLecture

Select_ImageLecture - Image Class class ImageType { public:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Image Class class ImageType { public: ImageType(); ImageType( int , int , int ); ImageType(ImageType&); ~ImageType(); void getImageInfo( int &, int &, int &); void setImageInfo( int , int , int ); void setPixelVal( int , int , int ); void getPixelVal( int , int , int &); private: int N, M, Q; //N: # rows, M: # columns int **pixelValue; };
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Image Class ImageType::ImageType() { N = 0 ; M = 0 ; Q = 0 ; pixelValue = NULL ; } ImageType::ImageType( int tmpN, int tmpM, int tmpQ) { int i, j; N = tmpN; M = tmpM; Q = tmpQ; pixelValue = new int * [N]; for (i= 0 ; i<N; i++) { pixelValue[i] = new int [M]; for (j= 0 ; j<M; j++) pixelValue[i][j] = 0 ; } } { int i, j; N = oldImage.N; M = oldImage.M; Q = oldImage.Q; pixelValue = new int * [N]; for (i= 0 ; i<N; i++) { pixelValue[i] = new int [M]; for (j= 0 ; j<M; j++) pixelValue[i][j] = oldImage.pixelValue[i][j]; } } ImageType::~ImageType() { int i; for (i= 0 ; i<N; i++) delete [] pixelValue[i]; delete [] pixelValue; } void ImageType::getImageInfo( int & rows, int & cols, int { rows = N; cols = M; levels = Q; } void ImageType::setImageInfo( int rows, int cols, int levels) { N= rows; M= cols; Q= levels; } void ImageType::setPixelVal( int i, int j, int val) { pixelValue[i][j] = val; } void ImageType::getPixelVal( int i, int j, int { val = pixelValue[i][j]; }
Background image of page 2
3 Writing a PGM image to a file #include <iostream > #include <fstream> #include "image.h" using namespace std; int writeImage( char fname[], ImageType& image) { int i, j; int N, M, Q; ofstream ofp; int val; image.getImageInfo(N, M, Q); ofp.open(fname, ios::out); ofp << "P2" << endl; ofp << M << " " << N << endl; ofp << Q << endl; if (!ofp) { cout << "Can't open file: " << fname << endl; exit( 1 ); } for (i= 0 ; i<N; i++) { for (j= 0 ; j<M; j++) { image.getPixelVal(i, j, val); if (j<M -1 ) ofp << ( unsigned int ) val << " " ; e lse ofp << ( unsigned int ) val; } i f (i<N -1 ) ofp << endl; } ofp.close(); return ( 1 ); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Reading a PGM image from a file #include <iostream > #include <fstream> #include "image.h" using namespace std; int readImage( char fname[], ImageType& image) { int i, j; int N, M, Q; unsigned int intImage; char header [ 100 ], *ptr; ifstream ifp; int val; ifp.open(fname, ios::in); if (!ifp) { cout << "Can't read image: " << fname << endl;
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

Select_ImageLecture - Image Class class ImageType { public:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online