001/*
002 *  $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/ReducePNG.java,v $
003 *  ReducePNG.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.ReducePNG [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 ReducePNG extends ReduceImage
050{
051
052  /** The file extension to search for. **/
053  protected String imageFileExtension_ = ".png";
054  protected String imageSecondaryFileExtension_ = ".png";
055
056  /**
057   * The default empty constructor for this class.  It initsbut does no grabbing.
058   **/
059  public ReducePNG()
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 ReducePNG(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   * The main constructor for this class.  It inits and grabs the spec'd URL
087   * into the current directory and the reductionSizePercent.
088   *
089   * @param reductionSizePercent the size to reduce the image by in percent.
090   * @param urlStr the string representation of the URL to grab.
091   *
092   **/
093  public ReducePNG(int reductionSizePercent, String dirStr)
094  {
095    File tmpFile = new File(dirStr);
096    if (tmpFile != null && tmpFile.exists() && tmpFile.isDirectory())
097      reduceDir(dirStr,
098                 reductionSizePercent,
099                 DEFAULT_REDUCTION_QUALITY, false);
100    else
101      reduceFile(dirStr,
102                  reductionSizePercent,
103                  DEFAULT_REDUCTION_QUALITY);
104  }
105
106
107  /**
108   * The main constructor for this class.  It inits and grabs the spec'd URL
109   * into the current directory and the reductionSizePercent.
110   *
111   * @param reducedSizeX the X size to reduce the image by .
112   * @param reducedSizeY the X size to reduce the image by .
113   * @param urlStr the string representation of the URL to grab.
114   *
115   **/
116  public ReducePNG(int reducedSizeX, int reducedSizeY, String dirStr)
117  {
118    File tmpFile = new File(dirStr);
119    if (tmpFile != null && tmpFile.exists() && tmpFile.isDirectory())
120      reduceDir(dirStr,
121                 reducedSizeX,
122                 reducedSizeY,
123                 DEFAULT_REDUCTION_QUALITY, false);
124    else
125      reduceFile(dirStr,
126                  reducedSizeX,
127                  reducedSizeY,
128                  DEFAULT_REDUCTION_QUALITY);
129  }
130
131
132  /**
133   * The main entry for this app. It takes url strings on the commandline.
134   *
135   * @param args is a dir to do the deed on (NOT REQUIRED - default is current dir).
136   **/
137  public static void main(String [] args)
138  {
139    if (args.length == 0)
140    {
141      System.out.println("ca.bc.webarts.tools.ReducePNG");
142      System.out.println("--------------------------------------");
143      System.out.println("Syntax:");
144      System.out.println("       java ca.bc.webarts.tools.ReducePNG [dir [percentReduction]||[reducedSixeX reducedSixeY] ]");
145      System.out.println("");
146      System.out.println("With no args it process current dir with default reduction Percent....\n");
147      ReducePNG instance =
148        new ReducePNG(System.getProperty("user.dir"));
149    }
150    else if (args.length == 1) // dir only
151    {
152      System.out.println("Reducing PNGs In: "+args[0]);
153      ReducePNG instance = new ReducePNG(args[0]);
154    }
155    else if (args.length == 2) // percent and dir
156    {
157      ReducePNG instance =
158        new ReducePNG(Integer.parseInt(args[1]),args[0]);
159    }
160    else if (args.length == 3) // X, Y and dir
161    {
162      ReducePNG instance =
163        new ReducePNG(Integer.parseInt(args[1]),Integer.parseInt(args[2]),args[0]);
164    }
165    else
166    {
167      System.out.println("ca.bc.webarts.tools.ReducePNG");
168      System.out.println("--------------------------------------");
169      System.out.println("Syntax:");
170      System.out.println("       java ca.bc.webarts.tools.ReducePNG [dir [percentReduction]||[reducedSixeX reducedSixeY] ]");
171      System.out.println("");
172      System.out.println("With no args it process current dir with default reduction Percent....\n");
173    }
174  }
175
176
177  /**
178   * Does the grunt work to reduce/convert a single file.
179   *
180   * @param fileName is the name of the file to convert.
181   * @param reductionSizePercent the size to reduce the image by in percent.
182   * @param reducedQuality is the name quality param for the reduction
183   *                       (jpg or png might be spec'd as 75).
184   * @return the conversion apps result code.
185   **/
186  public int reduceFile(String fileName,
187                         int reductionSizePercent,
188                         int reducedQuality)
189  {
190    int retVal = 0;
191    new File(saveLocation_).mkdir(); // ensures the dir exests
192    String fName = fileName;
193    if (fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR) > -1)
194      fName = fileName.substring(fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR)+1);
195    StringBuffer command = new StringBuffer("/usr/bin/convert");
196    String [] parms = {
197      "-resize",reductionSizePercent+"%",
198      "-quality",""+reducedQuality,
199      fileName,
200      saveLocation_+
201      (!saveLocation_.endsWith(SYSTEM_FILE_SEPERATOR)?SYSTEM_FILE_SEPERATOR:"")+
202      fName};
203
204    retVal = Util.executeNativeApp(command.toString(), parms, null, true);
205    System.out.println(" " + fileName + " reduced. (" + retVal + ")");
206    return retVal;
207  }
208
209  /**
210   * Does the grunt work to reduce/convert a single file.
211   *
212   * @param fileName is the name of the file to convert.
213   * @param reducedSizeX the X size to reduce the image by .
214   * @param reducedSizeY the X size to reduce the image by .
215   * @param reducedQuality is the name quality param for the reduction
216   *                       (jpg or png might be spec'd as 75).
217   * @return the conversion apps result code.
218   **/
219  public int reduceFile(String fileName,
220                         int reducedSizeX,
221                         int reducedSizeY,
222                         int reducedQuality)
223  {
224    int retVal = 0;
225    new File(saveLocation_).mkdir(); // ensures the dir exests
226    String fName = fileName;
227    if (fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR) > -1)
228      fName = fileName.substring(fileName.lastIndexOf(SYSTEM_FILE_SEPERATOR)+1);
229    StringBuffer command = new StringBuffer("/usr/bin/convert");
230    String [] parms = {
231      "-size",
232      reducedSizeX+"x"+reducedSizeY+"!",  // the "!" forced the geometry to the exact size, if not used, then it keeps the ratio
233      fileName,
234      "-quality",
235      ""+reducedQuality,
236      "-resize",
237      reducedSizeX+"x"+reducedSizeY+"!",  // the "!" forced the geometry to the exact size, if not used, then it keeps the ratio
238      saveLocation_+
239      (!saveLocation_.endsWith(SYSTEM_FILE_SEPERATOR)?SYSTEM_FILE_SEPERATOR:"")+
240      fName};
241
242    retVal = Util.executeNativeApp(command.toString(), parms, null, true);
243    System.out.println(" " + fileName + " reduced. (" + retVal + ")");
244    return retVal;
245  }
246
247
248  public  String getFileExtension() {return this.imageFileExtension_;}
249  public  String getSecondaryFileExtension() {return this.imageSecondaryFileExtension_;}
250}
251