src/java.desktop/windows/native/common/awt_makecube.cpp
author jwilhelm
Fri, 02 Mar 2018 21:00:12 +0100
changeset 49331 10b24a3af249
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "awt.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "awt_image.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "img_colors.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
} // extern "C"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
char *programname = "awt_makecube";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
unsigned char cube[LOOKUPSIZE * LOOKUPSIZE * LOOKUPSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
unsigned char reds[256], greens[256], blues[256], indices[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
int num_colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
PALETTEENTRY sysPal[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
int sys2cmap[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
int cmap2sys[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
int error[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
int cmapsize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
int virtcubesize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
int makecube_verbose = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
void printPalette(char *label, HPALETTE hPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
void usage(char *errmsg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    fprintf(stderr, "%s\n", errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    fprintf(stderr, "usage: %s [-cmapsize N] [-cubesize N]\n", programname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    fprintf(stderr, "\t-cmapsize N   set the number of colors to allocate\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    fprintf(stderr, "\t              in the colormap (2 <= N <= 256)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    fprintf(stderr, "\t-cubesize N   set the size of the cube of colors to\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    fprintf(stderr, "                scan as potential entries in the cmap\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    fprintf(stderr, "                (N must be a power of 2 and <= 32)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
void setsyscolor(int index, int red, int green, int blue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (sysPal[index].peFlags != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            usage("Internal error: system palette conflict");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (sysPal[i].peFlags != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                if (sysPal[i].peRed   == red &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    sysPal[i].peGreen == green &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    sysPal[i].peBlue  == blue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    // Already there.  Ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            } else if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            usage("Internal error: ran out of system palette entries");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    sysPal[index].peRed   = red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    sysPal[index].peGreen = green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    sysPal[index].peBlue  = blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    sysPal[index].peFlags = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
void addcmapcolor(int red, int green, int blue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    for (int i = 0; i < num_colors; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (red == reds[i] && green == greens[i] && blue == blues[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if (num_colors >= cmapsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        usage("Internal error: more than cmapsize static colors defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    reds[num_colors]   = red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    greens[num_colors] = green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    blues[num_colors]  = blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    num_colors++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
int main(int argc, char **argv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    programname = argv[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    for (i = 1; i < argc; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (strcmp(argv[i], "-cmapsize") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (i++ >= argc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                usage("no argument to -cmapsize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            cmapsize = atoi(argv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (cmapsize <= 2 || cmapsize > 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                usage("colormap size must be between 2 and 256");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else if (strcmp(argv[1], "-cubesize") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (i++ >= argc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                usage("no argument to -cubesize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            virtcubesize = atoi(argv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (virtcubesize == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                (virtcubesize & (virtcubesize - 1)) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                virtcubesize > 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                usage("cube size must by a power of 2 <= 32");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (strcmp(argv[i], "-verbose") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            makecube_verbose = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            usage("unknown argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (cmapsize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        cmapsize = CMAPSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    if (virtcubesize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        virtcubesize = VIRTCUBESIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if (0) {  // For testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        HDC hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        HPALETTE hPal = CreateHalftonePalette(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        printPalette("Halftone palette for current display", hPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        printPalette("Stock DEFAULT_PALETTE", (HPALETTE)GetStockObject(DEFAULT_PALETTE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        BITMAPINFOHEADER bmInfo = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            sizeof(BITMAPINFOHEADER), 1, 1, 1, 8, BI_RGB, 0, 1000, 1000, 0, 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        HBITMAP hBitmap = CreateDIBitmap(hDC, &bmInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                         0, NULL, NULL, DIB_RGB_COLORS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        HDC hMemDC = CreateCompatibleDC(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        SelectObject(hDC, hBitmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        hPal = CreateHalftonePalette(hMemDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        printPalette("Halftone palette for 8-bit DIBitmap", hPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // Allocate Windows static system colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        PALETTEENTRY palEntries[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        HPALETTE hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int n = GetPaletteEntries(hPal, 0, 256, palEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            addcmapcolor(palEntries[i].peRed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                         palEntries[i].peGreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                         palEntries[i].peBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            setsyscolor((i < n / 2) ? i : i + (256 - n),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        palEntries[i].peRed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        palEntries[i].peGreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        palEntries[i].peBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // Allocate java.awt.Color constant colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    addcmapcolor(  0,   0,   0);        // black
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    addcmapcolor(255, 255, 255);        // white
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    addcmapcolor(255,   0,   0);        // red
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    addcmapcolor(  0, 255,   0);        // green
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    addcmapcolor(  0,   0, 255);        // blue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    addcmapcolor(255, 255,   0);        // yellow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    addcmapcolor(255,   0, 255);        // magenta
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    addcmapcolor(  0, 255, 255);        // cyan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    addcmapcolor(192, 192, 192);        // lightGray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    addcmapcolor(128, 128, 128);        // gray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    addcmapcolor( 64,  64,  64);        // darkGray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    addcmapcolor(255, 175, 175);        // pink
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    addcmapcolor(255, 200,   0);        // orange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    img_makePalette(cmapsize, virtcubesize, LOOKUPSIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    50.0f, 250.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    num_colors, TRUE, reds, greens, blues, cube);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    if (makecube_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        fprintf(stderr, "Calculated colormap:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (i = 0; i < cmapsize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            fprintf(stderr, "%3d:(%3d,%3d,%3d)   ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    i, reds[i], greens[i], blues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // Now simulate adding the halftone palette to the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    // palette to get an idea of palette ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int cubevals[6] = {0, 44, 86, 135, 192, 255};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int b = 0; b < 6; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            for (int g = 0; g < 6; g++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                for (int r = 0; r < 6; r++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    setsyscolor(-1, cubevals[r], cubevals[g], cubevals[b]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int grayvals[26] = {  0,  17,  24,  30,  37,  44,  52,  60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                             68,  77,  86,  95, 105, 114, 125, 135,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            146, 157, 168, 180, 192, 204, 216, 229,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            242, 255 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (i = 0; i < 26; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            setsyscolor(-1, grayvals[i], grayvals[i], grayvals[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    if (makecube_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        fprintf(stderr, "System palette with simulated halftone palette:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            fprintf(stderr, "%3d:(%3d,%3d,%3d)   ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    i, sysPal[i].peRed, sysPal[i].peGreen, sysPal[i].peBlue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    if (makecube_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        HDC hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        HPALETTE hPal = CreateHalftonePalette(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        SelectPalette(hDC, hPal, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        RealizePalette(hDC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        PALETTEENTRY palEntries[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int n = GetSystemPaletteEntries(hDC, 0, 256, palEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        fprintf(stderr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                "realized halftone palette reads back %d entries\n", n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int broken = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            char *msg1 = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            char *msg2 = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (palEntries[i].peRed != sysPal[i].peRed ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                palEntries[i].peGreen != sysPal[i].peGreen ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                palEntries[i].peBlue != sysPal[i].peBlue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                msg1 = "no sysPal match!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (sysPal[i].peFlags == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    msg2 = "(OK)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    broken++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            } else if (sysPal[i].peFlags == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                msg1 = "no sysPal entry...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            fprintf(stderr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    "palEntries[%3d] = (%3d, %3d, %3d), flags = %d  %s %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    palEntries[i].peRed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    palEntries[i].peGreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    palEntries[i].peBlue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    palEntries[i].peFlags, msg1, msg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        fprintf(stderr, "%d broken entries\n", broken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
#define BIGERROR (255 * 255 * 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        sys2cmap[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        cmap2sys[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        error[i] = BIGERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // error[i] = -1 means cmap[i] is locked to cmap2sys[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // error[i] >= 0 means cmap[i] may lock to cmap2sys[i] on this run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    int nummapped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    int totalmapped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int maxerror = BIGERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (sysPal[i].peFlags == 0 || sys2cmap[i] >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            int red   = sysPal[i].peRed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int green = sysPal[i].peGreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            int blue  = sysPal[i].peBlue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int e = maxerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            int ix = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            for (int j = 0; j < 256; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                if (error[j] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                int t = red - reds[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                int d = t * t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                t = green - greens[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                d += t * t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                t = blue - blues[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                d += t * t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (d < e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    e = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    ix = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (ix >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (e < error[ix]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    if (cmap2sys[ix] >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        // To be fair we will not accept any matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        // looser than this former match that we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        // displaced with a better match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        if (maxerror > error[ix]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            maxerror = error[ix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        sys2cmap[cmap2sys[ix]] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    error[ix] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    sys2cmap[i] = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    cmap2sys[ix] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        nummapped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (error[i] >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (error[i] >= maxerror) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    // Throw this one back to be fair to a displaced entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    error[i] = BIGERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    sys2cmap[cmap2sys[i]] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    cmap2sys[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                error[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                nummapped++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        totalmapped += nummapped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (makecube_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            fprintf(stderr, "%3d colors mapped (%3d total), maxerror = %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    nummapped, totalmapped, maxerror);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    } while (nummapped != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (cmap2sys[i] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            for (int j = 0; j < 256; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (sys2cmap[j] < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    cmap2sys[i] = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    sys2cmap[j] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (j == 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                usage("Internal error: no unused system entry for cmap entry!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (i < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            sys2cmap[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            cmap2sys[i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else if (i < 20) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            sys2cmap[256 - 20 + i] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            cmap2sys[i] = 256 - 20 + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            sys2cmap[i - 10] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            cmap2sys[i] = i - 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    if (makecube_verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        fprintf(stderr, "cmap2sys mapping: \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            fprintf(stderr, "%4d", cmap2sys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (sys2cmap[cmap2sys[i]] != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                usage("Internal error: bad system palette back pointer!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        fprintf(stderr, "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    printf("unsigned char awt_reds[256] = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if ((i & 0xf) == 0) printf("\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        printf("%3d,", reds[sys2cmap[i]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    printf("\n};\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    printf("unsigned char awt_greens[256] = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if ((i & 0xf) == 0) printf("\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        printf("%3d,", greens[sys2cmap[i]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    printf("\n};\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    printf("unsigned char awt_blues[256] = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    for (i = 0; i < 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if ((i & 0xf) == 0) printf("\n\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        printf("%3d,", blues[sys2cmap[i]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    printf("\n};\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    fflush(stdout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
void printPalette(char *label, HPALETTE hPal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    PALETTEENTRY palEntries[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    fprintf(stderr, "%s (0x%08x):\n", label, hPal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    int n = GetPaletteEntries(hPal, 0, 256, palEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        fprintf(stderr, "palEntries[%3d] = (%3d, %3d, %3d), flags = %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                palEntries[i].peRed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                palEntries[i].peGreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                palEntries[i].peBlue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                palEntries[i].peFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
/* This helps eliminate any dependence on javai.dll at build time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
jio_fprintf (FILE *handle, const char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    va_start(args, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    len = vfprintf(handle, format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
}