Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <fstream>
00036
00037 #include "common/wave_ex.h"
00038 #include "perf/perf.h"
00039 #include "pgmppm/pgmppm.h"
00040 #include "util/parsing.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 struct context_t {
00055 context_t(void) throw() { this->clear(); }
00056 void clear(void) throw() {
00057 width = 0;
00058 height = 0;
00059 array = NULL;
00060 }
00061
00062
00063 int width;
00064 int height;
00065 byte_t * array;
00066 };
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 static int
00077 getPixel
00078 (
00079 IN void * context,
00080 IN int x,
00081 IN int y
00082 )
00083 {
00084 context_t * ctx = (context_t *) context;
00085 ASSERT(ctx, "null");
00086 ASSERT(ctx->array, "null");
00087 ASSERT(x >= 0 && x < ctx->width, "bad x: %d", x);
00088 ASSERT(y >= 0 && y < ctx->height, "bad y: %d", y);
00089
00090 int idx = (y * ctx->width) + x;
00091 return ctx->array[idx];
00092 }
00093
00094
00095
00096 static void
00097 writePgm
00098 (
00099 IO std::istream& stream,
00100 IN const char * filename
00101 )
00102 {
00103 ASSERT(stream.good(), "bad?");
00104 ASSERT(filename, "null");
00105
00106
00107 eParseBehavior parseFlags = eParse_Strip;
00108
00109
00110 std::string line = getNextLineFromStream(stream, parseFlags);
00111 std::string token;
00112
00113
00114 dictionary_t dict;
00115 getDictionaryFromString(line.c_str(), "first line of PGM input", dict);
00116 int width = atoi(getRequiredValue(dict, "width"));
00117 int height = atoi(getRequiredValue(dict, "height"));
00118 if (width < 2 || height < 2) {
00119 WAVE_EX(wex);
00120 wex << "Bad width or height: " << width << " x " << height;
00121 }
00122
00123
00124 context_t ctx;
00125 ctx.width = width;
00126 ctx.height = height;
00127 ctx.array = new byte_t[width * height];
00128 ASSERT(ctx.array, "out of memory");
00129
00130
00131 int max_gray = 255;
00132 byte_t * p = ctx.array;
00133 int expectX = 0;
00134 int expectY = 0;
00135 while (!stream.eof()) {
00136 line = getNextLineFromStream(stream, parseFlags);
00137
00138 getDictionaryFromString(line.c_str(), "PGM data line", dict);
00139 int x = atoi(getRequiredValue(dict, "x"));
00140 int y = atoi(getRequiredValue(dict, "y"));
00141 int gray = atoi(getRequiredValue(dict, "g"));
00142
00143 if (x != expectX || y != expectY) {
00144 WAVE_EX(wex);
00145 wex << "unexpected or out of order x,y values?\n";
00146 wex << " expected: (" << expectX << ", " << expectY << ")\n";
00147 wex << " read: (" << x << ", " << y << ")";
00148 }
00149 if (gray < 0 || gray > max_gray) {
00150 WAVE_EX(wex);
00151 wex << "invalid gray value: " << gray;
00152 }
00153
00154 *p = (byte_t) gray;
00155 ++p;
00156
00157 ++expectX;
00158 if (expectX >= width) {
00159 expectX = 0;
00160 ++expectY;
00161 }
00162
00163 if (expectY >= height) {
00164 break;
00165 }
00166 }
00167
00168
00169 std::ofstream outfile(filename);
00170 if (!outfile.good()) {
00171 WAVE_EX(wex);
00172 wex << "Failed to open file for writing: " << filename;
00173 }
00174 pgmppm::writePgm(outfile, width, height, max_gray, getPixel,
00175 (void *) &ctx);
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 int
00187 main
00188 (
00189 IN int argc,
00190 IN const char * argv[]
00191 )
00192 {
00193 ASSERT(2 == argc, "usage: writePgm <pgm-filename>");
00194 const char * filename = argv[1];
00195
00196 int retval = 0;
00197
00198 try {
00199 perf::Timer timer("overall timer");
00200 writePgm(std::cin, filename);
00201
00202 } catch (std::exception& e) {
00203 DPRINTF("Exception: %s", e.what());
00204 retval = 1;
00205 }
00206
00207
00208 perf::dumpTimingSummary(std::cerr);
00209 return retval;
00210 }
00211