< prev index next >

src/java.desktop/unix/native/common/awt/X11Color.c

Print this page
rev 56159 : 8230480: check malloc/calloc results in java.desktop


 385     char *forcemono;
 386     char *forcegray;
 387 
 388     make_uns_ordered_dither_array(img_oda_alpha, 256);
 389 
 390 
 391     forcemono = getenv("FORCEMONO");
 392     forcegray = getenv("FORCEGRAY");
 393     if (forcemono && !forcegray)
 394         forcegray = forcemono;
 395 
 396     /*
 397      * Get the colormap and make sure we have the right visual
 398      */
 399     dpy = awt_display;
 400     cm = awt_data->awt_cmap;
 401     depth = awt_data->awt_depth;
 402     pVI = &awt_data->awt_visInfo;
 403     awt_data->awt_num_colors = awt_data->awt_visInfo.colormap_size;
 404     awt_data->awtImage = (awtImageData *) calloc (1, sizeof (awtImageData));



 405 
 406     pPFV = XListPixmapFormats(dpy, &numpfv);
 407     if (pPFV) {
 408         for (i = 0; i < numpfv; i++) {
 409             if (pPFV[i].depth == depth) {
 410                 awt_data->awtImage->wsImageFormat = pPFV[i];
 411                 break;
 412             }
 413         }
 414         XFree(pPFV);
 415     }
 416     bpp = awt_data->awtImage->wsImageFormat.bits_per_pixel;
 417     if (bpp == 24) {
 418         bpp = 32;
 419     }
 420     awt_data->awtImage->clrdata.bitsperpixel = bpp;
 421     awt_data->awtImage->Depth = depth;
 422 
 423     if ((bpp == 32 || bpp == 16) && pVI->class == TrueColor && depth >= 15) {
 424         awt_data->AwtColorMatch = awt_color_matchTC;


 545         }
 546         awt_fill_imgcv(awt_data->awtImage->convert,
 547                        (IMGCV_SCALEBITS | IMGCV_INSIZEBITS | IMGCV_ALPHABITS
 548                         | IMGCV_ORDERBITS | IMGCV_CMBITS),
 549                        (IMGCV_UNSCALED | IMGCV_BYTEIN | IMGCV_OPAQUE
 550                         | IMGCV_RANDORDER | IMGCV_ICM),
 551                        OrdColorIcmOpqUnsImageConvert);
 552         awt_fill_imgcv(awt_data->awtImage->convert,
 553                        (IMGCV_SCALEBITS | IMGCV_INSIZEBITS | IMGCV_ALPHABITS
 554                         | IMGCV_ORDERBITS | IMGCV_CMBITS),
 555                        (IMGCV_UNSCALED | IMGCV_INTIN | IMGCV_OPAQUE
 556                         | IMGCV_RANDORDER | IMGCV_DCM8),
 557                        OrdColorDcmOpqUnsImageConvert);
 558 #endif /* NEED_IMAGE_CONVERT */
 559     } else {
 560         free (awt_data->awtImage);
 561         return 0;
 562     }
 563 
 564     if (depth > 12) {

 565         return 1;
 566     }
 567 
 568     if (depth == 12) {
 569         paletteSize = MAX_PALETTE12_SIZE;
 570     } else {
 571         paletteSize = MAX_PALETTE8_SIZE;
 572     }
 573 
 574     if (awt_data->awt_num_colors > paletteSize) {
 575         free (awt_data->awtImage);
 576         return 0;
 577     }
 578 
 579     /* Allocate ColorData structure */
 580     awt_data->color_data = ZALLOC (_ColorData);
 581     awt_data->color_data->screendata = 1; /* This ColorData struct corresponds
 582                                              to some AWT screen/visual, so when
 583                                              any IndexColorModel using this
 584                                              struct is finalized, don't free
 585                                              the struct in freeICMColorData.
 586                                            */
 587 
 588     /*
 589      * Initialize colors array
 590      */
 591     for (i = 0; i < awt_data->awt_num_colors; i++) {
 592         cols[i].pixel = i;
 593     }
 594 
 595     awt_data->color_data->awt_Colors =
 596         (ColorEntry *)calloc(paletteSize, sizeof (ColorEntry));




 597 
 598     XQueryColors(dpy, cm, cols, awt_data->awt_num_colors);
 599     for (i = 0; i < awt_data->awt_num_colors; i++) {
 600         awt_data->color_data->awt_Colors[i].r = cols[i].red >> 8;
 601         awt_data->color_data->awt_Colors[i].g = cols[i].green >> 8;
 602         awt_data->color_data->awt_Colors[i].b = cols[i].blue >> 8;
 603         awt_data->color_data->awt_Colors[i].flags = LIKELY_COLOR;
 604     }
 605 
 606     /*
 607      * Determine which colors in the colormap can be allocated and mark
 608      * them in the colors array
 609      */
 610     nfree = 0;
 611     for (i = (paletteSize / 2); i > 0; i >>= 1) {
 612         if (XAllocColorCells(dpy, cm, False, plane_masks, 0,
 613                              freecolors + nfree, i)) {
 614             nfree += i;
 615         }
 616     }


 650      * Allocate more colors, filling the color space evenly.
 651      */
 652 
 653     alloc_col(dpy, cm, 255, 255, 255, -1, awt_data);
 654     alloc_col(dpy, cm, 0, 0, 0, -1, awt_data);
 655 
 656     if (awt_data->awtImage->clrdata.grayscale) {
 657         int g;
 658         ColorEntry *p;
 659 
 660         if (!forcemono) {
 661             for (i = 128; i > 0; i >>= 1) {
 662                 for (g = i; g < 256; g += (i * 2)) {
 663                     alloc_col(dpy, cm, g, g, g, -1, awt_data);
 664                 }
 665             }
 666         }
 667 
 668         awt_data->color_data->img_grays =
 669             (unsigned char *)calloc(256, sizeof(unsigned char));



 670         for (g = 0; g < 256; g++) {
 671             int mindist, besti;
 672             int d;
 673 
 674             p = awt_data->color_data->awt_Colors;
 675             mindist = 256;
 676             besti = 0;
 677             for (i = 0 ; i < awt_data->awt_num_colors ; i++, p++) {
 678                 if (forcegray && (p->r != p->g || p->g != p->b))
 679                     continue;
 680                 if (forcemono && p->g != 0 && p->g != 255)
 681                     continue;
 682                 if (p->flags == ALLOCATED_COLOR) {
 683                     d = p->g - g;
 684                     if (d < 0) d = -d;
 685                     if (d < mindist) {
 686                         besti = i;
 687                         if (d == 0) {
 688                             break;
 689                         }


 793         if (cmapsize > CMAP_ALLOC_MAX) {
 794             cmapsize = CMAP_ALLOC_MAX;
 795         }
 796 
 797         if (cmapsize < allocatedColorsNum) {
 798             cmapsize = allocatedColorsNum;
 799         }
 800         cmapsize -= unavailableColorsNum;
 801     }
 802 
 803     k = 0;
 804     if (getenv("VIRTCUBESIZE") != 0) {
 805         k = atoi(getenv("VIRTCUBESIZE"));
 806     }
 807     if (k == 0 || (k & (k - 1)) != 0 || k > 32) {
 808         k = getVirtCubeSize();
 809     }
 810     awt_data->color_data->img_clr_tbl =
 811         (unsigned char *)calloc(LOOKUPSIZE * LOOKUPSIZE * LOOKUPSIZE,
 812                                 sizeof(unsigned char));



 813     img_makePalette(cmapsize, k, LOOKUPSIZE, 50, 250,
 814                     allocatedColorsNum, TRUE, reds, greens, blues,
 815                     awt_data->color_data->img_clr_tbl);
 816                     /*img_clr_tbl);*/
 817 
 818     for (i = 0; i < cmapsize; i++) {
 819         indices[i] = alloc_col(dpy, cm, reds[i], greens[i], blues[i], -1,
 820                                awt_data);
 821     }
 822     for (i = 0; i < LOOKUPSIZE * LOOKUPSIZE * LOOKUPSIZE  ; i++) {
 823         awt_data->color_data->img_clr_tbl[i] =
 824             indices[awt_data->color_data->img_clr_tbl[i]];
 825     }
 826 
 827     awt_data->color_data->img_oda_red   = &(std_img_oda_red[0][0]);
 828     awt_data->color_data->img_oda_green = &(std_img_oda_green[0][0]);
 829     awt_data->color_data->img_oda_blue  = &(std_img_oda_blue[0][0]);
 830     make_dither_arrays(cmapsize, awt_data->color_data);
 831     std_odas_computed = 1;
 832 


 841                 alloc_count++;
 842                 break;
 843               case LIKELY_COLOR:
 844                 reuse_count++;
 845                 break;
 846               case FREE_COLOR:
 847                 free_count++;
 848                 break;
 849             }
 850         }
 851         jio_fprintf(stdout, "%d total, %d allocated, %d reused, %d still free.\n",
 852                     awt_data->awt_num_colors, alloc_count, reuse_count, free_count);
 853     }
 854 #endif
 855 
 856     /* Fill in the ICM lut and lut2cmap mapping */
 857     awt_data->color_data->awt_numICMcolors = 0;
 858     awt_data->color_data->awt_icmLUT2Colors =
 859         (unsigned char *)calloc(paletteSize, sizeof (unsigned char));
 860     awt_data->color_data->awt_icmLUT = (int *)calloc(paletteSize, sizeof(int));




 861     for (i=0; i < paletteSize; i++) {
 862         /* Keep the mapping between this lut and the actual cmap */
 863         awt_data->color_data->awt_icmLUT2Colors
 864             [awt_data->color_data->awt_numICMcolors] = i;
 865 
 866         if (awt_data->color_data->awt_Colors[i].flags == ALLOCATED_COLOR) {
 867             /* Screen IndexColorModel LUTS are always xRGB */
 868             awt_data->color_data->awt_icmLUT
 869                     [awt_data->color_data->awt_numICMcolors++] = 0xff000000 |
 870                 (awt_data->color_data->awt_Colors[i].r<<16) |
 871                 (awt_data->color_data->awt_Colors[i].g<<8) |
 872                 (awt_data->color_data->awt_Colors[i].b);
 873         } else {
 874             /* Screen IndexColorModel LUTS are always xRGB */
 875             awt_data->color_data->awt_icmLUT
 876                         [awt_data->color_data->awt_numICMcolors++] = 0;
 877         }
 878     }
 879     return 1;
 880 }




 385     char *forcemono;
 386     char *forcegray;
 387 
 388     make_uns_ordered_dither_array(img_oda_alpha, 256);
 389 
 390 
 391     forcemono = getenv("FORCEMONO");
 392     forcegray = getenv("FORCEGRAY");
 393     if (forcemono && !forcegray)
 394         forcegray = forcemono;
 395 
 396     /*
 397      * Get the colormap and make sure we have the right visual
 398      */
 399     dpy = awt_display;
 400     cm = awt_data->awt_cmap;
 401     depth = awt_data->awt_depth;
 402     pVI = &awt_data->awt_visInfo;
 403     awt_data->awt_num_colors = awt_data->awt_visInfo.colormap_size;
 404     awt_data->awtImage = (awtImageData *) calloc (1, sizeof (awtImageData));
 405     if (awt_data->awtImage == NULL) {
 406         return 0;
 407     }
 408 
 409     pPFV = XListPixmapFormats(dpy, &numpfv);
 410     if (pPFV) {
 411         for (i = 0; i < numpfv; i++) {
 412             if (pPFV[i].depth == depth) {
 413                 awt_data->awtImage->wsImageFormat = pPFV[i];
 414                 break;
 415             }
 416         }
 417         XFree(pPFV);
 418     }
 419     bpp = awt_data->awtImage->wsImageFormat.bits_per_pixel;
 420     if (bpp == 24) {
 421         bpp = 32;
 422     }
 423     awt_data->awtImage->clrdata.bitsperpixel = bpp;
 424     awt_data->awtImage->Depth = depth;
 425 
 426     if ((bpp == 32 || bpp == 16) && pVI->class == TrueColor && depth >= 15) {
 427         awt_data->AwtColorMatch = awt_color_matchTC;


 548         }
 549         awt_fill_imgcv(awt_data->awtImage->convert,
 550                        (IMGCV_SCALEBITS | IMGCV_INSIZEBITS | IMGCV_ALPHABITS
 551                         | IMGCV_ORDERBITS | IMGCV_CMBITS),
 552                        (IMGCV_UNSCALED | IMGCV_BYTEIN | IMGCV_OPAQUE
 553                         | IMGCV_RANDORDER | IMGCV_ICM),
 554                        OrdColorIcmOpqUnsImageConvert);
 555         awt_fill_imgcv(awt_data->awtImage->convert,
 556                        (IMGCV_SCALEBITS | IMGCV_INSIZEBITS | IMGCV_ALPHABITS
 557                         | IMGCV_ORDERBITS | IMGCV_CMBITS),
 558                        (IMGCV_UNSCALED | IMGCV_INTIN | IMGCV_OPAQUE
 559                         | IMGCV_RANDORDER | IMGCV_DCM8),
 560                        OrdColorDcmOpqUnsImageConvert);
 561 #endif /* NEED_IMAGE_CONVERT */
 562     } else {
 563         free (awt_data->awtImage);
 564         return 0;
 565     }
 566 
 567     if (depth > 12) {
 568         free (awt_data->awtImage);
 569         return 1;
 570     }
 571 
 572     if (depth == 12) {
 573         paletteSize = MAX_PALETTE12_SIZE;
 574     } else {
 575         paletteSize = MAX_PALETTE8_SIZE;
 576     }
 577 
 578     if (awt_data->awt_num_colors > paletteSize) {
 579         free (awt_data->awtImage);
 580         return 0;
 581     }
 582 
 583     /* Allocate ColorData structure */
 584     awt_data->color_data = ZALLOC (_ColorData);
 585     awt_data->color_data->screendata = 1; /* This ColorData struct corresponds
 586                                              to some AWT screen/visual, so when
 587                                              any IndexColorModel using this
 588                                              struct is finalized, don't free
 589                                              the struct in freeICMColorData.
 590                                            */
 591 
 592     /*
 593      * Initialize colors array
 594      */
 595     for (i = 0; i < awt_data->awt_num_colors; i++) {
 596         cols[i].pixel = i;
 597     }
 598 
 599     awt_data->color_data->awt_Colors =
 600         (ColorEntry *)calloc(paletteSize, sizeof (ColorEntry));
 601     if (awt_data->color_data->awt_Colors == NULL) {
 602         free (awt_data->awtImage);
 603         return 0;
 604     }
 605 
 606     XQueryColors(dpy, cm, cols, awt_data->awt_num_colors);
 607     for (i = 0; i < awt_data->awt_num_colors; i++) {
 608         awt_data->color_data->awt_Colors[i].r = cols[i].red >> 8;
 609         awt_data->color_data->awt_Colors[i].g = cols[i].green >> 8;
 610         awt_data->color_data->awt_Colors[i].b = cols[i].blue >> 8;
 611         awt_data->color_data->awt_Colors[i].flags = LIKELY_COLOR;
 612     }
 613 
 614     /*
 615      * Determine which colors in the colormap can be allocated and mark
 616      * them in the colors array
 617      */
 618     nfree = 0;
 619     for (i = (paletteSize / 2); i > 0; i >>= 1) {
 620         if (XAllocColorCells(dpy, cm, False, plane_masks, 0,
 621                              freecolors + nfree, i)) {
 622             nfree += i;
 623         }
 624     }


 658      * Allocate more colors, filling the color space evenly.
 659      */
 660 
 661     alloc_col(dpy, cm, 255, 255, 255, -1, awt_data);
 662     alloc_col(dpy, cm, 0, 0, 0, -1, awt_data);
 663 
 664     if (awt_data->awtImage->clrdata.grayscale) {
 665         int g;
 666         ColorEntry *p;
 667 
 668         if (!forcemono) {
 669             for (i = 128; i > 0; i >>= 1) {
 670                 for (g = i; g < 256; g += (i * 2)) {
 671                     alloc_col(dpy, cm, g, g, g, -1, awt_data);
 672                 }
 673             }
 674         }
 675 
 676         awt_data->color_data->img_grays =
 677             (unsigned char *)calloc(256, sizeof(unsigned char));
 678         if ( awt_data->color_data->img_grays == NULL) {
 679             return 0;
 680         }
 681         for (g = 0; g < 256; g++) {
 682             int mindist, besti;
 683             int d;
 684 
 685             p = awt_data->color_data->awt_Colors;
 686             mindist = 256;
 687             besti = 0;
 688             for (i = 0 ; i < awt_data->awt_num_colors ; i++, p++) {
 689                 if (forcegray && (p->r != p->g || p->g != p->b))
 690                     continue;
 691                 if (forcemono && p->g != 0 && p->g != 255)
 692                     continue;
 693                 if (p->flags == ALLOCATED_COLOR) {
 694                     d = p->g - g;
 695                     if (d < 0) d = -d;
 696                     if (d < mindist) {
 697                         besti = i;
 698                         if (d == 0) {
 699                             break;
 700                         }


 804         if (cmapsize > CMAP_ALLOC_MAX) {
 805             cmapsize = CMAP_ALLOC_MAX;
 806         }
 807 
 808         if (cmapsize < allocatedColorsNum) {
 809             cmapsize = allocatedColorsNum;
 810         }
 811         cmapsize -= unavailableColorsNum;
 812     }
 813 
 814     k = 0;
 815     if (getenv("VIRTCUBESIZE") != 0) {
 816         k = atoi(getenv("VIRTCUBESIZE"));
 817     }
 818     if (k == 0 || (k & (k - 1)) != 0 || k > 32) {
 819         k = getVirtCubeSize();
 820     }
 821     awt_data->color_data->img_clr_tbl =
 822         (unsigned char *)calloc(LOOKUPSIZE * LOOKUPSIZE * LOOKUPSIZE,
 823                                 sizeof(unsigned char));
 824     if (awt_data->color_data->img_clr_tbl == NULL) {
 825         return 0;
 826     }
 827     img_makePalette(cmapsize, k, LOOKUPSIZE, 50, 250,
 828                     allocatedColorsNum, TRUE, reds, greens, blues,
 829                     awt_data->color_data->img_clr_tbl);
 830                     /*img_clr_tbl);*/
 831 
 832     for (i = 0; i < cmapsize; i++) {
 833         indices[i] = alloc_col(dpy, cm, reds[i], greens[i], blues[i], -1,
 834                                awt_data);
 835     }
 836     for (i = 0; i < LOOKUPSIZE * LOOKUPSIZE * LOOKUPSIZE  ; i++) {
 837         awt_data->color_data->img_clr_tbl[i] =
 838             indices[awt_data->color_data->img_clr_tbl[i]];
 839     }
 840 
 841     awt_data->color_data->img_oda_red   = &(std_img_oda_red[0][0]);
 842     awt_data->color_data->img_oda_green = &(std_img_oda_green[0][0]);
 843     awt_data->color_data->img_oda_blue  = &(std_img_oda_blue[0][0]);
 844     make_dither_arrays(cmapsize, awt_data->color_data);
 845     std_odas_computed = 1;
 846 


 855                 alloc_count++;
 856                 break;
 857               case LIKELY_COLOR:
 858                 reuse_count++;
 859                 break;
 860               case FREE_COLOR:
 861                 free_count++;
 862                 break;
 863             }
 864         }
 865         jio_fprintf(stdout, "%d total, %d allocated, %d reused, %d still free.\n",
 866                     awt_data->awt_num_colors, alloc_count, reuse_count, free_count);
 867     }
 868 #endif
 869 
 870     /* Fill in the ICM lut and lut2cmap mapping */
 871     awt_data->color_data->awt_numICMcolors = 0;
 872     awt_data->color_data->awt_icmLUT2Colors =
 873         (unsigned char *)calloc(paletteSize, sizeof (unsigned char));
 874     awt_data->color_data->awt_icmLUT = (int *)calloc(paletteSize, sizeof(int));
 875     if (awt_data->color_data->awt_icmLUT2Colors == NULL || awt_data->color_data->awt_icmLUT == NULL) {
 876         return 0;
 877     }
 878 
 879     for (i=0; i < paletteSize; i++) {
 880         /* Keep the mapping between this lut and the actual cmap */
 881         awt_data->color_data->awt_icmLUT2Colors
 882             [awt_data->color_data->awt_numICMcolors] = i;
 883 
 884         if (awt_data->color_data->awt_Colors[i].flags == ALLOCATED_COLOR) {
 885             /* Screen IndexColorModel LUTS are always xRGB */
 886             awt_data->color_data->awt_icmLUT
 887                     [awt_data->color_data->awt_numICMcolors++] = 0xff000000 |
 888                 (awt_data->color_data->awt_Colors[i].r<<16) |
 889                 (awt_data->color_data->awt_Colors[i].g<<8) |
 890                 (awt_data->color_data->awt_Colors[i].b);
 891         } else {
 892             /* Screen IndexColorModel LUTS are always xRGB */
 893             awt_data->color_data->awt_icmLUT
 894                         [awt_data->color_data->awt_numICMcolors++] = 0;
 895         }
 896     }
 897     return 1;
 898 }


< prev index next >