1/*
2 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * This file implements some of the standard utility procedures used
28 * by the image conversion package.
29 */
30
31#include "img_globals.h"
32
33#include "java_awt_image_IndexColorModel.h"
34#include "java_awt_Transparency.h"
35
36/*
37 * This function constructs an 8x8 ordered dither array which can be
38 * used to dither data into an output range with discreet values that
39 * differ by the value specified as quantum.  A monochrome screen would
40 * use a dither array constructed with the quantum 256.
41 * The array values produced are unsigned and intended to be used with
42 * a lookup table which returns the next color darker than the error
43 * adjusted color used as the index.
44 */
45void
46make_uns_ordered_dither_array(uns_ordered_dither_array oda,
47                              int quantum)
48{
49    int i, j, k;
50
51    oda[0][0] = 0;
52    for (k = 1; k < 8; k *= 2) {
53        for (i = 0; i < k; i++) {
54            for (j = 0; j < k; j++) {
55                oda[ i ][ j ] = oda[i][j] * 4;
56                oda[i+k][j+k] = oda[i][j] + 1;
57                oda[ i ][j+k] = oda[i][j] + 2;
58                oda[i+k][ j ] = oda[i][j] + 3;
59            }
60        }
61    }
62    for (i = 0; i < 8; i++) {
63        for (j = 0; j < 8; j++) {
64            oda[i][j] = oda[i][j] * quantum / 64;
65        }
66    }
67}
68
69/*
70 * This function constructs an 8x8 ordered dither array which can be
71 * used to dither data into an output range with discreet values that
72 * are distributed over the range from minerr to maxerr around a given
73 * target color value.
74 * The array values produced are signed and intended to be used with
75 * a lookup table which returns the closest color to the error adjusted
76 * color used as an index.
77 */
78void
79make_sgn_ordered_dither_array(char* oda, int minerr, int maxerr)
80{
81    int i, j, k;
82
83    oda[0] = 0;
84    for (k = 1; k < 8; k *= 2) {
85        for (i = 0; i < k; i++) {
86            for (j = 0; j < k; j++) {
87                oda[(i<<3) + j] = oda[(i<<3)+j] * 4;
88                oda[((i+k)<<3) + j+k] = oda[(i<<3)+j] + 1;
89                oda[(i<<3) + j+k] = oda[(i<<3)+j] + 2;
90                oda[((i+k)<<3) + j] = oda[(i<<3)+j] + 3;
91            }
92        }
93    }
94    k = 0;
95    for (i = 0; i < 8; i++) {
96        for (j = 0; j < 8; j++) {
97            oda[k] = oda[k] * (maxerr - minerr) / 64 + minerr;
98            k++;
99        }
100    }
101}
102
103#ifdef TESTING
104#include <stdio.h>
105
106/* Function to test the ordered dither error matrix initialization function. */
107main(int argc, char **argv)
108{
109    int i, j;
110    int quantum;
111    int max, val;
112    uns_ordered_dither_array oda;
113
114    if (argc > 1) {
115        quantum = atoi(argv[1]);
116    } else {
117        quantum = 64;
118    }
119    make_uns_ordered_dither_array(oda, quantum);
120    for (i = 0; i < 8; i++) {
121        for (j = 0; j < 8; j++) {
122            val = oda[i][j];
123            printf("%4d", val);
124            if (max < val) {
125                max = val;
126            }
127        }
128        printf("\n");
129    }
130    printf("\nmax = %d\n", max);
131}
132#endif /* TESTING */
133