/**
  * Method which updates the value of the bands
  *
  * @param img New image
  * @param n Number of bands
  */
 public void updateBandsFrame(JIPImage img, int n) {
   bandSel.removeAllItems();
   if (img instanceof JIPBmpColor) {
     bandSel.addItem("COLOR-Bands RGB");
   } else if (img instanceof JIPImgGeometric) {
     bandSel.addItem("Geometric image");
   } else {
     for (int i = 0; i < ((JIPImgBitmap) img).getNumBands(); i++)
       bandSel.addItem(prop.getProperty("Band") + " " + i);
     tamFrame.setText(
         prop.getProperty("FrameSize") + ": " + img.getWidth() + "x" + img.getHeight());
   }
 }
 /**
  * Method which updates the information of the panel, it can be the width, the height, number of
  * segments, image, bands, etc...
  *
  * @param w Width
  * @param h Height
  * @param x actual X
  * @param y actual Y
  * @param numSeg Number of segments
  * @param numPoint Number of points
  * @param numPoly Number of polygons
  * @param xini Initial X
  * @param yini Initial Y
  * @param xfin Final X
  * @param yfin Final Y
  * @param img Image
  * @param b Number of bands
  */
 public void updateInfo(
     int w,
     int h,
     int x,
     int y,
     int numSeg,
     int numPoint,
     int numPoly,
     int xini,
     int yini,
     int xfin,
     int yfin,
     JIPImage img,
     int b) {
   try {
     if (x < w && x >= 0 && y < h && y >= 0) {
       pos.setText(prop.getProperty("Coords") + ": (" + x + "," + y + ")");
       if (img == null) value.setText(prop.getProperty("Value") + ": <-1>");
       else if (img.getType() == ImageType.FLOAT) {
         value.setText(
             prop.getProperty("Value")
                 + ": <"
                 + Float.toString(((JIPBmpFloat) img).getPixelFloat(b, x, y))
                 + ">");
       } else if (img.getType() == ImageType.COLOR) {
         value.setText(
             prop.getProperty("Value")
                 + ": <"
                 + (int) ((JIPBmpColor) img).getPixelRed(x, y)
                 + ","
                 + (int) ((JIPBmpColor) img).getPixelGreen(x, y)
                 + ","
                 + (int) ((JIPBmpColor) img).getPixelBlue(x, y)
                 + ">");
       } else if (img instanceof JIPImgBitmap) {
         value.setText(
             prop.getProperty("Value")
                 + ": <"
                 + (int) ((JIPImgBitmap) img).getPixel(b, x, y)
                 + ">");
       } else { // Geometric type
         value.setText(prop.getProperty("Value") + ":<Geom>");
       }
     } else {
       pos.setText(prop.getProperty("Coords") + ": ( OUT )     ");
       value.setText(prop.getProperty("Value") + ": ( OUT )");
     }
   } catch (JIPException e) {
     logger.error(e);
   }
 }
  public JIPImage processImg(JIPImage img) throws JIPException {
    if (img.getType() == ImageType.COLOR || img instanceof JIPImgGeometric)
      throw new JIPException("GrayToGray can not be applied to this image format");
    String p1 = getParamValueString("gray");
    ImageType tipo = Enum.valueOf(ImageType.class, p1);
    JIPImgBitmap imgBmp = (JIPImgBitmap) img;
    int nbands = imgBmp.getNumBands();
    JIPImgBitmap res =
        (JIPImgBitmap) JIPImage.newImage(nbands, img.getWidth(), img.getHeight(), tipo);
    double[] bmp, bmpRes;
    double maxInput = 0, maxOutput = 0;

    switch (img.getType()) {
      case BYTE:
        maxInput = 255;
        break;
      case FLOAT:
      case BIT:
        maxInput = 1;
        break;
      case SHORT:
        maxInput = 65535;
        break;
    }
    switch (tipo) {
      case BYTE:
        maxOutput = 255;
        break;
      case FLOAT:
      case BIT:
        maxOutput = 1;
        break;
      case SHORT:
        maxOutput = 65535;
        break;
    }
    for (int b = 0; b < nbands; b++) {
      bmp = imgBmp.getAllPixels(b);
      bmpRes = new double[bmp.length];
      for (int i = 0; i < bmp.length; i++) bmpRes[i] = maxOutput * bmp[i] / maxInput;
      res.setAllPixels(b, bmpRes);
    }

    return res;
  }
  public JIPImage processImg(JIPImage img) throws JIPException {
    JIPImage res = null;
    ImageType t = img.getType();
    if (t == ImageType.EDGES
        || t == ImageType.POINT
        || t == ImageType.SEGMENT
        || t == ImageType.POLY
        || t == ImageType.BIT)
      throw new JIPException("Function ConvolveAscii can not be applied to this image type.");

    String convoMat = getParamValueString("matrix");
    float mult = getParamValueFloat("mult");
    float div = getParamValueFloat("div");
    String method = getParamValueString("method");

    // Check the file extension (only .txt)
    String[] fileExtension = convoMat.split("\\.");
    if (!fileExtension[fileExtension.length - 1].equals("txt"))
      throw new JIPException("File must be a .txt file.");

    try {
      FileInputStream convoF = new FileInputStream(convoMat);
      Reader r = new BufferedReader(new InputStreamReader(convoF));
      StreamTokenizer st = new StreamTokenizer(r);

      st.nextToken();
      int cw = (int) st.nval;
      st.nextToken();
      int ch = (int) st.nval;

      double[] mat = new double[cw * ch];
      for (int count = 0; count < cw * ch; count++) {
        st.nextToken();
        if (st.ttype == StreamTokenizer.TT_EOF)
          throw new JIPException("Error reading ASCII file. Incorrect data.");
        mat[count] = (float) st.nval;
      }

      // Incorrect data: more rows or columns than you has declared.
      st.nextToken();
      if (st.ttype != StreamTokenizer.TT_EOF)
        throw new JIPException("Error reading ASCII file. Incorrect data.");

      Function2D convolution = new ConvolveImage();
      JIPBmpByte convo = new JIPBmpByte(cw, ch);
      convo.setAllPixels(mat);
      convolution.setParamValue("image", convo);
      convolution.setParamValue("div", div);
      convolution.setParamValue("mult", mult);
      convolution.setParamValue("method", method);

      res = convolution.processImg(img);
      if (convolution.isInfo()) info = "ConvolveAscii info: " + convolution.getInfo();
    } catch (FileNotFoundException e) {
      throw new JIPException("File Not Found.");
    } catch (IOException e) {
      throw new JIPException("IO Exception.");
    }

    return res;
  }