| 1 | |
| 2 | #include <iostream> |
| 3 | #include <fstream> |
| 4 | #include <sstream> |
| 5 | #include <climits> |
| 6 | #include <string> |
| 7 | |
| 8 | #include "texture.hxx" |
| 9 | |
| 10 | #define NCOLS 4320 |
| 11 | #define NROWS 2160 |
| 12 | |
| 13 | enum STR2INT_ERROR { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE }; |
| 14 | |
| 15 | STR2INT_ERROR str2int (int &i, char const *s, int base = 0) |
| 16 | { |
| 17 | char *end; |
| 18 | long l; |
| 19 | errno = 0; |
| 20 | l = strtol(s, &end, base); |
| 21 | if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) { |
| 22 | std::cerr << "OVERFLOW" << std::endl; |
| 23 | return OVERFLOW; |
| 24 | } |
| 25 | if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) { |
| 26 | std::cerr << "UNDERFLOW" << std::endl; |
| 27 | return UNDERFLOW; |
| 28 | } |
| 29 | if (*s == '\0' || end == s) { |
| 30 | std::cerr << "INCONVERTIBLE: " << s << std::endl; |
| 31 | return INCONVERTIBLE; |
| 32 | } |
| 33 | i = l; |
| 34 | return SUCCESS; |
| 35 | } |
| 36 | |
| 37 | int main(int argc, char **argv) |
| 38 | { |
| 39 | if (argc < 3) { |
| 40 | printf("Uage: %s <infile>.asc <outfile>.rgb\n\n", argv[0]); |
| 41 | exit(-1); |
| 42 | } |
| 43 | |
| 44 | const char *infile = argv[1]; |
| 45 | const char *outfile = argv[2]; |
| 46 | |
| 47 | SGTexture texture(NCOLS, NROWS); |
| 48 | std::ifstream file(infile); |
| 49 | std::string str; |
| 50 | |
| 51 | texture.prepare(NCOLS, NROWS); |
| 52 | texture.set_colors(1); |
| 53 | |
| 54 | GLuint row = 0; |
| 55 | unsigned int line = 0; |
| 56 | while (std::getline(file, str)) |
| 57 | { |
| 58 | GLuint col = 0; |
| 59 | int num = 0; |
| 60 | |
| 61 | if (++line < 7) continue; |
| 62 | |
| 63 | std::istringstream iss(str); |
| 64 | std::string token; |
| 65 | while (std::getline(iss, token, ' ')) |
| 66 | { |
| 67 | if (str2int(num, token.c_str(), 10) == SUCCESS) |
| 68 | { |
| 69 | GLubyte val = (num == 32) ? 0 : 4*num; |
| 70 | texture.set_pixel(col++, NROWS-row, &val); |
| 71 | } |
| 72 | } |
| 73 | row++; |
| 74 | } |
| 75 | |
| 76 | texture.write_texture(outfile); |
| 77 | } |