Skip to content

Commit

Permalink
initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
tukinami committed Mar 3, 2023
0 parents commit 430bbad
Show file tree
Hide file tree
Showing 28 changed files with 2,558 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "i686-pc-windows-msvc"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
38 changes: 38 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "saori-resized-png-mini"
version = "1.0.0"
edition = "2021"
authors = [ "tukinami seika" ]
license = "MIT"
readme = "README.md"
description = "SAORI to resize image and save as png (limited edition)"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
resize = "0.7.4"
rgb = "0.8.36"
png = "0.17.7"
gif = "0.12.0"
jpeg-decoder = "0.3.0"
libwebp-sys = "0.8.0"
tinybmp = "0.4.0"
embedded-graphics = "0.7.1"

[target.'cfg(windows)'.dependencies]
winapi = {version = "0.3.9", features = ["winbase", "libloaderapi", "stringapiset"]}

[dev-dependencies]
tempfile = "3.3.0"
encoding_rs = "0.8.31"

[lib]
name = "resizedpngmini"
path = "src/lib.rs"
crate-type = ["rlib", "cdylib"]

[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 月波 清火

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Resized Png Mini

[GitHub repository](https://github.com/tukinami/saori-resized-png-mini)

## これは何?

デスクトップマスコット、「伺か」で使用できるSAORIの一種です。

拙作[Resized Png](https://github.com/tukinami/saori-resized-png)
の機能限定・軽量版になります。

機能としては、指定した画像ファイルを拡大または縮小し、pngとして出力します。

`Resized Png`からの変更点として、読み込める画像形式が限定されています。

「伺か」「SAORI」等の用語については詳しく説明いたしませんのでご了承下さい。

## 使い方

SAORI自体の使い方は、使用するSHIORIなどによって異なりますので、ご自身でお調べ下さい。

ここではこのSAORIの使い方について説明いたします。

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

### `GetImageType`

+ Argument1: 判別するファイルのパス

+ Result: 画像形式を表す文字列

指定されたファイルの画像形式を返します。
画像でない、または対応していない画像は`UNKNOWN`が返ります。

対応している形式は以下(色深度などによっては、対応していない場合があります):

+ `BMP`
+ `GIF`
+ `JPEG`
+ `PNG`
+ `WEBP`

### `ToResizedPng`

+ Argument1: 入力するファイルのパス
+ Argument2: 出力するファイルのパス
+ Argument3: 出力する画像の横幅の数値
+ Argument4: 出力する画像の縦幅の数値

+ Result: エラーコードの数値(下記参照)

入力された画像を拡大または縮小して、pngとして出力します。
何か問題があった場合は、Resultに`0`以外が入ります。

横幅と縦幅は、負の数を指定すると、もう片方の拡大縮小率に基づいて自動で値が決まります
(両方負の数にすると、何もせずに終了します)。
また、`0`を指定すると入力された画像の値を使用します。

#### エラーコード

0. 正常終了
1. 対応していない形式だった
2. ファイルが見つからなかった
3. 入出力に問題があった
4. 画像のデコードに問題があった
5. 画像のエンコードに問題があった
6. 画像のパラメータに問題があった
7. 画像の大きさが限界値を越えていた
8. 画像サイズが小さすぎた

## 使用ライブラリ

いずれも敬称略。ありがとうございます。

+ [winapi\_rs](https://github.com/retep998/winapi-rs) / Peter Atashian
+ [png](https://github.com/image-rs/image-png) / The image-rs Developers
+ [gif](https://github.com/image-rs/image-gif) / The image-rs Developers
+ [jpeg-decoder](https://github.com/image-rs/jpeg-decoder) / The image-rs Developers
+ [libwebp-sys](https://github.com/NoXF/libwebp-sys) / XianYou, Kornel Lesiński
+ [tinybmp](https://github.com/embedded-graphics/tinybmp) / James Waples, Ralf Fuest
+ [embedded-graphics](https://github.com/embedded-graphics/embedded-graphics) / James Waples, Ralf Fuest
+ [resize](https://github.com/PistonDevelopers/resize) / Kornel, Kagami Hiiragi
+ [rgb](https://github.com/kornelski/rust-rgb) / Kornel Lesiński
+ (テスト実行時) [encoding\_rs](https://github.com/hsivonen/encoding_rs) / Henri Sivonen
+ (テスト実行時) [tempfile](https://github.com/Stebalien/tempfile) / Steven Allen, The Rust Project Developers, Ashley Mannix, Jason White


## ライセンス

MITにて配布いたします。

## 作成者

月波 清火 (tukinami seika)

[GitHub](https://github.com/tukinami)
139 changes: 139 additions & 0 deletions src/chars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use winapi::{
shared::{
minwindef::LPBOOL,
ntdef::{LPCSTR, LPWSTR, NULL},
},
um::{
stringapiset::{MultiByteToWideChar, WideCharToMultiByte},
winnls::MB_PRECOMPOSED,
winnt::LPSTR,
},
};

pub(crate) fn multi_byte_to_wide_char(from: &[u8], codepage: u32) -> Result<Vec<u16>, ()> {
let mut from_buf: Vec<i8> = from.iter().map(|v| *v as i8).collect();
from_buf.push(0);

let to_buf_size = unsafe {
MultiByteToWideChar(
codepage,
MB_PRECOMPOSED,
from_buf.as_ptr(),
-1,
NULL as LPWSTR,
0,
)
};

if to_buf_size == 0 {
return Err(());
}

let mut to_buf = vec![0; to_buf_size as usize + 1];
let result = unsafe {
MultiByteToWideChar(
codepage,
MB_PRECOMPOSED,
from_buf.as_ptr(),
-1,
to_buf.as_mut_ptr(),
to_buf_size,
)
};

if result == 0 {
Err(())
} else {
Ok(to_buf)
}
}

pub(crate) fn wide_char_to_multi_byte(from: &mut Vec<u16>, codepage: u32) -> Result<Vec<i8>, ()> {
from.push(0);

let to_buf_size = unsafe {
WideCharToMultiByte(
codepage,
0,
from.as_ptr(),
-1,
NULL as LPSTR,
0,
NULL as LPCSTR,
NULL as LPBOOL,
)
};

if to_buf_size == 0 {
return Err(());
}

let mut to_buf: Vec<i8> = vec![0; to_buf_size as usize + 1];
let result = unsafe {
WideCharToMultiByte(
codepage,
0,
from.as_ptr(),
-1,
to_buf.as_mut_ptr(),
to_buf_size,
NULL as LPCSTR,
NULL as LPBOOL,
)
};

if result == 0 {
Err(())
} else {
Ok(to_buf)
}
}

#[cfg(test)]
mod tests {
use crate::request::SaoriCharset;

use super::*;

use encoding_rs;

mod multi_byte_to_wide_char {
use super::*;

#[test]
fn success_when_encoding_and_codepage_is_same() {
let case = "あいうえお仕様";
let (case_byte, _encoding, _is_err) = encoding_rs::SHIFT_JIS.encode(case);

let result =
multi_byte_to_wide_char(&case_byte, SaoriCharset::ShiftJIS.codepage()).unwrap();

let p = result.partition_point(|v| *v != 0);

let result = String::from_utf16_lossy(&result[..p]);

assert_eq!(&result, case);
}
}

mod wide_char_to_multi_byte {
use super::*;

#[test]
fn success_when_valid_wide_char_and_codepage_with_shift_jis() {
let case = "あいうえお仕様";
let mut case_chars: Vec<u16> = case.encode_utf16().collect();

let result =
wide_char_to_multi_byte(&mut case_chars, SaoriCharset::ShiftJIS.codepage())
.unwrap();

let result: Vec<u8> = result.iter().map(|v| *v as u8).collect();

let p = result.partition_point(|v| *v != 0);
let (encoded, _encoding, _is_err) = encoding_rs::SHIFT_JIS.decode(&result[..p]);

assert_eq!(&encoded, case);
}
}
}
Loading

0 comments on commit 430bbad

Please sign in to comment.