| 1 |
/* |
| 2 |
* Copyright 2025 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 "experimental/rust_bmp/decoder/impl/SkBmpRustCodec.h" |
| 9 |
|
| 10 |
#include "include/core/SkColorType.h" |
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkSpan.h" |
| 13 |
#include "include/core/SkStream.h" |
| 14 |
#include "include/private/SkEncodedInfo.h" |
| 15 |
#include "include/private/base/SkAssert.h" |
| 16 |
#include "include/private/base/SkTemplates.h" |
| 17 |
#include "rust/common/SkStreamAdapter.h" |
| 18 |
#include "rust/common/SpanUtils.h" |
| 19 |
#include "src/base/SkSafeMath.h" |
| 20 |
#include "src/codec/SkSwizzler.h" |
| 21 |
#include "src/core/SkStreamPriv.h" |
| 22 |
|
| 23 |
// Color type to use when creating the swizzler for xform |
| 24 |
// |
| 25 |
// Note that `bitsPerComponent` is always 8 for all supported BMP images. |
| 26 |
static constexpr SkColorType kXformSrcColorType = kRGBA_8888_SkColorType; |
| 27 |
|
| 28 |
// Static assertions to validate that Rust BmpColor/BmpAlpha enum values |
| 29 |
// match the corresponding SkEncodedInfo enum values. These assertions |
| 30 |
// ensure type-safe casting between the enums. |
| 31 |
static_assert(static_cast<int>(rust_bmp::BmpColor::RGB) == |
| 32 |
static_cast<int>(SkEncodedInfo::kRGB_Color), |
| 33 |
"BmpColor::RGB must match SkEncodedInfo::kRGB_Color"); |
| 34 |
static_assert(static_cast<int>(rust_bmp::BmpColor::RGBA) == |
| 35 |
static_cast<int>(SkEncodedInfo::kRGBA_Color), |
| 36 |
"BmpColor::RGBA must match SkEncodedInfo::kRGBA_Color"); |
| 37 |
static_assert(static_cast<int>(rust_bmp::BmpColor::BGR) == |
| 38 |
static_cast<int>(SkEncodedInfo::kBGR_Color), |
| 39 |
"BmpColor::BGR must match SkEncodedInfo::kBGR_Color"); |
| 40 |
static_assert(static_cast<int>(rust_bmp::BmpColor::BGRA) == |
| 41 |
static_cast<int>(SkEncodedInfo::kBGRA_Color), |
| 42 |
"BmpColor::BGRA must match SkEncodedInfo::kBGRA_Color"); |
| 43 |
|
| 44 |
static_assert(static_cast<int>(rust_bmp::BmpAlpha::Opaque) == |
| 45 |
static_cast<int>(SkEncodedInfo::kOpaque_Alpha), |
| 46 |
"BmpAlpha::Opaque must match SkEncodedInfo::kOpaque_Alpha"); |
| 47 |
static_assert(static_cast<int>(rust_bmp::BmpAlpha::Unpremul) == |
| 48 |
static_cast<int>(SkEncodedInfo::kUnpremul_Alpha), |
| 49 |
"BmpAlpha::Unpremul must match SkEncodedInfo::kUnpremul_Alpha"); |
| 50 |
|
| 51 |
namespace { |
| 52 |
|
| 53 |
// Helper function to map Rust DecodingResult to SkCodec::Result |
| 54 |
SkCodec::Result MapDecodingResult(rust_bmp::DecodingResult rustResult) { |
| 55 |
switch (rustResult) { |
| 56 |
case rust_bmp::DecodingResult::Success: |
| 57 |
return SkCodec::kSuccess; |
| 58 |
case rust_bmp::DecodingResult::FormatError: |
| 59 |
return SkCodec::kErrorInInput; |
| 60 |
case rust_bmp::DecodingResult::ParameterError: |
| 61 |
return SkCodec::kInvalidParameters; |
| 62 |
case rust_bmp::DecodingResult::UnsupportedFeature: |
| 63 |
return SkCodec::kUnimplemented; |
| 64 |
case rust_bmp::DecodingResult::IncompleteInput: |
| 65 |
return SkCodec::kIncompleteInput; |
| 66 |
case rust_bmp::DecodingResult::MemoryError: |
| 67 |
return SkCodec::kInternalError; |
| 68 |
case rust_bmp::DecodingResult::OtherError: |
| 69 |
return SkCodec::kErrorInInput; |
| 70 |
} |
| 71 |
SK_ABORT("Unexpected `rust_bmp::DecodingResult`: %d", static_cast<int>(rustResult)); |
| 72 |
} |
| 73 |
|
| 74 |
} // namespace |
| 75 |
|
| 76 |
std::unique_ptr<SkBmpRustCodec> SkBmpRustCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 77 |
Result* result) { |
| 78 |
Result resultStorage; |
| 79 |
if (!result) { |
| 80 |
result = &resultStorage; |
| 81 |
} |
| 82 |
|
| 83 |
if (!stream) { |
| 84 |
*result = kInvalidInput; |
| 85 |
return nullptr; |
| 86 |
} |
| 87 |
|
| 88 |
auto inputAdapter = std::make_unique<rust::stream::SkStreamAdapter>(stream.get()); |
| 89 |
rust::Box<rust_bmp::ResultOfReader> resultOfReader = |
| 90 |
rust_bmp::new_reader(std::move(inputAdapter)); |
| 91 |
|
| 92 |
rust_bmp::DecodingResult rustResult = resultOfReader->err(); |
| 93 |
if (rustResult != rust_bmp::DecodingResult::Success) { |
| 94 |
*result = MapDecodingResult(rustResult); |
| 95 |
return nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
rust::Box<rust_bmp::Reader> reader = resultOfReader->unwrap(); |
| 99 |
|
| 100 |
uint32_t width = reader->width(); |
| 101 |
uint32_t height = reader->height(); |
| 102 |
rust_bmp::BmpColor rustColor = reader->color(); |
| 103 |
rust_bmp::BmpAlpha rustAlpha = reader->alpha(); |
| 104 |
|
| 105 |
// BmpColor/BmpAlpha values match SkEncodedInfo enums (validated by static_assert). |
| 106 |
SkEncodedInfo::Color color = static_cast<SkEncodedInfo::Color>(rustColor); |
| 107 |
SkEncodedInfo::Alpha alpha = static_cast<SkEncodedInfo::Alpha>(rustAlpha); |
| 108 |
constexpr int kBitsPerComponent = 8; |
| 109 |
|
| 110 |
// TODO(crbug.com/452666425): Add ICC profile support for color-managed decoding. BMP files |
| 111 |
// can embed ICC profiles (BITMAPV5HEADER format). Once rust/icc module |
| 112 |
// is available, extract profile bytes in Rust FFI, parse with skcms to |
| 113 |
// create sk_sp<SkData>, and pass to SkEncodedInfo::Make() below. |
| 114 |
SkEncodedInfo encodedInfo = SkEncodedInfo::Make( |
| 115 |
width, |
| 116 |
height, |
| 117 |
color, |
| 118 |
alpha, |
| 119 |
kBitsPerComponent |
| 120 |
); |
| 121 |
|
| 122 |
// Pre-calculate srcRowBytes (width * bytesPerPixel) and check for overflow. |
| 123 |
// Also verify that the total image size (height * srcRowBytes) doesn't overflow. |
| 124 |
size_t bytesPerPixel = (color == SkEncodedInfo::kBGR_Color) ? 3 : 4; |
| 125 |
SkSafeMath safe; |
| 126 |
size_t srcRowBytes = safe.mul(safe.castTo<size_t>(width), bytesPerPixel); |
| 127 |
(void)safe.mul(safe.castTo<size_t>(height), srcRowBytes); |
| 128 |
if (!safe.ok()) { |
| 129 |
*result = kInternalError; |
| 130 |
return nullptr; |
| 131 |
} |
| 132 |
|
| 133 |
*result = kSuccess; |
| 134 |
return std::unique_ptr<SkBmpRustCodec>(new SkBmpRustCodec( |
| 135 |
std::move(encodedInfo), |
| 136 |
std::move(stream), |
| 137 |
std::move(reader), |
| 138 |
srcRowBytes |
| 139 |
)); |
| 140 |
} |
| 141 |
|
| 142 |
SkBmpRustCodec::SkBmpRustCodec(SkEncodedInfo&& encodedInfo, |
| 143 |
std::unique_ptr<SkStream> stream, |
| 144 |
rust::Box<rust_bmp::Reader> reader, |
| 145 |
size_t srcRowBytes) |
| 146 |
: SkCodec(std::move(encodedInfo), skcms_PixelFormat_BGR_888, |
| 147 |
// TODO(crbug.com/370522089): Pass stream to SkCodec once SkCodec |
| 148 |
// avoids unnecessary rewinding (which forces re-reading entire stream). |
| 149 |
/* stream = */ nullptr) |
| 150 |
, fReader(std::move(reader)) |
| 151 |
, fPrivStream(std::move(stream)) |
| 152 |
, fSrcRowBytes(srcRowBytes) { |
| 153 |
SkASSERT_RELEASE(fPrivStream); |
| 154 |
} |
| 155 |
|
| 156 |
SkBmpRustCodec::~SkBmpRustCodec() = default; |
| 157 |
|
| 158 |
bool SkBmpRustCodec::onRewind() { |
| 159 |
if (!fPrivStream->rewind()) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
auto inputAdapter = std::make_unique<rust::stream::SkStreamAdapter>(fPrivStream.get()); |
| 164 |
rust::Box<rust_bmp::ResultOfReader> resultOfReader = |
| 165 |
rust_bmp::new_reader(std::move(inputAdapter)); |
| 166 |
|
| 167 |
if (resultOfReader->err() != rust_bmp::DecodingResult::Success) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
fReader = resultOfReader->unwrap(); |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
SkCodec::Result SkBmpRustCodec::initializeSwizzler(const SkImageInfo& dstInfo, |
| 177 |
const Options& opts) { |
| 178 |
SkImageInfo swizzlerInfo = dstInfo; |
| 179 |
SkCodec::Options swizzlerOptions = opts; |
| 180 |
|
| 181 |
if (this->xformOnDecode()) { |
| 182 |
fXformBuffer.reset(new uint32_t[dstInfo.width()]); |
| 183 |
swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
| 184 |
if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 185 |
swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 186 |
} |
| 187 |
swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized; |
| 188 |
} |
| 189 |
|
| 190 |
fSwizzler = SkSwizzler::Make(this->getEncodedInfo(), nullptr, swizzlerInfo, swizzlerOptions); |
| 191 |
if (!fSwizzler) { |
| 192 |
// SkSwizzler doesn't support all possible destination color types (e.g., |
| 193 |
// kBGRA_10101010_XR_SkColorType). Return kInvalidConversion if the requested |
| 194 |
// destination format is not supported. |
| 195 |
return kInvalidConversion; |
| 196 |
} |
| 197 |
return kSuccess; |
| 198 |
} |
| 199 |
|
| 200 |
void SkBmpRustCodec::swizzleRow(const uint8_t* srcRow, void* dstRow) { |
| 201 |
if (this->xformOnDecode()) { |
| 202 |
const int32_t width = this->dimensions().width(); |
| 203 |
fSwizzler->swizzle(fXformBuffer.get(), srcRow); |
| 204 |
this->applyColorXform(dstRow, fXformBuffer.get(), width); |
| 205 |
} else { |
| 206 |
fSwizzler->swizzle(dstRow, srcRow); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
SkCodec::Result SkBmpRustCodec::onGetPixels(const SkImageInfo& info, |
| 211 |
void* dst, |
| 212 |
size_t dstRowStride, |
| 213 |
const Options& options, |
| 214 |
int* rowsDecoded) { |
| 215 |
Result result = this->initializeSwizzler(info, options); |
| 216 |
if (result != kSuccess) { |
| 217 |
return result; |
| 218 |
} |
| 219 |
|
| 220 |
return this->performFullDecode(info, dst, dstRowStride); |
| 221 |
} |
| 222 |
|
| 223 |
SkCodec::Result SkBmpRustCodec::performFullDecode(const SkImageInfo& dstInfo, |
| 224 |
void* dst, |
| 225 |
size_t dstRowStride) { |
| 226 |
// Reset the reader to start decoding from the beginning. |
| 227 |
fReader->reset_decode_state(); |
| 228 |
|
| 229 |
const int32_t height = this->dimensions().height(); |
| 230 |
|
| 231 |
SkSafeMath safe; |
| 232 |
size_t totalSize = safe.mul(fSrcRowBytes, safe.castTo<size_t>(height)); |
| 233 |
if (!safe.ok()) { |
| 234 |
return kInternalError; |
| 235 |
} |
| 236 |
|
| 237 |
std::vector<uint8_t> decodedData(totalSize); |
| 238 |
SkSpan<uint8_t> decodedImage = SkSpan(decodedData); |
| 239 |
|
| 240 |
// Decode all rows from the Rust decoder |
| 241 |
for (int32_t y = 0; y < height; ++y) { |
| 242 |
SkSpan<uint8_t> rowSpan = decodedImage.first(fSrcRowBytes); |
| 243 |
rust::Slice<uint8_t> rowSlice(rowSpan.data(), fSrcRowBytes); |
| 244 |
|
| 245 |
rust_bmp::DecodingResult result = fReader->next_row(rowSlice); |
| 246 |
if (result == rust_bmp::DecodingResult::IncompleteInput) { |
| 247 |
return kIncompleteInput; |
| 248 |
} |
| 249 |
if (result != rust_bmp::DecodingResult::Success) { |
| 250 |
return kErrorInInput; |
| 251 |
} |
| 252 |
|
| 253 |
decodedImage = decodedImage.subspan(fSrcRowBytes); |
| 254 |
} |
| 255 |
|
| 256 |
// Now swizzle all rows to the destination |
| 257 |
SkSpan<const uint8_t> srcImage = SkSpan(decodedData); |
| 258 |
for (int32_t y = 0; y < height; ++y) { |
| 259 |
SkSpan<const uint8_t> srcRow = srcImage.first(fSrcRowBytes); |
| 260 |
void* dstRow = SkTAddOffset<void>(dst, y * dstRowStride); |
| 261 |
|
| 262 |
this->swizzleRow(srcRow.data(), dstRow); |
| 263 |
|
| 264 |
srcImage = srcImage.subspan(fSrcRowBytes); |
| 265 |
} |
| 266 |
|
| 267 |
return kSuccess; |
| 268 |
} |