113
117
int outSubsamp = -1, outQual = 95;
114
118
boolean display = false;
116
if(argv.length > 1) {
117
for(int i = 1; i < argv.length; i++) {
118
if(argv[i].length() < 2) continue;
119
if(argv[i].length() > 2
120
&& argv[i].substring(0, 3).equalsIgnoreCase("-sc")) {
120
if (argv.length > 1) {
121
for (int i = 1; i < argv.length; i++) {
122
if (argv[i].length() < 2)
124
if (argv[i].length() > 2 &&
125
argv[i].substring(0, 3).equalsIgnoreCase("-sc")) {
122
if(i < argv.length - 1) {
127
if (i < argv.length - 1) {
123
128
String[] scaleArg = argv[++i].split("/");
124
if(scaleArg.length == 2) {
129
if (scaleArg.length == 2) {
125
130
TJScalingFactor tempsf =
126
131
new TJScalingFactor(Integer.parseInt(scaleArg[0]),
127
Integer.parseInt(scaleArg[1]));
128
for(int j = 0; j < sf.length; j++) {
129
if(tempsf.equals(sf[j])) {
132
Integer.parseInt(scaleArg[1]));
133
for (int j = 0; j < sf.length; j++) {
134
if (tempsf.equals(sf[j])) {
130
135
scaleFactor = sf[j];
136
if(match != 1) usage();
142
if (match != 1) usage();
138
if(argv[i].equalsIgnoreCase("-h") || argv[i].equalsIgnoreCase("-?"))
144
if (argv[i].equalsIgnoreCase("-h") || argv[i].equalsIgnoreCase("-?"))
140
if(argv[i].length() > 2
141
&& argv[i].substring(0, 3).equalsIgnoreCase("-sa")) {
142
if(i < argv.length - 1) {
146
if (argv[i].length() > 2 &&
147
argv[i].substring(0, 3).equalsIgnoreCase("-sa")) {
148
if (i < argv.length - 1) {
144
if(argv[i].substring(0, 1).equalsIgnoreCase("g"))
150
if (argv[i].substring(0, 1).equalsIgnoreCase("g"))
145
151
outSubsamp = TJ.SAMP_GRAY;
146
else if(argv[i].equals("444")) outSubsamp = TJ.SAMP_444;
147
else if(argv[i].equals("422")) outSubsamp = TJ.SAMP_422;
148
else if(argv[i].equals("420")) outSubsamp = TJ.SAMP_420;
152
else if (argv[i].equals("444"))
153
outSubsamp = TJ.SAMP_444;
154
else if (argv[i].equals("422"))
155
outSubsamp = TJ.SAMP_422;
156
else if (argv[i].equals("420"))
157
outSubsamp = TJ.SAMP_420;
153
if(argv[i].substring(0, 2).equalsIgnoreCase("-q")) {
154
if(i < argv.length - 1) {
163
if (argv[i].substring(0, 2).equalsIgnoreCase("-q")) {
164
if (i < argv.length - 1) {
155
165
int qual = Integer.parseInt(argv[++i]);
156
if(qual >= 1 && qual <= 100) outQual = qual;
166
if (qual >= 1 && qual <= 100)
161
if(argv[i].substring(0, 2).equalsIgnoreCase("-g"))
173
if (argv[i].substring(0, 2).equalsIgnoreCase("-g"))
162
174
xform.options |= TJTransform.OPT_GRAY;
163
if(argv[i].equalsIgnoreCase("-hflip"))
175
if (argv[i].equalsIgnoreCase("-hflip"))
164
176
xform.op = TJTransform.OP_HFLIP;
165
if(argv[i].equalsIgnoreCase("-vflip"))
177
if (argv[i].equalsIgnoreCase("-vflip"))
166
178
xform.op = TJTransform.OP_VFLIP;
167
if(argv[i].equalsIgnoreCase("-transpose"))
179
if (argv[i].equalsIgnoreCase("-transpose"))
168
180
xform.op = TJTransform.OP_TRANSPOSE;
169
if(argv[i].equalsIgnoreCase("-transverse"))
181
if (argv[i].equalsIgnoreCase("-transverse"))
170
182
xform.op = TJTransform.OP_TRANSVERSE;
171
if(argv[i].equalsIgnoreCase("-rot90"))
183
if (argv[i].equalsIgnoreCase("-rot90"))
172
184
xform.op = TJTransform.OP_ROT90;
173
if(argv[i].equalsIgnoreCase("-rot180"))
185
if (argv[i].equalsIgnoreCase("-rot180"))
174
186
xform.op = TJTransform.OP_ROT180;
175
if(argv[i].equalsIgnoreCase("-rot270"))
187
if (argv[i].equalsIgnoreCase("-rot270"))
176
188
xform.op = TJTransform.OP_ROT270;
177
if(argv[i].equalsIgnoreCase("-custom"))
189
if (argv[i].equalsIgnoreCase("-custom"))
178
190
xform.cf = new TJExample();
179
else if(argv[i].length() > 2
180
&& argv[i].substring(0, 2).equalsIgnoreCase("-c")) {
181
if(i >= argv.length - 1) usage();
191
else if (argv[i].length() > 2 &&
192
argv[i].substring(0, 2).equalsIgnoreCase("-c")) {
193
if (i >= argv.length - 1)
182
195
String[] cropArg = argv[++i].split(",");
183
if(cropArg.length != 3) usage();
196
if (cropArg.length != 3)
184
198
String[] dimArg = cropArg[2].split("[xX]");
185
if(dimArg.length != 2) usage();
199
if (dimArg.length != 2)
186
201
int tempx = Integer.parseInt(cropArg[0]);
187
202
int tempy = Integer.parseInt(cropArg[1]);
188
203
int tempw = Integer.parseInt(dimArg[0]);
189
204
int temph = Integer.parseInt(dimArg[1]);
190
if(tempx < 0 || tempy < 0 || tempw < 0 || temph < 0) usage();
191
xform.x = tempx; xform.y = tempy;
192
xform.width = tempw; xform.height = temph;
205
if (tempx < 0 || tempy < 0 || tempw < 0 || temph < 0)
210
xform.height = temph;
193
211
xform.options |= TJTransform.OPT_CROP;
195
if(argv[i].substring(0, 2).equalsIgnoreCase("-d"))
213
if (argv[i].substring(0, 2).equalsIgnoreCase("-d"))
197
if(argv[i].equalsIgnoreCase("-fastupsample")) {
215
if (argv[i].equalsIgnoreCase("-fastupsample")) {
198
216
System.out.println("Using fast upsampling code");
199
217
flags |= TJ.FLAG_FASTUPSAMPLE;
201
if(argv[i].equalsIgnoreCase("-fastdct")) {
219
if (argv[i].equalsIgnoreCase("-fastdct")) {
202
220
System.out.println("Using fastest DCT/IDCT algorithm");
203
221
flags |= TJ.FLAG_FASTDCT;
205
if(argv[i].equalsIgnoreCase("-accuratedct")) {
223
if (argv[i].equalsIgnoreCase("-accuratedct")) {
206
224
System.out.println("Using most accurate DCT/IDCT algorithm");
207
225
flags |= TJ.FLAG_ACCURATEDCT;
211
229
String[] inFileTokens = argv[0].split("\\.");
212
if(inFileTokens.length > 1)
230
if (inFileTokens.length > 1)
213
231
inFormat = inFileTokens[inFileTokens.length - 1];
214
232
String[] outFileTokens;
215
if(display) outFormat = "bmp";
217
236
outFileTokens = argv[1].split("\\.");
218
if(outFileTokens.length > 1)
237
if (outFileTokens.length > 1)
219
238
outFormat = outFileTokens[outFileTokens.length - 1];
222
241
File file = new File(argv[0]);
223
242
int width, height;
225
if(inFormat.equalsIgnoreCase("jpg")) {
244
if (inFormat.equalsIgnoreCase("jpg")) {
226
245
FileInputStream fis = new FileInputStream(file);
227
246
int inputSize = fis.available();
229
248
System.out.println("Input file contains no data");
236
255
TJDecompressor tjd;
237
if(xform.op != TJTransform.OP_NONE || xform.options != 0
238
|| xform.cf != null) {
256
if (xform.op != TJTransform.OP_NONE || xform.options != 0 ||
239
258
TJTransformer tjt = new TJTransformer(inputBuf);
240
TJTransform t[] = new TJTransform[1];
259
TJTransform[] t = new TJTransform[1];
242
261
t[0].options |= TJTransform.OPT_TRIM;
243
262
TJDecompressor[] tjdx = tjt.transform(t, 0);
246
else tjd = new TJDecompressor(inputBuf);
265
tjd = new TJDecompressor(inputBuf);
248
267
width = tjd.getWidth();
249
268
height = tjd.getHeight();
250
269
int inSubsamp = tjd.getSubsamp();
251
System.out.println("Source Image: " + width + " x " + height
252
+ " pixels, " + sampName[inSubsamp] + " subsampling");
253
if(outSubsamp < 0) outSubsamp = inSubsamp;
270
System.out.println("Source Image: " + width + " x " + height +
271
" pixels, " + sampName[inSubsamp] + " subsampling");
273
outSubsamp = inSubsamp;
255
if(outFormat.equalsIgnoreCase("jpg")
256
&& (xform.op != TJTransform.OP_NONE || xform.options != 0)
257
&& scaleFactor.isOne()) {
275
if (outFormat.equalsIgnoreCase("jpg") &&
276
(xform.op != TJTransform.OP_NONE || xform.options != 0) &&
277
scaleFactor.isOne()) {
258
278
file = new File(argv[1]);
259
279
FileOutputStream fos = new FileOutputStream(file);
260
280
fos.write(tjd.getJPEGBuf(), 0, tjd.getJPEGSize());
265
285
width = scaleFactor.getScaled(width);
266
286
height = scaleFactor.getScaled(height);
268
if(!outFormat.equalsIgnoreCase("jpg"))
288
if (!outFormat.equalsIgnoreCase("jpg"))
269
289
img = tjd.decompress(width, height, BufferedImage.TYPE_INT_RGB,
271
else bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags);
292
bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags);
275
295
img = ImageIO.read(file);
297
throw new Exception("Input image type not supported.");
276
298
width = img.getWidth();
277
299
height = img.getHeight();
279
if(img.getType() == BufferedImage.TYPE_BYTE_GRAY)
300
if (outSubsamp < 0) {
301
if (img.getType() == BufferedImage.TYPE_BYTE_GRAY)
280
302
outSubsamp = TJ.SAMP_GRAY;
281
else outSubsamp = TJ.SAMP_444;
304
outSubsamp = TJ.SAMP_444;
286
System.out.print("Dest. Image (" + outFormat + "): " + width + " x "
287
+ height + " pixels");
309
System.out.print("Dest. Image (" + outFormat + "): " + width + " x " +
290
313
ImageIcon icon = new ImageIcon(img);
291
314
JLabel label = new JLabel(icon, JLabel.CENTER);
292
315
JOptionPane.showMessageDialog(null, label, "Output Image",
293
JOptionPane.PLAIN_MESSAGE);
295
else if(outFormat.equalsIgnoreCase("jpg")) {
296
System.out.println(", " + sampName[outSubsamp]
297
+ " subsampling, quality = " + outQual);
316
JOptionPane.PLAIN_MESSAGE);
317
} else if (outFormat.equalsIgnoreCase("jpg")) {
318
System.out.println(", " + sampName[outSubsamp] +
319
" subsampling, quality = " + outQual);
298
320
TJCompressor tjc = new TJCompressor();
302
324
tjc.setSubsamp(outSubsamp);
303
325
tjc.setJPEGQuality(outQual);
305
327
jpegBuf = tjc.compress(img, flags);
307
329
tjc.setSourceImage(bmpBuf, width, 0, height, TJ.PF_BGRX);