blob: b5ae897256b248865c6af0f37bc404cd1adf9010 [file] [log] [blame]
// 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/buffer_w_stream.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
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();
gfx::BufferWStream stream;
const bool encoding_succeeded =
SkEncodeImage(&stream, bitmap, SkEncodedImageFormat::kPNG, 100);
DCHECK(encoding_succeeded);
return MakeDataURIForImage(
base::as_bytes(base::make_span(stream.TakeBuffer())), "png");
}
} // namespace webui