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 17 package com.android.ide.eclipse.gltrace; 18 19 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage; 20 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.ImageData; 23 import org.eclipse.swt.graphics.PaletteData; 24 import org.eclipse.swt.widgets.Display; 25 import org.liblzf.CLZF; 26 27 /** Utilities to deal with protobuf encoded {@link GLMessage}. */ 28 public class ProtoBufUtils { getImageData(GLMessage glMsg)29 private static ImageData getImageData(GLMessage glMsg) { 30 int width = glMsg.getFb().getWidth(); 31 int height = glMsg.getFb().getHeight(); 32 33 byte[] compressed = glMsg.getFb().getContents(0).toByteArray(); 34 byte[] uncompressed = new byte[width * height * 4]; 35 36 int size = CLZF.lzf_decompress(compressed, compressed.length, 37 uncompressed, uncompressed.length); 38 assert size == width * height * 4 : "Unexpected image size after decompression."; 39 40 int redMask = 0xff000000; 41 int greenMask = 0x00ff0000; 42 int blueMask = 0x0000ff00; 43 PaletteData palette = new PaletteData(redMask, greenMask, blueMask); 44 ImageData imageData = new ImageData( 45 width, 46 height, 47 32, // depth 48 palette, 49 1, // scan line padding 50 uncompressed); 51 imageData = imageData.scaledTo(imageData.width, -imageData.height); 52 53 return imageData; 54 } 55 56 /** Obtains the image stored in provided protocol buffer message. */ getImage(Display display, GLMessage glMsg)57 public static Image getImage(Display display, GLMessage glMsg) { 58 if (!glMsg.hasFb()) { 59 return null; 60 } 61 62 return new Image(display, getImageData(glMsg)); 63 } 64 65 /** 66 * Obtains the image stored in provided protocol buffer message scaled to the 67 * provided dimensions. 68 */ getScaledImage(Display display, GLMessage glMsg, int width, int height)69 public static Image getScaledImage(Display display, GLMessage glMsg, int width, int height) { 70 if (!glMsg.hasFb()) { 71 return null; 72 } 73 74 ImageData imageData = getImageData(glMsg); 75 return new Image(display, imageData.scaledTo(width, height)); 76 } 77 } 78