|
| 1 | +module ImageUtils { |
| 2 | + public use Image; |
| 3 | + |
| 4 | + /* Controls whether an image is printed */ |
| 5 | + config const image = false; |
| 6 | + /* A factor to scale the image by */ |
| 7 | + config const imageScale = 1; |
| 8 | + /* The framerate of the video */ |
| 9 | + config const framerate = 10; |
| 10 | + /* The height of the image, only applicable for 1D input data */ |
| 11 | + config const imageHeight = 256; |
| 12 | + |
| 13 | + private proc checkDimsHelper(width: int, height: int) { |
| 14 | + if height < 64 then |
| 15 | + halt("Image of minimum height 64 is required, got ", height); |
| 16 | + if width > 4000 then |
| 17 | + halt("Image of maximum width 4000 is required, got ", width); |
| 18 | + } |
| 19 | + private proc checkDims(data: [?d]) where d.isRectangular() && d.rank == 2 do |
| 20 | + checkDimsHelper(data.dim(1).size, data.dim(0).size); |
| 21 | + private proc checkDims(data: [?d]) where d.isRectangular() && d.rank == 1 do |
| 22 | + checkDimsHelper(d.size, imageHeight); |
| 23 | + |
| 24 | + |
| 25 | + private proc processFrameData(data: [?d]) where d.isRectangular() && d.rank == 1 { |
| 26 | + // make the 1d data into 2d data of a specific height |
| 27 | + var data2d: [0..#imageHeight, d.lowBound..d.highBound] data.eltType; |
| 28 | + for i in 0..#imageHeight do |
| 29 | + data2d[i,..] = data; |
| 30 | + |
| 31 | + return processFrameData(data2d); |
| 32 | + } |
| 33 | + private proc processFrameData(data: [?d]) where d.isRectangular() && d.rank == 2 { |
| 34 | + // create a white border around the image and turn raw data into pixels |
| 35 | + var image: [data.domain.expand(1)] int = 0xFFFFFF; |
| 36 | + image[data.domain] = interpolateColor(data, 0x000000, 0x0000FF); |
| 37 | + |
| 38 | + return scale(image, imageScale); |
| 39 | + } |
| 40 | + |
| 41 | + proc writeImage(u: []) throws { |
| 42 | + if !image then return; |
| 43 | + |
| 44 | + checkDims(u); |
| 45 | + |
| 46 | + @functionStatic |
| 47 | + ref pipe = try! new mediaPipe("heat.mp4", imageType.bmp, framerate); |
| 48 | + |
| 49 | + pipe.writeFrame(processFrameData(u)); |
| 50 | + } |
| 51 | +} |
0 commit comments