LayerHashCalculator.GetBlendingHash
LayerHashCalculator.GetBlendingHash method
Mendapat campuran hash.
public int GetBlendingHash()
Nilai Pengembalian
Hash unik untuk Layer Blending Options
Contoh
Kode berikut mendemonstrasikan API untuk mendapatkan hash unik untuk lapisan serupa di file berbeda.
[C#]
/// <summary>
/// Mendapatkan nama layer dengan.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gambar">Gambar.</param>
/// <nama param="nama">Nama.</param>
/// <returns></returns>
private static T GetLayerByName<T>(PsdImage image, string name) where T : Layer
{
var layers = image.Layers;
foreach (var layer in layers)
{
if (layer.Name == name)
{
return (T) layer;
}
}
return null;
}
/// <summary>
/// Apakah tidak sama.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expected">Yang diharapkan.</param>
/// <param name="aktual">Aktual.</param>
/// <exception cref="System.Exception">Argumen tidak boleh sama</exception>
public static void AreNotEqual<T>(T expected, T actual)
{
if (expected != null && expected.Equals(actual))
{
throw new Exception("Arguments must not be equal");
}
}
/// <summary>
/// Apakah sama.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expected">Yang diharapkan.</param>
/// <param name="aktual">Aktual.</param>
/// <exception cref="System.Exception">Argumen harus sama</exception>
public static void AreEqual<T>(T expected, T actual)
{
if (expected != null && !expected.Equals(actual))
{
throw new Exception("Arguments must be equal");
}
}
/// <summary>
/// Reguler uji hash konten lapisan.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void RegularLayerContentHashTest(string fileName)
{
using (var im = (PsdImage) Image.Load(fileName))
{
var layers = new Layer[9];
var hashers = new LayerHashCalculator[9];
for (int i = 0; i < layers.Length; i++)
{
layers[i] = GetLayerByName<Layer>(im, string.Format("Layer {0}", i + 1));
hashers[i] = new LayerHashCalculator(layers[i]);
}
AreNotEqual(hashers[0].GetChannelsHash(), hashers[1].GetChannelsHash());
AreNotEqual(hashers[1].GetChannelsHash(), hashers[2].GetChannelsHash());
AreNotEqual(hashers[0].GetChannelsHash(), hashers[2].GetChannelsHash());
AreNotEqual(hashers[5].GetChannelsHash(), hashers[7].GetChannelsHash());
AreNotEqual(hashers[0].GetChannelsHash(), hashers[8].GetChannelsHash());
// Hash dari layer ini sama
AreEqual(hashers[0].GetChannelsHash(), hashers[3].GetChannelsHash());
AreEqual(hashers[1].GetChannelsHash(), hashers[4].GetChannelsHash());
AreEqual(hashers[0].GetChannelsHash(), hashers[6].GetChannelsHash());
// Periksa hash mode pencampuran
AreEqual(hashers[0].GetBlendingHash(), hashers[3].GetBlendingHash());
AreEqual(hashers[1].GetBlendingHash(), hashers[4].GetBlendingHash());
AreNotEqual(hashers[0].GetBlendingHash(), hashers[6].GetBlendingHash());
// Tapi pointer berbeda
AreNotEqual(layers[0], layers[3]);
AreNotEqual(layers[1], layers[4]);
AreNotEqual(layers[0], layers[6]);
}
}
/// <summary>
/// Mengisi tes hash konten lapisan.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void FillLayerContentHashTest(string fileName)
{
using (var im = (PsdImage) Image.Load(fileName))
{
var fillLayersNames = new string[] { "Color Fill", "Gradient Fill", "Pattern Fill" };
var colorFillLayers = new Layer[4];
var colorFillHashers = new LayerHashCalculator[4];
for (int fillLayerIndex = 0; fillLayerIndex < fillLayersNames.Length; fillLayerIndex++)
{
for (int i = 0; i < 2; i++)
{
var index = 0 + i * 2;
colorFillLayers[index] = GetLayerByName<Layer>(im,
string.Format("{0} 1_{1}", fillLayersNames[fillLayerIndex], i + 1));
colorFillHashers[index] = new LayerHashCalculator(colorFillLayers[index]);
index = 1 + i * 2;
colorFillLayers[index] = GetLayerByName<Layer>(im,
string.Format("{0} 2_{1}", fillLayersNames[fillLayerIndex], i + 1));
colorFillHashers[index] = new LayerHashCalculator(colorFillLayers[index]);
}
// Lapisan serupa selalu ada dalam satu indeks
AreEqual(colorFillHashers[0].GetContentHash(), colorFillHashers[2].GetContentHash());
AreEqual(colorFillHashers[1].GetContentHash(), colorFillHashers[3].GetContentHash());
AreNotEqual(colorFillHashers[0].GetContentHash(), colorFillHashers[1].GetContentHash());
}
}
}
/// <summary>
/// Mencerdaskan uji hash konten lapisan objek.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void SmartObjectLayerContentHashTest(string fileName)
{
using (var im = (PsdImage) Image.Load(fileName))
{
var smartObjects = new Layer[]
{
GetLayerByName<Layer>(im, "Regular1_1"),
GetLayerByName<Layer>(im, "Regular1_2"),
GetLayerByName<Layer>(im, "Regular2_1"),
GetLayerByName<Layer>(im, "Regular2_2"),
GetLayerByName<Layer>(im, "Smart1_1"),
GetLayerByName<Layer>(im, "Smart1_2"),
GetLayerByName<Layer>(im, "Smart2_1"),
GetLayerByName<Layer>(im, "Smart2_2"),
};
var hashers = new LayerHashCalculator[smartObjects.Length];
for (int i = 0; i < smartObjects.Length; i++)
{
hashers[i] = new LayerHashCalculator(smartObjects[i]);
}
// Data saluran sama untuk Layer dan Createad dari mereka Smart Objects.
AreEqual(hashers[0].GetChannelsHash(), hashers[2].GetChannelsHash());
AreEqual(hashers[0].GetChannelsHash(), hashers[4].GetChannelsHash());
// Content Hash berbeda, karena Smart Object menggunakan data lain sebagai konten
AreNotEqual(hashers[0].GetContentHash(), hashers[4].GetContentHash());
// Tapi pencampuran hash serupa. Kedua layer - smart dan regular memiliki mode Normal Blend dan opacity 255
AreEqual(hashers[0].GetBlendingHash(), hashers[4].GetBlendingHash());
// Data saluran sama untuk Layer dan Createad dari mereka Smart Objects.
AreEqual(hashers[1].GetChannelsHash(), hashers[3].GetChannelsHash());
AreEqual(hashers[1].GetChannelsHash(), hashers[5].GetChannelsHash());
// Content Hash berbeda, karena Smart Object menggunakan data lain sebagai konten
AreNotEqual(hashers[1].GetContentHash(), hashers[5].GetContentHash());
// Tapi pencampuran hash serupa. Kedua layer - smart dan regular memiliki mode Normal Blend dan opacity 255
AreEqual(hashers[1].GetBlendingHash(), hashers[5].GetBlendingHash());
AreNotEqual(hashers[0].GetChannelsHash(), hashers[1].GetChannelsHash());
AreNotEqual(hashers[2].GetChannelsHash(), hashers[3].GetChannelsHash());
AreNotEqual(hashers[4].GetChannelsHash(), hashers[5].GetChannelsHash());
}
}
/// <summary>
/// Penyesuaian uji hash konten lapisan.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void AdjustmentLayersContentHashTest(string fileName)
{
using (var im = (PsdImage) Image.Load(fileName))
{
var adjustments = new Layer[]
{
GetLayerByName<Layer>(im, "Brightness/Contrast 1"),
GetLayerByName<Layer>(im, "Levels 1"),
GetLayerByName<Layer>(im, "Curves 1"),
GetLayerByName<Layer>(im, "Exposure 1"),
GetLayerByName<Layer>(im, "Vibrance 1"),
GetLayerByName<Layer>(im, "Hue/Saturation 1"),
GetLayerByName<Layer>(im, "Color Balance 1"),
GetLayerByName<Layer>(im, "Black & White 1"),
GetLayerByName<Layer>(im, "Photo Filter 1"),
GetLayerByName<Layer>(im, "Channel Mixer 1"),
GetLayerByName<Layer>(im, "Invert 1"),
GetLayerByName<Layer>(im, "Posterize 1"),
};
var length = adjustments.Length;
var hashers = new LayerHashCalculator[length];
for (int i = 0; i < length; i++)
{
hashers[i] = new LayerHashCalculator(adjustments[i]);
}
// Semua hash harus berbeda
for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
{
AreNotEqual(hashers[i].GetContentHash(), hashers[j].GetContentHash());
AreEqual(hashers[i].GetBlendingHash(), hashers[j].GetBlendingHash());
}
}
}
}
/// <summary>
/// Mengirim teks uji hash konten lapisan.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void TextLayersContentHashTest(string fileName)
{
using (var im = (PsdImage) Image.Load(fileName))
{
var textLayers1 = new TextLayer[]
{
GetLayerByName<TextLayer>(im, "Text 1"),
GetLayerByName<TextLayer>(im, "Text 1 Similar"),
GetLayerByName<TextLayer>(im, "Text 1 Changed"),
};
var textLayers2 = new TextLayer[]
{
GetLayerByName<TextLayer>(im, "Text 2"),
GetLayerByName<TextLayer>(im, "Text 2 Similar"),
GetLayerByName<TextLayer>(im, "Text 2 Changed 1"),
GetLayerByName<TextLayer>(im, "Text 2 Changed 2"),
GetLayerByName<TextLayer>(im, "Text 2 Rotated"),
};
var textHashers1 = new LayerHashCalculator[textLayers1.Length];
var textHashers2 = new LayerHashCalculator[textLayers2.Length];
for (int i = 0; i < textLayers1.Length; i++)
{
textHashers1[i] = new LayerHashCalculator(textLayers1[i]);
}
for (int i = 0; i < textLayers2.Length; i++)
{
textHashers2[i] = new LayerHashCalculator(textLayers2[i]);
}
AreEqual(textHashers1[0].GetContentHash(), textHashers1[1].GetContentHash());
AreNotEqual(textHashers1[0].GetContentHash(), textHashers1[2].GetContentHash());
AreEqual(textHashers2[0].GetContentHash(), textHashers2[1].GetContentHash());
AreNotEqual(textHashers2[0].GetContentHash(), textHashers2[2].GetContentHash());
AreNotEqual(textHashers2[0].GetContentHash(), textHashers2[3].GetContentHash());
// Matriks transformasi tidak digunakan dalam perhitungan hash. Anda juga harus memeriksanya
AreEqual(textHashers2[0].GetContentHash(), textHashers2[4].GetContentHash());
// Dalam hal ini kita memiliki rotasi dalam matriks
AreNotEqual(textLayers2[0].TransformMatrix, textLayers2[4].TransformMatrix);
// Dalam hal ini kami hanya memiliki terjemahan (Lapisan Teks Bergeser di bawah)
AreNotEqual(textLayers2[0].TransformMatrix, textLayers2[1].TransformMatrix);
}
}
/// <summary>
/// Mengelompokkan uji hash konten lapisan.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void GroupLayerContentHashTest(string fileName)
{
using (var im = (PsdImage) Image.Load(fileName))
{
var fillLayersNames = new string[] { "Color Fill", "Gradient Fill", "Pattern Fill" };
var groupLayers = new Layer[2];
var groupLayersHashers = new LayerHashCalculator[2];
groupLayers[0] = GetLayerByName<Layer>(im, "Fill");
groupLayers[1] = GetLayerByName<Layer>(im, "Fill copy");
for (int i = 0; i < groupLayers.Length; i++)
{
groupLayersHashers[i] = new LayerHashCalculator(groupLayers[i]);
}
// Group Layer Hash dihitung dari layer di dalamnya
AreEqual(groupLayersHashers[0].GetContentHash(), groupLayersHashers[1].GetContentHash());
AreNotEqual(groupLayers[0], groupLayers[1]);
}
}
/// <summary>
/// Reguler konten lapisan dari file yang berbeda tes hash.
/// </summary>
/// <param name="fileName">Nama file.</param>
public static void RegularLayerContentFromDifferentFilesHashTest(string fileName, string outputFile)
{
using (var im = (PsdImage) Image.Load(fileName, new PsdLoadOptions() { ReadOnlyMode = true }))
{
im.Save(outputFile);
}
using (var im = (PsdImage) Image.Load(fileName))
{
using (var imCopied = (PsdImage) Image.Load(outputFile))
{
for (int i = 0; i < im.Layers.Length; i++)
{
var layer = im.Layers[i];
var layer_copied = imCopied.Layers[i];
var hashCalc = new LayerHashCalculator(layer);
var hashCalc_copied = new LayerHashCalculator(layer_copied);
// Layer memiliki pointer yang berbeda
AreNotEqual(layer, layer_copied);
// Tapi hash dari layer adalah sama
AreEqual(hashCalc.GetChannelsHash(), hashCalc_copied.GetChannelsHash());
AreEqual(hashCalc.GetContentHash(), hashCalc_copied.GetContentHash());
}
}
}
File.Delete(outputFile);
}
Lihat juga
- class LayerHashCalculator
- ruang nama Aspose.PSD.FileFormats.Psd.Layers
- perakitan Aspose.PSD