Home | History | Annotate | Download | only in image
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.chimpchat.adb.image;
     17 
     18 import com.android.ddmlib.RawImage;
     19 
     20 import java.awt.Point;
     21 import java.awt.image.BufferedImage;
     22 import java.awt.image.DataBuffer;
     23 import java.awt.image.DataBufferByte;
     24 import java.awt.image.PixelInterleavedSampleModel;
     25 import java.awt.image.Raster;
     26 import java.awt.image.WritableRaster;
     27 import java.util.Hashtable;
     28 /**
     29  * Useful image related functions.
     30  */
     31 public class ImageUtils {
     32     // Utility class
     33     private ImageUtils() { }
     34 
     35     private static Hashtable<?,?> EMPTY_HASH = new Hashtable();
     36     private static int[] BAND_OFFSETS_32 = { 0, 1, 2, 3 };
     37     private static int[] BAND_OFFSETS_16 = { 0, 1 };
     38 
     39     /**
     40      * Convert a raw image into a buffered image.
     41      *
     42      * @param rawImage the raw image to convert
     43      * @param image the old image to (possibly) recycle
     44      * @return the converted image
     45      */
     46     public static BufferedImage convertImage(RawImage rawImage, BufferedImage image) {
     47         switch (rawImage.bpp) {
     48             case 16:
     49                 return rawImage16toARGB(image, rawImage);
     50             case 32:
     51                 return rawImage32toARGB(rawImage);
     52         }
     53         return null;
     54     }
     55 
     56     /**
     57      * Convert a raw image into a buffered image.
     58      *
     59      * @param rawImage the image to convert.
     60      * @return the converted image.
     61      */
     62     public static BufferedImage convertImage(RawImage rawImage) {
     63         return convertImage(rawImage, null);
     64     }
     65 
     66     static int getMask(int length) {
     67         int res = 0;
     68         for (int i = 0 ; i < length ; i++) {
     69             res = (res << 1) + 1;
     70         }
     71 
     72         return res;
     73     }
     74 
     75     private static BufferedImage rawImage32toARGB(RawImage rawImage) {
     76         // Do as much as we can to not make an extra copy of the data.  This is just a bunch of
     77         // classes that wrap's the raw byte array of the image data.
     78         DataBufferByte dataBuffer = new DataBufferByte(rawImage.data, rawImage.size);
     79 
     80         PixelInterleavedSampleModel sampleModel =
     81             new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, rawImage.width, rawImage.height,
     82                     4, rawImage.width * 4, BAND_OFFSETS_32);
     83         WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer,
     84                 new Point(0, 0));
     85         return new BufferedImage(new ThirtyTwoBitColorModel(rawImage), raster, false, EMPTY_HASH);
     86     }
     87 
     88     private static BufferedImage rawImage16toARGB(BufferedImage image, RawImage rawImage) {
     89         // Do as much as we can to not make an extra copy of the data.  This is just a bunch of
     90         // classes that wrap's the raw byte array of the image data.
     91         DataBufferByte dataBuffer = new DataBufferByte(rawImage.data, rawImage.size);
     92 
     93         PixelInterleavedSampleModel sampleModel =
     94             new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, rawImage.width, rawImage.height,
     95                     2, rawImage.width * 2, BAND_OFFSETS_16);
     96         WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer,
     97                 new Point(0, 0));
     98         return new BufferedImage(new SixteenBitColorModel(rawImage), raster, false, EMPTY_HASH);
     99     }
    100 }
    101