ARGBImage.java 31.7 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.stream.BitInputStream
 *  com.adobe.internal.io.stream.IO
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFRuntimeException
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 */
package com.adobe.internal.pdftoolkit.image;

import com.adobe.internal.io.stream.BitInputStream;
import com.adobe.internal.io.stream.IO;
import com.adobe.internal.pdftoolkit.color.ColorSpaceCache;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFRuntimeException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.graphicsDOM.utils.GraphicsUtils;
import com.adobe.internal.pdftoolkit.image.ImageInputSource;
import com.adobe.internal.pdftoolkit.image.ImageResamplingMethod;
import com.adobe.internal.pdftoolkit.image.Resampler;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferDouble;
import java.awt.image.DataBufferInt;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class ARGBImage {
    public static final int MEMORY_THRESHOLD = 12000000;
    private int width;
    private int scaledWidth;
    private int height;
    private int scaledHeight;
    private int bpc;
    private ColorSpaceCache colorSpace;
    private InputStream imageInputStream = null;
    private ImageInputSource imageSource = null;
    private int references = 0;
    private ImageResamplingMethod downScalingMethod;
    private boolean resamplingPending = false;
    private int[] rgbIndexTable = null;
    private double[] decode = null;
    private List<Object> outputTiles = null;
    private Object mask;

    public double[] getDecode() {
        return this.decode;
    }

    public ARGBImage(ImageInputSource decodedImage, int width, int height, int bitsPerComponent, ColorSpaceCache colorSpace, double[] decode) throws IOException {
        this(decodedImage, width, height, bitsPerComponent, colorSpace, false, false, decode);
    }

    public ARGBImage(ImageInputSource decodedImage, int width, int height, int bitsPerComponent, ColorSpaceCache colorSpace) throws IOException {
        this(decodedImage, width, height, bitsPerComponent, colorSpace, false, false, null);
    }

    public ARGBImage(ImageInputSource imageSource, int width, int height, int bitsPerComponent, ColorSpaceCache colorSpace, boolean isInline, boolean isMask, double[] decode) throws IOException {
        this.width = width;
        this.height = height;
        this.scaledHeight = height;
        this.scaledWidth = width;
        this.bpc = bitsPerComponent;
        this.colorSpace = colorSpace;
        this.imageSource = imageSource;
        this.imageInputStream = imageSource.getImageInputStream();
        this.decode = decode;
        ++this.references;
    }

    private int[] initializeOutputStorage(int height, int width, int index) {
        if (this.outputTiles == null) {
            this.outputTiles = new ArrayList<Object>();
        }
        BufferedImage tileImage = new BufferedImage(width, height, 2);
        int[] out = ((DataBufferInt)tileImage.getRaster().getDataBuffer()).getData();
        if (index < this.outputTiles.size()) {
            this.outputTiles.set(index, tileImage);
        } else {
            this.outputTiles.add(tileImage);
        }
        return out;
    }

    private void handleICCColorSpace(ColorSpaceCache colorSpace, int bpc, int width, int height, int[] output, int transferType, DataBuffer buffer) throws Exception {
        BufferedImage colorTable = null;
        try {
            colorTable = this.convertToRGB(colorSpace, bpc, width, height, transferType, buffer);
        }
        catch (Exception e) {
            if (colorSpace.getSubstituteColorSpace().getUnderlyingColorspace().getName() == ASName.k_DeviceCMYK) {
                colorTable = this.convertToRGB(colorSpace.getSubstituteColorSpace(), bpc, width, height, transferType, buffer);
            }
            throw e;
        }
        System.arraycopy(((DataBufferInt)colorTable.getRaster().getDataBuffer()).getData(), 0, output, 0, width * height);
    }

    private void handleColorSpaceWithTintTransform(ColorSpaceCache colorSpace, BitInputStream bitIS, int bpc, byte[] decodedData, int width, int height, int[] output) throws Exception {
        double[] transformedValues = this.applyTintTransform(colorSpace, bitIS, bpc, decodedData, width, height);
        this.handleICCColorSpace(colorSpace.getSubstituteColorSpace(), bpc, width, height, output, 5, new DataBufferDouble(transformedValues, transformedValues.length));
    }

    private boolean twoStepsForTintTransforms(ColorSpaceCache colorSpace) {
        return colorSpace.getSubstituteColorSpace().getUnderlyingColorspace().getName() == ASName.k_ICCBased || colorSpace.getSubstituteColorSpace().getUnderlyingColorspace().getName() == ASName.k_DeviceCMYK;
    }

    private void performAction(ColorSpaceCache colorSpace, BitInputStream bis, int bpc, byte[] decodedData, int width, int height, Action action) {
        BitInputStream bitIS = null;
        try {
            bitIS = bis == null ? new BitInputStream((InputStream)new ByteArrayInputStream(decodedData)) : bis;
            int numberOfComponents = colorSpace.getUnderlyingColorspace().getName() != ASName.k_Indexed ? colorSpace.getNumberOfComponents() : 1;
            double[] pixel = new double[numberOfComponents];
            double[] range = colorSpace.getRange();
            double inputRangeMax = Math.pow(2.0, bpc);
            int bitsReadinRow = 0;
            for (int i = 0; i < height; ++i) {
                bitsReadinRow = 0;
                for (int j = 0; j < width; ++j) {
                    for (int k = 0; k < numberOfComponents; ++k) {
                        bitsReadinRow += bpc;
                        pixel[k] = bitIS.read(bpc);
                        if (pixel[k] < 0.0) {
                            double[] arrd = pixel;
                            int n = k;
                            arrd[n] = arrd[n] + inputRangeMax;
                        }
                        if (colorSpace.getUnderlyingColorspace().getName() == ASName.k_Indexed || range == null || range[2 * k] == 0.0 && range[2 * k + 1] == inputRangeMax - 1.0) continue;
                        pixel[k] = GraphicsUtils.scaleToRange(0.0, inputRangeMax - 1.0, pixel[k], range[2 * k], range[2 * k + 1]);
                    }
                    action.perform(pixel, i, j);
                }
                if (bitsReadinRow % 8 == 0) continue;
                bitIS.skip((long)(8 - bitsReadinRow % 8));
            }
        }
        catch (IOException e) {
            throw new PDFRuntimeException("IO Exception while convering image to RGB", (Throwable)e);
        }
        finally {
            if (bitIS != null && bis == null) {
                try {
                    bitIS.close();
                }
                catch (IOException e) {
                    throw new PDFRuntimeException("IO Exception while closing BitInputStream.", (Throwable)e);
                }
            }
        }
    }

    private double[] applyTintTransform(ColorSpaceCache colorSpace, BitInputStream bitIS, int bpc, byte[] decodedData, int width, int height) {
        int transformedPixelSize = colorSpace.getSubstituteColorSpace().getNumberOfComponents();
        double[] transformedPixels = new double[width * height * transformedPixelSize];
        this.performAction(colorSpace, bitIS, bpc, decodedData, width, height, new ApplyTintTransform(colorSpace, transformedPixels, width, height, transformedPixelSize));
        return transformedPixels;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void convertToRGB(ColorSpaceCache colorSpace, BitInputStream bitIS, int bpc, byte[] decodedData, int width, int height, int[] output) {
        if (colorSpace.getUnderlyingColorspace().getName() == ASName.k_DeviceRGB) {
            BufferedImage bImage = this.convertToRGB(colorSpace, bpc, width, height, 0, new DataBufferByte(decodedData, decodedData.length));
            decodedData = null;
            System.arraycopy(((DataBufferInt)bImage.getRaster().getDataBuffer()).getData(), 0, output, 0, width * height);
        } else if (colorSpace.getUnderlyingColorspace().getName() == ASName.k_Indexed) {
            ColorSpaceCache baseCS = colorSpace.getSubstituteColorSpace();
            if (this.rgbIndexTable == null) {
                this.rgbIndexTable = new int[colorSpace.getHighValue() + 1];
                this.convertToRGB(baseCS, null, 8, colorSpace.getLookupData(), colorSpace.getHighValue() + 1, 1, this.rgbIndexTable);
            }
            this.performAction(colorSpace, bitIS, bpc, decodedData, width, height, new MapIndices(this.rgbIndexTable, width, output));
        } else if ((colorSpace.getUnderlyingColorspace().getName() == ASName.k_Separation || colorSpace.getUnderlyingColorspace().getName() == ASName.k_DeviceN) && this.twoStepsForTintTransforms(colorSpace)) {
            try {
                this.handleColorSpaceWithTintTransform(colorSpace, bitIS, bpc, decodedData, width, height, output);
            }
            catch (Exception e) {
                this.performAction(colorSpace, bitIS, bpc, decodedData, width, height, new ConvertToRGB(colorSpace, output, width, height));
            }
        } else if ((colorSpace.getUnderlyingColorspace().getName() == ASName.k_ICCBased || colorSpace.getUnderlyingColorspace().getName() == ASName.k_DeviceCMYK) && Arrays.equals(colorSpace.getRange(), colorSpace.getUnderlyingColorspace().getRange())) {
            if (decodedData != null) {
                try {
                    if (bpc > 8) {
                        BitInputStream bis = new BitInputStream((InputStream)new ByteArrayInputStream(decodedData));
                        try {
                            int[] data = new int[decodedData.length * 8 / bpc];
                            bis.read(data, bpc, data.length);
                            this.handleICCColorSpace(colorSpace, bpc, width, height, output, 3, new DataBufferInt(data, data.length));
                        }
                        finally {
                            if (bis != null) {
                                bis.close();
                            }
                        }
                    }
                    this.handleICCColorSpace(colorSpace, bpc, width, height, output, 0, new DataBufferByte(decodedData, decodedData.length));
                }
                catch (Exception e) {
                    this.convertToRGB(colorSpace.getSubstituteColorSpace(), bitIS, bpc, decodedData, width, height, output);
                }
            }
        } else {
            this.performAction(colorSpace, bitIS, bpc, decodedData, width, height, new ConvertToRGB(colorSpace, output, width, height));
        }
    }

    private BufferedImage convertToRGB(ColorSpaceCache colorSpace, int bpc, int width, int height, int transferType, DataBuffer buffer) {
        ColorModel cm = colorSpace.createColorModel(bpc, transferType);
        WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
        DataBufferWritableRaster newRaster = new DataBufferWritableRaster(raster.getSampleModel(), buffer, new Point(0, 0));
        BufferedImage srcImage = new BufferedImage(cm, newRaster, false, null);
        BufferedImage destImage = new BufferedImage(width, height, 2);
        ColorConvertOp colorConvOp = new ColorConvertOp(cm.getColorSpace(), ColorSpace.getInstance(1000), null);
        colorConvOp.filter(srcImage, destImage);
        return destImage;
    }

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

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

    public void downsample(double hScale, double vScale, ImageResamplingMethod method) {
        this.resample((int)Math.round((double)this.height * vScale), (int)Math.round((double)this.width * hScale), method);
    }

    private void resample(int destHeight, int destWidth, ImageResamplingMethod method, int index) {
        this.scaledHeight = destHeight;
        this.scaledWidth = destWidth;
        this.downScalingMethod = method;
        if (this.outputTiles == null || this.outputTiles.isEmpty()) {
            this.resamplingPending = true;
            return;
        }
        BufferedImage curTile = null;
        Object obj = this.outputTiles.get(index);
        if (obj != null) {
            if (obj instanceof SoftReference) {
                obj = ((SoftReference)obj).get();
            }
            if (obj instanceof BufferedImage) {
                curTile = (BufferedImage)obj;
            }
        }
        int[] pixels = ((DataBufferInt)curTile.getRaster().getDataBuffer()).getData();
        int scaledTileHeight = (int)Math.ceil(1.0 * (double)this.scaledHeight / (double)this.height * (double)curTile.getHeight());
        switch (method) {
            case kResampleBicubic: {
                pixels = Resampler.downSampleWithBicubicMethod(this.scaledWidth, scaledTileHeight, curTile.getWidth(), curTile.getHeight(), pixels, 3);
                break;
            }
            case kResampleLinear: {
                throw new UnsupportedOperationException("Linear algorithm for resampling image not implemented yet.");
            }
            case kResampleNearestNeighbor: {
                pixels = Resampler.downSampleWithNearestNeighbourMethod(this.scaledWidth, scaledTileHeight, curTile.getWidth(), curTile.getHeight(), pixels);
                break;
            }
            default: {
                pixels = Resampler.downSampleWithNearestNeighbourMethod(this.scaledWidth, scaledTileHeight, curTile.getWidth(), curTile.getHeight(), pixels);
            }
        }
        curTile = null;
        this.outputTiles.set(index, null);
        BufferedImage bufImage = new BufferedImage(this.scaledWidth, scaledTileHeight, 2);
        System.arraycopy(pixels, 0, ((DataBufferInt)bufImage.getRaster().getDataBuffer()).getData(), 0, this.scaledWidth * scaledTileHeight);
        this.outputTiles.set(index, bufImage);
    }

    public void resample(int destHeight, int destWidth, ImageResamplingMethod method) {
        this.resample(destHeight, destWidth, method, this.outputTiles == null ? -1 : this.outputTiles.size() - 1);
    }

    public BitInputStream getImageBitInputStream() {
        if (this.imageInputStream == null) {
            return null;
        }
        try {
            BitInputStream bitInputStream = new BitInputStream(this.imageInputStream);
            return bitInputStream;
        }
        catch (IOException e) {
            throw new PDFRuntimeException("Exception occured while creating BitInputStream", (Throwable)e);
        }
        finally {
            this.imageInputStream = null;
        }
    }

    public int getReferences() {
        return this.references;
    }

    public void setMask(boolean[] mask) {
        this.mask = mask;
    }

    public boolean[] getMask() {
        if (this.mask instanceof boolean[]) {
            return (boolean[])this.mask;
        }
        if (this.mask instanceof SoftReference) {
            return (boolean[])((SoftReference)this.mask).get();
        }
        return null;
    }

    public Iterator<BufferedImage> getBufferedImagesIterator() {
        return new ARGBTilesIterator();
    }

    public com.adobe.internal.pdftoolkit.color.ColorSpace getColorSpace() {
        return this.colorSpace.getUnderlyingColorspace();
    }

    public ARGBImage slice(ImageInputSource imageSource) {
        ++this.references;
        if (this.mask != null) {
            if (this.mask instanceof SoftReference) {
                this.mask = ((SoftReference)this.mask).get();
            }
            if (!(this.mask instanceof boolean[])) {
                return null;
            }
        }
        if (imageSource != null && this.imageSource == null) {
            this.imageSource = imageSource;
        }
        return this;
    }

    public void close() {
        if (--this.references == 0) {
            try {
                if (this.imageInputStream != null) {
                    this.imageInputStream.close();
                }
                this.imageInputStream = null;
                if (this.imageSource != null) {
                    this.imageSource.close();
                }
                this.imageSource = null;
            }
            catch (IOException e) {
                throw new PDFRuntimeException(null, (Throwable)e);
            }
            finally {
                if (this.mask instanceof boolean[]) {
                    this.mask = new SoftReference<boolean[]>((boolean[])this.mask);
                }
                this.rgbIndexTable = null;
            }
        }
    }

    public boolean isClosed() {
        return this.references == 0;
    }

    static class DataBufferWritableRaster
    extends WritableRaster {
        DataBufferWritableRaster(SampleModel sampleModel, DataBuffer dataBuffer, Point sampleModelTranslate) {
            super(sampleModel, dataBuffer, sampleModelTranslate);
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private static class MapIndices<T>
    extends Action {
        private int[] colorTableArray = null;

        private MapIndices(int[] colorTableArray, int width, int[] output) {
            this.colorTableArray = colorTableArray;
            this.width = width;
            this.output = output;
        }

        @Override
        public void perform(double[] pixel, int row, int column) {
            ((int[])this.output)[row * this.width + column] = this.colorTableArray[Math.min((int)pixel[0], this.colorTableArray.length - 1)];
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private static class ConvertToRGB<T>
    extends Action<T> {
        private ASName csName = null;

        private ConvertToRGB(ColorSpaceCache colorSpace, T output, int width, int height) {
            super(colorSpace, output, width, height);
            this.csName = colorSpace.getUnderlyingColorspace().getName();
        }

        @Override
        public void perform(double[] pixel, int row, int column) {
            if (this.csName == ASName.k_DeviceGray) {
                int gray = GraphicsUtils.rint(pixel[0] * 255.0);
                ((int[])this.output)[row * this.width + column] = -16777216 | gray << 16 | gray << 8 | gray;
            } else {
                double[] transformedPixel = this.colorSpace.toRGB(pixel);
                int red = GraphicsUtils.rint(transformedPixel[0] * 255.0);
                int green = GraphicsUtils.rint(transformedPixel[1] * 255.0);
                int blue = GraphicsUtils.rint(transformedPixel[2] * 255.0);
                ((int[])this.output)[row * this.width + column] = -16777216 | red << 16 | green << 8 | blue;
            }
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private static class ApplyTintTransform<T>
    extends Action<T> {
        private int outputPixelSize;

        private ApplyTintTransform(ColorSpaceCache colorSpace, T output, int width, int height, int outputPixelSize) {
            super(colorSpace, output, width, height);
            this.outputPixelSize = outputPixelSize;
        }

        @Override
        public void perform(double[] pixel, int row, int column) {
            double[] transformedPixel = this.colorSpace.applyTintTransform(pixel);
            for (int k = 0; k < this.outputPixelSize; ++k) {
                ((double[])this.output)[row * this.width * this.outputPixelSize + column * this.outputPixelSize + k] = transformedPixel[k];
            }
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private static abstract class Action<T> {
        protected ColorSpaceCache colorSpace;
        protected T output;
        protected int width;

        protected Action() {
        }

        protected Action(ColorSpaceCache colorSpace, T output, int width, int height) {
            this.colorSpace = colorSpace;
            this.output = output;
            this.width = width;
        }

        abstract void perform(double[] var1, int var2, int var3);
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private class ARGBTilesIterator
    implements Iterator<BufferedImage> {
        private int pixelsProcessed;
        private int totalPixelsToRead;
        private boolean readDataToArray;
        private BitInputStream bis;
        private int index;
        private int tileHeight;
        private int localMemoryThreshold;
        private int noc;
        private int bytesPerComponent;
        private int bitsPerRow;
        private List<Integer> tilesHeight;
        private int lastIndex;

        private void initializeImageStream() throws IOException {
            if (ARGBImage.this.imageInputStream == null) {
                ARGBImage.this.imageInputStream = ARGBImage.this.imageSource.getImageInputStream();
                this.bis = this.readDataToArray ? (ARGBImage.this.bpc < 8 ? new BitInputStream(ARGBImage.this.imageInputStream) : null) : new BitInputStream(ARGBImage.this.imageInputStream);
            }
        }

        private ARGBTilesIterator() {
            this.pixelsProcessed = 0;
            this.totalPixelsToRead = -1;
            this.readDataToArray = false;
            this.bis = null;
            this.index = 0;
            this.tileHeight = -1;
            this.localMemoryThreshold = -1;
            this.noc = -1;
            this.tilesHeight = new ArrayList<Integer>();
            this.lastIndex = -1;
            ARGBImage.this.imageInputStream = null;
            ASName csName = ARGBImage.this.colorSpace.getUnderlyingColorspace().getName();
            if (csName == ASName.k_ICCBased || csName == ASName.k_DeviceCMYK || csName == ASName.k_DeviceRGB || (csName == ASName.k_Separation || csName == ASName.k_DeviceN) && ARGBImage.this.twoStepsForTintTransforms(ARGBImage.this.colorSpace)) {
                this.readDataToArray = true;
            }
            if (this.readDataToArray) {
                this.bytesPerComponent = (int)Math.ceil(1.0 * (double)ARGBImage.this.bpc / 8.0);
                this.noc = ARGBImage.this.colorSpace.getUnderlyingColorspace().getName() == ASName.k_Indexed ? 1 : ARGBImage.this.colorSpace.getNumberOfComponents();
                this.bitsPerRow = ARGBImage.this.width * this.noc * ARGBImage.this.bpc;
                this.tileHeight = Math.min(ARGBImage.this.height, (int)Math.ceil(1.2E7 / Math.ceil(this.bitsPerRow / 8)));
                this.localMemoryThreshold = this.bytesPerComponent * this.tileHeight * ARGBImage.this.width * this.noc;
                this.totalPixelsToRead = ARGBImage.this.width * ARGBImage.this.height;
            } else {
                this.tileHeight = Math.min(ARGBImage.this.height, (int)Math.ceil(1.2E7 / (double)(3 * ARGBImage.this.width)));
            }
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         * Enabled aggressive exception aggregation
         */
        private boolean ConvertToRGB() {
            try {
                if (this.pixelsProcessed < this.totalPixelsToRead) {
                    byte[] currentDataToProcess = null;
                    byte[] decodedData = null;
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    try {
                        double tempHeight;
                        double size;
                        if (ARGBImage.this.bpc < 8) {
                            if (this.bitsPerRow % 8 != 0) {
                                int bytesPerRowToRead = (int)Math.ceil((this.bitsPerRow + 8 - this.bitsPerRow % 8) / ARGBImage.this.bpc);
                                int bytesPerRowToUse = ARGBImage.this.width * this.noc * this.bytesPerComponent;
                                currentDataToProcess = new byte[this.tileHeight * bytesPerRowToUse];
                                for (int i = 0; i < this.tileHeight && IO.copy((BitInputStream)this.bis, (long)bytesPerRowToRead, (OutputStream)os, (int)ARGBImage.this.bpc) != 0; ++i) {
                                    decodedData = os.toByteArray();
                                    System.arraycopy(decodedData, 0, currentDataToProcess, i * bytesPerRowToUse, bytesPerRowToUse);
                                    os.reset();
                                }
                            } else {
                                IO.copy((BitInputStream)this.bis, (long)this.localMemoryThreshold, (OutputStream)os, (int)ARGBImage.this.bpc);
                            }
                        } else {
                            IO.copy((InputStream)ARGBImage.this.imageInputStream, (long)this.localMemoryThreshold, (OutputStream)os);
                        }
                        if (currentDataToProcess == null) {
                            currentDataToProcess = decodedData = os.toByteArray();
                        }
                        if ((tempHeight = 1.0 * (size = 1.0 * (double)currentDataToProcess.length / (double)(this.noc * this.bytesPerComponent)) / (double)ARGBImage.this.width) - (double)((int)tempHeight) != 0.0 && size < (double)(this.totalPixelsToRead - this.pixelsProcessed)) {
                            tempHeight = (int)Math.ceil(tempHeight);
                            int moreBytesToRead = (int)tempHeight * ARGBImage.this.width * this.noc * this.bytesPerComponent - currentDataToProcess.length;
                            currentDataToProcess = new byte[decodedData.length + moreBytesToRead];
                            System.arraycopy(decodedData, 0, currentDataToProcess, 0, decodedData.length);
                            for (int i = 0; i < moreBytesToRead; ++i) {
                                currentDataToProcess[decodedData.length + i] = (byte)(ARGBImage.this.bpc < 8 ? this.bis.read(ARGBImage.this.bpc) : ARGBImage.this.imageInputStream.read());
                            }
                            size = (int)tempHeight * ARGBImage.this.width;
                        } else if (size > (double)(this.totalPixelsToRead - this.pixelsProcessed)) {
                            size = this.totalPixelsToRead - this.pixelsProcessed;
                        } else if (size == 0.0) {
                            if (os != null) {
                                os.close();
                            }
                            ARGBImage.this.resamplingPending = false;
                            boolean moreBytesToRead = false;
                            return moreBytesToRead;
                        }
                        decodedData = null;
                        int[] out = ARGBImage.this.initializeOutputStorage((int)tempHeight, ARGBImage.this.width, this.index);
                        ARGBImage.this.convertToRGB(ARGBImage.this.colorSpace, null, ARGBImage.this.bpc, currentDataToProcess, ARGBImage.this.width, (int)tempHeight, out);
                        this.pixelsProcessed = (int)((double)this.pixelsProcessed + size);
                    }
                    catch (EOFException e) {
                        if (this.pixelsProcessed < this.totalPixelsToRead) {
                            throw e;
                        }
                    }
                    finally {
                        if (os != null) {
                            os.close();
                        }
                    }
                    return true;
                }
                return false;
            }
            catch (IOException e) {
                throw new PDFRuntimeException("Exception occured while decoding image.", (Throwable)e);
            }
        }

        @Override
        public boolean hasNext() {
            Object obj;
            Object e = obj = ARGBImage.this.outputTiles == null || this.index >= ARGBImage.this.outputTiles.size() ? null : (Object)ARGBImage.this.outputTiles.get(this.index);
            if (obj != null) {
                if (obj instanceof SoftReference) {
                    obj = ((SoftReference)obj).get();
                }
                if (obj instanceof BufferedImage) {
                    ARGBImage.this.outputTiles.set(this.index, obj);
                    this.pixelsProcessed += ((BufferedImage)obj).getHeight() * ((BufferedImage)obj).getWidth();
                    return true;
                }
            }
            try {
                this.initializeImageStream();
            }
            catch (IOException e) {
                throw new PDFRuntimeException("Exception occured while decoding image.", (Throwable)e);
            }
            if (ARGBImage.this.outputTiles != null && this.index < ARGBImage.this.outputTiles.size()) {
                int totalHeight = 0;
                for (int i = this.lastIndex + 1; i < this.index; ++i) {
                    totalHeight += this.tilesHeight.get(i).intValue();
                }
                int bytesToSkip = totalHeight * (int)Math.ceil(1.0 * (double)this.bitsPerRow / 8.0);
                try {
                    if (this.bis != null) {
                        this.bis.skip((long)(bytesToSkip * 8));
                    } else {
                        ARGBImage.this.imageInputStream.skip(bytesToSkip);
                    }
                }
                catch (IOException e) {
                    throw new RuntimeException(e);
                }
                this.lastIndex = this.index;
            }
            if (this.readDataToArray) {
                if (!this.ConvertToRGB()) {
                    return false;
                }
            } else {
                int th = Math.min(this.tileHeight, ARGBImage.this.height - this.index * this.tileHeight);
                if (th <= 0) {
                    return false;
                }
                int[] out = ARGBImage.this.initializeOutputStorage(th, ARGBImage.this.width, this.index);
                ARGBImage.this.convertToRGB(ARGBImage.this.colorSpace, this.bis, ARGBImage.this.bpc, null, ARGBImage.this.width, th, out);
            }
            if (this.index < this.tilesHeight.size()) {
                this.tilesHeight.set(this.index, ((BufferedImage)ARGBImage.this.outputTiles.get(this.index)).getHeight());
            } else {
                this.tilesHeight.add(((BufferedImage)ARGBImage.this.outputTiles.get(this.index)).getHeight());
            }
            if (ARGBImage.this.resamplingPending && ARGBImage.this.downScalingMethod != null) {
                ARGBImage.this.resample(ARGBImage.this.scaledHeight, ARGBImage.this.scaledWidth, ARGBImage.this.downScalingMethod, this.index);
            }
            return true;
        }

        @Override
        public BufferedImage next() {
            BufferedImage bufImage = (BufferedImage)ARGBImage.this.outputTiles.get(this.index);
            ARGBImage.this.outputTiles.set(this.index, new SoftReference<BufferedImage>(bufImage));
            ++this.index;
            return bufImage;
        }

        @Override
        public void remove() {
            if (ARGBImage.this.outputTiles != null && this.index <= ARGBImage.this.outputTiles.size()) {
                ARGBImage.this.outputTiles.remove(this.index - 1);
            }
        }
    }

}