get a function that maps index-coordinates of image0-coordinate-space to the index-coordinates of image1-coordinate-space.
note that if you're mapping lots of indexes using Array.map, it would be nearly 40-times faster to use the lambdacalc.vectorize1 function instead.
Array.map
// suppose you have an RGBA image data buffer of `width = 100`, `height = 50`, `channels = 4`,// and you have an array of 6 pixel indexes: `idx0 = [1040, 1044, 1048, 1140, 1144, 1148]`,// and now, you want to convert these indexes to that of an "LA" image data buffer of:// `width = 10`, `height = 10`, and `channels = 2`, `x = 5`, `y = 10`// then:const coords0 = {x: 0, y: 0, width: 100, height: 50, channels: 4 as const}, coords1 = {x: 5, y: 10, width: 10, height: 10, channels: 2 as const}, coords0_to_coords1 = coordinateTransformer(coords0, coords1), idx0 = [4040, 4044, 4048, 4440, 4444, 4448], idx1 = idx0.map(coords0_to_coords1) // [10, 12, 14, 30, 32, 34] Copy
// suppose you have an RGBA image data buffer of `width = 100`, `height = 50`, `channels = 4`,// and you have an array of 6 pixel indexes: `idx0 = [1040, 1044, 1048, 1140, 1144, 1148]`,// and now, you want to convert these indexes to that of an "LA" image data buffer of:// `width = 10`, `height = 10`, and `channels = 2`, `x = 5`, `y = 10`// then:const coords0 = {x: 0, y: 0, width: 100, height: 50, channels: 4 as const}, coords1 = {x: 5, y: 10, width: 10, height: 10, channels: 2 as const}, coords0_to_coords1 = coordinateTransformer(coords0, coords1), idx0 = [4040, 4044, 4048, 4440, 4444, 4448], idx1 = idx0.map(coords0_to_coords1) // [10, 12, 14, 30, 32, 34]
the equation for mask_intervals can be easily derived as follows:
mask_intervals
p0 = px of data
y0 = y-coords of pixel in data
x0 = x-coords of pixel in data
w0 = width of data
c0 = channels of data
p1 = px of mask
y1 = y-coords of pixel in mask
x1 = x-coords of pixel in mask
w1 = width of mask
c1 = channels of mask
y = y-coords of mask's rect
x = x-coords of mask's rect
declare let [w0, w1, c0, c1]: number[]let p0 = (x0 + y0 * w0) * c0, x0 = (p0 / c0) % w0, y0 = trunc(p0 / (c0 * w0)), p1 = (x1 + y1 * w1) * c1, x1 = (p1 / c1) % w1, y1 = trunc(p1 / (c1 * w1)), x = x0 - x1, y = y0 - y1// so, now:p1 = c1 * (x1 + y1 * w1)p1 = c1 * ((x0 - x) + (y0 - y) * w1)p1 = c1 * ((((p0 / c0) % w0) - x) + (((p0 / c0) / w0 | 0) - y) * w1) Copy
declare let [w0, w1, c0, c1]: number[]let p0 = (x0 + y0 * w0) * c0, x0 = (p0 / c0) % w0, y0 = trunc(p0 / (c0 * w0)), p1 = (x1 + y1 * w1) * c1, x1 = (p1 / c1) % w1, y1 = trunc(p1 / (c1 * w1)), x = x0 - x1, y = y0 - y1// so, now:p1 = c1 * (x1 + y1 * w1)p1 = c1 * ((x0 - x) + (y0 - y) * w1)p1 = c1 * ((((p0 / c0) % w0) - x) + (((p0 / c0) / w0 | 0) - y) * w1)
(i0: number & coord0) => i1 as number & coord1 a function that takes in an integer index from coordinate space coords0 and converts it so that it's relative to coordinate space coords1
(i0: number & coord0) => i1 as number & coord1
get a function that maps index-coordinates of image0-coordinate-space to the index-coordinates of image1-coordinate-space.
note that if you're mapping lots of indexes using
Array.map
, it would be nearly 40-times faster to use the lambdacalc.vectorize1 function instead.Example
Derivation
the equation for
mask_intervals
can be easily derived as follows:p0 = px of data
,y0 = y-coords of pixel in data
,x0 = x-coords of pixel in data
,w0 = width of data
,c0 = channels of data
p1 = px of mask
,y1 = y-coords of pixel in mask
,x1 = x-coords of pixel in mask
,w1 = width of mask
,c1 = channels of mask
y = y-coords of mask's rect
,x = x-coords of mask's rect