1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "core/frame/ImageBitmap.h"
8 #include "core/html/HTMLCanvasElement.h"
9 #include "core/html/HTMLVideoElement.h"
10 #include "core/html/ImageData.h"
11 #include "core/html/canvas/CanvasRenderingContext.h"
12 #include "platform/graphics/BitmapImage.h"
13 #include "platform/graphics/GraphicsContext.h"
14 #include "platform/graphics/ImageBuffer.h"
15 #include "wtf/RefPtr.h"
19 static inline IntRect normalizeRect(const IntRect& rect)
21 return IntRect(std::min(rect.x(), rect.maxX()),
22 std::min(rect.y(), rect.maxY()),
23 std::max(rect.width(), -rect.width()),
24 std::max(rect.height(), -rect.height()));
27 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect)
29 IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cropRect);
30 if (!intersectRect.width() || !intersectRect.height())
34 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, intersectRect);
35 return BitmapImage::create(NativeImageSkia::create(cropped));
38 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect)
39 : m_imageElement(image)
41 , m_cropRect(cropRect)
43 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image->height()));
44 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
45 m_bitmapOffset = srcRect.location();
47 if (!srcRect.width() || !srcRect.height())
48 m_imageElement = nullptr;
50 m_imageElement->addClient(this);
52 ScriptWrappable::init(this);
55 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
56 : m_imageElement(nullptr)
57 , m_cropRect(cropRect)
58 , m_bitmapOffset(IntPoint())
62 if (video->webMediaPlayer())
63 playerSize = video->webMediaPlayer()->naturalSize();
65 IntRect videoRect = IntRect(IntPoint(), playerSize);
66 IntRect srcRect = intersection(cropRect, videoRect);
67 IntRect dstRect(IntPoint(), srcRect.size());
69 OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size());
72 GraphicsContext* c = buf->context();
74 c->translate(-srcRect.x(), -srcRect.y());
75 video->paintCurrentFrameInContext(c, videoRect);
76 m_bitmap = buf->copyImage(DontCopyBackingStore);
77 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
79 ScriptWrappable::init(this);
82 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
83 : m_imageElement(nullptr)
84 , m_cropRect(cropRect)
85 , m_bitmapOffset(IntPoint())
87 CanvasRenderingContext* sourceContext = canvas->renderingContext();
88 if (sourceContext && sourceContext->is3d())
89 sourceContext->paintRenderingResultsToCanvas();
91 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()));
92 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
93 m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cropRect);
95 ScriptWrappable::init(this);
98 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
99 : m_imageElement(nullptr)
100 , m_cropRect(cropRect)
101 , m_bitmapOffset(IntPoint())
103 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
105 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size());
108 if (srcRect.width() > 0 && srcRect.height() > 0)
109 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
111 m_bitmap = buf->copyImage(DontCopyBackingStore);
112 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
114 ScriptWrappable::init(this);
117 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
118 : m_imageElement(bitmap->imageElement())
120 , m_cropRect(cropRect)
121 , m_bitmapOffset(IntPoint())
123 IntRect oldBitmapRect = bitmap->bitmapRect();
124 IntRect srcRect = intersection(cropRect, oldBitmapRect);
125 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x()), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size());
127 if (m_imageElement) {
128 m_imageElement->addClient(this);
129 m_bitmapOffset = srcRect.location();
130 } else if (bitmap->bitmapImage()) {
131 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropRect.y() - oldBitmapRect.y()), cropRect.size());
132 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect);
135 ScriptWrappable::init(this);
138 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
139 : m_imageElement(nullptr)
140 , m_cropRect(cropRect)
142 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()));
143 m_bitmap = cropImage(image, cropRect);
144 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
146 ScriptWrappable::init(this);
149 ImageBitmap::~ImageBitmap()
153 m_imageElement->removeClient(this);
157 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
159 IntRect normalizedCropRect = normalizeRect(cropRect);
160 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
163 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
165 IntRect normalizedCropRect = normalizeRect(cropRect);
166 return adoptRefWillBeNoop(new ImageBitmap(video, normalizedCropRect));
169 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropRect)
171 IntRect normalizedCropRect = normalizeRect(cropRect);
172 return adoptRefWillBeNoop(new ImageBitmap(canvas, normalizedCropRect));
175 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& cropRect)
177 IntRect normalizedCropRect = normalizeRect(cropRect);
178 return adoptRefWillBeNoop(new ImageBitmap(data, normalizedCropRect));
181 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect)
183 IntRect normalizedCropRect = normalizeRect(cropRect);
184 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect));
187 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRect)
189 IntRect normalizedCropRect = normalizeRect(cropRect);
190 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
193 void ImageBitmap::notifyImageSourceChanged()
195 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
196 m_bitmapOffset = IntPoint();
197 m_imageElement = nullptr;
200 PassRefPtr<Image> ImageBitmap::bitmapImage() const
202 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect.height()) && (!m_imageElement || !m_bitmap));
204 return m_imageElement->cachedImage()->image();
208 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageMode, SourceImageStatus* status) const
210 *status = NormalSourceImageStatus;
211 return bitmapImage();
214 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
216 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect);
217 FloatRect newSrcRect = intersectRect;
218 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location());
219 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location()), m_bitmapRect.size());
220 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(),
221 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitmapRect.height());
222 newDstRect.moveBy(dstRect->location());
223 *srcRect = newSrcRect;
224 *dstRect = newDstRect;
227 FloatSize ImageBitmap::sourceSize() const
229 return FloatSize(width(), height());
232 void ImageBitmap::trace(Visitor* visitor)
234 visitor->trace(m_imageElement);
235 ImageLoaderClient::trace(visitor);