#png #image #apng #hdr #load-image

bin+lib micro_png

仅依赖少数几个库的低级极简APNG编写器和PNG读取器。支持所有可能的格式(包括HDR和Adam7)。

8个版本

0.3.5 2023年9月2日
0.3.4 2023年9月1日
0.3.3 2023年8月31日
0.2.0 2023年8月27日
0.1.6 2023年8月27日

190图像 中排名

Download history 54/week @ 2024-07-01 76/week @ 2024-07-29

每月 76 次下载

LGPL-3.0-only

190KB
3K SLoC

项目Wiki

https://github.com/js29a/micro_png/wiki

概述

use micro_png::*;

fn main() {
    // load an image
    let image = read_png("tmp/test.png").expect("can't load test.png");

    println!("{} x {}", image.width(), image.height());

    let data = image.data();

    (0 .. image.height()).for_each(|y| {
      (0 .. image.width()).for_each(|x| {
        let _pixel = data[y][x]; // (u16, u16, u16, u16)
      });
    });

    // now write it back as one-frame image

    write_apng("tmp/back.png",
        ImageData::RGBA16(vec![data]),
        None ,// automatically select filtering
        None, // no progress callback
        false // no Adam-7
    ).expect("can't save back.png");

    // write 2x2 pattern image

    let data: Vec<Vec<RGBA>> = vec![
        vec![(255, 0, 0, 255), (0, 0, 0, 255)],// the 1st line
        vec![(0, 0, 0, 255), (255, 0, 0, 255)],// the 2nd line
    ];

    write_apng("tmp/2x2.png",
        ImageData::RGBA(vec![data]), // write one frame
        None ,// automatically select filtering
        None, // no progress callback
        false // no Adam-7
    ).expect("can't save back.png");

    // write single-framed image usize builder

    let data_1: Vec<Vec<RGBA>> = vec![
        vec![(255, 0, 0, 255), (0, 0, 0, 255)],// the 1st line
        vec![(0, 0, 0, 255), (255, 0, 0, 255)],// the 2nd line
    ];

    let builder = APNGBuilder::new("tmp/foo.png", ImageData::RGBA(vec![data_1]))
        .set_adam_7(true);

    build_apng(builder).unwrap();

   // write some animations

    let data_2 = vec![
        vec![// frame #0
            vec![(255, 0, 0, 255), (0, 0, 0, 255)],// the 1st line
            vec![(0, 0, 0, 255), (255, 0, 0, 255)],// the 2nd line
        ],
        vec![// frame #1
            vec![(0, 0, 0, 255), (255, 0, 0, 255)],// the 1st line
            vec![(255, 0, 0, 255), (0, 0, 0, 255)],// the 2nd line
        ],
        vec![// frame #2
            vec![(0, 0, 0, 255), (255, 0, 0, 255)],// the 1st line
            vec![(255, 255, 0, 255), (0, 255, 0, 255)],// the 2nd line
        ],
    ];

    build_apng(
       APNGBuilder::new("tmp/bar.png", ImageData::RGBA(data_2))
           .set_def_dur((100, 1000)) // default frame duration: 100 / 1000 [sec]
           .set_dur(1, (500, 1000)) // duration for frame #1: 500 / 1000 [sec]
    ).unwrap();
}

支持的格式

枚举变体
ImageData::RGB 8位RGB无alpha
ImageData::RGBA 8位RGB带alpha
ImageData::RGB16 16位RGB无alpha
ImageData::RGBA16 16位RGB带alpha
ImageData::NDX n位索引调色板无alpha
ImageData::NDXA n位索引调色板带alpha
ImageData::GRAY k位灰度无alpha
ImageData::GRAYA i位灰度带alpha
  • n: 1, 2, 4, 8,
  • k: 1, 2, 4, 8, 16,
  • i: 8, 16,

待办事项

  • APNG 输入

依赖

~7–16MB
~222K SLoC