昨日の続き
カラー画像で誤差拡散
アプリダウンロード先
ここの20180223_.8.zipがそれ
元の画像
右隣への誤差拡散法
FloydSteinberg
FloydSteinberg(蛇行)
SierraLite
JaJuNi
Atkinson
以前試したときはうまく書けなかったけど、昨日までの白黒をカラー版にしてみたらうまくできた
JaJuNiとAtkinsonは色の境界、輪郭がはっきりするから電線が目立つ感じ
図形
蛇行のFloydSteinbergとJaJuNiがいいねえ、縞模様が出ていない
<summary>
</summary>
<param name="source"></param>
<returns></returns>
private BitmapSource ErrorDiffusionFloydSteinberg(BitmapSource source)
{
var wb = new WriteableBitmap(source);
int h = wb.PixelHeight;
int w = wb.PixelWidth;
int stride = wb.BackBufferStride;
var pixels = new byte[h * stride];
wb.CopyPixels(pixels, stride, 0);
float[] iPixels = new float[pixels.Length];
for (int i = 0; i < iPixels.Length; ++i)
{
iPixels[i] = pixels[i];
}
long p = 0, pp = 0;
float gosa = 0;
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
p = y * stride + (x * 4);
for (int i = 0; i < 3; ++i)
{
pp = p + i;
gosa = 0;
if (iPixels[pp] < 127.5f)
{
gosa = iPixels[pp];
iPixels[pp] = 0;
}
else
{
gosa = (iPixels[pp] - 255f);
iPixels[pp] = 255;
}
if (pp + 4 < pixels.Length && x < w - 1)
{
iPixels[pp + 4] += (gosa / 16f) * 7f;
}
if (y < h - 1)
{
if (x != 0)
{
iPixels[pp + stride - 4] += (gosa / 16f) * 3f;
}
iPixels[pp + stride] += (gosa / 16f) * 5f;
if (x < w - 1)
{
iPixels[pp + stride + 4] += (gosa / 16f) * 1f;
}
}
}
}
}
for (int i = 0; i < pixels.Length; ++i)
{
pixels[i] = (byte)(iPixels[i]);
}
wb.WritePixels(new Int32Rect(0, 0, w, h), pixels, stride, 0);
return wb;
}
昨日の白黒とほとんど同じで、違うのは色を変えたところだけ
白黒は色の情報が明るさだけだったのがカラーではRGBの3つになったので
それぞれに対して誤差拡散するので3ループしている。
PixelFormat.Pbgra32は1
ピクセル4byteで色の並びはBGRAなので
右隣の
ピクセルに誤差拡散するときは4byte先になるので元のindex
+4に値を足す。
左なら-4、2つ右なら+8なので
他の誤差拡散法も昨日のコピペ改変
画像ファイルを指定したPixelFormatのBitmapSourceで取得するのはいつものこれ
FormatConvertedBitmapをつかってPixelFormatを変換しているだけ
private BitmapSource GetBitmapSourceWithChangePixelFormat2(
string filePath, PixelFormat pixelFormat, double dpiX = 0, double dpiY = 0)
{
BitmapSource source = null;
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var bf = BitmapFrame.Create(fs);
var convertedBitmap = new FormatConvertedBitmap(bf, pixelFormat, null, 0);
int w = convertedBitmap.PixelWidth;
int h = convertedBitmap.PixelHeight;
int stride = (w * pixelFormat.BitsPerPixel + 7) / 8;
byte[] pixels = new byte[h * stride];
convertedBitmap.CopyPixels(pixels, stride, 0);
if (dpiX == 0) { dpiX = bf.DpiX; }
if (dpiY == 0) { dpiY = bf.DpiY; }
source = BitmapSource.Create(
w, h, dpiX, dpiY,
convertedBitmap.Format,
convertedBitmap.Palette, pixels, stride);
};
}
catch (Exception)
{
}
return source;
}
コード全部
こっちはログアウトしてからアクセスしたらログインを促された
自分しか見れないのかな、わかんね
gogowaten/
wpf_test: 20160611create
こっちは誰でも見れるみたいでログアウト状態でも見れた
GitHubを使ったのは今回が初めてのようなものなんだけど、アカウント自体は2年前に作っていたんだなあ
VisualStudioと連携できれば
ラクなんだけどなあ
実行ファイルも置いてみたけど.exeってあるのコワイw
関連記事
1ヶ月前
3日前
前回は1日前