Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gridtopgm.cpp

Go to the documentation of this file.
00001 #include <string>
00002 #include <iostream>
00003 #include <istream>
00004 #include <ostream>
00005 #include <fstream>
00006 
00007 #include "global.hpp"
00008 #include "parsecl.hpp"
00009 
00014 int main(int argc, char** argv) {
00015   using namespace Arak::Util;
00016   // Parse the command line.
00017   CommandLine cl;
00018   CommandLine::Parameter<double> 
00019     gamma("-g", "--gamma", "the gamma correction value", 1.0 / 0.45);
00020   cl.add(gamma);
00021   CommandLine::Parameter<int> 
00022     depth("-d", "--depth", "the image depth (# gray values)", 256);
00023   cl.add(depth);
00024   CommandLine::Parameter<double> 
00025     max("-min", "--min-intensity", "the surface value mapped to white", 0.0);
00026   cl.add(max);
00027   CommandLine::Parameter<double> 
00028     min("-max", "--max-intensity", "the surface value mapped to black", 1.0);
00029   cl.add(min);
00030   CommandLine::Parameter<std::string> 
00031     inputPath("-i", "--input", 
00032         "the input file of point color estimates", std::string());
00033   cl.add(inputPath);
00034   CommandLine::Parameter<std::string> 
00035     outputPath("-o", "--output", "the output PGM file", std::string());
00036   cl.add(outputPath);
00037   if (!cl.parse(argc, argv, std::cerr)) {
00038     cl.printUsage(std::cerr);
00039     exit(1);
00040   }
00041 
00042   // Grab the input and output streams.
00043   std::istream* in = &std::cin;
00044   if (inputPath.supplied()) {
00045     in = new std::ifstream(inputPath.value().data());
00046     if (!in->good()) {
00047       std::cerr << "Error: cannot open file " << inputPath.value()
00048     << " for reading." << std::endl;
00049       exit(1);
00050     }
00051   }
00052   std::ostream* out = &std::cout;
00053   if (outputPath.supplied()) {
00054     out = new std::ofstream(outputPath.value().data());
00055     if (!out->good()) {
00056       std::cerr << "Error: cannot open file " << outputPath.value()
00057     << " for writing." << std::endl;
00058       exit(1);
00059     }
00060   }
00061 
00062   // Compute the gamma corrected values.
00063   int* gcv = new int[depth.value()];
00064   gcv[0] = 0;
00065   for (int i = 1; i < depth.value(); i++) {
00066     double raw = double(i) / double(depth.value() - 1);
00067     double corr = pow(raw, gamma.value());
00068     gcv[i] = int(round(corr * double(depth.value() - 1)));
00069   }
00070 
00071   // Skip the number of samples.
00072   double tmp;
00073   *in >> tmp;
00074   // Read the dimensions.
00075   int rows, cols;
00076   *in >> rows;
00077   *in >> cols;
00078   // Skip the bounding box.
00079   *in >> tmp >> tmp >> tmp >> tmp;
00080 
00081   // Compute the interpolated values and output the file.
00082   *out << "P2" << std::endl;                       // magic number
00083   *out << "# " << outputPath.value() << std::endl; // filename
00084   *out << cols << " " << rows << std::endl;        // resolution
00085   *out << (depth.value() - 1) << std::endl;        // max-value
00086   for (int i = 0; i < rows; i++) {
00087     for (int j = 0; j < cols; j++) {
00088       double value;
00089       *in >> value;
00090       double intensity = 
00091   (value - min.value()) / (max.value() - min.value());
00092       intensity = (intensity < 0.0) ? 0.0 : intensity;
00093       intensity = (intensity > 1.0) ? 1.0 : intensity;
00094       // Compute the pixel value and then use gamma correction.
00095       *out << gcv[int(round(intensity * double(depth.value() - 1)))] << " ";
00096     }
00097     *out << std::endl;
00098   }
00099   out->flush();  
00100 
00101   // Delete any allocated streams.
00102   if (inputPath.supplied()) delete in;
00103   if (outputPath.supplied()) delete out;
00104   
00105   return 0;
00106 }

Generated on Wed May 25 14:39:16 2005 for Arak by doxygen 1.3.6