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
|
/* image.c
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information (NCBI)
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government do not place any restriction on its use or reproduction.
* We would, however, appreciate having the NCBI and the author cited in
* any work or product based on this material
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* ===========================================================================
*
* File Name: image.c
*
* Author: Alex Smirnov, Denis Vakatov
*
* $Revision: 6.9 $
*
* File Description:
* Image(pixmap) processing.
*
* Version Creation Date: 04/03/95
*
* Modifications:
* --------------------------------------------------------------------------
*
* ==========================================================================
*/
#include <vibtypes.h>
#include <vibprocs.h>
#include <vibincld.h>
#include <math.h>
#ifdef WIN_MAC
# if !defined(OS_UNIX_DARWIN)
#include "MoreCarbonAccessors.h"
#endif
#endif
#ifndef _IMAGE_
#include <image.h>
#endif
#ifndef _PDIAGNOS_
#include <pdiagnos.h>
#endif
#include <gifgen.h>
/*****************************************************************************
*
* TYPEDEFS
*
*****************************************************************************/
typedef struct Nlm_pimage {
Nlm_Uint1 red[256];
Nlm_Uint1 green[256];
Nlm_Uint1 blue[256];
Nlm_Int4 imageVer;
Nlm_Int4 imageVerOld;
Nlm_Int4 imageColorVer;
Nlm_Int4 imageColorVerOld;
Nlm_Uint1Ptr curImagePtr;
Nlm_Handle image;
Nlm_WindoW curWin;
Nlm_Uint2 width;
Nlm_Uint2 height;
Nlm_Uint2 totalColors;
Nlm_Uint2 saveColors;
Nlm_Uint2 imageLocked;
#ifdef WIN_MSWIN
HBITMAP pixMap;
HDC hMemDC;
BITMAPINFO *bitInfo;
#endif
#ifdef WIN_MOTIF
Nlm_Uint1 altcol[256];
XImage *imageX11;
#endif
#ifdef WIN_MAC
PixMap *pixelMap;
#endif
} Nlm_PImage, PNTR Nlm_PImagePtr;
/*****************************************************************************
*
* GLOBAL VARIABLE
*
*****************************************************************************/
#ifdef WIN_MSWIN
extern HDC Nlm_currentHDC;
#endif
#ifdef WIN_MOTIF
extern Display *Nlm_currentXDisplay;
extern Window Nlm_currentXWindow;
extern GC Nlm_currentXGC;
#endif
#ifdef WIN_MAC
extern Nlm_Boolean Nlm_hasColorQD;
#endif
/*****************************************************************************
*
* STATIC VARIABLE
*
*****************************************************************************/
static Nlm_CharPtr imageClass = "Image";
/*****************************************************************************
*
* FUNCTIONS
*
*****************************************************************************/
#ifdef WIN_MSWIN
static Nlm_CharPtr GetWinErrStr( void )
{
static Nlm_Char winErrStr[128];
#ifdef BORLAND
sprintf(winErrStr, "Windows error %d", GetLastError());
#else
Nlm_StringCpy(winErrStr, "Windows error");
#endif
return winErrStr;
}
static void AllocateColorMap(Nlm_PImagePtr image)
{
register RGBQUAD *rgbq = image->bitInfo->bmiColors;
register int i;
int totalColors = (int)image->totalColors;
image->bitInfo->bmiHeader.biClrUsed = totalColors;
for (i = 0; i < totalColors; i++, rgbq++)
{
rgbq->rgbRed = image->red [i];
rgbq->rgbGreen = image->green[i];
rgbq->rgbBlue = image->blue [i];
rgbq->rgbReserved = 0;
}
}
#endif
Nlm_Image Nlm_CreateImage( void )
{
Nlm_Image im;
Nlm_DiagReset ();
im = (Nlm_Image)MemNew( sizeof(Nlm_PImage) );
if ( !im )
Nlm_DiagPutRecord(DA_ERROR, imageClass, "CreateImage",
"Can not allocate memory block");
return im;
}
Nlm_Boolean Nlm_AllocateImage ( Nlm_Image image, Nlm_Uint2Ptr width,
Nlm_Uint2 height, Nlm_Uint2 saveColors,
Nlm_Uint2 totalColors )
{
register Nlm_PImagePtr im;
Nlm_Int2 i;
#ifdef WIN_MSWIN
BITMAPINFOHEADER PNTR bmpHeader;
PALETTEENTRY PNTR lppe;
#endif
#ifdef WIN_MOTIF
XVisualInfo visinfo;
#endif
#ifdef WIN_MAC
CTabHandle cTable;
CTabPtr cTablePtr;
ColorSpecPtr cSpecPtr;
#endif
Nlm_DiagReset ();
im = (Nlm_PImagePtr)image;
im->imageVer++;
#ifdef WIN_MSWIN
*width /= sizeof(LONG);
*width *= sizeof(LONG);
#endif
if ((*width == 0) || (height == 0) || (totalColors < saveColors) ||
(totalColors > 256) ) {
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Invalid image or palette size" );
return FALSE;
}
if ( im->image != NULL ) {
if ( im->imageLocked ) HandUnlock ( im->image );
HandFree ( im->image );
im->imageLocked = 0;
}
im->image = HandNew(*width * height );
if ( im->image == NULL ) {
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Can not allocate memory block" );
return FALSE;
}
#ifdef WIN_MSWIN
if ( im->bitInfo == NULL ){
im->bitInfo = (BITMAPINFO*)MemNew ( sizeof(BITMAPINFOHEADER) +
256*sizeof(RGBQUAD) );
}
if ( im->bitInfo == NULL ) {
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Can not allocate memory block" );
return FALSE;
}
bmpHeader = &(im->bitInfo->bmiHeader);
bmpHeader->biWidth = *width;
bmpHeader->biHeight = height;
bmpHeader->biSize = sizeof(BITMAPINFOHEADER);
bmpHeader->biCompression = BI_RGB;
bmpHeader->biXPelsPerMeter = 2000;
bmpHeader->biYPelsPerMeter = 2000;
bmpHeader->biClrImportant = 0;
bmpHeader->biSizeImage = 0;
bmpHeader->biBitCount = 8;
bmpHeader->biPlanes = 1;
bmpHeader->biClrUsed = 0;
lppe = (PALETTEENTRY PNTR)MemNew ( saveColors * sizeof(PALETTEENTRY));
if ( lppe == NULL ) {
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Can not allocate memory block" );
}
GetSystemPaletteEntries ( Nlm_currentHDC, 0, saveColors, lppe );
for ( i=0; i<saveColors; i++ ) {
im->red [i] = lppe[i].peRed;
im->green[i] = lppe[i].peGreen;
im->blue [i] = lppe[i].peBlue;
}
MemFree ( lppe );
#endif
#ifdef WIN_MOTIF
#ifdef OS_UNIX_LINUX
if(!Nlm_CheckX(&visinfo))
#else /* OS_UNIX_LINUX */
if( !(XMatchVisualInfo(Nlm_currentXDisplay,
DefaultScreen(Nlm_currentXDisplay),
8,PseudoColor,&visinfo) ||
XMatchVisualInfo(Nlm_currentXDisplay,
DefaultScreen(Nlm_currentXDisplay),
8,GrayScale,&visinfo)) )
#endif /* else OS_UNIX_LINUX */
{
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Your X display cannot support 8-bit(256 colors) neither PseudoColor nor GrayScale visual" );
return FALSE;
}
im->curImagePtr = (Nlm_Uint1Ptr)HandLock ( im->image );
if ( im->curImagePtr == NULL ){
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Can not lock memory block" );
return FALSE;
}
HandUnlock ( im->image );
if ( im->imageX11 == NULL ){
im->imageX11 = XCreateImage(Nlm_currentXDisplay,
visinfo.visual, visinfo.depth, ZPixmap, 0,
(char*)im->curImagePtr,
*width, height, 8, 0);
}
if ( im->imageX11 == NULL ){
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Can not create X11 image" );
return FALSE;
}
#endif
#ifdef WIN_MAC
#ifndef WIN_MAC_QUARTZ
if ( im->pixelMap == NULL ) {
im->pixelMap = (PixMap*)MemNew(sizeof(PixMap));
}
if ( im->pixelMap == NULL ) {
Nlm_DiagPutRecord ( DA_ERROR, imageClass, "AllocateImage",
"Can not allocate memory block" );
return FALSE;
}
im->pixelMap->hRes = 72;
im->pixelMap->vRes = 72;
im->pixelMap->bounds.left = 0;
im->pixelMap->bounds.top = 0;
im->pixelMap->cmpSize = 8;
/* 2001-03-22: Joshua Juran */
/* Evidently these two members don't exist in Carbon. So don't set them. */
#if !TARGET_API_MAC_CARBON
im->pixelMap->planeBytes = 0;
im->pixelMap->pmReserved = 0;
#endif
im->pixelMap->pmVersion = 0;
im->pixelMap->packType = 0;
im->pixelMap->packSize = 0;
im->pixelMap->pixelSize = 8;
im->pixelMap->pixelType = 0;
im->pixelMap->cmpCount = 1;
im->pixelMap->rowBytes = *width | 0x8000;
im->pixelMap->bounds.right = *width;
im->pixelMap->bounds.bottom = height;
cTablePtr = NULL;
if ( Nlm_hasColorQD ) cTable = GetCTable ( 72 );
else cTable = GetCTable ( 40 );
if ( cTable != NULL ){
HLock ( (Nlm_Handle)cTable );
cTablePtr = (CTabPtr)(*((CTabPtr*)cTable));
}
if ( cTablePtr != NULL ){
cSpecPtr = &(cTablePtr->ctTable[0]);
for ( i=0; i<(Nlm_Int2)saveColors; i++ ) {
im->red[i] = (cSpecPtr->rgb.red >> 8);
im->green[i] = (cSpecPtr->rgb.green >> 8);
im->blue[i] = (cSpecPtr->rgb.blue >> 8);
cSpecPtr++;
}
HUnlock ((Nlm_Handle)cTable );
} else {
for ( i=0; i<(Nlm_Int2)saveColors; i++ ) {
if ( i & 0x1 ){
im->red[i] = im->green[i] = im->blue[i] = 0;
} else {
im->red[i] = im->green[i] = im->blue[i] = 0xFFFF;
}
}
}
if ( cTable != NULL ) DisposeCTable(cTable);
#endif
#endif
for ( i=saveColors; i<256; i++ )
im->red[i] = im->green[i] = im->blue[i] = 0;
im->totalColors = totalColors;
im->saveColors = saveColors;
im->width = *width;
im->height = height;
return TRUE;
}
Nlm_Image Nlm_LoadImageGIF (Nlm_CharPtr fileName)
{
#if defined(WIN_MOTIF) || defined(WIN_MSWIN) || defined(WIN_MAC)
Nlm_PImagePtr nlm_image = NULL;
gdImagePtr gd_image = NULL;
int convert[256];
Nlm_Uint2 width, height;
Nlm_Uint2 nColors, saveColors;
Nlm_DiagReset();
{{ /* Read "gd"-wise image from the GIF metafile */
FILE *inp_stream = FileOpen(fileName, "rb");
if ( !inp_stream ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LoadImageGIF",
"Can't open GIF file for reading");
return NULL;
}
gd_image = gdImageCreateFromGif( inp_stream );
if ( !gd_image ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LoadImageGIF",
"Error occurred while reading GIF file");
FileClose( inp_stream );
return NULL;
}
FileClose( inp_stream );
}}
{{ /* Count number of valid colors in the image */
int gd_color;
nColors = 0;
for (gd_color = 0; gd_color < gdImageColorsTotal(gd_image); gd_color++)
{
if ( gdGetImageColor(gd_image, gd_color, NULL, NULL, NULL) )
nColors++;
}
}}
{{ /* Create and allocate "Nlm-wise" image */
width = (Nlm_Uint2)gdImageSX( gd_image );
height = (Nlm_Uint2)gdImageSY( gd_image );
saveColors = (Nlm_Uint2)(256 - nColors);
if (saveColors > 32)
saveColors = 32;
nlm_image = (Nlm_PImagePtr)Nlm_CreateImage();
if ( !AllocateImage((Nlm_Image)nlm_image, &width, height, saveColors,
(Nlm_Uint2)(saveColors + nColors)) )
{
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LoadImageGIF",
"Cannot allocate image");
Nlm_DeleteImage( (Nlm_Image)nlm_image );
gdImageDestroy( gd_image );
return NULL;
}
}}
{{ /* Copy palette */
int color = saveColors;
int gd_color;
int red, green, blue;
for (gd_color = 0; gd_color < gdImageColorsTotal(gd_image); gd_color++)
{
#ifdef _DEBUG
convert[gd_color] = -1;
#endif
if ( gdGetImageColor(gd_image, gd_color, &red, &green, &blue) )
{
VERIFY ( Nlm_SetColorImage((Nlm_Image)nlm_image, (Nlm_Uint1)color,
(Nlm_Uint1)red, (Nlm_Uint1)green,
(Nlm_Uint1)blue) );
convert[gd_color] = color++;
}
}
ASSERT ( color == saveColors + nColors );
}}
{{ /* Copy gdImage to Nlm_Image */
Nlm_Uint2 y;
Nlm_Uint1Ptr pixmap = Nlm_LockPixMapImage( (Nlm_Image)nlm_image );
for (y = 0; y < nlm_image->height; y++)
{
#if defined(WIN_MOTIF) || defined(WIN_MAC)
Nlm_Uint4 y_base = nlm_image->width * y;
#elif defined(WIN_MSWIN)
Nlm_Uint4 y_base = nlm_image->width * (nlm_image->height - y - 1);
#endif
Nlm_Uint2 x;
for (x = 0; x < nlm_image->width; x++)
{
int color = gdImageGetPixel(gd_image, x, y);
ASSERT ( color < gdImageColorsTotal(gd_image) );
ASSERT ( convert[color] != -1 );
pixmap[y_base + x] = (Nlm_Uint1)convert[color];
}
}
Nlm_UnlockPixMapImage( (Nlm_Image)nlm_image );
}}
gdImageDestroy( gd_image );
return (Nlm_Image)nlm_image;
#else
Nlm_DiagReset();
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LoadImageGIF", "Not implemented");
return NULL;
#endif
}
Nlm_Image Nlm_LoadImageBMP(Nlm_CharPtr fileName)
{
Nlm_DiagReset();
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LoadImageBMP", "Not implemented");
return FALSE;
}
Nlm_Boolean Nlm_LoadImageClip(Nlm_Image image)
{
Nlm_DiagReset();
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LoadImageClip", "Not implemented");
return FALSE;
}
Nlm_Boolean Nlm_SaveImageGIF(Nlm_Image image, Nlm_CharPtr fileName)
{
#if !defined(_OPENGL) && (defined(WIN_MOTIF) || defined(WIN_MSWIN) || defined(WIN_MAC))
Nlm_PImagePtr nlm_image = (Nlm_PImagePtr)image;
gdImagePtr gd_image;
Nlm_Int2 convert[256];
Nlm_DiagReset();
if ( !nlm_image ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageGIF", "NULL image");
return FALSE;
}
gd_image = gdImageCreate(nlm_image->width, nlm_image->height);
{{ /* Set palette */
Nlm_Uint2 color;
for (color = nlm_image->saveColors; color < nlm_image->totalColors;
color++)
{
Nlm_Uint1 red=0, green=0, blue=0;
Nlm_GetColorImage(image, (Nlm_Uint1)color, &red, &green, &blue);
convert[color] = (Nlm_Int2)gdImageColorAllocate(gd_image,
red, green, blue);
ASSERT ( 0 <= convert[color] && convert[color] < 256 );
}
}}
{{ /* Copy Nlm_Image to gdImage */
Nlm_Uint2 y;
Nlm_Uint1Ptr pixmap = Nlm_LockPixMapImage( (Nlm_Image)nlm_image );
for (y = 0; y < nlm_image->height; y++)
{
#if defined(WIN_MOTIF) || defined(WIN_MAC)
Nlm_Uint4 y_base = nlm_image->width * y;
#elif defined(WIN_MSWIN)
Nlm_Uint4 y_base = nlm_image->width * (nlm_image->height - y - 1);
#endif
Nlm_Uint2 x;
for (x = 0; x < nlm_image->width; x++)
{
Nlm_Uint1 color = pixmap[y_base + x];
gdImageSetPixel(gd_image, x, y, convert[color]);
}
}
Nlm_UnlockPixMapImage( (Nlm_Image)nlm_image );
}}
{{ /* Write out */
FILE *out_stream = FileOpen(fileName, "wb");
if (out_stream == NULL)
{
gdImageDestroy( gd_image );
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageGIF",
"Can't open GIF file for writing");
return FALSE;
}
gdImageGif(gd_image, out_stream);
FileClose( out_stream );
}}
gdImageDestroy( gd_image );
return TRUE;
#else
Nlm_DiagReset();
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageGIF", "Not implemented");
return FALSE;
#endif
}
Nlm_Boolean Nlm_SaveImageBMP(Nlm_Image image, Nlm_CharPtr fileName)
{
Nlm_DiagReset();
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageBMP", "Not implemented");
return FALSE;
}
Nlm_Boolean Nlm_SaveImageClip(Nlm_Image image)
{
register Nlm_PImagePtr im = (Nlm_PImagePtr)image;
#ifdef WIN_MSWIN
BITMAPINFO *bInfo;
Nlm_Handle bInfoHND;
Nlm_Int4 sizeHead;
#endif
Nlm_DiagReset();
if ( !im->image ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageClip", "Image is empty");
return FALSE;
}
#ifdef WIN_MSWIN
if ( !Nlm_LockPixMapImage(image) )
return FALSE;
sizeHead = sizeof(BITMAPINFOHEADER) + im->totalColors * sizeof(RGBQUAD);
bInfoHND = GlobalAlloc ( GMEM_MOVEABLE | GMEM_DDESHARE,
sizeHead + im->width * im->height);
if ( bInfoHND == NULL ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageClip",
"Cannot allocate memory" );
UnlockPixMapImage( image );
return FALSE;
}
bInfo = (BITMAPINFO*)HandLock( bInfoHND );
MemCpy(bInfo, im->bitInfo, sizeHead);
MemCpy((Nlm_Uint1Ptr)bInfo + sizeHead, im->curImagePtr,
im->width * im->height);
if( !OpenClipboard(NULL) ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageClip", GetWinErrStr());
GlobalFree( bInfoHND );
UnlockPixMapImage( image );
return FALSE;
}
EmptyClipboard();
if ( !SetClipboardData(CF_DIB, bInfoHND) ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageClip", GetWinErrStr());
GlobalFree( bInfoHND );
CloseClipboard();
UnlockPixMapImage( image );
return FALSE;
}
CloseClipboard();
UnlockPixMapImage( image );
return TRUE;
#else
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SaveImageClip", "Not implemented");
return FALSE;
#endif
}
Nlm_Boolean Nlm_PrintImage ( Nlm_Image image )
{
#ifdef WIN_MSWIN
register Nlm_PImagePtr im = (Nlm_PImagePtr)image;
PRINTDLG pd;
Nlm_Uint1Ptr fBuffer;
Nlm_Int2 xsize, ysize, xres, yres;
Nlm_Int2 dx, dy, caps;
DOCINFO info;
RECT rect;
HDC hDC;
#endif
Nlm_DiagReset();
#ifdef WIN_MSWIN
memset (&pd, 0, sizeof (PRINTDLG));
pd.lStructSize = sizeof (PRINTDLG);
pd.hwndOwner = Nlm_currentHWnd;
pd.Flags = PD_RETURNDC;
if (PrintDlg (&pd) == 0) return FALSE;
hDC = pd.hDC;
if( !hDC ) return FALSE;
caps = GetDeviceCaps( hDC, RASTERCAPS );
if( !(caps & RC_STRETCHDIB) ) return FALSE;
xres = GetDeviceCaps( hDC, LOGPIXELSX );
yres = GetDeviceCaps( hDC, LOGPIXELSY );
xsize = GetDeviceCaps( hDC, HORZRES );
ysize = GetDeviceCaps( hDC, VERTRES );
dx = (Nlm_Int2)(xsize - xres);
dy = (Nlm_Int2)(((Nlm_Int4)dx*im->height)/im->width);
rect.top = yres; rect.bottom = rect.top + dy;
rect.left = xres>>1; rect.right = rect.left + dx;
Escape( hDC, SET_BOUNDS, sizeof(RECT), (Nlm_CharPtr)&rect, NULL );
info.cbSize = sizeof(DOCINFO);
info.lpszDocName = "Vibrant";
info.lpszOutput = NULL;
StartDoc( hDC, &info );
StartPage( hDC );
fBuffer = Nlm_LockPixMapImage ( image );
StretchDIBits( hDC, xres>>1, yres, dx, dy,
0, 0, im->width, im->height,
fBuffer, im->bitInfo, DIB_RGB_COLORS, SRCCOPY );
Nlm_UnlockPixMapImage ( image );
EndPage( hDC );
EndDoc( hDC );
DeleteDC( hDC );
if (pd.hDevMode != NULL) {
GlobalFree (pd.hDevMode);
}
if (pd.hDevNames != NULL) {
GlobalFree (pd.hDevNames);
}
return TRUE;
#else
Nlm_DiagPutRecord(DA_ERROR, imageClass, "PrintImage", "Not implemented");
return FALSE;
#endif
}
void Nlm_DeleteImage(Nlm_Image image)
{
register Nlm_PImagePtr im = (Nlm_PImagePtr)image;
if ( im->image )
{
if ( im->imageLocked )
HandUnlock( im->image );
HandFree( im->image );
}
if ( im->curWin != NULL )
Nlm_SetColorMap(im->curWin, 0, NULL, NULL, NULL);
#ifdef WIN_MSWIN
if ( im->hMemDC ) DeleteDC( im->hMemDC );
if ( im->pixMap ) DeleteObject( im->pixMap );
if ( im->bitInfo ) MemFree( im->bitInfo );
#endif
#ifdef WIN_MOTIF
if ( im->imageX11 )
{
im->imageX11->data = NULL;
XDestroyImage( im->imageX11 );
}
#endif
#ifdef WIN_MAC
#ifndef WIN_MAC_QUARTZ
if ( im->pixelMap )
{
if ( im->pixelMap->pmTable )
DisposeCTable( im->pixelMap->pmTable );
MemFree( im->pixelMap );
}
#endif
#endif
MemFree( im );
}
Nlm_Boolean Nlm_SetColorImage(Nlm_Image image, Nlm_Uint1 color,
Nlm_Uint1 red, Nlm_Uint1 green, Nlm_Uint1 blue)
{
register Nlm_PImagePtr im = (Nlm_PImagePtr)image;
Nlm_DiagReset();
if (color >= im->totalColors) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "SetColorImage",
"Color is out of range");
return FALSE;
}
if (im->red [color] != red ||
im->green[color] != green ||
im->blue [color] != blue)
{
im->red [color] = red;
im->green[color] = green;
im->blue [color] = blue;
im->imageColorVer++;
}
return TRUE;
}
Nlm_Boolean Nlm_GetColorImage(Nlm_Image image, Nlm_Uint1 color,
Nlm_Uint1Ptr red, Nlm_Uint1Ptr green,
Nlm_Uint1Ptr blue)
{
register Nlm_PImagePtr im = (Nlm_PImagePtr)image;
Nlm_DiagReset();
if (color >= im->totalColors) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "GetColorImage",
"Color is out of range");
return FALSE;
}
if ( red ) *red = im->red [color];
if ( green ) *green = im->green[color];
if ( blue ) *blue = im->blue [color];
return TRUE;
}
Nlm_Uint1Ptr Nlm_LockPixMapImage(Nlm_Image image)
{
Nlm_PImagePtr im = (Nlm_PImagePtr)image;
if ( !im )
return NULL;
Nlm_DiagReset();
if ( im->imageLocked ) {
im->imageLocked++;
return im->curImagePtr;
}
im->curImagePtr = (Nlm_Uint1Ptr)HandLock( im->image );
if (im->curImagePtr == NULL)
Nlm_DiagPutRecord(DA_ERROR, imageClass, "LockPixMapImage",
"Cannot lock memory block");
else
im->imageLocked = 1;
return im->curImagePtr;
}
void Nlm_UnlockPixMapImage(Nlm_Image image)
{
Nlm_PImagePtr im = (Nlm_PImagePtr)image;
if ( !im )
return;
Nlm_DiagReset();
switch ( im->imageLocked ){
case 0:
break;
case 1:
HandUnlock ( im->image );
im->imageLocked = 0;
break;
default:
im->imageLocked--;
}
}
void Nlm_ImageModified(Nlm_Image image)
{
((Nlm_PImagePtr)image)->imageVer++;
}
void Nlm_GetImageSize(Nlm_Image image, Nlm_Uint2Ptr width, Nlm_Uint2Ptr height)
{
if ( width ) *width = ((Nlm_PImagePtr)image)->width;
if ( height ) *height = ((Nlm_PImagePtr)image)->height;
}
Nlm_Boolean Nlm_ImageSetPalette(Nlm_Image image, Nlm_WindoW w)
{
((Nlm_PImagePtr)image)->curWin = w;
return TRUE;
}
#ifdef WIN_MOTIF
static int ShiftXMask(unsigned long mask, int bitmap_unit)
{
int low, hi;
for(low = 0; low < bitmap_unit; low++) {
if((mask >> low) & (unsigned long)1) break;
}
for (hi = 1; hi < bitmap_unit - low; hi++) {
if(!((mask >> hi + low) & (unsigned long)1)) break;
}
return low - ( 8 - hi); /* correct for default 8 bit palette */
}
#endif /* WIN_MOTIF */
Nlm_Boolean Nlm_ImageShow(Nlm_Image image, Nlm_PoinT p)
{
register Nlm_PImagePtr im = (Nlm_PImagePtr)image;
#ifdef WIN_MAC
Rect rectSrc;
Rect rectDst;
GrafPtr port;
CTabHandle tabHandle;
CTabPtr tabPtr;
Nlm_Int2 i;
#endif
#ifdef WIN_MOTIF
XVisualInfo visinfo;
#endif /* WIN_MOTIF */
Nlm_DiagReset();
if ( !im->image ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "ImageShow", "Image is empty");
return FALSE;
}
#ifdef WIN_MSWIN
if (im->imageColorVer != im->imageColorVerOld)
{
if ( im->curWin )
Nlm_SetColorMap(im->curWin, im->totalColors,
im->red, im->green, im->blue);
AllocateColorMap( im );
if (im->hMemDC ) {
DeleteDC( im->hMemDC );
im->hMemDC = NULL;
}
im->imageColorVerOld = im->imageColorVer;
im->imageVer = im->imageVerOld + 1;
}
if (im->imageVer != im->imageVerOld)
{
if ( !Nlm_LockPixMapImage(image) )
return FALSE;
if ( im->pixMap ) {
DeleteObject( im->pixMap );
im->pixMap = NULL;
}
im->pixMap = CreateDIBitmap(Nlm_currentHDC,
(BITMAPINFOHEADER*)im->bitInfo,
CBM_INIT, im->curImagePtr, im->bitInfo,
DIB_RGB_COLORS);
Nlm_UnlockPixMapImage( image );
if ( !im->pixMap ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "ImageShow",
"Can not create pixmap");
return FALSE;
}
if ( !im->hMemDC )
im->hMemDC = CreateCompatibleDC( Nlm_currentHDC );
if ( !im->hMemDC ) {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "ImageShow",
"Can not create DC");
return FALSE;
}
SelectObject(im->hMemDC, im->pixMap);
im->imageVerOld = im->imageVer;
}
BitBlt(Nlm_currentHDC,
p.x, p.y, im->width, im->height, im->hMemDC, 0, 0, SRCCOPY);
#endif
#ifdef WIN_MOTIF
{{
if (im->imageColorVer != im->imageColorVerOld)
{
if (im->curWin != NULL)
Nlm_SetColorMap(im->curWin, im->totalColors,
im->red, im->green, im->blue);
else
{
int col;
for (col = im->saveColors; col < (int)im->totalColors; col++)
im->altcol[col] = Nlm_GetColorRGB(im->red [col],
im->green[col],
im->blue [col]);
}
im->imageColorVerOld = im->imageColorVer;
}
if (im->curWin == NULL)
{
register int i, im_size = im->width * im->height;
register Nlm_Uint1Ptr altcol = im->altcol;
register Nlm_Uint1Ptr src = im->curImagePtr;
register Nlm_Uint1Ptr dst = (Nlm_Uint1Ptr)
Nlm_MemGet(im_size, MGET_ERRPOST);
im->imageX11->data = (char *)dst;
if ( !im->imageX11->data )
return FALSE;
for (i = 0; i < im_size; i++)
*dst++ = altcol[*src++];
}
#ifdef OS_UNIX_LINUX
else if (Nlm_CheckX(&visinfo)){
Nlm_Int4 im_size, i, xx, yy;
Nlm_Uint1Ptr src = im->curImagePtr;
unsigned long dst;
int red_shift, blue_shift, green_shift;
im_size = (im->imageX11->height * im->imageX11->bytes_per_line
+ im->imageX11->xoffset * im->imageX11->bitmap_unit/8);
red_shift = ShiftXMask(im->imageX11->red_mask,
im->imageX11->bitmap_unit);
green_shift = ShiftXMask(im->imageX11->green_mask,
im->imageX11->bitmap_unit);
blue_shift = ShiftXMask(im->imageX11->blue_mask,
im->imageX11->bitmap_unit);
if((visinfo.class == PseudoColor || visinfo.class == GrayScale)
&& visinfo.depth == 16) {
im->imageX11->data = (char *) Nlm_MemNew((size_t)im_size);
for (xx = 0; xx < im->imageX11->width; xx++) {
for( yy = 0; yy < im->imageX11->height; yy++) {
i = yy * im->imageX11->width + xx;
XPutPixel(im->imageX11, xx, yy, (unsigned long)src[i]);
}
}
}
else if(visinfo.class == TrueColor) {
im->imageX11->data = (char *) Nlm_MemNew((size_t)im_size);
for (xx = 0; xx < im->imageX11->width; xx++) {
for( yy = 0; yy < im->imageX11->height; yy++) {
i = yy * im->imageX11->width + xx;
dst = (((red_shift > 0 ?
(((unsigned long)im->red[src[i]]) << red_shift) :
(((unsigned long)im->red[src[i]])>>-red_shift)) &
im->imageX11->red_mask) |
((green_shift > 0 ?
(((unsigned long)im->green[src[i]]) << green_shift) :
(((unsigned long)im->green[src[i]])>>-green_shift)) &
im->imageX11->green_mask) |
((blue_shift > 0 ?
(((unsigned long)im->blue[src[i]]) << blue_shift) :
(((unsigned long)im->blue[src[i]])>>-blue_shift)) &
im->imageX11->blue_mask) );
XPutPixel(im->imageX11, xx, yy, dst);
}
}
}
else if((visinfo.class == PseudoColor || visinfo.class == GrayScale)
&& visinfo.depth == 8) {
dst = dst; /* placeholder */
}
else {
Nlm_MemFree(im->imageX11->data);
Nlm_DiagPutRecord(DA_ERROR, imageClass, "ImageShow",
"Can't handle display class");
return FALSE;
}
}
else {
Nlm_DiagPutRecord(DA_ERROR, imageClass, "ImageShow",
"Not valid display class");
return FALSE;
}
#endif /* OS_UNIX_LINUX */
XPutImage(Nlm_currentXDisplay, Nlm_currentXWindow,
Nlm_currentXGC, im->imageX11, 0, 0, p.x, p.y,
im->imageX11->width, im->imageX11->height);
if (im->curWin == NULL)
{
Nlm_MemFree( im->imageX11->data );
im->imageX11->data = (char *)im->curImagePtr;
}
#ifdef OS_UNIX_LINUX
else if (Nlm_CheckX(&visinfo)) {
if(!((visinfo.class == PseudoColor || visinfo.class == GrayScale)
&& visinfo.depth == 8)) {
MemFree(im->imageX11->data);
im->imageX11->data = (char *)im->curImagePtr;
}
}
#endif /* OS_UNIX_LINUX */
XFlush( Nlm_currentXDisplay );
}}
#endif
#ifdef WIN_MAC
#ifndef WIN_MAC_QUARTZ
if ( Nlm_LockPixMapImage(image) == NULL ) return FALSE;
if ( im->imageColorVer != im->imageColorVerOld ){
if ( im->curWin != NULL ){
Nlm_SetColorMap ( im->curWin, im->totalColors,
im->red, im->green, im->blue );
}
tabHandle = im->pixelMap->pmTable;
if ( tabHandle != NULL ) DisposeCTable ( tabHandle );
im->pixelMap->pmTable = tabHandle = GetCTable(72);
HLock ( (Nlm_Handle)tabHandle );
tabPtr = *tabHandle;
for ( i=0; i<(Nlm_Int2)im->totalColors; i++ ){
tabPtr->ctTable[i].rgb.red = (Nlm_Uint2)im->red [i] << 8
| (Nlm_Uint2)im->red[i];
tabPtr->ctTable[i].rgb.green = (Nlm_Uint2)im->green[i] << 8
| (Nlm_Uint2)im->green[i];
tabPtr->ctTable[i].rgb.blue = (Nlm_Uint2)im->blue [i] << 8
| (Nlm_Uint2)im->blue[i];
if (i >= 255) break;
}
tabPtr->ctSeed = GetCTSeed();
HUnlock((Nlm_Handle)tabHandle );
im->imageColorVerOld = im->imageColorVer;
}
im->pixelMap->baseAddr = (Ptr)im->curImagePtr;
rectSrc.top = 0; rectSrc.bottom = im->height;
rectSrc.left = 0; rectSrc.right = im->width;
rectDst.top = p.y; rectDst.bottom = im->height+p.y;
rectDst.left = p.x; rectDst.right = im->width+p.x;
GetPort (&port);
CopyBits((BitMap*)im->pixelMap,
GetPortBitMapForCopyBits(port),
&rectSrc, &rectDst, srcCopy, NULL);
BackColor(blackColor);
Nlm_UnlockPixMapImage(image);
#endif
#endif
return TRUE;
}
Nlm_Uint1 Nlm_ImageGetBlack(Nlm_Image image)
{
return (Nlm_Uint1)((Nlm_PImagePtr)image)->saveColors;
}
Nlm_Uint1 Nlm_ImageGetColorOffset(Nlm_Image image)
{
return (Nlm_Uint1)(((Nlm_PImagePtr)image)->saveColors + 1);
}
Nlm_Uint1 Nlm_ImageGetColorMax(Nlm_Image image)
{
return (Nlm_Uint1)(((Nlm_PImagePtr)image)->totalColors - 1);
}
|