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.opencv;
  
  import java.io.IOException;
  import java.util.Locale;
  
  import javax.imageio.ImageReader;
  import javax.imageio.spi.ImageReaderSpi;
  import javax.imageio.stream.ImageInputStream;
  
  import org.miaixz.bus.core.Version;
  
  /**
   * @author Kimi Liu
   * @since Java 17+
   */
  public class NativeJ2kImageReaderSpi extends ImageReaderSpi {
  
      static final String[] SUFFIXES = { "jp2", "jp2k", "j2k", "j2c" };
      static final String[] NAMES = { "jpeg2000-cv", "jpeg2000", "JP2KSimpleBox", "jpeg 2000", "JPEG 2000", "JPEG2000" };
      static final String[] MIMES = { "image/jp2", "image/jp2k", "image/j2k", "image/j2c" };
  
      public NativeJ2kImageReaderSpi() {
          super("Miaixz Team", Version._VERSION, NAMES, SUFFIXES, MIMES, NativeImageReader.class.getName(),
                  new Class[] { ImageInputStream.class }, new String[] {}, false, // supportsStandardStreamMetadataFormat
                  null, // nativeStreamMetadataFormatName
                  null, // nativeStreamMetadataFormatClassName
                  null, // extraStreamMetadataFormatNames
                  null, // extraStreamMetadataFormatClassNames
                  false, // supportsStandardImageMetadataFormat
                  null, null, null, null);
      }
  
      @Override
      public String getDescription(Locale locale) {
          return "Natively-accelerated JPEG2000 Image Reader (OpenJPEG based)";
      }
  
      @Override
      public boolean canDecodeInput(Object source) throws IOException {
          // NativeImageReader.read() eventually instantiates a StreamSegment,
          // which does not support all ImageInputStreams
          if (!StreamSegment.supportsInputStream(source)) {
              return false;
          }
          ImageInputStream iis = (ImageInputStream) source;
          iis.mark();
          try {
              int marker = (iis.read() << 8) | iis.read();
  
              if (marker == 0xFF4F) {
                  return true;
              }
  
              iis.reset();
              iis.mark();
              byte[] b = new byte[12];
              iis.readFully(b);
  
              // Verify the signature box
              // The length of the signature box is 12
              if (b[0] != 0 || b[1] != 0 || b[2] != 0 || b[3] != 12) {
                  return false;
              }
  
              // The signature box type is "jP "
              if ((b[4] & 0xff) != 0x6A || (b[5] & 0xFF) != 0x50 || (b[6] & 0xFF) != 0x20 || (b[7] & 0xFF) != 0x20) {
                  return false;
              }
  
              // The signature content is 0x0D0A870A
              return (b[8] & 0xFF) != 0x0D || (b[9] & 0xFF) != 0x0A || (b[10] & 0xFF) != 0x87 || (b[11] & 0xFF) != 0x0A;
          } finally {
              iis.reset();
          }
      }
  
      @Override
      public ImageReader createReaderInstance(Object extension) {
          return new NativeImageReader(this, true);
      }
  
  }
  