1/*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef ImageSource_h
27#define ImageSource_h
28
29#include "platform/PlatformExport.h"
30#include "platform/graphics/ImageOrientation.h"
31#include "wtf/Forward.h"
32#include "wtf/Noncopyable.h"
33#include "wtf/OwnPtr.h"
34
35namespace blink {
36
37class DeferredImageDecoder;
38class ImageOrientation;
39class IntPoint;
40class IntSize;
41class NativeImageSkia;
42class SharedBuffer;
43
44// GIF and WebP support animation. The explanation below is in terms of GIF,
45// but the same constants are used for WebP, too.
46// GIFs have an optional 16-bit unsigned loop count that describes how an
47// animated GIF should be cycled.  If the loop count is absent, the animation
48// cycles once; if it is 0, the animation cycles infinitely; otherwise the
49// animation plays n + 1 cycles (where n is the specified loop count).  If the
50// GIF decoder defaults to cAnimationLoopOnce in the absence of any loop count
51// and translates an explicit "0" loop count to cAnimationLoopInfinite, then we
52// get a couple of nice side effects:
53//   * By making cAnimationLoopOnce be 0, we allow the animation cycling code in
54//     BitmapImage.cpp to avoid special-casing it, and simply treat all
55//     non-negative loop counts identically.
56//   * By making the other two constants negative, we avoid conflicts with any
57//     real loop count values.
58const int cAnimationLoopOnce = 0;
59const int cAnimationLoopInfinite = -1;
60const int cAnimationNone = -2;
61
62class PLATFORM_EXPORT ImageSource {
63    WTF_MAKE_NONCOPYABLE(ImageSource);
64public:
65    enum AlphaOption {
66        AlphaPremultiplied,
67        AlphaNotPremultiplied
68    };
69
70    enum GammaAndColorProfileOption {
71        GammaAndColorProfileApplied,
72        GammaAndColorProfileIgnored
73    };
74
75    ImageSource(AlphaOption alphaOption = AlphaPremultiplied, GammaAndColorProfileOption gammaAndColorProfileOption = GammaAndColorProfileApplied);
76    ~ImageSource();
77
78    // Tells the ImageSource that the Image no longer cares about decoded frame
79    // data except for the specified frame. Callers may pass WTF::kNotFound to
80    // clear all frames.
81    //
82    // In response, the ImageSource should delete cached decoded data for other
83    // frames where possible to keep memory use low. The expectation is that in
84    // the future, the caller may call createFrameAtIndex() with an index larger
85    // than the one passed to this function, and the implementation may then
86    // make use of the preserved frame data here in decoding that frame.
87    // By contrast, callers who call this function and then later ask for an
88    // earlier frame may require more work to be done, e.g. redecoding the image
89    // from the beginning.
90    //
91    // Implementations may elect to preserve more frames than the one requested
92    // here if doing so is likely to save CPU time in the future, but will pay
93    // an increased memory cost to do so.
94    //
95    // Returns the number of bytes of frame data actually cleared.
96    size_t clearCacheExceptFrame(size_t);
97
98    bool initialized() const;
99    void resetDecoder();
100
101    void setData(SharedBuffer& data, bool allDataReceived);
102    String filenameExtension() const;
103
104    bool isSizeAvailable();
105    bool hasColorProfile() const;
106    IntSize size(RespectImageOrientationEnum = DoNotRespectImageOrientation) const;
107    IntSize frameSizeAtIndex(size_t, RespectImageOrientationEnum = DoNotRespectImageOrientation) const;
108
109    bool getHotSpot(IntPoint&) const;
110
111    // Returns one of the cAnimationXXX constants at the top of the file, or
112    // a loop count. In the latter case, the actual number of times the animation
113    // cycles is one more than the loop count. See comment atop the file.
114    int repetitionCount();
115
116    size_t frameCount() const;
117
118    PassRefPtr<NativeImageSkia> createFrameAtIndex(size_t);
119
120    float frameDurationAtIndex(size_t) const;
121    bool frameHasAlphaAtIndex(size_t) const; // Whether or not the frame actually used any alpha.
122    bool frameIsCompleteAtIndex(size_t) const; // Whether or not the frame is fully received.
123    ImageOrientation orientationAtIndex(size_t) const; // EXIF image orientation
124
125    // Return the number of bytes in the decoded frame. If the frame is not yet
126    // decoded then return 0.
127    unsigned frameBytesAtIndex(size_t) const;
128
129private:
130    OwnPtr<DeferredImageDecoder> m_decoder;
131
132    AlphaOption m_alphaOption;
133    GammaAndColorProfileOption m_gammaAndColorProfileOption;
134};
135
136} // namespace blink
137
138#endif
139