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);
53 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
54 : m_imageElement(nullptr)
55 , m_cropRect(cropRect)
56 , m_bitmapOffset(IntPoint())
60 if (video->webMediaPlayer())
61 playerSize = video->webMediaPlayer()->naturalSize();
63 IntRect videoRect = IntRect(IntPoint(), playerSize);
64 IntRect srcRect = intersection(cropRect, videoRect);
65 IntRect dstRect(IntPoint(), srcRect.size());
67 OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size());
70 GraphicsContext* c = buf->context();
72 c->translate(-srcRect.x(), -srcRect.y());
73 video->paintCurrentFrameInContext(c, videoRect);
74 m_bitmap = buf->copyImage(DontCopyBackingStore);
75 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
78 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
79 : m_imageElement(nullptr)
80 , m_cropRect(cropRect)
81 , m_bitmapOffset(IntPoint())
83 CanvasRenderingContext* sourceContext = canvas->renderingContext();
84 if (sourceContext && sourceContext->is3d())
85 sourceContext->paintRenderingResultsToCanvas();
87 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()));
88 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
89 m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cropRect);
92 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
93 : m_imageElement(nullptr)
94 , m_cropRect(cropRect)
95 , m_bitmapOffset(IntPoint())
97 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
99 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size());
102 if (srcRect.width() > 0 && srcRect.height() > 0)
103 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
105 m_bitmap = buf->copyImage(DontCopyBackingStore);
106 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
109 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
110 : m_imageElement(bitmap->imageElement())
112 , m_cropRect(cropRect)
113 , m_bitmapOffset(IntPoint())
115 IntRect oldBitmapRect = bitmap->bitmapRect();
116 IntRect srcRect = intersection(cropRect, oldBitmapRect);
117 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x()), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size());
119 if (m_imageElement) {
120 m_imageElement->addClient(this);
121 m_bitmapOffset = srcRect.location();
122 } else if (bitmap->bitmapImage()) {
123 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropRect.y() - oldBitmapRect.y()), cropRect.size());
124 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect);
128 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
129 : m_imageElement(nullptr)
130 , m_cropRect(cropRect)
132 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()));
133 m_bitmap = cropImage(image, cropRect);
134 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
137 ImageBitmap::~ImageBitmap()
141 m_imageElement->removeClient(this);
145 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
147 IntRect normalizedCropRect = normalizeRect(cropRect);
148 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
151 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
153 IntRect normalizedCropRect = normalizeRect(cropRect);
154 return adoptRefWillBeNoop(new ImageBitmap(video, normalizedCropRect));
157 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropRect)
159 IntRect normalizedCropRect = normalizeRect(cropRect);
160 return adoptRefWillBeNoop(new ImageBitmap(canvas, normalizedCropRect));
163 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& cropRect)
165 IntRect normalizedCropRect = normalizeRect(cropRect);
166 return adoptRefWillBeNoop(new ImageBitmap(data, normalizedCropRect));
169 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect)
171 IntRect normalizedCropRect = normalizeRect(cropRect);
172 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect));
175 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRect)
177 IntRect normalizedCropRect = normalizeRect(cropRect);
178 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
181 void ImageBitmap::notifyImageSourceChanged()
183 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
184 m_bitmapOffset = IntPoint();
185 m_imageElement = nullptr;
188 PassRefPtr<Image> ImageBitmap::bitmapImage() const
190 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect.height()) && (!m_imageElement || !m_bitmap));
192 return m_imageElement->cachedImage()->image();
196 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageMode, SourceImageStatus* status) const
198 *status = NormalSourceImageStatus;
199 return bitmapImage();
202 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
204 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect);
205 FloatRect newSrcRect = intersectRect;
206 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location());
207 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location()), m_bitmapRect.size());
208 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(),
209 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitmapRect.height());
210 newDstRect.moveBy(dstRect->location());
211 *srcRect = newSrcRect;
212 *dstRect = newDstRect;
215 FloatSize ImageBitmap::sourceSize() const
217 return FloatSize(width(), height());
220 void ImageBitmap::trace(Visitor* visitor)
222 visitor->trace(m_imageElement);
223 ImageLoaderClient::trace(visitor);