OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2015 The Gifplayer Authors. All Rights Reserved. |
| 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 jp.tomorrowkey.android.gifplayer; |
| 18 |
| 19 import java.io.ByteArrayInputStream; |
| 20 import java.io.IOException; |
| 21 import java.io.InputStream; |
| 22 import java.nio.ByteBuffer; |
| 23 |
| 24 /** |
| 25 * A base wrapper for GIF image data. |
| 26 */ |
| 27 public class BaseGifImage { |
| 28 private final byte[] mData; |
| 29 private final int mOffset; |
| 30 private int mWidth; |
| 31 private int mHeight; |
| 32 |
| 33 private static final byte[] sColorTableBuffer = new byte[256 * 3]; |
| 34 |
| 35 int mHeaderSize; |
| 36 boolean mGlobalColorTableUsed; |
| 37 boolean mError; |
| 38 int[] mGlobalColorTable = new int[256]; |
| 39 int mGlobalColorTableSize; |
| 40 int mBackgroundColor; |
| 41 int mBackgroundIndex; |
| 42 |
| 43 public BaseGifImage(byte[] data) { |
| 44 this(data, 0); |
| 45 } |
| 46 |
| 47 /** |
| 48 * Unlike the desktop JVM, ByteBuffers created with allocateDirect() can (an
d since froyo, do) |
| 49 * provide a backing array, enabling zero-copy interop with native code. How
ever, they are |
| 50 * aligned on a byte boundary, meaning that they often have an arrayOffset a
s well - in those |
| 51 * cases, we can avoid allocating large byte arrays and a copy. |
| 52 */ |
| 53 public BaseGifImage(ByteBuffer data) { |
| 54 this(bufferToArray(data), bufferToOffset(data)); |
| 55 } |
| 56 |
| 57 private static int bufferToOffset(ByteBuffer buffer) { |
| 58 return buffer.hasArray() ? buffer.arrayOffset() : 0; |
| 59 } |
| 60 |
| 61 private static byte[] bufferToArray(ByteBuffer buffer) { |
| 62 if (buffer.hasArray()) { |
| 63 return buffer.array(); |
| 64 } else { |
| 65 int position = buffer.position(); |
| 66 try { |
| 67 byte[] newData = new byte[buffer.capacity()]; |
| 68 buffer.get(newData); |
| 69 return newData; |
| 70 } finally { |
| 71 buffer.position(position); |
| 72 } |
| 73 } |
| 74 } |
| 75 |
| 76 public BaseGifImage(byte[] data, int offset) { |
| 77 mData = data; |
| 78 mOffset = offset; |
| 79 |
| 80 GifHeaderStream stream = new GifHeaderStream(data); |
| 81 stream.skip(offset); |
| 82 try { |
| 83 readHeader(stream); |
| 84 mHeaderSize = stream.getPosition(); |
| 85 } catch (IOException e) { |
| 86 mError = true; |
| 87 } |
| 88 |
| 89 try { |
| 90 stream.close(); |
| 91 } catch (IOException e) { |
| 92 // Ignore |
| 93 } |
| 94 } |
| 95 |
| 96 public byte[] getData() { |
| 97 return mData; |
| 98 } |
| 99 |
| 100 public int getDataOffset() { |
| 101 return mOffset; |
| 102 } |
| 103 |
| 104 public int getWidth() { |
| 105 return mWidth; |
| 106 } |
| 107 |
| 108 public int getHeight() { |
| 109 return mHeight; |
| 110 } |
| 111 |
| 112 /** |
| 113 * Returns an estimate of the size of the object in bytes. |
| 114 */ |
| 115 public int getSizeEstimate() { |
| 116 return mData.length + mGlobalColorTable.length * 4; |
| 117 } |
| 118 |
| 119 /** |
| 120 * Reads GIF file header information. |
| 121 */ |
| 122 private void readHeader(InputStream stream) throws IOException { |
| 123 boolean valid = stream.read() == 'G'; |
| 124 valid = valid && stream.read() == 'I'; |
| 125 valid = valid && stream.read() == 'F'; |
| 126 if (!valid) { |
| 127 mError = true; |
| 128 return; |
| 129 } |
| 130 |
| 131 // Skip the next three letter, which represent the variation of the GIF
standard. |
| 132 stream.skip(3); |
| 133 |
| 134 readLogicalScreenDescriptor(stream); |
| 135 |
| 136 if (mGlobalColorTableUsed && !mError) { |
| 137 readColorTable(stream, mGlobalColorTable, mGlobalColorTableSize); |
| 138 mBackgroundColor = mGlobalColorTable[mBackgroundIndex]; |
| 139 } |
| 140 } |
| 141 |
| 142 /** |
| 143 * Reads Logical Screen Descriptor |
| 144 */ |
| 145 private void readLogicalScreenDescriptor(InputStream stream) throws IOExcept
ion { |
| 146 // logical screen size |
| 147 mWidth = readShort(stream); |
| 148 mHeight = readShort(stream); |
| 149 // packed fields |
| 150 int packed = stream.read(); |
| 151 mGlobalColorTableUsed = (packed & 0x80) != 0; // 1 : global color table
flag |
| 152 // 2-4 : color resolution - ignore |
| 153 // 5 : gct sort flag - ignore |
| 154 mGlobalColorTableSize = 2 << (packed & 7); // 6-8 : gct size |
| 155 mBackgroundIndex = stream.read(); |
| 156 stream.skip(1); // pixel aspect ratio - ignore |
| 157 } |
| 158 |
| 159 /** |
| 160 * Reads color table as 256 RGB integer values |
| 161 * |
| 162 * @param ncolors int number of colors to read |
| 163 */ |
| 164 static boolean readColorTable(InputStream stream, int[] colorTable, int ncol
ors) |
| 165 throws IOException { |
| 166 synchronized (sColorTableBuffer) { |
| 167 int nbytes = 3 * ncolors; |
| 168 int n = stream.read(sColorTableBuffer, 0, nbytes); |
| 169 if (n < nbytes) { |
| 170 return false; |
| 171 } else { |
| 172 int i = 0; |
| 173 int j = 0; |
| 174 while (i < ncolors) { |
| 175 int r = sColorTableBuffer[j++] & 0xff; |
| 176 int g = sColorTableBuffer[j++] & 0xff; |
| 177 int b = sColorTableBuffer[j++] & 0xff; |
| 178 colorTable[i++] = 0xff000000 | (r << 16) | (g << 8) | b; |
| 179 } |
| 180 } |
| 181 } |
| 182 |
| 183 return true; |
| 184 } |
| 185 |
| 186 /** |
| 187 * Reads next 16-bit value, LSB first |
| 188 */ |
| 189 private int readShort(InputStream stream) throws IOException { |
| 190 // read 16-bit value, LSB first |
| 191 return stream.read() | (stream.read() << 8); |
| 192 } |
| 193 |
| 194 private final class GifHeaderStream extends ByteArrayInputStream { |
| 195 |
| 196 private GifHeaderStream(byte[] buf) { |
| 197 super(buf); |
| 198 } |
| 199 |
| 200 public int getPosition() { |
| 201 return pos; |
| 202 } |
| 203 } |
| 204 } |
OLD | NEW |