/* * This Software is the original work of Daniel TUNG * This Software is submitted in partial fulfillment of the * requirements for the degree of BCSE. * Dept. Computer Science, Monash University 2000 * * Copyright (c) 2000 beetung@cs.monash.edu.au * beetung@geocities.com */ #include #include #include #include "fastbuf.h" ////////////////////////////// class FileReader { ////////////////////////////// enum { SBLU =(0), SGRE =(1), SRED =(2), SALP =(3), // SCREEN_COLUMN_PIXEL =(384), // SCREEN_ROW_PIXEL =(288), SCREEN_COLUMN_PIXEL =(396), SCREEN_ROW_PIXEL =(319), FRAME_SIZE_X =(SCREEN_COLUMN_PIXEL) , FRAME_SIZE_Y =(SCREEN_ROW_PIXEL) , FRAME_XY_SIZE =(FRAME_SIZE_X*FRAME_SIZE_Y) }; private: string fname; // int bufSize; // buf size read bool bAlloc; public: Fastbuf *buf; // buf[] unsigned char* tmpbuf; public: /// no buffer given for reading in the file /// so we create a new fastbuf object! /// //------------------------ FileReader(const string& s): fname (s),bAlloc(true) { //------------------------ buf = new Fastbuf; read2Mem(); setFastbuf(); } /// a fastbuf object given, so we set bAlloc flag to false so that /// we wont delete this buffer when we self-destruct /// //------------------------ FileReader(const string& s, Fastbuf& fb) : fname (s), bAlloc(false), buf(&fb) { //------------------------ #ifdef DEBUG cerr << "Inside FileReader::ctor() " << endl; #endif read2Mem(); setFastbuf(); } /// Read the file to memory... wasteful! /// //------------------------ void read2Mem() { //------------------------ ifstream ifs; // ifs.open(&fname[0], ios::in|ios::binary); if (ifs.bad()) { // file exist? cerr << "ERROR : cannot open file -->" <(&buf[0]); /// if (sizeof(int) ==32) ... /// // // 396x319 -- to compensate for my incorrect captured... shit! // int k=0; for (i=0;j<=bufSize&& j<=331920;i+=4) { if (k >=1152) { j=j+(1188-1152); k=0; } data[i+SRED] = tmpbuf[j+0]; data[i+SGRE] = tmpbuf[j+1]; data[i+SBLU] = tmpbuf[j+2]; data[i+SALP] = 0; k+=3; j+=3; } /* for (i=0;j<=bufSize;i+=4) { data[i+SRED] = tmpbuf[j+0]; data[i+SGRE] = tmpbuf[j+1]; data[i+SBLU] = tmpbuf[j+2]; data[i+SALP] = 0; j+=3; } */ } //------------------------ ~FileReader() { //------------------------ if (bAlloc) delete [] buf; delete [] tmpbuf; #ifdef DEBUG cout<<"~FileReader()" << endl; #endif } //------------------------ friend ostream& operator<< (ostream& os, FileReader& f) { //------------------------ os << "FileReader :" << f.fname << endl; os.flags (ios::hex | ios::showbase); unsigned char *a = (unsigned char*)f.buf; for(int i=0;i !=f.bufSize;i++) os << (unsigned char*)a[i] << "\t"; os.unsetf(ios::hex); os << endl; return os; } };