agentsclimarketplace

Dart oss image

Skill Python51888/StudyDart-Skills/skills/dart-open-source-projects/dart-oss-image

Agent skills for Study Dart

Install
npx -y skills add Python51888/StudyDart-Skills --skill dart-oss-image

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 3 stars3 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its author says it does

Copied from the file, not written here

学习 brendan-duncan/image 开源项目,通过纯 Dart 图片编解码库理解 core 库(dart:core、dart:typed_data)在底层算法实现中的深度应用。

SKILL.md

6.4 KB, as published. Nobody here has run it

学习 Dart Image 图片处理库项目

Contents

项目概览

项目名称: image 作者: brendan-duncan GitHub: https://github.com/brendan-duncan/image Stars: 1.3k | 难度: 进阶

一个纯 Dart 实现的图片处理库,支持 PNG、JPEG、WebP、GIF、BMP、TIFF 等多种格式的编解码。所有图片格式解析器和编码器均用纯 Dart 实现,不依赖原生 FFI。该项目是学习 Dart 底层编程、字节级操作和算法实现的绝佳范例。

与 dart-core-libraries 的关联

该项目深入使用了 dart-core-librariesdart-collections-iterables 技能的核心知识:

dart-core-libraries 主题项目中的应用
dart:typed_dataUint8List 字节级图像数据
dart:core 数值运算颜色通道位运算、压缩算法
dart:math双线性插值、色彩空间转换
dart:collection哈希表缓存、LZ 字典
dart:convert自定义编解码器基类
List 操作像素数组批处理
Iterable 链滤镜管道

项目架构分析

核心抽象

Image (class)
    ├── width / height / numChannels
    ├── pixels (Uint8List/Uint32List)
    ├── getPixel(x, y)
    └── setPixelRgba(x, y, r, g, b, a)

Decoder (abstract)
    ├── decode(Uint8List bytes) → Image
    ├── startDecode(Uint8List bytes) → Image (流式)

Encoder (abstract)
    └── encode(Image image) → Uint8List

Format (enum)
    ├── png
    ├── jpeg
    ├── webp
    ├── gif
    ├── bmp
    └── tiff

编解码流程

文件字节 (Uint8List)
    → FormatDetector 检测格式
    → 对应 Decoder (e.g. PngDecoder)
    → Image 对象 (像素矩阵)
    → 滤镜 / 变换 / 操作
    → 对应 Encoder (e.g. JpegEncoder)
    → 输出字节 (Uint8List)

底层算法实现详解

位运算像素处理

class Color {
  static int rgbaToUint32(int r, int g, int b, int a) {
    return (a << 24) | (r << 16) | (g << 8) | b;
  }

  static int getRed(int rgba) => (rgba >> 16) & 0xFF;
  static int getGreen(int rgba) => (rgba >> 8) & 0xFF;
  static int getBlue(int rgba) => rgba & 0xFF;
  static int getAlpha(int rgba) => (rgba >> 24) & 0xFF;
}

双线性插值

import 'dart:math';

Color bilinearInterpolate(
  Image img, double x, double y,
) {
  final x0 = x.floor();
  final y0 = y.floor();
  final x1 = min(x0 + 1, img.width - 1);
  final y1 = min(y0 + 1, img.height - 1);
  final dx = x - x0;
  final dy = y - y0;

  return _mixColors(
    _mixColors(img.getPixel(x0, y0), img.getPixel(x1, y0), dx),
    _mixColors(img.getPixel(x0, y1), img.getPixel(x1, y1), dx),
    dy,
  );
}

PNG 压缩算法

// Deflate 压缩核心
List<int> deflateCompress(List<int> data) {
  // LZ77 查找重复序列
  // Huffman 编码压缩
  // 纯 Dart 实现
}

Workflow: 通过 image 库学习底层 Dart 编程

Task Progress

  • Step 1: 浏览支持的格式。 查看项目 README 中的格式列表和编码器/解码器矩阵。
  • Step 2: 运行简单示例。 写一个简单的图片打开和保存代码,验证安装正确。
  • Step 3: 阅读 BMP 解码器。 BMP 是最简单的格式,从 BMPDecoder 开始理解解码流程。
  • Step 4: 分析像素操作。 理解 Uint32List 和位运算在像素处理中的用法。
  • Step 5: 实现自定义滤镜。 写一个灰度转换或模糊滤镜。
  • Step 6: 运行测试。 执行 dart test 观察编解码测试用例。
  • Step 7: 阅读高级格式解码。 在理解 BMP 后,继续阅读 PNG 和 JPEG 解码器。
  • Step 8: Feedback Loop。 写滤镜 → 跑测试 → 对比输出 → 优化算法 → 重复。

条件逻辑

  • 如果字节序(Endianness)出错: 不同格式使用不同的字节序(Big-Endian/Little-Endian),查阅具体格式规范。
  • 如果像素色彩异常: 检查 RGBA 通道顺序是否正确,部分格式使用 BGRA 而非 RGBA。
  • 如果编解码性能不佳: 考虑使用 Uint32List 单次操作替代 Uint8List 逐字节处理。
  • 如果需要理解压缩算法: 参考 PNG、GIF 规范文档,结合项目代码逐行对照。

Examples

从项目中学到的图片处理模式

import 'package:image/image.dart';

void main() {
  final image = decodePng(File('input.png').readAsBytesSync());
  if (image == null) return;

  for (var y = 0; y < image.height; y++) {
    for (var x = 0; x < image.width; x++) {
      final pixel = image.getPixel(x, y);
      final r = getRed(pixel);
      final g = getGreen(pixel);
      final b = getBlue(pixel);
      final gray = ((0.299 * r) + (0.587 * g) + (0.114 * b)).round();
      image.setPixelRgba(x, y, gray, gray, gray, getAlpha(pixel));
    }
  }

  File('output.png').writeAsBytesSync(encodePng(image));
}

结合 dart-core-libraries 的延伸练习:自定义滤镜管道

typedef Filter = Image Function(Image);

Image grayscale(Image img) {
  // 灰度转换逻辑
  return img;
}

Image invert(Image img) {
  for (var y = 0; y < img.height; y++) {
    for (var x = 0; x < img.width; x++) {
      final p = img.getPixel(x, y);
      img.setPixelRgba(x, y,
        255 - getRed(p),
        255 - getGreen(p),
        255 - getBlue(p),
        getAlpha(p),
      );
    }
  }
  return img;
}

Image applyFilters(Image img, List<Filter> filters) {
  return filters.fold(img, (current, filter) => filter(current));
}

void main() {
  final img = decodeImage(File('photo.jpg').readAsBytesSync())!;
  applyFilters(img, [grayscale, invert]);
}

Keep looking

Skills are one crate of 328,083. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.