Debian Codegraph @

src/image/SkImage_RasterFactories.cpp

raw
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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColorType.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/private/base/SkMath.h"
17 #include "src/core/SkCompressedDataUtils.h"
18 #include "src/core/SkImageFilterTypes.h"
19 #include "src/core/SkImageFilter_Base.h"
20 #include "src/image/SkImage_Base.h"
21 #include "src/image/SkImage_Raster.h"
22
23 #include <cstddef>
24 #include <cstdint>
25 #include <utility>
26
27 class SkImageFilter;
28 struct SkIPoint;
29 struct SkIRect;
30 enum class SkTextureCompressionType;
31
32 static bool valid_args(const SkImageInfo& info, size_t rowBytes, size_t* minSize) {
33 const int maxDimension = SK_MaxS32 >> 2;
34
35 // TODO(mtklein): eliminate anything here that setInfo() has already checked.
36 SkBitmap b;
37 if (!b.setInfo(info, rowBytes)) {
38 return false;
39 }
40
41 if (info.width() <= 0 || info.height() <= 0) {
42 return false;
43 }
44 if (info.width() > maxDimension || info.height() > maxDimension) {
45 return false;
46 }
47 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) {
48 return false;
49 }
50 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) {
51 return false;
52 }
53
54 if (kUnknown_SkColorType == info.colorType()) {
55 return false;
56 }
57 if (!info.validRowBytes(rowBytes)) {
58 return false;
59 }
60
61 size_t size = info.computeByteSize(rowBytes);
62 if (SkImageInfo::ByteSizeOverflowed(size)) {
63 return false;
64 }
65
66 if (minSize) {
67 *minSize = size;
68 }
69 return true;
70 }
71
72 namespace SkImages {
73 sk_sp<SkImage> RasterFromBitmap(const SkBitmap& bm) {
74 if (!bm.pixelRef()) {
75 return nullptr;
76 }
77
78 return SkImage_Raster::MakeFromBitmap(bm, SkCopyPixelsMode::kIfMutable);
79 }
80
81 sk_sp<SkImage> RasterFromPixmapCopy(const SkPixmap& pmap) {
82 size_t size;
83 if (!valid_args(pmap.info(), pmap.rowBytes(), &size) || !pmap.addr()) {
84 return nullptr;
85 }
86
87 // Here we actually make a copy of the caller's pixel data
88 sk_sp<SkData> data(SkData::MakeWithCopy(pmap.addr(), size));
89 return sk_make_sp<SkImage_Raster>(
90 pmap.info(), std::move(data), pmap.rowBytes(), /*mips=*/nullptr, kNeedNewImageUniqueID);
91 }
92
93 sk_sp<SkImage> RasterFromData(const SkImageInfo& info, sk_sp<SkData> data, size_t rowBytes) {
94 size_t size;
95 if (!valid_args(info, rowBytes, &size) || !data) {
96 return nullptr;
97 }
98
99 // did they give us enough data?
100 if (data->size() < size) {
101 return nullptr;
102 }
103
104 return sk_make_sp<SkImage_Raster>(
105 info, std::move(data), rowBytes, /*mips=*/nullptr, kNeedNewImageUniqueID);
106 }
107
108 sk_sp<SkImage> MakeWithFilter(sk_sp<SkImage> src,
109 const SkImageFilter* filter,
110 const SkIRect& subset,
111 const SkIRect& clipBounds,
112 SkIRect* outSubset,
113 SkIPoint* offset) {
114 if (!src || !filter) {
115 return nullptr;
116 }
117
118 sk_sp<skif::Backend> backend = skif::MakeRasterBackend({}, src->colorType());
119 return as_IFB(filter)->makeImageWithFilter(std::move(backend),
120 std::move(src),
121 subset,
122 clipBounds,
123 outSubset,
124 offset);
125 }
126
127 // TODO: this could be improved to decode and make use of the mipmap
128 // levels potentially present in the compressed data. For now, any
129 // mipmap levels are discarded.
130 sk_sp<SkImage> RasterFromCompressedTextureData(sk_sp<SkData> data,
131 int width,
132 int height,
133 SkTextureCompressionType type) {
134 size_t expectedSize = SkCompressedFormatDataSize(type, {width, height}, false);
135 if (!data || data->size() < expectedSize) {
136 return nullptr;
137 }
138
139 SkAlphaType at =
140 SkTextureCompressionTypeIsOpaque(type) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
141
142 SkImageInfo ii = SkImageInfo::MakeN32(width, height, at);
143
144 if (!valid_args(ii, ii.minRowBytes(), nullptr)) {
145 return nullptr;
146 }
147
148 SkBitmap bitmap;
149 if (!bitmap.tryAllocPixels(ii)) {
150 return nullptr;
151 }
152
153 if (!SkDecompress(std::move(data), {width, height}, type, &bitmap)) {
154 return nullptr;
155 }
156
157 bitmap.setImmutable();
158 return RasterFromBitmap(bitmap);
159 }
160
161 sk_sp<SkImage> RasterFromPixmap(const SkPixmap& pmap, RasterReleaseProc proc, ReleaseContext ctx) {
162 size_t size;
163 if (!valid_args(pmap.info(), pmap.rowBytes(), &size) || !pmap.addr()) {
164 return nullptr;
165 }
166
167 sk_sp<SkData> data(SkData::MakeWithProc(pmap.addr(), size, proc, ctx));
168 return sk_make_sp<SkImage_Raster>(
169 pmap.info(), std::move(data), pmap.rowBytes(), /*mips=*/nullptr, kNeedNewImageUniqueID);
170 }
171
172 } // namespace SkImages