void image::init_clone(const image& other, int32_t scalex, int32_t scaley) { init_new(other.width * (scalex<0 ? -scalex : scalex), other.height * (scaley<0 ? -scaley : scaley), other.fmt); insert_scale_unsafe(0, 0, other, scalex, scaley); }
int main (int argc, char **argv) { int *table; int table_size = 4; // 0,1,2,3 are special int nbits = 9; // Always start with 512 item table. int max_bits = 0, prev_code = 0; int maxed = 0; int choose; choose = which(argv); table = calloc( (1 << nbits), sizeof(int)); init_table(table, nbits, &table_size); // Encode if ( choose == 1 ) { // Prove that ncode created the file. putBits(nbits, MAGIC1); putBits(nbits, MAGIC2); // Get max_bits int tmp; tmp = get_arg(argv, argc, 'm'); max_bits = get_m_arg(argv, tmp); if (max_bits < 0) { exit(0); } putBits(nbits, max_bits); // Get ratio int c, check_time; int code_bits = 0, char_bits = 0, count = 0; double ratio; tmp = get_arg(argv, argc, 'r'); check_time = get_r_arg(argv, tmp, &ratio); if (check_time < 0) { exit(0); } // Let's go while ((c = getchar()) != EOF) { char_bits += 8; // Item found, read next char if ( (tmp = item_exists(prev_code, c, table, nbits))) { prev_code = tmp; continue; } // item not found. Add item to table. Output prev_code. // new prev_code is c's index else { code_bits += nbits; count++; if (!maxed) { add_item(prev_code, c, table, nbits, &table_size); } putBits(nbits, prev_code); prev_code = item_exists(0, c, table, nbits); // Resize table if (table_size > (0.99 * TABLE_SIZE) && !maxed) { if (nbits != max_bits) { code_bits += (2 * nbits) + 1; putBits(nbits++, 1); putBits(nbits, c); table = init_new(table, TABLE_SIZE, table_size, nbits); } else { maxed = 1; putBits(nbits, 2); code_bits += nbits; } } // -r if (check_time && !(count % check_time) && (code_bits > ratio * char_bits)) { // re init putBits(nbits, 3); nbits = 9; free(table); table = calloc( (1 << nbits), sizeof(int)); init_table(table, nbits, &table_size); maxed = 0; code_bits = 0; char_bits = 0; } } } putBits(nbits, prev_code); putBits(nbits, 0); flushBits(); free(table); } // Decode else { int i, code, hold = 0, tmp_code, mx=0; len_array outstring; outstring.array = malloc(MAX_LEN * sizeof(int)); // Check that there are no args if (argc != 1) { fprintf(stderr, "Unexpected argument to decode\n"); exit(0); } // Check that this is actually a file we encoded if ( (getBits(nbits) != MAGIC1) || (getBits(nbits) != MAGIC2)) { fprintf(stderr, "Wrong file"); exit(0); } // Let's go max_bits = getBits(nbits); while ( (code = getBits(nbits)) ) { outstring.len = 0; // Table resize if (code == 1) { nbits++; // get the next letter, add and resize code = getBits(nbits); tmp_code = add_item(prev_code, code, table, nbits-1, &table_size); table = init_new(table, TABLE_SIZE, table_size, nbits); hold = 1; continue; } // Size is maxed, set maxed after next add else if (code == 2) { mx = 1; continue; } // Reinit table else if (code == 3) { nbits = 9; free(table); table = calloc( (1 << nbits), sizeof(int)); init_table(table, nbits, &table_size); prev_code = 0; maxed = 0; mx = 0; continue; } // Code in table else if ( (table[code] % 2)) { backtrace(table, code, &outstring); for (i=0; i<outstring.len; i++) { printf("%c", outstring.array[i]); } // If !prev_code, item has len 1 & will be there already // If maxed, table full // If hold, first add after resize - already there if (prev_code && !hold && !maxed) { add_item(prev_code, outstring.array[0], table, nbits, &table_size); } prev_code = code; } // Item not found in table. Item must be the result of // table[prev_code] + table[prev_code][0] // We also MUST be able to add it - no worries about max_bits else { backtrace(table, prev_code, &outstring); outstring.array[outstring.len++] = outstring.array[0]; for (i=0; i<outstring.len; i++) { printf("%c", outstring.array[i]); } // If it is not the first one after a resize if (!hold) { prev_code = add_item(prev_code, outstring.array[0], table, nbits, &table_size); } else { prev_code = tmp_code; } // Check that we the code we get from adding is the expected if (prev_code != code) { fprintf(stderr, "File corrupted\n"); exit(0); } } if (hold) { hold = 0; } if (mx) { mx = 0; maxed = 1; } } free(table); free(outstring.array); } return(0); }
void start_newgame (GtkMenuItem *, gpointer) { printf("NewGame"); init_new(NULL,NULL); }