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