OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/frame/ImageBitmap.h" | 6 #include "core/frame/ImageBitmap.h" |
7 | 7 |
8 #include "core/html/HTMLCanvasElement.h" | 8 #include "core/html/HTMLCanvasElement.h" |
9 #include "core/html/HTMLVideoElement.h" | 9 #include "core/html/HTMLVideoElement.h" |
10 #include "core/html/ImageData.h" | 10 #include "core/html/ImageData.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 return IntRect(min(rect.x(), rect.maxX()), | 23 return IntRect(min(rect.x(), rect.maxX()), |
24 min(rect.y(), rect.maxY()), | 24 min(rect.y(), rect.maxY()), |
25 max(rect.width(), -rect.width()), | 25 max(rect.width(), -rect.width()), |
26 max(rect.height(), -rect.height())); | 26 max(rect.height(), -rect.height())); |
27 } | 27 } |
28 | 28 |
29 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect) | 29 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect) |
30 { | 30 { |
31 IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cro
pRect); | 31 IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cro
pRect); |
32 if (!intersectRect.width() || !intersectRect.height()) | 32 if (!intersectRect.width() || !intersectRect.height()) |
33 return 0; | 33 return nullptr; |
34 | 34 |
35 SkBitmap cropped; | 35 SkBitmap cropped; |
36 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, inters
ectRect); | 36 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, inters
ectRect); |
37 return BitmapImage::create(NativeImageSkia::create(cropped)); | 37 return BitmapImage::create(NativeImageSkia::create(cropped)); |
38 } | 38 } |
39 | 39 |
40 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | 40 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
41 : m_imageElement(image) | 41 : m_imageElement(image) |
42 , m_bitmap(0) | 42 , m_bitmap(nullptr) |
43 , m_cropRect(cropRect) | 43 , m_cropRect(cropRect) |
44 { | 44 { |
45 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); | 45 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); |
46 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 46 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
47 m_bitmapOffset = srcRect.location(); | 47 m_bitmapOffset = srcRect.location(); |
48 | 48 |
49 if (!srcRect.width() || !srcRect.height()) | 49 if (!srcRect.width() || !srcRect.height()) |
50 m_imageElement = 0; | 50 m_imageElement = nullptr; |
51 else | 51 else |
52 m_imageElement->addClient(this); | 52 m_imageElement->addClient(this); |
53 | 53 |
54 ScriptWrappable::init(this); | 54 ScriptWrappable::init(this); |
55 } | 55 } |
56 | 56 |
57 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 57 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
58 : m_imageElement(0) | 58 : m_imageElement(nullptr) |
59 , m_cropRect(cropRect) | 59 , m_cropRect(cropRect) |
60 , m_bitmapOffset(IntPoint()) | 60 , m_bitmapOffset(IntPoint()) |
61 { | 61 { |
62 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); | 62 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); |
63 IntRect srcRect = intersection(cropRect, videoRect); | 63 IntRect srcRect = intersection(cropRect, videoRect); |
64 IntRect dstRect(IntPoint(), srcRect.size()); | 64 IntRect dstRect(IntPoint(), srcRect.size()); |
65 | 65 |
66 OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size()); | 66 OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size()); |
67 if (!buf) | 67 if (!buf) |
68 return; | 68 return; |
69 GraphicsContext* c = buf->context(); | 69 GraphicsContext* c = buf->context(); |
70 c->clip(dstRect); | 70 c->clip(dstRect); |
71 c->translate(-srcRect.x(), -srcRect.y()); | 71 c->translate(-srcRect.x(), -srcRect.y()); |
72 video->paintCurrentFrameInContext(c, videoRect); | 72 video->paintCurrentFrameInContext(c, videoRect); |
73 m_bitmap = buf->copyImage(DontCopyBackingStore); | 73 m_bitmap = buf->copyImage(DontCopyBackingStore); |
74 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 74 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
75 | 75 |
76 ScriptWrappable::init(this); | 76 ScriptWrappable::init(this); |
77 } | 77 } |
78 | 78 |
79 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 79 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
80 : m_imageElement(0) | 80 : m_imageElement(nullptr) |
81 , m_cropRect(cropRect) | 81 , m_cropRect(cropRect) |
82 , m_bitmapOffset(IntPoint()) | 82 , m_bitmapOffset(IntPoint()) |
83 { | 83 { |
84 CanvasRenderingContext* sourceContext = canvas->renderingContext(); | 84 CanvasRenderingContext* sourceContext = canvas->renderingContext(); |
85 if (sourceContext && sourceContext->is3d()) | 85 if (sourceContext && sourceContext->is3d()) |
86 sourceContext->paintRenderingResultsToCanvas(); | 86 sourceContext->paintRenderingResultsToCanvas(); |
87 | 87 |
88 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size())
); | 88 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size())
); |
89 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 89 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
90 m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cr
opRect); | 90 m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cr
opRect); |
91 | 91 |
92 ScriptWrappable::init(this); | 92 ScriptWrappable::init(this); |
93 } | 93 } |
94 | 94 |
95 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 95 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
96 : m_imageElement(0) | 96 : m_imageElement(nullptr) |
97 , m_cropRect(cropRect) | 97 , m_cropRect(cropRect) |
98 , m_bitmapOffset(IntPoint()) | 98 , m_bitmapOffset(IntPoint()) |
99 { | 99 { |
100 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 100 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
101 | 101 |
102 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size()); | 102 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size()); |
103 if (!buf) | 103 if (!buf) |
104 return; | 104 return; |
105 if (srcRect.width() > 0 && srcRect.height() > 0) | 105 if (srcRect.width() > 0 && srcRect.height() > 0) |
106 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, In
tPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); | 106 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, In
tPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); |
107 | 107 |
108 m_bitmap = buf->copyImage(DontCopyBackingStore); | 108 m_bitmap = buf->copyImage(DontCopyBackingStore); |
109 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 109 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
110 | 110 |
111 ScriptWrappable::init(this); | 111 ScriptWrappable::init(this); |
112 } | 112 } |
113 | 113 |
114 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 114 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
115 : m_imageElement(bitmap->imageElement()) | 115 : m_imageElement(bitmap->imageElement()) |
116 , m_bitmap(0) | 116 , m_bitmap(nullptr) |
117 , m_cropRect(cropRect) | 117 , m_cropRect(cropRect) |
118 , m_bitmapOffset(IntPoint()) | 118 , m_bitmapOffset(IntPoint()) |
119 { | 119 { |
120 IntRect oldBitmapRect = bitmap->bitmapRect(); | 120 IntRect oldBitmapRect = bitmap->bitmapRect(); |
121 IntRect srcRect = intersection(cropRect, oldBitmapRect); | 121 IntRect srcRect = intersection(cropRect, oldBitmapRect); |
122 m_bitmapRect = IntRect(IntPoint(max(0, oldBitmapRect.x() - cropRect.x()), ma
x(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); | 122 m_bitmapRect = IntRect(IntPoint(max(0, oldBitmapRect.x() - cropRect.x()), ma
x(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); |
123 | 123 |
124 if (m_imageElement) { | 124 if (m_imageElement) { |
125 m_imageElement->addClient(this); | 125 m_imageElement->addClient(this); |
126 m_bitmapOffset = srcRect.location(); | 126 m_bitmapOffset = srcRect.location(); |
127 } else if (bitmap->bitmapImage()) { | 127 } else if (bitmap->bitmapImage()) { |
128 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR
ect.y() - oldBitmapRect.y()), cropRect.size()); | 128 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR
ect.y() - oldBitmapRect.y()), cropRect.size()); |
129 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); | 129 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); |
130 } | 130 } |
131 | 131 |
132 ScriptWrappable::init(this); | 132 ScriptWrappable::init(this); |
133 } | 133 } |
134 | 134 |
135 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | 135 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) |
136 : m_imageElement(0) | 136 : m_imageElement(nullptr) |
137 , m_cropRect(cropRect) | 137 , m_cropRect(cropRect) |
138 { | 138 { |
139 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()))
; | 139 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()))
; |
140 m_bitmap = cropImage(image, cropRect); | 140 m_bitmap = cropImage(image, cropRect); |
141 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 141 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
142 | 142 |
143 ScriptWrappable::init(this); | 143 ScriptWrappable::init(this); |
144 } | 144 } |
145 | 145 |
146 ImageBitmap::~ImageBitmap() | 146 ImageBitmap::~ImageBitmap() |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 PassRefPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRec
t) | 182 PassRefPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRec
t) |
183 { | 183 { |
184 IntRect normalizedCropRect = normalizeRect(cropRect); | 184 IntRect normalizedCropRect = normalizeRect(cropRect); |
185 return adoptRef(new ImageBitmap(image, normalizedCropRect)); | 185 return adoptRef(new ImageBitmap(image, normalizedCropRect)); |
186 } | 186 } |
187 | 187 |
188 void ImageBitmap::notifyImageSourceChanged() | 188 void ImageBitmap::notifyImageSourceChanged() |
189 { | 189 { |
190 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | 190 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); |
191 m_bitmapOffset = IntPoint(); | 191 m_bitmapOffset = IntPoint(); |
192 m_imageElement = 0; | 192 m_imageElement = nullptr; |
193 } | 193 } |
194 | 194 |
195 PassRefPtr<Image> ImageBitmap::bitmapImage() const | 195 PassRefPtr<Image> ImageBitmap::bitmapImage() const |
196 { | 196 { |
197 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect
.height()) && (!m_imageElement || !m_bitmap)); | 197 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect
.height()) && (!m_imageElement || !m_bitmap)); |
198 if (m_imageElement) | 198 if (m_imageElement) |
199 return m_imageElement->cachedImage()->image(); | 199 return m_imageElement->cachedImage()->image(); |
200 return m_bitmap; | 200 return m_bitmap; |
201 } | 201 } |
202 | 202 |
203 } | 203 } |
OLD | NEW |