This source file includes following definitions.
- fromData
- framesFromData
- reset
- assign
- isNull
- size
#include "config.h"
#include "public/platform/WebImage.h"
#include "platform/SharedBuffer.h"
#include "platform/graphics/Image.h"
#include "platform/graphics/skia/NativeImageSkia.h"
#include "platform/image-decoders/ImageDecoder.h"
#include "public/platform/WebData.h"
#include "public/platform/WebSize.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/PassRefPtr.h"
#include "wtf/Vector.h"
#include <algorithm>
using namespace WebCore;
namespace blink {
WebImage WebImage::fromData(const WebData& data, const WebSize& desiredSize)
{
RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
if (!decoder)
return WebImage();
decoder->setData(buffer.get(), true);
if (!decoder->isSizeAvailable())
return WebImage();
const size_t frameCount = decoder->frameCount();
size_t index = 0;
int frameAreaAtIndex = 0;
for (size_t i = 0; i < frameCount; ++i) {
const IntSize frameSize = decoder->frameSizeAtIndex(i);
if (WebSize(frameSize) == desiredSize) {
index = i;
break;
}
const int frameArea = frameSize.width() * frameSize.height();
if (frameArea < (desiredSize.width * desiredSize.height))
break;
if (!i || (frameArea < frameAreaAtIndex)) {
index = i;
frameAreaAtIndex = frameArea;
}
}
ImageFrame* frame = decoder->frameBufferAtIndex(index);
if (!frame)
return WebImage();
RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
if (!image)
return WebImage();
return WebImage(image->bitmap());
}
WebVector<WebImage> WebImage::framesFromData(const WebData& data)
{
const size_t maxFrameCount = 8;
RefPtr<SharedBuffer> buffer = PassRefPtr<SharedBuffer>(data);
OwnPtr<ImageDecoder> decoder(ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied, ImageSource::GammaAndColorProfileIgnored));
if (!decoder)
return WebVector<WebImage>();
decoder->setData(buffer.get(), true);
if (!decoder->isSizeAvailable())
return WebVector<WebImage>();
const size_t frameCount = decoder->frameCount();
IntSize lastSize;
Vector<WebImage> frames;
for (size_t i = 0; i < std::min(frameCount, maxFrameCount); ++i) {
const IntSize frameSize = decoder->frameSizeAtIndex(i);
if (frameSize == lastSize)
continue;
lastSize = frameSize;
ImageFrame* frame = decoder->frameBufferAtIndex(i);
if (!frame)
continue;
RefPtr<NativeImageSkia> image = frame->asNewNativeImage();
if (image && image->isDataComplete())
frames.append(WebImage(image->bitmap()));
}
return frames;
}
void WebImage::reset()
{
m_bitmap.reset();
}
void WebImage::assign(const WebImage& image)
{
m_bitmap = image.m_bitmap;
}
bool WebImage::isNull() const
{
return m_bitmap.isNull();
}
WebSize WebImage::size() const
{
return WebSize(m_bitmap.width(), m_bitmap.height());
}
WebImage::WebImage(const PassRefPtr<Image>& image)
{
operator=(image);
}
WebImage& WebImage::operator=(const PassRefPtr<Image>& image)
{
RefPtr<NativeImageSkia> p;
if (image && (p = image->nativeImageForCurrentFrame()))
assign(p->bitmap());
else
reset();
return *this;
}
}