Back to index

wims  3.65+svn20090927
TestEncoder.java
Go to the documentation of this file.
00001 package rene.util;
00002 
00003 import java.awt.Color;
00004 import java.awt.Font;
00005 import java.awt.FontMetrics;
00006 import java.awt.Frame;
00007 import java.awt.Graphics;
00008 import java.awt.Image;
00009 import java.awt.event.WindowAdapter;
00010 import java.awt.event.WindowEvent;
00011 import java.awt.event.WindowListener;
00012 import java.awt.image.ImageObserver;
00013 import java.awt.image.MemoryImageSource;
00014 import java.awt.image.PixelGrabber;
00015 import java.io.FileOutputStream;
00016 import java.io.IOException;
00017 import java.util.Calendar;
00018 
00047 public class TestEncoder extends Frame
00048 {
00049     String          message;
00050     String          timeStr;
00051     Image           clockImage = null;
00052     int             hour, minute;
00053     boolean         encodeAlpha;
00054     int             filter;
00055     int             compressionLevel;
00056     int             pixelDepth;
00057     String          filename;
00058     boolean         fileSaved = false;
00059 
00060     public TestEncoder(String s)
00061     {
00062         super(s);
00063         setSize(200,200);
00064     }
00065     
00066     public void drawClockImage(int hour, int minute)
00067     {
00068         // variables used for drawing hands of clock
00069         Graphics g;
00070         Font smallFont = new Font("Helvetica", Font.PLAIN, 9 );
00071         FontMetrics fm;
00072 
00073         int x0, y0, x1, y1;
00074         double angle;
00075         
00076         g = clockImage.getGraphics();
00077         g.setFont( smallFont );
00078         fm = g.getFontMetrics();
00079 
00080         // draw the clock face; yellow for AM, blue for PM
00081         if (hour < 12)
00082         {
00083             g.setColor( new Color( 255, 255, 192 ) );
00084         }
00085         else
00086         {
00087             g.setColor( new Color( 192, 192, 255) );
00088         }
00089         g.fillOval(10, 10, 80, 80);
00090         g.setColor( Color.black );
00091         g.drawOval(10, 10, 80, 80);
00092         g.drawOval( 48, 48, 4, 4);
00093         
00094         /* draw the 12 / 3 / 6/ 9 numerals */
00095         g.setFont( smallFont );
00096         g.drawString( "12", 50-fm.stringWidth("12")/2, 11+fm.getAscent() );
00097         g.drawString( "3", 88-fm.stringWidth("3"), 50+fm.getAscent()/2 );
00098         g.drawString( "6", 50-fm.stringWidth("6")/2, 88 );
00099         g.drawString( "9", 12, 50+fm.getAscent()/2 );
00100         
00101         x0 = 50;
00102         y0 = 50;
00103 
00104         /* draw the hour hand */
00105         hour %= 12;
00106         angle = -(hour * 30 + minute/2) + 90;
00107         angle = angle * (Math.PI / 180.0);
00108 
00109         x1 = (int) (x0 + 28 * (Math.cos( angle ) ));
00110         y1 = (int) (y0 - 28 * (Math.sin( angle ) ));
00111         g.drawLine( x0, y0, x1, y1 );
00112         
00113         /* and the minute hand */
00114         angle = -(minute * 6) + 90;
00115         angle = angle * Math.PI / 180.0;
00116         x1 = (int) (x0 + 35 * (Math.cos( angle ) ));
00117         y1 = (int) (y0 - 35 * (Math.sin( angle ) )); 
00118         g.drawLine( x0, y0, x1, y1 );
00119     }
00120 
00121     public void addAlphaToImage()
00122     {
00123         int width=100;
00124         int height=100;
00125         int alphaMask = 0;
00126         int[] pixels = new int[width * height];
00127 
00128         PixelGrabber pg = new PixelGrabber(clockImage, 0, 0,
00129             width, height, pixels, 0, width);
00130         try {
00131             pg.grabPixels();
00132         }
00133         catch (InterruptedException e) {
00134             System.err.println("interrupted waiting for pixels!");
00135             return;
00136         }
00137         if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
00138             System.err.println("image fetch aborted or errored");
00139             return;
00140         }
00141         for (int i=0; i<width*height; i++)
00142         {
00143             if ((i % width) == 0)
00144             {
00145                 alphaMask = (alphaMask >> 24) & 0xff;
00146                 alphaMask += 2;
00147                 if (alphaMask > 255)
00148                 {
00149                     alphaMask = 255;
00150                 }
00151                 alphaMask = (alphaMask << 24) & 0xff000000;
00152             }
00153             pixels[i] = (pixels[i] & 0xffffff) | alphaMask;
00154         }
00155         clockImage= createImage(new MemoryImageSource(width, height, pixels,
00156             0, width));
00157     }
00158 
00159     public void paint( Graphics g )
00160     {
00161         if (clockImage == null)
00162         {
00163             clockImage = createImage( 100, 100 );
00164         }
00165         if (clockImage != null)
00166         {
00167             if (!fileSaved)
00168             {
00169                 drawClockImage(hour, minute);
00170                 if (encodeAlpha) { addAlphaToImage(); }
00171                 saveClockImage();
00172                 fileSaved = true;
00173             }
00174             g.drawImage( clockImage, 50, 20, null );
00175         }
00176         if (message != null)
00177         {
00178             g.drawString( message, 10, 140 );
00179         }
00180     }
00181 
00182     protected static void usage()
00183     {
00184         System.out.print("Usage: TestEncoder -alpha -filter n -compress c");
00185         System.out.println("-alpha means to use alpha encoding (default none)");
00186         System.out.println("n is filter number 0=none (default), 1=sub, 2=up");
00187         System.out.println("c is compression factor (0-9); 1 default");
00188         System.exit(0);
00189     }
00190 
00191     public static void main(String[] args)
00192     {
00193         int i;
00194 
00195         TestEncoder te = new TestEncoder("Test PNG Alpha/Filter Encoder");
00196         i = 0;
00197         te.encodeAlpha = false;
00198         te.filter = 0;
00199         te.pixelDepth = 24;
00200         te.compressionLevel = 1;
00201         while (i < args.length)
00202         {
00203             if (args[i].equals("-alpha"))
00204             {
00205                 te.encodeAlpha = true;
00206                 i++;
00207             }
00208             else if (args[i].equals("-filter"))
00209             {
00210                 if (i != args.length-1)
00211                 {
00212                     try
00213                     {
00214                         te.filter = Integer.parseInt(args[i+1]);
00215                     }
00216                     catch (Exception e)
00217                     {
00218                         usage();
00219                         break;
00220                     }
00221                 }
00222                 i += 2;
00223             }
00224             else if (args[i].equals("-compress"))
00225             {
00226                 if (i != args.length-1)
00227                 {
00228                     try
00229                     {
00230                         te.compressionLevel = Integer.parseInt(args[i+1]);
00231                     }
00232                     catch (Exception e)
00233                     {
00234                         usage();
00235                         break;
00236                     }
00237                 }
00238                 i += 2;
00239             }
00240             else
00241             {
00242                 usage();
00243                 break;
00244             }
00245         }
00246         if (te.pixelDepth == 8)
00247         {
00248             te.encodeAlpha = false;
00249         }
00250         te.doYourThing();
00251     }
00252     
00253     public void doYourThing()
00254     {
00255         Image   img;
00256 
00257         // The resultant PNG data will go into this array...
00258         
00259         Calendar cal = Calendar.getInstance();  
00260 
00261         hour = cal.get(Calendar.HOUR);
00262         if (cal.get(Calendar.AM_PM) == 1)
00263         {
00264             hour += 12;
00265         }
00266         hour %= 24;
00267         minute = cal.get(Calendar.MINUTE);
00268 
00269         /*
00270          * format the time to a string of the form
00271          *    hhmm
00272          * for use in the filename
00273          */
00274         
00275         timeStr = Integer.toString( minute );
00276         if (minute < 10)
00277         {
00278             timeStr = "0" + timeStr;
00279         }
00280         timeStr = Integer.toString( hour ) + timeStr;
00281         if (hour < 10)
00282         {
00283             timeStr = "0" + timeStr;
00284         }
00285 
00286         filename = (encodeAlpha) ? "alphaclock" : "clock";
00287         filename += timeStr + "_f" + filter + 
00288            compressionLevel + ".png";
00289 
00290         message =  "File: " + filename;
00291 
00292 
00293         WindowListener l = new WindowAdapter() {
00294                         public void windowClosing(WindowEvent e) {
00295                         System.exit(0);
00296                        }
00297         };
00298         addWindowListener(l);
00299         show();
00300     }
00301     
00302     public void saveClockImage()
00303     {
00304         byte[] pngbytes;
00305         PngEncoder png =  new PngEncoder( clockImage,
00306             (encodeAlpha) ? PngEncoder.ENCODE_ALPHA : PngEncoder.NO_ALPHA,
00307             filter, compressionLevel);
00308 
00309         try
00310         {
00311             FileOutputStream outfile = new FileOutputStream( filename );
00312             pngbytes = png.pngEncode();
00313             if (pngbytes == null)
00314             {
00315                 System.out.println("Null image");
00316             }
00317             else
00318             {
00319                 outfile.write( pngbytes );
00320             }
00321             outfile.flush();
00322             outfile.close();
00323         }
00324         catch (IOException e)
00325         {
00326             e.printStackTrace();
00327         }
00328     }       
00329 
00330 }