Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: third_party/WebKit/Source/core/frame/ImageBitmap.cpp

Issue 1631733003: Implementing ImageBitmap option imageOrientation of flipY (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: support flipY and none at this moment Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "core/frame/ImageBitmap.h" 5 #include "core/frame/ImageBitmap.h"
6 6
7 #include "core/html/HTMLCanvasElement.h" 7 #include "core/html/HTMLCanvasElement.h"
8 #include "core/html/HTMLVideoElement.h" 8 #include "core/html/HTMLVideoElement.h"
9 #include "core/html/ImageData.h" 9 #include "core/html/ImageData.h"
10 #include "third_party/skia/include/core/SkSurface.h" 10 #include "third_party/skia/include/core/SkSurface.h"
11 #include "wtf/RefPtr.h" 11 #include "wtf/RefPtr.h"
12 12
13 namespace blink { 13 namespace blink {
14 14
15 static inline IntRect normalizeRect(const IntRect& rect) 15 static inline IntRect normalizeRect(const IntRect& rect)
16 { 16 {
17 return IntRect(std::min(rect.x(), rect.maxX()), 17 return IntRect(std::min(rect.x(), rect.maxX()),
18 std::min(rect.y(), rect.maxY()), 18 std::min(rect.y(), rect.maxY()),
19 std::max(rect.width(), -rect.width()), 19 std::max(rect.width(), -rect.width()),
20 std::max(rect.height(), -rect.height())); 20 std::max(rect.height(), -rect.height()));
21 } 21 }
22 22
23 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect) 23 // TODO(xidachen): this function needs to be changed later on when implementing premultiplyAlpha option
24 static SkImage* flipSkImageVertically(SkImage* input)
25 {
26 int width = input->width();
27 int height = input->height();
28 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
29 OwnPtr<uint8_t[]> imagePixels = adoptArrayPtr(new uint8_t[width * height * i nfo.bytesPerPixel()]);
30 int imageRowBytes = info.bytesPerPixel() * width;
31 input->readPixels(info, imagePixels.get(), imageRowBytes, 0, 0);
32
33 for (int i = 0; i < height / 2; i++) {
34 int topFirstElement = i * imageRowBytes;
35 int topLastElement = (i + 1) * imageRowBytes;
36 int bottomFirstElement = (height - 1 - i) * imageRowBytes;
37 std::swap_ranges(imagePixels.get() + topFirstElement, imagePixels.get() + topLastElement, imagePixels.get() + bottomFirstElement);
38 }
39 return SkImage::NewRasterCopy(info, imagePixels.get(), imageRowBytes);
40 }
41
42 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect, bool flipYEnabled)
24 { 43 {
25 ASSERT(image); 44 ASSERT(image);
26 45
27 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); 46 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height()));
28 const IntRect srcRect = intersection(imgRect, cropRect); 47 const IntRect srcRect = intersection(imgRect, cropRect);
29 48
30 if (cropRect == srcRect) 49 if (cropRect == srcRect) {
50 if (flipYEnabled)
51 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(imag e->imageForCurrentFrame()->newSubset(srcRect))));
31 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()- >newSubset(srcRect))); 52 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()- >newSubset(srcRect)));
53 }
32 54
33 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height())); 55 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height()));
34 56
35 if (srcRect.isEmpty()) 57 if (srcRect.isEmpty())
36 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); 58 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot()));
37 59
38 SkScalar dstLeft = std::min(0, -cropRect.x()); 60 SkScalar dstLeft = std::min(0, -cropRect.x());
39 SkScalar dstTop = std::min(0, -cropRect.y()); 61 SkScalar dstTop = std::min(0, -cropRect.y());
40 if (cropRect.x() < 0) 62 if (cropRect.x() < 0)
41 dstLeft = -cropRect.x(); 63 dstLeft = -cropRect.x();
42 if (cropRect.y() < 0) 64 if (cropRect.y() < 0)
43 dstTop = -cropRect.y(); 65 dstTop = -cropRect.y();
44 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft , dstTop); 66 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft , dstTop);
67 if (flipYEnabled)
68 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(surface- >newImageSnapshot())));
45 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); 69 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot()));
46 } 70 }
47 71
48 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) 72 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options)
49 { 73 {
50 m_image = cropImage(image->cachedImage()->image(), cropRect); 74 if (options.imageOrientation() == "flipY")
Justin Novosad 2016/02/03 14:32:52 Put this string value in a constant.
xidachen 2016/02/03 15:29:51 Thanks for pointing this out. It is now defined as
75 m_image = cropImage(image->cachedImage()->image(), cropRect, true);
76 else
77 m_image = cropImage(image->cachedImage()->image(), cropRect, false);
51 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) ); 78 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) );
52 } 79 }
53 80
54 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) 81 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options)
55 { 82 {
56 IntSize playerSize; 83 IntSize playerSize;
57 if (video->webMediaPlayer()) 84 if (video->webMediaPlayer())
58 playerSize = video->webMediaPlayer()->naturalSize(); 85 playerSize = video->webMediaPlayer()->naturalSize();
59 86
60 IntRect videoRect = IntRect(IntPoint(), playerSize); 87 IntRect videoRect = IntRect(IntPoint(), playerSize);
61 IntRect srcRect = intersection(cropRect, videoRect); 88 IntRect srcRect = intersection(cropRect, videoRect);
62 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); 89 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels);
63 if (!buffer) 90 if (!buffer)
64 return; 91 return;
65 92
66 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); 93 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y()));
67 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); 94 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr);
68 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); 95 if (options.imageOrientation() == "flipY")
96 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get())));
97 else
98 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration));
69 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) ); 99 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) );
70 } 100 }
71 101
72 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options) 102 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options)
73 { 103 {
74 ASSERT(canvas->isPaintable()); 104 ASSERT(canvas->isPaintable());
75 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect); 105 if (options.imageOrientation() == "flipY")
106 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration). get(), cropRect, true);
107 else
108 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration). get(), cropRect, false);
76 m_image->setOriginClean(canvas->originClean()); 109 m_image->setOriginClean(canvas->originClean());
77 } 110 }
78 111
79 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) 112 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options)
80 { 113 {
81 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 114 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
82 115
83 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); 116 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels);
84 if (!buffer) 117 if (!buffer)
85 return; 118 return;
86 119
87 if (srcRect.isEmpty()) { 120 if (srcRect.isEmpty()) {
88 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); 121 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration));
89 return; 122 return;
90 } 123 }
91 124
92 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y())); 125 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y()));
93 if (cropRect.x() < 0) 126 if (cropRect.x() < 0)
94 dstPoint.setX(-cropRect.x()); 127 dstPoint.setX(-cropRect.x());
95 if (cropRect.y() < 0) 128 if (cropRect.y() < 0)
96 dstPoint.setY(-cropRect.y()); 129 dstPoint.setY(-cropRect.y());
97 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); 130 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint);
98 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); 131 if (options.imageOrientation() == "flipY")
132 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get())));
133 else
134 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration));
99 } 135 }
100 136
101 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options) 137 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options)
102 { 138 {
103 m_image = cropImage(bitmap->bitmapImage(), cropRect); 139 if (options.imageOrientation() == "flipY")
140 m_image = cropImage(bitmap->bitmapImage(), cropRect, true);
141 else
142 m_image = cropImage(bitmap->bitmapImage(), cropRect, false);
104 m_image->setOriginClean(bitmap->originClean()); 143 m_image->setOriginClean(bitmap->originClean());
105 } 144 }
106 145
107 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options) 146 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options)
108 { 147 {
109 m_image = cropImage(image.get(), cropRect); 148 if (options.imageOrientation() == "flipY")
149 m_image = cropImage(image.get(), cropRect, true);
150 else
151 m_image = cropImage(image.get(), cropRect, false);
110 m_image->setOriginClean(image->originClean()); 152 m_image->setOriginClean(image->originClean());
111 } 153 }
112 154
113 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) 155 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image)
114 { 156 {
115 m_image = image; 157 m_image = image;
116 } 158 }
117 159
118 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() 160 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer()
119 { 161 {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 { 269 {
228 return FloatSize(width(), height()); 270 return FloatSize(width(), height());
229 } 271 }
230 272
231 DEFINE_TRACE(ImageBitmap) 273 DEFINE_TRACE(ImageBitmap)
232 { 274 {
233 ImageLoaderClient::trace(visitor); 275 ImageLoaderClient::trace(visitor);
234 } 276 }
235 277
236 } // namespace blink 278 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698