001: /*
002: * $RCSfile: TIFFCodecLibT6Compressor.java,v $
003: *
004: *
005: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * - Redistribution of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * - Redistribution in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of
020: * contributors may be used to endorse or promote products derived
021: * from this software without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any
024: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
025: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
026: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027: * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
028: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
029: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
031: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035: * POSSIBILITY OF SUCH DAMAGES.
036: *
037: * You acknowledge that this software is not designed or intended for
038: * use in the design, construction, operation or maintenance of any
039: * nuclear facility.
040: *
041: * $Revision: 1.2 $
042: * $Date: 2006/01/30 23:22:34 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.tiff;
046:
047: public class TIFFCodecLibT6Compressor extends TIFFT6Compressor {
048:
049: private static final boolean DEBUG = false; // XXX 'false' for release!!!
050:
051: Object encoder;
052:
053: public TIFFCodecLibT6Compressor() {
054: super ();
055:
056: try {
057: com.sun.medialib.codec.g4fax.Encoder encoder = new com.sun.medialib.codec.g4fax.Encoder();
058: this .encoder = encoder;
059: } catch (Throwable t) {
060: throw new RuntimeException("CodecLib not available");
061: }
062: }
063:
064: /**
065: * Encode a buffer of data using CCITT T.6 Compression also known as
066: * Group 4 facsimile compression.
067: *
068: * @param data The row of data to compress.
069: * @param lineStride Byte step between the same sample in different rows.
070: * @param colOffset Bit offset within first <code>data[rowOffset]</code>.
071: * @param width Number of bits in the row.
072: * @param height Number of rows in the buffer.
073: * @param compData The compressed data.
074: *
075: * @return The number of bytes saved in the compressed data array.
076: */
077: public synchronized final int encodeT6(byte[] data, int lineStride,
078: int colOffset, int width, int height, byte[] compData) {
079:
080: // Defer to superclass if bit offset is not byte-aligned.
081: if (colOffset % 8 != 0) {
082: return super .encodeT6(data, lineStride, colOffset, width,
083: height, compData);
084: }
085:
086: // Set image to data if possible; otherwise copy.
087: int bytesPerRow = (width + 7) / 8;
088: byte[] image = null;
089:
090: if (colOffset == 0 && bytesPerRow == lineStride) {
091: image = data;
092: } else {
093: image = new byte[bytesPerRow * height];
094: int dataOffset = colOffset / 8;
095: int imageOffset = 0;
096: for (int row = 0; row < height; row++) {
097: System.arraycopy(data, dataOffset, image, imageOffset,
098: bytesPerRow);
099: dataOffset += lineStride;
100: imageOffset += bytesPerRow;
101: }
102: }
103:
104: // Attempt the codecLib encoder.
105: com.sun.medialib.codec.g4fax.Encoder clibEncoder = (com.sun.medialib.codec.g4fax.Encoder) encoder;
106: //System.out.println("Using codecLib G4 encoder");
107:
108: // Set encoding flags.
109: int encodingFlags = inverseFill ? com.sun.medialib.codec.g4fax.Constants.G4FAX_LSB2MSB
110: : 0;
111:
112: int result = com.sun.medialib.codec.g4fax.Constants.G4FAX_FAILURE;
113: try {
114: if (DEBUG) {
115: System.out.println("Using MediaLib G4 encoder");
116: }
117: result = clibEncoder.encode(compData, image, width, height,
118: encodingFlags);
119: } catch (Throwable t) {
120: if (DEBUG) {
121: System.out.println("MediaLib G4 encoder failed: " + t);
122: }
123: // XXX Should write a warning to listeners here.
124: result = com.sun.medialib.codec.g4fax.Constants.G4FAX_FAILURE;
125: }
126:
127: // If the codecLib encoder failed, try the superclass.
128: if (result == com.sun.medialib.codec.g4fax.Constants.G4FAX_FAILURE) {
129: if (DEBUG) {
130: System.out.println("Falling back to Java G4 encoder");
131: }
132: result = super.encodeT6(data, lineStride, colOffset, width,
133: height, compData);
134: }
135:
136: return result;
137: }
138: }
|