Changeset 3527
- Timestamp:
- 05/27/09 07:04:17 (4 years ago)
- File:
-
- 1 edited
-
libpipi/trunk/examples/img2twit.cpp (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libpipi/trunk/examples/img2twit.cpp
r3526 r3527 245 245 { 246 246 public: 247 bitstack( ) { alloc(); init(0); }247 bitstack(int max) { alloc(max); init(0); } 248 248 249 249 ~bitstack() { delete[] digits; delete[] str; } … … 286 286 287 287 private: 288 bitstack( uint32_t i) { alloc(); init(i); }288 bitstack(int max, uint32_t x) { alloc(max); init(x); } 289 289 290 290 bitstack(bitstack &b) 291 291 { 292 alloc( );292 alloc(b.max_size); 293 293 msb = b.msb; 294 memcpy(digits, b.digits, ( MAX_MSG_LEN+ 1) * sizeof(uint32_t));294 memcpy(digits, b.digits, (max_size + 1) * sizeof(uint32_t)); 295 295 } 296 296 297 297 bitstack(bitstack const &b) 298 298 { 299 alloc( );299 alloc(b.max_size); 300 300 msb = b.msb; 301 memcpy(digits, b.digits, (MAX_MSG_LEN + 1) * sizeof(uint32_t)); 302 } 303 304 void alloc() 305 { 306 digits = new uint32_t[MAX_MSG_LEN + 1]; 307 str = new char[(MAX_MSG_LEN + 1) * 8 + 1]; 301 memcpy(digits, b.digits, (max_size + 1) * sizeof(uint32_t)); 302 } 303 304 void alloc(int max) 305 { 306 max_size = max; 307 digits = new uint32_t[max_size + 1]; 308 str = new char[(max_size + 1) * 8 + 1]; 308 309 } 309 310 … … 311 312 { 312 313 msb = 0; 313 memset(digits, 0, ( MAX_MSG_LEN+ 1) * sizeof(uint32_t));314 memset(digits, 0, (max_size + 1) * sizeof(uint32_t)); 314 315 digits[0] = i; 315 316 } 316 317 317 318 /* Could be done much faster, but we don't care! */ 318 void add(uint32_t x) { add(bitstack( x)); }319 void sub(uint32_t x) { sub(bitstack( x)); }319 void add(uint32_t x) { add(bitstack(max_size, x)); } 320 void sub(uint32_t x) { sub(bitstack(max_size, x)); } 320 321 321 322 void add(bitstack const &_b) … … 410 411 } 411 412 412 int msb ;413 int msb, max_size; 413 414 uint32_t *digits; 414 415 char *str; … … 795 796 int main(int argc, char *argv[]) 796 797 { 797 uint32_t unicode_data[ 4096]; /* FIXME: allocate this dynamically */798 uint32_t unicode_data[2048]; 798 799 int opstats[2 * NB_OPS]; 799 800 char const *srcname = NULL, *dstname = NULL; … … 899 900 /* Decoding mode: read UTF-8 text from stdin */ 900 901 if(dstname) 901 for(int i = 0; i < MAX_MSG_LEN; i++) 902 unicode_data[i] = fread_utf8(stdin); 903 904 /* Autodetect charset if decoding, otherwise switch to CJK. */ 905 if(!unichars) 906 { 907 if(dstname) 908 { 909 if(unicode_data[0] >= 0x0021 && unicode_data[0] < 0x007f) 910 unichars = unichars_ascii; 911 else if(unicode_data[0] >= 0x4e00 && unicode_data[0] < 0x9fa6) 912 unichars = unichars_cjk; 913 else if(unicode_data[0] >= 0x25a0 && unicode_data[0] < 0x27bf) 914 unichars = unichars_symbols; 915 else 902 for(MAX_MSG_LEN = 0; ;) 903 { 904 uint32_t ch = fread_utf8(stdin); 905 if(ch == 0xffffffff || ch == '\n') 906 break; 907 if(ch <= ' ') 908 continue; 909 unicode_data[MAX_MSG_LEN++] = ch; 910 911 if(MAX_MSG_LEN >= 2048) 916 912 { 917 fprintf(stderr, "Error: unable to detect charset\n");913 fprintf(stderr, "Error: message too long.\n"); 918 914 return EXIT_FAILURE; 919 915 } 920 916 } 917 918 if(MAX_MSG_LEN == 0) 919 { 920 fprintf(stderr, "Error: empty message.\n"); 921 return EXIT_FAILURE; 922 } 923 924 /* Autodetect charset if decoding, otherwise switch to CJK. */ 925 if(dstname) 926 { 927 char const *charset; 928 929 if(unicode_data[0] >= 0x0021 && unicode_data[0] < 0x007f) 930 { 931 unichars = unichars_ascii; 932 charset = "ascii"; 933 } 934 else if(unicode_data[0] >= 0x4e00 && unicode_data[0] < 0x9fa6) 935 { 936 unichars = unichars_cjk; 937 charset = "cjk"; 938 } 939 else if(unicode_data[0] >= 0x25a0 && unicode_data[0] < 0x27bf) 940 { 941 unichars = unichars_symbols; 942 charset = "symbols"; 943 } 921 944 else 922 unichars = unichars_cjk; 923 } 945 { 946 fprintf(stderr, "Error: unable to detect charset\n"); 947 return EXIT_FAILURE; 948 } 949 950 if(DEBUG_MODE) 951 fprintf(stderr, "Detected charset \"%s\"\n", charset); 952 } 953 else if(!unichars) 954 unichars = unichars_cjk; 924 955 925 956 pipi_set_gamma(1.0); … … 942 973 MAX_ITERATIONS = ITERATIONS_PER_POINT * POINTS_PER_CELL * TOTAL_CELLS; 943 974 944 bitstack b ; /* We cannot declare this before, because MAX_MSG_LEN945 *wouldn't be defined. */975 bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because 976 * MAX_MSG_LEN wouldn't be defined. */ 946 977 947 978 if(dstname) … … 994 1025 if(DEBUG_MODE) 995 1026 { 996 fprintf(stderr, "M aximum message size: %i\n", MAX_MSG_LEN);1027 fprintf(stderr, "Message size: %i\n", MAX_MSG_LEN); 997 1028 fprintf(stderr, "Available characters: %i\n", NUM_CHARACTERS); 998 1029 fprintf(stderr, "Available bits: %f\n", TOTAL_BITS);
Note: See TracChangeset
for help on using the changeset viewer.
