private void exportColorPaletteDef() {
   final ImageInfo imageInfo = getFormModel().getModifiedImageInfo();
   if (imageInfo == null) {
     // Normally this code is unreachable because, the export Button should be
     // disabled if the color manipulation form has no ImageInfo.
     return;
   }
   final SnapFileChooser fileChooser = new SnapFileChooser();
   fileChooser.setDialogTitle("Export Colour Palette"); /*I18N*/
   fileChooser.setFileFilter(getOrCreateColorPaletteDefinitionFileFilter());
   fileChooser.setCurrentDirectory(getIODir().toFile());
   final int result = fileChooser.showSaveDialog(getToolViewPaneControl());
   File file = fileChooser.getSelectedFile();
   if (file != null && file.getParentFile() != null) {
     setIODir(file.getParentFile());
   }
   if (result == JFileChooser.APPROVE_OPTION) {
     if (file != null) {
       if (Boolean.TRUE.equals(SnapDialogs.requestOverwriteDecision(titlePrefix, file))) {
         file = FileUtils.ensureExtension(file, FILE_EXTENSION);
         try {
           final ColorPaletteDef colorPaletteDef = imageInfo.getColorPaletteDef();
           ColorPaletteDef.storeColorPaletteDef(colorPaletteDef, file);
         } catch (IOException e) {
           showErrorDialog("Failed to export colour palette:\n" + e.getMessage()); /*I18N*/
         }
       }
     }
   }
 }
 private TiffShort[] createColorMap(Product product) {
   final ImageInfo imageInfo = product.getBandAt(0).getImageInfo(null, ProgressMonitor.NULL);
   final ColorPaletteDef paletteDef = imageInfo.getColorPaletteDef();
   final TiffShort[] redColor = new TiffShort[TIFF_COLORMAP_SIZE];
   Arrays.fill(redColor, new TiffShort(0));
   final TiffShort[] greenColor = new TiffShort[TIFF_COLORMAP_SIZE];
   Arrays.fill(greenColor, new TiffShort(0));
   final TiffShort[] blueColor = new TiffShort[TIFF_COLORMAP_SIZE];
   Arrays.fill(blueColor, new TiffShort(0));
   final float factor = 65535.0f / 255.0f;
   for (ColorPaletteDef.Point point : paletteDef.getPoints()) {
     final Color color = point.getColor();
     final int red = (int) (color.getRed() * factor);
     final int green = (int) (color.getGreen() * factor);
     final int blue = (int) (color.getBlue() * factor);
     int mapIndex = (int) Math.floor(point.getSample());
     redColor[mapIndex] = new TiffShort(red);
     greenColor[mapIndex] = new TiffShort(green);
     blueColor[mapIndex] = new TiffShort(blue);
   }
   final TiffShort[] colorMap = new TiffShort[TIFF_COLORMAP_SIZE * 3];
   System.arraycopy(redColor, 0, colorMap, 0, redColor.length);
   System.arraycopy(greenColor, 0, colorMap, TIFF_COLORMAP_SIZE, greenColor.length);
   System.arraycopy(blueColor, 0, colorMap, TIFF_COLORMAP_SIZE * 2, blueColor.length);
   return colorMap;
 }
 private void applyColorPaletteDef(
     ColorPaletteDef colorPaletteDef, RasterDataNode targetRaster, ImageInfo targetImageInfo) {
   if (isIndexCoded(targetRaster)) {
     targetImageInfo.setColors(colorPaletteDef.getColors());
   } else {
     Stx stx = targetRaster.getStx(false, ProgressMonitor.NULL);
     Boolean autoDistribute = getAutoDistribute(colorPaletteDef);
     if (autoDistribute == null) {
       return;
     }
     targetImageInfo.setColorPaletteDef(
         colorPaletteDef, stx.getMinimum(), stx.getMaximum(), autoDistribute);
   }
 }