Home | History | Annotate | Download | only in graphics
      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 #include "wtf/Vector.h"
     35 
     36 namespace WebCore {
     37 
     38 class DeferredImageDecoder;
     39 class ImageOrientation;
     40 class IntPoint;
     41 class IntSize;
     42 class NativeImageSkia;
     43 class SharedBuffer;
     44 
     45 // Right now GIFs are the only recognized image format that supports animation.
     46 // The animation system and the constants below are designed with this in mind.
     47 // GIFs have an optional 16-bit unsigned loop count that describes how an
     48 // animated GIF should be cycled.  If the loop count is absent, the animation
     49 // cycles once; if it is 0, the animation cycles infinitely; otherwise the
     50 // animation plays n + 1 cycles (where n is the specified loop count).  If the
     51 // GIF decoder defaults to cAnimationLoopOnce in the absence of any loop count
     52 // and translates an explicit "0" loop count to cAnimationLoopInfinite, then we
     53 // get a couple of nice side effects:
     54 //   * By making cAnimationLoopOnce be 0, we allow the animation cycling code in
     55 //     BitmapImage.cpp to avoid special-casing it, and simply treat all
     56 //     non-negative loop counts identically.
     57 //   * By making the other two constants negative, we avoid conflicts with any
     58 //     real loop count values.
     59 const int cAnimationLoopOnce = 0;
     60 const int cAnimationLoopInfinite = -1;
     61 const int cAnimationNone = -2;
     62 
     63 class PLATFORM_EXPORT ImageSource {
     64     WTF_MAKE_NONCOPYABLE(ImageSource);
     65 public:
     66     enum AlphaOption {
     67         AlphaPremultiplied,
     68         AlphaNotPremultiplied
     69     };
     70 
     71     enum GammaAndColorProfileOption {
     72         GammaAndColorProfileApplied,
     73         GammaAndColorProfileIgnored
     74     };
     75 
     76     ImageSource(AlphaOption alphaOption = AlphaPremultiplied, GammaAndColorProfileOption gammaAndColorProfileOption = GammaAndColorProfileApplied);
     77     ~ImageSource();
     78 
     79     // Tells the ImageSource that the Image no longer cares about decoded frame
     80     // data except for the specified frame. Callers may pass WTF::kNotFound to
     81     // clear all frames.
     82     //
     83     // In response, the ImageSource should delete cached decoded data for other
     84     // frames where possible to keep memory use low. The expectation is that in
     85     // the future, the caller may call createFrameAtIndex() with an index larger
     86     // than the one passed to this function, and the implementation may then
     87     // make use of the preserved frame data here in decoding that frame.
     88     // By contrast, callers who call this function and then later ask for an
     89     // earlier frame may require more work to be done, e.g. redecoding the image
     90     // from the beginning.
     91     //
     92     // Implementations may elect to preserve more frames than the one requested
     93     // here if doing so is likely to save CPU time in the future, but will pay
     94     // an increased memory cost to do so.
     95     //
     96     // Returns the number of bytes of frame data actually cleared.
     97     size_t clearCacheExceptFrame(size_t);
     98 
     99     bool initialized() const;
    100 
    101     void setData(SharedBuffer* data, bool allDataReceived);
    102     String filenameExtension() const;
    103 
    104     bool isSizeAvailable();
    105     IntSize size(RespectImageOrientationEnum = DoNotRespectImageOrientation) const;
    106     IntSize frameSizeAtIndex(size_t, RespectImageOrientationEnum = DoNotRespectImageOrientation) const;
    107 
    108     bool getHotSpot(IntPoint&) const;
    109 
    110     int repetitionCount();
    111 
    112     size_t frameCount() const;
    113 
    114     PassRefPtr<NativeImageSkia> createFrameAtIndex(size_t);
    115 
    116     float frameDurationAtIndex(size_t) const;
    117     bool frameHasAlphaAtIndex(size_t) const; // Whether or not the frame actually used any alpha.
    118     bool frameIsCompleteAtIndex(size_t) const; // Whether or not the frame is fully received.
    119     ImageOrientation orientationAtIndex(size_t) const; // EXIF image orientation
    120 
    121     // Return the number of bytes in the decoded frame. If the frame is not yet
    122     // decoded then return 0.
    123     unsigned frameBytesAtIndex(size_t) const;
    124 
    125 private:
    126     OwnPtr<DeferredImageDecoder> m_decoder;
    127 
    128     AlphaOption m_alphaOption;
    129     GammaAndColorProfileOption m_gammaAndColorProfileOption;
    130 };
    131 
    132 }
    133 
    134 #endif
    135