Skip to content

Instantly share code, notes, and snippets.

@leffsprojects
Created November 24, 2025 00:21
Show Gist options
  • Select an option

  • Save leffsprojects/67016efc9a1e94c11329215e4defa9dc to your computer and use it in GitHub Desktop.

Select an option

Save leffsprojects/67016efc9a1e94c11329215e4defa9dc to your computer and use it in GitHub Desktop.
Documentation for the program to move a picture into another picture
A172
Sometimes one needs to move a picture into a "frame" so when it is printed out that picture is a
certain physical size. The use case for which I implemented this program was preparing my
photo to send with my passport renewal.
I have a figure, 3C.jpg. I know that my head fits in a specific rectangle of the source documentation. That is, I know the x, y position as well as its height and width. That has to appear with a
height of 1 3/8 inches when printed on a photo. I use the photo printing service at Walmart. For
14 cents, one gets a picture that is six inches high and four inches wide. Thus, in simple words,
one specifies an input photo and the position , height and width in pixes where one’s head (or
other thing that one needs a photo of). One specifies the percentage in the output picture that that
rectangle should be in the output (as either height or width). In the use case discussed, one specifies a percentage of 6 over 1 3/8 for the height.
Thus assume:
• the original picture was 400 wide by 300 height‘
• the area that one wanted . For the use case I mentioned, I determined that the region containing the head would be 150 width by 200 high. Thus, I would put those two numbers in
for width and height below.
• as mentioned above, the ouput would be printed as six inches high and the area X should
appear 1 3/8 high on that output
Then, one would specify that the percentage for the height of the output file would be:
1. Input argument documentation
So to document A172, A172 tells what is missing from the command line argument.
1.1. Name of input and output file
• Name of input file, e. g. 3CA.jpg
• Name of output file, e. g. 3CA.png
1.2. for result file
This is rectangle A
• width
• height
default is the height that would make the height of 1 3/8 high in a six-inch result file.
1.3. Rectangle B
As you know, the goal of A172 is to copy stuff from the source to the result picture file, while
making a specified rectangle in the source picture takes a specific amount of space in the target.
-2-
Rectangle B is that rectangle.
• x
• y
• width
• height
The default is to take these measurements from Rectangle C. That is we use that which we are
copying to be the specific size.
1.4. Rectangle C
Move this entire rectangle to the output rectangle
• x
• y
• width
• height
the default is to take these measurements from Rectangle B which is described above. Obviously, both must not be missng. Also, if any information is entered for either, then all four numbers should be created.
1.5. Rectangle D
The last is the position where to move the rectangle to the result. We need the x and y values
only. The width and height in the result are the same as that which is to be moved. In this version, we don’t attempt to sample or expand the picture.
x
y
The default is for both of these to be zero.
import Debug.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.Graphics2D;
import java.awt.Color;
import javax.imageio.spi.*;
public class NewA172 {
/**
* As we go through the command-line arguments, if we find a problem, we set this to true;
* that way, we can continue checking the command-line input but know not to attempt
* processing.
*/
static boolean FatalError;
/**
* position of input file name in argument list
*/
static final int IFN=0;
/**
* Input File Name
*/
static String ifn;
/**
* Position of period in {@link ifn}
*/
static int PosIFN;
/**
* Main part of the Input File Name
*/
static String IMN;
/**
* {@link File} corresponding to the Input File
*/
static File InputFile;
/**
* Can Read the {@link InputFile}
*/
static boolean IFCR;
/**
* The extension (that is, what is after the period) of the {@link IFN}
*/
static String InputExtension;
/**
* position of output file name in argument list
*/
static final int OFN = IFN +1;
/**
* Output File Name
*/
static String ofn;
/**
* position of period in the output file name ({@link ofn})
*/
static int PosOFN;
/**
* Extension of Output File Name
*/
static String OutputExtension;
/**
* Output File
*/
static File OutputFile;
/**
* {@link OutputFile} Exists
*/
static boolean OFE;
/**
* Parent of {@link OutputFile}
*/
static File OFP;
/**
* Parent of {@link OutputFile}, as a string
*/
static String ofp;
/**
* {@link OutputFile} can write out to;
* if it exists, then we check {@code canWrite}, otherwise, we check that we
* {@code canWrite} the {@code ParentDirectory} of the {@link OutputFile}
*/
static boolean OFCR;
/**
* Output File Canonical File
*/
static File OFCF;
/**
Current Directory
*/
static File CD;
/**
* position of the item with this extension in the {@link IWSPI}, {@link ISPID} {@link IE} array/tables
*/
static int EP;
/**
* put of Output Name before the period, that is, without extension
*/
static String OMN;
/**
* position of the item of this extension in the {@link IWSPI} <BR>(output extension)
*/
static int OP;
/**
* width of output result (Rectangle <B>A</B>)
*/
static final int RW = OFN+1;
/**
* Output Result Width
*/
static int rw;
/**
* height of output result (Rectangle <B>A</B>)
*/
static final int RH = RW+1;
/**
* Output Result Height
*/
static int rh;
/**
* position of the rencangle that takes a specific amount of space in
* the target
* <BR>Rectangle <B>B</B>
*/
static final int RBx = RH+1;
/**
* Rectangle that takes a specific amount of space x position
* <BR> Rectangle B
*/
static int rbx;
/**
* position in argument list of the rectangle that takes a specific amount of space in the target, <I>x</I>
* <BR>Rectangle <B>B</B>
*/
static final int RBy = RBx+1;
/**
* Rectangle that takes a specific amount of space <I>y</I>- position
* <BR> Rectangle B
*/
static int rby;
/**
* Width of rectangle that will be a certain amount of space in the target
* <BR>Rectangle <B>B</B>
*/
static final int RBw = RBy + 1;
/**
* width of the rectangle that takes a specific amount of space in the target
* <BR>Rectangle <B>B</B>
*/
static int rbw;;
/**
* position in argument list of
* <BR>Height of rectangle that will be a certain amount of space in the target
* <BR>Rectangle <B>B</B>
*/
static final int RBh = RBw + 1;
/**
*
* <BR>Height of rectangle that will be a certain amount of space in the target
* <BR>Rectangle <B>B</B>
*/
static int rbh;
/**
* position in argument list of <BR>
* x position of Rectangle to copy from Source
* <BR>Rectangle <B>C</B>
*/
static final int RCx = RBh + 1;
/**
* x position of Rectangle to copy from Source
* <BR>Rectangle <B>C</B>
*/
static int rcx;
/**
* position in argument list of <BR>
* y position of Rectangle to copy from Source
* <BR> Rectangle <B>C</B>
*/
static final int RCy = RCx + 1;
/**
* y position of Rectanlge to copy from Source
* <BR>Rectangle <B>C</B>
*/
static int rcy;
/**
* position in argument list of <BR>
* width of Rectangle to copy from Source
* <BR> Rectangle <B>C</B>
*/
static final int RCw = RCy + 1;
/**
* width of Rectangle to copy from Source
* <BR> Rectangle <B>C</B>
*/
static int rcw;
/**
* position in argument list of <BR>
* height of Rectangle to copy from Source
* <BR> Rectangle <B>C</B>
*/
static final int RCh = RCw + 1;
/**
* x position where to move the picture into the result
*/
/**
* height of Rectangle to copy from Source
* <BR> Rectangle <B>C</B>
*/
static int rch;
/**
* position in argument list of <BR>
* Result Rectangle <BR><I>x</I>- position where to move the picture into the result
*/
static final int RRx = RCh + 1;
/**
* Result Rectangle <BR><I>x</I>-position where to move the picture into the result
*/
static int rrx;
/**
* position in argument list of <BR>
* Result Rectangle <BR><I>y</I>- position where to move the picture into the result
*/
static final int RRy = RRx + 1;
/**
* Result Rectangle <I>y</I>- <BR>position where to move the picture into the result
*/
static int rry;
/**
* Number of Arguments
*/
static final int NOA = RRy + 1;
/**
* on if this is the United States Passport special case
*/
static boolean Passport;
/**
* this is the count of arguments actually passed on the command line
*/
static int argc;
/**
* what this variable usually is
*/
static int i;
/**
* what this variable usually is
*/
static int j;
/**
* List of SPI that can write images.
* see Stack Overflow question 59089118
*/
static ImageWriterSpi[] IWSPI;
/**
* see Stack Overflow question 59089118
*/
static IIORegistry IR;
/**
* Description of each ISPI,
* see Stack Overflow question 59089118
* element I of ISPID matches element I of {@link IWSPI}.
*/
static String[] ISPID;
/**
* Iterator for Service Providers/ Stack Overflow question 59089118
*/
static Iterator<ImageWriterSpi> II;
/**
* possible extensions for each Image SPI;
*/
static String [][] IE;
/**
* Number of entries in
*{@link IWSPI}, {@link ISPID} {@link IE} array/tables
*/
static int NOIA;
/**
* Element of I bit array is on if element I is valid and not a default
* value. <P>That is, if <I>i</I>th argument is "{@code -}"
* {@code BIT [I]} will be false
* Also, if there are <I>n</I> arguments,
* <BR>
* where <I>n</I> &lt; {@link NOA} elements of {@code BIT[I]} where <I>I</I> &gt; {@code argc} will also be set to false. (That is, if the user entered ten arguments, all positions from 11 on will be marked {@code false}.)
*/
static boolean [] BIT;
/**
* If the argument ( # <I>I</I> ) is a percentage, then this is on
*<BR>Only valid for <I>I</I> between {@link RW} inclusive
*/
static boolean [] PERCENTAGE;
/**
* If the argument ( # <I> I </I> ) is a percentage, thhis is its value
* <BR>Only valid if {@link PERCENTAGE} is on
*/
static float [] Percentage;
/**
* If argument ( # <I> I </I> ) is not default but not a percent, then this is its value
* <BR> only valid if (@link BIT) is on and {@link PERCENTAGE} is off.
*/
static int []Value;
/**
* in order to find out if argument <I>i</I> is a float number, this is the position of
* the period (or decimal point) in argument <I>i</I>. Used to decide if {@link PERCENTAGE} position <I>i</I>is to be turned on.
*/
static int PP;
/**
* copy everything associated with arguments in the range
* from K to L to the arguments from I to J
*<P>That is {@link Value}, {@link PERCENTAGE}, {@link Percentage} and {@link BIT}
* @param I start of range to receive stuff
* @param J end of range to receive stuff
* @param K Start of range from which to copy stuff
* @param L End of range from which to copy stuff
*/
static void COPYVALUES (int I, int J, int K, int L) {
int i,j;
for (i=I,j=K;i<=J&&j<=L;i++,j++){
//Debug.P (" copying value from "+j+ " to " + i + " ",false);
BIT[i]=BIT[j];
Value[i]=Value[j];
PERCENTAGE[i] = PERCENTAGE[j];
Percentage[i] = Percentage[j];
}
//Debug.P("");
}
/**
* All the command lines arguments whose positions are in in the indicated rangge were given values, nnot defaulted
* @param I start of range
* @param J end of range
* @returns true if all {@link BIT}[<I>i</I>{@code(]} are true
*/
static boolean ALLON(int I, int J) {
boolean ReturnThis;
ReturnThis = true;
for (i=I;i<J;i++) {
if (!BIT[i]){ReturnThis=false;}
}
return ReturnThis;
}
/**
* All the command lines arguments whose positions are in in the indicated rangge were given values, are defaulted
* @param I start of range
* @param J end of range
* @returns true if all {@link BIT}[<I>i</I>{@code(]} are false
*/
static boolean ALLDEFAULT (int I,int J) {
boolean ReturnThis;
ReturnThis = true;
for (i=I;i<J;i++){
if (BIT[i]){ReturnThis = false;}
}
return ReturnThis;
}
/**
* if the given position in the argument list is a value, return the value
* otherwise, get the percentage. Give error if {@link BIT} not on.
* @param pos , position
* @param posP , use this value for percentage
*/
static int V ( int pos, int posP) {
int ReturnThis;
if (!BIT[pos]) {
FatalError= true;
Debug.P ("V gives error as position "+pos+ " not valid , Fatal Error set to true");
ReturnThis = -1;
}
else {
if (!PERCENTAGE[pos]){
ReturnThis = Value[pos];
}
else {
ReturnThis = (int)(Percentage[pos] * posP);
}
}
return ReturnThis;
}
/**
* Rectangle <B>B</B> valid and Rectangle <B>C</B> valid
*<P>
* command line arguments for Rectangle <B>B </B>are all entered or
* command line arguments for Rectnagle <B>C </B>are all entered or
*<BR>both
*/
static boolean bcw;
/**
* The Image to be converted
*/
static BufferedImage BI;
/**
* The type of the input image ({@link BI));
*/
static int ImageType;
/**
* Input File Raster ( corresponds to {@link BI})
*/
static WritableRaster wr;
/**
* Output Raster
*/
static BufferedImage OR;
/**
* Input Image's Height ( from {@link wr});
*/
static int IH;
/**
* Input Image's Height ( from {@link wr});
*/
static int IW;
/**
* find position of this extension in the {@link IWSPI}, {@link ISPID} {@link IE} array/tables
* @param E which extension to search
*/
/**
* Place to put the Rectangle <B>B</B> being moved.
*/
static WritableRaster pr;
/**
* for output image which will be written
*/
static WritableRaster NR;
static int SearchExtension (String E) {
boolean DF;
DF=true;
int i,j;
if (DF) {Debug.P (" Search Extension |"+E+"| ");}
boolean Found;
Found = false;
for (i=0;i<NOIA && !Found;i++){
for (j=0;j<IE[i].length&& !Found;j++) {
if (IE[i][j].toLowerCase().equals(E)){
Found=true;
}
}
}
if (DF) {if (Found){Debug.P (" found position "+i);}else {Debug.P (" not found ");}}
if (Found){ return i;}
return -1;
}
public static void main (String [] arg) throws IOException {
Debug.P ("NOA is " + NOA);
argc = arg.length;
BIT = new boolean[NOA];
for (i=0;i<NOA;i++){
BIT[i] = true;
}
for (i=argc;i<NOA;i++) {
BIT[i] = false;
}
for (i=0;i<argc;i++){
if ("-".equals(arg[i])){
BIT[i] = false;
}
}
PERCENTAGE = new boolean [NOA];
for (i=RW;i<argc;i++){
PERCENTAGE[i] = false;
}
for (i=0;i<arg.length;i++){
Debug.P ("Argument I |"+arg[i]+"| BIT["+i+"] = "+BIT[i] + " Percentage["+i+"] = "+PERCENTAGE[i]);
}
// in case I got the final int for the positions in the argument list wrong,
// I output those positions now
Debug.P (" position of input file in argument list " + IFN +
" position of output file in argument list " + OFN +
" Rectangle A width "+RW+ " Rectnagle A height " + RH );
Debug.P (" Rectangle B ("+ RBx + "," + RBy+") "+RBw + "x"+RBh );
Debug.P (" Rectangle C ("+RCx+ ","+RCy+") "+RCw +"x"+RCh);
Debug.P (" RR x "+RRx + " RRy "+RRy);
// Get List of possible output file types
i=0;
IR = IIORegistry.getDefaultInstance();
II = IR.getServiceProviders(ImageWriterSpi.class,false);
ImageWriterSpi ISPI;
IE = new String [20][];
IWSPI = new ImageWriterSpi[20];
ISPID = new String[20];
while ( II.hasNext()){
ISPI = II.next();
IWSPI[i] = ISPI;
ISPID[i]=ISPI.getDescription(Locale.ENGLISH);
IE[i]=ISPI.getFormatNames();
i++;
}
NOIA = i;
Debug.P (" list IO Service Providers " + NOIA);
for (i=0;i<NOIA;i++) {
Debug.P (" i a= "+i+": ",false);Debug.P(IWSPI[i]+ " Description |"+ISPID[i]+"| ",false);Debug.P( " "+IE[i].length,false);
for (j=0;j<IE[i].length;j++){
Debug.P (" |"+IE[i][j]+"| ",false);
}
}
OutputFile = null;
InputFile = null;
// Process Input File Name
if (BIT[IFN] &&!BIT[OFN]){
Debug.P ("Here Input File but no OFN ",false);
ifn = arg[IFN];
InputFile = new File(ifn);
PosIFN = ifn.indexOf('.');
Debug.P (" ofn |"+ifn+"| PosIFN "+PosIFN);
if (PosIFN == -1){
System.err.println (" Input File Name is missing Extension" +
" and there is no Output File Name ");
FatalError = true;
Debug.P (" Fatal Error missing extension on input file name ");
}
else {
InputExtension = ifn.substring(PosIFN+1);
IMN = ifn.substring(0,PosIFN);
Debug.P (" found Input Extension |"+InputExtension + "| IMN |"+IMN+"| ",false);
EP=SearchExtension(InputExtension);
if (EP == -1) {
System.err.println(" Input File has invalid extension ");
FatalError = true;
Debug.P (" fatal error invalid extension here one |"+InputExtension+"| ");
}
if (EP==0 && NOIA>1) {
Debug.P (" hre EP0",false);
OutputExtension = IE[1][0];
Debug.P (" chose output file extension |"+OutputExtension+"| ",false);
ofn = IMN+"."+OutputExtension;
OutputFile = new File (ofn);
}
else
if (EP>0) {
Debug.P ("here EP > 0 ");
OutputExtension = IE[0][0];
Debug.P(" chose output file extension |"+OutputExtension+"| ",false);
ofn = IMN+"."+OutputExtension;
OutputFile = new File(ofn);
}
else {
System.err.println (" Input File or Output File extension woes ");
FatalError =true;
Debug.P (" fatal error invalid extension here two ");
}
}
}
else
if (!BIT[IFN] && !BIT[OFN]){
Debug.P (" missing input file name and missing output file name");
System.err.println (" missing input file name and missing output file name");
FatalError = true;
} // Input File Name valid but no Output File Name
else
if (BIT[OFN] && BIT[IFN]){
ifn = arg[IFN];
ofn = arg[OFN];
Debug.P(" else OFN nabled |"+ofn+"| ",false);
PosOFN=ofn.indexOf('.');
Debug.P ("PosOFN = "+PosOFN+ " ",false);
if (PosOFN != -1){
Debug.P("ofn |"+ofn+"| PosOFN "+PosOFN+" ",false);
OMN = ofn.substring(0,PosOFN );
OutputExtension= ofn.substring(PosOFN+1);
Debug.P (" OMN |"+OMN+"| Output Extension |"+OutputExtension+"| ",false);
OP = SearchExtension(OutputExtension);
}
Debug.P (" OP "+OP+ " ");
if (OP==-1) {
System.err.println (" Output file name extension invalid ");
FatalError=true;
Debug.P( " fatal error invalid extension on output file ");
}
OutputFile = new File (ofn);
InputFile = new File (ifn);
}
// Check that we can read the input file ");
if (InputFile !=null && BIT[IFN]){
Debug.P (" There is an input file ",false);
IFCR = InputFile.canRead();
if (!IFCR) {
System.err.println (" cannot read Input File |"+ifn+"|");
Debug.P (" cannot read Input File |"+ifn+"|");
FatalError = true;
}
}
if (OutputFile !=null){
OFE = OutputFile.exists();
if (OFE){
Debug.P( " OFCR is |"+OutputFile+"|",false);
OFCR=OutputFile.canWrite();
Debug.P ("Ofcr in OFE "+OFCR,false);
}
else {
Debug.P ("if Output File does not exist |"+OutputFile+"| ",false);
OFP = OutputFile.getParentFile();
Debug.P (" Output File Get parent |"+OFP+"| ",false);
if ( OFP !=null) {
OFCR= OFP.canWrite();
Debug.P (" OFCR == not OFE "+OFCR,false);
}
else {
OFCF = OutputFile.getCanonicalFile();
Debug.P (" Second Output File "+OutputFile + " OFCF |"+OFCF+"| ",false);
ofp = OFCF.getParent();
if (ofp !=null){
OFP = new File(ofp);
}
else {
OFP = null;
}
Debug.P(" 2nd OFP |"+OFP+"| ");
if (OFP !=null){
OFCR = OFP.canWrite();
Debug.P("OFCF ca write; OFCR = "+OFCR,false);
}
else {
CD = new File(".");
Debug.P(" the current directory is |"+CD+"| ",false);
OFCR= (CD.canWrite());
Debug.P("can write "+OFCR);
}
}
}
if (!OFCR){
System.err.println (" cannot write to the designated output file |"+ofn+"| ");
FatalError = true;
Debug.P (" fata eroor cannot write the file");
}
}
// everything after the first two arguments should be a number. If it has a
// decimal, then it is a percentage
Debug.P (" about to process default values NOA is :"+NOA);
Value = new int[NOA];
Percentage = new float[NOA];
for (i=RW;i<NOA;i++){
if (BIT[i]){
Debug.P (" parsing integer argument # i "+i+" |"+arg[i]+"| ",false);
PP = arg[i].indexOf('.');
Debug.P (" PP is "+PP,false);
if (PP<0){
try {
Value[i]=Integer.parseInt(arg[i]);
Debug.P ( "Value ["+i+"] = "+Value[i]);
PERCENTAGE[i]=false;
}
catch (NumberFormatException NPEv) {
BIT[i]=false;
System.err.println ("argument # "+i+" |"+arg[i]+"| did not parse as valid number ");
FatalError = true;
Debug.P (" parse error # "+i);
} // try catch to convert the integer value
} // this is an integer value (number of ixes)
else {
try {
Percentage[i]=Float.parseFloat(arg[i]);
PERCENTAGE[i]=true;
}
catch (NumberFormatException NPEfa) {
BIT[i] = false;
Debug.P (" percentage calculation failed on position # "+i+" |"+arg[i]+"| ");
System.err.println ("argument # "+i+ " appears to be a percentage but not parsing ");
FatalError = true;
} // try catch to convert the percentage
} // this is a percentge
} // this argument is not a default value (missing)`
} // for loop to go through all arguments
Debug.P (" abouit to read input File |"+InputFile+"| ",false);
BI = ImageIO.read(InputFile);
Debug.P (" BI read in "+BI,false);
if (BI == null) {
System.err.println("Error: Could not read input image.|"+InputFile+"| |"+ifn+"|");
Debug.P (" cannot open input image, returning |"+InputFile+"|"+ifn+"|");
return;
}
ImageType = BI.getType();
Debug.P (" Image Type "+ImageType,false);
wr = BI.getRaster();
Debug.P (" writeable raster from BI.get Raster "+wr);
IH = wr.getHeight();
IW = wr.getWidth();
Debug.P (" IH "+ IH + " IW " + IW);
// do Rectangle B and REctangle C
bcw=false;
Debug.P (" bcw logic Fatal Error now is: "+FatalError+ " ",false);
Debug.P ("BCW rbx: "+rbx+ " rby "+rby+ " rbw " + rbw + " rbh " + rbh + " rcx " + rcx + " rcy " + rcy + " rcw "+rcw + " rch " + rch + " IW " + IW + " IH "+IH);
if (ALLON(RBx,RBh) && ALLDEFAULT(RCx,RCh)){
bcw=true;
Debug.P ("B ON, C ALL DEFUALT ",false);
}
if (ALLON(RCx,RCh) && ALLDEFAULT(RBx,RBh)){
bcw = true;
Debug.P ("C On, B ALL DEFAULT ",false);
}
if (ALLON(RCx,RCh) && ALLON(RBx,RBh)){
bcw = true;
Debug.P (" all are on ",false);
}
if (!bcw) {
System.err.println (" either all four measurements for Rectangle B are defined or all four measurements for Rectangle C are defined or both ");
FatalError = true;
Debug.P (" bcw was NOT set! Fatal Error: "+FatalError,false);
}
if (ALLON(RBx,RBh) && ALLDEFAULT(RCx,RCh)){
COPYVALUES(RCx,RCh,RBx,RBh);
Debug.P (" copying values from RB to RC # "+RCx+" "+RCh+ " "+RBx+" "+RBh+" ");
}
if (ALLON(RCx,RCh) && ALLDEFAULT(RBx,RBh)){
COPYVALUES(RBx,RBh,RCx,RCh);
Debug.P (" copying values from RC to RB ($ "+RBx + " "+ RBh + " "+RCx + " "+ RCh+" ");
}
Debug.P (" bcw is: "+bcw);
rbx=V(RBx,IW);
rby=V(RBy,IH);
rbw=V(RBw,IW);
rbh=V(RBh,IH);
rcx=V(RCx,IW);
rcy=V(RCy,IH);
rcw=V(RCw,IW);
rch=V(RCh,IH);
Debug.P ("after running V commands with ALLON: rbx: "+rbx+ " rby "+rby+ " rbw " + rbw + " rbh " + rbh + " rcx " + rcx + " rcy " + rcy + " rcw "+rcw + " rch " + rch + " IW " + IW + " IH "+IH);
Debug.P ("after copy values: rbx: "+rbx+ " rby "+rby+ " rbw " + rbw + " rbh " + rbh + " rcx " + rcx + " rcy " + rcy + " rcw "+rcw + " rch " + rch + " IW " + IW + " IH "+IH);
Passport = false;
// create output buffer;
// see if we have height and width for it.
Debug.P (" Fatal error before check of Bit RW and Bit RH " +FatalError);
if (BIT[RW] && BIT[RH] ){
Debug.P ("RW and RH ",false);
if (PERCENTAGE[RW] && PERCENTAGE[RH]){
if (Percentage[RW]<1.0) {
System.err.println (" output width percentage is less than one ");
Debug.P (" PER RW < 0 , setting fatal error",false);
FatalError = true;
}
rw = (int)(Percentage[RW]*IW);
rh = (int)(Percentage[RH]*IH);
Debug.P (" new rw "+rw + "new rh "+rh,false);
} // RW and RH are both percentages
else
if (PERCENTAGE[RH]){
Debug.P (" perccentgae rh only ",false);
rw=(int)(Percentage[RH]*Value[RH]);
rh = Value[RH];
Debug.P ( " rw is " + rw + " rh is "+ rh ,false);
} // only rH is a percentage
else
if (PERCENTAGE[RH] && !PERCENTAGE[RW]){
Debug.P (" RH specifW];a RH is a percentage ",false);
rw = Value [RW];
rh = (int)(rw*Percentage[RH]);
Debug.P (" RW is " + RW + " RH is " + rh);
}
else { /// both rh and rw are values
rw = Value[RW];
rh = Value[RH];
Debug.P (" ordinary values for rh "+rh + " and rw " + rw,false);
}
Debug.P ( " Fatal Error X "+FatalError);
} // user defined both width and height for output image
else
if (BIT[RW] && ! BIT[RH]) {
System.err.println (" it does not make siense to only specify one of the output images width and height ");
FatalError = true;
Debug.P (" RW on and RH off ");
} // user defined width of output but not height
else
if (BIT[RH] && ! BIT[RW]){ // user defined height of output but not width
System.err.println (" it does not make sense to specify only one of output image height and width ");
FatalError=true;
Debug.P(" RH on bt RW is off ");
}
else
if (!BIT[RW] && !BIT [RH]) {
Debug.P (" United Stataes passport case RH and RW both unspecified "+rbh + " " ,false);
Passport=true;
rh = (int) (( 6.0/1.175)* rbh);
Debug.P (" rh set to "+ rh,false);
rw = (int)(4.0*rh/6.0);
Debug.P(" rw is "+ rw);
rch = (int)((2.0/1.175) *rbh);
rcw = rch;
Debug.P(" new rch is " + rch + " new rcw is "+ rcw + " IH " + IH + " IW " +IW);
} // end of if/elses for width and height of output image
// now do RX and rY
Debug.P (" after calculationg rh set to "+rh + " rw " + rw );
if (BIT[RRx]) {
if (PERCENTAGE[RRx]) {
rrx = (int)(Percentage[RRx]*IW);
Debug.P("rrx p ",false);
}
else {
rrx = Value[RRx];
Debug.P (" rrx v ",false);
}
}
else {
Debug.P("rxd ",false);
rrx=0;
}
if (BIT[RRy]) {
if (PERCENTAGE[RRy]){
rry=(int)(Percentage[RRy]*IH);
Debug.P ( " ry p ",false);
}
else {
rry=Value[RRy];
Debug.P( "ry v ",false);
}
}
else {
Debug.P (" ry d ",false);
rry = 0;
}
Debug.P (" rx set at "+ rrx + " ry " + rry);
for (i=0;i<NOA;i++) {
if (BIT[i]) {
if (!PERCENTAGE[i]){
Debug.P ("Value ["+i+"] = "+Value[i]);
}
else {
Debug.P ("Percentage["+i+"] = "+Percentage[i]);
}
}
}
if (FatalError) {
Debug.P (" returning due to fatal error ");
System.out.println ("exitng due to fatal error ");
System.exit(-1);
}
Debug.P ("setting up pr: rbx: "+rbx+ " rby "+rby+ " rbw " + rbw + " rbh " + rbh + " rcx " + rcx + " rcy " + rcy + " rcw "+rcw + " rch " + rch + " IW " + IW + " IH "+IH + " Passport " + Passport);
if (rcx+rcw>IW) {rcw=IW-rcx-1;System.out.println ("rectangle C width outside size of input picture; truncating to: "+rcw);Debug.P ("rectangle C width outside sie of input picture. \nrcw now is "+rcw+" rcx is "+rcx);}
if (rcy+rch>IH) {rch = IH-rcy-1; System.out.println (" rectangle c height outside size of input picture; truncating to "+rch); Debug.P (" rectangle c height outside size of input picture. \nrch now is: "+rch + " IH "+IH + " rcy "+rcy);}
pr = wr.createWritableChild(rcx,rcy,rcw,rch,0,0,null);
Debug.P (" create piece of input rw:"+rw + " rh: "+rh + " PR " + pr);
OR = new BufferedImage (rw,rh,ImageType);
if (Passport) {
Graphics2D G;
G = OR.createGraphics();
G.setPaint(new Color (255,255,255));
G.fillRect (0,0,rw,rh);
}
Debug.P(" got OR "+OR);
NR =OR.getRaster();
Debug.P(" got raster for new buffered image "+ NR+ " rrx "+rrx + " rry ");
NR.setRect(rrx,rry,pr);
ImageIO.write(OR,IE[OP][0],OutputFile);
Debug.P ("wrote output file! ");
System.out.println("Processing complete. Output saved to " + ofn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment