01: /*
02: * $RCSfile: CodecUtils.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.2 $
09: * $Date: 2006/08/22 00:12:04 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.codecimpl;
13:
14: import java.awt.image.RenderedImage;
15: import java.awt.image.SampleModel;
16: import java.awt.image.SinglePixelPackedSampleModel;
17: import java.io.IOException;
18: import java.lang.reflect.InvocationTargetException;
19: import java.lang.reflect.Method;
20:
21: /**
22: * A class for utility functions for codecs.
23: */
24: class CodecUtils {
25: /**
26: * The <code>initCause()</code> method of <code>IOException</code>
27: * which is available from J2SE 1.4 onward.
28: */
29: static Method ioExceptionInitCause;
30:
31: static {
32: try {
33: Class c = Class.forName("java.io.IOException");
34: ioExceptionInitCause = c.getMethod("initCause",
35: new Class[] { java.lang.Throwable.class });
36: } catch (Exception e) {
37: ioExceptionInitCause = null;
38: }
39: }
40:
41: /**
42: * Returns <code>true</code> if and only if <code>im</code>
43: * has a <code>SinglePixelPackedSampleModel</code> with a
44: * sample size of at most 8 bits for each of its bands.
45: *
46: * @param src The <code>RenderedImage</code> to test.
47: * @return Whether the image is byte-packed.
48: */
49: static final boolean isPackedByteImage(RenderedImage im) {
50: SampleModel imageSampleModel = im.getSampleModel();
51:
52: if (imageSampleModel instanceof SinglePixelPackedSampleModel) {
53: for (int i = 0; i < imageSampleModel.getNumBands(); i++) {
54: if (imageSampleModel.getSampleSize(i) > 8) {
55: return false;
56: }
57: }
58:
59: return true;
60: }
61:
62: return false;
63: }
64:
65: /**
66: * Converts the parameter exception to an <code>IOException</code>.
67: */
68: static final IOException toIOException(Exception cause) {
69: IOException ioe;
70:
71: if (cause != null) {
72: if (cause instanceof IOException) {
73: ioe = (IOException) cause;
74: } else if (ioExceptionInitCause != null) {
75: ioe = new IOException(cause.getMessage());
76: try {
77: ioExceptionInitCause.invoke(ioe,
78: new Object[] { cause });
79: } catch (Exception e2) {
80: // Ignore it ...
81: }
82: } else {
83: ioe = new IOException(cause.getClass().getName() + ": "
84: + cause.getMessage());
85: }
86: } else {
87: ioe = new IOException();
88: }
89:
90: return ioe;
91: }
92: }
|