Problem Computing Dot Product with mir
Kyle Ingraham
kyle at kyleingraham.com
Tue Feb 23 02:59:10 UTC 2021
On Monday, 22 February 2021 at 07:14:26 UTC, 9il wrote:
> On Sunday, 21 February 2021 at 16:18:05 UTC, Kyle Ingraham
> wrote:
>> I am trying to convert sRGB pixel values to XYZ with mir using
>> the following guide:
>> http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
>>
>> [...]
>
> mir-glas is deprecated experimental project. It is worth use
> mir-blas or lubeck instead. There is also naming issue. In the
> classic BLAS naming dot refers a function that accept two 1D
> vectors.
Your suggestion worked beautifully. I went with lubeck. I don't
know how I missed it seeing that it is right there in the Dlang
tour.
Thank you for writing such useful libraries.
Final code ended up being:
import std.stdio;
import kaleidic.lubeck: mtimes;
import mir.ndslice: as, byDim, fuse, map, sliced;
void main()
{
auto rgbToXyz = [0.4124564, 0.3575761, 0.1804375,
0.2126729, 0.7151522, 0.0721750,
0.0193339, 0.1191920, 0.9503041]
.as!double
.sliced(3, 3);
ubyte[] rgbPixels = [255, 0, 0,
0, 255, 0,
0, 0, 255,
120, 120, 120];
auto xyzPixels = rgbPixels
// convert input array elements to double - lazy
.as!double
// create a slice-shell over the input array
.sliced(2, 2, 3)
// normalize pixel channels to range [0, 1]
.map!(chnl => chnl / 255.0)
// sRGB inverse compand
.map!(chnl => chnl <= 0.04045 ? chnl / 12.92 : ((chnl + 0.055)
/ 1.055) ^^ 2.4)
// linear RGB to XYZ
// iterator by x and y over pixels (3rd dimension)
.byDim!(0, 1) // same as .pack!1
// dot product of each pixel with conversion matrix
.map!(pixel => mtimes(rgbToXyz, pixel))
// join iterator values into a matrix
.fuse;
xyzPixels.writeln;
xyzPixels.shape.writeln;
}
More information about the Digitalmars-d-learn
mailing list