Package com.lightcrafts.image

Examples of com.lightcrafts.image.UnknownImageTypeException


        final RawImageInfo rawInfo = (RawImageInfo)imageInfo.getAuxiliaryInfo();
        final DCRaw dcRaw = rawInfo.getDCRaw();

        if (!dcRaw.decodable() || dcRaw.rawColors() != 3)
            throw new UnknownImageTypeException("Unsupported Camera");

        final ColorModel colorModel = RasterFactory.createComponentColorModel(
            DataBuffer.TYPE_USHORT, JAIContext.linearColorSpace, false, false,
            Transparency.OPAQUE
        );

        final ImageMetadata metadata = imageInfo.getMetadata();
        final int imageWidth = metadata.getImageWidth();
        final int imageHeight = metadata.getImageHeight();

        String cacheKey = null;
        File imageFile = null;
        final FileCache fileCache = RawImageCache.getCacheFor( imageInfo );

        if ( CACHE_CONVERSION && fileCache != null ) {
            System.out.println("Checking cache for: " + imageInfo);
            final long t1 = System.currentTimeMillis();
            cacheKey = RawImageCache.getCacheKeyFor( imageInfo );
            if ( cacheKey != null )
                imageFile = RawImageCache.getCachedImageFileFor( cacheKey );
            if (imageFile != null) {
                final String fileName = imageFile.getAbsolutePath();
                try {
                    /* final LCTIFFReader reader = new LCTIFFReader( fileName, true );
                    final PlanarImage image = reader.getImage( thread ); */
                    final PlanarImage image = new LCTIFFReader.TIFFImage(fileName);
                    final long t2 = System.currentTimeMillis();
                    System.out.println("Retrieved Cached image in " + (t2 - t1) + "ms");
                    return image;
                } catch (LCImageLibException e) {
                    // never mind, don't use the cache
                    e.printStackTrace();
                }
            } else
                System.out.println("File not in cache.");
        }

        System.out.println("metadata width: " + imageWidth + ", height: " + imageHeight);

        ProgressIndicator indicator = null;
        if ( thread != null )
            indicator = thread.getProgressIndicator();
        if ( indicator != null ) {
            indicator.setMinimum( 0 );
            indicator.setMaximum( 3 );
        }

        final int filters = dcRaw.getFilters();

        final BufferedImage dcrawImage = (BufferedImage) dcRaw.runDCRaw(DCRaw.dcrawMode.full, false);

        System.out.println("dcraw width: " + dcrawImage.getWidth() + ", height: " + dcrawImage.getHeight());

        // Merge the secondary pixels of Fuji S3 and S5
        if (this instanceof RAFImageType && dcRaw.getSecondaryPixels() &&
            (dcRaw.getCameraMake(true).endsWith("S3PRO") || dcRaw.getCameraMake(true).endsWith("S5PRO")))
        {
            final int rawWidth = dcrawImage.getWidth();
            final int rawHeight = dcrawImage.getHeight();

            int rx, ry, bx, by;

            System.out.println("Filters: 0x" + Long.toString(0xffffffffL & filters, 16));

            switch (filters) {
                case 0x16161616:
                    rx=1; ry=1; bx=0; by=0;
                    break;
                case 0x61616161:
                    rx=1; ry=0; bx=0; by=1;
                    break;
                case 0x49494949:
                    rx=0; ry=1; bx=1; by=0;
                    break;
                case 0x94949494:
                    rx=0; ry=0; bx=1; by=1;
                    break;
                case 0:
                case -1:
                    rx=0; ry=0; bx=0; by=0;
                    break;
                default:
                    throw new UnknownImageTypeException("Unknown Bayer filter pattern.");
            }

            final ShortInterleavedRaster dcrawRaster = (ShortInterleavedRaster) dcrawImage.getRaster();

            final BufferedImage dcrawImage2 = (BufferedImage) dcRaw.runDCRaw(DCRaw.dcrawMode.full, true);
View Full Code Here


    public synchronized RenderedImage runDCRaw(dcrawMode mode, boolean secondaryPixels)
            throws IOException, UnknownImageTypeException, BadImageFileException
    {
        if (!m_decodable || (mode == dcrawMode.full && m_rawColors != 3))
            throw new UnknownImageTypeException("Unsuported Camera");

        RenderedImage result = null;

        File of = null;

        try {
            if (mode == dcrawMode.preview) {
                if (m_thumbWidth >= 1024 && m_thumbHeight >= 768) {
                    mode = dcrawMode.thumb;
                }
            }

            long t1 = System.currentTimeMillis();

            of = File.createTempFile("LZRAWTMP", ".ppm");

            boolean four_colors = false;
            final String makeModel = m_make + ' ' + m_model;
            for (String s : four_color_cameras)
                if (s.equalsIgnoreCase(makeModel)) {
                    four_colors = true;
                    break;
                }

            if (secondaryPixels)
                runDCRawInfo(true);

            String cmd[];
            switch (mode) {
                case full:
                    if (four_colors)
                        cmd = new String[] { DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-f", "-H", "1", "-t", "0", "-o", "0", "-4", m_fileName };
                    else if (m_filters == -1 || (m_make != null && m_make.equalsIgnoreCase("SIGMA")))
                        cmd = new String[] { DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-H", "1", "-t", "0", "-o", "0", "-4", m_fileName };
                    else if (secondaryPixels)
                        cmd = new String[] { DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-j", "-H", "1", "-t", "0", "-s", "1", "-d", "-4", m_fileName };
                    else
                        cmd = new String[] { DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-j", "-H", "1", "-t", "0", "-d", "-4", m_fileName };
                    break;
                case preview:
                    cmd = new String[] { DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-t", "0", "-o", "1", "-w", "-h", m_fileName };
                    break;
                case thumb:
                    cmd = new String[] { DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-e", m_fileName };
                    break;
                default:
                    throw new IllegalArgumentException("Unknown mode " + mode);
            }

            String ofName = null;

            synchronized (DCRaw.class) {
                Process p = null;
                InputStream dcrawStdErr;
                InputStream dcrawStdOut;
                if (ForkDaemon.INSTANCE != null) {
                    ForkDaemon.INSTANCE.invoke(cmd);
                    dcrawStdErr = ForkDaemon.INSTANCE.getStdErr();
                    dcrawStdOut = ForkDaemon.INSTANCE.getStdOut();
                } else {
                    p = Runtime.getRuntime().exec(cmd);
                    dcrawStdErr = new BufferedInputStream(p.getErrorStream());
                    dcrawStdOut = p.getInputStream();
                }

                String line, args;
                // output expected on stderr
                while ((line = readln(dcrawStdErr)) != null) {
                    // System.out.println(line);

                    if ((args = match(line, DCRAW_OUTPUT)) != null)
                        ofName = args.substring(0, args.indexOf(" ..."));
                }

                // Flush stdout just in case...
                while ((line = readln(dcrawStdOut)) != null)
                    ; // System.out.println(line);

                if (p != null) {
                    dcrawStdErr.close();

                    try {
                        p.waitFor();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    m_error = p.exitValue();
                    p.destroy();
                } else
                    m_error = 0;
            }

            System.out.println("dcraw value: " + m_error);

            if (m_error > 0) {
                of.delete();
                throw new BadImageFileException(of);
            }

            if (!ofName.equals(of.getPath())) {
                of.delete();
                of = new File(ofName);
            }

            if (of.getName().endsWith(".jpg") || of.getName().endsWith(".tiff")) {
                if (of.getName().endsWith(".jpg")) {
                    try {
                        LCJPEGReader jpegReader = new LCJPEGReader(of.getPath());
                        result = jpegReader.getImage();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        LCTIFFReader tiffReader = new LCTIFFReader(of.getPath());
                        result = tiffReader.getImage(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                long t2 = System.currentTimeMillis();

                int totalData = result.getWidth() *
                                result.getHeight() *
                                result.getColorModel().getNumColorComponents() *
                                (result.getColorModel().getTransferType() == DataBuffer.TYPE_BYTE ? 1 : 2);

                System.out.println("Read " + totalData + " bytes in " + (t2 - t1) + "ms");
            } else {
                ImageData imageData;
                try {
                    imageData = readPPM(of);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BadImageFileException(of, e);
                }

                // do not change the initial image geometry
                // m_width = Math.min(m_width, imageData.width);
                // m_height = Math.min(m_height, imageData.height);

                long t2 = System.currentTimeMillis();

                int totalData = imageData.width *
                                imageData.height *
                                imageData.bands * (imageData.dataType == DataBuffer.TYPE_BYTE ? 1 : 2);

                System.out.println("Read " + totalData + " bytes in " + (t2 - t1) + "ms");

                final ColorModel cm;
                if (mode == dcrawMode.full) {
                    if (imageData.bands == 1) {
                        cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY),
                                                     false, false,
                                                     Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
                    } else {
                        cm = JAIContext.colorModel_linear16;
                    }
                } else {
                    if (imageData.bands == 3)
                        cm = new ComponentColorModel(JAIContext.sRGBColorSpace,
                                                     false, false,
                                                     Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
                    else if (imageData.bands == 4)
                        cm = new ComponentColorModel(JAIContext.CMYKColorSpace,
                                                     false, false,
                                                     Transparency.OPAQUE, imageData.dataType);
                    else
                        throw new UnknownImageTypeException("Weird number of bands: " + imageData.bands);
                }

                final DataBuffer buf = imageData.dataType == DataBuffer.TYPE_BYTE
                                       ? new DataBufferByte((byte[]) imageData.data,
                                                            imageData.bands * imageData.width * imageData.height)
 
View Full Code Here

TOP

Related Classes of com.lightcrafts.image.UnknownImageTypeException

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.