# For legacy code, is there a better patch for RasterArray than Raster?

Notice the time calculation around the graphics in the code below. That is taking 6 seconds which seems like a problem.

(* for 3-tuple from list of eight:  8x 8x 8 = 512; + 20 padding = 532 \
*)

f = 2; (* number of images *)

name = Table[i, {i, 1, f}];

For[loop = 1, loop <= f, loop++,

bckrow =.;
bckcol =.;
sum =.;

bckrow = Table[{x, y}, {x, 1, 532}, {y, 1, 532}];
bckcol = Table[{x, y}, {x, 1, 532}, {y, 1, 532}];

bckrow[[All, All]] = {0.0, 0.0, 0.0};
bckcol[[All, All]] = {0.0, 0.0, 0.0};
tup = Tuples[{
Random[Real, {(-1.0 + (loop/400)), -0.25}, 4],
Random[Real, {(-1.0 + (loop/400)), -0.25}, 4],
Random[Real, {(0 + (loop/100)), 1.0}, 4],
Random[Real, {(0 + (loop/100)), 1.0}, 4],
Random[Real, {(1.0 - (loop/100)), 0.0}, 4],
Random[Real, {(1.0 - (loop/100)), 0.0}, 4],
Random[Real, {0.25, (1.0 - (loop/400))}, 4],
Random[Real, {0.25, (1.0 - (loop/400))}, 4]},
3];

For[j = 1, j <= 512, j++, bckcol[[All, j]] = tup[[j]]];
For[i = 1, i <= 512, i++, bckrow[[i, All]] = tup[[i]]];

sum = bckrow + bckcol;

For[i = 1, i <= 532, i++, For[j = 1, j <= 532, j++,
If[((0 > sum[[i, j, 1]]) || (sum[[i, j, 1]] > 1) ||
(0 > sum[[i, j, 2]]) || (sum[[i, j, 2]] > 1) ||
(0 > sum[[i, j, 3]]) || (sum[[i, j, 3]] > 1)),
( sum[[i, j, 1]] = sum[[i, j, 2]] = sum[[i, j, 3]] = 0)]]];

t1 = AbsoluteTime[];
Print[Graphics[Raster[sum], ImageSize -> {532, 532}]] ;
t2 = AbsoluteTime[];
t = t2 - t1;
Print[t]
] (* ] = First For in program *)
$$$$

• I don't think your problem is Raster, but the visualization of the results. If you remove the Print statement from Print[Graphics[...]], and use RepeatedTiming on the whole loop, the entire loop only takes 3.1 s. If you add back the Print to show the images, and use AbsoluteTiming on the whole loop, it takes 13 seconds. I think producing the visual output is the most time consuming part here. Mar 22, 2021 at 15:48
• OK, so we have it down to the Print statement. Why would printing a 532 x 532 RGB image take so long? I frequently run 1000 x 1000 in other programs and do not notice a delay when I use MatrixPlot. Mar 22, 2021 at 15:57
• Have you tried Image instead of Raster? Mar 22, 2021 at 22:27
• Image works if I get rid of Table. Using RandomReal[{0, 0}, {532, 532, 3}] strangely helps speed. Mar 22, 2021 at 23:02

Sorry to answer my own question, but v6 to v12 is a complete rewrite, and it runs quickly:


bckrow =.;
bckcol =.;
sum =.;

bckrow = RandomReal[{0, 0}, {532, 532, 3}];
bckcol = RandomReal[{0, 0}, {532, 532, 3}];

tup = Tuples[{
Random[Real, {(-1.0 + (loop/400)), -0.25}, 4],
Random[Real, {(-1.0 + (loop/400)), -0.25}, 4],
Random[Real, {(0 + (loop/100)), 1.0}, 4],
Random[Real, {(0 + (loop/100)), 1.0}, 4],
Random[Real, {(1.0 - (loop/100)), 0.0}, 4],
Random[Real, {(1.0 - (loop/100)), 0.0}, 4],
Random[Real, {0.25, (1.0 - (loop/400))}, 4],
Random[Real, {0.25, (1.0 - (loop/400))}, 4]},
3];

For[j = 1, j <= 512, j++, bckcol[[All, j]] = tup[[j]]];
For[i = 1, i <= 512, i++, bckrow[[i, All]] = tup[[i]]];

sum = bckrow + bckcol;

For[i = 1, i <= 532, i++, For[j = 1, j <= 532, j++,
If[((0 > sum[[i, j, 1]]) || (sum[[i, j, 1]] > 1) ||
(0 > sum[[i, j, 2]]) || (sum[[i, j, 2]] > 1) ||
(0 > sum[[i, j, 3]]) || (sum[[i, j, 3]] > 1)),
( sum[[i, j, 1]] = sum[[i, j, 2]] = sum[[i, j, 3]] = 0)]]];

ImageRotate[Image[sum], 90 \[Degree]]


An alternative approach without For loops:

SeedRandom[1]
loop = 1;
{range1, range2} = {{1/4, 1 - loop/400}, {loop/100, 1}};

tupB = Tuples[Random[Real, #, 4] & /@ {- Reverse @ range1, - Reverse @ range1, range2,
range2, 1 - range2, 1 - range2, range1, range1}, 3];

sumB = ConstantArray[0, {532, 532, 3}];

sumB[[;; Length @ tupB, All]] = tupB;

sumB = Reverse[sumB + Transpose[sumB]];

sumB = Map[Boole[And @@ (0 <= # <= 1 & /@ #)] # &, sumB, {-2}];

Image[sumB]
`