Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image::clipped() の性能を改善 #1108

Merged
merged 2 commits into from Sep 24, 2023

Conversation

Raclamusi
Copy link
Member

Issue: #1087

Image::clipped()Image::squareClipped() の性能を改善しました。

Issue で提案したコードからの変更点

  • int32(0)0 に変更しました
  • いくつかの実引数に丸カッコをつけました

@Reputeless
Copy link
Member

右辺値版の実装について、一部の入力で結果が正しくないようです。

20230924-164726-714
上は正しく、下が正しくない結果。

再現コード

Visual Studio 2022 17.7.4

# include <Siv3D.hpp> // Siv3D v0.6.12

void Main()
{
	Scene::SetBackground(ColorF{ 1.0, 0.3, 0.3 });

	const Image image{ U"example/windmill.png" };

	const int32 x = 320;
	const int32 y = 20;
	const int32 w = 600;
	const int32 h = 160;

	const Image expected = image.clipped(x, y, w, h);
	const Image actual = Image{ image }.clipped(x, y, w, h);

	const Texture expectedTexture{ expected };
	const Texture actualTexture{ actual };

	while (System::Update())
	{
		expectedTexture.draw(40, 40).drawFrame(0, 2, Palette::Yellow);
		actualTexture.draw(40, 240).drawFrame(0, 2, Palette::Yellow);
	}
}

テスト用コード

これをパスすれば OK です。

# include <Siv3D.hpp> // Siv3D v0.6.12

void Main()
{
	const Image image1{ U"example/windmill.png" };
	constexpr int32 N = 5000;

	for (int32 i = 0; i < N; ++i)
	{
		const int32 x = Random(-image1.width() * 3, image1.width() * 3);
		const int32 y = Random(-image1.height() * 3, image1.height() * 3);
		const int32 w = Random(-image1.width() * 3, image1.width() * 3);
		const int32 h = Random(-image1.height() * 3, image1.height() * 3);
		const Image image2 = image1.clipped(x, y, w, h);

		if ((w <= 0) || (h <= 0))
		{
			if (not image2.isEmpty())
			{
				Print << U"TEST0 Error! x: {}, y: {}, w: {}, h: {}"_fmt(x, y, w, h);
			}
		}
		else if ((image2.width() != w) || (image2.height() != h))
		{
			Print << U"TEST0 Error! x: {}, y: {}, w: {}, h: {}"_fmt(x, y, w, h);
			break;
		}

		Image base{ image1 };
		image2.overwrite(base, x, y);

		if ((base.size() != image1.size()) || (base.asArray() != image1.asArray()))
		{
			Print << U"TEST1 Error! x: {}, y: {}, w: {}, h: {}"_fmt(x, y, w, h);
			break;
		}
	}

	for (int32 i = 0; i < N; ++i)
	{
		const int32 x = Random(-image1.width() * 3, image1.width() * 3);
		const int32 y = Random(-image1.height() * 3, image1.height() * 3);
		const int32 w = Random(-image1.width() * 3, image1.width() * 3);
		const int32 h = Random(-image1.height() * 3, image1.height() * 3);
		const Image image2 = Image{ image1 }.clipped(x, y, w, h);

		if ((w <= 0) || (h <= 0))
		{
			if (not image2.isEmpty())
			{
				Print << U"TEST2 Error! x: {}, y: {}, w: {}, h: {}"_fmt(x, y, w, h);
			}
		}
		else if ((image2.width() != w) || (image2.height() != h))
		{
			Print << U"TEST2 Error! x: {}, y: {}, w: {}, h: {}"_fmt(x, y, w, h);
			break;
		}

		Image base{ image1 };
		image2.overwrite(base, x, y);

		if ((base.size() != image1.size()) || (base.asArray() != image1.asArray()))
		{
			Print << U"TEST3 Error! x: {}, y: {}, w: {}, h: {}"_fmt(x, y, w, h);
			break;
		}
	}

	Print << U"Finsihed";

	while (System::Update())
	{

	}
}

@Raclamusi
Copy link
Member Author

コピー前にゼロ埋めしている箇所があったので、修正しました。

@Reputeless Reputeless merged commit 9f39785 into Siv3D:v6_develop Sep 24, 2023
2 checks passed
v0.6 Roadmap automation moved this from ToDo to Done Sep 24, 2023
@Reputeless
Copy link
Member

Merged. Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants