summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcst/transaction/tools/TestRasdamanUtils.java
blob: 0b6d7946827af35283d2c5e81bceb9476310d175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcst.transaction.tools;

//~--- non-JDK imports --------------------------------------------------------
import java.awt.Graphics;
import java.awt.Panel;
import org.odmg.*;

import rasj.*;

//~--- JDK imports ------------------------------------------------------------

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

/** Test class for various Rasdaman tasks. Comment/uncomment the lines in the
 * main() function and see what happens.
 *
 * @author Andrei Aiordachioaie
 */
public class TestRasdamanUtils {

    private static RasdamanUtils myUtils;

    public static void main(String[] args) throws ODMGException, RasResultIsNoIntervalException, IOException, Exception {
//		myUtils = new RasdamanUtils("http://kahlua.eecs.jacobs-university.de" + ":7001", "RASBASE");
        myUtils = new RasdamanUtils("http://localhost" + ":7001", "RASBASE");

        myUtils.init();

        /* Try to comment/uncomment lines below ... */
//      myUtils.insertGrayImageAsArray("andreiTest1");
//      myUtils.loadCoverage("mr");
//      myUtils.printAllCollections();
//      testLoadRasdaman("andreiTest1");
//		testLoadRasdaman("andreiFlickrColor");
//      insertImageFromInternet("andreiFlickr1", "http://farm4.static.flickr.com/3347/3500129555_137c537e75_m.jpg");
        myUtils.deleteCollection("andreiFlickrColor");
//      myUtils.commitAndClose();
//      myUtils.deleteCollection("flickr1");

//      updateImageFromInternet("andreiFlickr1", "http://localhost/peta/car.jpeg");

//        insertImageFromInternet("andreiFlickrColor", "http://localhost/peta/flickr1.jpg");

//      try
//      {
//          myUtils.deleteCollection("andreiFlickrColor");
//          myUtils.commitAndClose();
//      }
//      catch (Exception e)
//      {
//          e.printStackTrace();
//          myUtils.abortAndClose();
//      }

//      System.err.println("---------------------");
//      insertImageFromInternet("andreiWedding1", "http://farm3.static.flickr.com/2483/3758629356_d551cd5e6e.jpg");
//      System.err.println("---------------------");
//      testLoadRasdaman("andreiWedding1");

        /* Stop commenting/uncommenting lines here */

        try {
            myUtils.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            myUtils.abortAndClose();
        }

//        understandColorImage("http://localhost/peta/flickr1.jpg");
    }

    private static void insertImageFromInternet(String identifier, String href) {
        try {
            // Step 1: fetch the image from Internet
            URL url = new URL(href);
            BufferedImage img0 = ImageIO.read(url);
            BufferedImage img = myUtils.convertImageToGray(img0);

            // Display image for check
            displayImage(identifier, img);

            // Step 2: Insert image into rasdaman
            myUtils.insertGrayImageAsArray(identifier, img);
//			myUtils.insertColorImageAsArray(identifier, img);
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void updateImageFromInternet(String identifier, String href) {
        try {
            // Step 1: fetch the image from Internet
            URL url = new URL(href);
            BufferedImage img0 = ImageIO.read(url);
            BufferedImage img = myUtils.convertImageToGray(img0);

            // Display image for check
            displayImage(identifier, img);

            // Step 2: Update image into rasdaman
            myUtils.updateGrayImageWithArray(identifier, img, 0, 0, img.getWidth(), img.getHeight());
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void testLoadRasdaman(String identifier) {
        try {
            BufferedImage img = myUtils.loadCoverage(identifier);

            if (img == null) {
                throw new Exception("Did not receive image from MyRasUtils !");
            }
            displayImage(identifier, img);
        } catch (Exception e) {
            System.err.println("Error !");
            e.printStackTrace();
        }
    }

    public static void displayImage(String name, BufferedImage img) {
        JFrame f = new JFrame("Image " + name);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new ShowImage(img));
        f.setSize(img.getWidth() + 50, img.getHeight() + 50);
        f.setVisible(true);
    }

    private static void understandColorImage(String href) {
        String identifier = "image1";
        try {
            // Step 1: fetch the image from Internet
            URL url = new URL(href);
            BufferedImage img0 = ImageIO.read(url);
            BufferedImage img = myUtils.convertImageToGray(img0);

            // Display image for check
            displayImage(identifier, img);

            // Step 2: Insert image into rasdaman
            System.out.println(img);        // gray-image
            System.out.println(img0);       // color image
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * Panel to display loaded image
     *
     *
     * @version        09.Jul 2009
     * @author         Andrei Aiordachioaie
     */
    public static class ShowImage extends Panel {

        BufferedImage image;

        public ShowImage(BufferedImage img) {
            this.image = img;
        }

        public void paint(Graphics g) {
            if (image != null) {
                g.drawImage(image, 0, 0, null);
            }
        }
    }
}