SkImageDecoder.h revision 3f1f06a26bdb2022a5c72f93ae623a57b6659464
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkImageDecoder_DEFINED
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkImageDecoder_DEFINED
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmap.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRefCnt.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkStream;
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkImageDecoder
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Base class for decoding compressed images into a SkBitmap
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkImageDecoder {
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual ~SkImageDecoder();
3225e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Format {
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kUnknown_Format,
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kBMP_Format,
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kGIF_Format,
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kICO_Format,
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kJPEG_Format,
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kPNG_Format,
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kWBMP_Format,
4125e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kLastKnownFormat = kWBMP_Format
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
4425e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the compressed data's format (see Format enum)
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual Format getFormat() const;
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns true if the decoder should try to dither the resulting image.
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        The default setting is true.
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool getDitherImage() const { return fDitherImage; }
5325e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Set to true if the the decoder should try to dither the resulting image.
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        The default setting is true.
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void setDitherImage(bool dither) { fDitherImage = dither; }
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** \class Peeker
6025e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Base class for optional callbacks to retrieve meta/chunk data out of
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        an image as it is being decoded.
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class Peeker : public SkRefCnt {
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /** Return true to continue decoding, or false to indicate an error, which
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            will cause the decoder to not return the image.
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        virtual bool peek(const char tag[], const void* data, size_t length) = 0;
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Peeker* getPeeker() const { return fPeeker; }
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Peeker* setPeeker(Peeker*);
7425e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** \class Peeker
7625e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Base class for optional callbacks to retrieve meta/chunk data out of
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        an image as it is being decoded.
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class Chooser : public SkRefCnt {
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        virtual void begin(int count) {}
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        virtual void inspect(int index, SkBitmap::Config config, int width, int height) {}
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /** Return the index of the subimage you want, or -1 to choose none of them.
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        virtual int choose() = 0;
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Chooser* getChooser() const { return fChooser; }
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Chooser* setChooser(Chooser*);
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
923f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    /** This optional table describes the caller's preferred config based on
933f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        information about the src data. For this table, the src attributes are
943f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        described in terms of depth (index (8), 16, 32/24) and if there is
953f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        per-pixel alpha. These inputs combine to create an index into the
963f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        pref[] table, which contains the caller's preferred config for that
973f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        input, or kNo_Config if there is no preference.
983f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com
993f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        To specify no preferrence, call setPrefConfigTable(NULL), which is
1003f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        the default.
1013f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com
1023f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        Note, it is still at the discretion of the codec as to what output
1033f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        config is actually returned, as it may not be able to support the
1043f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        caller's preference.
1053f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com
1063f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        Here is how the index into the table is computed from the src:
1073f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            depth [8, 16, 32/24] -> 0, 2, 4
1083f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            alpha [no, yes] -> 0, 1
1093f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        The two index values are OR'd together.
1103f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            src: 8-index, no-alpha  -> 0
1113f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            src: 8-index, yes-alpha -> 1
1123f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            src: 16bit,   no-alpha  -> 2    // e.g. 565
1133f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            src: 16bit,   yes-alpha -> 3    // e.g. 1555
1143f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            src: 32/24,   no-alpha  -> 4
1153f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com            src: 32/24,   yes-alpha -> 5
1163f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com     */
1173f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    void setPrefConfigTable(const SkBitmap::Config pref[6]);
1183f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap::Allocator* getAllocator() const { return fAllocator; }
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*);
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // sample-size, if set to > 1, tells the decoder to return a smaller than
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // original bitmap, sampling 1 pixel for every size pixels. e.g. if sample
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // size is set to 3, then the returned bitmap will be 1/3 as wide and high,
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // and will contain 1/9 as many pixels as the original.
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // Note: this is a hint, and the codec may choose to ignore this, or only
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // approximate the sample size.
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int getSampleSize() const { return fSampleSize; }
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void setSampleSize(int size);
13025e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Reset the sampleSize to its default of 1
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void resetSampleSize() { this->setSampleSize(1); }
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Decoding is synchronous, but for long decodes, a different thread can
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        call this method safely. This sets a state that the decoders will
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        periodically check, and if they see it changed to cancel, they will
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        cancel. This will result in decode() returning false. However, there is
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        no guarantee that the decoder will see the state change in time, so
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        it is possible that cancelDecode() will be called, but will be ignored
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and decode() will return true (assuming no other problems were
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        encountered).
14325e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        This state is automatically reset at the beginning of decode().
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void cancelDecode() {
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // now the subclass must query shouldCancelDecode() to be informed
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // of the request
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fShouldCancelDecode = true;
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Passed to the decode method. If kDecodeBounds_Mode is passed, then
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        only the bitmap's width/height/config need be set. If kDecodePixels_Mode
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        is passed, then the bitmap must have pixels or a pixelRef.
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Mode {
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kDecodeBounds_Mode, //!< only return width/height/config in bitmap
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kDecodePixels_Mode  //!< return entire bitmap (including pixels)
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
16025e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Given a stream, decode it into the specified bitmap.
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If the decoder can decompress the image, it calls bitmap.setConfig(),
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and then if the Mode is kDecodePixels_Mode, call allocPixelRef(),
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        which will allocated a pixelRef. To access the pixel memory, the codec
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        needs to call lockPixels/unlockPixels on the
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bitmap. It can then set the pixels with the decompressed image.
16725e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com    *   If the image cannot be decompressed, return false. After the
16825e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com    *   decoding, the function converts the decoded config in bitmap
16925e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com    *   to pref if possible. Whether a conversion is feasible is
17025e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com    *   tested by Bitmap::canCopyTo(pref).
17125e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
17225e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com        note: document use of Allocator, Peeker and Chooser
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode);
1753f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) {
1763f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode);
1773f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    }
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Given a stream, this will try to find an appropriate decoder object.
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If none is found, the method returns NULL.
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkImageDecoder* Factory(SkStream*);
183a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Decode the image stored in the specified file, and store the result
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in bitmap. Return true for success or false on failure.
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If pref is kNo_Config, then the decoder is free to choose the most natural
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        config given the image data. If pref something other than kNo_Config,
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the decoder will attempt to decode the image into that format, unless
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        there is a conflict (e.g. the image has per-pixel alpha and the bitmap's
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        config does not support that), in which case the decoder will choose a
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        closest match configuration.
19331d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com
194b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com        @param format On success, if format is non-null, it is set to the format
195b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                      of the decoded file. On failure it is ignored.
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool DecodeFile(const char file[], SkBitmap* bitmap,
198b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                           SkBitmap::Config prefConfig, Mode,
19931d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com                           Format* format = NULL);
200b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com    static bool DecodeFile(const char file[], SkBitmap* bitmap) {
201b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com        return DecodeFile(file, bitmap, SkBitmap::kNo_Config,
202b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                          kDecodePixels_Mode, NULL);
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Decode the image stored in the specified memory buffer, and store the
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        result in bitmap. Return true for success or false on failure.
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If pref is kNo_Config, then the decoder is free to choose the most natural
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        config given the image data. If pref something other than kNo_Config,
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        the decoder will attempt to decode the image into that format, unless
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        there is a conflict (e.g. the image has per-pixel alpha and the bitmap's
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        config does not support that), in which case the decoder will choose a
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        closest match configuration.
21331d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com
21431d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com        @param format On success, if format is non-null, it is set to the format
215b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                       of the decoded buffer. On failure it is ignored.
216b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com     */
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap,
218b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                             SkBitmap::Config prefConfig, Mode,
21931d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com                             Format* format = NULL);
220b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com    static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config,
222b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                            kDecodePixels_Mode, NULL);
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Decode the image stored in the specified SkStream, and store the result
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        in bitmap. Return true for success or false on failure.
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        If pref is kNo_Config, then the decoder is free to choose the most
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        natural config given the image data. If pref something other than
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kNo_Config, the decoder will attempt to decode the image into that
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        format, unless there is a conflict (e.g. the image has per-pixel alpha
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        and the bitmap's config does not support that), in which case the
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        decoder will choose a closest match configuration.
23331d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com
234b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com        @param format On success, if format is non-null, it is set to the format
235b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                      of the decoded stream. On failure it is ignored.
236b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com     */
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool DecodeStream(SkStream* stream, SkBitmap* bitmap,
238b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                             SkBitmap::Config prefConfig, Mode,
23931d1c64bd54dd2b216846a30da4cc2e96765ec5freed@android.com                             Format* format = NULL);
240b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com    static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) {
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return DecodeStream(stream, bitmap, SkBitmap::kNo_Config,
242b3ade9d1b0a63f8f0dc3bee5785e930c8e84311dreed@android.com                            kDecodePixels_Mode, NULL);
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24425e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Return the default config for the running device.
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Currently this used as a suggestion to image decoders that need to guess
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        what config they should decode into.
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Default is kNo_Config, but this can be changed with SetDeviceConfig()
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkBitmap::Config GetDeviceConfig();
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Set the default config for the running device.
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Currently this used as a suggestion to image decoders that need to guess
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        what config they should decode into.
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Default is kNo_Config.
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        This can be queried with GetDeviceConfig()
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void SetDeviceConfig(SkBitmap::Config);
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /** @cond UNIT_TEST */
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDEBUGCODE(static void UnitTest();)
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  /** @endcond */
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // must be overridden in subclasses. This guy is called by decode(...)
2653f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0;
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Can be queried from within onDecode, to see if the user (possibly in
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        a different thread) has requested the decode to cancel. If this returns
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        true, your onDecode() should stop and return false.
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Each subclass needs to decide how often it can query this, to balance
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        responsiveness with performance.
27225e9834c03a050afbf339f457b8c401aecb26c0bweita@google.com
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Calling this outside of onDecode() may return undefined values.
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool shouldCancelDecode() const { return fShouldCancelDecode; }
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
27925e9834c03a050afbf339f457b8c401aecb26c0bweita@google.comprotected:
2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkImageDecoder();
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // helper function for decoders to handle the (common) case where there is only
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // once choice available in the image file.
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) const;
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /*  Helper for subclasses. Call this to allocate the pixel memory given the bitmap's
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        width/height/rowbytes/config. Returns true on success. This method handles checking
2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for an optional Allocator.
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool allocPixelRef(SkBitmap*, SkColorTable*) const;
2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2923f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    enum SrcDepth {
2933f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        kIndex_SrcDepth,
2943f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        k16Bit_SrcDepth,
2953f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        k32Bit_SrcDepth
2963f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    };
2973f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    /** The subclass, inside onDecode(), calls this to determine the config of
2983f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the
2993f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        src image. This routine returns the caller's preference given
3003f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        srcDepth and hasAlpha, or kNo_Config if there is no preference.
3013f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com
3023f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        Note: this also takes into account GetDeviceConfig(), so the subclass
3033f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com        need not call that.
3043f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com     */
3053f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    SkBitmap::Config getPrefConfig(SrcDepth, bool hasAlpha) const;
3063f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Peeker*                 fPeeker;
3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Chooser*                fChooser;
3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkBitmap::Allocator*    fAllocator;
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int                     fSampleSize;
3123f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    SkBitmap::Config        fDefaultPref;   // use if fUsePrefTable is false
3133f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    SkBitmap::Config        fPrefTable[6];  // use if fUsePrefTable is true
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool                    fDitherImage;
3153f1f06a26bdb2022a5c72f93ae623a57b6659464reed@android.com    bool                    fUsePrefTable;
3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    mutable bool            fShouldCancelDecode;
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // illegal
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkImageDecoder(const SkImageDecoder&);
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkImageDecoder& operator=(const SkImageDecoder&);
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
323a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com/** Calling newDecoder with a stream returns a new matching imagedecoder
324a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    instance, or NULL if none can be found. The caller must manage its ownership
325a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    of the stream as usual, calling unref() when it is done, as the returned
326a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    decoder may have called ref() (and if so, the decoder is responsible for
327a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    balancing its ownership when it is destroyed).
328a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com */
329a14ea0e930c82daa2364ece4bd0b06256272302areed@android.comclass SkImageDecoderFactory : public SkRefCnt {
330a14ea0e930c82daa2364ece4bd0b06256272302areed@android.compublic:
331a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    virtual SkImageDecoder* newDecoder(SkStream*) = 0;
332a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com};
333a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com
334a14ea0e930c82daa2364ece4bd0b06256272302areed@android.comclass SkDefaultImageDecoderFactory : SkImageDecoderFactory {
335a14ea0e930c82daa2364ece4bd0b06256272302areed@android.compublic:
336a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    // calls SkImageDecoder::Factory(stream)
337a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    virtual SkImageDecoder* newDecoder(SkStream* stream) {
338a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com        return SkImageDecoder::Factory(stream);
339a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com    }
340a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com};
341a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com
342a14ea0e930c82daa2364ece4bd0b06256272302areed@android.com
3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
344