1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
|
/*
* Copyright (c) 2004 Valeriy Onuchin <Valeri dot Onoutchine at cern dot ch>
* Copyright (c) 2000-2004 Sasha Vasko <sasha at aftercode.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#undef LOCAL_DEBUG
#undef DO_CLOCKING
#ifndef NO_DEBUG_OUTPUT
#undef DEBUG_RECTS
#undef DEBUG_RECTS2
#endif
#ifdef _WIN32
#include "win32/config.h"
#else
#include "config.h"
#endif
#define USE_64BIT_FPU
#include <string.h>
#ifdef DO_CLOCKING
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef _WIN32
# include "win32/afterbase.h"
#else
# include "afterbase.h"
#endif
#include "asvisual.h"
#include "scanline.h"
#include "blender.h"
#include "asimage.h"
#include "ascmap.h"
static ASVisual __as_dummy_asvisual = {0};
static ASVisual *__as_default_asvisual = &__as_dummy_asvisual ;
/* these are internal library things - user should not mess with it ! */
ASVisual *_set_default_asvisual( ASVisual *new_v )
{
ASVisual *old_v = __as_default_asvisual ;
__as_default_asvisual = new_v?new_v:&__as_dummy_asvisual ;
/* This should be done in application - we should not meddle with other lib's stuff!
#if HAVE_AFTERBASE_FLAG
if (new_v && new_v->dpy && !get_current_X_display (new_v->dpy))
set_current_X_display (new_v->dpy);
#endif
*/
return old_v;
}
ASVisual *get_default_asvisual()
{
return __as_default_asvisual?__as_default_asvisual:&__as_dummy_asvisual;
}
/* internal buffer used for compression/decompression */
static CARD8 *__as_compression_buffer = NULL ;
static size_t __as_compression_buffer_len = 0; /* allocated size */
static inline CARD8* get_compression_buffer( size_t size )
{
if( size > __as_compression_buffer_len )
__as_compression_buffer_len = (size+1023)&(~0x03FF) ;
return (__as_compression_buffer = realloc( __as_compression_buffer, __as_compression_buffer_len ));
}
static inline void release_compression_buffer( CARD8 *ptr )
{
/* do nothing so far */
}
#ifdef TRACK_ASIMAGES
static ASHashTable *__as_image_registry = NULL ;
#endif
#ifdef HAVE_MMX
Bool asimage_use_mmx = True;
#else
Bool asimage_use_mmx = False;
#endif
/* ********************* ASImage ************************************/
void
asimage_init (ASImage * im, Bool free_resources)
{
if (im != NULL)
{
if (free_resources)
{
register int i ;
for( i = im->height*4-1 ; i>= 0 ; --i )
if( im->red[i] != 0 )
forget_data( NULL, im->red[i] );
if( im->red )
free(im->red);
#ifndef X_DISPLAY_MISSING
if( im->alt.ximage )
XDestroyImage( im->alt.ximage );
if( im->alt.mask_ximage )
XDestroyImage( im->alt.mask_ximage );
#endif
if( im->alt.argb32 )
free( im->alt.argb32 );
if( im->alt.vector )
free( im->alt.vector );
if( im->name )
free( im->name );
}
memset (im, 0x00, sizeof (ASImage));
im->magic = MAGIC_ASIMAGE ;
im->back_color = ARGB32_DEFAULT_BACK_COLOR ;
}
}
void
flush_asimage_cache( ASImage *im )
{
#ifndef X_DISPLAY_MISSING
if( im->alt.ximage )
{
XDestroyImage( im->alt.ximage );
im->alt.ximage = NULL ;
}
if( im->alt.mask_ximage )
{
XDestroyImage( im->alt.mask_ximage );
im->alt.mask_ximage = NULL ;
}
#endif
}
static void
alloc_asimage_channels ( ASImage *im )
{
/* we want result to be 32bit aligned and padded */
im->red = safecalloc (1, sizeof (ASStorageID) * im->height * 4);
LOCAL_DEBUG_OUT( "allocated %p for channels of the image %p", im->red, im );
if( im->red == NULL )
{
show_error( "Insufficient memory to create image %dx%d!", im->width, im->height );
return ;
}
im->green = im->red+im->height;
im->blue = im->red+(im->height*2);
im->alpha = im->red+(im->height*3);
im->channels[IC_RED] = im->red ;
im->channels[IC_GREEN] = im->green ;
im->channels[IC_BLUE] = im->blue ;
im->channels[IC_ALPHA] = im->alpha ;
}
void
asimage_start (ASImage * im, unsigned int width, unsigned int height, unsigned int compression)
{
if (im)
{
asimage_init (im, True);
im->height = height;
im->width = width;
alloc_asimage_channels( im );
if( compression == 0 )
set_flags( im->flags, ASIM_NO_COMPRESSION );
}
}
Bool
asimage_replace (ASImage *im, ASImage *from)
{
if ( im && from && im != from )
if( im->magic == MAGIC_ASIMAGE && from->magic == MAGIC_ASIMAGE && from->imageman == NULL )
{
int ref_count = im->ref_count ;
ASImageManager *imageman = im->imageman ;
char *name = im->name ;
ASFlagType saved_flags = im->flags & (ASIM_NAME_IS_FILENAME|ASIM_NO_COMPRESSION) ;
im->name = NULL ;
asimage_init (im, True);
memcpy( im, from, sizeof(ASImage) );
/* Assume : from->name == NULL as from->imageman == NULL (see above ) */
memset( from, 0x00, sizeof(ASImage) );
im->ref_count = ref_count ;
im->imageman = imageman ;
im->name = name ;
set_flags( im->flags, saved_flags );
return True ;
}
return False;
}
static ASImage*
check_created_asimage( ASImage *im, unsigned int width, unsigned int height )
{
if( im->width == 0 || im->height == 0 )
{
free( im );
im = NULL ;
#ifdef TRACK_ASIMAGES
show_error( "failed to create ASImage of size %dx%d", width, height );
#endif
}else
{
#ifdef TRACK_ASIMAGES
show_progress( "created ASImage %p of size %dx%d (%s compressed )", im,
width, height, (get_flags(im->flags, ASIM_NO_COMPRESSION)?" no":"") );
if( __as_image_registry == NULL )
__as_image_registry = create_ashash( 0, pointer_hash_value, NULL, NULL );
add_hash_item( __as_image_registry, AS_HASHABLE(im), im );
#endif
}
return im ;
}
ASImage *
create_asimage( unsigned int width, unsigned int height, unsigned int compression)
{
ASImage *im = safecalloc( 1, sizeof(ASImage) );
asimage_start( im, width, height, compression );
return check_created_asimage( im, width, height );
}
void
destroy_asimage( ASImage **im )
{
if( im )
{
if( *im && !AS_ASSERT_NOTVAL((*im)->imageman,NULL))
{
#ifdef TRACK_ASIMAGES
show_progress( "destroying ASImage %p of size %dx%d", *im, (*im)->width, (*im)->height );
remove_hash_item( __as_image_registry, AS_HASHABLE(*im), NULL, False );
#endif
asimage_init( *im, True );
(*im)->magic = 0;
free( *im );
*im = NULL ;
}else if( *im )
{
show_error( "Failed to destroy ASImage %p:", *im );
print_asimage_func (AS_HASHABLE(*im));
}
}
}
void print_asimage_func (ASHashableValue value)
{
ASImage *im = (ASImage*)value ;
if( im && im->magic == MAGIC_ASIMAGE )
{
unsigned int k;
unsigned int red_mem = 0, green_mem = 0, blue_mem = 0, alpha_mem = 0;
unsigned int red_count = 0, green_count = 0, blue_count = 0, alpha_count = 0;
ASStorageSlot slot ;
fprintf( stderr,"\n\tASImage[%p].size = %dx%d;\n", im, im->width, im->height );
fprintf( stderr,"\tASImage[%p].back_color = 0x%lX;\n", im, (long)im->back_color );
fprintf( stderr,"\t\tASImage[%p].alt.ximage = %p;\n", im, im->alt.ximage );
if( im->alt.ximage )
{
fprintf( stderr,"\t\t\tASImage[%p].alt.ximage.bytes_per_line = %d;\n", im, im->alt.ximage->bytes_per_line);
fprintf( stderr,"\t\t\tASImage[%p].alt.ximage.size = %dx%d;\n", im, im->alt.ximage->width, im->alt.ximage->height);
}
fprintf( stderr,"\t\tASImage[%p].alt.mask_ximage = %p;\n", im, im->alt.mask_ximage);
if( im->alt.mask_ximage )
{
fprintf( stderr,"\t\t\tASImage[%p].alt.mask_ximage.bytes_per_line = %d;\n", im, im->alt.mask_ximage->bytes_per_line);
fprintf( stderr,"\t\t\tASImage[%p].alt.mask_ximage.size = %dx%d;\n", im, im->alt.mask_ximage->width, im->alt.mask_ximage->height);
}
fprintf( stderr,"\t\tASImage[%p].alt.argb32 = %p;\n", im, im->alt.argb32 );
fprintf( stderr,"\t\tASImage[%p].alt.vector = %p;\n", im, im->alt.vector );
fprintf( stderr,"\tASImage[%p].imageman = %p;\n", im, im->imageman );
fprintf( stderr,"\tASImage[%p].ref_count = %d;\n", im, im->ref_count );
fprintf( stderr,"\tASImage[%p].name = \"%s\";\n", im, im->name );
fprintf( stderr,"\tASImage[%p].flags = 0x%lX;\n", im, im->flags );
for( k = 0 ; k < im->height ; k++ )
{
if( im->red[k] )
if( query_storage_slot(NULL, im->red[k], &slot ) )
{
++red_count;
red_mem += slot.size ;
}
if( im->green[k] )
if( query_storage_slot(NULL, im->green[k], &slot ) )
{
++green_count;
green_mem += slot.size ;
}
if( im->blue[k] )
if( query_storage_slot(NULL, im->blue[k], &slot ) )
{
++blue_count;
blue_mem += slot.size ;
}
if( im->alpha[k] )
if( query_storage_slot(NULL, im->alpha[k], &slot ) )
{
++alpha_count;
alpha_mem += slot.size ;
}
}
fprintf( stderr,"\tASImage[%p].uncompressed_size = %d;\n", im, im->width*red_count +
im->width*green_count +
im->width*blue_count +
im->width*alpha_count );
fprintf( stderr,"\tASImage[%p].compressed_size = %d;\n", im, red_mem + green_mem +blue_mem + alpha_mem );
fprintf( stderr,"\t\tASImage[%p].channel[red].lines_count = %d;\n", im, red_count );
fprintf( stderr,"\t\tASImage[%p].channel[red].memory_used = %d;\n", im, red_mem );
fprintf( stderr,"\t\tASImage[%p].channel[green].lines_count = %d;\n", im, green_count );
fprintf( stderr,"\t\tASImage[%p].channel[green].memory_used = %d;\n", im, green_mem );
fprintf( stderr,"\t\tASImage[%p].channel[blue].lines_count = %d;\n", im, blue_count );
fprintf( stderr,"\t\tASImage[%p].channel[blue].memory_used = %d;\n", im, blue_mem );
fprintf( stderr,"\t\tASImage[%p].channel[alpha].lines_count = %d;\n", im, alpha_count );
fprintf( stderr,"\t\tASImage[%p].channel[alpha].memory_used = %d;\n", im, alpha_mem );
}
}
void
print_asimage_registry()
{
#ifdef TRACK_ASIMAGES
print_ashash( __as_image_registry, print_asimage_func );
#endif
}
void
purge_asimage_registry()
{
#ifdef TRACK_ASIMAGES
if( __as_image_registry )
destroy_ashash( &__as_image_registry );
#endif
}
/* ******************** ASImageManager ****************************/
static void
asimage_destroy (ASHashableValue value, void *data)
{
if( data )
{
ASImage *im = (ASImage*)data ;
if( im != NULL )
{
if( AS_ASSERT_NOTVAL(im->magic, MAGIC_ASIMAGE) )
im = NULL ;
else
im->imageman = NULL ;
}
if( im == NULL || (char*)value != im->name )
free( (char*)value );/* name */
destroy_asimage( &im );
}
}
ASImageManager *create_image_manager( struct ASImageManager *reusable_memory, double gamma, ... )
{
ASImageManager *imman = reusable_memory ;
int i ;
va_list ap;
if( imman == NULL )
imman = safecalloc( 1, sizeof(ASImageManager));
else
memset( imman, 0x00, sizeof(ASImageManager));
va_start (ap, gamma);
for( i = 0 ; i < MAX_SEARCH_PATHS ; i++ )
{
char *path = va_arg(ap,char*);
if( path == NULL )
break;
imman->search_path[i] = mystrdup( path );
}
va_end (ap);
imman->search_path[MAX_SEARCH_PATHS] = NULL ;
imman->gamma = gamma ;
imman->image_hash = create_ashash( 7, string_hash_value, string_compare, asimage_destroy );
return imman;
}
void
destroy_image_manager( struct ASImageManager *imman, Bool reusable )
{
if( imman )
{
int i = MAX_SEARCH_PATHS;
destroy_ashash( &(imman->image_hash) );
while( --i >= 0 )
if(imman->search_path[i])
free( imman->search_path[i] );
if( !reusable )
free( imman );
else
memset( imman, 0x00, sizeof(ASImageManager));
}
}
Bool
store_asimage( ASImageManager* imageman, ASImage *im, const char *name )
{
Bool res = False ;
if( !AS_ASSERT(im) )
if( AS_ASSERT_NOTVAL(im->magic, MAGIC_ASIMAGE) )
im = NULL ;
if( !AS_ASSERT(imageman) && !AS_ASSERT(im) && !AS_ASSERT((char*)name) )
{
if( im->imageman == NULL )
{
int hash_res ;
char *stored_name = mystrdup( name );
if( im->name )
free( im->name );
im->name = stored_name ;
hash_res = add_hash_item( imageman->image_hash, AS_HASHABLE(im->name), im);
res = ( hash_res == ASH_Success);
if( !res )
{
free( im->name );
im->name = NULL ;
}else
{
im->imageman = imageman ;
im->ref_count = 1 ;
}
}
}
return res ;
}
inline ASImage *
query_asimage( ASImageManager* imageman, const char *name )
{
ASImage *im = NULL ;
if( !AS_ASSERT(imageman) && !AS_ASSERT(name) )
{
ASHashData hdata = {0} ;
if( get_hash_item( imageman->image_hash, AS_HASHABLE((char*)name), &hdata.vptr) == ASH_Success )
{
im = hdata.vptr ;
if( im->magic != MAGIC_ASIMAGE )
im = NULL ;
}
}
return im;
}
ASImage *
fetch_asimage( ASImageManager* imageman, const char *name )
{
ASImage *im = query_asimage( imageman, name );
if( im )
{
im->ref_count++ ;
}
return im;
}
ASImage *
dup_asimage( ASImage* im )
{
if( !AS_ASSERT(im) )
if( AS_ASSERT_NOTVAL(im->magic,MAGIC_ASIMAGE) )
{
im = NULL ;
show_error( "ASImage %p has invalid magic number - discarding!", im );
}
if( !AS_ASSERT(im) && !AS_ASSERT(im->imageman) )
{
/* fprintf( stderr, __FUNCTION__" on image %p ref_count = %d\n", im, im->ref_count ); */
im->ref_count++ ;
return im;
}else if( im )
{
show_debug( __FILE__, "dup_asimage", __LINE__, "Attempt to duplicate ASImage %p that is not tracked by any image manager!", im );
}
return NULL ;
}
inline int
release_asimage( ASImage *im )
{
int res = -1 ;
if( !AS_ASSERT(im) )
{
if( im->magic == MAGIC_ASIMAGE )
{
if( --(im->ref_count) <= 0 )
{
ASImageManager *imman = im->imageman ;
if( !AS_ASSERT(imman) )
if( remove_hash_item(imman->image_hash, (ASHashableValue)(char*)im->name, NULL, True) != ASH_Success )
destroy_asimage( &im );
}else
res = im->ref_count ;
}
}
return res ;
}
void
forget_asimage( ASImage *im )
{
if( !AS_ASSERT(im) )
{
if( im->magic == MAGIC_ASIMAGE )
{
ASImageManager *imman = im->imageman ;
if( !AS_ASSERT(imman) )
remove_hash_item(imman->image_hash, (ASHashableValue)(char*)im->name, NULL, False);
im->ref_count = 0;
im->imageman = NULL;
}
}
}
void
relocate_asimage( ASImageManager* to_imageman, ASImage *im )
{
if( !AS_ASSERT(im) )
{
if( im->magic == MAGIC_ASIMAGE )
{
ASImageManager *imman = im->imageman ;
int ref_count = im->ref_count ;
if( imman != NULL )
{
remove_hash_item(imman->image_hash, (ASHashableValue)(char*)im->name, NULL, False);
im->ref_count = 0;
im->imageman = NULL;
}
if( to_imageman != NULL )
{
if( add_hash_item( to_imageman->image_hash, AS_HASHABLE(im->name), im) == ASH_Success )
{
im->ref_count = ref_count < 1 ? 1: ref_count;
im->imageman = to_imageman ;
}
}
}
}
}
void
forget_asimage_name( ASImageManager *imman, const char *name )
{
if( !AS_ASSERT(imman) && name != NULL )
{
remove_hash_item(imman->image_hash, AS_HASHABLE((char*)name), NULL, False);
}
}
inline int
safe_asimage_destroy( ASImage *im )
{
int res = -1 ;
if( !AS_ASSERT(im) )
{
if( im->magic == MAGIC_ASIMAGE )
{
ASImageManager *imman = im->imageman ;
if( imman != NULL )
{
res = --(im->ref_count) ;
if( im->ref_count <= 0 )
remove_hash_item(imman->image_hash, (ASHashableValue)(char*)im->name, NULL, True);
}else
{
destroy_asimage( &im );
res = -1 ;
}
}
}
return res ;
}
int
release_asimage_by_name( ASImageManager *imageman, char *name )
{
int res = -1 ;
ASImage *im = NULL ;
if( !AS_ASSERT(imageman) && !AS_ASSERT(name) )
{
ASHashData hdata ;
if( get_hash_item( imageman->image_hash, AS_HASHABLE((char*)name), &hdata.vptr) == ASH_Success )
{
im = hdata.vptr ;
res = release_asimage( im );
}
}
return res ;
}
void
print_asimage_manager(ASImageManager *imageman)
{
#ifdef TRACK_ASIMAGES
print_ashash( imageman->image_hash, string_print );
#endif
}
/* ******************** ASGradient ****************************/
void
destroy_asgradient( ASGradient **pgrad )
{
if( pgrad && *pgrad )
{
if( (*pgrad)->color )
{
free( (*pgrad)->color );
(*pgrad)->color = NULL ;
}
if( (*pgrad)->offset )
{
free( (*pgrad)->offset );
(*pgrad)->offset = NULL ;
}
(*pgrad)->npoints = 0 ;
free( *pgrad );
*pgrad = NULL ;
}
}
ASGradient *
flip_gradient( ASGradient *orig, int flip )
{
ASGradient *grad ;
int npoints ;
int type ;
Bool inverse_points = False ;
flip &= FLIP_MASK ;
if( orig == NULL || flip == 0 )
return orig;
grad = safecalloc( 1, sizeof(ASGradient));
grad->npoints = npoints = orig->npoints ;
type = orig->type ;
grad->color = safemalloc( npoints*sizeof(ARGB32) );
grad->offset = safemalloc( npoints*sizeof(double) );
if( get_flags(flip, FLIP_VERTICAL) )
{
Bool upsidedown = get_flags(flip, FLIP_UPSIDEDOWN) ;
switch(type)
{
case GRADIENT_Left2Right :
type = GRADIENT_Top2Bottom ; inverse_points = !upsidedown ;
break;
case GRADIENT_TopLeft2BottomRight :
type = GRADIENT_BottomLeft2TopRight ; inverse_points = upsidedown ;
break;
case GRADIENT_Top2Bottom :
type = GRADIENT_Left2Right ; inverse_points = upsidedown ;
break;
case GRADIENT_BottomLeft2TopRight :
type = GRADIENT_TopLeft2BottomRight ; inverse_points = !upsidedown ;
break;
}
}else if( flip == FLIP_UPSIDEDOWN )
{
inverse_points = True ;
}
grad->type = type ;
if( inverse_points )
{
register int i = 0, k = npoints;
while( --k >= 0 )
{
grad->color[i] = orig->color[k] ;
grad->offset[i] = 1.0 - orig->offset[k] ;
++i ;
}
}else
{
register int i = npoints ;
while( --i >= 0 )
{
grad->color[i] = orig->color[i] ;
grad->offset[i] = orig->offset[i] ;
}
}
return grad;
}
/* ******************** ASImageLayer ****************************/
void
init_image_layers( register ASImageLayer *l, int count )
{
memset( l, 0x00, sizeof(ASImageLayer)*count );
while( --count >= 0 )
{
l[count].merge_scanlines = alphablend_scanlines ;
/* l[count].solid_color = ARGB32_DEFAULT_BACK_COLOR ; */
}
}
ASImageLayer *
create_image_layers( int count )
{
ASImageLayer *l = NULL;
if( count > 0 )
{
l = safecalloc( count, sizeof(ASImageLayer) );
init_image_layers( l, count );
}
return l;
}
void
destroy_image_layers( register ASImageLayer *l, int count, Bool reusable )
{
if( l )
{
register int i = count;
while( --i >= 0 )
{
if( l[i].im )
{
if( l[i].im->imageman )
release_asimage( l[i].im );
else
destroy_asimage( &(l[i].im) );
}
if( l[i].bevel )
free( l[i].bevel );
}
if( !reusable )
free( l );
else
memset( l, 0x00, sizeof(ASImageLayer)*count );
}
}
/* **********************************************************************/
/* Compression/decompression */
/* **********************************************************************/
size_t
asimage_add_line_mono (ASImage * im, ColorPart color, CARD8 value, unsigned int y)
{
if (AS_ASSERT(im) || color <0 || color >= IC_NUM_CHANNELS )
return 0;
if (y >= im->height)
return 0;
if( im->channels[color][y] )
forget_data( NULL, im->channels[color][y] );
im->channels[color][y] = store_data( NULL, &value, 1, 0, 0);
return im->width;
}
size_t
asimage_add_line (ASImage * im, ColorPart color, register CARD32 * data, unsigned int y)
{
if (AS_ASSERT(im) || color <0 || color >= IC_NUM_CHANNELS )
return 0;
if (y >= im->height)
return 0;
if( im->channels[color][y] )
forget_data( NULL, im->channels[color][y] );
im->channels[color][y] = store_data( NULL, (CARD8*)data, im->width*4, ASStorage_RLEDiffCompress|ASStorage_32Bit, 0);
return im->width;
}
size_t
asimage_add_line_bgra (ASImage * im, register CARD32 * data, unsigned int y)
{
if (AS_ASSERT(im) )
return 0;
if (y >= im->height)
return 0;
if( im->channels[IC_ALPHA][y] )
forget_data( NULL, im->channels[IC_ALPHA][y] );
im->channels[IC_ALPHA][y] = store_data( NULL, (CARD8*)data, im->width*4,
ASStorage_24BitShift|ASStorage_Masked|
ASStorage_RLEDiffCompress|ASStorage_32Bit, 0);
if( im->channels[IC_RED][y] )
forget_data( NULL, im->channels[IC_RED][y] );
im->channels[IC_RED][y] = store_data( NULL, (CARD8*)data, im->width*4,
ASStorage_16BitShift|ASStorage_Masked|
ASStorage_RLEDiffCompress|ASStorage_32Bit, 0);
if( im->channels[IC_GREEN][y] )
forget_data( NULL, im->channels[IC_GREEN][y] );
im->channels[IC_GREEN][y] = store_data( NULL, (CARD8*)data, im->width*4,
ASStorage_8BitShift|ASStorage_Masked|
ASStorage_RLEDiffCompress|ASStorage_32Bit, 0);
if( im->channels[IC_BLUE][y] )
forget_data( NULL, im->channels[IC_BLUE][y] );
im->channels[IC_BLUE][y] = store_data( NULL, (CARD8*)data, im->width*4,
ASStorage_Masked|
ASStorage_RLEDiffCompress|ASStorage_32Bit, 0);
return im->width;
}
unsigned int
asimage_print_line (ASImage * im, ColorPart color, unsigned int y, unsigned long verbosity)
{
if (AS_ASSERT(im) || color < 0 || color >= IC_NUM_CHANNELS )
return 0;
if (y >= im->height)
return 0;
return print_storage_slot(NULL, im->channels[color][y]);
}
void print_asimage( ASImage *im, int flags, char * func, int line )
{
if( im )
{
register unsigned int k ;
int total_mem = 0 ;
fprintf( stderr, "%s:%d> printing ASImage %p.\n", func, line, im);
for( k = 0 ; k < im->height ; k++ )
{
fprintf( stderr, "%s:%d> ******* %d *******\n", func, line, k );
total_mem+=asimage_print_line( im, IC_RED , k, flags );
total_mem+=asimage_print_line( im, IC_GREEN, k, flags );
total_mem+=asimage_print_line( im, IC_BLUE , k, flags );
total_mem+=asimage_print_line( im, IC_ALPHA , k, flags );
}
fprintf( stderr, "%s:%d> Total memory : %u - image size %dx%d ratio %d%%\n", func, line, total_mem, im->width, im->height, (total_mem*100)/(im->width*im->height*3) );
}else
fprintf( stderr, "%s:%d> Attempted to print NULL ASImage.\n", func, line);
}
void print_component( register CARD32 *data, int nonsense, int len );
int
asimage_decode_line (ASImage * im, ColorPart color, CARD32 * to_buf, unsigned int y, unsigned int skip, unsigned int out_width)
{
ASStorageID id = im->channels[color][y];
register int i = 0;
/* that thing below is supposedly highly optimized : */
LOCAL_DEBUG_CALLER_OUT( "im->width = %d, color = %d, y = %d, skip = %d, out_width = %d", im->width, color, y, skip, out_width );
if( id )
{
i = fetch_data32( NULL, id, to_buf, skip, out_width, 0, NULL);
LOCAL_DEBUG_OUT( "decoded %d pixels", i );
#if defined(LOCAL_DEBUG) && !defined(NO_DEBUG_OUTPUT)
{
int z = -1 ;
while( ++z < i )
fprintf( stderr, "%lX ", (unsigned long)to_buf[z] );
fprintf( stderr, "\n");
}
#endif
return i;
}
return 0;
}
void
move_asimage_channel( ASImage *dst, int channel_dst, ASImage *src, int channel_src )
{
if( !AS_ASSERT(dst) && !AS_ASSERT(src) && channel_src >= 0 && channel_src < IC_NUM_CHANNELS &&
channel_dst >= 0 && channel_dst < IC_NUM_CHANNELS )
{
register int i = MIN(dst->height, src->height);
register ASStorageID *dst_rows = dst->channels[channel_dst] ;
register ASStorageID *src_rows = src->channels[channel_src] ;
while( --i >= 0 )
{
if( dst_rows[i] )
forget_data( NULL, dst_rows[i] );
dst_rows[i] = src_rows[i] ;
src_rows[i] = 0 ;
}
}
}
void
copy_asimage_channel( ASImage *dst, int channel_dst, ASImage *src, int channel_src )
{
if( !AS_ASSERT(dst) && !AS_ASSERT(src) && channel_src >= 0 && channel_src < IC_NUM_CHANNELS &&
channel_dst >= 0 && channel_dst < IC_NUM_CHANNELS )
{
register int i = MIN(dst->height, src->height);
register ASStorageID *dst_rows = dst->channels[channel_dst] ;
register ASStorageID *src_rows = src->channels[channel_src] ;
LOCAL_DEBUG_OUT( "src = %p, dst = %p, dst->width = %d, src->width = %d", src, dst, dst->width, src->width );
while( --i >= 0 )
{
if( dst_rows[i] )
forget_data( NULL, dst_rows[i] );
dst_rows[i] = dup_data( NULL, src_rows[i] );
}
}
}
void
copy_asimage_lines( ASImage *dst, unsigned int offset_dst,
ASImage *src, unsigned int offset_src,
unsigned int nlines, ASFlagType filter )
{
if( !AS_ASSERT(dst) && !AS_ASSERT(src) &&
offset_src < src->height && offset_dst < dst->height )
{
int chan;
if( offset_src+nlines > src->height )
nlines = src->height - offset_src ;
if( offset_dst+nlines > dst->height )
nlines = dst->height - offset_dst ;
for( chan = 0 ; chan < IC_NUM_CHANNELS ; ++chan )
if( get_flags( filter, 0x01<<chan ) )
{
register int i = -1;
register ASStorageID *dst_rows = &(dst->channels[chan][offset_dst]) ;
register ASStorageID *src_rows = &(src->channels[chan][offset_src]) ;
LOCAL_DEBUG_OUT( "copying %d lines of channel %d...", nlines, chan );
while( ++i < (int)nlines )
{
if( dst_rows[i] )
forget_data( NULL, dst_rows[i] );
dst_rows[i] = dup_data( NULL, src_rows[i] );
}
}
#if 0
for( i = 0 ; i < nlines ; ++i )
{
asimage_print_line( src, IC_ALPHA, i, (i==4)?VRB_EVERYTHING:VRB_LINE_SUMMARY );
asimage_print_line( dst, IC_ALPHA, i, (i==4)?VRB_EVERYTHING:VRB_LINE_SUMMARY );
}
#endif
}
}
Bool
asimage_compare_line (ASImage *im, ColorPart color, CARD32 *to_buf, CARD32 *tmp, unsigned int y, Bool verbose)
{
register unsigned int i;
asimage_decode_line( im, color, tmp, y, 0, im->width );
for( i = 0 ; i < im->width ; i++ )
if( tmp[i] != to_buf[i] )
{
if( verbose )
show_error( "line %d, component %d differ at offset %d ( 0x%lX(compresed) != 0x%lX(orig) )\n", y, color, i, (unsigned long)tmp[i], (unsigned long)to_buf[i] );
return False ;
}
return True;
}
ASFlagType
get_asimage_chanmask( ASImage *im)
{
ASFlagType mask = 0 ;
int color ;
if( !AS_ASSERT(im) )
for( color = 0; color < IC_NUM_CHANNELS ; color++ )
{
register ASStorageID *chan = im->channels[color];
register int y, height = im->height ;
for( y = 0 ; y < height ; y++ )
if( chan[y] )
{
set_flags( mask, 0x01<<color );
break;
}
}
return mask ;
}
int
check_asimage_alpha (ASVisual *asv, ASImage *im )
{
int recomended_depth = 0 ;
unsigned int i;
ASScanline buf;
if( asv == NULL )
asv = get_default_asvisual();
if (im == NULL)
return 0;
prepare_scanline( im->width, 0, &buf, asv->BGR_mode );
buf.flags = SCL_DO_ALPHA ;
for (i = 0; i < im->height; i++)
{
int count = asimage_decode_line (im, IC_ALPHA, buf.alpha, i, 0, buf.width);
if( count < (int)buf.width )
{
if( ARGB32_ALPHA8(im->back_color) == 0 )
{
if( recomended_depth == 0 )
recomended_depth = 1 ;
}else if( ARGB32_ALPHA8(im->back_color) != 0xFF )
{
recomended_depth = 8 ;
break ;
}
}
while( --count >= 0 )
if( buf.alpha[count] == 0 )
{
if( recomended_depth == 0 )
recomended_depth = 1 ;
}else if( (buf.alpha[count]&0xFF) != 0xFF )
{
recomended_depth = 8 ;
break ;
}
if( recomended_depth == 8 )
break;
}
free_scanline(&buf, True);
return recomended_depth;
}
static Bool
check_line_empty (ASImage *im, CARD32 *buf, int line, int *left_return, int *right_return, int threshold)
{
int count = asimage_decode_line (im, IC_ALPHA, buf, line, 0, im->width);
*left_return = 0 ;
*right_return = im->width-1;
int i;
if (count < (int)im->width)
return (ARGB32_ALPHA8(im->back_color) <= threshold);
for (i = 0 ; i < im->width ; ++i)
if ((int)buf[i] <= threshold) (*left_return)++;
else break;
for (i = (int)im->width-1 ; i > *left_return ; --i)
if ((int)buf[i] <= threshold) (*right_return)--;
else break;
return (*left_return >= (int)im->width-1);
}
void
get_asimage_closure (ASImage *im, int *left_return, int *top_return, int *right_return, int *bottom_return, int threshold)
{
int left = 0, top = 0;
int right = 0, bottom = 0;
int left_tmp, right_tmp;
if (im != NULL) {
unsigned int i;
ASScanline buf;
bottom = im->height-1;
right = im->width-1;
prepare_scanline (im->width, 0, &buf, 0);
buf.flags = SCL_DO_ALPHA ;
/* top */
for (; top < im->height; ++top)
if (!check_line_empty (im, buf.alpha, top, &left_tmp, &right_tmp, threshold))
break;
/* bottom */
for (; bottom > top; --bottom)
if (!check_line_empty (im, buf.alpha, bottom, &left_tmp, &right_tmp, threshold))
break;
/* sides */
if (bottom < top && (left_return != NULL || right_return != NULL)) {
left = im->width-1;
right = 0;
for (i = top ; i <= bottom ; ++i) {
check_line_empty (im, buf.alpha, bottom, &left_tmp, &right_tmp, threshold);
if (left_tmp < left) left = left_tmp;
if (right_tmp > right) right = right_tmp;
}
}
free_scanline(&buf, True);
}
if (left_return)
*left_return = left;
if (top_return)
*top_return = top;
if (right_return)
*right_return = right;
if (bottom_return)
*bottom_return = bottom;
}
/* ********************************************************************************/
/* Vector -> ASImage functions : */
/* ********************************************************************************/
Bool
set_asimage_vector( ASImage *im, register double *vector )
{
if( vector == NULL || im == NULL )
return False;
if( im->alt.vector == NULL )
im->alt.vector = safemalloc( im->width*im->height*sizeof(double));
{
register double *dst = im->alt.vector ;
register int i = im->width*im->height;
while( --i >= 0 )
dst[i] = vector[i] ;
}
return True;
}
ASVectorPalette*
vectorize_asimage( ASImage *im, unsigned int max_colors, unsigned int dither,
int opaque_threshold )
{
ASVectorPalette* pal ;
double *vec ;
ASColormap cmap;
unsigned int r, g, b, v;
unsigned int x, y, j ;
if( im->alt.vector == NULL )
im->alt.vector = safemalloc( im->width*im->height*sizeof(double));
vec = im->alt.vector ;
/* contributed by Valeriy Onuchin from Root project at cern.ch */
dither = dither > 7 ? 7 : dither;
{
int *res = colormap_asimage(im, &cmap, max_colors, dither, opaque_threshold);
for ( y = 0; y < im->height; y++)
{
for ( x = 0; x < im->width; x++)
{
int i = y*im->width + x;
g = INDEX_SHIFT_GREEN(cmap.entries[res[i]].green);
b = INDEX_SHIFT_BLUE(cmap.entries[res[i]].blue);
r = INDEX_SHIFT_RED(cmap.entries[res[i]].red);
v = MAKE_INDEXED_COLOR24(r,g,b);
v = (v>>12)&0x0FFF;
vec[(im->height - y - 1)*im->width + x] = ((double)v)/0x0FFF;
}
}
free (res);
}
pal = safecalloc( 1, sizeof(ASVectorPalette));
pal->npoints = cmap.count ;
pal->points = safemalloc( sizeof(double)*cmap.count);
pal->channels[IC_RED] = safemalloc( sizeof(CARD16)*cmap.count);
pal->channels[IC_GREEN] = safemalloc( sizeof(CARD16)*cmap.count);
pal->channels[IC_BLUE] = safemalloc( sizeof(CARD16)*cmap.count);
pal->channels[IC_ALPHA] = safemalloc( sizeof(CARD16)*cmap.count);
for ( j = 0; j < cmap.count; j++) {
g = INDEX_SHIFT_GREEN(cmap.entries[j].green);
b = INDEX_SHIFT_BLUE(cmap.entries[j].blue);
r = INDEX_SHIFT_RED(cmap.entries[j].red);
v = MAKE_INDEXED_COLOR24(r,g,b);
v = (v>>12)&0x0FFF;
pal->points[j] = ((double)v)/0x0FFF;
/* palette uses 16 bit color values for greater precision */
pal->channels[IC_RED][j] = cmap.entries[j].red<<QUANT_ERR_BITS;
pal->channels[IC_GREEN][j] = cmap.entries[j].green<<QUANT_ERR_BITS;
pal->channels[IC_BLUE][j] = cmap.entries[j].blue<<QUANT_ERR_BITS;
pal->channels[IC_ALPHA][j] = 0xFFFF;
}
destroy_colormap(&cmap, True);
return pal;
}
/* ********************************************************************************/
/* Convinience function - very fast image cloning : */
/* ********************************************************************************/
ASImage*
clone_asimage( ASImage *src, ASFlagType filter )
{
ASImage *dst = NULL ;
START_TIME(started);
if( !AS_ASSERT(src) )
{
int chan ;
dst = create_asimage(src->width, src->height, 100);
if( get_flags( src->flags, ASIM_DATA_NOT_USEFUL ) )
set_flags( dst->flags, ASIM_DATA_NOT_USEFUL );
dst->back_color = src->back_color ;
for( chan = 0 ; chan < IC_NUM_CHANNELS; chan++ )
if( get_flags( filter, 0x01<<chan) )
{
register int i = dst->height;
register ASStorageID *dst_rows = dst->channels[chan] ;
register ASStorageID *src_rows = src->channels[chan] ;
while( --i >= 0 )
dst_rows[i] = dup_data( NULL, src_rows[i] );
}
}
SHOW_TIME("", started);
return dst;
}
/* ********************************************************************************/
/* Convinience function
* - generate rectangles list for channel values exceeding threshold: */
/* ********************************************************************************/
XRectangle*
get_asimage_channel_rects( ASImage *src, int channel, unsigned int threshold, unsigned int *rects_count_ret )
{
XRectangle *rects = NULL ;
int rects_count = 0, rects_allocated = 0 ;
START_TIME(started);
if( !AS_ASSERT(src) && channel < IC_NUM_CHANNELS )
{
int i = src->height;
ASStorageID *src_rows = src->channels[channel] ;
unsigned int *height = safemalloc( (src->width+1)*2 * sizeof(unsigned int) );
unsigned int *prev_runs = NULL ;
int prev_runs_count = 0 ;
unsigned int *runs = safemalloc( (src->width+1)*2 * sizeof(unsigned int) );
unsigned int *tmp_runs = safemalloc( (src->width+1)*2 * sizeof(unsigned int) );
unsigned int *tmp_height = safemalloc( (src->width+1)*2 * sizeof(unsigned int) );
Bool count_empty = (ARGB32_CHAN8(src->back_color,channel)>= threshold);
#ifdef DEBUG_RECTS
fprintf( stderr, "%d:back_color = #%8.8lX, count_empty = %d, thershold = %d\n", __LINE__, src->back_color, count_empty, threshold );
#endif
while( --i >= -1 )
{
int runs_count = 0 ;
#ifdef DEBUG_RECTS
fprintf( stderr, "%d: LINE %d **********************\n", __LINE__, i );
#ifdef DEBUG_RECTS2
asimage_print_line (src, channel, i, 0xFFFFFFFF);
#else
asimage_print_line (src, channel, i, VRB_LINE_CONTENT);
#endif
#endif
if( i >= 0 )
{
if( src_rows[i] )
{
runs_count = threshold_stored_data(NULL, src_rows[i], runs, src->width, threshold);
}else if( count_empty )
{
runs_count = 2 ;
runs[0] = 0 ;
runs[1] = src->width ;
}
}
#ifdef DEBUG_RECTS
fprintf( stderr, "runs_count = %d\n", runs_count );
#endif
if( runs_count > 0 && (runs_count &0x0001) != 0 )
{ /* allways wants to have even number of runs */
runs[runs_count] = 0 ;
++runs_count ;
}
if( prev_runs_count > 0 )
{ /* here we need to merge runs and add all the detached rectangles to the rects list */
int k = 0, l = 0, last_k = 0 ;
int tmp_count = 0 ;
unsigned int *tmp ;
if( runs_count == 0 )
{
runs[0] = src->width ;
runs[1] = src->width ;
runs_count = 2 ;
}
tmp_runs[0] = 0 ;
tmp_runs[1] = src->width ;
/* two passes : in first pass we go through old runs and try and see if they are continued
* in this line. If not - we add them to the list of rectangles. At
* the same time we subtract them from new line's runs : */
for( l = 0 ; l < prev_runs_count ; ++l, ++l )
{
int start = prev_runs[l], end = prev_runs[l+1] ;
int matching_runs = 0 ;
#ifdef DEBUG_RECTS
fprintf( stderr, "%d: prev run %d : start = %d, end = %d, last_k = %d, height = %d\n", __LINE__, l, start, end, last_k, height[l] );
#endif
for( k = last_k ; k < runs_count ; ++k, ++k )
{
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: new run %d : start = %d, end = %d\n", __LINE__, k, runs[k], runs[k+1] );
#endif
if( (int)runs[k] > end )
{ /* add entire run to rectangles list */
if( rects_count >= rects_allocated )
{
rects_allocated = rects_count + 8 + (rects_count>>3);
rects = realloc( rects, rects_allocated*sizeof(XRectangle));
}
rects[rects_count].x = start ;
rects[rects_count].y = i+1 ;
rects[rects_count].width = (end-start)+1 ;
rects[rects_count].height = height[l] ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: added rectangle at y = %d\n", __LINE__, rects[rects_count].y );
#endif
++rects_count ;
++matching_runs;
break;
}else if( (int)runs[k+1] >= start )
{
if( start < (int)runs[k] )
{ /* add rectangle start, , runs[k]-start, height[l] */
if( rects_count >= rects_allocated )
{
rects_allocated = rects_count + 8 + (rects_count>>3);
rects = realloc( rects, rects_allocated*sizeof(XRectangle));
}
rects[rects_count].x = start ;
rects[rects_count].y = i+1 ;
rects[rects_count].width = runs[k]-start ;
rects[rects_count].height = height[l] ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: added rectangle at y = %d\n", __LINE__, rects[rects_count].y );
#endif
++rects_count ;
start = runs[k] ;
}else if( start > (int)runs[k] )
{
tmp_runs[tmp_count] = runs[k] ;
tmp_runs[tmp_count+1] = start-1 ;
tmp_height[tmp_count] = 1 ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: tmp_run %d added : %d ... %d, height = %d\n", __LINE__, tmp_count, runs[k], start-1, 1 );
#endif
++tmp_count ; ++tmp_count ;
runs[k] = start ;
}
/* at that point both runs start at the same point */
if( end < (int)runs[k+1] )
{
runs[k] = end+1 ;
}else
{
if( end > (int)runs[k+1] )
{
/* add rectangle runs[k+1]+1, , end - runs[k+1], height[l] */
if( rects_count >= rects_allocated )
{
rects_allocated = rects_count + 8 + (rects_count>>3);
rects = realloc( rects, rects_allocated*sizeof(XRectangle));
}
rects[rects_count].x = runs[k+1]+1 ;
rects[rects_count].y = i+1 ;
rects[rects_count].width = end - runs[k+1] ;
rects[rects_count].height = height[l] ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: added rectangle at y = %d\n", __LINE__, rects[rects_count].y );
#endif
++rects_count ;
end = runs[k+1] ;
}
/* eliminating new run - it was all used up :) */
runs[k] = src->width ;
runs[k+1] = src->width ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: eliminating new run %d\n", __LINE__, k );
#endif
++k ; ++k ;
}
tmp_runs[tmp_count] = start ;
tmp_runs[tmp_count+1] = end ;
tmp_height[tmp_count] = height[l]+1 ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: tmp_run %d added : %d ... %d, height = %d\n", __LINE__, tmp_count, start, end, height[l]+1 );
#endif
++tmp_count ; ++tmp_count ;
last_k = k ;
++matching_runs;
break;
}
}
if( matching_runs == 0 )
{ /* no new runs for this prev run - add rectangle */
#ifdef DEBUG_RECTS
fprintf( stderr, "%d: NO MATCHING NEW RUNS : start = %d, end = %d, height = %d\n", __LINE__, start, end, height[l] );
#endif
if( rects_count >= rects_allocated )
{
rects_allocated = rects_count + 8 + (rects_count>>3);
rects = realloc( rects, rects_allocated*sizeof(XRectangle));
}
rects[rects_count].x = start ;
rects[rects_count].y = i+1 ;
rects[rects_count].width = (end-start)+1 ;
rects[rects_count].height = height[l] ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: added rectangle at y = %d\n", __LINE__, rects[rects_count].y );
#endif
++rects_count ;
}
}
/* second pass: we need to pick up remaining new runs */
/* I think these should be inserted in oredrly manner so that we have runs list arranged in ascending order */
for( k = 0 ; k < runs_count ; ++k, ++k )
if( runs[k] < src->width )
{
int ii = tmp_count ;
while( ii > 0 && tmp_runs[ii-1] > runs[k] )
{
tmp_runs[ii] = tmp_runs[ii-2] ;
tmp_runs[ii+1] = tmp_runs[ii-1] ;
tmp_height[ii] = tmp_height[ii-2] ;
--ii ; --ii ;
}
tmp_runs[ii] = runs[k] ;
tmp_runs[ii+1] = runs[k+1] ;
tmp_height[ii] = 1 ;
#ifdef DEBUG_RECTS
fprintf( stderr, "*%d: tmp_run %d added : %d ... %d, height = %d\n", __LINE__, ii, runs[k], runs[k+1], 1 );
#endif
++tmp_count, ++tmp_count;
}
tmp = prev_runs ;
prev_runs = tmp_runs ;
tmp_runs = tmp ;
tmp = height ;
height = tmp_height ;
tmp_height = tmp ;
prev_runs_count = tmp_count ;
}else if( runs_count > 0 )
{
int k = runs_count;
prev_runs_count = runs_count ;
prev_runs = runs ;
runs = safemalloc( (src->width+1)*2 * sizeof(unsigned int) );
while( --k >= 0 )
height[k] = 1 ;
}
}
free( runs );
if( prev_runs )
free( prev_runs );
free( tmp_runs );
free( tmp_height );
free( height );
}
SHOW_TIME("", started);
if( rects_count_ret )
*rects_count_ret = rects_count ;
return rects;
}
/***********************************************************************************/
void
raw2scanline( register CARD8 *row, ASScanline *buf, CARD8 *gamma_table, unsigned int width, Bool grayscale, Bool do_alpha )
{
register int x = width;
if( grayscale )
row += do_alpha? width<<1 : width ;
else
row += width*(do_alpha?4:3) ;
if( gamma_table )
{
if( !grayscale )
{
while ( --x >= 0 )
{
row -= 3 ;
if( do_alpha )
{
--row;
buf->alpha[x] = row[3];
}
buf->xc1[x] = gamma_table[row[0]];
buf->xc2[x]= gamma_table[row[1]];
buf->xc3[x] = gamma_table[row[2]];
}
}else /* greyscale */
while ( --x >= 0 )
{
if( do_alpha )
buf->alpha[x] = *(--row);
buf->red [x] = gamma_table[*(--row)];
}
}else
{
if( !grayscale )
{
while ( --x >= 0 )
{
row -= 3 ;
if( do_alpha )
{
--row;
buf->alpha[x] = row[3];
}
buf->xc1[x] = row[0];
buf->xc2[x]= row[1];
buf->xc3[x] = row[2];
}
}else /* greyscale */
while ( --x >= 0 )
{
if( do_alpha )
buf->alpha[x] = *(--row);
buf->red [x] = *(--row);
}
}
}
/* ********************************************************************************/
/* The end !!!! */
/* ********************************************************************************/
|