C
C#3y ago
LukeZurg22

Replacing A Specific Colour of A WriteableBitmap [Answered]

Good morning, I am currently working on a method that is meant to search a WriteableBitmap image for a colour and replace it.
2 Replies
LukeZurg22
LukeZurg22OP3y ago
Below is the method in question that i'm working with. How this works is that the provinceColor is found in the image, and from provinceColor I extrapolate a new countryColor which is meant to be redrawn over a countryMap. The countryMap/outputMap != the provinceMap/givenMap..
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
var pixels = (uint*)givenMap.BackBuffer;
var overWrittenMapPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = pixels[index];

if (rawPixel == provinceColor)
{
pixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Lock();

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = overWrittenMapPixels[index];

if (rawPixel == newColor)
{
overWrittenMapPixels[index] = newColor;
}

});

overWrittenMap.Unlock();
return overWrittenMap;
}
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
var pixels = (uint*)givenMap.BackBuffer;
var overWrittenMapPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = pixels[index];

if (rawPixel == provinceColor)
{
pixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Lock();

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = overWrittenMapPixels[index];

if (rawPixel == newColor)
{
overWrittenMapPixels[index] = newColor;
}

});

overWrittenMap.Unlock();
return overWrittenMap;
}
Problem solved. I was not conscious of the pixels being used. The method below works perfectly fine.
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
overWrittenMap.Lock();
var givenPixels = (uint*)givenMap.BackBuffer;
var overWrittenPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = givenPixels[index];

if (rawPixel == provinceColor)
{
overWrittenPixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Unlock();

return overWrittenMap;
}
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
overWrittenMap.Lock();
var givenPixels = (uint*)givenMap.BackBuffer;
var overWrittenPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = givenPixels[index];

if (rawPixel == provinceColor)
{
overWrittenPixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Unlock();

return overWrittenMap;
}
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server