|
| 1 | +package zarr_implementations.jzarr; |
| 2 | + |
| 3 | +import com.bc.zarr.ArrayParams; |
| 4 | +import com.bc.zarr.CompressorFactory; |
| 5 | +import com.bc.zarr.DataType; |
| 6 | +import com.bc.zarr.ZarrArray; |
| 7 | +import com.bc.zarr.ZarrGroup; |
| 8 | + |
| 9 | +import javax.imageio.ImageIO; |
| 10 | +import java.awt.*; |
| 11 | +import java.awt.image.BufferedImage; |
| 12 | +import java.awt.image.DataBuffer; |
| 13 | +import java.awt.image.DataBufferByte; |
| 14 | +import java.awt.image.DataBufferInt; |
| 15 | +import java.awt.image.WritableRaster; |
| 16 | +import java.io.ByteArrayOutputStream; |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.stream.IntStream; |
| 24 | + |
| 25 | + |
| 26 | +public class App { |
| 27 | + |
| 28 | + enum Compression { |
| 29 | + raw("null"), |
| 30 | + zlib("zlib"), |
| 31 | + blosc("blosc"); |
| 32 | + |
| 33 | + private final String value; |
| 34 | + |
| 35 | + private Compression(final String value) { |
| 36 | + this.value = value; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String toString() { |
| 41 | + return value; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // NOTE for now we use 100, 100, 1 as block-size in all examples |
| 46 | + // maybe it's a better idea to make this more irregular though |
| 47 | + private static final int WIDTH = 512; |
| 48 | + private static final int HEIGHT = 512; |
| 49 | + private static final int CHANNELS = 3; |
| 50 | + private static final int[] CHUNKS = new int[]{100, 100, 1}; |
| 51 | + private static final int[] SHAPE = new int[] {WIDTH, HEIGHT, CHANNELS}; |
| 52 | + private static final Path IN_PATH = Paths.get("..", "..", "data", "reference_image.png"); |
| 53 | + private static final Path OUT_PATH = Paths.get("..", "..", "data", "jzarr_flat.zr"); |
| 54 | + |
| 55 | + private static int[] getTestData() throws IOException { |
| 56 | + final BufferedImage image = ImageIO.read(new File(IN_PATH.toString())); |
| 57 | + int[] result = new int[WIDTH * HEIGHT * CHANNELS]; |
| 58 | + for (int i = 0; i < WIDTH; i++) { |
| 59 | + for (int j = 0; j < HEIGHT; j++) { |
| 60 | + Color color = new Color(image.getRGB(i, j)); |
| 61 | + int index = (WIDTH*3*j) + (3*i); |
| 62 | + result[index + 0] = color.getRed(); |
| 63 | + result[index + 1] = color.getGreen(); |
| 64 | + result[index + 2] = color.getBlue(); |
| 65 | + } |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + private static int[] getArrayData(ZarrArray zarr) throws Exception { |
| 72 | + int[] data = new int[WIDTH * HEIGHT * CHANNELS]; |
| 73 | + zarr.read(data, SHAPE, new int[]{0, 0, 0}); |
| 74 | + int[] unsigned = new int[data.length]; |
| 75 | + for (int i = 0; i < data.length; i++) { |
| 76 | + unsigned[i] = data[i] & 0xff; |
| 77 | + } |
| 78 | + return unsigned; |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String args[]) throws Exception { |
| 82 | + |
| 83 | + if (args.length != 0 && args.length != 3) { |
| 84 | + System.out.println("usage: App"); |
| 85 | + System.out.println("usage: App -verify fpath dsname"); |
| 86 | + System.exit(2); // EARLY EXIT |
| 87 | + } else if (args.length == 3) { |
| 88 | + String fpath = args[1]; |
| 89 | + String dsname = args[2]; |
| 90 | + ZarrArray verification = ZarrGroup.open(fpath).openArray(dsname); |
| 91 | + int[] shape = verification.getShape(); |
| 92 | + if (!Arrays.equals(SHAPE, shape)) { |
| 93 | + throw new RuntimeException(String.format( |
| 94 | + "shape-mismatch expected:%s found:%s", |
| 95 | + Arrays.toString(SHAPE), Arrays.toString(shape) |
| 96 | + )); |
| 97 | + } |
| 98 | + |
| 99 | + int[] test = getTestData(); |
| 100 | + int[] verify = getArrayData(verification); |
| 101 | + if (!Arrays.equals(test, verify)) { |
| 102 | + throw new RuntimeException(String.format( |
| 103 | + "values don't match")); |
| 104 | + } |
| 105 | + return; // EARLY EXIT |
| 106 | + } |
| 107 | + |
| 108 | + int[] data = getTestData(); |
| 109 | + |
| 110 | + final ZarrGroup container = ZarrGroup.create(OUT_PATH); |
| 111 | + for (final Compression compressionType : Compression.values()) { |
| 112 | + ArrayParams arrayParams = new ArrayParams() |
| 113 | + .shape(SHAPE) |
| 114 | + .chunks(CHUNKS) |
| 115 | + .dataType(DataType.u1) |
| 116 | + // .nested(nested) FIXME: requires a different branch |
| 117 | + .compressor(CompressorFactory.create(compressionType.toString())); // jzarr name, "null" |
| 118 | + |
| 119 | + String dsname = compressionType.name(); // zarr_implementation name, "raw" |
| 120 | + if ("blosc".equals(dsname)) { |
| 121 | + dsname = "blosc/lz4"; // FIXME: better workaround? |
| 122 | + } |
| 123 | + Path subdir = OUT_PATH.resolve(dsname); |
| 124 | + ZarrArray zArray = ZarrArray.create(subdir, arrayParams); |
| 125 | + // final ZarrArray zarr = ZarrArray.open(getRootPath().resolve(pathName)); |
| 126 | + zArray.write(data, SHAPE, new int[]{0, 0, 0}); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments