diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/chrome/browser/ui/webui/util | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/chrome/browser/ui/webui/util')
-rw-r--r-- | chromium/chrome/browser/ui/webui/util/OWNERS | 6 | ||||
-rw-r--r-- | chromium/chrome/browser/ui/webui/util/image_util.cc | 57 | ||||
-rw-r--r-- | chromium/chrome/browser/ui/webui/util/image_util.h | 22 |
3 files changed, 85 insertions, 0 deletions
diff --git a/chromium/chrome/browser/ui/webui/util/OWNERS b/chromium/chrome/browser/ui/webui/util/OWNERS new file mode 100644 index 00000000000..13b837bc5f2 --- /dev/null +++ b/chromium/chrome/browser/ui/webui/util/OWNERS @@ -0,0 +1,6 @@ +dpapad@chromium.org +johntlee@chromium.org +robliao@chromium.org +tluk@chromium.org + +# COMPONENT: UI>Browser>WebUI
\ No newline at end of file diff --git a/chromium/chrome/browser/ui/webui/util/image_util.cc b/chromium/chrome/browser/ui/webui/util/image_util.cc new file mode 100644 index 00000000000..f914194ee67 --- /dev/null +++ b/chromium/chrome/browser/ui/webui/util/image_util.cc @@ -0,0 +1,57 @@ +// Copyright (c) 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/webui/util/image_util.h" + +#include "base/base64.h" +#include "third_party/skia/include/core/SkStream.h" +#include "ui/gfx/image/image_skia.h" + +namespace { +// Writes bytes to a std::vector that can be fetched. This is used to record the +// output of skia image encoding. +class BufferWStream : public SkWStream { + public: + BufferWStream() = default; + ~BufferWStream() override = default; + + // Returns the output buffer by moving. + std::vector<unsigned char> GetBuffer() { return std::move(result_); } + + // SkWStream: + bool write(const void* buffer, size_t size) override { + const unsigned char* bytes = reinterpret_cast<const unsigned char*>(buffer); + result_.insert(result_.end(), bytes, bytes + size); + return true; + } + + size_t bytesWritten() const override { return result_.size(); } + + private: + std::vector<unsigned char> result_; +}; +} // namespace + +namespace webui { + +std::string MakeDataURIForImage(base::span<const uint8_t> image_data, + base::StringPiece mime_subtype) { + std::string result = "data:image/"; + result.append(mime_subtype.begin(), mime_subtype.end()); + result += ";base64,"; + result += base::Base64Encode(image_data); + return result; +} + +std::string EncodePNGAndMakeDataURI(gfx::ImageSkia image, float scale_factor) { + const SkBitmap& bitmap = image.GetRepresentation(scale_factor).GetBitmap(); + BufferWStream stream; + const bool encoding_succeeded = + SkEncodeImage(&stream, bitmap, SkEncodedImageFormat::kPNG, 100); + DCHECK(encoding_succeeded); + return MakeDataURIForImage( + base::as_bytes(base::make_span(stream.GetBuffer())), "png"); +} + +} // namespace webui diff --git a/chromium/chrome/browser/ui/webui/util/image_util.h b/chromium/chrome/browser/ui/webui/util/image_util.h new file mode 100644 index 00000000000..fd25a1769eb --- /dev/null +++ b/chromium/chrome/browser/ui/webui/util/image_util.h @@ -0,0 +1,22 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_WEBUI_UTIL_IMAGE_UTIL_H_ +#define CHROME_BROWSER_UI_WEBUI_UTIL_IMAGE_UTIL_H_ + +#include "base/containers/span.h" +#include "base/strings/string_piece_forward.h" + +namespace gfx { +class ImageSkia; +} // namespace gfx + +namespace webui { +std::string MakeDataURIForImage(base::span<const uint8_t> image_data, + base::StringPiece mime_subtype); + +std::string EncodePNGAndMakeDataURI(gfx::ImageSkia image, float scale_factor); +} // namespace webui + +#endif // CHROME_BROWSER_UI_WEBUI_UTIL_IMAGE_UTIL_H_ |