Changeset 3531
- Timestamp:
- 05/27/09 07:04:41 (4 years ago)
- File:
-
- 1 edited
-
libpipi/trunk/examples/img2twit.cpp (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libpipi/trunk/examples/img2twit.cpp
r3530 r3531 63 63 64 64 /* The maximum image size we want to support */ 65 #define MAX_W 400066 #define MAX_H 400065 #define RANGE_W 4000 66 #define RANGE_H 4000 67 67 68 68 /* How does the algorithm work: one point per cell, or two */ … … 443 443 } 444 444 445 static inline void index2cell(int index, int *dx, int *dy) 446 { 447 // This check will be used once we add points to our list 448 //if(index < dw * dh * POINTS_PER_CELL) 449 { 450 *dx = (index / POINTS_PER_CELL) % dw; 451 *dy = (index / POINTS_PER_CELL) / dw; 452 } 453 } 454 445 455 static inline void set_point(int index, float x, float y, float r, 446 456 float g, float b, float s) 447 457 { 448 int dx = (index / POINTS_PER_CELL) % dw; 449 int dy = (index / POINTS_PER_CELL) / dw; 458 int dx, dy; 459 460 index2cell(index, &dx, &dy); 450 461 451 462 float fx = (x - dx * RANGE_X) / RANGE_X; … … 469 480 { 470 481 uint32_t pt = points[index]; 471 472 unsigned int dx = (index / POINTS_PER_CELL) % dw; 473 unsigned int dy = (index / POINTS_PER_CELL) / dw;482 int dx, dy; 483 484 index2cell(index, &dx, &dy); 474 485 475 486 *s = int2fullrange(pt % RANGE_S, RANGE_S); pt /= RANGE_S; … … 937 948 } 938 949 950 bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because 951 * MAX_MSG_LEN wouldn't be defined. */ 952 939 953 /* Autodetect charset if decoding, otherwise switch to CJK. */ 940 954 if(dstname) … … 973 987 /* Precompute bit allocation */ 974 988 NUM_CHARACTERS = count_unichars(); 989 990 if(dstname) 991 { 992 /* Decoding mode: find each character's index in our character 993 * list, and push it to our wonderful custom bitstream. */ 994 for(int i = MAX_MSG_LEN; i--; ) 995 b.push(uni2index(unicode_data[i]), NUM_CHARACTERS); 996 997 /* Read width and height from bitstream */ 998 src = NULL; 999 width = b.pop(RANGE_W) + 1; 1000 height = b.pop(RANGE_H) + 1; 1001 } 1002 else 1003 { 1004 /* Argument given: open image for encoding */ 1005 src = pipi_load(srcname); 1006 1007 if(!src) 1008 { 1009 fprintf(stderr, "Error loading %s\n", srcname); 1010 return EXIT_FAILURE; 1011 } 1012 1013 width = pipi_get_image_width(src); 1014 height = pipi_get_image_height(src); 1015 } 1016 1017 if(width <= 0 || height <= 0 || width > RANGE_W || height > RANGE_H) 1018 { 1019 fprintf(stderr, "Error: image size %ix%i is out of bounds\n", 1020 width, height); 1021 return EXIT_FAILURE; 1022 } 1023 975 1024 TOTAL_BITS = MAX_MSG_LEN * logf(NUM_CHARACTERS) / logf(2); 976 HEADER_BITS = logf( MAX_W * MAX_H) / logf(2);1025 HEADER_BITS = logf(RANGE_W * RANGE_H) / logf(2); 977 1026 DATA_BITS = TOTAL_BITS - HEADER_BITS; 978 1027 #if POINTS_PER_CELL == 1 … … 988 1037 MAX_ITERATIONS = ITERATIONS_PER_POINT * POINTS_PER_CELL * TOTAL_CELLS; 989 1038 990 bitstack b(MAX_MSG_LEN); /* We cannot declare this before, because991 * MAX_MSG_LEN wouldn't be defined. */992 993 if(dstname)994 {995 /* Decoding mode: find each character's index in our character996 * list, and push it to our wonderful custom bitstream. */997 for(int i = MAX_MSG_LEN; i--; )998 b.push(uni2index(unicode_data[i]), NUM_CHARACTERS);999 1000 /* Read width and height from bitstream */1001 src = NULL;1002 width = b.pop(MAX_W);1003 height = b.pop(MAX_H);1004 }1005 else1006 {1007 /* Argument given: open image for encoding */1008 src = pipi_load(srcname);1009 1010 if(!src)1011 {1012 fprintf(stderr, "Error loading %s\n", srcname);1013 return EXIT_FAILURE;1014 }1015 1016 width = pipi_get_image_width(src);1017 height = pipi_get_image_height(src);1018 }1019 1020 1039 /* Compute "best" w/h ratio */ 1021 1040 dw = 1; dh = TOTAL_CELLS; … … 1043 1062 fprintf(stderr, "Available characters: %i\n", NUM_CHARACTERS); 1044 1063 fprintf(stderr, "Available bits: %f\n", TOTAL_BITS); 1045 fprintf(stderr, " Maximum image resolution: %ix%i\n", MAX_W, MAX_H);1064 fprintf(stderr, "Width/Height ranges: %ix%i\n", RANGE_W, RANGE_H); 1046 1065 fprintf(stderr, "Image resolution: %ix%i\n", width, height); 1047 1066 fprintf(stderr, "Header bits: %f\n", HEADER_BITS); 1048 1067 fprintf(stderr, "Bits available for data: %f\n", DATA_BITS); 1068 fprintf(stderr, "X/Y/Red/Green/Blue/Extra ranges: %i %i %i %i %i %i\n", 1069 RANGE_X, RANGE_Y, RANGE_R, RANGE_G, RANGE_B, RANGE_S); 1049 1070 fprintf(stderr, "Cell bits: %f\n", CELL_BITS); 1050 1071 fprintf(stderr, "Available cells: %i\n", TOTAL_CELLS); … … 1211 1232 for(int i = 0; i < npoints; i++) 1212 1233 b.push(points[i], RANGE_SYXRGB); 1213 b.push(height , MAX_H);1214 b.push(width , MAX_W);1234 b.push(height - 1, RANGE_H); 1235 b.push(width - 1, RANGE_W); 1215 1236 1216 1237 /* Pop Unicode characters from the bitstream and print them */
Note: See TracChangeset
for help on using the changeset viewer.
