DitherOp.java 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/*
 * Decompiled with CFR 0_118.
 */
package com.day.image;

import com.day.image.AbstractBufferedImageOp;
import com.day.image.ImageSupport;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.util.Hashtable;

public class DitherOp
extends AbstractBufferedImageOp {
    public static final DitherAlgorithm DITHER_NONE = new DitherSimple();
    public static final DitherAlgorithm DITHER_SIMPLE_ERROR_CORRECTION = new DitherErrorCorrection();
    private final int nCol;
    private final Color transparency;
    private final Color bgcolor;
    private final DitherAlgorithm algorithm;

    public DitherOp(int nCol, Color transparency, Color bgcolor, DitherAlgorithm algorithm, RenderingHints hints) {
        super(hints);
        if (algorithm == null) {
            throw new NullPointerException("algorithm");
        }
        if (nCol < 2) {
            throw new IllegalArgumentException("nCol < 2");
        }
        this.nCol = nCol;
        this.transparency = transparency;
        this.bgcolor = bgcolor;
        this.algorithm = algorithm;
    }

    public static BufferedImage convertToIndexed(BufferedImage src, int nCol, Color transparency, Color bgcolor, RenderingHints hints) {
        if (src == null) {
            throw new NullPointerException("src image is null");
        }
        ColorModel srcCM = src.getColorModel();
        if (srcCM instanceof IndexColorModel) {
            IndexColorModel icm = (IndexColorModel)srcCM;
            if (icm.getMapSize() <= nCol) {
                return src;
            }
            src = icm.convertToIntDiscrete(src.getRaster(), true);
        }
        src = ImageSupport.coerceData(src, true);
        boolean doTransparency = transparency != null && nCol > 2;
        int transpOffset = doTransparency ? 1 : 0;
        DitherOp dither = new DitherOp(nCol - transpOffset, transparency, bgcolor, DITHER_NONE, hints);
        WritableRaster srcRas = src.getRaster();
        dither.prepareSourceImage(src);
        OctTree tree = dither.buildColorTree(srcRas);
        int w = src.getWidth();
        int h = src.getHeight();
        dither.indexColorTree(tree, false);
        int size = tree.leafCount + transpOffset;
        byte[] cmap = tree.createColorMap(new byte[size * 3]);
        int bits = 0;
        int ts = size;
        while (ts > 0) {
            ts >>= 1;
            ++bits;
        }
        int transparentIndex = size - 1;
        if (doTransparency) {
            OctTreeNode node = tree.getNode(transparency.getRed(), transparency.getGreen(), transparency.getBlue());
            node.nofcolors = transparentIndex;
        }
        IndexColorModel cm = new IndexColorModel(bits, size, cmap, 0, false, transparentIndex);
        WritableRaster dstRas = cm.createCompatibleWritableRaster(w, h);
        BufferedImage dst = new BufferedImage(cm, dstRas, cm.isAlphaPremultiplied(), null);
        int bands = srcRas.getNumBands();
        int[] srcChunk = new int[bands * w];
        int[] dstChunk = new int[w];
        for (int y = 0; y < h; ++y) {
            srcRas.getPixels(0, y, w, 1, srcChunk);
            int x = srcChunk.length - bands;
            int i = dstChunk.length - 1;
            while (x >= 0) {
                if (srcChunk[x + 3] == 0 && doTransparency) {
                    dstChunk[i] = transparentIndex;
                } else {
                    OctTreeNode node = tree.getNode(srcChunk[x], srcChunk[x + 1], srcChunk[x + 2]);
                    dstChunk[i] = node.nofcolors;
                }
                x -= bands;
                --i;
            }
            dstRas.setPixels(0, y, w, 1, dstChunk);
        }
        return dst;
    }

    public BufferedImage filter(BufferedImage src, BufferedImage dst) {
        src = ImageSupport.coerceData(src, true);
        return super.filter(src, dst);
    }

    protected void doFilter(BufferedImage srcIm, BufferedImage dstIm) {
        WritableRaster srcRas = srcIm.getRaster();
        WritableRaster dstRas = dstIm.getRaster();
        this.prepareSourceImage(srcIm);
        OctTree tree = this.buildColorTree(srcRas);
        if (!tree.isReduced()) {
            dstIm.getGraphics().drawImage(srcIm, 0, 0, null);
        }
        this.indexColorTree(tree, false);
        this.algorithm.dither(srcRas, dstRas, tree);
    }

    private void prepareSourceImage(BufferedImage srcIm) {
        if (this.bgcolor != null) {
            Graphics2D g2 = srcIm.createGraphics();
            g2.setComposite(AlphaComposite.DstOver);
            Color col = this.bgcolor.getAlpha() != 0 ? new Color(this.bgcolor.getRGB() & 16777215, true) : this.bgcolor;
            g2.setColor(col);
            g2.fillRect(0, 0, srcIm.getWidth(), srcIm.getHeight());
            g2.dispose();
        }
    }

    private OctTree buildColorTree(Raster srcRas) {
        int w = srcRas.getWidth();
        int h = srcRas.getHeight();
        int size = w * h;
        int bands = srcRas.getNumBands();
        int[] chunk = new int[bands * w];
        OctTree tree = new OctTree();
        for (int y = 0; y < h; ++y) {
            srcRas.getPixels(0, y, w, 1, chunk);
            for (int x = chunk.length - bands; x >= 0; x -= bands) {
                tree.insertNode(chunk[x], chunk[x + 1], chunk[x + 2], 1);
            }
            while (tree.leafCount > this.nCol) {
                tree.reduceNode();
            }
        }
        if (this.transparency != null) {
            tree.insertNode(this.transparency.getRed(), this.transparency.getGreen(), this.transparency.getBlue(), size);
        }
        if (this.bgcolor != null && !this.bgcolor.equals(this.transparency)) {
            tree.insertNode(this.bgcolor.getRed(), this.bgcolor.getGreen(), this.bgcolor.getBlue(), size);
        }
        while (tree.leafCount > this.nCol) {
            tree.reduceNode();
        }
        return tree;
    }

    private byte[] indexColorTree(OctTree tree, boolean createCMap) {
        tree.indexTree();
        if (this.transparency != null) {
            tree.getNode((int)this.transparency.getRed(), (int)this.transparency.getGreen(), (int)this.transparency.getBlue()).a = 0;
        }
        return createCMap ? tree.createColorMap(null) : null;
    }

    private static class OctTree {
        private static final int MAX_DEPTH = 8;
        private int leafCount;
        private final OctTreeNode root = new OctTreeNode(null);
        private final OctTreeNode[] redtable = new OctTreeNode[9];
        private final int[] rednof = new int[9];
        private int reddepth;

        public OctTree() {
            for (int i = 0; i < this.redtable.length; ++i) {
                this.redtable[i] = new OctTreeNode(null);
            }
        }

        public void indexTree() {
            this.root.indexTree();
        }

        public OctTreeNode getNode(int r, int g, int b) {
            OctTreeNode node = this.root;
            int m = 256;
            while (node != null) {
                if (node.leaf) {
                    return node;
                }
                int c = 0;
                if ((r & (m >>= 1)) != 0) {
                    c = (short)(c | 4);
                }
                if ((g & m) != 0) {
                    c = (short)(c | 2);
                }
                if ((b & m) != 0) {
                    c = (short)(c + 1);
                }
                node = node.childs[c];
            }
            return null;
        }

        public void insertNode(int r, int g, int b, int count) {
            OctTreeNode node = this.root;
            int m = 256;
            while (node != null) {
                if (node.leaf) {
                    node.nofcolors += count;
                    node.sr += r * count;
                    node.sg += g * count;
                    node.sb += b * count;
                    break;
                }
                int c = 0;
                if ((r & (m >>= 1)) != 0) {
                    c |= 4;
                }
                if ((g & m) != 0) {
                    c |= 2;
                }
                if ((b & m) != 0) {
                    c |= 1;
                }
                if (node.childs[c] == null) {
                    node = node.childs[c] = new OctTreeNode(node);
                    if (m != 1) continue;
                    node.leaf = true;
                    ++this.leafCount;
                    this.insertNode(this.redtable[8], node);
                    int[] arrn = this.rednof;
                    arrn[8] = arrn[8] + 1;
                    this.reddepth = 8;
                    continue;
                }
                node = node.childs[c];
            }
        }

        public boolean isReduced() {
            for (int i = 7; i >= 0; --i) {
                if (this.rednof[i] == 0) continue;
                return true;
            }
            return false;
        }

        public void reduceNode() {
            OctTreeNode node = this.getReducibleNode();
            int sr = 0;
            int sg = 0;
            int sb = 0;
            int cc = 0;
            int c = 0;
            for (int i = 0; i < 8; ++i) {
                OctTreeNode child = node.childs[i];
                if (child == null) continue;
                ++c;
                sr += child.sr;
                sg += child.sg;
                sb += child.sb;
                cc += child.nofcolors;
                this.deleteNode(this.redtable[child.depth], child);
                int[] arrn = this.rednof;
                int n = child.depth;
                arrn[n] = arrn[n] - 1;
                node.childs[i] = null;
            }
            node.leaf = true;
            node.nofcolors = cc;
            node.sr = sr;
            node.sg = sg;
            node.sb = sb;
            int d = node.depth;
            this.insertNode(this.redtable[d], node);
            int[] arrn = this.rednof;
            int n = d;
            arrn[n] = arrn[n] + 1;
            if (this.rednof[d + 1] == 0) {
                --this.reddepth;
            }
            this.leafCount -= c - 1;
        }

        public byte[] createColorMap(byte[] cmap) {
            int mapSize = this.leafCount * 3;
            if (cmap == null || cmap.length < mapSize) {
                cmap = new byte[mapSize];
            }
            int j = 0;
            for (int i = this.redtable.length - 1; i >= 0; --i) {
                OctTreeNode n = this.redtable[i].next;
                while (n != null) {
                    n.nofcolors = j / 3;
                    cmap[j++] = (byte)(n.r & 255);
                    cmap[j++] = (byte)(n.g & 255);
                    cmap[j++] = (byte)(n.b & 255);
                    n = n.next;
                }
            }
            return cmap;
        }

        private void deleteNode(OctTreeNode root, OctTreeNode node) {
            OctTreeNode p = root;
            OctTreeNode q = root.next;
            while (q != null && q != node) {
                p = q;
                q = q.next;
            }
            if (q == node) {
                p.next = node.next;
                node.next = null;
            }
        }

        private void insertNode(OctTreeNode root, OctTreeNode node) {
            OctTreeNode p = root;
            OctTreeNode q = p.next;
            while (q != null) {
                p = q;
                q = q.next;
            }
            p.next = node;
            node.next = null;
        }

        private OctTreeNode getReducibleNode() {
            int m = Integer.MAX_VALUE;
            OctTreeNode n = this.redtable[this.reddepth].next;
            OctTreeNode bn = null;
            while (n != null) {
                if (n.nofcolors < m) {
                    m = n.nofcolors;
                    bn = n;
                }
                n = n.next;
            }
            return bn != null ? bn.parent : null;
        }
    }

    private static final class OctTreeNode {
        public int sr;
        public int sg;
        public int sb;
        public int r;
        public int g;
        public int b;
        public int a = 255;
        public OctTreeNode[] childs = new OctTreeNode[8];
        public OctTreeNode parent;
        public boolean leaf;
        public int nofcolors;
        public int depth;
        public OctTreeNode next;

        private OctTreeNode(OctTreeNode parent) {
            this.depth = parent != null ? parent.depth + 1 : 0;
            this.parent = parent;
            this.leaf = false;
            this.next = null;
        }

        public void indexTree() {
            if (this.leaf) {
                int roundoff = this.nofcolors >> 1;
                this.r = (this.sr + roundoff) / this.nofcolors;
                this.g = (this.sg + roundoff) / this.nofcolors;
                this.b = (this.sb + roundoff) / this.nofcolors;
            } else {
                for (int i = 0; i < 8; ++i) {
                    if (this.childs[i] == null) continue;
                    this.childs[i].indexTree();
                }
            }
        }
    }

    private static class DitherErrorCorrection
    implements DitherAlgorithm {
        private DitherErrorCorrection() {
        }

        public void dither(Raster src, WritableRaster dst, OctTree tree) {
        }
    }

    private static class DitherSimple
    implements DitherAlgorithm {
        private DitherSimple() {
        }

        public void dither(Raster src, WritableRaster dst, OctTree tree) {
            int w = src.getWidth();
            int h = src.getHeight();
            int bands = src.getNumBands();
            int[] chunk = new int[bands * w];
            for (int y = 0; y < h; ++y) {
                src.getPixels(0, y, w, 1, chunk);
                for (int x = chunk.length - bands; x >= 0; x -= bands) {
                    OctTreeNode node = tree.getNode(chunk[x], chunk[x + 1], chunk[x + 2]);
                    chunk[x] = node.r;
                    chunk[x + 1] = node.g;
                    chunk[x + 2] = node.b;
                    chunk[x + 3] = node.a;
                }
                dst.setPixels(0, y, w, 1, chunk);
            }
        }
    }

    public static interface DitherAlgorithm {
        public void dither(Raster var1, WritableRaster var2, OctTree var3);
    }

}