Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

nm16.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <cstdio>
00003 
00004 using namespace std;
00005 
00006 int kernelSize = 2;
00007 int halfKernelSize = 1;
00008 
00009 
00010 float readS16(istream& in)
00011 {
00012     unsigned char c[2];
00013     cin.read((char*) c, 2);
00014 
00015     return (((short) c[0] << 8) | c[1]) * (1.0f / 65535.0f);
00016 }
00017 
00018 
00019 float* readRowS16(istream& in, int width)
00020 {
00021     float* row = new float[width];
00022     for (int i = 0; i < width; i++)
00023         row[i] = readS16(in);
00024 
00025     return row;
00026 }
00027 
00028 
00029 int main(int argc, char* argv[])
00030 {
00031     if (argc < 5)
00032     {
00033         cerr << "Usage: nm16 <width> <height> <bumpheight> <filter method>\n";
00034         return 1;
00035     }
00036 
00037     int width = 0;
00038     int height = 0;
00039     float bumpheight = 1.0f;
00040     int method = 0;
00041 
00042     if (sscanf(argv[1], " %d", &width) != 1 ||
00043         sscanf(argv[2], " %d", &height) != 1)
00044     {
00045         cerr << "Bad image dimensions.\n";
00046         return 1;
00047     }
00048 
00049     if (sscanf(argv[3], " %f", &bumpheight) != 1)
00050     {
00051         cerr << "Invalid bump height.\n";
00052         return 1;
00053     }
00054 
00055     if (sscanf(argv[4], " %d", &method) != 1)
00056     {
00057         cerr << "Bad filter method.\n";
00058         return 1;
00059     }
00060 
00061     // Binary 8-bit grayscale header
00062     // cout << "P5\n";
00063     // Binary 8-bit/channel RGB header
00064     cout << "P6\n";
00065     cout << width << ' ' << height << '\n' << "255\n";
00066 
00067     float** samples = new float*[height];
00068 
00069     for (int i = 0; i < kernelSize - 1; i++)
00070         samples[i] = readRowS16(cin, width);
00071 
00072     for (int y = 0; y < height; y++)
00073     {
00074         // Out with the old . . .
00075         if (y > halfKernelSize)
00076             delete[] samples[y - halfKernelSize - 1];
00077 
00078         // . . . and in with the new.
00079         if (y < height - halfKernelSize)
00080             samples[y + halfKernelSize] = readRowS16(cin, width);
00081 
00082         for (int x = 0; x < width; x++)
00083         {
00084 #if 0
00085             unsigned char v = (unsigned char) (samples[y][x]  * 255.99f);
00086             cout.write((char*) &v, 1);
00087 #endif
00088             float dx;
00089             if (x == width - 1)
00090                 dx = samples[y][0] - samples[y][x];
00091             else
00092                 dx = samples[y][x + 1] - samples[y][x];
00093 
00094             float dy;
00095             if (y == height - 1)
00096                 dy = samples[y][x] - samples[y - 1][x];
00097             else
00098                 dy = samples[y + 1][x] - samples[y][x];
00099         }
00100     }
00101 
00102     return 0;
00103 }

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1