| 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 |
#ifndef SkBmpRustCodec_DEFINED |
| 8 |
#define SkBmpRustCodec_DEFINED |
| 9 |
|
| 10 |
#include <memory> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include "experimental/rust_bmp/ffi/FFI.rs.h" |
| 14 |
#include "include/codec/SkCodec.h" |
| 15 |
#include "include/codec/SkEncodedImageFormat.h" |
| 16 |
#include "include/core/SkSpan.h" |
| 17 |
#include "third_party/rust/cxx/v1/cxx.h" |
| 18 |
|
| 19 |
struct SkEncodedInfo; |
| 20 |
class SkStream; |
| 21 |
class SkSwizzler; |
| 22 |
|
| 23 |
// This class provides the Skia image decoding API (`SkCodec`) on top of: |
| 24 |
// * The third-party `image` crate (containing BMP decompression and decoding |
| 25 |
// implemented in Rust) |
| 26 |
// * Skia's `SkSwizzler` and `skcms_Transform` (pixel format and color space |
| 27 |
// transformations implemented in C++). |
| 28 |
class SkBmpRustCodec final : public SkCodec { |
| 29 |
public: |
| 30 |
static std::unique_ptr<SkBmpRustCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); |
| 31 |
|
| 32 |
~SkBmpRustCodec() override; |
| 33 |
|
| 34 |
protected: |
| 35 |
SkBmpRustCodec(SkEncodedInfo&&, |
| 36 |
std::unique_ptr<SkStream>, |
| 37 |
rust::Box<rust_bmp::Reader>, |
| 38 |
size_t srcRowBytes); |
| 39 |
|
| 40 |
SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kBMP; } |
| 41 |
|
| 42 |
bool onRewind() override; |
| 43 |
|
| 44 |
private: |
| 45 |
// Codec implementation |
| 46 |
Result onGetPixels(const SkImageInfo& info, |
| 47 |
void* dst, |
| 48 |
size_t dstRowStride, |
| 49 |
const Options& options, |
| 50 |
int* rowsDecoded) override; |
| 51 |
|
| 52 |
// Helper methods |
| 53 |
Result performFullDecode(const SkImageInfo& dstInfo, void* dst, size_t dstRowStride); |
| 54 |
Result initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts); |
| 55 |
void swizzleRow(const uint8_t* srcRow, void* dstRow); |
| 56 |
|
| 57 |
rust::Box<rust_bmp::Reader> fReader; |
| 58 |
std::unique_ptr<SkStream> fPrivStream; |
| 59 |
std::unique_ptr<SkSwizzler> fSwizzler; |
| 60 |
|
| 61 |
// Using 4-bytes-wide `uint32_t` for each pixel, because |
| 62 |
// `kXformSrcColorType = kRGBA_8888_SkColorType`. |
| 63 |
std::unique_ptr<uint32_t[]> fXformBuffer; |
| 64 |
|
| 65 |
const size_t fSrcRowBytes; |
| 66 |
}; |
| 67 |
|
| 68 |
#endif // SkBmpRustCodec_DEFINED |