Exemplo n.º 1
0
  void readPng() throws IOException {
    for (int i = 0; i < PNGID.length; i++) {
      if (PNGID[i] != is.read()) {
        throw new IOException("File is not a valid PNG.");
      }
    }
    byte buffer[] = new byte[TRANSFERSIZE];
    while (true) {
      int len = getInt(is);
      String marker = getString(is);
      if (len < 0 || !checkMarker(marker)) throw new IOException("Corrupted PNG file.");
      if (IDAT.equals(marker)) {
        int size;
        while (len != 0) {
          size = is.read(buffer, 0, Math.min(len, TRANSFERSIZE));
          if (size < 0) return;
          idat.write(buffer, 0, size);
          len -= size;
        }
      } else if (tRNS.equals(marker)) {
        switch (colorType) {
          case 0:
            if (len >= 2) {
              len -= 2;
              int gray = getWord(is);
              if (bitDepth == 16) transRedGray = gray;
              else additional.put(PdfName.MASK, new PdfLiteral("[" + gray + " " + gray + "]"));
            }
            break;
          case 2:
            if (len >= 6) {
              len -= 6;
              int red = getWord(is);
              int green = getWord(is);
              int blue = getWord(is);
              if (bitDepth == 16) {
                transRedGray = red;
                transGreen = green;
                transBlue = blue;
              } else
                additional.put(
                    PdfName.MASK,
                    new PdfLiteral(
                        "[" + red + " " + red + " " + green + " " + green + " " + blue + " " + blue
                            + "]"));
            }
            break;
          case 3:
            if (len > 0) {
              trans = new byte[len];
              for (int k = 0; k < len; ++k) trans[k] = (byte) is.read();
              len = 0;
            }
            break;
        }
        Utilities.skip(is, len);
      } else if (IHDR.equals(marker)) {
        width = getInt(is);
        height = getInt(is);

        bitDepth = is.read();
        colorType = is.read();
        compressionMethod = is.read();
        filterMethod = is.read();
        interlaceMethod = is.read();
      } else if (PLTE.equals(marker)) {
        if (colorType == 3) {
          PdfArray colorspace = new PdfArray();
          colorspace.add(PdfName.INDEXED);
          colorspace.add(getColorspace());
          colorspace.add(new PdfNumber(len / 3 - 1));
          ByteBuffer colortable = new ByteBuffer();
          while ((len--) > 0) {
            colortable.append_i(is.read());
          }
          colorspace.add(new PdfString(colorTable = colortable.toByteArray()));
          additional.put(PdfName.COLORSPACE, colorspace);
        } else {
          Utilities.skip(is, len);
        }
      } else if (pHYs.equals(marker)) {
        int dx = getInt(is);
        int dy = getInt(is);
        int unit = is.read();
        if (unit == 1) {
          dpiX = (int) ((float) dx * 0.0254f + 0.5f);
          dpiY = (int) ((float) dy * 0.0254f + 0.5f);
        } else {
          if (dy != 0) XYRatio = (float) dx / (float) dy;
        }
      } else if (cHRM.equals(marker)) {
        xW = (float) getInt(is) / 100000f;
        yW = (float) getInt(is) / 100000f;
        xR = (float) getInt(is) / 100000f;
        yR = (float) getInt(is) / 100000f;
        xG = (float) getInt(is) / 100000f;
        yG = (float) getInt(is) / 100000f;
        xB = (float) getInt(is) / 100000f;
        yB = (float) getInt(is) / 100000f;
        hasCHRM =
            !(Math.abs(xW) < 0.0001f
                || Math.abs(yW) < 0.0001f
                || Math.abs(xR) < 0.0001f
                || Math.abs(yR) < 0.0001f
                || Math.abs(xG) < 0.0001f
                || Math.abs(yG) < 0.0001f
                || Math.abs(xB) < 0.0001f
                || Math.abs(yB) < 0.0001f);
      } else if (sRGB.equals(marker)) {
        int ri = is.read();
        intent = intents[ri];
        gamma = 2.2f;
        xW = 0.3127f;
        yW = 0.329f;
        xR = 0.64f;
        yR = 0.33f;
        xG = 0.3f;
        yG = 0.6f;
        xB = 0.15f;
        yB = 0.06f;
        hasCHRM = true;
      } else if (gAMA.equals(marker)) {
        int gm = getInt(is);
        if (gm != 0) {
          gamma = 100000f / (float) gm;
          if (!hasCHRM) {
            xW = 0.3127f;
            yW = 0.329f;
            xR = 0.64f;
            yR = 0.33f;
            xG = 0.3f;
            yG = 0.6f;
            xB = 0.15f;
            yB = 0.06f;
            hasCHRM = true;
          }
        }
      } else if (iCCP.equals(marker)) {
        do {
          --len;
        } while (is.read() != 0);
        is.read();
        --len;
        byte icccom[] = new byte[len];
        int p = 0;
        while (len > 0) {
          int r = is.read(icccom, p, len);
          if (r < 0) throw new IOException("Premature end of file.");
          p += r;
          len -= r;
        }
        byte iccp[] = PdfReader.FlateDecode(icccom, true);
        icccom = null;
        //                try {
        //                    icc_profile = ICC_Profile.getInstance(iccp);
        //                }
        // catch (Exception e) {
        icc_profile = null;
        // }
      } else if (IEND.equals(marker)) {
        break;
      } else {
        Utilities.skip(is, len);
      }
      Utilities.skip(is, 4);
    }
  }