1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87

ash / public / cpp / image_util.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_PUBLIC_CPP_IMAGE_UTIL_H_
#define ASH_PUBLIC_CPP_IMAGE_UTIL_H_

#include <string>

#include "ash/public/cpp/ash_public_export.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "services/data_decoder/public/mojom/image_decoder.mojom-shared.h"
#include "ui/gfx/image/image_skia.h"

namespace base {
class FilePath;
class SequencedTaskRunner;
}  // namespace base

namespace gfx {
class ImageSkia;
class Size;
}  // namespace gfx

namespace ash {
namespace image_util {

// Returns a `gfx::ImageSkia` of the specified `size` which draws nothing.
ASH_PUBLIC_EXPORT gfx::ImageSkia CreateEmptyImage(const gfx::Size& size);

// Resizes a `gfx::ImageSkia` to just fill the entirety of `new_size`,
// maintaining aspect ratio, and crops any portion of the image outside of the
// bounds of `new_size`.
ASH_PUBLIC_EXPORT gfx::ImageSkia ResizeAndCropImage(
    const gfx::ImageSkia& image_skia,
    const gfx::Size& new_size);

struct ASH_PUBLIC_EXPORT AnimationFrame {
  gfx::ImageSkia image;
  base::TimeDelta duration;
};

using DecodeImageCallback = base::OnceCallback<void(const gfx::ImageSkia&)>;
using DecodeAnimationCallback =
    base::OnceCallback<void(std::vector<AnimationFrame>)>;

// TESTING NOTE: See ash::InProcessImageDecoder for unit testing code that
// calls any of the DecodeImage*() functions below.

// Reads contents at `file_path` and calls `callback` with a decoded image or
// animation, respectively.
// Calls `callback` with an empty image/vector on
// failure to read the file or decode the image.
// If the image is too large, it will be repeatedly halved until it fits in
// `IPC::Channel::kMaximumMessageSize` bytes.
//
// A custom `file_task_runner` may be specified if desired; if not, an arbitrary
// task runner is used internally.
ASH_PUBLIC_EXPORT void DecodeImageFile(
    DecodeImageCallback callback,
    const base::FilePath& file_path,
    data_decoder::mojom::ImageCodec codec =
        data_decoder::mojom::ImageCodec::kDefault,
    scoped_refptr<base::SequencedTaskRunner> file_task_runner = nullptr);
ASH_PUBLIC_EXPORT void DecodeAnimationFile(
    DecodeAnimationCallback callback,
    const base::FilePath& file_path,
    scoped_refptr<base::SequencedTaskRunner> file_task_runner = nullptr);

// Reads contents of `data` and calls `callback` with a decoded image or
// animation, respectively.
// Calls `callback` with an empty image/vector on failure to decode the
// image.
// If the image is too large, it will be repeatedly halved until it fits in
// `IPC::Channel::kMaximumMessageSize` bytes.
ASH_PUBLIC_EXPORT void DecodeImageData(DecodeImageCallback callback,
                                       data_decoder::mojom::ImageCodec codec,
                                       const std::string& data);
ASH_PUBLIC_EXPORT void DecodeAnimationData(DecodeAnimationCallback callback,
                                           const std::string& data);

}  // namespace image_util
}  // namespace ash

#endif  // ASH_PUBLIC_CPP_IMAGE_UTIL_H_