Skip to content

Commit

Permalink
Merge pull request #1 from nikolat/main
Browse files Browse the repository at this point in the history
画像サイズを取得する機能を追加
  • Loading branch information
tukinami committed Jun 12, 2024
2 parents 70811bd + 2eeae5f commit 04f6736
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: build
on:
workflow_dispatch:

jobs:
build:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup cache
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Setup toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: i686-pc-windows-msvc
components: rustfmt, clippy

- name: Build
run: cargo build --release --target=i686-pc-windows-msvc

- name: Prepare for upload
run: |
mkdir artifact
mkdir artifact/saori-resized-png-mini
copy target/i686-pc-windows-msvc/release/resizedpngmini.dll artifact/saori-resized-png-mini/resizedpngmini.dll
copy README.md artifact/saori-resized-png-mini/README.md
copy LICENSE artifact/saori-resized-png-mini/LICENSE
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: saori-resized-png-mini
path: artifact/
36 changes: 36 additions & 0 deletions .github/workflows/check_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: check-and-test
on:
push:
branches:
- main

jobs:
check-and-test:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup cache
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Setup toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: i686-pc-windows-msvc
components: rustfmt, clippy

- name: Check
run: cargo check

- name: Test
run: cargo test
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SAORI自体の使い方は、使用するSHIORIなどによって異なります
ここではこのSAORIの使い方について説明いたします。

Argument0に、使用する機能名を指定して使用します。
指定できる機能は`GetImageType``ToResizedPng`です。
指定できる機能は`GetImageType``GetImageInfo``ToResizedPng`です。

### `GetImageType`

Expand All @@ -41,6 +41,14 @@ Argument0に、使用する機能名を指定して使用します。
+ `PNG`
+ `WEBP`

### `GetImageInfo`

+ Argument1: 情報を取得するファイルのパス

+ Result: Valueの情報を「,」でつなげたもの
+ Value0: 画像の幅
+ Value1: 画像の高さ

### `ToResizedPng`

+ Argument1: 入力するファイルのパス
Expand Down
12 changes: 11 additions & 1 deletion src/procedure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use crate::request::*;
use crate::resized_png::{get_image_type, to_resized_png};
use crate::resized_png::{get_image_info, get_image_type, to_resized_png};
use crate::response::*;

/// load時に呼ばれる関数
Expand Down Expand Up @@ -35,6 +35,16 @@ pub fn execute(path: &str, request: &SaoriRequest, response: &mut SaoriResponse)
response.set_result(v.to_string());
}
}
"GetImageInfo" => {
if let Some(input_path_str) = args.get(1) {
let input_path = path.join(input_path_str);

let (width, height) = get_image_info(&input_path);

response.set_result(format!("{},{}", width, height));
response.set_value(vec![width.to_string(), height.to_string()]);
}
}
"ToResizedPng" => {
if let (
Some(input_path_str),
Expand Down
52 changes: 52 additions & 0 deletions src/resized_png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ pub(crate) fn get_image_type(src_path: &PathBuf) -> &'static str {
"UNKNOWN"
}

pub(crate) fn get_image_info(src_path: &PathBuf) -> (i64, i64) {
let (_src_rgba, input_width_raw, input_height_raw) = match image::png::read_image_data(src_path)
.or(image::bmp::read_image_data(src_path))
.or(image::gif::read_image_data(src_path))
.or(image::jpeg::read_image_data(src_path))
.or(image::webp::read_image_data(src_path))
{
Ok(v) => v,
Err(_) => return (0, 0),
};

let (input_width, input_height) = match NonZeroU32::new(input_width_raw)
.zip(NonZeroU32::new(input_height_raw))
.ok_or(ResizedPngError::InputSizeError)
{
Ok(v) => v,
Err(_) => return (0, 0),
};

return (input_width.get() as i64, input_height.get() as i64);
}

pub(crate) fn to_resized_png(
src_path: &PathBuf,
dist_path: &PathBuf,
Expand Down Expand Up @@ -147,6 +169,36 @@ mod tests {
}
}

mod get_image_info {
use super::*;

#[test]
fn get_image_info_when_image_file_exists() {
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_target/image/sample.png");
let (width, height) = get_image_info(&path);
assert_eq!(width, 100);
assert_eq!(height, 200);
}

#[test]
fn get_image_info_when_non_image_file_exists() {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
let (width, height) = get_image_info(&path);
assert_eq!(width, 0);
assert_eq!(height, 0);
}

#[test]
fn get_image_info_when_file_does_not_exist() {
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_target/something_wrong.png");
let (width, height) = get_image_info(&path);
assert_eq!(width, 0);
assert_eq!(height, 0);
}
}

mod to_resized_png {
use super::*;

Expand Down

0 comments on commit 04f6736

Please sign in to comment.