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 hat.buffer;
26
27 import optkl.ifacemapper.Buffer;
28 import optkl.ifacemapper.MappableIface;
29
30 import java.awt.image.BufferedImage;
31 import java.awt.image.DataBuffer;
32 import java.awt.image.DataBufferByte;
33 import java.awt.image.DataBufferFloat;
34 import java.awt.image.DataBufferInt;
35 import java.awt.image.DataBufferUShort;
36 import java.lang.foreign.MemorySegment;
37
38 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
39 import static java.lang.foreign.ValueLayout.JAVA_FLOAT;
40 import static java.lang.foreign.ValueLayout.JAVA_INT;
41 import static java.lang.foreign.ValueLayout.JAVA_SHORT;
42
43 public interface ImageIfaceBuffer<T extends ImageIfaceBuffer<?>> extends Buffer {
44 @SuppressWarnings("unchecked")
45 default T syncFromRasterDataBuffer(DataBuffer dataBuffer) { // int[], byte[], short[], float[]
46 switch (dataBuffer) {
47 case DataBufferInt arr ->
48 MemorySegment.copy(arr.getData(), 0, MappableIface.getMemorySegment(this), JAVA_INT, 16L, arr.getData().length);
49 case DataBufferByte arr ->
50 MemorySegment.copy(arr.getData(), 0, MappableIface.getMemorySegment(this), JAVA_BYTE, 16L, arr.getData().length);
51 case DataBufferUShort arr ->
52 MemorySegment.copy(arr.getData(), 0, MappableIface.getMemorySegment(this), JAVA_SHORT, 16L, arr.getData().length);
53 case DataBufferFloat arr ->
54 MemorySegment.copy(arr.getData(), 0, MappableIface.getMemorySegment(this), JAVA_FLOAT, 16L, arr.getData().length);
55 default -> throw new IllegalStateException("Unexpected value: " + dataBuffer);
56 }
57 return (T)this;
58 }
59
60 default T syncFromRaster(BufferedImage bufferedImage) { // int[], byte[], short[], float[]
61 return syncFromRasterDataBuffer(bufferedImage.getRaster().getDataBuffer());
62 }
63
64 @SuppressWarnings("unchecked")
65 default T syncToRasterDataBuffer(DataBuffer dataBuffer) { // int[], byte[], short[], float[]
66 switch (dataBuffer) {
67 case DataBufferUShort arr ->
68 MemorySegment.copy(MappableIface.getMemorySegment(this), JAVA_SHORT, 16L, arr.getData(), 0, arr.getData().length);
69 case DataBufferInt arr ->
70 MemorySegment.copy(MappableIface.getMemorySegment(this), JAVA_INT, 16L, arr.getData(), 0, arr.getData().length);
71 case DataBufferByte arr ->
72 MemorySegment.copy(MappableIface.getMemorySegment(this), JAVA_BYTE, 16L, arr.getData(), 0, arr.getData().length);
73 case DataBufferFloat arr ->
74 MemorySegment.copy(MappableIface.getMemorySegment(this), JAVA_FLOAT, 16L, arr.getData(), 0, arr.getData().length);
75 default -> throw new IllegalStateException("Unexpected value: " + dataBuffer);
76 }
77 return (T) this;
78 }
79
80 default T syncToRaster(BufferedImage bufferedImage) { // int[], byte[], short[], float[]
81 return syncToRasterDataBuffer(bufferedImage.getRaster().getDataBuffer());
82 }
83 }