How to multiply/upscale color depth? #1762
-
I'm struggling with multiplying pixel values. I have raw binary data that stores 3bit pixel values per byte (so values ranging from 0..7). In other words I want to change the I've tried simply altering the Here is the code sample: // This is just test data
var data = new byte[16] { 7, 0, 7, 0, // First row is alternating max 3 bit value <-> 0
7, 7, 7, 7, // Second row is all pixels at max 3 bit value
1, 3, 5, 7, // Third row is incrementing values
0, 0, 0, 0 }; // Fourth row is empty values
using var image = new MagickImage();
image.ReadPixels(
data: data,
settings: new PixelReadSettings(
width: 4,
height: 4,
storageType: StorageType.Char, // Char = byte
"R") // Red/Cyan/Gray = first channel
);
var pc = image.GetPixels();
foreach (var pixel in pc)
{
var pixelValue = pixel.GetChannel(0);
Console.WriteLine($"Pixel Raw of X{pixel.X} Y{pixel.Y} has value {pixelValue}");
}
// Red channel = gray scale channel (index 0). I don't know how else to create a 'pure' gray scale image.
var basePixelValue = MagickColor.FromRgb(32, 0, 0);
using var multiplyImage = new MagickImage(
width: 4,
height: 4,
color: basePixelValue
);
// This doesn't seem to work...
image.Composite(multiplyImage, CompositeOperator.Multiply);
var pc2 = image.GetPixels();
foreach (var pixel in pc2)
{
var pixelValue = pixel.GetChannel(0);
Console.WriteLine($"Pixel after Multiply of X{pixel.X} Y{pixel.Y} has value {pixelValue}");
}
File.WriteAllBytes("c:\\temp\\images\\test_image.tiff", image.ToByteArray(MagickFormat.Tiff)); The console output is: Pixel Raw of X0 Y0 has value 7
Pixel Raw of X1 Y0 has value 0
Pixel Raw of X2 Y0 has value 7
Pixel Raw of X3 Y0 has value 0
Pixel Raw of X0 Y1 has value 7
Pixel Raw of X1 Y1 has value 7
Pixel Raw of X2 Y1 has value 7
Pixel Raw of X3 Y1 has value 7
Pixel Raw of X0 Y2 has value 1
Pixel Raw of X1 Y2 has value 3
Pixel Raw of X2 Y2 has value 5
Pixel Raw of X3 Y2 has value 7
Pixel Raw of X0 Y3 has value 0
Pixel Raw of X1 Y3 has value 0
Pixel Raw of X2 Y3 has value 0
Pixel Raw of X3 Y3 has value 0
Pixel after Multiply of X0 Y0 has value 1
Pixel after Multiply of X1 Y0 has value 0
Pixel after Multiply of X2 Y0 has value 1
Pixel after Multiply of X3 Y0 has value 0
Pixel after Multiply of X0 Y1 has value 1
Pixel after Multiply of X1 Y1 has value 1
Pixel after Multiply of X2 Y1 has value 1
Pixel after Multiply of X3 Y1 has value 1
Pixel after Multiply of X0 Y2 has value 0
Pixel after Multiply of X1 Y2 has value 0
Pixel after Multiply of X2 Y2 has value 1
Pixel after Multiply of X3 Y2 has value 1
Pixel after Multiply of X0 Y3 has value 0
Pixel after Multiply of X1 Y3 has value 0
Pixel after Multiply of X2 Y3 has value 0
Pixel after Multiply of X3 Y3 has value 0 I'm using Can someone help me out? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You could use the p.s. The result of |
Beta Was this translation helpful? Give feedback.
You could use the
Fx
method ofMagickImage
for this. You can multiply the red channel like thisimage.Fx("r*32");
p.s. The result of
GetPixels
isIDisposable
so you should add a using:using var pc = image.GetPixels();