Search in sources :

Example 1 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class PdfXConformanceImp method checkPDFXConformance.

/**
 * Business logic that checks if a certain object is in conformance with PDF/X.
 * @param writer	the writer that is supposed to write the PDF/X file
 * @param key		the type of PDF/X conformance that has to be checked
 * @param obj1		the object that is checked for conformance
 */
public static void checkPDFXConformance(PdfWriter writer, int key, Object obj1) {
    if (writer == null || !writer.isPdfX())
        return;
    int conf = writer.getPDFXConformance();
    switch(key) {
        case PDFXKEY_COLOR:
            switch(conf) {
                case PdfWriter.PDFX1A2001:
                    if (obj1 instanceof ExtendedColor) {
                        ExtendedColor ec = (ExtendedColor) obj1;
                        switch(ec.getType()) {
                            case ExtendedColor.TYPE_CMYK:
                            case ExtendedColor.TYPE_GRAY:
                                return;
                            case ExtendedColor.TYPE_RGB:
                                throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                            case ExtendedColor.TYPE_SEPARATION:
                                SpotColor sc = (SpotColor) ec;
                                checkPDFXConformance(writer, PDFXKEY_COLOR, sc.getPdfSpotColor().getAlternativeCS());
                                break;
                            case ExtendedColor.TYPE_SHADING:
                                ShadingColor xc = (ShadingColor) ec;
                                checkPDFXConformance(writer, PDFXKEY_COLOR, xc.getPdfShadingPattern().getShading().getColorSpace());
                                break;
                            case ExtendedColor.TYPE_PATTERN:
                                PatternColor pc = (PatternColor) ec;
                                checkPDFXConformance(writer, PDFXKEY_COLOR, pc.getPainter().getDefaultColor());
                                break;
                        }
                    } else if (obj1 instanceof Color)
                        throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                    break;
            }
            break;
        case PDFXKEY_CMYK:
            break;
        case PDFXKEY_RGB:
            if (conf == PdfWriter.PDFX1A2001)
                throw new PdfXConformanceException("Colorspace RGB is not allowed.");
            break;
        case PDFXKEY_FONT:
            if (!((BaseFont) obj1).isEmbedded())
                throw new PdfXConformanceException("All the fonts must be embedded. This one isn't: " + ((BaseFont) obj1).getPostscriptFontName());
            break;
        case PDFXKEY_IMAGE:
            PdfImage image = (PdfImage) obj1;
            if (image.get(PdfName.SMASK) != null)
                throw new PdfXConformanceException("The /SMask key is not allowed in images.");
            switch(conf) {
                case PdfWriter.PDFX1A2001:
                    PdfObject cs = image.get(PdfName.COLORSPACE);
                    if (cs == null)
                        return;
                    if (cs.isName()) {
                        if (PdfName.DEVICERGB.equals(cs))
                            throw new PdfXConformanceException("Colorspace RGB is not allowed.");
                    } else if (cs.isArray()) {
                        if (PdfName.CALRGB.equals(((PdfArray) cs).getPdfObject(0)))
                            throw new PdfXConformanceException("Colorspace CalRGB is not allowed.");
                    }
                    break;
            }
            break;
        case PDFXKEY_GSTATE:
            PdfDictionary gs = (PdfDictionary) obj1;
            PdfObject obj = gs.get(PdfName.BM);
            if (obj != null && !PdfGState.BM_NORMAL.equals(obj) && !PdfGState.BM_COMPATIBLE.equals(obj))
                throw new PdfXConformanceException("Blend mode " + obj.toString() + " not allowed.");
            obj = gs.get(PdfName.CA);
            double v = 0.0;
            if (obj != null && (v = ((PdfNumber) obj).doubleValue()) != 1.0)
                throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v);
            obj = gs.get(PdfName.ca);
            v = 0.0;
            if (obj != null && (v = ((PdfNumber) obj).doubleValue()) != 1.0)
                throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v);
            break;
        case PDFXKEY_LAYER:
            throw new PdfXConformanceException("Layers are not allowed.");
    }
}
Also used : PdfArray(com.lowagie.text.pdf.PdfArray) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) ExtendedColor(com.lowagie.text.pdf.ExtendedColor) Color(java.awt.Color) PatternColor(com.lowagie.text.pdf.PatternColor) SpotColor(com.lowagie.text.pdf.SpotColor) ShadingColor(com.lowagie.text.pdf.ShadingColor) ExtendedColor(com.lowagie.text.pdf.ExtendedColor) PdfObject(com.lowagie.text.pdf.PdfObject) PdfNumber(com.lowagie.text.pdf.PdfNumber) PatternColor(com.lowagie.text.pdf.PatternColor) ShadingColor(com.lowagie.text.pdf.ShadingColor) PdfImage(com.lowagie.text.pdf.PdfImage) PdfXConformanceException(com.lowagie.text.pdf.PdfXConformanceException) SpotColor(com.lowagie.text.pdf.SpotColor)

Example 2 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class GifImage method readImage.

/**
 * Reads next frame image
 */
protected void readImage() throws IOException {
    // (sub)image position & size
    ix = readShort();
    iy = readShort();
    iw = readShort();
    ih = readShort();
    int packed = in.read();
    // 1 - local color table flag
    lctFlag = (packed & 0x80) != 0;
    // 2 - interlace flag
    interlace = (packed & 0x40) != 0;
    // 3 - sort flag
    // 4-5 - reserved
    // 6-8 - local color table size
    lctSize = 2 << (packed & 7);
    m_bpc = newBpc(m_gbpc);
    if (lctFlag) {
        // read table
        m_curr_table = readColorTable((packed & 7) + 1);
        m_bpc = newBpc((packed & 7) + 1);
    } else {
        m_curr_table = m_global_table;
    }
    if (transparency && transIndex >= m_curr_table.length / 3) {
        transparency = false;
    }
    if (transparency && m_bpc == 1) {
        // Acrobat 5.05 doesn't like this combination
        byte[] tp = new byte[12];
        System.arraycopy(m_curr_table, 0, tp, 0, 6);
        m_curr_table = tp;
        m_bpc = 2;
    }
    // decode pixel data
    boolean skipZero = decodeImageData();
    if (!skipZero)
        skip();
    Image img = null;
    try {
        img = new ImgRaw(iw, ih, 1, m_bpc, m_out);
        PdfArray colorspace = new PdfArray();
        colorspace.add(PdfName.INDEXED);
        colorspace.add(PdfName.DEVICERGB);
        int len = m_curr_table.length;
        colorspace.add(new PdfNumber(len / 3 - 1));
        colorspace.add(new PdfString(m_curr_table));
        PdfDictionary ad = new PdfDictionary();
        ad.put(PdfName.COLORSPACE, colorspace);
        img.setAdditional(ad);
        if (transparency) {
            img.setTransparency(new int[] { transIndex, transIndex });
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    img.setOriginalType(Image.ORIGINAL_GIF);
    img.setOriginalData(fromData);
    img.setUrl(fromUrl);
    GifFrame gf = new GifFrame();
    gf.image = img;
    gf.ix = ix;
    gf.iy = iy;
    // add image to frame list
    frames.add(gf);
// resetFrame();
}
Also used : ExceptionConverter(com.lowagie.text.ExceptionConverter) PdfArray(com.lowagie.text.pdf.PdfArray) ImgRaw(com.lowagie.text.ImgRaw) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfNumber(com.lowagie.text.pdf.PdfNumber) Image(com.lowagie.text.Image) PdfString(com.lowagie.text.pdf.PdfString) IOException(java.io.IOException)

Example 3 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class PngImage method getImage.

Image getImage() throws IOException {
    readPng();
    try {
        int pal0 = 0;
        int palIdx = 0;
        palShades = false;
        if (trans != null) {
            for (int k = 0; k < trans.length; ++k) {
                int n = trans[k] & 0xff;
                if (n == 0) {
                    ++pal0;
                    palIdx = k;
                }
                if (n != 0 && n != 255) {
                    palShades = true;
                    break;
                }
            }
        }
        if ((colorType & 4) != 0)
            palShades = true;
        genBWMask = (!palShades && (pal0 > 1 || transRedGray >= 0));
        if (!palShades && !genBWMask && pal0 == 1) {
            additional.put(PdfName.MASK, new PdfLiteral("[" + palIdx + " " + palIdx + "]"));
        }
        boolean needDecode = (interlaceMethod == 1) || (bitDepth == 16) || ((colorType & 4) != 0) || palShades || genBWMask;
        switch(colorType) {
            case 0:
                inputBands = 1;
                break;
            case 2:
                inputBands = 3;
                break;
            case 3:
                inputBands = 1;
                break;
            case 4:
                inputBands = 2;
                break;
            case 6:
                inputBands = 4;
                break;
        }
        if (needDecode)
            decodeIdat();
        int components = inputBands;
        if ((colorType & 4) != 0)
            --components;
        int bpc = bitDepth;
        if (bpc == 16)
            bpc = 8;
        Image img;
        if (image != null) {
            if (colorType == 3)
                img = new ImgRaw(width, height, components, bpc, image);
            else
                img = Image.getInstance(width, height, components, bpc, image);
        } else {
            img = new ImgRaw(width, height, components, bpc, idat.toByteArray());
            img.setDeflated(true);
            PdfDictionary decodeparms = new PdfDictionary();
            decodeparms.put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth));
            decodeparms.put(PdfName.PREDICTOR, new PdfNumber(15));
            decodeparms.put(PdfName.COLUMNS, new PdfNumber(width));
            decodeparms.put(PdfName.COLORS, new PdfNumber((colorType == 3 || (colorType & 2) == 0) ? 1 : 3));
            additional.put(PdfName.DECODEPARMS, decodeparms);
        }
        if (additional.get(PdfName.COLORSPACE) == null)
            additional.put(PdfName.COLORSPACE, getColorspace());
        if (intent != null)
            additional.put(PdfName.INTENT, intent);
        if (additional.size() > 0)
            img.setAdditional(additional);
        if (icc_profile != null)
            img.tagICC(icc_profile);
        if (palShades) {
            Image im2 = Image.getInstance(width, height, 1, 8, smask);
            im2.makeMask();
            img.setImageMask(im2);
        }
        if (genBWMask) {
            Image im2 = Image.getInstance(width, height, 1, 1, smask);
            im2.makeMask();
            img.setImageMask(im2);
        }
        img.setDpi(dpiX, dpiY);
        img.setXYRatio(XYRatio);
        img.setOriginalType(Image.ORIGINAL_PNG);
        return img;
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(com.lowagie.text.ExceptionConverter) PdfLiteral(com.lowagie.text.pdf.PdfLiteral) ImgRaw(com.lowagie.text.ImgRaw) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfNumber(com.lowagie.text.pdf.PdfNumber) Image(com.lowagie.text.Image) IOException(java.io.IOException)

Example 4 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class PngImage method readPng.

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) (dx * 0.0254f + 0.5f);
                dpiY = (int) (dy * 0.0254f + 0.5f);
            } else {
                if (dy != 0)
                    XYRatio = (float) dx / (float) dy;
            }
        } else if (cHRM.equals(marker)) {
            xW = getInt(is) / 100000f;
            yW = getInt(is) / 100000f;
            xR = getInt(is) / 100000f;
            yR = getInt(is) / 100000f;
            xG = getInt(is) / 100000f;
            yG = getInt(is) / 100000f;
            xB = getInt(is) / 100000f;
            yB = 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 / 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 (RuntimeException e) {
                icc_profile = null;
            }
        } else if (IEND.equals(marker)) {
            break;
        } else {
            Utilities.skip(is, len);
        }
        Utilities.skip(is, 4);
    }
}
Also used : PdfLiteral(com.lowagie.text.pdf.PdfLiteral) PdfArray(com.lowagie.text.pdf.PdfArray) PdfNumber(com.lowagie.text.pdf.PdfNumber) IOException(java.io.IOException) PdfString(com.lowagie.text.pdf.PdfString) ByteBuffer(com.lowagie.text.pdf.ByteBuffer) PdfString(com.lowagie.text.pdf.PdfString)

Example 5 with PdfNumber

use of com.lowagie.text.pdf.PdfNumber in project itext2 by albfernandez.

the class TiffImage method getTiffImageColor.

protected static Image getTiffImageColor(TIFFDirectory dir, RandomAccessFileOrArray s) {
    try {
        int compression = getTiffCompression(dir);
        int predictor = 1;
        TIFFLZWDecoder lzwDecoder = null;
        switch(compression) {
            case TIFFConstants.COMPRESSION_NONE:
            case TIFFConstants.COMPRESSION_LZW:
            case TIFFConstants.COMPRESSION_PACKBITS:
            case TIFFConstants.COMPRESSION_DEFLATE:
            case TIFFConstants.COMPRESSION_ADOBE_DEFLATE:
            case TIFFConstants.COMPRESSION_OJPEG:
            case TIFFConstants.COMPRESSION_JPEG:
                break;
            default:
                throw new IllegalArgumentException("The compression " + compression + " is not supported.");
        }
        int photometric = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_PHOTOMETRIC);
        switch(photometric) {
            case TIFFConstants.PHOTOMETRIC_MINISWHITE:
            case TIFFConstants.PHOTOMETRIC_MINISBLACK:
            case TIFFConstants.PHOTOMETRIC_RGB:
            case TIFFConstants.PHOTOMETRIC_SEPARATED:
            case TIFFConstants.PHOTOMETRIC_PALETTE:
                break;
            default:
                if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG)
                    throw new IllegalArgumentException("The photometric " + photometric + " is not supported.");
        }
        float rotation = 0;
        if (dir.isTagPresent(TIFFConstants.TIFFTAG_ORIENTATION)) {
            int rot = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_ORIENTATION);
            if (rot == TIFFConstants.ORIENTATION_BOTRIGHT || rot == TIFFConstants.ORIENTATION_BOTLEFT)
                rotation = (float) Math.PI;
            else if (rot == TIFFConstants.ORIENTATION_LEFTTOP || rot == TIFFConstants.ORIENTATION_LEFTBOT)
                rotation = (float) (Math.PI / 2.0);
            else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIENTATION_RIGHTBOT)
                rotation = -(float) (Math.PI / 2.0);
        }
        if (dir.isTagPresent(TIFFConstants.TIFFTAG_PLANARCONFIG) && dir.getFieldAsLong(TIFFConstants.TIFFTAG_PLANARCONFIG) == TIFFConstants.PLANARCONFIG_SEPARATE)
            throw new IllegalArgumentException("Planar images are not supported.");
        int samplePerPixel = 1;
        if (dir.isTagPresent(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL)) {
            // 1,3,4
            samplePerPixel = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL);
        }
        if (dir.isTagPresent(TIFFConstants.TIFFTAG_EXTRASAMPLES)) {
            if (samplePerPixel != 4) {
                // TIFFTAG_EXTRASAMPLES is supposed to be set when RGB image data has an alpha channel (the 4th channel in this case).
                throw new IllegalArgumentException("Extra samples are not supported.");
            }
        }
        int bitsPerSample = 1;
        if (dir.isTagPresent(TIFFConstants.TIFFTAG_BITSPERSAMPLE))
            bitsPerSample = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_BITSPERSAMPLE);
        switch(bitsPerSample) {
            case 1:
            case 2:
            case 4:
            case 8:
                break;
            default:
                throw new IllegalArgumentException("Bits per sample " + bitsPerSample + " is not supported.");
        }
        Image img = null;
        int h = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH);
        int w = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH);
        int dpiX = 0;
        int dpiY = 0;
        int resolutionUnit = TIFFConstants.RESUNIT_INCH;
        if (dir.isTagPresent(TIFFConstants.TIFFTAG_RESOLUTIONUNIT))
            resolutionUnit = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_RESOLUTIONUNIT);
        dpiX = getDpi(dir.getField(TIFFConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
        dpiY = getDpi(dir.getField(TIFFConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
        int fillOrder = 1;
        boolean reverse = false;
        TIFFField fillOrderField = dir.getField(TIFFConstants.TIFFTAG_FILLORDER);
        if (fillOrderField != null)
            fillOrder = fillOrderField.getAsInt(0);
        reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB);
        int rowsStrip = h;
        if (// another hack for broken tiffs
        dir.isTagPresent(TIFFConstants.TIFFTAG_ROWSPERSTRIP))
            rowsStrip = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP);
        if (rowsStrip <= 0 || rowsStrip > h)
            rowsStrip = h;
        long[] offset = getArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPOFFSETS);
        long[] size = getArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPBYTECOUNTS);
        if ((size == null || (size.length == 1 && (size[0] == 0 || size[0] + offset[0] > s.length()))) && h == rowsStrip) {
            // some TIFF producers are really lousy, so...
            size = new long[] { s.length() - (int) offset[0] };
        }
        if (compression == TIFFConstants.COMPRESSION_LZW) {
            TIFFField predictorField = dir.getField(TIFFConstants.TIFFTAG_PREDICTOR);
            if (predictorField != null) {
                predictor = predictorField.getAsInt(0);
                if (predictor != 1 && predictor != 2) {
                    throw new RuntimeException("Illegal value for Predictor in TIFF file.");
                }
                if (predictor == 2 && bitsPerSample != 8) {
                    throw new RuntimeException(bitsPerSample + "-bit samples are not supported for Horizontal differencing Predictor.");
                }
            }
            lzwDecoder = new TIFFLZWDecoder(w, predictor, samplePerPixel);
        }
        int rowsLeft = h;
        ByteArrayOutputStream stream = null;
        DeflaterOutputStream zip = null;
        CCITTG4Encoder g4 = null;
        if (bitsPerSample == 1 && samplePerPixel == 1) {
            g4 = new CCITTG4Encoder(w);
        } else {
            stream = new ByteArrayOutputStream();
            if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG)
                zip = new DeflaterOutputStream(stream);
        }
        if (compression == TIFFConstants.COMPRESSION_OJPEG) {
            if ((!dir.isTagPresent(TIFFConstants.TIFFTAG_JPEGIFOFFSET))) {
                throw new IOException("Missing tag(s) for OJPEG compression.");
            }
            int jpegOffset = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFOFFSET);
            int jpegLength = s.length() - jpegOffset;
            if (dir.isTagPresent(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT)) {
                jpegLength = (int) dir.getFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT) + (int) size[0];
            }
            byte[] jpeg = new byte[Math.min(jpegLength, s.length() - jpegOffset)];
            int posFilePointer = s.getFilePointer();
            posFilePointer += jpegOffset;
            s.seek(posFilePointer);
            s.readFully(jpeg);
            img = new Jpeg(jpeg);
        } else if (compression == TIFFConstants.COMPRESSION_JPEG) {
            if (size.length > 1)
                throw new IOException("Compression JPEG is only supported with a single strip. This image has " + size.length + " strips.");
            byte[] jpeg = new byte[(int) size[0]];
            s.seek(offset[0]);
            s.readFully(jpeg);
            img = new Jpeg(jpeg);
        } else {
            for (int k = 0; k < offset.length; ++k) {
                byte[] im = new byte[(int) size[k]];
                s.seek(offset[k]);
                s.readFully(im);
                int height = Math.min(rowsStrip, rowsLeft);
                byte[] outBuf = null;
                if (compression != TIFFConstants.COMPRESSION_NONE)
                    outBuf = new byte[(w * bitsPerSample * samplePerPixel + 7) / 8 * height];
                if (reverse)
                    TIFFFaxDecoder.reverseBits(im);
                switch(compression) {
                    case TIFFConstants.COMPRESSION_DEFLATE:
                    case TIFFConstants.COMPRESSION_ADOBE_DEFLATE:
                        inflate(im, outBuf);
                        break;
                    case TIFFConstants.COMPRESSION_NONE:
                        outBuf = im;
                        break;
                    case TIFFConstants.COMPRESSION_PACKBITS:
                        decodePackbits(im, outBuf);
                        break;
                    case TIFFConstants.COMPRESSION_LZW:
                        lzwDecoder.decode(im, outBuf, height);
                        break;
                }
                if (bitsPerSample == 1 && samplePerPixel == 1) {
                    g4.fax4Encode(outBuf, height);
                } else {
                    zip.write(outBuf);
                }
                rowsLeft -= rowsStrip;
            }
            if (bitsPerSample == 1 && samplePerPixel == 1) {
                img = Image.getInstance(w, h, false, Image.CCITTG4, photometric == TIFFConstants.PHOTOMETRIC_MINISBLACK ? Image.CCITT_BLACKIS1 : 0, g4.close());
            } else {
                zip.close();
                img = Image.getInstance(w, h, samplePerPixel, bitsPerSample, stream.toByteArray());
                img.setDeflated(true);
            }
        }
        img.setDpi(dpiX, dpiY);
        if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG) {
            if (dir.isTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) {
                try {
                    TIFFField fd = dir.getField(TIFFConstants.TIFFTAG_ICCPROFILE);
                    ICC_Profile icc_prof = ICC_Profile.getInstance(fd.getAsBytes());
                    if (samplePerPixel == icc_prof.getNumComponents())
                        img.tagICC(icc_prof);
                } catch (RuntimeException e) {
                // empty
                }
            }
            if (dir.isTagPresent(TIFFConstants.TIFFTAG_COLORMAP)) {
                TIFFField fd = dir.getField(TIFFConstants.TIFFTAG_COLORMAP);
                char[] rgb = fd.getAsChars();
                byte[] palette = new byte[rgb.length];
                int gColor = rgb.length / 3;
                int bColor = gColor * 2;
                for (int k = 0; k < gColor; ++k) {
                    palette[k * 3] = (byte) (rgb[k] >>> 8);
                    palette[k * 3 + 1] = (byte) (rgb[k + gColor] >>> 8);
                    palette[k * 3 + 2] = (byte) (rgb[k + bColor] >>> 8);
                }
                PdfArray indexed = new PdfArray();
                indexed.add(PdfName.INDEXED);
                indexed.add(PdfName.DEVICERGB);
                indexed.add(new PdfNumber(gColor - 1));
                indexed.add(new PdfString(palette));
                PdfDictionary additional = new PdfDictionary();
                additional.put(PdfName.COLORSPACE, indexed);
                img.setAdditional(additional);
            }
            img.setOriginalType(Image.ORIGINAL_TIFF);
        }
        if (photometric == TIFFConstants.PHOTOMETRIC_MINISWHITE)
            img.setInverted(true);
        if (rotation != 0)
            img.setInitialRotation(rotation);
        return img;
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : PdfArray(com.lowagie.text.pdf.PdfArray) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) PdfNumber(com.lowagie.text.pdf.PdfNumber) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Jpeg(com.lowagie.text.Jpeg) Image(com.lowagie.text.Image) PdfString(com.lowagie.text.pdf.PdfString) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) ExceptionConverter(com.lowagie.text.ExceptionConverter) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ICC_Profile(java.awt.color.ICC_Profile)

Aggregations

PdfNumber (com.lowagie.text.pdf.PdfNumber)15 PdfDictionary (com.lowagie.text.pdf.PdfDictionary)13 PdfArray (com.lowagie.text.pdf.PdfArray)11 IOException (java.io.IOException)6 PdfObject (com.lowagie.text.pdf.PdfObject)5 PdfString (com.lowagie.text.pdf.PdfString)5 Image (com.lowagie.text.Image)4 ExceptionConverter (com.lowagie.text.ExceptionConverter)3 ImgRaw (com.lowagie.text.ImgRaw)3 PdfIndirectReference (com.lowagie.text.pdf.PdfIndirectReference)3 PdfLiteral (com.lowagie.text.pdf.PdfLiteral)3 DocumentException (com.lowagie.text.DocumentException)2 Rectangle (com.lowagie.text.Rectangle)2 ExtendedColor (com.lowagie.text.pdf.ExtendedColor)2 PRIndirectReference (com.lowagie.text.pdf.PRIndirectReference)2 PatternColor (com.lowagie.text.pdf.PatternColor)2 PdfImage (com.lowagie.text.pdf.PdfImage)2 PdfName (com.lowagie.text.pdf.PdfName)2 PdfXConformanceException (com.lowagie.text.pdf.PdfXConformanceException)2 ShadingColor (com.lowagie.text.pdf.ShadingColor)2