package edu.cmu.cs.diamond.opendiamond;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // get scopes
        List<Scope> scopes = ScopeSource.getPredefinedScopeList();
        for (Scope scope : scopes) {
            System.out.println(scope);
        }

        // use first scope
        Scope scope = scopes.get(2);

        // set up the rgb filter
        Filter rgb = null;
        try {
            FilterCode c = new FilterCode(new FileInputStream(
                    "/opt/diamond/lib/fil_rgb.a"));
            rgb = new Filter("RGB", c, "f_eval_img2rgb", "f_init_img2rgb",
                    "f_fini_img2rgb", 1, new String[0], new String[0], 400);
            System.out.println(rgb);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // init diamond
        Search search = Search.getSharedInstance();
        search.setScope(scope);

        // make a new searchlet
        Searchlet searchlet = new Searchlet();
        searchlet.addFilter(rgb);
        searchlet.setApplicationDependencies(new String[] { "RGB" });
        search.setSearchlet(searchlet);

        Result r;
        for (int ii = 0; ii < 1; ii++) {
            // begin search
            search.start();

            // read some results
            int count = 0;
            try {
                while ((r = search.getNextResult()) != null && count < 10) {
                    processResult(r);

                    count++;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            search.stop();
        }
    }

    private static void processResult(Result r) {
        System.out.println(r);

        byte data[] = r.getData();

        try {
            // try reading the data
            ByteArrayInputStream in = new ByteArrayInputStream(data);
            BufferedImage img = ImageIO.read(in);

            // else, try the other one
            data = r.getValue("_rgb_image.rgbimage");
            byte tmp[] = r.getValue("_cols.int");
            int w = Util.extractInt(tmp);
            tmp = r.getValue("_rows.int");
            int h = Util.extractInt(tmp);

            System.out.println(w + "x" + h);

            img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    int i = (y * w + x) * 4;
                    // System.out.println(x);
                    // System.out.println(y);
                    int val = (data[i] & 0xFF) << 16
                            | (data[i + 1] & 0xFF) << 8 | (data[i + 2] & 0xFF);
                    img.setRGB(x, y, val);
                }
            }

            JFrame j = new JFrame();
            j.setLocationByPlatform(true);
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            j.getContentPane().add(new JButton(new ImageIcon(img)));
            j.pack();
            j.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -263,93 +263,8 @@
 eIO;
-%0Aimport javax.swing.ImageIcon;%0Aimport javax.swing.JButton;%0Aimport javax.swing.JFrame;
 %0A%0Apu
@@ -374,21 +374,167 @@
     
-// get scopes
+String scopeName = null;%0A        if (args.length %3E= 1) %7B%0A            scopeName = args%5B0%5D;%0A        %7D%0A        %0A%0A        // get scopes%0A        Scope scope = null;
 %0A   
@@ -617,20 +617,16 @@
 (Scope s
-cope
  : scope
@@ -666,23 +666,98 @@
 ln(s
-cope);%0A
+);%0A            if (s.getName().equals(scopeName)) %7B%0A                scope = s;%0A   
         
 %7D%0A%0A 
@@ -756,74 +756,198 @@
     
+ 
 %7D%0A
-%0A        // use first scope%0A        Scope
+        %7D%0A        %0A        if (scope == null) %7B%0A            System.out.println(%22Cannot find
  scope 
-=
+%5C%22%22 +
  scope
-s.get(2
+Name + %22%5C%22 from command line%22);%0A            System.exit(1
 );
+%0A        %7D%0A        
 %0A%0A  
@@ -3413,32 +3413,34 @@
 %0A            %7D%0A%0A
+//
             JFra
@@ -3456,24 +3456,26 @@
 w JFrame();%0A
+//
             
@@ -3497,32 +3497,34 @@
 Platform(true);%0A
+//
             j.se
@@ -3569,16 +3569,18 @@
 CLOSE);%0A
+//
         
@@ -3636,24 +3636,26 @@
 con(img)));%0A
+//
             
@@ -3660,24 +3660,26 @@
   j.pack();%0A
+//
             

Update Demo to be a useful command line test