javax.media : JMF « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » JMFScreenshots 
javax.media
    
//-----------------------------------------------------------------------//
//                                                                       //
//                           T e s t I m a g e 3                         //
//                                                                       //
//  Copyright (C) Herve Bitteur 2000-2007. All rights reserved.          //
//  This software is released under the terms of the GNU General Public  //
//  License. Please contact the author at herve.bitteur@laposte.net      //
//  to report bugs & suggestions.                                        //
//-----------------------------------------------------------------------//
package omr.jai;

import omr.jai.ImageInfo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Arrays;
import javax.media.jai.InterpolationNearest;
import javax.media.jai.InterpolationBilinear;

public class TestImage3
    extends JPanel
{
    //RenderedImage image;
    PlanarImage image;

    // Affine tranform
    final float ratio = 4f;
    AffineTransform scaleXform = AffineTransform.getScaleInstance(ratio, ratio);

    private static char WHITE = '-';    // And transparent
    private static char[] charTable = new char[]
    {
        '#',  // 0 Black
        '$',  // 1
        '*',  // 2
        '0',  // 3
        'o',  // 4
        '+',  // 5
        '.',  // 6
        WHITE // 7
    };

    //------------//
    // TestImage3 //
    //------------//
    public TestImage3()
    {
        JFrame frame = new JFrame(getClass().getName());
        Container pane = frame.getContentPane();
        pane.setLayout(new BorderLayout());

        pane.add(this);

        image = decodeImage(new String[] {
                "#-###############",
                "-----------------",
                "#################",
                "-----------------",
                "#################",
                "-----------------",
                "#################",
                "-----------------",
                "#################",
                "-----------------",
                "#################",
                "-----------------",
                "-----------------",
                "-----------------",
                "---####----------",
                "-------##--------",
                "---------####----",
                "-------------#---",
                "-------------#---",
                "-----------------",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "--#############--",
                "-----------------",
                "-----------------",
                "---####----------",
                "-------##--------",
                "---------####----",
                "-------------#---",
                "-------------#---",
                "-----------------",
                "--#############--",
                "--#############--",
                "-----------------",
                "-----------------"
        });

        //        checkImageFormat();

        ImageInfo.print(image);

        // Scaling
        final float scale = 1f;
        ParameterBlock pb = new ParameterBlock()
            .addSource(image)
            .add(scale)
            .add(scale)
            .add(0f)
            .add(0f)
            .add(new InterpolationNearest());
        image = JAI.create("scale", pb);
        dumpPixels(0057);

        if (false) {
            System.out.println("\nBand Selection");
            image = JAI.create("bandselect",image,new int[] {012});
            ImageInfo.print(image);
            dumpPixels(0057);
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100100);
        frame.pack();
        frame.setSize(100250);
        frame.setVisible(true);
    }

    private void dumpPixels (int x0,
                             int y0,
                             int w,
                             int h)
    {
        Raster raster = image.getData();
        int[] pixel = null;
        System.out.print("pixels=");
        for (int y = y0; y < y0+h; y++) {
            System.out.println();
            for (int x = x0; x <= x0+w; x++) {
                pixel = raster.getPixel(x, y, pixel);
                System.out.print(" [");
                for (int i = 0; i < pixel.length; i++) {
                    System.out.print(String.format("%3x", pixel[i]));
                }
                System.out.print("]");
            }
        }
        System.out.println();
    }


    //------//
    // main //
    //------//
    public static void main(String... args)
    {
        new TestImage3();
    }

    //----------------//
    // paintComponent //
    //----------------//
    public void paintComponent(Graphics g)
    {
        // For background
        super.paintComponent(g);

        // Meant for visual check
        if (image != null) {

            Graphics2D g2 = (Graphics2Dg;

            g2.drawRenderedImage (image, scaleXform);
            //g2.drawImage (image, 1, 1, this);
        }
    }

    //-------------//
    // decodeImage //
    //-------------//
    public static PlanarImage decodeImage (String[] rows)
    {
        // Create the DataBuffer to hold the pixel samples
        final int width = rows[0].length();
        final int height = rows.length;

        // Create Raster
        Raster raster;
        if (true) {
            raster = Raster.createPackedRaster
            (DataBuffer.TYPE_INT, width, height,
             new int[] {0x00ff00000x0000ff000x000000ff0xff000000},// bandMasks RGBA
             null);
        else {
            raster = Raster.createInterleavedRaster
                (DataBuffer.TYPE_BYTE, width, height,
                 4,// num of bands
                 null);
        }

        // Populate the data buffer
        DataBuffer dataBuffer = raster.getDataBuffer();
        int index = 0;
        for (String row : rows) {
            for (int x = 0; x < width; x++) {
                int argb = toARGB(row.charAt(x));
                dataBuffer.setElem(index, argb);
                index++;
            }
        }

        // Dump
//         final int size = width * height;
//         System.out.println("DataBuffer :");
//         for (int i = 0; i < size; i++) {
//             if (i % width == 0) {
//                 System.out.println();
//             }
//             System.out.print(String.format("%8x ", dataBuffer.getElem(i)));
//         }
//         System.out.println();

        // Create the image
        BufferedImage bufferedImage = new BufferedImage
                (width, height, BufferedImage.TYPE_INT_ARGB);
        bufferedImage.setData(raster);

        // Dump
//         System.out.println("BufferedImage :");
//         for (int y = 0; y < height; y++) {
//             System.out.println();
//             for (int x = 0; x < width; x++) {
//                 System.out.print(String.format("%8x ", bufferedImage.getRGB(x, y)));
//             }
//         }
//         System.out.println();

        return PlanarImage.wrapRenderedImage(bufferedImage);
    }

    //--------//
    // toARGB //
    //--------//
    /**
     * Compute the ARGB pixel that corresponds to the given char
     *
     @param c the char
     @return the corresponding pixel value (ARGB format)
     */
    private static int toARGB (char c)
    {
        // Check the char
        if (c == WHITE) {
            return 0x00ffffff;      // Totally transparent / white
        else {
            for (int i = charTable.length -1; i >= 0; i--) {
                if (charTable[i== c) {
                    int level = + i * 36// Range 3 .. 255 (not too bad)
                    return
                        255   << 24 |      // Alpha (opaque)
                        level << 16 |      // R
                        level <<  |      // G
                        level;             // B
                }
            }
        }

        return 0x00ffffff;      // Totally transparent / white
    }

    //------------------//
    // checkImageFormat //
    //------------------//
    /**
     * Check if the image format (and especially its color model) is
     * properly handled by Audiveris.
     *
     @throws ImageFormatException is the format is not supported
     */
    private void checkImageFormat()
    {
        // Check nb of bands
        int numBands = image.getSampleModel().getNumBands();
        if (numBands != 1) {
            if (numBands == 3) {
                image = colorToGray(image);
            else {
                throw new RuntimeException
                    ("Unsupported sample model" +
                     " numBands=" + numBands);
            }
        }

        // Check pixel size
        ColorModel colorModel = image.getColorModel();
        int pixelSize = colorModel.getPixelSize();
        if (pixelSize != 8) {
            System.out.println("pixelSize=" + pixelSize +
                    " colorModel=" + colorModel);
            image = grayToGray256(image);
        }
    }

    //--------//
    // invert //
    //--------//
    private static PlanarImage invert (PlanarImage image)
    {
        return JAI.create("Invert",
                          new ParameterBlock()
                          .addSource(image)
                          .add(null)
                          .add(null)
                          .add(null)
                          .add(null)
                          .add(null),
                          null);
    }

    //-------------//
    // colorToGray //
    //-------------//
    private static PlanarImage colorToGray (PlanarImage image)
    {
        System.out.println("Converting color image to gray ...");
        double[][] matrix = { {0.114d0.587d0.299d0.0d} };

        return JAI.create("bandcombine",
                          new ParameterBlock()
                          .addSource(image)
                          .add(matrix),
                          null);
    }

    //---------------//
    // grayToGray256 //
    //---------------//
    private static PlanarImage grayToGray256 (PlanarImage image)
    {
        System.out.println("Converting gray image to gray-256 ...");

        ColorSpace colorSpace = ColorSpace.getInstance
            (java.awt.color.ColorSpace.CS_GRAY);

//        int[] bits = new int[]{8};
//        int opaque = Transparency.OPAQUE;
//        int dataType = DataBuffer.TYPE_BYTE;
//        ColorModel colorModel = new ComponentColorModel
//            (colorSpace, bits, false, false, opaque, dataType);

        return JAI.create("colorConvert", image, colorSpace, null);
    }

}
//-----------------------------------------------------------------------//
////
//I m a g e I n f o                           //
////
//Copyright (C) Herve Bitteur 2000-2007. All rights reserved.          //
//This software is released under the terms of the GNU General Public  //
//License. Please contact the author at herve.bitteur@laposte.net      //
//to report bugs & suggestions.                                        //
//-----------------------------------------------------------------------//


import java.awt.Transparency;
import java.awt.image.*;
import java.io.File;
import javax.media.jai.*;

public class ImageInfo
{
public static void main(String[] args)
{
// Open the image (using the name passed as a command line parameter)
PlanarImage pi = JAI.create("fileload", args[0]);

// Get the image file size (non-JAI related).
File image = new File(args[0]);
System.out.println("Image file size: "+image.length()+" bytes.");

print(pi);
}

public static void print (PlanarImage pi)
{
// Show the image dimensions and coordinates.
System.out.print("Image Dimensions: ");
System.out.print(pi.getWidth()+"x"+pi.getHeight()+" pixels");

// Remember getMaxX and getMaxY return the coordinate of the next point!
System.out.println(" (from "+pi.getMinX()+","+pi.getMinY()+" to " +
(pi.getMaxX()-1)+","+(pi.getMaxY()-1)+")");
if ((pi.getNumXTiles() != 1)||(pi.getNumYTiles() != 1)) { // Is it tiled?
// Tiles number, dimensions and coordinates.
System.out.print("Tiles: ");
System.out.print(pi.getTileWidth()+"x"+pi.getTileHeight()+" pixels"+
" ("+pi.getNumXTiles()+"x"+pi.getNumYTiles()+" tiles)");
System.out.print(" (from "+pi.getMinTileX()+","+pi.getMinTileY()+
" to "+pi.getMaxTileX()+","+pi.getMaxTileY()+")");
System.out.println(" offset: "+pi.getTileGridXOffset()+","+
pi.getTileGridXOffset());
}

// Display info about the SampleModel of the image.
SampleModel sm = pi.getSampleModel();
System.out.println("Number of bands: "+sm.getNumBands());
System.out.print("Data type: ");
switch(sm.getDataType()) {
case DataBuffer.TYPE_BYTE: System.out.println("byte")break;
case DataBuffer.TYPE_SHORT: System.out.println("short")break;
case DataBuffer.TYPE_USHORT: System.out.println("ushort")break;
case DataBuffer.TYPE_INT: System.out.println("int")break;
case DataBuffer.TYPE_FLOAT: System.out.println("float")break;
case DataBuffer.TYPE_DOUBLE: System.out.println("double")break;
case DataBuffer.TYPE_UNDEFINED:System.out.println("undefined")break;
}

// Display info about the ColorModel of the image.
ColorModel cm = pi.getColorModel();
if (cm != null)
{
System.out.println("Number of color components: "+
cm.getNumComponents());
System.out.println("Bits per pixel: "+cm.getPixelSize());
System.out.print("Image Transparency: ");
switch(cm.getTransparency()) {
case Transparency.OPAQUE: System.out.println("opaque")break;
case Transparency.BITMASK: System.out.println("bitmask")break;
case Transparency.TRANSLUCENT:
System.out.println("translucent")break;
}
}
else System.out.println("No color model.");
}
}

   
    
    
    
  
Related examples in the same category
1.Query the installed version of the JMFQuery the installed version of the JMF
2.Select a media file from tjelocal file system and gain statistics
3.Renderer for RGB images using AWT Image using Java Media API
4.A motion detection algoritm for use with the Java Media Framework API (JMF).
5.Media player
6.Sample program to demonstrate FramePositioningControl.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.