WebKit/chromium/src/WebImageSkia.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2009 Google 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 are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 #include "config.h"
       
    32 #include "WebImage.h"
       
    33 
       
    34 #include "Image.h"
       
    35 #include "ImageSource.h"
       
    36 #include "NativeImageSkia.h"
       
    37 #include "SharedBuffer.h"
       
    38 
       
    39 #include "WebData.h"
       
    40 #include "WebSize.h"
       
    41 
       
    42 #include <wtf/OwnPtr.h>
       
    43 #include <wtf/PassRefPtr.h>
       
    44 
       
    45 using namespace WebCore;
       
    46 
       
    47 namespace WebKit {
       
    48 
       
    49 WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize)
       
    50 {
       
    51     ImageSource source;
       
    52     source.setData(PassRefPtr<SharedBuffer>(data).get(), true);
       
    53     if (!source.isSizeAvailable())
       
    54         return WebImage();
       
    55 
       
    56     // Frames are arranged by decreasing size, then decreasing bit depth.
       
    57     // Pick the frame closest to |desiredSize|'s area without being smaller,
       
    58     // which has the highest bit depth.
       
    59     const size_t frameCount = source.frameCount();
       
    60     size_t index = 0;  // Default to first frame if none are large enough.
       
    61     int frameAreaAtIndex = 0;
       
    62     for (size_t i = 0; i < frameCount; ++i) {
       
    63         const IntSize frameSize = source.frameSizeAtIndex(i);
       
    64         if (WebSize(frameSize) == desiredSize) {
       
    65             index = i;
       
    66             break;  // Perfect match.
       
    67         }
       
    68 
       
    69         const int frameArea = frameSize.width() * frameSize.height();
       
    70         if (frameArea < (desiredSize.width * desiredSize.height))
       
    71             break;  // No more frames that are large enough.
       
    72 
       
    73         if (!i || (frameArea < frameAreaAtIndex)) {
       
    74             index = i;  // Closer to desired area than previous best match.
       
    75             frameAreaAtIndex = frameArea;
       
    76         }
       
    77     }
       
    78 
       
    79     OwnPtr<NativeImageSkia> frame(source.createFrameAtIndex(index));
       
    80     if (!frame.get())
       
    81         return WebImage();
       
    82 
       
    83     return WebImage(*frame);
       
    84 }
       
    85 
       
    86 void WebImage::reset()
       
    87 {
       
    88     m_bitmap.reset();
       
    89 }
       
    90 
       
    91 void WebImage::assign(const WebImage& image)
       
    92 {
       
    93     m_bitmap = image.m_bitmap;
       
    94 }
       
    95 
       
    96 bool WebImage::isNull() const
       
    97 {
       
    98     return m_bitmap.isNull();
       
    99 }
       
   100 
       
   101 WebSize WebImage::size() const
       
   102 {
       
   103     return WebSize(m_bitmap.width(), m_bitmap.height());
       
   104 }
       
   105 
       
   106 WebImage::WebImage(const PassRefPtr<Image>& image)
       
   107 {
       
   108     operator=(image);
       
   109 }
       
   110 
       
   111 WebImage& WebImage::operator=(const PassRefPtr<Image>& image)
       
   112 {
       
   113     NativeImagePtr p;
       
   114     if (image.get() && (p = image->nativeImageForCurrentFrame()))
       
   115         assign(*p);
       
   116     else
       
   117         reset();
       
   118     return *this;
       
   119 }
       
   120 
       
   121 } // namespace WebKit