3030
3131public class ImageAveragerNEF extends JPanel implements ActionListener , PropertyChangeListener {
3232
33+ private static final String _AVG = "Average" ;
34+ private static final String _LIGHTEN ="Lighten" ;
35+ private static final String _DARKEN = "Darken" ;
36+ JComboBox <String > selection ;
3337 JFileChooser chooser ;
3438 String choosertitle ;
3539 JButton go ;
@@ -56,7 +60,7 @@ protected void done() {
5660 String result = get ();
5761
5862 JOptionPane .showMessageDialog (null , result , "Processing is over" , JOptionPane .INFORMATION_MESSAGE );
59- go . setEnabled (true );
63+ setEnabledBtn (true );
6064 progressMonitor .close ();
6165 super .done ();
6266 } catch (Exception ex ) {
@@ -83,6 +87,14 @@ protected String doInBackground() throws Exception {
8387 int w = 0 ;
8488 int h = 0 ;
8589 WritableRaster wRaster = null ;
90+ boolean avg =false ;
91+ boolean litn =false ;
92+ if (selection .getSelectedItem ().equals (_AVG )){
93+ avg =true ;
94+ }
95+ else if (selection .getSelectedItem ().equals (_LIGHTEN )){
96+ litn =true ;
97+ }
8698
8799 for (int i = 0 ; i < totalFiles ; i ++) {
88100 File file = new File (fileName .get (i ));
@@ -114,17 +126,48 @@ protected String doInBackground() throws Exception {
114126 Color c =new Color (RGB );
115127
116128
117- averagePixel [width ][height ][0 ] += c .getRed ();
118- averagePixel [width ][height ][1 ] += c .getGreen ();
119- averagePixel [width ][height ][2 ] += c .getBlue ();
129+ if (avg ) {
130+ averagePixel [width ][height ][0 ] += c .getRed ();
131+ averagePixel [width ][height ][1 ] += c .getGreen ();
132+ averagePixel [width ][height ][2 ] += c .getBlue ();
120133
121- if (i == totalFiles - 1 ) // update the raster while
122- // processing last file
123- {
124- averagePixel [width ][height ][0 ] /= totalFiles ;
125- averagePixel [width ][height ][1 ] /= totalFiles ;
126- averagePixel [width ][height ][2 ] /= totalFiles ;
127- wRaster .setPixel (width , height ,averagePixel [width ][height ]);
134+ if (i == totalFiles - 1 ) // update the raster while
135+ // processing last file
136+ {
137+ averagePixel [width ][height ][0 ] /= totalFiles ;
138+ averagePixel [width ][height ][1 ] /= totalFiles ;
139+ averagePixel [width ][height ][2 ] /= totalFiles ;
140+ wRaster .setPixel (width , height , averagePixel [width ][height ]);
141+ }
142+ }
143+ else if (litn ){
144+ averagePixel [width ][height ][0 ] = Math .max (c .getRed (), averagePixel [width ][height ][0 ]);
145+ averagePixel [width ][height ][1 ] = Math .max (c .getGreen (), averagePixel [width ][height ][1 ]);
146+ averagePixel [width ][height ][2 ] = Math .max (c .getBlue (), averagePixel [width ][height ][2 ]);
147+
148+ if (i == totalFiles - 1 ) // update the raster while
149+ // processing last file
150+ {
151+ wRaster .setPixel (width , height , averagePixel [width ][height ]);
152+ }
153+ }
154+ else {
155+ if (i ==0 ){
156+ averagePixel [width ][height ][0 ] = c .getRed ();
157+ averagePixel [width ][height ][1 ] = c .getGreen ();
158+ averagePixel [width ][height ][2 ] = c .getBlue ();
159+ }
160+ else {
161+ averagePixel [width ][height ][0 ] = Math .min (c .getRed (), averagePixel [width ][height ][0 ]);
162+ averagePixel [width ][height ][1 ] = Math .min (c .getGreen (), averagePixel [width ][height ][1 ]);
163+ averagePixel [width ][height ][2 ] = Math .min (c .getBlue (), averagePixel [width ][height ][2 ]);
164+ }
165+
166+ if (i == totalFiles - 1 ) // update the raster while
167+ // processing last file
168+ {
169+ wRaster .setPixel (width , height , averagePixel [width ][height ]);
170+ }
128171 }
129172 }
130173 }
@@ -134,14 +177,16 @@ protected String doInBackground() throws Exception {
134177 final TagRational exposure = exifIFD.getExposureTime();*/
135178
136179
137- progressMonitor .setProgress (i * 100 / totalFiles );
138- progressMonitor .setNote ("File: " + i + "/" + totalFiles + " processed!" );
180+ progressMonitor .setProgress (( i + 1 ) * 100 / totalFiles );
181+ progressMonitor .setNote ("File: " + ( i + 1 ) + "/" + totalFiles + " processed!" );
139182 if (isCancelled () || progressMonitor .isCanceled ()) {
140183 return _ABORTED ;
141184 }
142185 }
143- File file = new File (directoryName + "\\ Output_"
144- + System .currentTimeMillis () + ".tiff" );
186+
187+ String name ="Output_" +selection .getSelectedItem ()+"_" + System .currentTimeMillis () + ".tiff" ;
188+
189+ File file = new File (directoryName + "\\ " +name );
145190 FileOutputStream fileoutput = new FileOutputStream (file );
146191
147192
@@ -152,7 +197,7 @@ protected String doInBackground() throws Exception {
152197 BufferedImage newImage = new BufferedImage (w , h , BufferedImage .TYPE_INT_RGB );
153198 newImage .setData (wRaster );
154199 enc .encode (newImage );
155- ImageIO .write (newImage , "png" , new File (directoryName + "\\ Output_" + System .currentTimeMillis () + "png" ));
200+ // ImageIO.write(newImage, "png", new File(directoryName + "\\Output_" + System.currentTimeMillis() + ". png"));
156201
157202 fileoutput .close ();
158203
@@ -191,16 +236,31 @@ public void windowClosing(WindowEvent e) {
191236 }
192237 });
193238
194- frame .getContentPane ().add (this , "Center" );
239+ final JPanel compsToExperiment = new JPanel ();
240+ GridLayout experimentLayout = new GridLayout (2 ,1 );
241+ compsToExperiment .setLayout (experimentLayout );
242+
243+
195244 frame .setSize (this .getPreferredSize ());
196245 frame .setVisible (true );
246+ selection =new JComboBox <String >(new String []{_AVG , _LIGHTEN , _DARKEN });
247+ compsToExperiment .add (selection );
248+
197249 go = new JButton ("Select Folder" );
198250 go .addActionListener (this );
199- add (go );
251+ compsToExperiment .add (go );
252+ frame .add (compsToExperiment );
253+ frame .setVisible (true );
254+ }
255+
256+ public void setEnabledBtn (boolean val ){
257+ go .setEnabled (val );
258+ selection .setEnabled (val );
200259 }
201260
202261 public void actionPerformed (ActionEvent e ) {
203- go .setEnabled (false );
262+ setEnabledBtn (false );
263+
204264
205265 chooser = new JFileChooser ();
206266 chooser .setCurrentDirectory (new File (
@@ -230,12 +290,13 @@ public boolean accept(File dir, String name) {
230290 }
231291 });
232292
233- progressMonitor = new ProgressMonitor (this , "Loading values ..." , "" , 0 , 100 );
293+ progressMonitor = new ProgressMonitor (this , "Loading Files, please wait ..." , "" , 0 , 100 );
234294 progressMonitor .setMillisToDecideToPopup (1 );
235295 progressMonitor .setMillisToPopup (2 );
236296
237297 for (File file : files ) {
238298 if (file .isFile ()) {
299+ progressMonitor .setProgress (0 );
239300 progressMonitor .setNote ("Loaded: " + file .getCanonicalPath ());
240301 results .add (file .getCanonicalPath ());
241302 }
@@ -253,12 +314,12 @@ public boolean accept(File dir, String name) {
253314 }
254315
255316 } else {
256- go . setEnabled (true );
317+ setEnabledBtn (true );
257318 }
258319 }
259320
260321 public Dimension getPreferredSize () {
261- return new Dimension (400 , 100 );
322+ return new Dimension (400 , 200 );
262323 }
263324
264325}
0 commit comments