content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.image.nimble;
  
  import java.awt.*;
  import java.awt.color.ColorSpace;
  import java.awt.image.ColorModel;
  import java.awt.image.DataBuffer;
  import java.awt.image.Raster;
  import java.awt.image.SampleModel;
  
  /**
   * @author Kimi Liu
   * @since Java 17+
   */
  public class SampledColorModel extends ColorModel {
  
      private static final int[] BITS = { 8, 8, 8 };
  
      private final ColorSubsampling subsampling;
  
      public SampledColorModel(ColorSpace cspace, ColorSubsampling subsampling) {
          super(24, BITS, cspace, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
          this.subsampling = subsampling;
      }
  
      @Override
      public boolean isCompatibleRaster(Raster raster) {
          return isCompatibleSampleModel(raster.getSampleModel());
      }
  
      @Override
      public boolean isCompatibleSampleModel(SampleModel sm) {
          return sm instanceof SampledSampleModel;
      }
  
      @Override
      public SampleModel createCompatibleSampleModel(int w, int h) {
          return new SampledSampleModel(w, h, subsampling);
      }
  
      @Override
      public int getAlpha(int pixel) {
          return 255;
      }
  
      @Override
      public int getBlue(int pixel) {
          return pixel & 0xFF;
      }
  
      @Override
      public int getGreen(int pixel) {
          return pixel & 0xFF00;
      }
  
      @Override
      public int getRed(int pixel) {
          return pixel & 0xFF0000;
      }
  
      @Override
      public int getAlpha(Object inData) {
          return 255;
      }
  
      @Override
      public int getBlue(Object inData) {
          return getRGB(inData) & 0xFF;
      }
  
      @Override
      public int getGreen(Object inData) {
          return (getRGB(inData) >> 8) & 0xFF;
      }
  
      @Override
      public int getRed(Object inData) {
          return getRGB(inData) >> 16;
      }
  
      @Override
      public int getRGB(Object inData) {
          byte[] ba = (byte[]) inData;
          ColorSpace cs = getColorSpace();
          float[] fba = new float[] { (ba[0] & 0xFF) / 255f, (ba[1] & 0xFF) / 255f, (ba[2] & 0xFF) / 255f };
          float[] rgb = cs.toRGB(fba);
          int ret = (((int) (rgb[0] * 255)) << 16) | (((int) (rgb[1] * 255)) << 8) | (((int) (rgb[2] * 255)));
          return ret;
      }
  
  }
  