Exemplo n.º 1
0
  // vartika says on 3 august
  // public void paintRegion(Graphics2D g, int dx, int dy, long sx, long sy,
  public ReturnObject paintRegion(long sx, long sy, int w, int h, double downsample)
      throws IOException {
    if (downsample < 1.0) {
      throw new IllegalArgumentException("downsample (" + downsample + ") must be >= 1.0");
    }

    // get the level
    int level = getBestLevelForDownsample(downsample);

    // figure out its downsample
    double levelDS = levelDownsamples[level];

    // compute the difference
    double relativeDS = downsample / levelDS;

    // scale source coordinates into level coordinates
    long baseX = (long) (downsample * sx);
    long baseY = (long) (downsample * sy);
    long levelX = (long) (relativeDS * sx);
    long levelY = (long) (relativeDS * sy);

    // scale width and height by relative downsample
    int levelW = (int) Math.round(relativeDS * w);
    int levelH = (int) Math.round(relativeDS * h);

    // clip to edge of image
    levelW = (int) Math.min(levelW, getLevelWidth(level) - levelX);
    levelH = (int) Math.min(levelH, getLevelHeight(level) - levelY);
    w = (int) Math.round(levelW / relativeDS);
    h = (int) Math.round(levelH / relativeDS);

    if (debug) {
      System.out.println(
          "levelW " + levelW + ", levelH " + levelH + ", baseX " + baseX + ", baseY " + baseY);
    }

    if (levelW <= 0 || levelH <= 0) {
      // nothing to draw
      return null;
    }

    BufferedImage img = new BufferedImage(levelW, levelH, BufferedImage.TYPE_INT_ARGB_PRE);

    int data[] = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();

    paintRegionARGB(data, baseX, baseY, level, img.getWidth(), img.getHeight());

    // vartika says on 5 august
    System.out.println("data in paintRegion: " + data);
    System.out.println("img in paintRegion: " + img);

    // from here return the buffered image to client for drawing
    // below code should be at client
    // vartika says on 2 august
    // g.scale(1.0 / relativeDS, 1.0 / relativeDS);

    // vartika on 6 august
    // trying to convert int data[] to byte [] or String...
    ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(data);
    byte[] byteArray = byteBuffer.array();

    // vartika on 10 august
    // saving buffered image to disk as png
    // try {
    // vartika on 13 august to for demo
    // File outputFile = new File("/home/vartika/humintec/image_files/BufferedImageToPng.png");
    //  ImageIO.write(img, "png", outputFile);

    // } catch(IOException e) {
    //   e.printStackTrace();
    // }

    // instead of using int[]data to create bytearray, use the buffered image to create byte []
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(img, "png", baos);
    baos.flush();
    byte[] byteArrayFromBufferedImage = baos.toByteArray();
    baos.close();
    ReturnObject tmpobj = createReturnObject(byteArrayFromBufferedImage, img, w, h);
    return tmpobj;

    // vartika to uncomment later - 10 august
    // vartika says on 3 august
    /*ReturnObject obj = createReturnObject(byteArray, img, w, h);
    return obj;*/

    /* g.drawImage(img, dx, dy, w, h, null);

    if (debug) {
        System.out.println(img);

        if (debugThingy == 0) {
            g.setColor(new Color(1.0f, 0.0f, 0.0f, 0.4f));
            debugThingy = 1;
        } else {
            g.setColor(new Color(0.0f, 1.0f, 0.0f, 0.4f));
            debugThingy = 0;
        }
        g.fillRect(dx, dy, w, h);
    }*/
  }