@Override
    protected void loadUncompressedRaster(
        final RAWImageInputStream iis, final WritableRaster raster, final RAWImageReaderSupport ir)
        throws IOException {

      final DataBufferUShort dataBuffer = (DataBufferUShort) raster.getDataBuffer();
      final short[] data = dataBuffer.getData();
      final int width = raster.getWidth();
      final int height = raster.getHeight();
      final int pixelStride = 1;
      final int scanStride = width * pixelStride;
      setBitsPerSample(12);
      selectBitReader(iis, raster, 12);
      //
      // We can rely on the fact that the array has been zeroed by the JVM, so we just set nonzero
      // samples.
      //
      for (int y = 0; y < height; y++) {
        final int row = getRow(y, height);
        int i = row * scanStride;

        for (int x = 0; x < width; x++) {
          int sample = (int) iis.readBits(12);

          data[i] = (short) sample;
          endOfColumn(x, iis);
          i += pixelStride;
        }

        ir.processImageProgress((100.0f * y) / height);
        endOfRow(y, iis);
      }
    }
Example #2
0
  /**
   * Constructs a ShortBandedRaster with the given SampleModel, DataBuffer, and parent. DataBuffer
   * must be a DataBufferUShort and SampleModel must be of type BandedSampleModel. When translated
   * into the base Raster's coordinate system, aRegion must be contained by the base Raster. Origin
   * is the coordinate in the new Raster's coordinate system of the origin of the base Raster. (The
   * base Raster is the Raster's ancestor which has no parent.)
   *
   * <p>Note that this constructor should generally be called by other constructors or create
   * methods, it should not be used directly.
   *
   * @param sampleModel The SampleModel that specifies the layout.
   * @param dataBuffer The DataBufferUShort that contains the image data.
   * @param aRegion The Rectangle that specifies the image area.
   * @param origin The Point that specifies the origin.
   * @param parent The parent (if any) of this raster.
   */
  public ShortBandedRaster(
      SampleModel sampleModel,
      DataBuffer dataBuffer,
      Rectangle aRegion,
      Point origin,
      ShortBandedRaster parent) {

    super(sampleModel, dataBuffer, aRegion, origin, parent);
    this.maxX = minX + width;
    this.maxY = minY + height;
    if (!(dataBuffer instanceof DataBufferUShort)) {
      throw new RasterFormatException("ShortBandedRaster must have " + "ushort DataBuffers");
    }
    DataBufferUShort dbus = (DataBufferUShort) dataBuffer;

    if (sampleModel instanceof BandedSampleModel) {
      BandedSampleModel bsm = (BandedSampleModel) sampleModel;
      this.scanlineStride = bsm.getScanlineStride();
      int bankIndices[] = bsm.getBankIndices();
      int bandOffsets[] = bsm.getBandOffsets();
      int dOffsets[] = dbus.getOffsets();
      dataOffsets = new int[bankIndices.length];
      data = new short[bankIndices.length][];
      int xOffset = aRegion.x - origin.x;
      int yOffset = aRegion.y - origin.y;
      for (int i = 0; i < bankIndices.length; i++) {
        data[i] = stealData(dbus, bankIndices[i]);
        dataOffsets[i] =
            dOffsets[bankIndices[i]] + xOffset + yOffset * scanlineStride + bandOffsets[i];
      }
    } else {
      throw new RasterFormatException("ShortBandedRasters must have " + "BandedSampleModels");
    }
    verify();
  }
    @Override
    protected void computeRect(PlanarImage[] sources, WritableRaster dest, Rectangle destRect) {
      final DataBufferUShort dataBuffer = (DataBufferUShort) dest.getDataBuffer();
      final short[] tileData = dataBuffer.getData();

      final int tileWidth = this.getTileWidth();
      final int tileHeight = this.getTileHeight();
      final int tileX = destRect.x / tileWidth;
      final int tileY = destRect.y / tileHeight;

      if (tileWidth * tileHeight != tileData.length) {
        throw new IllegalStateException(
            String.format(
                "tileWidth (=%d) * tileHeight (=%d) != tileData.length (=%d)",
                tileWidth, tileHeight, tileData.length));
      }

      final int resolution = getLevel();
      final Dimension jp2TileDim = getJp2TileDim(bandInfo, resolution);

      final int jp2TileWidth = jp2TileDim.width;
      final int jp2TileHeight = jp2TileDim.height;
      final int jp2TileX = destRect.x / jp2TileWidth;
      final int jp2TileY = destRect.y / jp2TileHeight;

      // Res - Img Size - Tile W
      //  0  -  10960   -  4096
      //  1  -   5480   -  2048
      //  2  -   2740   -  1024
      //  3  -   1370   -   512
      //  4  -    685   -   256
      //  5  -    343   -   128

      final File outputFile =
          new File(
              cacheDir,
              FileUtils.exchangeExtension(
                  imageFile.getName(),
                  String.format("_R%d_TX%d_TY%d.pgx", resolution, jp2TileX, jp2TileY)));
      final File outputFile0 = getFirstComponentOutputFile(outputFile);
      if (!outputFile0.exists()) {
        System.out.printf(
            "Jp2ExeImage.readTileData(): recomputing res=%d, tile=(%d,%d)\n",
            resolution, jp2TileX, jp2TileY);
        try {
          decompressTile(outputFile, jp2TileX, jp2TileY);
        } catch (IOException e) {
          // warn
          outputFile0.delete();
        }
        if (!outputFile0.exists()) {
          Arrays.fill(tileData, (short) 0);
          return;
        }
      }

      try {
        System.out.printf(
            "Jp2ExeImage.readTileData(): reading res=%d, tile=(%d,%d)\n",
            resolution, jp2TileX, jp2TileY);
        readTileData(
            outputFile0,
            tileX,
            tileY,
            tileWidth,
            tileHeight,
            jp2TileX,
            jp2TileY,
            jp2TileWidth,
            jp2TileHeight,
            tileData,
            destRect);
      } catch (IOException e) {
        // warn
      }
    }