1 /*
   2  * Copyright (c) 1996, 2018, 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 "jni.h"
  32 #include "img_globals.h"
  33 
  34 #include "java_awt_image_IndexColorModel.h"
  35 #include "java_awt_Transparency.h"
  36 
  37 /*
  38  * This function constructs an 8x8 ordered dither array which can be
  39  * used to dither data into an output range with discreet values that
  40  * differ by the value specified as quantum.  A monochrome screen would
  41  * use a dither array constructed with the quantum 256.
  42  * The array values produced are unsigned and intended to be used with
  43  * a lookup table which returns the next color darker than the error
  44  * adjusted color used as the index.
  45  */
  46 JNIEXPORT void JNICALL
  47 make_uns_ordered_dither_array(uns_ordered_dither_array oda,
  48                               int quantum)
  49 {
  50     int i, j, k;
  51 
  52     oda[0][0] = 0;
  53     for (k = 1; k < 8; k *= 2) {
  54         for (i = 0; i < k; i++) {
  55             for (j = 0; j < k; j++) {
  56                 oda[ i ][ j ] = oda[i][j] * 4;
  57                 oda[i+k][j+k] = oda[i][j] + 1;
  58                 oda[ i ][j+k] = oda[i][j] + 2;
  59                 oda[i+k][ j ] = oda[i][j] + 3;
  60             }
  61         }
  62     }
  63     for (i = 0; i < 8; i++) {
  64         for (j = 0; j < 8; j++) {
  65             oda[i][j] = oda[i][j] * quantum / 64;
  66         }
  67     }
  68 }
  69 
  70 /*
  71  * This function constructs an 8x8 ordered dither array which can be
  72  * used to dither data into an output range with discreet values that
  73  * are distributed over the range from minerr to maxerr around a given
  74  * target color value.
  75  * The array values produced are signed and intended to be used with
  76  * a lookup table which returns the closest color to the error adjusted
  77  * color used as an index.
  78  */
  79 void
  80 make_sgn_ordered_dither_array(char* oda, int minerr, int maxerr)
  81 {
  82     int i, j, k;
  83 
  84     oda[0] = 0;
  85     for (k = 1; k < 8; k *= 2) {
  86         for (i = 0; i < k; i++) {
  87             for (j = 0; j < k; j++) {
  88                 oda[(i<<3) + j] = oda[(i<<3)+j] * 4;
  89                 oda[((i+k)<<3) + j+k] = oda[(i<<3)+j] + 1;
  90                 oda[(i<<3) + j+k] = oda[(i<<3)+j] + 2;
  91                 oda[((i+k)<<3) + j] = oda[(i<<3)+j] + 3;
  92             }
  93         }
  94     }
  95     k = 0;
  96     for (i = 0; i < 8; i++) {
  97         for (j = 0; j < 8; j++) {
  98             oda[k] = oda[k] * (maxerr - minerr) / 64 + minerr;
  99             k++;
 100         }
 101     }
 102 }
 103 
 104 #ifdef TESTING
 105 #include <stdio.h>
 106 
 107 /* Function to test the ordered dither error matrix initialization function. */
 108 main(int argc, char **argv)
 109 {
 110     int i, j;
 111     int quantum;
 112     int max, val;
 113     uns_ordered_dither_array oda;
 114 
 115     if (argc > 1) {
 116         quantum = atoi(argv[1]);
 117     } else {
 118         quantum = 64;
 119     }
 120     make_uns_ordered_dither_array(oda, quantum);
 121     for (i = 0; i < 8; i++) {
 122         for (j = 0; j < 8; j++) {
 123             val = oda[i][j];
 124             printf("%4d", val);
 125             if (max < val) {
 126                 max = val;
 127             }
 128         }
 129         printf("\n");
 130     }
 131     printf("\nmax = %d\n", max);
 132 }
 133 #endif /* TESTING */