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  @SuppressWarnings("javadoc")
44  public class ImageNullPixelMask {
45  
46      private final AbstractNullPixelMask[] nullPixelMasks;
47  
48      private final long nullValue;
49  
50      private final ICompressorControl compressorControl;
51  
52      private final String compressAlgorithm;
53  
54      public ImageNullPixelMask(int tileCount, long nullValue, String compressAlgorithm) {
55          nullPixelMasks = new AbstractNullPixelMask[tileCount];
56          this.nullValue = nullValue;
57          this.compressAlgorithm = compressAlgorithm;
58          compressorControl = CompressorProvider.findCompressorControl(null, this.compressAlgorithm, byte.class);
59      }
60  
61      public NullPixelMaskPreserver createTilePreserver(TileBuffer tileBuffer, int tileIndex) {
62          return add(new NullPixelMaskPreserver(tileBuffer, tileIndex, nullValue, compressorControl));
63      }
64  
65      public NullPixelMaskRestorer createTileRestorer(TileBuffer tileBuffer, int tileIndex) {
66          return add(new NullPixelMaskRestorer(tileBuffer, tileIndex, nullValue, compressorControl));
67      }
68  
69      @SuppressWarnings("deprecation")
70      public byte[][] getColumn() {
71          byte[][] column = new byte[nullPixelMasks.length][];
72          for (AbstractNullPixelMask tileMask : nullPixelMasks) {
73              column[tileMask.getTileIndex()] = tileMask.getMaskBytes();
74          }
75          return column;
76      }
77  
78      public String getCompressAlgorithm() {
79          return compressAlgorithm;
80      }
81  
82      public void setColumn(byte[][] nullPixels) {
83          for (AbstractNullPixelMask tileMask : nullPixelMasks) {
84              byte[] tileMaskBytes = nullPixels[tileMask.getTileIndex()];
85              if (tileMaskBytes != null && tileMaskBytes.length > 0) {
86                  tileMask.setMask(ByteBuffer.wrap(tileMaskBytes));
87              }
88          }
89      }
90  
91      private <T extends AbstractNullPixelMask> T add(T nullPixelMask) {
92          nullPixelMasks[nullPixelMask.getTileIndex()] = nullPixelMask;
93          return nullPixelMask;
94      }
95  
96  }