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

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: replace "flipY" by a static const char* 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 const char* imageOrientationFlipY = "flipY";
16
15 static inline IntRect normalizeRect(const IntRect& rect) 17 static inline IntRect normalizeRect(const IntRect& rect)
16 { 18 {
17 return IntRect(std::min(rect.x(), rect.maxX()), 19 return IntRect(std::min(rect.x(), rect.maxX()),
18 std::min(rect.y(), rect.maxY()), 20 std::min(rect.y(), rect.maxY()),
19 std::max(rect.width(), -rect.width()), 21 std::max(rect.width(), -rect.width()),
20 std::max(rect.height(), -rect.height())); 22 std::max(rect.height(), -rect.height()));
21 } 23 }
22 24
23 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect) 25 // TODO(xidachen): this function needs to be changed later on when implementing premultiplyAlpha option
26 static SkImage* flipSkImageVertically(SkImage* input)
27 {
28 int width = input->width();
29 int height = input->height();
30 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
31 OwnPtr<uint8_t[]> imagePixels = adoptArrayPtr(new uint8_t[width * height * i nfo.bytesPerPixel()]);
32 int imageRowBytes = info.bytesPerPixel() * width;
33 input->readPixels(info, imagePixels.get(), imageRowBytes, 0, 0);
34
35 for (int i = 0; i < height / 2; i++) {
36 int topFirstElement = i * imageRowBytes;
37 int topLastElement = (i + 1) * imageRowBytes;
38 int bottomFirstElement = (height - 1 - i) * imageRowBytes;
39 std::swap_ranges(imagePixels.get() + topFirstElement, imagePixels.get() + topLastElement, imagePixels.get() + bottomFirstElement);
40 }
41 return SkImage::NewRasterCopy(info, imagePixels.get(), imageRowBytes);
42 }
43
44 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect, bool flipYEnabled)
24 { 45 {
25 ASSERT(image); 46 ASSERT(image);
26 47
27 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); 48 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height()));
28 const IntRect srcRect = intersection(imgRect, cropRect); 49 const IntRect srcRect = intersection(imgRect, cropRect);
29 50
30 if (cropRect == srcRect) 51 if (cropRect == srcRect) {
52 if (flipYEnabled)
53 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(imag e->imageForCurrentFrame()->newSubset(srcRect))));
31 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()- >newSubset(srcRect))); 54 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()- >newSubset(srcRect)));
55 }
32 56
33 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height())); 57 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height()));
34 58
35 if (srcRect.isEmpty()) 59 if (srcRect.isEmpty())
36 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); 60 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot()));
37 61
38 SkScalar dstLeft = std::min(0, -cropRect.x()); 62 SkScalar dstLeft = std::min(0, -cropRect.x());
39 SkScalar dstTop = std::min(0, -cropRect.y()); 63 SkScalar dstTop = std::min(0, -cropRect.y());
40 if (cropRect.x() < 0) 64 if (cropRect.x() < 0)
41 dstLeft = -cropRect.x(); 65 dstLeft = -cropRect.x();
42 if (cropRect.y() < 0) 66 if (cropRect.y() < 0)
43 dstTop = -cropRect.y(); 67 dstTop = -cropRect.y();
44 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft , dstTop); 68 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft , dstTop);
69 if (flipYEnabled)
70 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(surface- >newImageSnapshot())));
45 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); 71 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot()));
46 } 72 }
47 73
48 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) 74 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options)
49 { 75 {
50 m_image = cropImage(image->cachedImage()->image(), cropRect); 76 if (options.imageOrientation() == imageOrientationFlipY)
77 m_image = cropImage(image->cachedImage()->image(), cropRect, true);
78 else
79 m_image = cropImage(image->cachedImage()->image(), cropRect, false);
51 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) ); 80 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) );
52 } 81 }
53 82
54 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) 83 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options)
55 { 84 {
56 IntSize playerSize; 85 IntSize playerSize;
57 if (video->webMediaPlayer()) 86 if (video->webMediaPlayer())
58 playerSize = video->webMediaPlayer()->naturalSize(); 87 playerSize = video->webMediaPlayer()->naturalSize();
59 88
60 IntRect videoRect = IntRect(IntPoint(), playerSize); 89 IntRect videoRect = IntRect(IntPoint(), playerSize);
61 IntRect srcRect = intersection(cropRect, videoRect); 90 IntRect srcRect = intersection(cropRect, videoRect);
62 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); 91 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels);
63 if (!buffer) 92 if (!buffer)
64 return; 93 return;
65 94
66 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); 95 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y()));
67 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); 96 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr);
68 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); 97 if (options.imageOrientation() == imageOrientationFlipY)
98 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get())));
99 else
100 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration));
69 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) ); 101 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) );
70 } 102 }
71 103
72 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options) 104 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options)
73 { 105 {
74 ASSERT(canvas->isPaintable()); 106 ASSERT(canvas->isPaintable());
75 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect); 107 if (options.imageOrientation() == imageOrientationFlipY)
108 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration). get(), cropRect, true);
109 else
110 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration). get(), cropRect, false);
76 m_image->setOriginClean(canvas->originClean()); 111 m_image->setOriginClean(canvas->originClean());
77 } 112 }
78 113
79 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) 114 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options)
80 { 115 {
81 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 116 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
82 117
83 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); 118 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels);
84 if (!buffer) 119 if (!buffer)
85 return; 120 return;
86 121
87 if (srcRect.isEmpty()) { 122 if (srcRect.isEmpty()) {
88 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); 123 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration));
89 return; 124 return;
90 } 125 }
91 126
92 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y())); 127 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y()));
93 if (cropRect.x() < 0) 128 if (cropRect.x() < 0)
94 dstPoint.setX(-cropRect.x()); 129 dstPoint.setX(-cropRect.x());
95 if (cropRect.y() < 0) 130 if (cropRect.y() < 0)
96 dstPoint.setY(-cropRect.y()); 131 dstPoint.setY(-cropRect.y());
97 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); 132 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint);
98 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); 133 if (options.imageOrientation() == imageOrientationFlipY)
134 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get())));
135 else
136 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration));
99 } 137 }
100 138
101 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options) 139 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options)
102 { 140 {
103 m_image = cropImage(bitmap->bitmapImage(), cropRect); 141 if (options.imageOrientation() == imageOrientationFlipY)
142 m_image = cropImage(bitmap->bitmapImage(), cropRect, true);
143 else
144 m_image = cropImage(bitmap->bitmapImage(), cropRect, false);
104 m_image->setOriginClean(bitmap->originClean()); 145 m_image->setOriginClean(bitmap->originClean());
105 } 146 }
106 147
107 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options) 148 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options)
108 { 149 {
109 m_image = cropImage(image.get(), cropRect); 150 if (options.imageOrientation() == imageOrientationFlipY)
151 m_image = cropImage(image.get(), cropRect, true);
152 else
153 m_image = cropImage(image.get(), cropRect, false);
110 m_image->setOriginClean(image->originClean()); 154 m_image->setOriginClean(image->originClean());
111 } 155 }
112 156
113 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) 157 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image)
114 { 158 {
115 m_image = image; 159 m_image = image;
116 } 160 }
117 161
118 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() 162 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer()
119 { 163 {
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 { 279 {
236 return FloatSize(width(), height()); 280 return FloatSize(width(), height());
237 } 281 }
238 282
239 DEFINE_TRACE(ImageBitmap) 283 DEFINE_TRACE(ImageBitmap)
240 { 284 {
241 ImageLoaderClient::trace(visitor); 285 ImageLoaderClient::trace(visitor);
242 } 286 }
243 287
244 } // namespace blink 288 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698