Esempio n. 1
0
 private static ImageIcon makeGrayImageIcon1(Image img) {
   BufferedImage source =
       new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
   Graphics g = source.createGraphics();
   g.drawImage(img, 0, 0, null);
   g.dispose();
   ColorConvertOp colorConvert =
       new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
   BufferedImage destination = colorConvert.filter(source, null);
   return new ImageIcon(destination);
 }
  /** Decodes an image */
  public PlanarImage decodeJAI() throws IOException {

    // ------------------------------------------- construimos la imagen
    int tileWidth = width, tileHeight = height;

    int[] order = new int[bands];
    for (int i = 0; i < bands; i++) order[i] = i;
    ComponentSampleModel csm =
        new ComponentSampleModel(
            DataBuffer.TYPE_SHORT, tileWidth, tileHeight, tileWidth * bands, bands, order);

    for (int i = 0; i < bands; i++) order[i] = 16;
    ColorSpace cs;
    switch (bands) {
      case 1:
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        break;
      case 3:
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        break;
      default:
        // F = 15 component
        cs = ColorSpace.getInstance(ColorSpace.TYPE_FCLR);
    }
    ComponentColorModel ccm =
        new ComponentColorModel(
            cs, order, false, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);

    outImage = new TiledImage(0, 0, width, height, 0, 0, csm, ccm);

    // --------------------------------------------------- decodificamos

    decode(); // ya redefinimos aqui el setValue, asi que todo el proceso es igual

    return outImage;
  }
Esempio n. 3
0
  public BufferedImage getBufferedImage(int type, Color c) {

    BufferedImage image = null;
    float[] colComp = new float[3];
    c.getRGBColorComponents(colComp);
    double red = (double) colComp[0];
    double green = (double) colComp[1];
    double blue = (double) colComp[2];
    // System.out.println("blue, green, red = "+ blue +", " + green + ", " + red);

    double x = 0.0;
    double x2;
    switch (type) {
      case ScalarImage.TYPE_BYTE_RANDOM:
        {
          int numCol = 256;
          byte[] bBuf = new byte[numCol * 3];
          blue *= 255 * 4.;
          green *= 255 * 4.;
          red *= 255 * 4.;
          double delta = 1.0 / (double) (numCol + 1);
          int j = 0;
          for (int i = 0; i < numCol; i++) {
            if (i % 5 == 0) x = 0.7 * Math.random() + 0.3 * x;
            x2 = x * x;
            bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
            bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
            bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
            // x += delta;
          }
          IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false);
          // image = new
          // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm);
          byte[] idxBuffer = new byte[size];
          for (int i = 0; i < size; i++) {
            idxBuffer[i] = (byte) (clamp(f[i] * 255.));
          }
          DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size);
          int idxOffset[] = {0};
          int idxBits[] = {8};
          try {
            ComponentSampleModel idxSampleModel =
                new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset);
            WritableRaster rasterIdx =
                java.awt.image.Raster.createWritableRaster(
                    idxSampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(cm, rasterIdx, false, null);
          } catch (Exception e) {
            System.out.println("Exception caught while acquiring image:");
            System.out.println(e.getMessage());
          }
        }
        break;
      case BufferedImage.TYPE_BYTE_INDEXED:
        {
          int numCol = 256;
          byte[] bBuf = new byte[numCol * 3];
          blue *= 255 * 4.;
          green *= 255 * 4.;
          red *= 255 * 4.;
          double delta = 1.0 / (double) (numCol + 1);
          int j = 0;
          for (int i = 0; i < numCol; i++) {
            x2 = x * x;
            bBuf[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
            bBuf[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
            bBuf[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
            x += delta;
          }
          IndexColorModel cm = new IndexColorModel(8, numCol, bBuf, 0, false);
          // image = new
          // BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,cm);
          byte[] idxBuffer = new byte[size];
          for (int i = 0; i < size; i++) {
            idxBuffer[i] = (byte) (clamp(f[i] * 255.));
          }
          DataBufferByte dataBuffer = new DataBufferByte(idxBuffer, size);
          int idxOffset[] = {0};
          int idxBits[] = {8};
          try {
            ComponentSampleModel idxSampleModel =
                new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, idxOffset);
            WritableRaster rasterIdx =
                java.awt.image.Raster.createWritableRaster(
                    idxSampleModel, dataBuffer, new Point(0, 0));
            image = new BufferedImage(cm, rasterIdx, false, null);
          } catch (Exception e) {
            System.out.println("Exception caught while acquiring image:");
            System.out.println(e.getMessage());
          }
        }
        break;
      case BufferedImage.TYPE_BYTE_GRAY:
        break;
      case BufferedImage.TYPE_3BYTE_BGR:
      default:
        byte[] byteBuffer = new byte[size * 3];
        blue *= 255 * 4.;
        green *= 255 * 4.;
        red *= 255 * 4.;

        int j = 0;
        for (int i = 0; i < size; i++) {
          x = f[i];
          x2 = x * x;
          /*
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * red ) * x * x + ( 4 * red - 255 ) * x);
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * green ) * x * x + ( 4 * green - 255 ) * x);
          byteBuffer[j++] = (byte)clamp( ( 2 * 255 - 4 * blue ) * x * x + ( 4 * blue - 255 ) * x);
          */
          byteBuffer[j++] = (byte) clamp((510 - red) * x2 + (red - 255) * x);
          byteBuffer[j++] = (byte) clamp((510 - green) * x2 + (green - 255) * x);
          byteBuffer[j++] = (byte) clamp((510 - blue) * x2 + (blue - 255) * x);
        }
        DataBufferByte dataBuffer = new DataBufferByte(byteBuffer, size * 3);
        int componentOffset[] = {0, 1, 2};
        int componentBits[] = {8, 8, 8};
        try {
          WritableRaster raster =
              java.awt.image.Raster.createWritableRaster(
                  new PixelInterleavedSampleModel(
                      DataBuffer.TYPE_BYTE, width, height, 3, width * 3, componentOffset),
                  dataBuffer,
                  new Point(0, 0));
          image =
              new BufferedImage(
                  new ComponentColorModel(
                      ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB),
                      componentBits,
                      false,
                      false,
                      ColorModel.OPAQUE,
                      DataBuffer.TYPE_BYTE),
                  raster,
                  false,
                  null);

        } catch (Exception e) {
          System.out.println("Exception caught while acquiring image:");
          System.out.println(e.getMessage());
        }
        break;
    }
    return image;
  }