001/*
002 *  $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/ReduceJPEG.java,v $
003 *  ReduceJPEG.java - A widget to grab URLs through a proxy.
004 *
005 *  $Revision: 1006 $
006 *  $Date: 2015-10-11 19:06:06 -0700 (Sun, 11 Oct 2015) $
007 *  $Locker:  $
008 *
009 *
010 *  Written by Tom Gutwin - WebARTS Design.
011 *  Copyright (C) 2003 WebARTS Design, North Vancouver Canada
012 *  http://www.webarts.bc.ca
013 *
014 *  This program is free software; you can redistribute it and/or modify
015 *  it under the terms of the GNU General Public License as published by
016 *  the Free Software Foundation; either version 2 of the License, or
017 *  (at your option) any later version.
018 *
019 *  This program is distributed in the hope that it will be useful,
020 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
021 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022 *  GNU General Public License for more details.
023 *
024 *  You should have received a copy of the GNU General Public License
025 *  along with this program; if not, write to the Free Software
026 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
027 */
028package ca.bc.webarts.tools;
029
030import java.io.File;
031
032import ca.bc.webarts.widgets.Util;
033import ca.bc.webarts.tools.NativeAppLauncher;
034
035/**
036 * A tool to downsize/grade all jpg files in a dir and save the results in a
037 * subdir. It does the processing by calling the native ImageMagik 'convert'
038 * command.
039 *
040 * <pre>
041 *    Usage:
042 *      java ca.bc.webarts.widgets.ReduceJPEG [directory]
043 *
044 *      The directory is not required... if NOT spec'd the current dir will be processed.
045 *</pre>
046 *
047 * @author     Tom Gutwin P.Eng
048 **/
049public class ReduceJPEG extends ReduceImage
050{
051
052  /** The file extension to search for. **/
053  protected String imageFileExtension_ = ".jpg";
054  protected String imageSecondaryFileExtension_ = ".jpeg";
055
056  /**
057   * The default empty constructor for this class.  It initsbut does no grabbing.
058   **/
059  public ReduceJPEG()
060  {
061    this(System.getProperty("user.dir") );
062  }
063
064
065  /**
066   * The main constructor for this class.  It inits and grabs the spec'd URL
067   * into the current directory.
068   *
069   * @param urlStr the string representation of the URL to grab.
070   *
071   **/
072  public ReduceJPEG(String dirStr)
073  {
074    File tmpFile = new File(dirStr);
075    if (tmpFile != null && tmpFile.exists() && tmpFile.isDirectory())
076      reduceDir(dirStr,
077                 DEFAULT_REDUCTION_PERCENTAGE,
078                 DEFAULT_REDUCTION_QUALITY, false);
079    else
080      reduceFile(dirStr,
081                  DEFAULT_REDUCTION_PERCENTAGE,
082                  DEFAULT_REDUCTION_QUALITY);
083  }
084
085
086  /**
087   * The main constructor for this class.  It inits and grabs the spec'd URL
088   * into the current directory and the reductionSizePercent.
089   *
090   * @param reductionSizePercent the size to reduce the image by in percent.
091   * @param urlStr the string representation of the URL to grab.
092   *
093   **/
094  public ReduceJPEG(int reductionSizePercent, String dirStr)
095  {
096    File tmpFile = new File(dirStr);
097    if (tmpFile != null && tmpFile.exists() && tmpFile.isDirectory())
098      reduceDir(dirStr,
099                 reductionSizePercent,
100                 DEFAULT_REDUCTION_QUALITY, false);
101    else
102      reduceFile(dirStr,
103                  reductionSizePercent,
104                  DEFAULT_REDUCTION_QUALITY);
105  }
106
107
108  /**
109   * The main constructor for this class.  It inits and grabs the spec'd URL
110   * into the current directory and the reductionSizePercent.
111   *
112   * @param reducedSizeX the X size to reduce the image by .
113   * @param reducedSizeY the X size to reduce the image by .
114   * @param urlStr the string representation of the URL to grab.
115   *
116   **/
117  public ReduceJPEG(int reducedSizeX, int reducedSizeY, String dirStr)
118  {
119    File tmpFile = new File(dirStr);
120    if (tmpFile != null && tmpFile.exists() && tmpFile.isDirectory())
121      reduceDir(dirStr,
122                 reducedSizeX,
123                 reducedSizeY,
124                 DEFAULT_REDUCTION_QUALITY, false);
125    else
126      reduceFile(dirStr,
127                  reducedSizeX,
128                  reducedSizeY,
129                  DEFAULT_REDUCTION_QUALITY);
130  }
131
132
133  public  String getFileExtension() {return this.imageFileExtension_;}
134  public  String getSecondaryFileExtension() {return this.imageSecondaryFileExtension_;}
135
136
137  /**
138   * The main entry for this app. It takes url strings on the commandline.
139   *
140   * @param args is a dir to do the deed on (NOT REQUIRED - default is current dir).
141   **/
142  public static void main(String [] args)
143  {
144    if (args.length == 0)
145    {
146      System.out.println("ca.bc.webarts.tools.ReduceJPEG");
147      System.out.println("--------------------------------------");
148      System.out.println("Syntax:");
149      System.out.println("       java ca.bc.webarts.tools.ReduceJPEG [dir [percentReduction]||[reducedSixeX reducedSixeY] ]");
150      System.out.println("");
151      System.out.println("With no args it process current dir with default reduction Percent....\n");
152      ReduceJPEG instance =
153        new ReduceJPEG(System.getProperty("user.dir"));
154    }
155    else if (args.length == 1) // dir only
156    {
157      System.out.println("Reducing JPEGs In: "+args[0]);
158      ReduceJPEG instance = new ReduceJPEG(args[0]);
159    }
160    else if (args.length == 2) // percent and dir
161    {
162      ReduceJPEG instance =
163        new ReduceJPEG(Integer.parseInt(args[1]),args[0]);
164    }
165    else if (args.length == 3) // X, Y and dir
166    {
167      ReduceJPEG instance =
168        new ReduceJPEG(Integer.parseInt(args[1]),Integer.parseInt(args[2]),args[0]);
169    }
170    else
171    {
172      System.out.println("ca.bc.webarts.tools.ReduceJPEG");
173      System.out.println("--------------------------------------");
174      System.out.println("Syntax:");
175      System.out.println("       java ca.bc.webarts.tools.ReduceJPEG [dir [percentReduction]||[reducedSixeX reducedSixeY] ]");
176      System.out.println("");
177      System.out.println("With no args it process current dir with default reduction Percent....\n");
178    }
179  }
180
181
182  /**
183   * Does the grunt work to reduce/convert a single file.
184   *
185   * @param fileName is the name of the file to convert.
186   * @param reductionSizePercent the size to reduce the image by in percent.
187   * @param reducedQuality is the name quality param for the reduction
188   *                       (jpg or png might be spec'd as 75).
189   * @return the conversion apps result code.
190   **/
191  public int reduceFile(String fileName,
192                         int reductionSizePercent,
193                         int reducedQuality)
194  {
195    int retVal = 0;
196    new File(saveLocation_).mkdir(); // ensures the dir exests
197    String fName = fileName;
198    if (fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR) > -1)
199      fName = fileName.substring(fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR)+1);
200    StringBuffer command = new StringBuffer("/usr/bin/convert");
201    String [] parms = {
202      "-resize",reductionSizePercent+"%",
203      "-quality",""+reducedQuality,
204      fileName,
205      saveLocation_+
206      (!saveLocation_.endsWith(SYSTEM_FILE_SEPERATOR)?SYSTEM_FILE_SEPERATOR:"")+
207      fName};
208
209    retVal = Util.executeNativeApp(command.toString(), parms, null, true);
210    System.out.println(" " + fileName + " reduced. (" + retVal + ")");
211    return retVal;
212  }
213
214  /**
215   * Does the grunt work to reduce/convert a single file.
216   *
217   * @param fileName is the name of the file to convert.
218   * @param reducedSizeX the X size to reduce the image by .
219   * @param reducedSizeY the X size to reduce the image by .
220   * @param reducedQuality is the name quality param for the reduction
221   *                       (jpg or png might be spec'd as 75).
222   * @return the conversion apps result code.
223   **/
224  public int reduceFile(String fileName,
225                         int reducedSizeX,
226                         int reducedSizeY,
227                         int reducedQuality)
228  {
229    int retVal = 0;
230    new File(saveLocation_).mkdir(); // ensures the dir exests
231    String fName = fileName;
232    if (fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR) > -1)
233      fName = fileName.substring(fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR)+1);
234    StringBuffer command = new StringBuffer("/usr/bin/convert");
235    String [] parms = {
236      "-size",
237      reducedSizeX+"x"+reducedSizeY+"!",  // the "!" forced the geometry to the exact size, if not used, then it keeps the ratio
238      fileName,
239      "-quality",
240      ""+reducedQuality,
241      "-resize",
242      reducedSizeX+"x"+reducedSizeY+"!",  // the "!" forced the geometry to the exact size, if not used, then it keeps the ratio
243      saveLocation_+
244      (!saveLocation_.endsWith(SYSTEM_FILE_SEPERATOR)?SYSTEM_FILE_SEPERATOR:"")+
245      fName};
246
247    retVal = Util.executeNativeApp(command.toString(), parms, null, true);
248    System.out.println(" " + fileName + " reduced. (" + retVal + ")");
249    return retVal;
250  }
251
252}
253