Package org.apache.sanselan

Examples of org.apache.sanselan.ImageReadException


        int paletteInfo = convertByteArrayToShort("PaletteInfo", 68, pcxHeaderBytes, BYTE_ORDER_LSB);
        int hScreenSize = convertByteArrayToShort("hScreenSize", 70, pcxHeaderBytes, BYTE_ORDER_LSB);
        int vScreenSize = convertByteArrayToShort("vScreenSize", 72, pcxHeaderBytes, BYTE_ORDER_LSB);

        if (manufacturer != 10)
            throw new ImageReadException("Not a Valid PCX File: manufacturer is " + manufacturer);
        if (isStrict)
        {
            // Note that reserved is sometimes set to a non-zero value
            // by Paintbrush itself, so it shouldn't be enforced.
            if (bytesPerLine % 2 != 0)
                throw new ImageReadException("Not a Valid PCX File: bytesPerLine is odd");
        }

        return new PcxHeader(manufacturer, version, encoding, bitsPerPixel,
                xMin, yMin, xMax, yMax, hDpi, vDpi, colormap, reserved,
                nPlanes, bytesPerLine, paletteInfo, hScreenSize, vScreenSize);
View Full Code Here


            int r;
            for (int bytesRead = 0; bytesRead < samples.length; bytesRead += r)
            {
                r = is.read(samples, bytesRead, samples.length - bytesRead);
                if (r < 0)
                    throw new ImageReadException("Premature end of file reading image data");
            }
        }
        else
        {
            if (pcxHeader.encoding == PcxHeader.ENCODING_RLE)
            {
                for (int bytesRead = 0; bytesRead < samples.length;)
                {
                    byte b = readByte("Pixel", is, "Error reading image data");
                    int count;
                    byte sample;
                    if ((b & 0xc0) == 0xc0)
                    {
                        count = b & 0x3f;
                        sample = readByte("Pixel", is, "Error reading image data");
                    }
                    else
                    {
                        count = 1;
                        sample = b;
                    }
                    for (int i = 0; i < count && bytesRead + i < samples.length; i++)
                        samples[bytesRead + i] = sample;
                    bytesRead += count;
                }
            }
            else
                throw new ImageReadException("Invalid PCX encoding " + pcxHeader.encoding);
        }
    }
View Full Code Here

    private BufferedImage readImage(PcxHeader pcxHeader, InputStream is, ByteSource byteSource)
            throws ImageReadException, IOException
    {
        int xSize = pcxHeader.xMax - pcxHeader.xMin + 1;
        if (xSize < 0)
            throw new ImageReadException("Image width is negative");
        int ySize = pcxHeader.yMax - pcxHeader.yMin + 1;
        if (ySize < 0)
            throw new ImageReadException("Image height is negative");

        int scanlineLength = pcxHeader.bytesPerLine * pcxHeader.nPlanes;
        byte[] scanline = new byte[scanlineLength];
        if ((pcxHeader.bitsPerPixel == 1 || pcxHeader.bitsPerPixel == 2
                || pcxHeader.bitsPerPixel == 4 || pcxHeader.bitsPerPixel == 8) &&
                pcxHeader.nPlanes == 1)
        {
            int bytesPerImageRow = (xSize * pcxHeader.bitsPerPixel + 7) / 8;
            byte[] image = new byte[ySize * bytesPerImageRow];
            for (int y = 0; y < ySize; y++)
            {
                readScanLine(pcxHeader, is, scanline);
                System.arraycopy(scanline, 0, image, y*bytesPerImageRow, bytesPerImageRow);
            }
            DataBufferByte dataBuffer = new DataBufferByte(image, image.length);
            int[] palette;
            if (pcxHeader.bitsPerPixel == 1)
                palette = new int[] { 0x000000, 0xffffff };
            else if (pcxHeader.bitsPerPixel == 8)
            {
                // Normally the palette is read 769 bytes from the end of the file.
                // However DCX files have multiple PCX images in one file, so
                // there could be extra data before the end! So try look for the palette
                // immediately after the image data first.
                palette = read256ColorPalette(is);
                if (palette == null)
                    palette = read256ColorPaletteFromEndOfFile(byteSource);
                if (palette == null)
                    throw new ImageReadException(
                            "No 256 color palette found in image that needs it");
            }
            else
                palette = pcxHeader.colormap;
            WritableRaster raster;
            if (pcxHeader.bitsPerPixel == 8)
            {
                raster = WritableRaster.createInterleavedRaster(dataBuffer,
                        xSize, ySize, bytesPerImageRow, 1, new int[]{0}, null);
            }
            else
            {
                raster =  WritableRaster.createPackedRaster(dataBuffer,
                        xSize, ySize, pcxHeader.bitsPerPixel, null);
            }
            IndexColorModel colorModel = new IndexColorModel(pcxHeader.bitsPerPixel,
                    1 << pcxHeader.bitsPerPixel, palette, 0, false, -1, DataBuffer.TYPE_BYTE);
            return new BufferedImage(colorModel, raster,
                    colorModel.isAlphaPremultiplied(), new Properties());
        }
        else if (pcxHeader.bitsPerPixel == 1 && 2 <= pcxHeader.nPlanes
                && pcxHeader.nPlanes <= 4)
        {
            IndexColorModel colorModel = new IndexColorModel(pcxHeader.nPlanes,
                    1 << pcxHeader.nPlanes, pcxHeader.colormap, 0, false, -1, DataBuffer.TYPE_BYTE);
            BufferedImage image = new BufferedImage(xSize, ySize, BufferedImage.TYPE_BYTE_BINARY, colorModel);
            byte[] unpacked = new byte[xSize];
            for (int y = 0; y < ySize; y++)
            {
                readScanLine(pcxHeader, is, scanline);
                int nextByte = 0;
                Arrays.fill(unpacked, (byte) 0);
                for (int plane = 0; plane < pcxHeader.nPlanes; plane++)
                {
                    for (int i = 0; i < pcxHeader.bytesPerLine; i++)
                    {
                        int b = 0xff & scanline[nextByte++];
                        for (int j = 0; j < 8 && 8*i + j < unpacked.length; j++)
                            unpacked[8*i + j] |= (byte) (((b >> (7 - j)) & 0x1) << plane);
                    }
                }
                image.getRaster().setDataElements(0, y, xSize, 1, unpacked);
            }
            return image;
        }
        else if (pcxHeader.bitsPerPixel == 8 && pcxHeader.nPlanes == 3)
        {
            byte[][] image = new byte[3][];
            image[0] = new byte[xSize*ySize];
            image[1] = new byte[xSize*ySize];
            image[2] = new byte[xSize*ySize];
            for (int y = 0; y < ySize; y++)
            {
                readScanLine(pcxHeader, is, scanline);
                System.arraycopy(scanline, 0, image[0], y*xSize, xSize);
                System.arraycopy(scanline, pcxHeader.bytesPerLine,
                        image[1], y*xSize, xSize);
                System.arraycopy(scanline, 2*pcxHeader.bytesPerLine,
                        image[2], y*xSize, xSize);
            }
            DataBufferByte dataBuffer = new DataBufferByte(image, image[0].length);
            WritableRaster raster = WritableRaster.createBandedRaster(dataBuffer,
                    xSize, ySize, xSize, new int[]{0,1,2},
                    new int[]{0,0,0}, null);
            ColorModel colorModel = new ComponentColorModel(
                    ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            return new BufferedImage(colorModel, raster,
                    colorModel.isAlphaPremultiplied(), new Properties());
        }
        else if ((pcxHeader.bitsPerPixel == 24 && pcxHeader.nPlanes == 1) ||
                (pcxHeader.bitsPerPixel == 32 && pcxHeader.nPlanes == 1))
        {
            int rowLength = 3 * xSize;
            byte[] image = new byte[rowLength * ySize];
            for (int y = 0; y < ySize; y++)
            {
                readScanLine(pcxHeader, is, scanline);
                if (pcxHeader.bitsPerPixel == 24)
                    System.arraycopy(scanline, 0, image, y*rowLength, rowLength);
                else
                {
                    for (int x = 0; x < xSize; x++)
                    {
                        image[y*rowLength + 3*x] = scanline[4*x];
                        image[y*rowLength + 3*x + 1] = scanline[4*x + 1];
                        image[y*rowLength + 3*x + 2] = scanline[4*x + 2];
                    }
                }
            }
            DataBufferByte dataBuffer = new DataBufferByte(image, image.length);
            WritableRaster raster = WritableRaster.createInterleavedRaster(
                    dataBuffer, xSize, ySize, rowLength, 3,
                    new int[]{2,1,0}, null);
            ColorModel colorModel = new ComponentColorModel(
                    ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
                    Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
            return new BufferedImage(colorModel, raster,
                    colorModel.isAlphaPremultiplied(), new Properties());
        }
        else
        {
            throw new ImageReadException("Invalid/unsupported image with bitsPerPixel "
                    + pcxHeader.bitsPerPixel + " and planes " + pcxHeader.nPlanes);
        }
    }
View Full Code Here

        // System.out.println("3*width*height: " + 3 * width * height);
        // System.out.println("((width*height+7)/8): "
        // + ((width * height + 7) / 8));

        if (identifier1 != PNM_PREFIX_BYTE)
            throw new ImageReadException("PNM file has invalid header.");

        if (identifier2 == PBM_TEXT_CODE)
            return new PBMFileInfo(width, height, false);
        else if (identifier2 == PBM_RAW_CODE)
            return new PBMFileInfo(width, height, true);
        else if (identifier2 == PGM_TEXT_CODE)
        {
            int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
            return new PGMFileInfo(width, height, false, maxgray);
        } else if (identifier2 == PGM_RAW_CODE)
        {
            int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
            return new PGMFileInfo(width, height, true, maxgray);
        } else if (identifier2 == PPM_TEXT_CODE)
        {
            int max = Integer.parseInt(wsr.readtoWhiteSpace());
            return new PPMFileInfo(width, height, false, max);
        } else if (identifier2 == PPM_RAW_CODE)
        {
            int max = Integer.parseInt(wsr.readtoWhiteSpace());
            // System.out.println("max: " + max);
            return new PPMFileInfo(width, height, true, max);
        } else
            throw new ImageReadException("PNM file has invalid header.");
    }
View Full Code Here

            throws ImageReadException, IOException
    {
        FileInfo info = readHeader(byteSource);

        if (info == null)
            throw new ImageReadException("PNM: Couldn't read Header");

        return new Dimension(info.width, info.height);
    }
View Full Code Here

            throws ImageReadException, IOException
    {
        FileInfo info = readHeader(byteSource);

        if (info == null)
            throw new ImageReadException("PNM: Couldn't read Header");

        ArrayList Comments = new ArrayList();

        int BitsPerPixel = info.getBitDepth() * info.getNumComponents();
        ImageFormat Format = info.getImageType();
View Full Code Here

    private int[] getColorTable(byte bytes[]) throws ImageReadException,
            IOException
    {
        if ((bytes.length % 3) != 0)
            throw new ImageReadException("Bad Color Table Length: "
                    + bytes.length);
        int length = bytes.length / 3;

        int result[] = new int[length];
View Full Code Here

            is = byteSource.getInputStream();
            StringBuilder firstComment = new StringBuilder();
            ByteArrayOutputStream preprocessedFile = BasicCParser.preprocess(
                    is, firstComment, null);
            if (!firstComment.toString().trim().equals("XPM"))
                throw new ImageReadException("Parsing XPM file failed, " +
                        "signature isn't '/* XPM */'");

            XpmParseResult xpmParseResult = new XpmParseResult();
            xpmParseResult.cParser = new BasicCParser(
                    new ByteArrayInputStream(preprocessedFile.toByteArray()));
View Full Code Here

            throws IOException, ImageReadException
    {
        stringBuilder.setLength(0);
        String token = cParser.nextToken();
        if (token.charAt(0) != '"')
            throw new ImageReadException("Parsing XPM file failed, " +
                    "no string found where expected");
        BasicCParser.unescapeString(stringBuilder, token);
        for (token = cParser.nextToken(); token.charAt(0) == '"'; token = cParser.nextToken())
        {
            BasicCParser.unescapeString(stringBuilder, token);
        }
        if (token.equals(","))
            return true;
        else if (token.equals("}"))
            return false;
        else
            throw new ImageReadException("Parsing XPM file failed, " +
                    "no ',' or '}' found where expected");
    }
View Full Code Here

    private XpmHeader parseXpmValuesSection(String row)
            throws ImageReadException
    {
        String[] tokens = BasicCParser.tokenizeRow(row);
        if (tokens.length < 4 && tokens.length > 7)
            throw new ImageReadException("Parsing XPM file failed, " +
                    "<Values> section has incorrect tokens");
        try
        {
            int width = Integer.parseInt(tokens[0]);
            int height = Integer.parseInt(tokens[1]);
            int numColors = Integer.parseInt(tokens[2]);
            int numCharsPerPixel = Integer.parseInt(tokens[3]);
            int xHotSpot = -1;
            int yHotSpot = -1;
            boolean xpmExt = false;
            if (tokens.length >= 6)
            {
                xHotSpot = Integer.parseInt(tokens[4]);
                yHotSpot = Integer.parseInt(tokens[5]);
            }
            if (tokens.length == 5 || tokens.length == 7)
            {
                if (tokens[tokens.length-1].equals("XPMEXT"))
                    xpmExt = true;
                else
                    throw new ImageReadException("Parsing XPM file failed, " +
                            "can't parse <Values> section XPMEXT");
            }
            return new XpmHeader(width, height, numColors, numCharsPerPixel,
                    xHotSpot, yHotSpot, xpmExt);
        }
        catch (NumberFormatException nfe)
        {
            throw new ImageReadException("Parsing XPM file failed, " +
                    "error parsing <Values> section", nfe);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.sanselan.ImageReadException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.