1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <sstream> // stringstream
#include <iostream> // cout, cerr
#include <fstream> // ifstream
#include <istream>
using namespace std;
// This is the code to read .txt file in, which will convert
// the contents to an array of numbers in double
double* readTXT(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i=0;
ifstream myfile (fileName);
if (myfile.is_open())
{
while ( myfile.good())
{
if (i>sizeR*sizeC-1) break;
myfile >> *(data+i);
// cout << *(data+i) << ' '; // This line display the converted data on the screen, you may comment it out.
i++;
}
myfile.close();
}
else cout << "Unable to open file";
cout << i;
return data;
}
// This is the code to write to .pgm image, which will convert the data in double to
// an image in PGM format
void WritePGM(char *filename, double *data, int& sizeR, int& sizeC, int Q)
{
int i, j;
unsigned char *image;
ofstream myfile;
image = (unsigned char *) new unsigned char [sizeR*sizeC];
// convert the integer values to unsigned char
for(i=0; i<sizeR*sizeC; i++)
image[i]=(unsigned char)data[i];
myfile.open(filename, ios::out);
if (!myfile) {
cout << "Can't open file: " << filename << endl;
exit(1);
}
myfile << "P5" << endl;
myfile << sizeC << " " << sizeR << endl;
myfile << Q << endl;
myfile.write( reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));
if (myfile.fail()) {
cout << "Can't write image " << filename << endl;
exit(0);
}
myfile.close();
}
int main()
{
// This part will show you how to use the two ReadIn functions.
double* data = 0;
int M=0; int N=0;
cout << endl;
cout << "Data from text file -------------------------------------------" << endl;
M = 768; N = 1024; //M and N represent the size of the image, e.g. for task 1, M = 512, N = 512
char* fileName2 = "C:\\Users\\FileName\\Logo.txt";
data = readTXT(fileName2, M, N);
char* fileName = "C:\\Users\\FileName\\Logo.pgm";
int Q = 255; // Q is the maximum intensity value for your image, 255 for greyscale and 1 for binary.
// int Q = 1; // set Q to 1 if processing binary image
WritePGM(fileName, data, M, N, Q);
return 0;
}
|