Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/awt_common/awt_makecube.cpp
32287 views
/*1* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "awt.h"26#include "awt_image.h"2728extern "C" {29#include "img_colors.h"30} // extern "C"3132char *programname = "awt_makecube";3334unsigned char cube[LOOKUPSIZE * LOOKUPSIZE * LOOKUPSIZE];3536unsigned char reds[256], greens[256], blues[256], indices[256];37int num_colors;3839PALETTEENTRY sysPal[256];4041int sys2cmap[256];42int cmap2sys[256];43int error[256];4445int cmapsize = 0;46int virtcubesize = 0;47int makecube_verbose = 0;4849void printPalette(char *label, HPALETTE hPal);5051void usage(char *errmsg)52{53fprintf(stderr, "%s\n", errmsg);54fprintf(stderr, "usage: %s [-cmapsize N] [-cubesize N]\n", programname);55fprintf(stderr, "\t-cmapsize N set the number of colors to allocate\n");56fprintf(stderr, "\t in the colormap (2 <= N <= 256)\n");57fprintf(stderr, "\t-cubesize N set the size of the cube of colors to\n");58fprintf(stderr, " scan as potential entries in the cmap\n");59fprintf(stderr, " (N must be a power of 2 and <= 32)\n");60exit(1);61}6263void setsyscolor(int index, int red, int green, int blue)64{65if (index >= 0) {66if (sysPal[index].peFlags != 0) {67usage("Internal error: system palette conflict");68}69} else {70for (int i = 0; i < 256; i++) {71if (sysPal[i].peFlags != 0) {72if (sysPal[i].peRed == red &&73sysPal[i].peGreen == green &&74sysPal[i].peBlue == blue)75{76// Already there. Ignore it.77return;78}79} else if (index < 0) {80index = i;81}82}83if (index < 0) {84usage("Internal error: ran out of system palette entries");85}86}87sysPal[index].peRed = red;88sysPal[index].peGreen = green;89sysPal[index].peBlue = blue;90sysPal[index].peFlags = 1;91}9293void addcmapcolor(int red, int green, int blue)94{95for (int i = 0; i < num_colors; i++) {96if (red == reds[i] && green == greens[i] && blue == blues[i]) {97return;98}99}100if (num_colors >= cmapsize) {101usage("Internal error: more than cmapsize static colors defined");102}103reds[num_colors] = red;104greens[num_colors] = green;105blues[num_colors] = blue;106num_colors++;107}108109int main(int argc, char **argv)110{111int i;112113programname = argv[0];114115for (i = 1; i < argc; i++) {116if (strcmp(argv[i], "-cmapsize") == 0) {117if (i++ >= argc) {118usage("no argument to -cmapsize");119}120cmapsize = atoi(argv[i]);121if (cmapsize <= 2 || cmapsize > 256) {122usage("colormap size must be between 2 and 256");123}124} else if (strcmp(argv[1], "-cubesize") == 0) {125if (i++ >= argc) {126usage("no argument to -cubesize");127}128virtcubesize = atoi(argv[i]);129if (virtcubesize == 0 ||130(virtcubesize & (virtcubesize - 1)) != 0 ||131virtcubesize > 32)132{133usage("cube size must by a power of 2 <= 32");134}135} else if (strcmp(argv[i], "-verbose") == 0) {136makecube_verbose = 1;137} else {138usage("unknown argument");139}140}141142if (cmapsize == 0) {143cmapsize = CMAPSIZE;144}145if (virtcubesize == 0) {146virtcubesize = VIRTCUBESIZE;147}148149if (0) { // For testing150HDC hDC = CreateDC("DISPLAY", NULL, NULL, NULL);151HPALETTE hPal = CreateHalftonePalette(hDC);152printPalette("Halftone palette for current display", hPal);153printPalette("Stock DEFAULT_PALETTE", (HPALETTE)GetStockObject(DEFAULT_PALETTE));154BITMAPINFOHEADER bmInfo = {155sizeof(BITMAPINFOHEADER), 1, 1, 1, 8, BI_RGB, 0, 1000, 1000, 0, 0156};157HBITMAP hBitmap = CreateDIBitmap(hDC, &bmInfo,1580, NULL, NULL, DIB_RGB_COLORS);159HDC hMemDC = CreateCompatibleDC(hDC);160SelectObject(hDC, hBitmap);161hPal = CreateHalftonePalette(hMemDC);162printPalette("Halftone palette for 8-bit DIBitmap", hPal);163exit(0);164}165166// Allocate Windows static system colors.167{168PALETTEENTRY palEntries[256];169HPALETTE hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);170int n = GetPaletteEntries(hPal, 0, 256, palEntries);171for (i = 0; i < n; i++) {172addcmapcolor(palEntries[i].peRed,173palEntries[i].peGreen,174palEntries[i].peBlue);175setsyscolor((i < n / 2) ? i : i + (256 - n),176palEntries[i].peRed,177palEntries[i].peGreen,178palEntries[i].peBlue);179}180}181182// Allocate java.awt.Color constant colors.183addcmapcolor( 0, 0, 0); // black184addcmapcolor(255, 255, 255); // white185addcmapcolor(255, 0, 0); // red186addcmapcolor( 0, 255, 0); // green187addcmapcolor( 0, 0, 255); // blue188addcmapcolor(255, 255, 0); // yellow189addcmapcolor(255, 0, 255); // magenta190addcmapcolor( 0, 255, 255); // cyan191addcmapcolor(192, 192, 192); // lightGray192addcmapcolor(128, 128, 128); // gray193addcmapcolor( 64, 64, 64); // darkGray194addcmapcolor(255, 175, 175); // pink195addcmapcolor(255, 200, 0); // orange196197img_makePalette(cmapsize, virtcubesize, LOOKUPSIZE,19850.0f, 250.0f,199num_colors, TRUE, reds, greens, blues, cube);200201if (makecube_verbose) {202fprintf(stderr, "Calculated colormap:\n");203for (i = 0; i < cmapsize; i++) {204fprintf(stderr, "%3d:(%3d,%3d,%3d) ",205i, reds[i], greens[i], blues[i]);206}207fprintf(stderr, "\n");208}209210// Now simulate adding the halftone palette to the system211// palette to get an idea of palette ordering.212{213int cubevals[6] = {0, 44, 86, 135, 192, 255};214for (int b = 0; b < 6; b++) {215for (int g = 0; g < 6; g++) {216for (int r = 0; r < 6; r++) {217setsyscolor(-1, cubevals[r], cubevals[g], cubevals[b]);218}219}220}221int grayvals[26] = { 0, 17, 24, 30, 37, 44, 52, 60,22268, 77, 86, 95, 105, 114, 125, 135,223146, 157, 168, 180, 192, 204, 216, 229,224242, 255 };225for (i = 0; i < 26; i++) {226setsyscolor(-1, grayvals[i], grayvals[i], grayvals[i]);227}228}229230if (makecube_verbose) {231fprintf(stderr, "System palette with simulated halftone palette:\n");232for (i = 0; i < 256; i++) {233fprintf(stderr, "%3d:(%3d,%3d,%3d) ",234i, sysPal[i].peRed, sysPal[i].peGreen, sysPal[i].peBlue);235}236}237238if (makecube_verbose) {239HDC hDC = CreateDC("DISPLAY", NULL, NULL, NULL);240HPALETTE hPal = CreateHalftonePalette(hDC);241SelectPalette(hDC, hPal, FALSE);242RealizePalette(hDC);243PALETTEENTRY palEntries[256];244int n = GetSystemPaletteEntries(hDC, 0, 256, palEntries);245fprintf(stderr,246"realized halftone palette reads back %d entries\n", n);247int broken = 0;248for (i = 0; i < 256; i++) {249char *msg1 = "";250char *msg2 = "";251if (palEntries[i].peRed != sysPal[i].peRed ||252palEntries[i].peGreen != sysPal[i].peGreen ||253palEntries[i].peBlue != sysPal[i].peBlue)254{255msg1 = "no sysPal match!";256if (sysPal[i].peFlags == 0) {257msg2 = "(OK)";258} else {259broken++;260}261} else if (sysPal[i].peFlags == 0) {262msg1 = "no sysPal entry...";263}264fprintf(stderr,265"palEntries[%3d] = (%3d, %3d, %3d), flags = %d %s %s\n",266i,267palEntries[i].peRed,268palEntries[i].peGreen,269palEntries[i].peBlue,270palEntries[i].peFlags, msg1, msg2);271}272fprintf(stderr, "%d broken entries\n", broken);273}274275#if 0276#define BIGERROR (255 * 255 * 255)277278for (i = 0; i < 256; i++) {279sys2cmap[i] = -1;280cmap2sys[i] = -1;281error[i] = BIGERROR;282// error[i] = -1 means cmap[i] is locked to cmap2sys[i]283// error[i] >= 0 means cmap[i] may lock to cmap2sys[i] on this run284}285286int nummapped;287int totalmapped = 0;288do {289int maxerror = BIGERROR;290for (i = 0; i < 256; i++) {291if (sysPal[i].peFlags == 0 || sys2cmap[i] >= 0) {292continue;293}294int red = sysPal[i].peRed;295int green = sysPal[i].peGreen;296int blue = sysPal[i].peBlue;297int e = maxerror;298int ix = -1;299for (int j = 0; j < 256; j++) {300if (error[j] < 0) {301continue;302}303int t = red - reds[j];304int d = t * t;305t = green - greens[j];306d += t * t;307t = blue - blues[j];308d += t * t;309if (d < e) {310e = d;311ix = j;312}313}314if (ix >= 0) {315if (e < error[ix]) {316if (cmap2sys[ix] >= 0) {317// To be fair we will not accept any matches318// looser than this former match that we just319// displaced with a better match.320if (maxerror > error[ix]) {321maxerror = error[ix];322}323sys2cmap[cmap2sys[ix]] = -1;324}325error[ix] = e;326sys2cmap[i] = ix;327cmap2sys[ix] = i;328}329}330}331nummapped = 0;332for (i = 0; i < 256; i++) {333if (error[i] >= 0) {334if (error[i] >= maxerror) {335// Throw this one back to be fair to a displaced entry.336error[i] = BIGERROR;337sys2cmap[cmap2sys[i]] = -1;338cmap2sys[i] = -1;339continue;340}341error[i] = -1;342nummapped++;343}344}345totalmapped += nummapped;346if (makecube_verbose) {347fprintf(stderr, "%3d colors mapped (%3d total), maxerror = %d\n",348nummapped, totalmapped, maxerror);349}350} while (nummapped != 0);351352for (i = 0; i < 256; i++) {353if (cmap2sys[i] < 0) {354for (int j = 0; j < 256; j++) {355if (sys2cmap[j] < 0) {356cmap2sys[i] = j;357sys2cmap[j] = i;358break;359}360}361if (j == 256) {362usage("Internal error: no unused system entry for cmap entry!\n");363}364}365}366#else367for (i = 0; i < 256; i++) {368if (i < 10) {369sys2cmap[i] = i;370cmap2sys[i] = i;371} else if (i < 20) {372sys2cmap[256 - 20 + i] = i;373cmap2sys[i] = 256 - 20 + i;374} else {375sys2cmap[i - 10] = i;376cmap2sys[i] = i - 10;377}378}379#endif380381if (makecube_verbose) {382fprintf(stderr, "cmap2sys mapping: \n");383for (i = 0; i < 256; i++) {384fprintf(stderr, "%4d", cmap2sys[i]);385if (sys2cmap[cmap2sys[i]] != i) {386usage("Internal error: bad system palette back pointer!\n");387}388}389fprintf(stderr, "\n");390}391392printf("unsigned char awt_reds[256] = {");393for (i = 0; i < 256; i++) {394if ((i & 0xf) == 0) printf("\n\t");395printf("%3d,", reds[sys2cmap[i]]);396}397printf("\n};\n");398printf("unsigned char awt_greens[256] = {");399for (i = 0; i < 256; i++) {400if ((i & 0xf) == 0) printf("\n\t");401printf("%3d,", greens[sys2cmap[i]]);402}403printf("\n};\n");404printf("unsigned char awt_blues[256] = {");405for (i = 0; i < 256; i++) {406if ((i & 0xf) == 0) printf("\n\t");407printf("%3d,", blues[sys2cmap[i]]);408}409printf("\n};\n");410fflush(stdout);411return 0;412}413414void printPalette(char *label, HPALETTE hPal)415{416PALETTEENTRY palEntries[256];417fprintf(stderr, "%s (0x%08x):\n", label, hPal);418int n = GetPaletteEntries(hPal, 0, 256, palEntries);419for (int i = 0; i < n; i++) {420fprintf(stderr, "palEntries[%3d] = (%3d, %3d, %3d), flags = %d\n",421i,422palEntries[i].peRed,423palEntries[i].peGreen,424palEntries[i].peBlue,425palEntries[i].peFlags);426}427}428429/* This helps eliminate any dependence on javai.dll at build time. */430int431jio_fprintf (FILE *handle, const char *format, ...)432{433int len;434435va_list args;436va_start(args, format);437len = vfprintf(handle, format, args);438va_end(args);439440return len;441}442443444