/* MediaUtil LLJTran - $RCSfile: LLJTran.java,v $
* Copyright (C) 1999-2005 Dmitriy Rogatkin, Suresh Mahalingam. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* $Id: LLJTran.java,v 1.6 2006/05/13 07:24:01 msuresh Exp $
*
* Some ideas and algorithms were borrowed from:
* Thomas G. Lane, and James R. Weeks
*
*/
package mediautil.image.jpeg;
import mediautil.gen.BasicIo;
import mediautil.gen.FileFormatException;
import mediautil.gen.Log;
import mediautil.gen.ProgressCallback;
import mediautil.gen.directio.IterativeReader;
import mediautil.gen.directio.IterativeWriter;
import java.awt.*;
import java.io.*;
class IterativeReadVars
{
public static final int READING_STAGE = 1;
public static final int READING_DCT_STAGE = 2;
public static final int READING_APPX_STAGE = 3;
public static final int IMAGE_READ_STAGE = 4;
public static final int DONE_STAGE = 5;
// For info
public int minReadRequest;
public int maxReadRequest;
public InputStream is;
public int readUpto;
public int stage;
public int sections;
public boolean keep_appxs, appxsCleared;
public int appxPos, appxLen;
public boolean throwException;
//Vars for readDCT
public double currentProgress, callbackProgress, progressPerMcu;
public int []last_dc;
public int [][]DCT;
public int next_restart_num;
public int ix, iy;
}
class IterativeWriteVars
{
public static final int WRITE_BEGIN = 0;
public static final int WRITE_COMMENTS = 1;
public static final int WRITE_APPXS = 2;
public static final int WRITE_DQT = 3;
public static final int WRITE_DHT = 4;
public static final int WRITE_START = 5;
public static final int WRITE_DCT = 6;
public static final int WRITE_COMPLETE = 7;
// For info
public int minWriteRequest;
public int maxWriteRequest;
public OutputStream os;
public int op;
public int options;
public int restart_interval;
public String comment;
public Class custom_appx;
public int state = WRITE_COMPLETE;
public byte huffTables[];
public int currentAppxPos, currentAppx;
public byte saveAppxs[];
public int svX;
public int svY;
public int svWidthMCU;
public int svHeightMCU;
// initWriteDCT vars
public boolean transformDct;
public int [][][][][] new_dct_coefs;
public double currentProgress, callbackProgress, progressPerMcu;
public int []last_dc;
public int restarts_to_go;
public int xCropOffsetMCU;
public int yCropOffsetMCU;
public boolean handleXEdge;
public boolean handleYEdge;
public int new_ix, new_iy;
public boolean pullDownMode;
// For unused method writeJpeg
public boolean restoreVars;
// if transformDct true it indicates if full Dct array needs to be allocated
// or if the rows of old dct array can be reused.
public boolean reuseDctRows = true;
public void freeMemory()
{
huffTables = null;
new_dct_coefs = null;
last_dc = null;
}
}
/**
* LLJTran is a class for Lossless Jpeg Transformation.<p>
*
* With the help of Supporting classes like Exif it can also be used to change
* Image Header Information (Exif header) like Date, Thumbnail, Orientation
* etc.<p>
*
* Following are the key features:<p>
*
* <ul>
* <li> Supports lossless rotation, transpose, transverse and crop
* <li> Trimming or relocating Non Transformable edge blocks similiar to
* <a target=_blank href="http://www.ijg.org">jpegtran</a> or processing them
* like regular MCU blocks. Please see the documentation for the
* {@link #OPT_XFORM_TRIM} and {@link #OPT_XFORM_ADJUST_EDGES} for more info
* on this.
* <li> Reading and Modifying Image Header Information (Exif) including
* Thumbnail
* <li> Built-in transformation of Thumbnail and Orientation marker
* <li> Supports directio Interfaces {@link IterativeReader} and {@link IterativeWriter}
* enabling things like Sharing the jpeg input file with say jkd's
* ImageReader while reading
* <li>Does <b>not</b> Support Multi-Threading for the same Object to be
* used simultaneously by more than one thread. However different threads
* can have their own LLJTran Objects.
* </ul>
*
* @author Dmitriy Rogatkin & Suresh Mahalingam (msuresh@cheerful.com)
*/
public class LLJTran extends BasicJpegIo
implements IterativeReader, IterativeWriter {
/** Name of the JFIF format */
public static final String JFIF = "JFIF";
/** Name of the FPXR format */
public static final String FPXR = "FPXR";
/** Name of the JPEG format */
public static final String JPEG = "JPEG";
// TODO: move rotation constants to some interface
/** Identifies the identity transformation or no transformation */
public static final int NONE = 0;
/** Identifies the Horizontal Flip transformation */
public static final int FLIP_H = 1;
/** Identifies the Vertical Flip transformation */
public static final int FLIP_V = 2;
/** Identifies the Transpose transformation */
public static final int TRANSPOSE = 3;
/** Identifies the Transverse transformation */
public static final int TRANSVERSE = 4;
/** Identifies the Rotate 90 degrees clockwise transformation */
public static final int ROT_90 = 5;
/** Identifies the Rotate 180 degrees transformation */
public static final int ROT_180 = 6;
/** Identifies the Rotate 270 degrees clockwise transformation */
public static final int ROT_270 = 7;
/** Identifies the crop transformation */
public static final int CROP = 8;
/** Identifies the comment transformation.
* This is not used in LLJTran as of now */
public static final int COMMENT = 9;
protected static final int DCTSIZE2 = 64;
protected static final int DCTSIZE = 8;
protected static final int BYTE_SIZE = 8;
private static final int HUFF_LOOKAHEAD = 8;
/** Identifies that no part of the Image has been read successfully. Not a
* valid option while reading */
public static final int READ_NONE = 0;
/** Identifies the option to read upto the Image Header Info while reading */
public static final int READ_INFO = 1;
/** Identifies the option to read upto the Image Header including image
* dimensions, parameters and tables for decoding while reading */
public static final int READ_HEADER = 2;
/** Identifies the option to read the entire image while reading */
public static final int READ_ALL = 3;
/** Flag to specifiy that the Image Header information (Exif width, height
* and resolution) should be adjusted when transforming the ima