~ubuntu-branches/ubuntu/saucy/libjpeg-turbo/saucy-security

« back to all changes in this revision

Viewing changes to java/TJExample.java

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2013-07-28 16:52:51 UTC
  • mfrom: (1.1.3) (9.1.1 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130728165251-7vg6wszhm941kdej
Tags: 1.3.0-0ubuntu1
* New upstream release.
  - drop debian/patches/branch-updates.diff
  - refresh tjunittest.patch (now renamed to install-tjunittest.patch)
* Update debian/control:
  - add myself to Uploaders.
* Update debian/copyright:
  - add RSA Data Security copyright (md5).
* Update debian/libturbojpeg.install:
  - install libturbojpeg.so.0* (needed by tjunittest and tjbench).

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
    System.out.println("Options:\n");
52
52
    System.out.println("-scale M/N = if the input image is a JPEG file, scale the width/height of the");
53
53
    System.out.print("             output image by a factor of M/N (M/N = ");
54
 
    for(int i = 0; i < sf.length; i++) {
 
54
    for (int i = 0; i < sf.length; i++) {
55
55
      System.out.print(sf[i].getNum() + "/" + sf[i].getDenom());
56
 
      if(sf.length == 2 && i != sf.length - 1) System.out.print(" or ");
57
 
      else if(sf.length > 2) {
58
 
        if(i != sf.length - 1) System.out.print(", ");
59
 
        if(i == sf.length - 2) System.out.print("or ");
 
56
      if (sf.length == 2 && i != sf.length - 1)
 
57
        System.out.print(" or ");
 
58
      else if (sf.length > 2) {
 
59
        if (i != sf.length - 1)
 
60
          System.out.print(", ");
 
61
        if (i == sf.length - 2)
 
62
          System.out.print("or ");
60
63
      }
61
64
    }
62
65
    System.out.println(")\n");
90
93
    System.exit(1);
91
94
  }
92
95
 
93
 
  private final static String sampName[] = {
 
96
  private static final String[] sampName = {
94
97
    "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0"
95
98
  };
96
99
 
97
 
  public static void main(String argv[]) {
 
100
  public static void main(String[] argv) {
98
101
 
99
 
    BufferedImage img = null;  byte[] bmpBuf = null;
 
102
    BufferedImage img = null;
 
103
    byte[] bmpBuf = null;
100
104
    TJTransform xform = new TJTransform();
101
105
    int flags = 0;
102
106
 
104
108
 
105
109
      sf = TJ.getScalingFactors();
106
110
 
107
 
      if(argv.length < 2) {
 
111
      if (argv.length < 2) {
108
112
        usage();
109
113
      }
110
114
 
113
117
      int outSubsamp = -1, outQual = 95;
114
118
      boolean display = false;
115
119
 
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)
 
123
            continue;
 
124
          if (argv[i].length() > 2 &&
 
125
              argv[i].substring(0, 3).equalsIgnoreCase("-sc")) {
121
126
            int match = 0;
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];
131
 
                    match = 1;  break;
 
136
                    match = 1;
 
137
                    break;
132
138
                  }
133
139
                }
134
140
              }
135
141
            }
136
 
            if(match != 1) usage();
 
142
            if (match != 1) usage();
137
143
          }
138
 
          if(argv[i].equalsIgnoreCase("-h") || argv[i].equalsIgnoreCase("-?"))
 
144
          if (argv[i].equalsIgnoreCase("-h") || argv[i].equalsIgnoreCase("-?"))
139
145
            usage();
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) {
143
149
              i++;
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;
149
 
              else usage();
150
 
            }
151
 
            else usage();
 
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;
 
158
              else
 
159
                usage();
 
160
            } else
 
161
              usage();
152
162
          }
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;
157
 
              else usage();
158
 
            }
159
 
            else usage();
 
166
              if (qual >= 1 && qual <= 100)
 
167
                outQual = qual;
 
168
              else
 
169
                usage();
 
170
            } else
 
171
              usage();
160
172
          }
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)
 
194
              usage();
182
195
            String[] cropArg = argv[++i].split(",");
183
 
            if(cropArg.length != 3) usage();
 
196
            if (cropArg.length != 3)
 
197
              usage();
184
198
            String[] dimArg = cropArg[2].split("[xX]");
185
 
            if(dimArg.length != 2) usage();
 
199
            if (dimArg.length != 2)
 
200
              usage();
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)
 
206
              usage();
 
207
            xform.x = tempx;
 
208
            xform.y = tempy;
 
209
            xform.width = tempw;
 
210
            xform.height = temph;
193
211
            xform.options |= TJTransform.OPT_CROP;
194
212
          }
195
 
          if(argv[i].substring(0, 2).equalsIgnoreCase("-d"))
 
213
          if (argv[i].substring(0, 2).equalsIgnoreCase("-d"))
196
214
            display = true;
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;
200
218
          }
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;
204
222
          }
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;
208
226
          }
209
227
        }
210
228
      }
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";
 
233
      if (display)
 
234
        outFormat = "bmp";
216
235
      else {
217
236
        outFileTokens = argv[1].split("\\.");
218
 
        if(outFileTokens.length > 1)
 
237
        if (outFileTokens.length > 1)
219
238
          outFormat = outFileTokens[outFileTokens.length - 1];
220
239
      }
221
240
 
222
241
      File file = new File(argv[0]);
223
242
      int width, height;
224
243
 
225
 
      if(inFormat.equalsIgnoreCase("jpg")) {
 
244
      if (inFormat.equalsIgnoreCase("jpg")) {
226
245
        FileInputStream fis = new FileInputStream(file);
227
246
        int inputSize = fis.available();
228
 
        if(inputSize < 1) {
 
247
        if (inputSize < 1) {
229
248
          System.out.println("Input file contains no data");
230
249
          System.exit(1);
231
250
        }
234
253
        fis.close();
235
254
 
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 ||
 
257
            xform.cf != null) {
239
258
          TJTransformer tjt = new TJTransformer(inputBuf);
240
 
          TJTransform t[] = new TJTransform[1];
 
259
          TJTransform[] t = new TJTransform[1];
241
260
          t[0] = xform;
242
261
          t[0].options |= TJTransform.OPT_TRIM;
243
262
          TJDecompressor[] tjdx = tjt.transform(t, 0);
244
263
          tjd = tjdx[0];
245
 
        }
246
 
        else tjd = new TJDecompressor(inputBuf);
 
264
        } else
 
265
          tjd = new TJDecompressor(inputBuf);
247
266
 
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");
 
272
        if (outSubsamp < 0)
 
273
          outSubsamp = inSubsamp;
254
274
 
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);
267
287
 
268
 
        if(!outFormat.equalsIgnoreCase("jpg"))
 
288
        if (!outFormat.equalsIgnoreCase("jpg"))
269
289
          img = tjd.decompress(width, height, BufferedImage.TYPE_INT_RGB,
270
290
                               flags);
271
 
        else bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags);
 
291
        else
 
292
          bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags);
272
293
        tjd.close();
273
 
      }
274
 
      else {
 
294
      } else {
275
295
        img = ImageIO.read(file);
 
296
        if (img == null)
 
297
          throw new Exception("Input image type not supported.");
276
298
        width = img.getWidth();
277
299
        height = img.getHeight();
278
 
        if(outSubsamp < 0) {
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;
 
303
          else
 
304
            outSubsamp = TJ.SAMP_444;
282
305
        }
283
306
      }
284
307
      System.gc();
285
 
      if(!display)
286
 
        System.out.print("Dest. Image (" + outFormat + "):  " + width + " x "
287
 
          + height + " pixels");
 
308
      if (!display)
 
309
        System.out.print("Dest. Image (" + outFormat + "):  " + width + " x " +
 
310
                         height + " pixels");
288
311
 
289
 
      if(display) {
 
312
      if (display) {
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);
294
 
      }
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();
299
321
        int jpegSize;
300
322
        byte[] jpegBuf;
301
323
 
302
324
        tjc.setSubsamp(outSubsamp);
303
325
        tjc.setJPEGQuality(outQual);
304
 
        if(img != null)
 
326
        if (img != null)
305
327
          jpegBuf = tjc.compress(img, flags);
306
328
        else {
307
329
          tjc.setSourceImage(bmpBuf, width, 0, height, TJ.PF_BGRX);
314
336
        FileOutputStream fos = new FileOutputStream(file);
315
337
        fos.write(jpegBuf, 0, jpegSize);
316
338
        fos.close();
317
 
      }
318
 
      else {
 
339
      } else {
319
340
        System.out.print("\n");
320
341
        file = new File(argv[1]);
321
342
        ImageIO.write(img, outFormat, file);
322
343
      }
323
344
 
324
 
    }
325
 
    catch(Exception e) {
 
345
    } catch(Exception e) {
326
346
      e.printStackTrace();
327
347
      System.exit(-1);
328
348
    }
329
349
  }
330
350
 
331
351
  public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion,
332
 
    Rectangle planeRegion, int componentIndex, int transformIndex,
333
 
    TJTransform transform) throws Exception {
334
 
    for(int i=0; i<bufferRegion.width*bufferRegion.height; i++) {
335
 
            coeffBuffer.put(i, (short)(-coeffBuffer.get(i)));
 
352
                           Rectangle planeRegion, int componentIndex,
 
353
                           int transformIndex, TJTransform transform)
 
354
                           throws Exception {
 
355
    for (int i = 0; i < bufferRegion.width * bufferRegion.height; i++) {
 
356
      coeffBuffer.put(i, (short)(-coeffBuffer.get(i)));
336
357
    }
337
358
  }
338
359
 
339
 
  static TJScalingFactor sf [] = null;
 
360
  static TJScalingFactor[] sf = null;
340
361
};