1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/favicons/decoders/LoadFaviconResult.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.favicons.decoders; 1.9 + 1.10 +import android.graphics.Bitmap; 1.11 +import android.util.Log; 1.12 + 1.13 +import java.io.ByteArrayOutputStream; 1.14 +import java.util.Iterator; 1.15 + 1.16 +/** 1.17 + * Class representing the result of loading a favicon. 1.18 + * This operation will produce either a collection of favicons, a single favicon, or no favicon. 1.19 + * It is necessary to model single favicons differently to a collection of one favicon (An entity 1.20 + * that may not exist with this scheme) since the in-database representation of these things differ. 1.21 + * (In particular, collections of favicons are stored in encoded ICO format, whereas single icons are 1.22 + * stored as decoded bitmap blobs.) 1.23 + */ 1.24 +public class LoadFaviconResult { 1.25 + private static final String LOGTAG = "LoadFaviconResult"; 1.26 + 1.27 + byte[] faviconBytes; 1.28 + int offset; 1.29 + int length; 1.30 + 1.31 + boolean isICO; 1.32 + Iterator<Bitmap> bitmapsDecoded; 1.33 + 1.34 + public Iterator<Bitmap> getBitmaps() { 1.35 + return bitmapsDecoded; 1.36 + } 1.37 + 1.38 + /** 1.39 + * Return a representation of this result suitable for storing in the database. 1.40 + * 1.41 + * @return A byte array containing the bytes from which this result was decoded, 1.42 + * or null if re-encoding failed. 1.43 + */ 1.44 + public byte[] getBytesForDatabaseStorage() { 1.45 + // Begin by normalising the buffer. 1.46 + if (offset != 0 || length != faviconBytes.length) { 1.47 + final byte[] normalised = new byte[length]; 1.48 + System.arraycopy(faviconBytes, offset, normalised, 0, length); 1.49 + offset = 0; 1.50 + faviconBytes = normalised; 1.51 + } 1.52 + 1.53 + // For results containing multiple images, we store the result verbatim. (But cutting the 1.54 + // buffer to size first). 1.55 + // We may instead want to consider re-encoding the entire ICO as a collection of efficiently 1.56 + // encoded PNGs. This may not be worth the CPU time (Indeed, the encoding of single-image 1.57 + // favicons may also not be worth the time/space tradeoff.). 1.58 + if (isICO) { 1.59 + return faviconBytes; 1.60 + } 1.61 + 1.62 + // For results containing a single image, we re-encode the 1.63 + // result as a PNG in an effort to save space. 1.64 + final Bitmap favicon = ((FaviconDecoder.SingleBitmapIterator) bitmapsDecoded).peek(); 1.65 + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); 1.66 + 1.67 + try { 1.68 + if (favicon.compress(Bitmap.CompressFormat.PNG, 100, stream)) { 1.69 + return stream.toByteArray(); 1.70 + } 1.71 + } catch (OutOfMemoryError e) { 1.72 + Log.w(LOGTAG, "Out of memory re-compressing favicon."); 1.73 + } 1.74 + 1.75 + Log.w(LOGTAG, "Favicon re-compression failed."); 1.76 + return null; 1.77 + } 1.78 + 1.79 +}