001: /*
002: * $RCSfile: TIFFCodecLibRLECompressor.java,v $
003: *
004: *
005: * Copyright (c) 2006 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.1 $
042: * $Date: 2006/04/22 00:04:23 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.tiff;
046:
047: import java.io.IOException;
048: import javax.imageio.IIOException;
049:
050: public class TIFFCodecLibRLECompressor extends TIFFRLECompressor {
051:
052: private static final boolean DEBUG = false; // XXX 'false' for release!!!
053:
054: Object encoder;
055:
056: public TIFFCodecLibRLECompressor() {
057: super ();
058:
059: try {
060: com.sun.medialib.codec.g3fax.Encoder encoder = new com.sun.medialib.codec.g3fax.Encoder();
061: this .encoder = encoder;
062: } catch (Throwable t) {
063: throw new RuntimeException("CodecLib not available");
064: }
065: }
066:
067: public int encode(byte[] b, int off, int width, int height,
068: int[] bitsPerSample, int scanlineStride) throws IOException {
069: if (bitsPerSample.length != 1 || bitsPerSample[0] != 1) {
070: throw new IIOException(
071: "Bits per sample must be 1 for RLE compression!");
072: }
073:
074: // Set image to data if possible; otherwise copy.
075: int bytesPerRow = (width + 7) / 8;
076: byte[] image = null;
077:
078: if (off == 0 && bytesPerRow == scanlineStride) {
079: image = b;
080: } else {
081: image = new byte[bytesPerRow * height];
082: int dataOffset = off;
083: int imageOffset = 0;
084: for (int row = 0; row < height; row++) {
085: System.arraycopy(b, dataOffset, image, imageOffset,
086: bytesPerRow);
087: dataOffset += scanlineStride;
088: imageOffset += bytesPerRow;
089: }
090: }
091:
092: // In the worst case, 2 bits of input will result in 9 bits of output,
093: // plus 2 extra bits if the row starts with black.
094: int maxBits = 9 * ((width + 1) / 2) + 2;
095: byte[] compData = new byte[((maxBits + 7) / 8) * height];
096:
097: // Attempt the codecLib encoder.
098: com.sun.medialib.codec.g3fax.Encoder clibEncoder = (com.sun.medialib.codec.g3fax.Encoder) encoder;
099:
100: // Set RLE encoding flag.
101: int encodingFlags = com.sun.medialib.codec.g3fax.Constants.G3FAX_RLE_CODING;
102: if (inverseFill) {
103: encodingFlags |= com.sun.medialib.codec.g3fax.Constants.G3FAX_LSB2MSB;
104: if (DEBUG) {
105: System.out.println("Setting LSB flag");
106: }
107: }
108:
109: // Set result flag.
110: int result = com.sun.medialib.codec.g3fax.Constants.G3FAX_FAILURE;
111: try {
112: if (DEBUG) {
113: System.out.println("Using MediaLib RLE encoder");
114: }
115: result = clibEncoder.encode(compData, image, width, height,
116: 2, // k parameter
117: encodingFlags);
118: stream.write(compData, 0, result);
119: } catch (Throwable t) {
120: if (DEBUG) {
121: System.out.println("MediaLib RLE encoder failed: " + t);
122: }
123: // XXX Should write a warning to listeners here.
124: result = com.sun.medialib.codec.g3fax.Constants.G3FAX_FAILURE;
125: }
126:
127: // If the codecLib encoder failed, try the superclass.
128: if (result == com.sun.medialib.codec.g3fax.Constants.G3FAX_FAILURE) {
129: if (DEBUG) {
130: System.out.println("Falling back to Java RLE encoder");
131: }
132: result = super.encode(b, off, width, height, bitsPerSample,
133: scanlineStride);
134: }
135:
136: return result;
137: }
138: }
|