1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package shade;
26 
27 import hat.buffer.F32Array;
28 import optkl.util.carriers.ArenaAndLookupCarrier;
29 import shade.types.vec4;
30 
31 import java.awt.Transparency;
32 import java.awt.color.ColorSpace;
33 import java.awt.image.BufferedImage;
34 import java.awt.image.ColorModel;
35 import java.awt.image.ComponentColorModel;
36 import java.awt.image.DataBuffer;
37 import java.awt.image.DataBufferFloat;
38 import java.awt.image.PixelInterleavedSampleModel;
39 import java.awt.image.Raster;
40 import java.awt.image.SampleModel;
41 import java.awt.image.WritableRaster;
42 
43 record FloatImage(
44         int width,
45         int height,
46         int widthXHeight,
47         int channels,
48         ColorSpace colorSpace,
49         ColorModel colorModel,
50         SampleModel sampleModel,
51         DataBufferFloat dataBufferFloat,
52         float[] data,
53         WritableRaster raster,
54         BufferedImage bufferedImage,
55         F32Array f32Array
56 ) {
57     public static FloatImage of(ArenaAndLookupCarrier arenaAndLookupCarrier, int width, int height) {
58         // We need an RGB colorspace.
59         ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
60 
61         // Create the Color Model. 32 bits per component, no alpha, non-premultiplied
62         ColorModel colorModel = new ComponentColorModel(colorSpace, false, false,
63                 Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
64 
65         int channels = width*height*3;
66         // Create the Sample Model (Pixel Interleaved) bands for RGB, scanline stride is width * 3
67         SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT,
68                 width, height, 3, width * 3, new int[]{0, 1, 2});
69 
70         // Create the DataBuffer (an actual heap allocated  float array)
71         DataBufferFloat dataBufferFloat = new DataBufferFloat(width * height * 3);
72 
73         // Get the float pixels
74 
75         float[] data = dataBufferFloat.getData();
76 
77         // Create the Raster
78         WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBufferFloat, null);
79 
80         BufferedImage bufferedImage = new BufferedImage(colorModel, raster, false, null);
81 
82         F32Array f32Array = F32Array.create(arenaAndLookupCarrier, channels);
83 
84         return new FloatImage(width,height,width*height,channels,
85                 colorSpace, colorModel, sampleModel, dataBufferFloat, data, raster, bufferedImage, f32Array);
86     }
87 
88     public void set(int i,vec4 outFragColor) {
89         data[i * 3 + 0] = outFragColor.x();
90         data[i * 3 + 1] = outFragColor.y();
91         data[i * 3 + 2] = outFragColor.z();
92     }
93 
94     public void sync() {
95         f32Array.copyFrom(data);
96     }
97 }