001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024: package com.sun.mmedia;
025:
026: /**
027: * Utility class that provides various array conversion
028: *
029: */
030: public final class FormatConversionUtils {
031:
032: /**
033: * Converts image int[] array (32-bit per pixel)
034: * to byte[] xRGB array (4 bytes per pixel)
035: * int<->byte[4] conversion is platform specific !
036: *
037: * @param ints integer array to convert
038: * @return same array but converted to byte[] (in big-endian format)
039: */
040: public static byte[] intArrayToByteArray(int[] ints) {
041: if (ints == null)
042: return null;
043:
044: byte[] bytes = new byte[ints.length * 4];
045:
046: int intcount, bytecount;
047: for (intcount = 0, bytecount = 0; intcount < ints.length;) {
048: bytes[bytecount++] = (byte) ((ints[intcount] & 0xFF000000) >> 24);
049: bytes[bytecount++] = (byte) ((ints[intcount] & 0x00FF0000) >> 16);
050: bytes[bytecount++] = (byte) ((ints[intcount] & 0x0000FF00) >> 8);
051: bytes[bytecount++] = (byte) ((ints[intcount] & 0x000000FF));
052: intcount++;
053: }
054: return bytes;
055: }
056:
057: /**
058: * Converts image byte[] xRGB array (4 bytes per pixel)
059: * to int[] array (32-bit per pixel),
060: * int<->byte[4] conversion is platform specific !
061: *
062: * @param bytes byte array to convert
063: * @return same array but converted to int[] (in big-endian format)
064: */
065: public static int[] byteArrayToIntArray(byte[] bytes) {
066: if (bytes == null)
067: return null;
068:
069: int[] ints = new int[bytes.length / 4];
070:
071: int intcount, bytecount;
072: for (intcount = 0, bytecount = 0; bytecount < bytes.length;) {
073: ints[intcount] = ((((int) (bytes[bytecount + 0])) << 24) & 0xFF000000)
074: | //A
075: ((((int) (bytes[bytecount + 1])) << 16) & 0x00FF0000)
076: | //R
077: ((((int) (bytes[bytecount + 2])) << 8) & 0x0000FF00)
078: | //G
079: ((((int) (bytes[bytecount + 3]))) & 0x000000FF); //B
080:
081: intcount++;
082: bytecount += 4;
083:
084: }
085: return ints;
086: }
087:
088: /**
089: * Creates a copy of a string array.
090: * Primary usage of this method is to create copies of
091: * preset, parameter, metadata and other arrays, returned to user.
092: *
093: * @param array String array to copy
094: * @return copy of array
095: */
096: public static String[] stringArrayCopy(String[] array) {
097: if (array == null)
098: return null;
099:
100: String[] copy = new String[array.length];
101: System.arraycopy(array, 0, copy, 0, array.length);
102: return copy;
103: }
104: }
|