#include #include #include #include #include #define MAX(a,b) (a > b ? a : b) #define MIN(a,b) (a < b ? a : b) #define SCILABx #if 0 #define X_ORG 0 #define X_DIR 1 #define Y_ORG 0 #define Y_DIR 1 #define USE_SPECIAL int rotate=0; float scale = 0; #endif // karney tek Plots #if 1 #define X_ORG 0 #define X_DIR -1 #define Y_ORGx -3072 #define Y_ORG 0 #define Y_DIR 1 #define USE_SPECIAL 1 #define USE_REGIONS #define PLOT_REGIONS int rotate=1; float scale = 0; #endif // title Plots #if 0 #define X_ORG 0 #define X_DIR -1 #define Y_ORGx -3072 #define Y_ORG 0 #define Y_DIR 1 #define USE_SPECIAL 2 int rotate=1; float scale = 2.8; #endif struct { int x,y; } coords[50000]; int num_coords = 0; int last_coords = 0; struct { int x,y; int dir; int length; int num_coords; int first_coord, last_coord; int closest; int special; int valid; } segs[10000]; int num_segs = 0; int orig_length = 0; int orig_up = 0; int new_length = 0; int new_up = 0; unsigned char strs[1000][200]; int num_strs = 0; int last_x = 0, last_y = 0; void write_coord(int x, int y, int force) { x = x * X_DIR + X_ORG; y = y * Y_DIR + Y_ORG; //fprintf(stderr,"x,y %d,%d\n", x,y); if (force || last_x != x || last_y != y) { coords[num_coords].x = x; coords[num_coords].y = y; num_coords++; orig_length += MAX(abs(x - last_x), abs(y - last_y)); } // printf("%d,%d\r\n",x - last_x,y - last_y); // else // fprintf(stderr,"Dup deleted\n"); last_x = x; last_y = y; } void add_segs(int str) { int i; int length = 0; if (str) { segs[num_segs].first_coord = num_strs-1; segs[num_segs].last_coord = num_strs-1; segs[num_segs].num_coords = 1; segs[num_segs].x = last_x; segs[num_segs].y = last_y; segs[num_segs].length = 0; segs[num_segs].dir = 1; segs[num_segs].valid = str; num_segs++; segs[num_segs].first_coord = num_strs-1; segs[num_segs].last_coord = num_strs-1; segs[num_segs].num_coords = 1; segs[num_segs].x = last_x; segs[num_segs].y = last_y; segs[num_segs].length = 0; segs[num_segs].dir = -1; segs[num_segs].valid = str; num_segs++; } else { for (i = last_coords + 1; i < num_coords; i++) { length += MAX(abs(coords[i].x - coords[i-1].x), abs(coords[i].y - coords[i-1].y)); } segs[num_segs].first_coord = last_coords; segs[num_segs].last_coord = num_coords - 1; segs[num_segs].num_coords = num_coords - last_coords; segs[num_segs].x = coords[last_coords].x; segs[num_segs].y = coords[last_coords].y; segs[num_segs].length = length; segs[num_segs].dir = 1; segs[num_segs].valid = 1; num_segs++; segs[num_segs].last_coord = last_coords; segs[num_segs].first_coord = num_coords - 1; segs[num_segs].num_coords = num_coords - last_coords; segs[num_segs].x = coords[num_coords-1].x; segs[num_segs].y = coords[num_coords-1].y; segs[num_segs].length = length; segs[num_segs].dir = -1; segs[num_segs].valid = 1; num_segs++; last_coords = num_coords; } } void simplify_graphic() { static int x = 0; static int y = 0; int state = 0; int first = 1; int didnt_send = 0; int c,last; int extra_x = 0, extra_y = 0; while ((c = getchar()) != EOF) { //fprintf(stderr,"c %d-%x x,y %d,%d extra %d,%d\n", c,c, x,y, extra_x,extra_y); // Ignore ESC as start of seq. Not sure this is correct if (state == 0 && c == 27) { fprintf(stderr,"ESC in graphics ignored\n"); c = getchar(); continue; } if (c == 29) { /* putchar_pad(c);*/ //printf("8094,0\r\n"); // Pen up orig_up++; state = 0; first = 1; if (last_coords != num_coords) { add_segs(0); } continue; } if (c == 31) { /* putchar_pad(c);*/ break; } if ((c & 0x60) == 0x20) { if (state == 0) { extra_x = 0; extra_y = 0; y = (y & 0x1f) | ((c & 0x1f) << 5); state = 1; } else { x = (x & 0x1f) | ((c & 0x1f) << 5); } } if ((c & 0x60) == 0x60) { if ((last & 0x60) == 0x60) { extra_x = last & 0x3; extra_y = (last >> 2) & 0x3; } y = (y & 0xfe0) | ((c & 0x1f)); state = 1; } if ((c & 0x60) == 0x40) { state = 0; x = (x & 0xfe0) | ((c & 0x1f)); write_coord((x << 2) | extra_x,(y << 2) | extra_y,first); first = 0; } last = c; } if (last_coords != num_coords) { add_segs(0); } } unsigned char str[512]; int strl = 0; void dump_str(int type) { if (strl != 0) { // printf("%d,0\r\n",type); str[strl] = 0; // printf("%d\r\n%s\r\n",strl, str); strcpy(strs[num_strs], str); num_strs++; add_segs(type); } strl = 0; } void print_coord(int dx, int dy) { if (rotate) { int t = dx; dx = dy; dy = t; } if (scale != 0) { dx = dx * scale; dy = dy * scale; } #ifndef SCILAB if (dx == 0) printf(",%d\r\n", dy); else if (dy == 0) printf("%d\r\n", dx); else #endif printf("%d,%d\r\n", dx, dy); } #if USE_SPECIAL == 1 int special_list[][2] = { {42,82}, //{235,234} {211,235}, {224,208}, {218,363}, {218,321}, {208,363}, {274,318}, {274,362}, {274,395}, {216,255}, {215,251}, {213,243}, {230,229}, {1616,-889}, {336,-330}, }; #endif #if USE_SPECIAL == 2 int special_list[][2] = { {47,-51} }; #endif #define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0])) int find_closest(int last_x, int last_y, int dont_match) { int min_len, len; int min_seg; int i,j; int min_valid, orig_min_valid; int dx,dy; int ignore; min_valid = -1; for (i = 0; i < num_segs && min_valid == -1; i++) { if (segs[i].valid) min_valid = i; } orig_min_valid = min_valid; #ifdef PLOT_REGIONS if (min_valid <= 159*2) min_valid = 159*2; else if (min_valid <= 875) min_valid = 875; else #endif min_valid = 999999; #ifdef USE_SPECIAL for (j = 0; j < ARRAYSIZE(special_list); j++) { if (dont_match == special_list[j][0] && special_list[j][1] < 0) { fprintf(stderr,"special match %d\n",dont_match); return -special_list[j][1]; } } #endif min_len = 999999999; min_seg = -1; for (i = 0; i < num_segs; i++) { #if 0 len = MAX(abs(segs[i].x - last_x), abs(segs[i].y - last_y)); #else dx = abs(segs[i].x - last_x); dy = abs(segs[i].y - last_y); len = dx*dx + dy*dy; if (dont_match == 636 && (i == 525 || i == 872 || i == 873)) fprintf(stderr,"%d %d len %d lx,ly %d,%d x,y %d,%d\n", dont_match, i, len, last_x,last_y,segs[i].x,segs[i].y); #endif //len = len + abs(i - dont_match) * 250; //len = len + abs(i - min_valid) * 50; //if (len < min_len && segs[i].valid && (i & ~1) != (dont_match & ~1) && ignore = 0; #ifdef USE_SPECIAL for (j = 0; j < ARRAYSIZE(special_list) && !ignore; j++) { if (dont_match/2 == special_list[j][0] && i/2 == special_list[j][1]) ignore = 1; } #endif if (dont_match == 873 && i == 525) fprintf(stderr,"%d len %d min %d valid %d min %d ignore %d\n",i, len, min_len, segs[i].valid, min_valid, ignore); if (len < min_len && segs[i].valid && i <= min_valid && !ignore) { min_len = len; min_seg = i; } } if (dont_match == 546) fprintf(stderr,"Picked %d\n",min_seg); return min_seg; } void dump_segs() { int last_x, last_y; int i,j; last_y = 0; last_x = 0; int more = 1; int min_len, len; int min_seg; int dx,dy; int last_special = -1; int last_seg = -1; #if 0 for (j = 0; j < num_segs; j++) { min_seg = find_closest(segs[j].x, segs[j].y, j); if (min_seg == -1) break; segs[j].closest = min_seg; segs[j].special = -1; } #endif i = -2; while (more) { #if 0 i += 2; if (!segs[i].valid) break; #else for (i = 0; i < -num_segs; i += 2) { if (segs[i].valid && !segs[segs[i].closest].valid && !segs[segs[i+1].closest].valid) { min_seg = find_closest(segs[i].x, segs[i].y, i); if (min_seg != -1) segs[min_seg].special = i; } } min_seg = find_closest(last_x, last_y, last_seg); if (min_seg == -1) break; #if 0 last_special = -1; if (last_special != -1 && segs[last_special].valid) { i = last_special; } else if (segs[min_seg].special != -1 && segs[segs[min_seg].special].valid) { //printf("special %d %d %d\n",min_seg, segs[min_seg].special, // segs[segs[min_seg].special].valid); i = segs[min_seg].special; } else #endif i = min_seg; #endif if (segs[i].valid < 0) { if (segs[i].x != last_x || segs[i].y != last_y) { printf("8094\r\n"); // Pen up new_up++; dx = segs[i].x - last_x; dy = segs[i].y - last_y; new_length += MAX(abs(dx),abs(dy)); print_coord(dx, dy); } printf("%d\r\n", segs[i].valid); printf("%d\r\n",strlen(strs[segs[i].first_coord])); printf("%s\r\n",strs[segs[i].first_coord]); last_x = segs[i].x; last_y = segs[i].y; } else { if (segs[i].x != last_x || segs[i].y != last_y) { #ifdef SCILAB printf("8094,%d\r\n",i); // Pen up #else printf("8094\r\n"); // Pen up #endif new_up++; #if 0 dx = segs[i].x - last_x; dy = segs[i].y - last_y; new_length += MAX(abs(dx),abs(dy)); print_coord(dx, dy), last_x = segs[i].x; last_y = segs[i].y; #endif } else { #ifdef SCILAB printf("8096,%d\r\n",i); // for scilab #endif } //printf("%d %d:%d %d %d,%d\n",i, segs[i].first_coord, segs[i].last_coord, segs[i].dir, segs[i].x,segs[i].y); //printf("%d,%d %d,%d\n", segs[i].x,segs[i].y,segs[i+segs[i].dir].x,segs[i+segs[i].dir].y); for (j = segs[i].first_coord; segs[i].dir > 0 ? j <= segs[i].last_coord : j >= segs[i].last_coord; j += segs[i].dir) { dx = coords[j].x - last_x; dy = coords[j].y - last_y; new_length += MAX(abs(dx),abs(dy)); if (dx != 0 || dy != 0) print_coord(dx, dy); last_x = coords[j].x; last_y = coords[j].y; } } segs[i].valid = 0; segs[i + segs[i].dir].valid = 0; last_special = segs[i + segs[i].dir].special; last_seg = i; } fprintf(stderr,"Orig length %d, pen ups %d\n",orig_length, orig_up); fprintf(stderr,"New length %d, pen ups %d\n",new_length, new_up); fprintf(stderr,"num segs %d\n",num_segs); printf("8095\r\n"); num_segs = 0; num_coords = 0; orig_length = 0; orig_up = 0; new_length = 0; new_up = 0; } int main(int argc, char *argv[]) { int found_graph = 0; int c; int esc_active = 0; while ((c = getchar()) != EOF) { if (c == 29) { /*putchar(c); */ found_graph = 1; dump_str(-8095); orig_up++; //printf("8094,0\r\n"); simplify_graphic(); esc_active = 0; } else if (found_graph) /* putchar(c); */ if (c == '\n') { dump_str(-8094); } else { unsigned char cu = toupper(c); if (cu == 27) esc_active = 1; // Should handle font size else { if (cu >= 32 && cu < 128 && !esc_active) str[strl++] = cu; if (cu == 12) { dump_segs(); printf("Clear\r\n"); fprintf(stderr,"Clear found\n"); } esc_active = 0; } } } dump_segs(); return 0; }