public static BufferedImage convertImageData(ImageData data) {
    BufferedImage bimg = null;
    int height = data.getHeight(), width = data.getWidth();
    Object pixels = data.getData();
    if (pixels instanceof int[] && data.getElementWidth() == 1) {
      int[] arr = (int[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        int l = arr[j];
        barray[k++] = (byte) (l & 0xFF);
        barray[k++] = (byte) ((l >>> 8) & 0xFF);
        barray[k++] = (byte) ((l >>> 16) & 0xFF);
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else if (pixels instanceof byte[]
        && data.getElementWidth() == 1) { // Assume gray scale model?
      byte[] arr = (byte[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        byte l = arr[j];
        barray[k++] = l;
        barray[k++] = l;
        barray[k++] = l;
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else {
      throw new RuntimeException("Unexpected data.");
    }
    return bimg;
  }
Esempio n. 2
0
 // If defaultNewIsNull is true, then newIsNull is ignored and it should behave as
 // if newIsNull were false.
 private void runTest(
     WritableRaster source,
     double sourceNoData,
     double newValue,
     boolean defaultNewIsNull,
     boolean newIsNull) {
   ColorModel cm1 = RasterUtils.createColorModel(source);
   java.util.Hashtable<String, Object> arg1Properties = new java.util.Hashtable<String, Object>();
   arg1Properties.put(OpImageUtils.NODATA_PROPERTY, sourceNoData);
   BufferedImage s1 = new BufferedImage(cm1, source, false, arg1Properties);
   RenderedOp op = null;
   if (defaultNewIsNull) {
     op = ReplaceNullDescriptor.create(s1, newValue, null);
   } else {
     op = ReplaceNullDescriptor.create(s1, newValue, newIsNull, null);
   }
   Raster r = op.getData();
   if (!defaultNewIsNull && newIsNull) {
     Assert.assertEquals(newValue, OpImageUtils.getNoData(op, Double.NaN), EPSILON);
   } else {
     Assert.assertTrue(Double.isNaN(OpImageUtils.getNoData(op, Double.NaN)));
   }
   Assert.assertEquals(width, r.getWidth());
   Assert.assertEquals(height, r.getHeight());
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       double v = r.getSampleDouble(x, y, 0);
       int pixelId = getPixelId(x, y, width);
       Assert.assertEquals(pixelId, v, EPSILON);
     }
   }
 }
Esempio n. 3
0
  private Raster clipRowToRect(
      final Raster raster, final Rectangle rect, final int[] bands, final int xSub) {
    if (rect.contains(raster.getMinX(), 0, raster.getWidth(), 1)
        && xSub == 1
        && bands == null /* TODO: Compare bands with that of raster */) {
      return raster;
    }

    return raster.createChild(rect.x / xSub, 0, rect.width / xSub, 1, 0, 0, bands);
  }
Esempio n. 4
0
 /**
  * Constructs IntegerRaster with the given size.
  *
  * @param _nrow the number of rows
  * @param _ncol the number of columns
  */
 public IntegerRaster(int _nrow, int _ncol) {
   nrow = _nrow;
   ncol = _ncol;
   dimension = new Dimension(ncol, nrow);
   int size = nrow * ncol;
   ComponentColorModel ccm =
       new ComponentColorModel(
           ColorSpace.getInstance(ColorSpace.CS_sRGB),
           new int[] {8, 8, 8},
           false, // hasAlpha
           false,
           Transparency.OPAQUE,
           DataBuffer.TYPE_BYTE);
   BandedSampleModel csm =
       new BandedSampleModel(
           DataBuffer.TYPE_BYTE, ncol, nrow, ncol, new int[] {0, 1, 2}, new int[] {0, 0, 0});
   rgbData = new byte[3][size];
   DataBuffer databuffer = new DataBufferByte(rgbData, size);
   WritableRaster raster = Raster.createWritableRaster(csm, databuffer, new Point(0, 0));
   image = new BufferedImage(ccm, raster, false, null);
   // col in x direction, row in y direction
   xmin = 0;
   xmax = ncol;
   ymin = nrow;
   ymax = 0; // zero is on top
 }
Esempio n. 5
0
 // returns a value specifying some kind of average brightness in the image.
 protected int getAverageBrightness(BufferedImage img) {
   Raster r = img.getData();
   int total = 0;
   for (int y = 0; y < r.getHeight(); y++) {
     for (int x = 0; x < r.getWidth(); x++) {
       total += r.getSample(r.getMinX() + x, r.getMinY() + y, 0);
     }
   }
   return (int) (total / ((r.getWidth() / factorD) * (r.getHeight() / factorD)));
 }
  public final Rectangle2D getBounds2D(Raster src) {
    // We position source raster to (0,0) even if it is translated child
    // raster.
    // This means that we need only width and height of the src
    int width = src.getWidth();
    int height = src.getHeight();

    float[] corners = {0, 0, width, 0, width, height, 0, height};

    at.transform(corners, 0, corners, 0, 4);

    Rectangle2D.Float bounds = new Rectangle2D.Float(corners[0], corners[1], 0, 0);
    bounds.add(corners[2], corners[3]);
    bounds.add(corners[4], corners[5]);
    bounds.add(corners[6], corners[7]);

    return bounds;
  }
Esempio n. 7
0
 @BeforeClass
 public static void init() {
   width = 10;
   height = 10;
   sm = new BandedSampleModel(DataBuffer.TYPE_DOUBLE, width, height, 1);
   numbered = Raster.createWritableRaster(sm, new Point(0, 0));
   numberedWithNoData = Raster.createWritableRaster(sm, new Point(0, 0));
   numberedWithNanNoData = Raster.createWritableRaster(sm, new Point(0, 0));
   for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
       int pixelId = getPixelId(x, y, width);
       numbered.setSample(x, y, 0, (double) pixelId);
       numberedWithNoData.setSample(
           x, y, 0, ((pixelId % noDataModValue) == 0) ? NON_NAN_NODATA_VALUE : (double) pixelId);
       numberedWithNanNoData.setSample(
           x, y, 0, ((pixelId % noDataModValue) == 0) ? Double.NaN : (double) pixelId);
     }
   }
 }
  public WritableRaster createCompatibleDestRaster(Raster src) {
    // Here approach is other then in createCompatibleDestImage -
    // destination should include only
    // transformed image, but not (0,0) in source coordinate system

    Rectangle2D newBounds = getBounds2D(src);
    return src.createCompatibleWritableRaster(
        (int) newBounds.getX(),
        (int) newBounds.getY(),
        (int) newBounds.getWidth(),
        (int) newBounds.getHeight());
  }
  public final WritableRaster filter(Raster src, WritableRaster dst) {
    if (src == dst) {
      // awt.252=Source can't be same as the destination
      throw new IllegalArgumentException(Messages.getString("awt.252")); // $NON-NLS-1$
    }

    if (dst == null) {
      dst = createCompatibleDestRaster(src);
    } else if (src.getNumBands() != dst.getNumBands()) {
      // awt.253=Different number of bands in source and destination
      throw new IllegalArgumentException(Messages.getString("awt.253")); // $NON-NLS-1$
    }

    if (slowFilter(src, dst) != 0) {
      // awt.21F=Unable to transform source
      throw new ImagingOpException(Messages.getString("awt.21F")); // $NON-NLS-1$
      // TODO - uncomment
      // if (ippFilter(src, dst, BufferedImage.TYPE_CUSTOM) != 0)
      // throw new ImagingOpException("Unable to transform source");
    }

    return dst;
  }
Esempio n. 10
0
  public static BufferedImage toGrayScale(BufferedImage image) {
    BufferedImage result =
        new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Raster raster = image.getData();
    WritableRaster grayRaster = result.getRaster();
    int height = raster.getHeight();
    int sample = 0;

    for (int x = 0; x < raster.getWidth(); ++x) {
      for (int y = 0; y < height; ++y) {

        sample = raster.getSample(x, y, 0) + raster.getSample(x, y, 1) + raster.getSample(x, y, 2);

        grayRaster.setSample(x, y, 0, (int) ((float) sample / 3.0));
      }
    }
    return result;
  }
Esempio n. 11
0
  @Override
  protected BufferedImage doInBackground() {
    int x, y, b;
    int[] A = null;
    int[] sum = new int[bands], sum_x = new int[bands], sum_y = new int[bands];
    WritableRaster raster = image.getRaster();

    for (x = 0; x < size.getWidth(); x++) {
      for (y = 0; y < size.getHeight(); y++) {
        if (y == 0 || y == size.getHeight() - 1 || x == 0 || x == size.getWidth() - 1) {
          for (b = 0; b < bands; b++) {
            sum[b] = 0;
          }
        } else {
          for (b = 0; b < bands; b++) {
            A =
                input.getSamples(
                    x - 1, y - 1, CSobelOperator.matrix_size, CSobelOperator.matrix_size, b, A);
            sum_x[b] = CSobelOperator.getGx(A);
            sum_y[b] = CSobelOperator.getGy(A);
          }

          for (b = 0; b < bands; b++) {
            sum[b] = Math.abs(sum_x[b]) + Math.abs(sum_y[b]);
          }
        }
        raster.setPixel(x, y, sum);
        setProgress(
            100
                * (x * ((int) size.getHeight()) + y)
                / ((int) (size.getWidth() * size.getHeight())));
      }
    }
    image.setData(raster);
    return image;
  }
Esempio n. 12
0
  public static UIFont createGuiFont(String name, int font_size) {
    Font font = load_ttf(StringUtils.concat(name, ".ttf"), font_size);
    UIFont tmp = new UIFont();

    tmp.font_texture = new Texture();

    Graphics2D graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
    graphics.setFont(font);

    tmp.fontMetrics = graphics.getFontMetrics();

    WritableRaster raster;
    BufferedImage bufferedImage;

    raster =
        Raster.createInterleavedRaster(
            DataBuffer.TYPE_BYTE,
            (int) tmp.getFontImageWidth(),
            (int) tmp.getFontImageHeight(),
            4,
            null);
    bufferedImage = new BufferedImage(glAlphaColorModel, raster, false, null);

    // Draw the characters on our image
    Graphics2D imageGraphics = bufferedImage.createGraphics();
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    imageGraphics.setFont(font);
    imageGraphics.setColor(Color.white.getAWTColor());

    // draw every CHAR by line...
    for (int i : key_table.keySet())
      imageGraphics.drawString(
          key_table.get(i), 0, (int) (tmp.fontMetrics.getMaxAscent() + (tmp.getHeight() * i)));

    // Generate texture data
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    ByteBuffer imageData = BufferUtils.create_byte_buffer(data.length);
    imageData.order(ByteOrder.nativeOrder());
    imageData.put(data, 0, data.length);
    imageData.flip();

    glBindTexture(GL_TEXTURE_2D, tmp.font_texture.id);

    // Setup wrap mode
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGBA8,
        (int) tmp.getFontImageWidth(),
        (int) tmp.getFontImageHeight(),
        0,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        imageData);

    return tmp;
  }
Esempio n. 13
0
  public void decode() throws IOException {
    readTagDescriptors();

    final MessageDigest md;
    try {
      md = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return;
    }

    final ComponentColorModel colorModel =
        new ComponentColorModel(
            ColorSpace.getInstance(ColorSpace.CS_sRGB),
            true,
            false,
            Transparency.TRANSLUCENT,
            DataBuffer.TYPE_BYTE);
    final int[] bandOffsets = {2, 1, 0, 3};
    final Map<String, String> duplicateGuard = new HashMap<String, String>();
    final byte[] bytes = data.array();
    final int[] tags = new int[8];

    final List<String> symLinkCommand = new ArrayList<String>(4);
    symLinkCommand.add("ln");
    symLinkCommand.add("-s");
    symLinkCommand.add(null);
    symLinkCommand.add(null);

    for (int fileIndex = 0; fileIndex < fileCount; fileIndex++) {
      data.position(fileDescriptorsOffset + ((4 + 8) * fileIndex));
      final int fileDataOffset = data.getInt();
      int numOfTags = 8;
      for (int i = 0; i < 8; i++) {
        int tag = data.get() & 0xff;
        if (tag == 0) {
          numOfTags = i;
          break;
        }
        tags[i] = tag;
      }

      data.position(fileDataSectionOffset + fileDataOffset);
      final int artRows = data.getShort();
      final int artColumns = data.getShort();
      // skip unknown
      data.position(data.position() + 28);

      final int[] subimageOffsets = readNumericArray(9, false);
      final int[] subimageWidths = readNumericArray(9, true);
      final int[] subimageHeights = readNumericArray(9, true);

      assert numOfTags >= 2;
      final File dir;
      final int tagStartIndexForFilename;
      if (numOfTags == 2) {
        tagStartIndexForFilename = 1;
        dir = new File(outputDir, names[tags[0]].toString());
      } else {
        tagStartIndexForFilename = 2;
        dir = new File(outputDir, names[tags[0]] + "/" + names[tags[1]]);
      }

      boolean dirCreated = false;

      final int imageCount = artRows * artColumns;
      for (int subImageIndex = 0; subImageIndex < imageCount; subImageIndex++) {
        final int w = subimageWidths[subImageIndex];
        final int h = subimageHeights[subImageIndex];
        if (w <= 0 || h <= 0) {
          continue;
        }

        final int srcPos = fileDataSectionOffset + fileDataOffset + subimageOffsets[subImageIndex];
        final int srcLength = w * h * 4;

        md.update(bytes, srcPos, srcLength);
        md.update((byte) w);
        md.update((byte) h);
        final String digest = convertToHex(md.digest());
        md.reset();

        if (!dirCreated) {
          //noinspection ResultOfMethodCallIgnored
          dir.mkdirs();
          dirCreated = true;
        }

        final byte[] bgra = new byte[srcLength];
        // cannot pass bytes directly, offset in DataBufferByte is not working
        System.arraycopy(bytes, srcPos, bgra, 0, bgra.length);
        BufferedImage image =
            new BufferedImage(
                colorModel,
                (WritableRaster)
                    Raster.createRaster(
                        new PixelInterleavedSampleModel(
                            DataBuffer.TYPE_BYTE, w, h, 4, w * 4, bandOffsets),
                        new DataBufferByte(bgra, bgra.length),
                        null),
                false,
                null);

        final StringBuilder outFilename =
            createOutpuFile(
                numOfTags, tags, tagStartIndexForFilename, imageCount > 0, subImageIndex);
        final String oldOutFilename = duplicateGuard.get(digest);
        if (oldOutFilename == null) {
          File file = new File(dir, outFilename.toString());
          duplicateGuard.put(digest, file.getPath());
          ImageIO.write(image, "png", file);
        } else {
          symLinkCommand.set(2, oldOutFilename);
          symLinkCommand.set(3, dir + "/" + outFilename);
          Process process = new ProcessBuilder(symLinkCommand).start();
          try {
            if (process.waitFor() != 0) {
              throw new IOException(
                  "Can't create symlink " + symLinkCommand.get(2) + " " + symLinkCommand.get(3));
            }
          } catch (InterruptedException e) {
            throw new IOException(e);
          }
        }
      }
    }
  }
  public void build_bricks() {

    ImagePlus imp;
    ImagePlus orgimp;
    ImageStack stack;
    FileInfo finfo;

    if (lvImgTitle.isEmpty()) return;
    orgimp = WindowManager.getImage(lvImgTitle.get(0));
    imp = orgimp;

    finfo = imp.getFileInfo();
    if (finfo == null) return;

    int[] dims = imp.getDimensions();
    int imageW = dims[0];
    int imageH = dims[1];
    int nCh = dims[2];
    int imageD = dims[3];
    int nFrame = dims[4];
    int bdepth = imp.getBitDepth();
    double xspc = finfo.pixelWidth;
    double yspc = finfo.pixelHeight;
    double zspc = finfo.pixelDepth;
    double z_aspect = Math.max(xspc, yspc) / zspc;

    int orgW = imageW;
    int orgH = imageH;
    int orgD = imageD;
    double orgxspc = xspc;
    double orgyspc = yspc;
    double orgzspc = zspc;

    lv = lvImgTitle.size();
    if (filetype == "JPEG") {
      for (int l = 0; l < lv; l++) {
        if (WindowManager.getImage(lvImgTitle.get(l)).getBitDepth() != 8) {
          IJ.error("A SOURCE IMAGE MUST BE 8BIT GLAYSCALE");
          return;
        }
      }
    }

    // calculate levels
    /*		int baseXY = 256;
    		int baseZ = 256;

    		if (z_aspect < 0.5) baseZ = 128;
    		if (z_aspect > 2.0) baseXY = 128;
    		if (z_aspect >= 0.5 && z_aspect < 1.0) baseZ = (int)(baseZ*z_aspect);
    		if (z_aspect > 1.0 && z_aspect <= 2.0) baseXY = (int)(baseXY/z_aspect);

    		IJ.log("Z_aspect: " + z_aspect);
    		IJ.log("BaseXY: " + baseXY);
    		IJ.log("BaseZ: " + baseZ);
    */

    int baseXY = 256;
    int baseZ = 128;
    int dbXY = Math.max(orgW, orgH) / baseXY;
    if (Math.max(orgW, orgH) % baseXY > 0) dbXY *= 2;
    int dbZ = orgD / baseZ;
    if (orgD % baseZ > 0) dbZ *= 2;
    lv = Math.max(log2(dbXY), log2(dbZ)) + 1;

    int ww = orgW;
    int hh = orgH;
    int dd = orgD;
    for (int l = 0; l < lv; l++) {
      int bwnum = ww / baseXY;
      if (ww % baseXY > 0) bwnum++;
      int bhnum = hh / baseXY;
      if (hh % baseXY > 0) bhnum++;
      int bdnum = dd / baseZ;
      if (dd % baseZ > 0) bdnum++;

      if (bwnum % 2 == 0) bwnum++;
      if (bhnum % 2 == 0) bhnum++;
      if (bdnum % 2 == 0) bdnum++;

      int bw = (bwnum <= 1) ? ww : ww / bwnum + 1 + (ww % bwnum > 0 ? 1 : 0);
      int bh = (bhnum <= 1) ? hh : hh / bhnum + 1 + (hh % bhnum > 0 ? 1 : 0);
      int bd = (bdnum <= 1) ? dd : dd / bdnum + 1 + (dd % bdnum > 0 ? 1 : 0);

      bwlist.add(bw);
      bhlist.add(bh);
      bdlist.add(bd);

      IJ.log("LEVEL: " + l);
      IJ.log("  width: " + ww);
      IJ.log("  hight: " + hh);
      IJ.log("  depth: " + dd);
      IJ.log("  bw: " + bw);
      IJ.log("  bh: " + bh);
      IJ.log("  bd: " + bd);

      int xyl2 = Math.max(ww, hh) / baseXY;
      if (Math.max(ww, hh) % baseXY > 0) xyl2 *= 2;
      if (lv - 1 - log2(xyl2) <= l) {
        ww /= 2;
        hh /= 2;
      }
      IJ.log("  xyl2: " + (lv - 1 - log2(xyl2)));

      int zl2 = dd / baseZ;
      if (dd % baseZ > 0) zl2 *= 2;
      if (lv - 1 - log2(zl2) <= l) dd /= 2;
      IJ.log("  zl2: " + (lv - 1 - log2(zl2)));

      if (l < lv - 1) {
        lvImgTitle.add(lvImgTitle.get(0) + "_level" + (l + 1));
        IJ.selectWindow(lvImgTitle.get(0));
        IJ.run(
            "Scale...",
            "x=- y=- z=- width="
                + ww
                + " height="
                + hh
                + " depth="
                + dd
                + " interpolation=Bicubic average process create title="
                + lvImgTitle.get(l + 1));
      }
    }

    for (int l = 0; l < lv; l++) {
      IJ.log(lvImgTitle.get(l));
    }

    Document doc = newXMLDocument();
    Element root = doc.createElement("BRK");
    root.setAttribute("version", "1.0");
    root.setAttribute("nLevel", String.valueOf(lv));
    root.setAttribute("nChannel", String.valueOf(nCh));
    root.setAttribute("nFrame", String.valueOf(nFrame));
    doc.appendChild(root);

    for (int l = 0; l < lv; l++) {
      IJ.showProgress(0.0);

      int[] dims2 = imp.getDimensions();
      IJ.log(
          "W: "
              + String.valueOf(dims2[0])
              + " H: "
              + String.valueOf(dims2[1])
              + " C: "
              + String.valueOf(dims2[2])
              + " D: "
              + String.valueOf(dims2[3])
              + " T: "
              + String.valueOf(dims2[4])
              + " b: "
              + String.valueOf(bdepth));

      bw = bwlist.get(l).intValue();
      bh = bhlist.get(l).intValue();
      bd = bdlist.get(l).intValue();

      boolean force_pow2 = false;
      /*			if(IsPowerOf2(bw) && IsPowerOf2(bh) && IsPowerOf2(bd)) force_pow2 = true;

      			if(force_pow2){
      				//force pow2
      				if(Pow2(bw) > bw) bw = Pow2(bw)/2;
      				if(Pow2(bh) > bh) bh = Pow2(bh)/2;
      				if(Pow2(bd) > bd) bd = Pow2(bd)/2;
      			}

      			if(bw > imageW) bw = (Pow2(imageW) == imageW) ? imageW : Pow2(imageW)/2;
      			if(bh > imageH) bh = (Pow2(imageH) == imageH) ? imageH : Pow2(imageH)/2;
      			if(bd > imageD) bd = (Pow2(imageD) == imageD) ? imageD : Pow2(imageD)/2;

      */
      if (bw > imageW) bw = imageW;
      if (bh > imageH) bh = imageH;
      if (bd > imageD) bd = imageD;

      if (bw <= 1 || bh <= 1 || bd <= 1) break;

      if (filetype == "JPEG" && (bw < 8 || bh < 8)) break;

      Element lvnode = doc.createElement("Level");
      lvnode.setAttribute("lv", String.valueOf(l));
      lvnode.setAttribute("imageW", String.valueOf(imageW));
      lvnode.setAttribute("imageH", String.valueOf(imageH));
      lvnode.setAttribute("imageD", String.valueOf(imageD));
      lvnode.setAttribute("xspc", String.valueOf(xspc));
      lvnode.setAttribute("yspc", String.valueOf(yspc));
      lvnode.setAttribute("zspc", String.valueOf(zspc));
      lvnode.setAttribute("bitDepth", String.valueOf(bdepth));
      root.appendChild(lvnode);

      Element brksnode = doc.createElement("Bricks");
      brksnode.setAttribute("brick_baseW", String.valueOf(bw));
      brksnode.setAttribute("brick_baseH", String.valueOf(bh));
      brksnode.setAttribute("brick_baseD", String.valueOf(bd));
      lvnode.appendChild(brksnode);

      ArrayList<Brick> bricks = new ArrayList<Brick>();
      int mw, mh, md, mw2, mh2, md2;
      double tx0, ty0, tz0, tx1, ty1, tz1;
      double bx0, by0, bz0, bx1, by1, bz1;
      for (int k = 0; k < imageD; k += bd) {
        if (k > 0) k--;
        for (int j = 0; j < imageH; j += bh) {
          if (j > 0) j--;
          for (int i = 0; i < imageW; i += bw) {
            if (i > 0) i--;
            mw = Math.min(bw, imageW - i);
            mh = Math.min(bh, imageH - j);
            md = Math.min(bd, imageD - k);

            if (force_pow2) {
              mw2 = Pow2(mw);
              mh2 = Pow2(mh);
              md2 = Pow2(md);
            } else {
              mw2 = mw;
              mh2 = mh;
              md2 = md;
            }

            if (filetype == "JPEG") {
              if (mw2 < 8) mw2 = 8;
              if (mh2 < 8) mh2 = 8;
            }

            tx0 = i == 0 ? 0.0d : ((mw2 - mw + 0.5d) / mw2);
            ty0 = j == 0 ? 0.0d : ((mh2 - mh + 0.5d) / mh2);
            tz0 = k == 0 ? 0.0d : ((md2 - md + 0.5d) / md2);

            tx1 = 1.0d - 0.5d / mw2;
            if (mw < bw) tx1 = 1.0d;
            if (imageW - i == bw) tx1 = 1.0d;

            ty1 = 1.0d - 0.5d / mh2;
            if (mh < bh) ty1 = 1.0d;
            if (imageH - j == bh) ty1 = 1.0d;

            tz1 = 1.0d - 0.5d / md2;
            if (md < bd) tz1 = 1.0d;
            if (imageD - k == bd) tz1 = 1.0d;

            bx0 = i == 0 ? 0.0d : (i + 0.5d) / (double) imageW;
            by0 = j == 0 ? 0.0d : (j + 0.5d) / (double) imageH;
            bz0 = k == 0 ? 0.0d : (k + 0.5d) / (double) imageD;

            bx1 = Math.min((i + bw - 0.5d) / (double) imageW, 1.0d);
            if (imageW - i == bw) bx1 = 1.0d;

            by1 = Math.min((j + bh - 0.5d) / (double) imageH, 1.0d);
            if (imageH - j == bh) by1 = 1.0d;

            bz1 = Math.min((k + bd - 0.5d) / (double) imageD, 1.0d);
            if (imageD - k == bd) bz1 = 1.0d;

            int x, y, z;
            x = i - (mw2 - mw);
            y = j - (mh2 - mh);
            z = k - (md2 - md);
            bricks.add(
                new Brick(
                    x, y, z, mw2, mh2, md2, 0, 0, tx0, ty0, tz0, tx1, ty1, tz1, bx0, by0, bz0, bx1,
                    by1, bz1));
          }
        }
      }

      Element fsnode = doc.createElement("Files");
      lvnode.appendChild(fsnode);

      stack = imp.getStack();

      int totalbricknum = nFrame * nCh * bricks.size();
      int curbricknum = 0;
      for (int f = 0; f < nFrame; f++) {
        for (int ch = 0; ch < nCh; ch++) {
          int sizelimit = bdsizelimit * 1024 * 1024;
          int bytecount = 0;
          int filecount = 0;
          int pd_bufsize = Math.max(sizelimit, bw * bh * bd * bdepth / 8);
          byte[] packed_data = new byte[pd_bufsize];
          String base_dataname =
              basename
                  + "_Lv"
                  + String.valueOf(l)
                  + "_Ch"
                  + String.valueOf(ch)
                  + "_Fr"
                  + String.valueOf(f);
          String current_dataname = base_dataname + "_data" + filecount;

          Brick b_first = bricks.get(0);
          if (b_first.z_ != 0) IJ.log("warning");
          int st_z = b_first.z_;
          int ed_z = b_first.z_ + b_first.d_;
          LinkedList<ImageProcessor> iplist = new LinkedList<ImageProcessor>();
          for (int s = st_z; s < ed_z; s++)
            iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));

          //					ImagePlus test;
          //					ImageStack tsst;
          //					test = NewImage.createByteImage("test", imageW, imageH, imageD,
          // NewImage.FILL_BLACK);
          //					tsst = test.getStack();
          for (int i = 0; i < bricks.size(); i++) {
            Brick b = bricks.get(i);

            if (ed_z > b.z_ || st_z < b.z_ + b.d_) {
              if (b.z_ > st_z) {
                for (int s = 0; s < b.z_ - st_z; s++) iplist.pollFirst();
                st_z = b.z_;
              } else if (b.z_ < st_z) {
                IJ.log("warning");
                for (int s = st_z - 1; s > b.z_; s--)
                  iplist.addFirst(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));
                st_z = b.z_;
              }

              if (b.z_ + b.d_ > ed_z) {
                for (int s = ed_z; s < b.z_ + b.d_; s++)
                  iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));
                ed_z = b.z_ + b.d_;
              } else if (b.z_ + b.d_ < ed_z) {
                IJ.log("warning");
                for (int s = 0; s < ed_z - (b.z_ + b.d_); s++) iplist.pollLast();
                ed_z = b.z_ + b.d_;
              }
            } else {
              IJ.log("warning");
              iplist.clear();
              st_z = b.z_;
              ed_z = b.z_ + b.d_;
              for (int s = st_z; s < ed_z; s++)
                iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1)));
            }

            if (iplist.size() != b.d_) {
              IJ.log("Stack Error");
              return;
            }

            //						int zz = st_z;

            int bsize = 0;
            byte[] bdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8];
            Iterator<ImageProcessor> ipite = iplist.iterator();
            while (ipite.hasNext()) {

              //							ImageProcessor tsip = tsst.getProcessor(zz+1);

              ImageProcessor ip = ipite.next();
              ip.setRoi(b.x_, b.y_, b.w_, b.h_);
              if (bdepth == 8) {
                byte[] data = (byte[]) ip.crop().getPixels();
                System.arraycopy(data, 0, bdata, bsize, data.length);
                bsize += data.length;
              } else if (bdepth == 16) {
                ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
                short[] data = (short[]) ip.crop().getPixels();
                for (short e : data) buffer.putShort(e);
                System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length);
                bsize += buffer.array().length;
              } else if (bdepth == 32) {
                ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8);
                buffer.order(ByteOrder.LITTLE_ENDIAN);
                float[] data = (float[]) ip.crop().getPixels();
                for (float e : data) buffer.putFloat(e);
                System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length);
                bsize += buffer.array().length;
              }
            }

            String filename =
                basename
                    + "_Lv"
                    + String.valueOf(l)
                    + "_Ch"
                    + String.valueOf(ch)
                    + "_Fr"
                    + String.valueOf(f)
                    + "_ID"
                    + String.valueOf(i);

            int offset = bytecount;
            int datasize = bdata.length;

            if (filetype == "RAW") {
              int dummy = -1;
              // do nothing
            }
            if (filetype == "JPEG" && bdepth == 8) {
              try {
                DataBufferByte db = new DataBufferByte(bdata, datasize);
                Raster raster = Raster.createPackedRaster(db, b.w_, b.h_ * b.d_, 8, null);
                BufferedImage img =
                    new BufferedImage(b.w_, b.h_ * b.d_, BufferedImage.TYPE_BYTE_GRAY);
                img.setData(raster);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
                String format = "jpg";
                Iterator<javax.imageio.ImageWriter> iter =
                    ImageIO.getImageWritersByFormatName("jpeg");
                javax.imageio.ImageWriter writer = iter.next();
                ImageWriteParam iwp = writer.getDefaultWriteParam();
                iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                iwp.setCompressionQuality((float) jpeg_quality * 0.01f);
                writer.setOutput(ios);
                writer.write(null, new IIOImage(img, null, null), iwp);
                // ImageIO.write(img, format, baos);
                bdata = baos.toByteArray();
                datasize = bdata.length;
              } catch (IOException e) {
                e.printStackTrace();
                return;
              }
            }
            if (filetype == "ZLIB") {
              byte[] tmpdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8];
              Deflater compresser = new Deflater();
              compresser.setInput(bdata);
              compresser.setLevel(Deflater.DEFAULT_COMPRESSION);
              compresser.setStrategy(Deflater.DEFAULT_STRATEGY);
              compresser.finish();
              datasize = compresser.deflate(tmpdata);
              bdata = tmpdata;
              compresser.end();
            }

            if (bytecount + datasize > sizelimit && bytecount > 0) {
              BufferedOutputStream fis = null;
              try {
                File file = new File(directory + current_dataname);
                fis = new BufferedOutputStream(new FileOutputStream(file));
                fis.write(packed_data, 0, bytecount);
              } catch (IOException e) {
                e.printStackTrace();
                return;
              } finally {
                try {
                  if (fis != null) fis.close();
                } catch (IOException e) {
                  e.printStackTrace();
                  return;
                }
              }
              filecount++;
              current_dataname = base_dataname + "_data" + filecount;
              bytecount = 0;
              offset = 0;
              System.arraycopy(bdata, 0, packed_data, bytecount, datasize);
              bytecount += datasize;
            } else {
              System.arraycopy(bdata, 0, packed_data, bytecount, datasize);
              bytecount += datasize;
            }

            Element filenode = doc.createElement("File");
            filenode.setAttribute("filename", current_dataname);
            filenode.setAttribute("channel", String.valueOf(ch));
            filenode.setAttribute("frame", String.valueOf(f));
            filenode.setAttribute("brickID", String.valueOf(i));
            filenode.setAttribute("offset", String.valueOf(offset));
            filenode.setAttribute("datasize", String.valueOf(datasize));
            filenode.setAttribute("filetype", String.valueOf(filetype));

            fsnode.appendChild(filenode);

            curbricknum++;
            IJ.showProgress((double) (curbricknum) / (double) (totalbricknum));
          }
          if (bytecount > 0) {
            BufferedOutputStream fis = null;
            try {
              File file = new File(directory + current_dataname);
              fis = new BufferedOutputStream(new FileOutputStream(file));
              fis.write(packed_data, 0, bytecount);
            } catch (IOException e) {
              e.printStackTrace();
              return;
            } finally {
              try {
                if (fis != null) fis.close();
              } catch (IOException e) {
                e.printStackTrace();
                return;
              }
            }
          }
        }
      }

      for (int i = 0; i < bricks.size(); i++) {
        Brick b = bricks.get(i);
        Element bricknode = doc.createElement("Brick");
        bricknode.setAttribute("id", String.valueOf(i));
        bricknode.setAttribute("st_x", String.valueOf(b.x_));
        bricknode.setAttribute("st_y", String.valueOf(b.y_));
        bricknode.setAttribute("st_z", String.valueOf(b.z_));
        bricknode.setAttribute("width", String.valueOf(b.w_));
        bricknode.setAttribute("height", String.valueOf(b.h_));
        bricknode.setAttribute("depth", String.valueOf(b.d_));
        brksnode.appendChild(bricknode);

        Element tboxnode = doc.createElement("tbox");
        tboxnode.setAttribute("x0", String.valueOf(b.tx0_));
        tboxnode.setAttribute("y0", String.valueOf(b.ty0_));
        tboxnode.setAttribute("z0", String.valueOf(b.tz0_));
        tboxnode.setAttribute("x1", String.valueOf(b.tx1_));
        tboxnode.setAttribute("y1", String.valueOf(b.ty1_));
        tboxnode.setAttribute("z1", String.valueOf(b.tz1_));
        bricknode.appendChild(tboxnode);

        Element bboxnode = doc.createElement("bbox");
        bboxnode.setAttribute("x0", String.valueOf(b.bx0_));
        bboxnode.setAttribute("y0", String.valueOf(b.by0_));
        bboxnode.setAttribute("z0", String.valueOf(b.bz0_));
        bboxnode.setAttribute("x1", String.valueOf(b.bx1_));
        bboxnode.setAttribute("y1", String.valueOf(b.by1_));
        bboxnode.setAttribute("z1", String.valueOf(b.bz1_));
        bricknode.appendChild(bboxnode);
      }

      if (l < lv - 1) {
        imp = WindowManager.getImage(lvImgTitle.get(l + 1));
        int[] newdims = imp.getDimensions();
        imageW = newdims[0];
        imageH = newdims[1];
        imageD = newdims[3];
        xspc = orgxspc * ((double) orgW / (double) imageW);
        yspc = orgyspc * ((double) orgH / (double) imageH);
        zspc = orgzspc * ((double) orgD / (double) imageD);
        bdepth = imp.getBitDepth();
      }
    }

    File newXMLfile = new File(directory + basename + ".vvd");
    writeXML(newXMLfile, doc);

    for (int l = 1; l < lv; l++) {
      imp = WindowManager.getImage(lvImgTitle.get(l));
      imp.changes = false;
      imp.close();
    }
  }
Esempio n. 15
0
  public BufferedImage decompress(final QuickTime.ImageDesc pDescription, final InputStream pStream)
      throws IOException {
    byte[] data = new byte[pDescription.dataSize];

    DataInputStream stream = new DataInputStream(pStream);
    try {
      stream.readFully(data, 0, pDescription.dataSize);
    } finally {
      stream.close();
    }

    DataBuffer buffer = new DataBufferByte(data, data.length);

    WritableRaster raster;

    // TODO: Depth parameter can be 1-32 (color) or 33-40 (gray scale)
    switch (pDescription.depth) {
      case 40: // 8 bit gray (untested)
        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width,
                1,
                new int[] {0},
                null);
        break;
      case 24: // 24 bit RGB
        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width * 3,
                3,
                new int[] {0, 1, 2},
                null);
        break;
      case 32: // 32 bit ARGB
        // WORKAROUND: There is a bug in the way Java 2D interprets the band offsets in
        // Raster.createInterleavedRaster (see below) before Java 6. So, instead of
        // passing a correct offset array below, we swap channel 1 & 3 to make it ABGR...
        for (int y = 0; y < pDescription.height; y++) {
          for (int x = 0; x < pDescription.width; x++) {
            int offset = 4 * y * pDescription.width + x * 4;
            byte temp = data[offset + 1];
            data[offset + 1] = data[offset + 3];
            data[offset + 3] = temp;
          }
        }

        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width * 4,
                4,
                new int[] {3, 2, 1, 0}, // B & R mixed up. {1, 2, 3, 0} is correct
                null);
        break;
      default:
        throw new IIOException("Unsupported RAW depth: " + pDescription.depth);
    }

    ColorModel cm =
        new ComponentColorModel(
            pDescription.depth <= 32
                ? ColorSpace.getInstance(ColorSpace.CS_sRGB)
                : ColorSpace.getInstance(ColorSpace.CS_GRAY),
            pDescription.depth == 32,
            false,
            pDescription.depth == 32 ? Transparency.TRANSLUCENT : Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
  }
Esempio n. 16
0
  public synchronized RenderedImage runDCRaw(dcrawMode mode, boolean secondaryPixels)
      throws IOException, UnknownImageTypeException, BadImageFileException {
    if (!m_decodable || (mode == dcrawMode.full && m_rawColors != 3))
      throw new UnknownImageTypeException("Unsuported Camera");

    RenderedImage result = null;

    File of = null;

    try {
      if (mode == dcrawMode.preview) {
        if (m_thumbWidth >= 1024 && m_thumbHeight >= 768) {
          mode = dcrawMode.thumb;
        }
      }

      long t1 = System.currentTimeMillis();

      of = File.createTempFile("LZRAWTMP", ".ppm");

      boolean four_colors = false;
      final String makeModel = m_make + ' ' + m_model;
      for (String s : four_color_cameras)
        if (s.equalsIgnoreCase(makeModel)) {
          four_colors = true;
          break;
        }

      if (secondaryPixels) runDCRawInfo(true);

      String cmd[];
      switch (mode) {
        case full:
          if (four_colors)
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-f",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-o",
                  "0",
                  "-4",
                  m_fileName
                };
          else if (m_filters == -1 || (m_make != null && m_make.equalsIgnoreCase("SIGMA")))
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-o",
                  "0",
                  "-4",
                  m_fileName
                };
          else if (secondaryPixels)
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-j",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-s",
                  "1",
                  "-d",
                  "-4",
                  m_fileName
                };
          else
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-j",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-d",
                  "-4",
                  m_fileName
                };
          break;
        case preview:
          cmd =
              new String[] {
                DCRAW_PATH,
                "-F",
                of.getAbsolutePath(),
                "-v",
                "-t",
                "0",
                "-o",
                "1",
                "-w",
                "-h",
                m_fileName
              };
          break;
        case thumb:
          cmd = new String[] {DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-e", m_fileName};
          break;
        default:
          throw new IllegalArgumentException("Unknown mode " + mode);
      }

      String ofName = null;

      synchronized (DCRaw.class) {
        Process p = null;
        InputStream dcrawStdErr;
        InputStream dcrawStdOut;
        if (ForkDaemon.INSTANCE != null) {
          ForkDaemon.INSTANCE.invoke(cmd);
          dcrawStdErr = ForkDaemon.INSTANCE.getStdErr();
          dcrawStdOut = ForkDaemon.INSTANCE.getStdOut();
        } else {
          p = Runtime.getRuntime().exec(cmd);
          dcrawStdErr = new BufferedInputStream(p.getErrorStream());
          dcrawStdOut = p.getInputStream();
        }

        String line, args;
        // output expected on stderr
        while ((line = readln(dcrawStdErr)) != null) {
          // System.out.println(line);

          if ((args = match(line, DCRAW_OUTPUT)) != null)
            ofName = args.substring(0, args.indexOf(" ..."));
        }

        // Flush stdout just in case...
        while ((line = readln(dcrawStdOut)) != null) ; // System.out.println(line);

        if (p != null) {
          dcrawStdErr.close();

          try {
            p.waitFor();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          m_error = p.exitValue();
          p.destroy();
        } else m_error = 0;
      }

      System.out.println("dcraw value: " + m_error);

      if (m_error > 0) {
        of.delete();
        throw new BadImageFileException(of);
      }

      if (!ofName.equals(of.getPath())) {
        of.delete();
        of = new File(ofName);
      }

      if (of.getName().endsWith(".jpg") || of.getName().endsWith(".tiff")) {
        if (of.getName().endsWith(".jpg")) {
          try {
            LCJPEGReader jpegReader = new LCJPEGReader(of.getPath());
            result = jpegReader.getImage();
          } catch (Exception e) {
            e.printStackTrace();
          }
        } else {
          try {
            LCTIFFReader tiffReader = new LCTIFFReader(of.getPath());
            result = tiffReader.getImage(null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        long t2 = System.currentTimeMillis();

        int totalData =
            result.getWidth()
                * result.getHeight()
                * result.getColorModel().getNumColorComponents()
                * (result.getColorModel().getTransferType() == DataBuffer.TYPE_BYTE ? 1 : 2);

        System.out.println("Read " + totalData + " bytes in " + (t2 - t1) + "ms");
      } else {
        ImageData imageData;
        try {
          imageData = readPPM(of);
        } catch (Exception e) {
          e.printStackTrace();
          throw new BadImageFileException(of, e);
        }

        // do not change the initial image geometry
        // m_width = Math.min(m_width, imageData.width);
        // m_height = Math.min(m_height, imageData.height);

        long t2 = System.currentTimeMillis();

        int totalData =
            imageData.width
                * imageData.height
                * imageData.bands
                * (imageData.dataType == DataBuffer.TYPE_BYTE ? 1 : 2);

        System.out.println("Read " + totalData + " bytes in " + (t2 - t1) + "ms");

        final ColorModel cm;
        if (mode == dcrawMode.full) {
          if (imageData.bands == 1) {
            cm =
                new ComponentColorModel(
                    ColorSpace.getInstance(ColorSpace.CS_GRAY),
                    false,
                    false,
                    Transparency.OPAQUE,
                    DataBuffer.TYPE_USHORT);
          } else {
            cm = JAIContext.colorModel_linear16;
          }
        } else {
          if (imageData.bands == 3)
            cm =
                new ComponentColorModel(
                    JAIContext.sRGBColorSpace,
                    false,
                    false,
                    Transparency.OPAQUE,
                    DataBuffer.TYPE_BYTE);
          else if (imageData.bands == 4)
            cm =
                new ComponentColorModel(
                    JAIContext.CMYKColorSpace,
                    false,
                    false,
                    Transparency.OPAQUE,
                    imageData.dataType);
          else throw new UnknownImageTypeException("Weird number of bands: " + imageData.bands);
        }

        final DataBuffer buf =
            imageData.dataType == DataBuffer.TYPE_BYTE
                ? new DataBufferByte(
                    (byte[]) imageData.data, imageData.bands * imageData.width * imageData.height)
                : new DataBufferUShort(
                    (short[]) imageData.data, imageData.bands * imageData.width * imageData.height);

        final WritableRaster raster =
            Raster.createInterleavedRaster(
                buf,
                imageData.width,
                imageData.height,
                imageData.bands * imageData.width,
                imageData.bands,
                imageData.bands == 3 ? new int[] {0, 1, 2} : new int[] {0},
                null);

        result = new BufferedImage(cm, raster, false, null);
      }
    } catch (IOException e) {
      if (of != null) of.delete();
      throw e;
    } finally {
      if (of != null) of.delete();
    }
    return result;
  }
  /**
   * Slow filter.
   *
   * @param src the src.
   * @param dst the dst.
   * @return the int.
   */
  private int slowFilter(Raster src, WritableRaster dst) {
    // TODO: make correct interpolation
    // TODO: what if there are different data types?

    Rectangle srcBounds = src.getBounds();
    Rectangle dstBounds = dst.getBounds();
    Rectangle normDstBounds = new Rectangle(0, 0, dstBounds.width, dstBounds.height);
    Rectangle bounds = getBounds2D(src).getBounds().intersection(normDstBounds);

    AffineTransform inv = null;
    try {
      inv = at.createInverse();
    } catch (NoninvertibleTransformException e) {
      return -1;
    }

    double[] m = new double[6];
    inv.getMatrix(m);

    int minSrcX = srcBounds.x;
    int minSrcY = srcBounds.y;
    int maxSrcX = srcBounds.x + srcBounds.width;
    int maxSrcY = srcBounds.y + srcBounds.height;

    int minX = bounds.x + dstBounds.x;
    int minY = bounds.y + dstBounds.y;
    int maxX = minX + bounds.width;
    int maxY = minY + bounds.height;

    int hx = (int) (m[0] * 256);
    int hy = (int) (m[1] * 256);
    int vx = (int) (m[2] * 256);
    int vy = (int) (m[3] * 256);
    int sx = (int) (m[4] * 256) + hx * bounds.x + vx * bounds.y + (srcBounds.x) * 256;
    int sy = (int) (m[5] * 256) + hy * bounds.x + vy * bounds.y + (srcBounds.y) * 256;

    vx -= hx * bounds.width;
    vy -= hy * bounds.width;

    if (src.getTransferType() == dst.getTransferType()) {
      for (int y = minY; y < maxY; y++) {
        for (int x = minX; x < maxX; x++) {
          int px = sx >> 8;
          int py = sy >> 8;
          if (px >= minSrcX && py >= minSrcY && px < maxSrcX && py < maxSrcY) {
            Object val = src.getDataElements(px, py, null);
            dst.setDataElements(x, y, val);
          }
          sx += hx;
          sy += hy;
        }
        sx += vx;
        sy += vy;
      }
    } else {
      float pixel[] = null;
      for (int y = minY; y < maxY; y++) {
        for (int x = minX; x < maxX; x++) {
          int px = sx >> 8;
          int py = sy >> 8;
          if (px >= minSrcX && py >= minSrcY && px < maxSrcX && py < maxSrcY) {
            pixel = src.getPixel(px, py, pixel);
            dst.setPixel(x, y, pixel);
          }
          sx += hx;
          sy += hy;
        }
        sx += vx;
        sy += vy;
      }
    }

    return 0;
  }
  /**
   * Ipp filter.
   *
   * @param src the src.
   * @param dst the dst.
   * @param imageType the image type.
   * @return the int.
   */
  @SuppressWarnings("unused")
  private int ippFilter(Raster src, WritableRaster dst, int imageType) {
    int srcStride, dstStride;
    boolean skipChannel = false;
    int channels;
    int offsets[] = null;

    switch (imageType) {
      case BufferedImage.TYPE_INT_RGB:
      case BufferedImage.TYPE_INT_BGR:
        {
          channels = 4;
          srcStride = src.getWidth() * 4;
          dstStride = dst.getWidth() * 4;
          skipChannel = true;
          break;
        }

      case BufferedImage.TYPE_INT_ARGB:
      case BufferedImage.TYPE_INT_ARGB_PRE:
      case BufferedImage.TYPE_4BYTE_ABGR:
      case BufferedImage.TYPE_4BYTE_ABGR_PRE:
        {
          channels = 4;
          srcStride = src.getWidth() * 4;
          dstStride = dst.getWidth() * 4;
          break;
        }

      case BufferedImage.TYPE_BYTE_GRAY:
      case BufferedImage.TYPE_BYTE_INDEXED:
        {
          channels = 1;
          srcStride = src.getWidth();
          dstStride = dst.getWidth();
          break;
        }

      case BufferedImage.TYPE_3BYTE_BGR:
        {
          channels = 3;
          srcStride = src.getWidth() * 3;
          dstStride = dst.getWidth() * 3;
          break;
        }

      case BufferedImage.TYPE_USHORT_GRAY: // TODO - could be done in
        // native code?
      case BufferedImage.TYPE_USHORT_565_RGB:
      case BufferedImage.TYPE_USHORT_555_RGB:
      case BufferedImage.TYPE_BYTE_BINARY:
        {
          return slowFilter(src, dst);
        }

      default:
        {
          SampleModel srcSM = src.getSampleModel();
          SampleModel dstSM = dst.getSampleModel();

          if (srcSM instanceof PixelInterleavedSampleModel
              && dstSM instanceof PixelInterleavedSampleModel) {
            // Check PixelInterleavedSampleModel
            if (srcSM.getDataType() != DataBuffer.TYPE_BYTE
                || dstSM.getDataType() != DataBuffer.TYPE_BYTE) {
              return slowFilter(src, dst);
            }

            channels = srcSM.getNumBands(); // Have IPP functions for 1,
            // 3 and 4 channels
            if (channels != 1 && channels != 3 && channels != 4) {
              return slowFilter(src, dst);
            }

            int dataTypeSize = DataBuffer.getDataTypeSize(srcSM.getDataType()) / 8;

            srcStride = ((ComponentSampleModel) srcSM).getScanlineStride() * dataTypeSize;
            dstStride = ((ComponentSampleModel) dstSM).getScanlineStride() * dataTypeSize;
          } else if (srcSM instanceof SinglePixelPackedSampleModel
              && dstSM instanceof SinglePixelPackedSampleModel) {
            // Check SinglePixelPackedSampleModel
            SinglePixelPackedSampleModel sppsm1 = (SinglePixelPackedSampleModel) srcSM;
            SinglePixelPackedSampleModel sppsm2 = (SinglePixelPackedSampleModel) dstSM;

            // No IPP function for this type
            if (sppsm1.getDataType() == DataBuffer.TYPE_USHORT) {
              return slowFilter(src, dst);
            }

            channels = sppsm1.getNumBands();
            // Have IPP functions for 1, 3 and 4 channels
            if (channels != 1 && channels != 3 && channels != 4) {
              return slowFilter(src, dst);
            }

            // Check compatibility of sample models
            if (sppsm1.getDataType() != sppsm2.getDataType()
                || !Arrays.equals(sppsm1.getBitOffsets(), sppsm2.getBitOffsets())
                || !Arrays.equals(sppsm1.getBitMasks(), sppsm2.getBitMasks())) {
              return slowFilter(src, dst);
            }

            for (int i = 0; i < channels; i++) {
              if (sppsm1.getSampleSize(i) != 8) {
                return slowFilter(src, dst);
              }
            }

            if (channels == 3) {
              channels = 4;
            }

            int dataTypeSize = DataBuffer.getDataTypeSize(sppsm1.getDataType()) / 8;

            srcStride = sppsm1.getScanlineStride() * dataTypeSize;
            dstStride = sppsm2.getScanlineStride() * dataTypeSize;
          } else {
            return slowFilter(src, dst);
          }

          // Fill offsets if there's a child raster
          if (src.getParent() != null || dst.getParent() != null) {
            if (src.getSampleModelTranslateX() != 0
                || src.getSampleModelTranslateY() != 0
                || dst.getSampleModelTranslateX() != 0
                || dst.getSampleModelTranslateY() != 0) {
              offsets = new int[4];
              offsets[0] = -src.getSampleModelTranslateX() + src.getMinX();
              offsets[1] = -src.getSampleModelTranslateY() + src.getMinY();
              offsets[2] = -dst.getSampleModelTranslateX() + dst.getMinX();
              offsets[3] = -dst.getSampleModelTranslateY() + dst.getMinY();
            }
          }
        }
    }

    double m00 = at.getScaleX();
    double m01 = at.getShearX();
    double m02 = at.getTranslateX();
    double m10 = at.getShearY();
    double m11 = at.getScaleY();
    double m12 = at.getTranslateY();

    Object srcData, dstData;
    AwtImageBackdoorAccessor dbAccess = AwtImageBackdoorAccessor.getInstance();
    try {
      srcData = dbAccess.getData(src.getDataBuffer());
      dstData = dbAccess.getData(dst.getDataBuffer());
    } catch (IllegalArgumentException e) {
      return -1; // Unknown data buffer type
    }

    return ippAffineTransform(
        m00,
        m01,
        m02,
        m10,
        m11,
        m12,
        srcData,
        src.getWidth(),
        src.getHeight(),
        srcStride,
        dstData,
        dst.getWidth(),
        dst.getHeight(),
        dstStride,
        iType,
        channels,
        skipChannel,
        offsets);
  }
  /** Convert standard img to a buffered image. */
  public static BufferedImage convertImage(Image img) {
    int height = img.getHeight(null), width = img.getWidth(null);
    // FloatMatrix fm = new FloatMatrix( height, width ) ;
    PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, false);
    try {
      grabber.grabPixels();
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    Object pixels = grabber.getPixels();
    ColorModel cm = grabber.getColorModel();

    BufferedImage bimg = null;

    // REVISIT  Makes some unwarranted assumptions about the layout of the PixelGrabber data
    // as being either int (ARGB?) or byte (gray scale?)
    if (pixels instanceof int[]) {
      int[] arr = (int[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        int l = arr[j];
        barray[k++] = (byte) (l & 0xFF);
        barray[k++] = (byte) ((l >>> 8) & 0xFF);
        barray[k++] = (byte) ((l >>> 16) & 0xFF);
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else if (pixels instanceof byte[]) { // Assume gray scale model?
      byte[] arr = (byte[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        byte l = arr[j];
        barray[k++] = l;
        barray[k++] = l;
        barray[k++] = l;
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else {
      throw new RuntimeException("Unexpected data.");
    }
    return bimg;
  }
Esempio n. 20
0
  /**
   * Loads PNG files and returns the result as an int[][]. The only PNG formats permitted are those
   * with up to 256 grays (including simple black and white) or indexed colors from an up to
   * 256-sized color table. Each integer value represents the gray level or the color table index
   * value of the pixel. The Y dimension is not flipped.
   */
  public static int[][] loadPNGFile(InputStream str) throws IOException {
    // read the bytes into a byte array
    BufferedInputStream stream = new BufferedInputStream(str);
    ArrayList list = new ArrayList();
    int count = 0;
    while (true) {
      byte[] buffer = new byte[16384 * 16];
      int len = stream.read(buffer);
      if (len <= 0) // all done
      break;
      else if (len < buffer.length) {
        byte[] buf2 = new byte[len];
        System.arraycopy(buffer, 0, buf2, 0, len);
        buffer = buf2;
      }
      count += len;
      list.add(buffer);
    }
    byte[] data = new byte[count];
    int cur = 0;
    for (int i = 0; i < list.size(); i++) {
      byte[] b = (byte[]) (list.get(i));
      System.arraycopy(b, 0, data, cur, b.length);
      cur += b.length;
    }

    // Next convert the byte array to a buffered image
    BufferedImage image = ((ToolkitImage) (new ImageIcon(data).getImage())).getBufferedImage();

    // Is the color model something we can use?
    int type = image.getType();
    if (type == BufferedImage.TYPE_BYTE_BINARY || type == BufferedImage.TYPE_BYTE_GRAY) {
      int w = image.getWidth();
      int h = image.getHeight();
      int[][] result = new int[w][h];
      // obviously this could be done more efficiently
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++) result[i][j] = (image.getRGB(i, j) & 0xFF);
      return result;
    } else if (type == BufferedImage.TYPE_BYTE_INDEXED) {
      Raster raster = image.getRaster();
      if (raster.getTransferType() != DataBuffer.TYPE_BYTE) // uh oh
      throw new IOException("Input Stream must contain an image with byte data if indexed.");
      byte[] pixel = new byte[1];
      int w = image.getWidth();
      int h = image.getHeight();
      int[][] result = new int[w][h];
      // obviously this could be done more efficiently
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++) {
          result[i][j] = ((byte[]) (raster.getDataElements(i, j, pixel)))[0];
          if (result[i][j] < 0) result[i][j] += 256;
        }
      return result;
    }
    // else if (type == TYPE_USHORT_GRAY)   // at present we don't handle shorts
    //    {
    //    }
    else
      throw new IOException(
          "Input Stream must contain a binary, byte-sized grayscale, or byte-sized indexed color scheme: "
              + image);
  }
Esempio n. 21
0
 public Raster getRaster(int x, int y, int w, int h) {
   return Raster.createBandedRaster(0, 0, 0, 0, NULL_POINT);
 }
Esempio n. 22
0
  public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    checkIndex(imageIndex);
    readHeader();

    if (iis == null) throw new IllegalStateException("input is null");

    BufferedImage img;

    clearAbortRequest();
    processImageStarted(imageIndex);

    if (param == null) param = getDefaultReadParam();

    sourceRegion = new Rectangle(0, 0, 0, 0);
    destinationRegion = new Rectangle(0, 0, 0, 0);

    computeRegions(
        param, this.width, this.height, param.getDestination(), sourceRegion, destinationRegion);

    scaleX = param.getSourceXSubsampling();
    scaleY = param.getSourceYSubsampling();

    // If the destination band is set used it
    sourceBands = param.getSourceBands();
    destBands = param.getDestinationBands();

    seleBand = (sourceBands != null) && (destBands != null);
    noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) || seleBand;

    if (!seleBand) {
      sourceBands = new int[colorPlanes];
      destBands = new int[colorPlanes];
      for (int i = 0; i < colorPlanes; i++) destBands[i] = sourceBands[i] = i;
    }

    // If the destination is provided, then use it.  Otherwise, create new one
    bi = param.getDestination();

    // Get the image data.
    WritableRaster raster = null;

    if (bi == null) {
      if (sampleModel != null && colorModel != null) {
        sampleModel =
            sampleModel.createCompatibleSampleModel(
                destinationRegion.width + destinationRegion.x,
                destinationRegion.height + destinationRegion.y);
        if (seleBand) sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
        raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
        bi = new BufferedImage(colorModel, raster, false, null);
      }
    } else {
      raster = bi.getWritableTile(0, 0);
      sampleModel = bi.getSampleModel();
      colorModel = bi.getColorModel();

      noTransform &= destinationRegion.equals(raster.getBounds());
    }

    byte bdata[] = null; // buffer for byte data

    if (sampleModel.getDataType() == DataBuffer.TYPE_BYTE)
      bdata = (byte[]) ((DataBufferByte) raster.getDataBuffer()).getData();

    readImage(bdata);

    if (abortRequested()) processReadAborted();
    else processImageComplete();

    return bi;
  }
Esempio n. 23
0
  /**
   * Paint the image onto a Graphics object. The painting is performed tile-by-tile, and includes a
   * grey region covering the unused portion of image tiles as well as the general background. At
   * this point the image must be byte data.
   */
  public synchronized void paintComponent(Graphics g) {

    Graphics2D g2D = null;
    if (g instanceof Graphics2D) {
      g2D = (Graphics2D) g;
    } else {
      return;
    }

    // if source is null, it's just a component
    if (source == null) {
      g2D.setColor(getBackground());
      g2D.fillRect(0, 0, componentWidth, componentHeight);
      return;
    }

    int transX = -originX;
    int transY = -originY;

    // Get the clipping rectangle and translate it into image coordinates.
    Rectangle clipBounds = g.getClipBounds();

    if (clipBounds == null) {
      clipBounds = new Rectangle(0, 0, componentWidth, componentHeight);
    }

    // clear the background (clip it) [minimal optimization here]
    if (transX > 0
        || transY > 0
        || transX < (componentWidth - source.getWidth())
        || transY < (componentHeight - source.getHeight())) {
      g2D.setColor(getBackground());
      g2D.fillRect(0, 0, componentWidth, componentHeight);
    }

    clipBounds.translate(-transX, -transY);

    // Determine the extent of the clipping region in tile coordinates.
    int txmin, txmax, tymin, tymax;
    int ti, tj;

    txmin = XtoTileX(clipBounds.x);
    txmin = Math.max(txmin, minTileX);
    txmin = Math.min(txmin, maxTileX);

    txmax = XtoTileX(clipBounds.x + clipBounds.width - 1);
    txmax = Math.max(txmax, minTileX);
    txmax = Math.min(txmax, maxTileX);

    tymin = YtoTileY(clipBounds.y);
    tymin = Math.max(tymin, minTileY);
    tymin = Math.min(tymin, maxTileY);

    tymax = YtoTileY(clipBounds.y + clipBounds.height - 1);
    tymax = Math.max(tymax, minTileY);
    tymax = Math.min(tymax, maxTileY);
    Insets insets = getInsets();

    // Loop over tiles within the clipping region
    for (tj = tymin; tj <= tymax; tj++) {
      for (ti = txmin; ti <= txmax; ti++) {
        int tx = TileXtoX(ti);
        int ty = TileYtoY(tj);

        Raster tile = source.getTile(ti, tj);
        if (tile != null) {
          DataBuffer dataBuffer = tile.getDataBuffer();

          WritableRaster wr = tile.createWritableRaster(sampleModel, dataBuffer, null);

          BufferedImage bi =
              new BufferedImage(colorModel, wr, colorModel.isAlphaPremultiplied(), null);

          // correctly handles band offsets
          if (brightnessEnabled == true) {
            SampleModel sm =
                sampleModel.createCompatibleSampleModel(tile.getWidth(), tile.getHeight());

            WritableRaster raster = RasterFactory.createWritableRaster(sm, null);

            BufferedImage bimg =
                new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);

            // don't move this code
            ByteLookupTable lutTable = new ByteLookupTable(0, lutData);
            LookupOp lookup = new LookupOp(lutTable, null);
            lookup.filter(bi, bimg);

            g2D.drawImage(bimg, biop, tx + transX + insets.left, ty + transY + insets.top);
          } else {
            AffineTransform transform;

            transform =
                AffineTransform.getTranslateInstance(
                    tx + transX + insets.left, ty + transY + insets.top);

            g2D.drawRenderedImage(bi, transform);
          }
        }
      }
    }
  }
Esempio n. 24
0
  @Override
  public BufferedImage read(final int imageIndex, final ImageReadParam param) throws IOException {
    Iterator<ImageTypeSpecifier> imageTypes = getImageTypes(imageIndex);
    ImageTypeSpecifier rawType = getRawImageType(imageIndex);

    if (header.getPaletteInfo() != PCX.PALETTEINFO_COLOR
        && header.getPaletteInfo() != PCX.PALETTEINFO_GRAY) {
      processWarningOccurred(
          String.format(
              "Unsupported color mode: %d, colors may look incorrect", header.getPaletteInfo()));
    }

    int width = getWidth(imageIndex);
    int height = getHeight(imageIndex);

    BufferedImage destination = getDestination(param, imageTypes, width, height);

    Rectangle srcRegion = new Rectangle();
    Rectangle destRegion = new Rectangle();
    computeRegions(param, width, height, destination, srcRegion, destRegion);

    WritableRaster destRaster =
        clipToRect(
            destination.getRaster(),
            destRegion,
            param != null ? param.getDestinationBands() : null);
    checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands());

    int compression = header.getCompression();

    // Wrap input (COMPRESSION_RLE is really the only value allowed)
    DataInput input =
        compression == PCX.COMPRESSION_RLE
            ? new DataInputStream(
                new DecoderStream(IIOUtil.createStreamAdapter(imageInput), new RLEDecoder()))
            : imageInput;

    int xSub = param != null ? param.getSourceXSubsampling() : 1;
    int ySub = param != null ? param.getSourceYSubsampling() : 1;

    processImageStarted(imageIndex);

    if (rawType.getColorModel() instanceof IndexColorModel && header.getChannels() > 1) {
      // Bit planes!
      // Create raster from a default 8 bit layout
      WritableRaster rowRaster = GRAYSCALE.createBufferedImage(header.getWidth(), 1).getRaster();

      // Clip to source region
      Raster clippedRow =
          clipRowToRect(
              rowRaster,
              srcRegion,
              param != null ? param.getSourceBands() : null,
              param != null ? param.getSourceXSubsampling() : 1);

      int planeWidth = header.getBytesPerLine();
      byte[] planeData = new byte[planeWidth * 8];

      byte[] rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData();

      for (int y = 0; y < height; y++) {
        switch (header.getBitsPerPixel()) {
          case 1:
            readRowByte(
                input,
                srcRegion,
                xSub,
                ySub,
                planeData,
                0,
                planeWidth * header.getChannels(),
                destRaster,
                clippedRow,
                y);
            break;
          default:
            throw new AssertionError();
        }

        int pixelPos = 0;
        for (int planePos = 0; planePos < planeWidth; planePos++) {
          BitRotator.bitRotateCW(planeData, planePos, planeWidth, rowDataByte, pixelPos, 1);
          pixelPos += 8;
        }

        processImageProgress(100f * y / height);

        if (y >= srcRegion.y + srcRegion.height) {
          break;
        }

        if (abortRequested()) {
          processReadAborted();
          break;
        }
      }
    } else if (header.getBitsPerPixel() == 24 || header.getBitsPerPixel() == 32) {
      // Can't use width here, as we need to take bytesPerLine into account, and re-create a width
      // based on this
      int rowWidth = (header.getBytesPerLine() * 8) / header.getBitsPerPixel();
      WritableRaster rowRaster = rawType.createBufferedImage(rowWidth, 1).getRaster();

      // Clip to source region
      Raster clippedRow =
          clipRowToRect(
              rowRaster,
              srcRegion,
              param != null ? param.getSourceBands() : null,
              param != null ? param.getSourceXSubsampling() : 1);

      for (int y = 0; y < height; y++) {
        byte[] rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData();
        readRowByte(
            input,
            srcRegion,
            xSub,
            ySub,
            rowDataByte,
            0,
            rowDataByte.length,
            destRaster,
            clippedRow,
            y);

        processImageProgress(100f * y / height);

        if (y >= srcRegion.y + srcRegion.height) {
          break;
        }

        if (abortRequested()) {
          processReadAborted();
          break;
        }
      }
    } else {
      // Can't use width here, as we need to take bytesPerLine into account, and re-create a width
      // based on this
      int rowWidth = (header.getBytesPerLine() * 8) / header.getBitsPerPixel();
      WritableRaster rowRaster = rawType.createBufferedImage(rowWidth, 1).getRaster();

      // Clip to source region
      Raster clippedRow =
          clipRowToRect(
              rowRaster,
              srcRegion,
              param != null ? param.getSourceBands() : null,
              param != null ? param.getSourceXSubsampling() : 1);

      for (int y = 0; y < height; y++) {
        for (int c = 0; c < header.getChannels(); c++) {
          WritableRaster destChannel =
              destRaster.createWritableChild(
                  destRaster.getMinX(),
                  destRaster.getMinY(),
                  destRaster.getWidth(),
                  destRaster.getHeight(),
                  0,
                  0,
                  new int[] {c});
          Raster srcChannel =
              clippedRow.createChild(
                  clippedRow.getMinX(), 0, clippedRow.getWidth(), 1, 0, 0, new int[] {c});

          switch (header.getBitsPerPixel()) {
            case 1:
            case 2:
            case 4:
            case 8:
              byte[] rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData(c);
              readRowByte(
                  input,
                  srcRegion,
                  xSub,
                  ySub,
                  rowDataByte,
                  0,
                  rowDataByte.length,
                  destChannel,
                  srcChannel,
                  y);
              break;
            default:
              throw new AssertionError();
          }

          if (abortRequested()) {
            break;
          }
        }

        processImageProgress(100f * y / height);

        if (y >= srcRegion.y + srcRegion.height) {
          break;
        }

        if (abortRequested()) {
          processReadAborted();
          break;
        }
      }
    }

    processImageComplete();

    return destination;
  }