1e85730157e0b4fb12b0500d1a41b3e370874a74dDRC/*
27a8c53e4bd7a0dbe442f4643f7e92f5355f69a68DRC * Copyright (C)2011, 2013-2015 D. R. Commander.  All Rights Reserved.
36eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * Copyright (C)2015 Viktor Szathmáry.  All Rights Reserved.
4e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *
5e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * Redistribution and use in source and binary forms, with or without
6e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * modification, are permitted provided that the following conditions are met:
7e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *
8e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * - Redistributions of source code must retain the above copyright notice,
9e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *   this list of conditions and the following disclaimer.
10e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * - Redistributions in binary form must reproduce the above copyright notice,
11e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *   this list of conditions and the following disclaimer in the documentation
12e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *   and/or other materials provided with the distribution.
13e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * - Neither the name of the libjpeg-turbo Project nor the names of its
14e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *   contributors may be used to endorse or promote products derived from this
15e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *   software without specific prior written permission.
16e85730157e0b4fb12b0500d1a41b3e370874a74dDRC *
17e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27e85730157e0b4fb12b0500d1a41b3e370874a74dDRC * POSSIBILITY OF SUCH DAMAGE.
28e85730157e0b4fb12b0500d1a41b3e370874a74dDRC */
29e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
30e85730157e0b4fb12b0500d1a41b3e370874a74dDRCpackage org.libjpegturbo.turbojpeg;
31e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
3292549de2c2b139070294aec12ec39b7c86a56b52DRC/**
3392549de2c2b139070294aec12ec39b7c86a56b52DRC * TurboJPEG lossless transformer
3492549de2c2b139070294aec12ec39b7c86a56b52DRC */
35e85730157e0b4fb12b0500d1a41b3e370874a74dDRCpublic class TJTransformer extends TJDecompressor {
36e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
3792549de2c2b139070294aec12ec39b7c86a56b52DRC  /**
3892549de2c2b139070294aec12ec39b7c86a56b52DRC   * Create a TurboJPEG lossless transformer instance.
3992549de2c2b139070294aec12ec39b7c86a56b52DRC   */
406eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  public TJTransformer() throws TJException {
41e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    init();
42e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
43e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
4492549de2c2b139070294aec12ec39b7c86a56b52DRC  /**
4592549de2c2b139070294aec12ec39b7c86a56b52DRC   * Create a TurboJPEG lossless transformer instance and associate the JPEG
46fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * image stored in <code>jpegImage</code> with the newly created instance.
4792549de2c2b139070294aec12ec39b7c86a56b52DRC   *
482c74e5124d25112809bdc26cbc36aa764e8870c3DRC   * @param jpegImage JPEG image buffer (size of the JPEG image is assumed to
497a8c53e4bd7a0dbe442f4643f7e92f5355f69a68DRC   * be the length of the array.)  This buffer is not modified.
5092549de2c2b139070294aec12ec39b7c86a56b52DRC   */
516eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  public TJTransformer(byte[] jpegImage) throws TJException {
52e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    init();
53fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    setSourceImage(jpegImage, jpegImage.length);
54e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
55e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
5692549de2c2b139070294aec12ec39b7c86a56b52DRC  /**
5792549de2c2b139070294aec12ec39b7c86a56b52DRC   * Create a TurboJPEG lossless transformer instance and associate the JPEG
5892549de2c2b139070294aec12ec39b7c86a56b52DRC   * image of length <code>imageSize</code> bytes stored in
59fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * <code>jpegImage</code> with the newly created instance.
6092549de2c2b139070294aec12ec39b7c86a56b52DRC   *
617a8c53e4bd7a0dbe442f4643f7e92f5355f69a68DRC   * @param jpegImage JPEG image buffer.  This buffer is not modified.
6292549de2c2b139070294aec12ec39b7c86a56b52DRC   *
632c74e5124d25112809bdc26cbc36aa764e8870c3DRC   * @param imageSize size of the JPEG image (in bytes)
6492549de2c2b139070294aec12ec39b7c86a56b52DRC   */
656eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  public TJTransformer(byte[] jpegImage, int imageSize) throws TJException {
66e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    init();
67fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    setSourceImage(jpegImage, imageSize);
68e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
69e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
7092549de2c2b139070294aec12ec39b7c86a56b52DRC  /**
7192549de2c2b139070294aec12ec39b7c86a56b52DRC   * Losslessly transform the JPEG image associated with this transformer
7292549de2c2b139070294aec12ec39b7c86a56b52DRC   * instance into one or more JPEG images stored in the given destination
7392549de2c2b139070294aec12ec39b7c86a56b52DRC   * buffers.  Lossless transforms work by moving the raw coefficients from one
7492549de2c2b139070294aec12ec39b7c86a56b52DRC   * JPEG image structure to another without altering the values of the
7592549de2c2b139070294aec12ec39b7c86a56b52DRC   * coefficients.  While this is typically faster than decompressing the
7692549de2c2b139070294aec12ec39b7c86a56b52DRC   * image, transforming it, and re-compressing it, lossless transforms are not
7765d4a46d3b01dd56a03565b62f223262698b1bedDRC   * free.  Each lossless transform requires reading and performing Huffman
7865d4a46d3b01dd56a03565b62f223262698b1bedDRC   * decoding on all of the coefficients in the source image, regardless of the
7965d4a46d3b01dd56a03565b62f223262698b1bedDRC   * size of the destination image.  Thus, this method provides a means of
8065d4a46d3b01dd56a03565b62f223262698b1bedDRC   * generating multiple transformed images from the same source or of applying
8165d4a46d3b01dd56a03565b62f223262698b1bedDRC   * multiple transformations simultaneously, in order to eliminate the need to
8265d4a46d3b01dd56a03565b62f223262698b1bedDRC   * read the source coefficients multiple times.
8392549de2c2b139070294aec12ec39b7c86a56b52DRC   *
842c74e5124d25112809bdc26cbc36aa764e8870c3DRC   * @param dstBufs an array of image buffers.  <code>dstbufs[i]</code> will
8592549de2c2b139070294aec12ec39b7c86a56b52DRC   * receive a JPEG image that has been transformed using the parameters in
862c74e5124d25112809bdc26cbc36aa764e8870c3DRC   * <code>transforms[i]</code>.  Use {@link TJ#bufSize} to determine the
8765d4a46d3b01dd56a03565b62f223262698b1bedDRC   * maximum size for each buffer based on the transformed or cropped width and
88fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * height and the level of subsampling used in the source image.
8992549de2c2b139070294aec12ec39b7c86a56b52DRC   *
902c74e5124d25112809bdc26cbc36aa764e8870c3DRC   * @param transforms an array of {@link TJTransform} instances, each of
9192549de2c2b139070294aec12ec39b7c86a56b52DRC   * which specifies the transform parameters and/or cropping region for the
9292549de2c2b139070294aec12ec39b7c86a56b52DRC   * corresponding transformed output image
9392549de2c2b139070294aec12ec39b7c86a56b52DRC   *
94fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * @param flags the bitwise OR of one or more of
95fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
9692549de2c2b139070294aec12ec39b7c86a56b52DRC   */
97e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  public void transform(byte[][] dstBufs, TJTransform[] transforms,
986eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis                        int flags) throws TJException {
9967bee8683db22050e346e0d002c5969da854d582DRC    if (jpegBuf == null)
1006eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis      throw new IllegalStateException("JPEG buffer not initialized");
101e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    transformedSizes = transform(jpegBuf, jpegBufSize, dstBufs, transforms,
10267bee8683db22050e346e0d002c5969da854d582DRC                                 flags);
103e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
10467bee8683db22050e346e0d002c5969da854d582DRC
10592549de2c2b139070294aec12ec39b7c86a56b52DRC  /**
10692549de2c2b139070294aec12ec39b7c86a56b52DRC   * Losslessly transform the JPEG image associated with this transformer
10792549de2c2b139070294aec12ec39b7c86a56b52DRC   * instance and return an array of {@link TJDecompressor} instances, each of
10892549de2c2b139070294aec12ec39b7c86a56b52DRC   * which has a transformed JPEG image associated with it.
10992549de2c2b139070294aec12ec39b7c86a56b52DRC   *
1102c74e5124d25112809bdc26cbc36aa764e8870c3DRC   * @param transforms an array of {@link TJTransform} instances, each of
11192549de2c2b139070294aec12ec39b7c86a56b52DRC   * which specifies the transform parameters and/or cropping region for the
11292549de2c2b139070294aec12ec39b7c86a56b52DRC   * corresponding transformed output image
11392549de2c2b139070294aec12ec39b7c86a56b52DRC   *
11492549de2c2b139070294aec12ec39b7c86a56b52DRC   * @return an array of {@link TJDecompressor} instances, each of
11540dd3146cde2ba5036fe76a4f09e1125b4592347DRC   * which has a transformed JPEG image associated with it.
11692549de2c2b139070294aec12ec39b7c86a56b52DRC   *
117fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * @param flags the bitwise OR of one or more of
118fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * {@link TJ#FLAG_BOTTOMUP TJ.FLAG_*}
11992549de2c2b139070294aec12ec39b7c86a56b52DRC   */
120e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  public TJDecompressor[] transform(TJTransform[] transforms, int flags)
1216eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis                                    throws TJException {
122e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    byte[][] dstBufs = new byte[transforms.length][];
123580f3915378acde2552c4a1fcf5934c87fc71350DRC    if (jpegWidth < 1 || jpegHeight < 1)
1246eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis      throw new IllegalStateException("JPEG buffer not initialized");
12567bee8683db22050e346e0d002c5969da854d582DRC    for (int i = 0; i < transforms.length; i++) {
126580f3915378acde2552c4a1fcf5934c87fc71350DRC      int w = jpegWidth, h = jpegHeight;
12767bee8683db22050e346e0d002c5969da854d582DRC      if ((transforms[i].options & TJTransform.OPT_CROP) != 0) {
12867bee8683db22050e346e0d002c5969da854d582DRC        if (transforms[i].width != 0) w = transforms[i].width;
12967bee8683db22050e346e0d002c5969da854d582DRC        if (transforms[i].height != 0) h = transforms[i].height;
130e85730157e0b4fb12b0500d1a41b3e370874a74dDRC      }
131580f3915378acde2552c4a1fcf5934c87fc71350DRC      dstBufs[i] = new byte[TJ.bufSize(w, h, jpegSubsamp)];
132e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    }
133e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    TJDecompressor[] tjd = new TJDecompressor[transforms.length];
134e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    transform(dstBufs, transforms, flags);
13567bee8683db22050e346e0d002c5969da854d582DRC    for (int i = 0; i < transforms.length; i++)
136e85730157e0b4fb12b0500d1a41b3e370874a74dDRC      tjd[i] = new TJDecompressor(dstBufs[i], transformedSizes[i]);
13792549de2c2b139070294aec12ec39b7c86a56b52DRC    return tjd;
138e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
13967bee8683db22050e346e0d002c5969da854d582DRC
14092549de2c2b139070294aec12ec39b7c86a56b52DRC  /**
141fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * Returns an array containing the sizes of the transformed JPEG images
142fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * generated by the most recent transform operation.
14392549de2c2b139070294aec12ec39b7c86a56b52DRC   *
144fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC   * @return an array containing the sizes of the transformed JPEG images
14540dd3146cde2ba5036fe76a4f09e1125b4592347DRC   * generated by the most recent transform operation.
14692549de2c2b139070294aec12ec39b7c86a56b52DRC   */
1476eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  public int[] getTransformedSizes() {
14867bee8683db22050e346e0d002c5969da854d582DRC    if (transformedSizes == null)
1496eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis      throw new IllegalStateException("No image has been transformed yet");
150e85730157e0b4fb12b0500d1a41b3e370874a74dDRC    return transformedSizes;
151e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
152e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
1536eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  private native void init() throws TJException;
154e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
155e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  private native int[] transform(byte[] srcBuf, int srcSize, byte[][] dstBufs,
1566eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis    TJTransform[] transforms, int flags) throws TJException;
157e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
158e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  static {
159b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    TJLoader.load();
160e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  }
161e85730157e0b4fb12b0500d1a41b3e370874a74dDRC
162e85730157e0b4fb12b0500d1a41b3e370874a74dDRC  private int[] transformedSizes = null;
1636eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis}
164