Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/EncodeUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2023 Google LLC
3
 *
4
 * Use of this source code is governed by a BSD-style license that can be
5
 * found in the LICENSE file.
6
 */
7
8
#include "tools/EncodeUtils.h"
9
10
#include "include/core/SkBitmap.h"
11
#include "include/core/SkData.h"
12
#include "include/core/SkDataTable.h"
13
#include "include/core/SkPixmap.h"
14
#include "include/core/SkRefCnt.h"
15
#include "include/core/SkStream.h"
16
#include "include/core/SkString.h"
17
#include "include/encode/SkPngEncoder.h"
18
#include "src/base/SkBase64.h"
19
20
#include <cstddef>
21
#include <cstdint>
22
23
namespace ToolUtils {
24
25
0
bool BitmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
26
0
    SkPixmap pm;
27
0
    if (!bitmap.peekPixels(&pm)) {
28
0
        dst->set("peekPixels failed");
29
0
        return false;
30
0
    }
31
32
    // We're going to embed this PNG in a data URI, so make it as small as possible
33
0
    SkPngEncoder::Options options;
34
0
    options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
35
0
    options.fZLibLevel = 9;
36
37
0
    SkDynamicMemoryWStream wStream;
38
0
    if (!SkPngEncoder::Encode(&wStream, pm, options)) {
39
0
        dst->set("SkPngEncoder::Encode failed");
40
0
        return false;
41
0
    }
42
43
0
    sk_sp<SkData> pngData = wStream.detachAsData();
44
0
    size_t len = SkBase64::EncodedSize(pngData->size());
45
46
    // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
47
    // Infra says these can be pretty big, as long as we're only outputting them on failure.
48
0
    static const size_t kMaxBase64Length = 1024 * 1024;
49
0
    if (len > kMaxBase64Length) {
50
0
        dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
51
0
        return false;
52
0
    }
53
54
0
    dst->resize(len);
55
0
    SkBase64::Encode(pngData->data(), pngData->size(), dst->data());
56
0
    dst->prepend("data:image/png;base64,");
57
0
    return true;
58
0
}
59
60
0
bool EncodeImageToPngFile(const char* path, const SkBitmap& src) {
61
0
    SkFILEWStream file(path);
62
0
    return file.isValid() && SkPngEncoder::Encode(&file, src.pixmap(), {});
63
0
}
64
65
0
bool EncodeImageToPngFile(const char* path, const SkPixmap& src) {
66
0
    SkFILEWStream file(path);
67
0
    return file.isValid() && SkPngEncoder::Encode(&file, src, {});
68
0
}
69
70
}  // namespace ToolUtils