Layer.java 53.6 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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
/*
 * Decompiled with CFR 0_118.
 */
package com.day.image;

import com.day.image.ColorCurve;
import com.day.image.DistortOp;
import com.day.image.DitherOp;
import com.day.image.EmbossOp;
import com.day.image.ImageSupport;
import com.day.image.LineStyle;
import com.day.image.MultitoneOp;
import com.day.image.ResizeOp;
import com.day.image.font.AbstractFont;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BandCombineOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;
import java.awt.image.ConvolveOp;
import java.awt.image.ImageObserver;
import java.awt.image.IndexColorModel;
import java.awt.image.Kernel;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.RescaleOp;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.imageio.IIOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class Layer {
    public static final LuminanceSystem GAMMA22;
    public static final LuminanceSystem LINEAR;
    public static final LuminanceSystem REC709;
    public static final String DEFAULT_MIME_TYPE = "image/gif";
    public static final int RED_CHANNEL_ID = 0;
    public static final int GREEN_CHANNEL_ID = 1;
    public static final int BLUE_CHANNEL_ID = 2;
    public static final int ALPHA_CHANNEL_ID = 3;
    private static final int DEFAULT_IMAGE_ORIGINX = 0;
    private static final int DEFAULT_IMAGE_ORIGINY = 0;
    private static final int DEFAULT_IMAGE_WIDTH = 1;
    private static final int DEFAULT_IMAGE_HEIGHT = 1;
    protected static final Color TRANSPARENT_IMAGE_BACKGROUND;
    private static final float DEFAULT_IMAGE_OPACITY = 1.0f;
    private static final Composite DEFAULT_LAYER_COMPOSITE;
    static final int IMAGE_TYPE = 2;
    private static final AffineTransform IDENTITY_XFORM;
    private static Map RENDERING_HINTS;
    private static final int MAX_SUBSAMPLING_SIZE = 1280;
    private boolean baseImgIsRGBA;
    private BufferedImage baseImg;
    private Graphics2D g2;
    private int x;
    private int y;
    private int width;
    private int height;
    private Paint backGround;
    private Color bgColor;
    private float opacity;
    private Color transparency;
    private String mimeType;
    private LuminanceSystem lumSys = GAMMA22;
    private Composite layerComposite;
    private int imageIndex;
    private int numImages;
    private static final int RGBA_BLUR_KERNEL_MAX = 256;
    private static final int RGBA_BLUR_KERNEL_HALF = 128;

    public Layer(int width, int height, Paint bground) {
        this.init(width, height, bground);
    }

    public Layer(InputStream input) throws IOException, IIOException {
        this(input, 0, null);
    }

    public Layer(InputStream input, Dimension max) throws IOException, IIOException {
        this(input, 0, max);
    }

    public Layer(InputStream input, int idx) throws IOException, IIOException {
        this(input, idx, null);
    }

    public Layer(InputStream input, int idx, Dimension max) throws IOException, IIOException {
        this(input, idx, max, null);
    }

    public Layer(InputStream input, int idx, Dimension max, ImageReadParam params) throws IOException, IIOException {
        ImageInputStream ios = null;
        ImageReader reader = null;
        boolean inputWrapped = false;
        try {
            if (!input.markSupported()) {
                input = new BufferedInputStream(input, 1024){

                    public void close() {
                        if (this.in == null) {
                            return;
                        }
                        this.in = null;
                        this.buf = null;
                    }
                };
                inputWrapped = true;
            }
            input.mark(1024);
            ios = ImageIO.createImageInputStream(input);
            Iterator<ImageReader> readers = ImageIO.getImageReaders(ios);
            while (readers.hasNext()) {
                reader = readers.next();
                reader.setInput(ios, true);
                if (params == null) {
                    params = reader.getDefaultReadParam();
                }
                IOException imageReadFailure = null;
                if (max != null) {
                    max = new Dimension(max);
                    ios.mark();
                    int samplefactor = 0;
                    try {
                        samplefactor = Layer.calculateSampleFactor(max, reader.getWidth(idx), reader.getHeight(idx));
                    }
                    catch (IOException e) {
                        imageReadFailure = e;
                    }
                    try {
                        ios.reset();
                    }
                    catch (IOException ie) {
                        // empty catch block
                    }
                    if (samplefactor > 1) {
                        params.setSourceSubsampling(samplefactor, samplefactor, 0, 0);
                    }
                }
                BufferedImage fromInput = null;
                if (imageReadFailure == null) {
                    try {
                        ios.mark();
                        fromInput = reader.read(idx, params);
                    }
                    catch (IOException e) {
                        imageReadFailure = e;
                    }
                }
                if (imageReadFailure != null) {
                    if (readers.hasNext()) {
                        reader.dispose();
                        ios.reset();
                        continue;
                    }
                    throw imageReadFailure;
                }
                if (fromInput == null) {
                    throw new IllegalStateException("Unexpected missing image");
                }
                this.imageIndex = idx;
                ColorModel cm = fromInput.getColorModel();
                if (cm instanceof IndexColorModel) {
                    IndexColorModel icm = (IndexColorModel)cm;
                    if (max != null) {
                        this.init(max.width, max.height, TRANSPARENT_IMAGE_BACKGROUND);
                        BufferedImage bm = icm.convertToIntDiscrete(fromInput.getRaster(), true);
                        ResizeOp.doFilter_progressive(bm, this.baseImg);
                        bm.flush();
                    } else {
                        this.init(1, 1, TRANSPARENT_IMAGE_BACKGROUND);
                        this.setImage(icm.convertToIntDiscrete(fromInput.getRaster(), true));
                    }
                } else if (max != null) {
                    this.init(max.width, max.height, TRANSPARENT_IMAGE_BACKGROUND);
                    ResizeOp.doFilter_progressive(fromInput, this.baseImg);
                } else {
                    this.init(1, 1, TRANSPARENT_IMAGE_BACKGROUND);
                    this.setImage(fromInput);
                    fromInput = null;
                }
                this.mimeType = reader.getOriginatingProvider().getMIMETypes()[0];
                if (this.mimeType.toLowerCase().endsWith("gif")) {
                    ImageSupport.getGIFMetaData(this, reader);
                }
                if (fromInput != null) {
                    fromInput.flush();
                }
                this.numImages = this.imageIndex + 1;
                do {
                    try {
                        reader.read(this.numImages).flush();
                    }
                    catch (IndexOutOfBoundsException ioo) {
                        break;
                    }
                    catch (Throwable t) {
                        break;
                    }
                    ++this.numImages;
                } while (true);
                return;
            }
            try {
                throw new IIOException("No decoder available to load the image");
            }
            catch (OutOfMemoryError oome) {
                throw new IIOException("Not enough memory to load the image");
            }
        }
        finally {
            if (reader != null) {
                reader.dispose();
            }
            try {
                if (ios != null) {
                    ios.close();
                }
            }
            catch (IOException ignore) {}
            if (inputWrapped) {
                try {
                    input.close();
                }
                catch (IOException ignore) {}
            }
        }
    }

    protected static int calculateSampleFactor(Dimension max, int w, int h) {
        int tw = w;
        int th = h;
        if (max.width > 0 && max.width < tw) {
            th = h * max.width / w;
            tw = max.width;
        }
        if (max.height > 0 && max.height < th) {
            tw = w * max.height / h;
            th = max.height;
        }
        max.width = tw;
        max.height = th;
        if (tw > th && tw < 1280) {
            tw = 1280;
        }
        if (th > tw && th < 1280) {
            th = 1280;
        }
        return Math.min(w / tw, h / th);
    }

    public Layer(Layer src) {
        this.init(src.width, src.height, src.backGround);
        this.x = src.x;
        this.y = src.y;
        this.opacity = src.opacity;
        this.transparency = src.transparency;
        this.mimeType = src.mimeType;
        this.layerComposite = src.layerComposite;
        this.imageIndex = src.imageIndex;
        this.numImages = src.numImages;
        this.g2.drawRenderedImage(src.getImage(), IDENTITY_XFORM);
    }

    public Layer(BufferedImage image) {
        if (image == null) {
            throw new NullPointerException("image");
        }
        this.setImage(image, false);
        this.x = 0;
        this.y = 0;
        this.bgColor = TRANSPARENT_IMAGE_BACKGROUND;
        this.backGround = this.bgColor;
        this.opacity = 1.0f;
        this.transparency = null;
        this.mimeType = "image/gif";
        this.layerComposite = DEFAULT_LAYER_COMPOSITE;
    }

    /*
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    public boolean write(String mimeType, double quality, OutputStream outStream) throws IIOException, IOException {
        IIOMetadata imageMetadata;
        BufferedImage writableImage;
        JPEGImageWriteParam iwp;
        ImageWriter writer;
        int targetImageType;
        IIOMetadata streamMetadata222;
        block37 : {
            ImageTypeSpecifier its;
            block38 : {
                if (mimeType == null || mimeType.length() == 0) {
                    mimeType = this.mimeType;
                }
                if (mimeType == null || mimeType.length() == 0) {
                    mimeType = "image/gif";
                }
                String format = mimeType.substring(mimeType.indexOf(47) + 1);
                writer = null;
                ImageOutputStream ios = null;
                try {
                    writer = ImageSupport.getImageWriter(format);
                    if (writer != null) {
                        ios = ImageIO.createImageOutputStream(outStream);
                        writer.setOutput(ios);
                        streamMetadata222 = null;
                        imageMetadata = null;
                        iwp = null;
                        targetImageType = this.getImage().getType();
                        if ("gif".equalsIgnoreCase(format)) {
                            IIOMetadata[] gifMeta = ImageSupport.createGIFMetadata(this, writer, (int)quality);
                            targetImageType = this.getImage().getType();
                            if (gifMeta[0] != null) {
                                streamMetadata222 = gifMeta[0];
                            }
                            if (gifMeta[1] != null) {
                                imageMetadata = gifMeta[1];
                            }
                        } else if ("jpg".equalsIgnoreCase(format) || "jpeg".equalsIgnoreCase(format)) {
                            if (quality < 0.0 || quality > 1.0) {
                                quality = 0.82;
                            }
                            iwp = new JPEGImageWriteParam(null);
                            iwp.setCompressionMode(2);
                            iwp.setCompressionQuality((float)quality);
                            targetImageType = 1;
                        } else if (this.transparency != null) {
                            long trans = this.transparency.getRGB();
                            this.replaceColor(trans, trans & 0xFFFFFF, false);
                        }
                        its = ImageTypeSpecifier.createFromRenderedImage(this.getImage());
                        if (writer.getOriginatingProvider().canEncodeImage(its)) break block37;
                        break block38;
                    }
                    boolean streamMetadata222 = false;
                    return streamMetadata222;
                }
                catch (IllegalArgumentException iae) {
                    boolean ignore = false;
                    return ignore;
                }
                catch (IOException ioe) {
                    Throwable throwable222;
                    boolean ignore = false;
                    return ignore;
                    {
                        catch (Throwable throwable222) {}
                    }
                    catch (OutOfMemoryError oome) {
                        throw new IIOException("Not enough memory to store the image");
                        throw throwable222;
                        finally {
                            if (writer != null) {
                                writer.dispose();
                            }
                            if (ios != null) {
                                try {
                                    ios.close();
                                }
                                catch (IOException ignore) {}
                            }
                        }
                    }
                }
            }
            for (int bit = 1; bit <= 13; ++bit) {
                its = ImageTypeSpecifier.createFromBufferedImageType(bit);
                if (!writer.getOriginatingProvider().canEncodeImage(its)) continue;
                targetImageType = bit;
                break;
            }
        }
        if (targetImageType != this.getImage().getType()) {
            boolean sourceHasAlpha = this.getImage().getColorModel().hasAlpha();
            boolean targetSupportsAlpha = ImageTypeSpecifier.createFromBufferedImageType(targetImageType).getColorModel().hasAlpha();
            writableImage = new BufferedImage(this.width, this.height, targetImageType);
            Graphics2D g2d = writableImage.createGraphics();
            if (sourceHasAlpha && !targetSupportsAlpha) {
                g2d.drawImage(this.getImage(), 0, 0, this.getBackgroundColor(), null);
            } else {
                g2d.drawRenderedImage(this.getImage(), IDENTITY_XFORM);
            }
        } else {
            writableImage = this.getImage();
        }
        if (!writer.getOriginatingProvider().canEncodeImage(writableImage)) return true;
        IIOImage image = new IIOImage(writableImage, null, imageMetadata);
        writer.write(streamMetadata222, image, iwp);
        return true;
    }

    public void dispose() {
        if (this.g2 != null) {
            this.g2.dispose();
            this.g2 = null;
        }
        if (this.baseImg != null) {
            this.baseImg.flush();
            this.baseImg = null;
        }
        this.backGround = null;
        this.bgColor = null;
        this.lumSys = null;
        this.mimeType = null;
        this.transparency = null;
    }

    public void merge(Layer layer) {
        if (layer != null) {
            this.merge(new Layer[]{layer});
        }
    }

    public void merge(Layer[] layers) {
        if (layers == null || layers.length == 0) {
            return;
        }
        int newX = this.x;
        int newY = this.y;
        int newR = this.x + this.width;
        int newB = this.y + this.height;
        for (int i = 0; i < layers.length; ++i) {
            Layer l = layers[i];
            int r = l.x + l.width;
            int b = l.y + l.height;
            if (newX > l.x) {
                newX = l.x;
            }
            if (newY > l.y) {
                newY = l.y;
            }
            if (newR < r) {
                newR = r;
            }
            if (newB >= b) continue;
            newB = b;
        }
        int newW = newR - newX;
        int newH = newB - newY;
        if (newX != this.x || newY != this.y || newW != this.width || newH != this.height) {
            BufferedImage oldImage = this.getImage();
            Graphics2D oldG2 = this.getG2();
            Composite oldComposite = oldG2.getComposite();
            Paint oldPaint = this.g2.getPaint();
            Stroke oldStroke = this.g2.getStroke();
            AffineTransform oldAffineTransform = this.g2.getTransform();
            BufferedImage newImage = new BufferedImage(newW, newH, 2);
            Graphics2D newG2 = newImage.createGraphics();
            newG2.setPaint(this.backGround);
            newG2.fillRect(0, 0, newW, newH);
            newG2.drawRenderedImage(oldImage, AffineTransform.getTranslateInstance(this.x - newX, this.y - newY));
            newG2.dispose();
            this.setImage(newImage);
            this.getG2().setComposite(oldComposite);
            this.getG2().setPaint(oldPaint);
            this.getG2().setStroke(oldStroke);
            this.getG2().setTransform(oldAffineTransform);
            this.x = newX;
            this.y = newY;
        }
        Composite oldComposite = this.g2.getComposite();
        for (int i2 = 0; i2 < layers.length; ++i2) {
            float op = layers[i2].opacity;
            Composite composite = layers[i2].layerComposite;
            if ((double)op < 0.99999 && composite instanceof AlphaComposite) {
                int acRule = ((AlphaComposite)composite).getRule();
                composite = AlphaComposite.getInstance(acRule, op);
            }
            this.g2.setComposite(composite);
            this.g2.drawRenderedImage(layers[i2].getImage(), AffineTransform.getTranslateInstance(layers[i2].x - newX, layers[i2].y - newY));
        }
        this.g2.setComposite(oldComposite);
    }

    public void blit(Layer src, int dw, int dh) {
        this.blit(src, 0, 0, dw, dh, 0, 0);
    }

    public void blit(Layer src, int dx, int dy, int dw, int dh, int sx, int sy) {
        BufferedImage srcImage = src.getImage().getSubimage(sx, sy, dw, dh);
        this.g2.drawRenderedImage(srcImage, AffineTransform.getTranslateInstance(dx, dy));
    }

    public void copyChannel(Layer src, int fromChannel, int toChannel) {
        if (src == null) {
            src = this;
        }
        if (fromChannel < 0) {
            fromChannel = 3;
        }
        if (toChannel < 0) {
            toChannel = fromChannel;
        }
        if (src == this && fromChannel == toChannel) {
            return;
        }
        int w = Math.min(this.width, src.width);
        int h = Math.min(this.height, src.height);
        if (fromChannel > src.getImageRGBA().getRaster().getNumBands()) {
            throw new IndexOutOfBoundsException("fromChannel");
        }
        if (toChannel > this.getImageRGBA().getRaster().getNumBands()) {
            throw new IndexOutOfBoundsException("toChannel");
        }
        this.getImageRGBA().getRaster().setSamples(0, 0, w, h, toChannel, src.getImageRGBA().getRaster().getSamples(0, 0, w, h, fromChannel, (int[])null));
    }

    public void colorMask(Layer src, Color col) {
        if (src == null) {
            src = this;
        }
        if (col == null) {
            col = Color.black;
        }
        int w = Math.min(this.width, src.width);
        int h = Math.min(this.height, src.height);
        int color = col.getRGB();
        BufferedImage image = this.getImageRGBA();
        int[] de = (int[])image.getRaster().getDataElements(0, 0, w, h, null);
        int[] se = (int[])src.getImageRGBA().getRaster().getDataElements(0, 0, w, h, null);
        for (int i = 0; i < se.length; ++i) {
            if (se[i] != de[i]) continue;
            de[i] = color;
        }
        image.getRaster().setDataElements(0, 0, w, h, de);
    }

    public void flatten(Color color) {
        if (color == null) {
            color = this.backGround instanceof Color ? (Color)this.backGround : this.getBackgroundColor();
        }
        Paint op = this.g2.getPaint();
        Composite oc = this.g2.getComposite();
        this.g2.setPaint(color);
        this.g2.setComposite(AlphaComposite.DstOver);
        this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        this.g2.fillRect(0, 0, this.width, this.height);
        this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        this.g2.setComposite(oc);
        this.g2.setPaint(op);
    }

    public void rotate(double degrees) {
        int xmin;
        int ymax;
        int ymin;
        int xmax;
        double theta = Math.toRadians(degrees);
        double cos = Math.cos(theta);
        double sin = Math.sin(theta);
        int q = (int)(degrees / 90.0);
        if ((q & 1) == 1) {
            double y2 = (double)this.width * sin + 0.0 * cos;
            double y4 = 0.0 * sin + (double)this.height * cos;
            double x1 = 0.0;
            double x3 = (double)this.width * cos - (double)this.height * sin;
            if (q == 1) {
                ymin = (int)Math.floor(y4);
                ymax = (int)Math.ceil(y2);
                xmin = (int)Math.floor(x3);
                xmax = (int)Math.ceil(x1);
            } else {
                ymin = (int)Math.floor(y2);
                ymax = (int)Math.ceil(y4);
                xmin = (int)Math.floor(x1);
                xmax = (int)Math.ceil(x3);
            }
        } else {
            double y1 = 0.0;
            double y3 = (double)this.width * sin + (double)this.height * cos;
            double x2 = (double)this.width * cos - 0.0 * sin;
            double x4 = 0.0 * cos - (double)this.height * sin;
            if (q == 0) {
                ymin = (int)Math.floor(y1);
                ymax = (int)Math.ceil(y3);
                xmin = (int)Math.floor(x4);
                xmax = (int)Math.ceil(x2);
            } else {
                ymin = (int)Math.floor(y3);
                ymax = (int)Math.ceil(y1);
                xmin = (int)Math.floor(x2);
                xmax = (int)Math.ceil(x4);
            }
        }
        this.width = xmax - xmin;
        this.height = ymax - ymin;
        xmin = xmin < 0 ? - xmin : 0;
        ymin = ymin < 0 ? - ymin : 0;
        AffineTransform rot = new AffineTransform();
        rot.translate(xmin, ymin);
        rot.rotate(theta);
        BufferedImage newImage = new BufferedImage(this.width, this.height, 2);
        this.transformImage(newImage, rot);
    }

    public void flipHorizontally() {
        AffineTransform scale = new AffineTransform();
        scale.scale(-1.0, 1.0);
        scale.translate(- this.width, 0.0);
        BufferedImage newImage = new BufferedImage(this.width, this.height, 2);
        this.transformImage(newImage, scale);
    }

    public void flipVertically() {
        AffineTransform scale = new AffineTransform();
        scale.scale(1.0, -1.0);
        scale.translate(0.0, - this.height);
        BufferedImage newImage = new BufferedImage(this.width, this.height, 2);
        this.transformImage(newImage, scale);
    }

    public void resize(int width, int height) {
        this.resize(width, height, false);
    }

    public void resize(int width, int height, boolean fast) {
        if (width <= 0) {
            width = this.width;
        }
        if (height <= 0) {
            height = this.height;
        }
        if (width == this.width && height == this.height) {
            return;
        }
        double sx = (double)width / (double)this.width;
        double sy = (double)height / (double)this.height;
        ResizeOp op = new ResizeOp(sx, sy, this.g2.getRenderingHints());
        op.setFast(fast);
        this.setImage(op.filter(this.getImageRGBA(), null));
    }

    public void crop(Rectangle2D rect) {
        rect = this.checkRect(rect);
        BufferedImage newImage = this.getImage().getSubimage((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
        this.transformImage(newImage, null);
    }

    public void emboss(Layer bump, int azimut, int elevation, int filtersize) {
        if (bump == null) {
            bump = this;
        }
        if (azimut < 0) {
            azimut = 30;
        }
        if (elevation < 0) {
            elevation = 30;
        }
        if (filtersize < 0) {
            filtersize = 3;
        }
        EmbossOp op = new EmbossOp(bump.getImageRGBA(), azimut += 180, elevation, filtersize);
        BufferedImage newImage = op.filter(this.getImageRGBA(), null);
        this.setImage(newImage);
    }

    public void xForm(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, boolean crop) {
        float fx1 = (float)x1 / (float)this.width;
        float fx2 = (float)x2 / (float)this.width;
        float fx3 = (float)x3 / (float)this.width;
        float fx4 = (float)x4 / (float)this.width;
        float fy1 = (float)y1 / (float)this.height;
        float fy2 = (float)y2 / (float)this.height;
        float fy3 = (float)y3 / (float)this.height;
        float fy4 = (float)y4 / (float)this.height;
        float[][] coords = new float[][]{{fx1, fy1}, {fx2, fy2}, {fx3, fy3}, {fx4, fy4}};
        DistortOp bop = new DistortOp(coords, crop, this.getBackgroundColor());
        BufferedImage newImg = bop.filter(this.getImageRGBA(), null);
        this.setImage(newImg);
    }

    public void grayscale() {
        float[][] bwBopEl = new float[][]{{this.lumSys.r(), this.lumSys.g(), this.lumSys.b(), 0.0f, 0.0f}, {this.lumSys.r(), this.lumSys.g(), this.lumSys.b(), 0.0f, 0.0f}, {this.lumSys.r(), this.lumSys.g(), this.lumSys.b(), 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 1.0f, 0.0f}};
        this.setImage(ImageSupport.coerceData(this.getImageRGBA(), true));
        WritableRaster raster = this.getImageRGBA().getRaster();
        BandCombineOp bco = new BandCombineOp(bwBopEl, null);
        bco.filter(raster, raster);
    }

    public void colorize(Color darkcolor, Color brightcolor) {
        this.grayscale();
        ColorCurve[] curves = new ColorCurve[]{new ColorCurve(darkcolor, new float[]{0.0f, 1.0f}), new ColorCurve(brightcolor, new float[]{1.0f, 0.0f})};
        MultitoneOp mo = new MultitoneOp(curves, null);
        BufferedImage image = this.getImageRGBA();
        mo.filter(image, image);
    }

    public void monotone(Color color) {
        if (color == null) {
            throw new NullPointerException("color");
        }
        this.multitone(new Color[]{color});
    }

    public void multitone(Color[] colors) {
        if (colors == null) {
            throw new NullPointerException("colors");
        }
        if (colors.length == 0) {
            throw new IllegalArgumentException("empty colors");
        }
        int numcols = colors.length;
        for (int i = 0; i < numcols; ++i) {
            if (colors[i] != null) continue;
            throw new NullPointerException("colors[" + i + "]");
        }
        this.grayscale();
        MultitoneOp mo = new MultitoneOp(colors, null);
        BufferedImage image = this.getImageRGBA();
        mo.filter(image, image);
    }

    public void multitone(ColorCurve[] colorCurves) {
        this.grayscale();
        MultitoneOp mo = new MultitoneOp(colorCurves, null);
        BufferedImage image = this.getImageRGBA();
        mo.filter(image, image);
    }

    public void blur(double radius, double scale, int flags, double gran, double maxdata) {
        double tmp;
        if (radius < 0.0) {
            radius = 1.0;
        }
        if (scale < 0.0) {
            scale = 1.0;
        }
        if (flags < 0) {
            flags = 2;
        }
        if (gran < 0.0) {
            gran = 1.0;
        }
        if (maxdata < 0.0) {
            maxdata = 255.0;
        }
        double[] kField = new double[256];
        double max = 0.0;
        double delta = gran / (2.0 * maxdata);
        int kernelEdge = 0;
        for (int j = 0; j < 256; ++j) {
            tmp = (double)(j - 128) / radius;
            kField[j] = Math.exp((- tmp) * tmp / 2.0);
            max += kField[j];
        }
        int kernelsize = 255;
        for (tmp = 2.0 * (kField[kernelsize] / max); tmp < delta && kernelsize > 128; tmp += 2.0 * kField[kernelsize] / max, --kernelsize) {
            kField[kernelsize] = 0.0;
            kField[256 - kernelsize] = 0.0;
        }
        kernelEdge = 2 * kernelsize - 256;
        if (kernelEdge == 0) {
            return;
        }
        float[] kField2 = new float[kernelEdge * kernelEdge];
        int kfoff = 256 - kernelsize;
        for (int x = 0; x < kernelEdge; ++x) {
            for (int y = 0; y < kernelEdge; ++y) {
                int off = kernelEdge * y + x;
                kField2[off] = (float)(kField[x + kfoff] + kField[y + kfoff]);
                max += (double)kField2[off];
            }
        }
        max /= scale;
        int i = 0;
        while (i < kField2.length) {
            float[] arrf = kField2;
            int n = i++;
            arrf[n] = (float)((double)arrf[n] / max);
        }
        Kernel kernel = new Kernel(kernelEdge, kernelEdge, kField2);
        ConvolveOp blur = new ConvolveOp(kernel, 1, this.g2.getRenderingHints());
        BufferedImage image = this.getImageRGBA();
        this.setImage(blur.filter(image, null));
    }

    public void sharpen(float amount, float radius) {
        if (amount <= 0.0f || (double)radius < 0.5) {
            return;
        }
        if (amount > 1.0f) {
            amount = 1.0f;
        }
        if (radius > 10.0f) {
            radius = 10.0f;
        }
        int edge = 2 * (int)((double)radius + 0.5) + 1;
        float[] matrix = new float[edge * edge];
        for (int i = 0; i < matrix.length; ++i) {
            matrix[i] = -1.0f;
        }
        matrix[(edge + 1) * (edge / 2)] = (float)(matrix.length - 1) + amount;
        Kernel kernel = new Kernel(edge, edge, matrix);
        ConvolveOp sharpen = new ConvolveOp(kernel);
        this.setImage(sharpen.filter(this.getImage(), null));
    }

    public void xFormColors(double[][] matrix, double[] vector, boolean crop) {
        int i;
        float[][] bopEl = new float[4][5];
        for (i = 0; i < 4 && i < matrix.length; ++i) {
            for (int j = 0; j < 4 && j < matrix[i].length; ++j) {
                bopEl[i][j] = (float)matrix[i][j];
            }
        }
        for (i = 0; i < 4 && i < vector.length; ++i) {
            bopEl[i][4] = (float)vector[i];
        }
        BufferedImage image = this.getImageRGBA();
        new BandCombineOp(bopEl, null).filter(image.getRaster(), image.getRaster());
    }

    public void replaceColor(long color1, long color2, boolean ignoreAlpha) {
        BufferedImage image = this.getImageRGBA();
        int[] rgbArray = image.getRGB(0, 0, this.width, this.height, null, 0, this.width);
        int len = rgbArray.length;
        int c1 = (int)color1;
        int c2 = (int)color2;
        if (ignoreAlpha) {
            c1 &= 16777215;
            c2 &= 16777215;
            for (int i = 0; i < len; ++i) {
                if ((rgbArray[i] & 16777215) != c1) continue;
                rgbArray[i] = rgbArray[i] & -16777216 | c2;
            }
        } else {
            for (int i = 0; i < len; ++i) {
                if (rgbArray[i] != c1) continue;
                rgbArray[i] = c2;
            }
        }
        image.setRGB(0, 0, this.width, this.height, rgbArray, 0, this.width);
    }

    public void adjust(int brightness, float contrast) {
        if (brightness < -255) {
            brightness = -255;
        }
        if (brightness > 255) {
            brightness = 255;
        }
        if (contrast < 0.0f) {
            contrast = 0.0f;
        }
        RescaleOp rop = new RescaleOp(contrast, brightness, null);
        BufferedImage image = this.getImageRGBA();
        rop.filter(image, image);
    }

    public void reduceColors(int numColors) {
        DitherOp dither = new DitherOp(numColors, this.transparency, this.bgColor, DitherOp.DITHER_NONE, null);
        this.setImage(dither.filter(this.getImageRGBA(), null));
    }

    public void setPaint(Paint paint) {
        this.g2.setPaint(paint);
    }

    public Paint getPaint() {
        return this.g2.getPaint();
    }

    public void setStroke(Stroke stroke) {
        this.g2.setStroke(stroke);
    }

    public Stroke getStroke() {
        return this.g2.getStroke();
    }

    public void setLineStyle(LineStyle lineStyle) {
        this.g2.setPaint(lineStyle);
        this.g2.setStroke(lineStyle);
    }

    public void setComposite(Composite composite) {
        this.g2.setComposite(composite);
    }

    public Composite getComposite() {
        return this.g2.getComposite();
    }

    public void setTransform(AffineTransform transfrom) {
        this.g2.setTransform(transfrom);
    }

    public void setLuminanceSystem(LuminanceSystem system) {
        if (system != null) {
            this.lumSys = system;
        }
    }

    public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) {
        this.g2.setRenderingHint(hintKey, hintValue);
    }

    public Object getRenderingHint(RenderingHints.Key hintKey) {
        return this.g2.getRenderingHint(hintKey);
    }

    public int drawText(int x, int y, int width, int height, String text, AbstractFont font, int align, double cs, int ls) {
        return font.drawText(this, x, y, width, height, text, this.g2.getPaint(), this.g2.getStroke(), align, cs, ls);
    }

    public void fillRect(Rectangle2D rect) {
        if (rect == null) {
            this.g2.fillRect(0, 0, this.width, this.height);
        } else {
            this.g2.fill(rect);
        }
    }

    public void fillRect(Layer src, Rectangle2D rect) {
        if (src == null) {
            throw new NullPointerException("src");
        }
        if (src == this) {
            return;
        }
        TexturePaint tp = new TexturePaint(src.getImage(), new Rectangle(0, 0, src.width, src.height));
        Paint oldPaint = this.g2.getPaint();
        this.g2.setPaint(tp);
        this.fillRect(rect);
        this.g2.setPaint(oldPaint);
    }

    public void drawRect(Rectangle2D rect) {
        if (rect == null) {
            this.g2.drawRect(0, 0, this.width - 1, this.height - 1);
        } else {
            this.g2.draw(rect);
        }
    }

    public void drawLine(float x1, float y1, float x2, float y2) {
        this.g2.draw(new Line2D.Float(x1, y1, x2, y2));
    }

    public void drawPolyLine(float[][] points) {
        GeneralPath shape = new GeneralPath();
        shape.moveTo(points[0][0], points[0][1]);
        for (int i = 1; i < points.length; ++i) {
            shape.lineTo(points[i][0], points[i][1]);
        }
        this.g2.draw(shape);
    }

    public void drawEllipse(float cx, float cy, float a, float b) {
        this.g2.draw(new Ellipse2D.Double(cx - a, cy - b, a * 2.0f, b * 2.0f));
    }

    public void fillEllipse(float cx, float cy, float a, float b) {
        this.g2.fill(new Ellipse2D.Double(cx - a, cy - b, a * 2.0f, b * 2.0f));
    }

    public void drawSegment(float cx, float cy, float a, float b, double from, double extent) {
        this.g2.draw(new Arc2D.Double(cx - a, cy - b, a * 2.0f, b * 2.0f, from, extent, 0));
    }

    public void drawSector(float cx, float cy, float a, float b, double from, double extent) {
        this.g2.draw(new Arc2D.Double(cx - a, cy - b, a * 2.0f, b * 2.0f, from, extent, 2));
    }

    public void fillSector(float cx, float cy, float a, float b, double from, double extent) {
        this.g2.fill(new Arc2D.Double(cx - a, cy - b, a * 2.0f, b * 2.0f, from, extent, 2));
    }

    public void draw(Shape shape) {
        this.g2.draw(shape);
    }

    public void fill(Shape shape) {
        this.g2.fill(shape);
    }

    public int getPixel(int x, int y) {
        return this.getImage().getRGB(x, y);
    }

    public void setPixel(int x, int y, long color) {
        this.getImage().setRGB(x, y, (int)color);
    }

    public Rectangle2D getBoundingBox() {
        return this.getBoundingBox(this.getBackgroundColor());
    }

    public Rectangle2D getBoundingBox(Color bgcolor) {
        int i;
        int[] pixels = this.getImageRGBA().getRGB(0, 0, this.width, this.height, null, 0, this.width);
        int br = pixels.length - 1;
        int bgcol = bgcolor.getRGB();
        int top = 0;
        int bottom = 0;
        int left = 0;
        int right = 0;
        if (br == 0) {
            return new Rectangle(1, 1);
        }
        for (i = 0; i <= br; ++i) {
            if (bgcol == pixels[i]) continue;
            top = i / this.width;
            break;
        }
        for (i = br; i >= 0; --i) {
            if (bgcol == pixels[i]) continue;
            bottom = i / this.width + 1;
            break;
        }
        for (i = 0; i != br; i += this.width) {
            if (i > br) {
                i -= br;
            }
            if (bgcol == pixels[i]) continue;
            left = i % this.width;
            break;
        }
        for (i = br; i != 0; i -= this.width) {
            if (i < 0) {
                i += br;
            }
            if (bgcol == pixels[i]) continue;
            right = i % this.width + 1;
            break;
        }
        return new Rectangle(left, top, right, bottom);
    }

    public void floodFill(Color fillColor, int blur) {
        this.floodFill(fillColor, blur, this.getBackgroundColor());
    }

    public void floodFill(Color fillColor, int blur, Color bgColor) {
        int i;
        int bc;
        int fc = fillColor.getRGB();
        if (this.isColorNear(fc, bc = bgColor.getRGB(), blur)) {
            return;
        }
        Rectangle2D rect = this.getBoundingBox(bgColor);
        int l = (int)rect.getMinX();
        int r = (int)rect.getMaxX();
        int t = (int)rect.getMinY();
        int b = (int)rect.getMaxY();
        Paint oldPaint = this.g2.getPaint();
        this.g2.setPaint(fillColor);
        this.g2.fillRect(0, 0, this.width, t);
        this.g2.fillRect(0, b, this.width, this.height - b);
        this.g2.fillRect(0, t, l, b);
        this.g2.fillRect(r, t, this.width - r, b);
        BufferedImage image = this.getImageRGBA();
        int[] pixels = image.getRGB(0, 0, this.width, this.height, null, 0, this.width);
        int end = r + this.width * t;
        int di = this.width * (b - t - 1);
        for (i = l + this.width * t; i < end; ++i) {
            if (this.isColorNear(pixels[i], bc, blur)) {
                this.floodRecursive(pixels, i, fc, bc, blur);
            }
            if (!this.isColorNear(pixels[i + di], bc, blur)) continue;
            this.floodRecursive(pixels, i + di, fc, bc, blur);
        }
        end = l + this.width * b;
        di = r - l - 1;
        for (i = l + this.width * t; i < end; i += this.width) {
            if (this.isColorNear(pixels[i], bc, blur)) {
                this.floodRecursive(pixels, i, fc, bc, blur);
            }
            if (!this.isColorNear(pixels[i + di], bc, blur)) continue;
            this.floodRecursive(pixels, i + di, fc, bc, blur);
        }
        image.setRGB(0, 0, this.width, this.height, pixels, 0, this.width);
        if (this.transparency == bgColor) {
            this.transparency = fillColor;
        }
        this.g2.setPaint(oldPaint);
    }

    public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
        this.g2.drawImage(img, op, x, y);
    }

    public void draw(Graphics g) {
        g.drawImage(this.getImage(), 0, 0, null);
    }

    public int getX() {
        return this.x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return this.y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return this.width;
    }

    public int getHeight() {
        return this.height;
    }

    public Rectangle getBounds() {
        return this.getImage().getRaster().getBounds();
    }

    public int getImageIndex() {
        return this.imageIndex;
    }

    public int getNumImages() {
        return this.numImages;
    }

    public Paint getBackground() {
        return this.backGround;
    }

    public void setBackground(Paint bground) {
        Paint paint = this.backGround = bground != null ? bground : TRANSPARENT_IMAGE_BACKGROUND;
        if (this.backGround instanceof Color) {
            this.bgColor = (Color)this.backGround;
        }
    }

    public void setBackgroundColor(Color bgColor) {
        this.bgColor = bgColor;
    }

    public Color getBackgroundColor() {
        if (this.bgColor == null) {
            if (this.backGround instanceof Color) {
                this.bgColor = (Color)this.backGround;
            } else {
                long[] cs = new long[2 * this.width + 2 * this.height];
                int[] numCs = new int[2 * this.width + 2 * this.height];
                int cols = -1;
                BufferedImage image = this.getImageRGBA();
                int[] rowT = image.getRGB(0, 0, this.width, 1, null, 0, this.width);
                int[] rowB = image.getRGB(0, this.height - 1, this.width, 1, null, 0, this.width);
                for (int i = 0; i < rowT.length; ++i) {
                    long c1 = (long)rowT[i] & 0xFFFFFFFFL;
                    long c2 = (long)rowB[i] & 0xFFFFFFFFL;
                    for (int j = 0; j <= cols; ++j) {
                        if (c1 == cs[j]) {
                            int[] arrn = numCs;
                            int n = j;
                            arrn[n] = arrn[n] + 1;
                            c1 = -1;
                        }
                        if (c2 != cs[j]) continue;
                        int[] arrn = numCs;
                        int n = j;
                        arrn[n] = arrn[n] + 1;
                        c2 = -1;
                    }
                    if (c1 >= 0) {
                        cs[++cols] = c1;
                    }
                    if (c2 < 0) continue;
                    cs[++cols] = c2;
                }
                int[] colL = image.getRGB(0, 0, 1, this.height, null, 0, 1);
                int[] colR = image.getRGB(this.width - 1, 0, 1, this.height, null, 0, 1);
                for (int i2 = 0; i2 < colL.length; ++i2) {
                    long c1 = (long)colL[i2] & 0xFFFFFFFFL;
                    long c2 = (long)colR[i2] & 0xFFFFFFFFL;
                    for (int j = 0; j <= cols; ++j) {
                        if (c1 == cs[j]) {
                            int[] arrn = numCs;
                            int n = j;
                            arrn[n] = arrn[n] + 1;
                            c1 = -1;
                        }
                        if (c2 != cs[j]) continue;
                        int[] arrn = numCs;
                        int n = j;
                        arrn[n] = arrn[n] + 1;
                        c2 = -1;
                    }
                    if (c1 >= 0) {
                        cs[++cols] = c1;
                    }
                    if (c2 < 0) continue;
                    cs[++cols] = c2;
                }
                int max = 0;
                int maxOcc = numCs[0];
                for (int i3 = 1; i3 < cols; ++i3) {
                    if (numCs[i3] <= maxOcc) continue;
                    max = i3;
                    maxOcc = numCs[i3];
                }
                this.bgColor = new Color((int)cs[max], true);
            }
        }
        return this.bgColor;
    }

    public Color getTransparency() {
        return this.transparency;
    }

    public void setTransparency(Color transparency) {
        this.transparency = transparency;
    }

    public void setMimeType(String mimeType) {
        if (mimeType != null && mimeType.length() > 0) {
            this.mimeType = mimeType;
        }
    }

    public String getMimeType() {
        return this.mimeType;
    }

    public float getOpacity() {
        return this.opacity;
    }

    public void setOpacity(float opacity) {
        if (!Float.isNaN(opacity)) {
            if (opacity > 1.0f) {
                opacity = 1.0f;
            }
            if (opacity < 0.0f) {
                opacity = 0.0f;
            }
            this.opacity = opacity;
        }
    }

    public Composite getLayerComposite() {
        return this.layerComposite;
    }

    public void setLayerComposite(Composite layerComposite) {
        if (layerComposite != null) {
            this.layerComposite = layerComposite;
        }
    }

    public BufferedImage getImage() {
        return this.baseImg;
    }

    private BufferedImage getImageRGBA() {
        if (!this.baseImgIsRGBA) {
            BufferedImage newImage = new BufferedImage(this.baseImg.getWidth(), this.baseImg.getHeight(), 2);
            Graphics2D g2 = newImage.createGraphics();
            g2.drawRenderedImage(this.baseImg, IDENTITY_XFORM);
            g2.dispose();
            this.setImage(newImage);
        }
        return this.baseImg;
    }

    public Graphics2D getG2() {
        return this.g2;
    }

    void setImage(BufferedImage image) {
        this.setImage(image, false);
    }

    private void setImage(BufferedImage image, boolean paintBackground) {
        if (image != null && image != this.baseImg) {
            if (this.baseImg != null) {
                this.baseImg.flush();
            }
            if (this.g2 != null) {
                this.g2.dispose();
            }
            this.baseImg = image;
            this.baseImgIsRGBA = this.baseImg.getType() == 6;
            this.width = this.baseImg.getWidth();
            this.height = this.baseImg.getHeight();
            this.g2 = this.baseImg.createGraphics();
            this.setRenderingHints(this.g2);
            this.g2.setBackground(this.backGround instanceof Color ? (Color)this.backGround : Color.white);
            if (paintBackground) {
                this.g2.setPaint(this.backGround);
                this.g2.fillRect(0, 0, this.width, this.height);
            }
        }
    }

    public String toString() {
        return "Layer: left=" + this.x + ", top=" + this.y + ", width=" + this.width + ", height=" + this.height + ", background=" + this.backGround + ", mime=" + this.mimeType;
    }

    private void init(int width, int height, Paint bground) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("width or height <= 0");
        }
        this.x = 0;
        this.y = 0;
        this.setBackground(bground);
        this.opacity = 1.0f;
        this.transparency = null;
        this.mimeType = "image/gif";
        this.layerComposite = DEFAULT_LAYER_COMPOSITE;
        this.setImage(new BufferedImage(width, height, 2), true);
    }

    private void setRenderingHints(Graphics2D g2) {
        if (RENDERING_HINTS == null) {
            HashMap<RenderingHints.Key, Object> tmp = new HashMap<RenderingHints.Key, Object>(7);
            tmp.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            tmp.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            tmp.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            tmp.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
            tmp.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            tmp.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            tmp.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            RENDERING_HINTS = tmp;
        }
        g2.setRenderingHints(RENDERING_HINTS);
    }

    private Rectangle2D checkRect(Rectangle2D rect) {
        if (rect == null) {
            return new Rectangle2D.Float(this.x, this.y, this.x + this.width, this.y + this.height);
        }
        double rx = rect.getX();
        double ry = rect.getY();
        double rw = rect.getWidth();
        double rh = rect.getHeight();
        if (rx < (double)this.x) {
            rx = this.x;
        }
        if (ry < (double)this.y) {
            ry = this.y;
        }
        if (rx + rw > (double)(this.x + this.width)) {
            rw = (double)(this.x + this.width) - rx;
        } else if (rw == 0.0) {
            rw = 1.0;
        }
        if (ry + rh > (double)(this.y + this.height)) {
            rh = (double)(this.y + this.height) - ry;
        } else if (rh == 0.0) {
            rh = 1.0;
        }
        rect.setRect(rx, ry, rw, rh);
        return rect;
    }

    private void transformImage(BufferedImage newImage, AffineTransform xform) {
        Graphics2D newG2 = newImage.createGraphics();
        this.setRenderingHints(newG2);
        if (xform != null) {
            newG2.drawRenderedImage(this.getImage(), xform);
        }
        this.setImage(newImage);
    }

    private boolean isColorNear(int col1, int col2, long maxDist) {
        if (col1 == col2) {
            return true;
        }
        int a1 = col1 >>> 24 & 255;
        int a2 = col2 >>> 24 & 255;
        long r = a1 * (col1 >>> 16 & 255) - a2 * (col2 >>> 16 & 255);
        long g = a1 * (col1 >>> 8 & 255) - a2 * (col2 >>> 8 & 255);
        long b = a1 * (col1 & 255) - a2 * (col2 & 255);
        return r * r + g * g + b * b <= maxDist * maxDist * 255 * 255;
    }

    private void floodRecursive(int[] pixels, int pos, int fillCol, int bgCol, int blur) {
        int j = blur > 0 ? blur / 2 : 0;
        int pix = pixels[pos];
        if (bgCol == pix) {
            pixels[pos] = fillCol;
        } else if (blur > 0) {
            int pr = pix >>> 16 & 255;
            int pg = pix >>> 8 & 255;
            int pb = pix & 255;
            int br = bgCol >>> 16 & 255;
            int bg = bgCol >>> 8 & 255;
            int bb = bgCol & 255;
            int dr = pr - br;
            int dg = pg - bg;
            int db = pb - bb;
            int aq = (int)Math.sqrt(dr * dr + dg * dg + db * db);
            int ai = blur - aq;
            int r = (pr * aq - br * ai) / blur;
            int g = (pg * aq - bg * ai) / blur;
            int b = (pb * aq - bb * ai) / blur;
            pixels[pos] = pix & -16777216 + (r << 16) + (g << 8) + b;
        }
        int[] o = new int[4];
        int i = 0;
        int x = pos % this.width;
        int y = pos / this.width;
        if (x + 1 < this.width) {
            o[i++] = pos + 1;
        }
        if (x > 0) {
            o[i++] = pos - 1;
        }
        if (y > 0) {
            o[i++] = pos - this.width;
        }
        if (y + 1 < this.height) {
            o[i++] = pos + this.width;
        }
        while (i > 0) {
            if (!this.isColorNear(pixels[o[--i]], bgCol, blur)) continue;
            try {
                this.floodRecursive(pixels, o[i], fillCol, bgCol, j);
                continue;
            }
            catch (StackOverflowError soe) {
                return;
            }
        }
    }

    static {
        ImageSupport.initialize();
        GAMMA22 = new LuminanceSystem("Gamma 2.2", 0.229f, 0.587f, 0.114f);
        LINEAR = new LuminanceSystem("Linear", 0.3086f, 0.6094f, 0.082f);
        REC709 = new LuminanceSystem("Rec709", 0.2125f, 0.7154f, 0.0721f);
        TRANSPARENT_IMAGE_BACKGROUND = new Color(0, 0, 0, 0);
        DEFAULT_LAYER_COMPOSITE = AlphaComposite.SrcOver;
        IDENTITY_XFORM = new AffineTransform();
        RENDERING_HINTS = null;
    }

    public static final class LuminanceSystem {
        private final String name;
        private final float r;
        private final float g;
        private final float b;
        private String stringRep;

        LuminanceSystem(String name, float r, float g, float b) {
            this.name = name;
            this.r = r;
            this.g = g;
            this.b = b;
            this.stringRep = null;
        }

        LuminanceSystem(float r, float g, float b) {
            this(null, r, g, b);
        }

        float r() {
            return this.r;
        }

        float g() {
            return this.g;
        }

        float b() {
            return this.b;
        }

        public String toString() {
            if (this.stringRep == null) {
                StringBuffer buf = new StringBuffer();
                if (this.name != null) {
                    buf.append(this.name);
                    buf.append(' ');
                }
                this.stringRep = buf.append('[').append(this.r).append(',').append(this.g).append(',').append(this.b).append(']').toString();
            }
            return this.stringRep;
        }
    }

}