File: Iview.java

package info (click to toggle)
tya 1.6-1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 628 kB
  • ctags: 843
  • sloc: ansic: 7,028; asm: 618; java: 497; makefile: 88; sh: 18
file content (232 lines) | stat: -rw-r--r-- 4,894 bytes parent folder | download | duplicates (3)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// test application for TYA
// (c) Mon Sep 22 20:02:41 1997 Albrecht Kleine
//
//
import java.awt.*;
import java.awt.image.*;

public class Iview extends Frame 
{
    Image image,image0;
    IviewCanvas ic;
    Label ilab;
    String xfilename;
    static public void main(String[] args) 
    {
       if (args.length > 0)
	 new Iview(args[0]);
       else
	 System.out.println("Usage: Iview filename");
    }

    // constructor
    Iview(String filename)
    {
        super("IView");
        xfilename=filename;
        ic = new IviewCanvas();
        ilab=new Label();
        add("Center",ic);
        add("North", ilab);
        MenuBar menubar = new MenuBar();
        Menu file = new Menu("File");
        Menu filter = new Menu("Filter");
        menubar.add(file);
        menubar.add(filter);
        file.add("Quit");
        filter.add("Filter red");
        filter.add("Filter green");
        filter.add("Filter blue");
        filter.add("No filter");
        setMenuBar(menubar);
	image0=image=CreateImageDecoder();
	ic.setImage(image);
	ilab.setText(filename);
        pack();
        show();
    }

   private Image CreateImageDecoder()
   {
        Image iimage;
        try
	{
         iimage = Toolkit.getDefaultToolkit().getImage(xfilename);  // jpg,gif
	}
        catch (Exception ex)
	{
           ex.printStackTrace();
	   return null;
	}
        return iimage;
   }

   public boolean action(Event e, Object arg)
    {
        if (e.target instanceof MenuItem) 
        {
            String label = (String) arg;
            if (label.equals("Quit")) 
		System.exit(0);
	    else
	        if (label.equals("Filter red"))
                  ColorFilter(30);
	    else
	        if (label.equals("Filter green"))
                  ColorFilter(31);
	    else
	        if (label.equals("Filter blue"))
                  ColorFilter(32);
	    else
	        if (label.equals("No filter"))
                  ColorFilter(0);
        }
	return false;
    }


   void ColorFilter(int filternr)
   {
      if (filternr!=0)
      {
       Image ii=CreateImageDecoder();
       ImageProducer ipro;
       ipro  = ii.getSource();
       IviewColorFilter gf=new IviewColorFilter(filternr);  
       ipro=new FilteredImageSource(ipro,gf);
       image= this.createImage(ipro);
      }
      else
       image=image0;
      ic.setImage(image);
      ic.repaint();
   }
}



// ----simple mini control for an image display------------------------------
//
class IviewCanvas extends Canvas 
{
    private Image image;
    IviewCanvas() 
    {
        this.image = null;
    }

    public Dimension preferredSize()
    {
      int w,h;
      if (image!=null)
	 {
	    w = image.getWidth(this);
	    h = image.getHeight(this);
         }
      else
	 {
	    w=250;		// for writing an error message
	    h=100;
	 }
       
     return new Dimension(w,h);
    }

    public Image getImage()
    {
     return image;
    }
    

   
    public void setImage(Image image)
    {
      if (image!=null)
      {
        int i;
        long startTime;
        MediaTracker tracker;
        System.out.println("waiting for MediaTracker...");
        startTime=System.currentTimeMillis();
        tracker = new MediaTracker (this);
        tracker.addImage(image,0);
        try 
	{ 
	   tracker.waitForID(0); 
	} 
        catch (Exception ex)
	{
	   System.out.println("error Exception:");
           ex.printStackTrace();
	   return;
	}
        switch (i=tracker.statusID(0,false))
	 {
	  case MediaTracker.COMPLETE:
	    long readyTime=System.currentTimeMillis();
	    System.out.println("MediaTracker OK. ");
	    System.out.println(IviewColorFilter.cnt+" filter calls, Time="+
	       ( readyTime - startTime)+ " msec");
	    break;
	  default:
	    System.out.println("MediaTracker status = "+i);
	    image=null;
	    break;
	 }
      }
      this.image=image;
    }

    public void paint(Graphics g) 
    {
      if (image!=null)
	 g.drawImage(image, 0, 0, this);
      else
	 {
	    Font f=new Font("Courier",Font.BOLD,18);
	    g.setFont(f);
	    g.drawString("NO IMAGE LOADED",30,30);
	 }
    } 
}

//--------------------------------------------------------------------------
// Filter

class IviewColorFilter extends RGBImageFilter 
{
  int nummer;
  static int cnt;
  
  public IviewColorFilter(int filternummer) 
  {
   canFilterIndexColorModel = false;
   nummer=filternummer;
   cnt=0;
  }
   
  public int filterRGB(int x, int y, int rgb)
  {
      int a,r,g,b;
      cnt++;
      // for speed testing 
      r = (rgb & 0x00ff0000);
      g = (rgb & 0x0000ff00);
      b =  rgb & 0x000000ff ;
      a = 0xff<<24;
      switch (nummer)
      {
       case 30: g=0;		// Rotfilter
		b=0;
		return a|r|g|b;
       case 31:	r=0;		// Grnfilter
		b=0;
		return a|r|g|b;
       case 32: r=0;		// Blaufilter
		g=0;
		return a|r|g|b;
      }
     return rgb; 		// default: do nothing
  }
}