View Javadoc
1   package nom.tam.image.compression.tile.mask;
2   
3   import java.nio.ByteBuffer;
4   
5   import nom.tam.fits.compression.algorithm.api.ICompressorControl;
6   import nom.tam.fits.compression.provider.CompressorProvider;
7   import nom.tam.image.tile.operation.buffer.TileBuffer;
8   
9   /*
10   * #%L
11   * nom.tam FITS library
12   * %%
13   * Copyright (C) 1996 - 2024 nom-tam-fits
14   * %%
15   * This is free and unencumbered software released into the public domain.
16   *
17   * Anyone is free to copy, modify, publish, use, compile, sell, or
18   * distribute this software, either in source code form or as a compiled
19   * binary, for any purpose, commercial or non-commercial, and by any
20   * means.
21   *
22   * In jurisdictions that recognize copyright laws, the author or authors
23   * of this software dedicate any and all copyright interest in the
24   * software to the public domain. We make this dedication for the benefit
25   * of the public at large and to the detriment of our heirs and
26   * successors. We intend this dedication to be an overt act of
27   * relinquishment in perpetuity of all present and future rights to this
28   * software under copyright law.
29   *
30   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36   * OTHER DEALINGS IN THE SOFTWARE.
37   * #L%
38   */
39  
40  /**
41   * Support for blank (<code>null</code>) values in compressed images.
42   */
43  public class ImageNullPixelMask {
44  
45      private final AbstractNullPixelMask[] nullPixelMasks;
46  
47      private final long nullValue;
48  
49      private final ICompressorControl compressorControl;
50  
51      private final String compressAlgorithm;
52  
53      public ImageNullPixelMask(int tileCount, long nullValue, String compressAlgorithm) {
54          nullPixelMasks = new AbstractNullPixelMask[tileCount];
55          this.nullValue = nullValue;
56          this.compressAlgorithm = compressAlgorithm;
57          compressorControl = CompressorProvider.findCompressorControl(null, this.compressAlgorithm, byte.class);
58      }
59  
60      public NullPixelMaskPreserver createTilePreserver(TileBuffer tileBuffer, int tileIndex) {
61          return add(new NullPixelMaskPreserver(tileBuffer, tileIndex, nullValue, compressorControl));
62      }
63  
64      public NullPixelMaskRestorer createTileRestorer(TileBuffer tileBuffer, int tileIndex) {
65          return add(new NullPixelMaskRestorer(tileBuffer, tileIndex, nullValue, compressorControl));
66      }
67  
68      @SuppressWarnings("deprecation")
69      public byte[][] getColumn() {
70          byte[][] column = new byte[nullPixelMasks.length][];
71          for (AbstractNullPixelMask tileMask : nullPixelMasks) {
72              column[tileMask.getTileIndex()] = tileMask.getMaskBytes();
73          }
74          return column;
75      }
76  
77      public String getCompressAlgorithm() {
78          return compressAlgorithm;
79      }
80  
81      public void setColumn(byte[][] nullPixels) {
82          for (AbstractNullPixelMask tileMask : nullPixelMasks) {
83              byte[] tileMaskBytes = nullPixels[tileMask.getTileIndex()];
84              if (tileMaskBytes != null && tileMaskBytes.length > 0) {
85                  tileMask.setMask(ByteBuffer.wrap(tileMaskBytes));
86              }
87          }
88      }
89  
90      private <T extends AbstractNullPixelMask> T add(T nullPixelMask) {
91          nullPixelMasks[nullPixelMask.getTileIndex()] = nullPixelMask;
92          return nullPixelMask;
93      }
94  
95  }