//***************************************************************************
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "image.h"
#include "utils.h"
//***************************************************************************
#define BUF_SIZE 256
//***************************************************************************
int WriteToRGBFile(char *FileName, char *fmt,
        GrayImage &rI, GrayImage &gI, GrayImage &bI)
{
WriteToRGBFile(FileName, rI, gI, bI);
char *tmpfile = convert_image_format(FileName, fmt);
moveFile(tmpfile, FileName);

return 0;
}
//***************************************************************************
int GrayImage::ReadRGBimageFromFile2(char *FileName, 
                GrayImage *rI, GrayImage *gI, GrayImage *bI)
{
cout << "Reading RGB image from file \"" << FileName << "\":" << endl;

char *tmpfile = convert_image_format(FileName, "ppm");
int rtn = ReadRGBimageFromFile(tmpfile, rI, gI, bI);
deleteFile(tmpfile);

return rtn;
}
//***************************************************************************
int GrayImage::ReadRGBimageFromFile(char *FileName, 
                GrayImage *rI, GrayImage *gI, GrayImage *bI)
{
int i,j;
char buf[BUF_SIZE];
ifstream inp(FileName);

if (!inp)               // Invalid FileName
        {
         cerr << "Invalid filename: \"" << FileName << "\" does not exist!!"
                << endl;
         exit(-1);
        }

if (!strstr(FileName, "mytmp"))
        cout << "Reading RGB image from file \"" << FileName << "\":" << endl;

inp.getline(magicNumber,BUF_SIZE);

inp.getline(buf,BUF_SIZE);

int buflen = strlen(buf);
while (buf[0] == '#' || buflen < 2)
        {
         inp.getline(buf,BUF_SIZE);
         buflen = strlen(buf);
        }

width = atoi(buf);
height = atoi(strpbrk(buf, " \t"));

numpixels = height*width;

inp.getline(buf,BUF_SIZE);
numLevels = atoi(buf);

if (!allocateFlag)
        allocateMem( );

rI->Init(height, width, numLevels);
gI->Init(height, width, numLevels);
bI->Init(height, width, numLevels);

numBits = (int) (log((double) (numLevels+2))/log(2.0));

cout << "[" << numBits << "-bit ";

if (magicNumber[1] == '6' || magicNumber[1] == '3')     // RGB image
        {
         unsigned char rgb[3];
         if (magicNumber[1] == '6')             // RAWBITs
                {
                 cout << "RGB RAWBITs PPM format]";
                 for (i = 0; i < height; i++)
                        for (j = 0; j < width; j++)
                                {
                                 inp.read((char *) rgb,3);
                                 rI->p[i][j] = rgb[0];
                                 gI->p[i][j] = rgb[1];
                                 bI->p[i][j] = rgb[2];
                                 p[i][j] = (unsigned char) (sqrt((double) 
                                        (rgb[0]*rgb[0] + rgb[1]*rgb[1] 
                                        + rgb[2]*rgb[2])) /sqrt(3.0));
                                }
                }
         else                                   // ASCII
                {
                 cout << "RGB ASCII PPM format]";
                 for (i = 0; i < height; i++)
                        for (j = 0; j < width; j++)
                                {
                                 int pix;
                                 for (int k = 0; k < 3; k++)
                                        {
                                         inp >> pix;
                                         rgb[k] = (unsigned char) pix;
                                        }
                                 rI->p[i][j] = rgb[0];
                                 gI->p[i][j] = rgb[1];
                                 bI->p[i][j] = rgb[2];
                                 p[i][j] = (unsigned char) (sqrt((double) 
                                        (rgb[0]*rgb[0] + rgb[1]*rgb[1] 
                                        + rgb[2]*rgb[2])) /sqrt(3.0));
                                }
                }
        }
else
        {
         cerr << "Not an RGB image!!" << endl;
         exit(-1);
        }

inp.close();

cout << endl;
cout << "Width = " << width << endl;
cout << "Height = " << height << endl;
cout << "Numpixels = " << numpixels << endl;

numLevels = 255;
rI->numLevels = gI->numLevels = bI->numLevels = numLevels;
numBits = 8;
rI->numBits = gI->numBits = bI->numBits = numBits;
magicNumber[1] = '5';
rI->magicNumber[1] = gI->magicNumber[1] = bI->magicNumber[1] = magicNumber[1]; 

return 0;               // No error encountered....successful completion
}
//***************************************************************************
int WriteToRGBFile(char *FileName, 
                GrayImage &rI, GrayImage &gI, GrayImage &bI)
{
char buf[BUF_SIZE];
ofstream out(FileName);

if (!out)
        return -1;      // Invalid Filename

int height = rI.height;
int width = rI.width;

cout << "Writing " << height << "x" << width << " (HxW) RGB image to file \"" <<
        FileName << "\"...";
cout.flush();

out << "P6" << endl;    // RGB RAWBITs format
out << "# Created by GrayImage::WriteToRGBFile" << endl;
out << width << " " << height << endl;
out << rI.numLevels << endl;

for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
                {
                 out.write((char *) &rI.p[i][j],1);
                 out.write((char *) &gI.p[i][j],1);
                 out.write((char *) &bI.p[i][j],1);
                }

out.close();
cout << "done!" << endl;

return 0;               // No error encountered....successful completion
}
//***************************************************************************