public void testCreateIndexed() {
    byte[] redLUT = new byte[] {1, 10};
    byte[] greenLUT = new byte[] {2, 20};
    byte[] blueLUT = new byte[] {3, 30};
    byte[] alphaLUT = new byte[] {4, 40};

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createIndexed(
            redLUT, greenLUT, blueLUT, alphaLUT, 1, DataBuffer.TYPE_BYTE);
    ColorModel model = type.getColorModel();

    assertEquals(
        "Failed to return the colorspace", ColorSpace.TYPE_RGB, model.getColorSpace().getType());
    assertEquals(
        "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency());
    assertEquals(
        "Failed to return the tranfer type", DataBuffer.TYPE_BYTE, model.getTransferType());
    assertEquals("Failed to return the red color component", 1, model.getRed(0));
    assertEquals("Failed to return the red color component", 10, model.getRed(1));
    assertEquals("Failed to return the green color component", 2, model.getGreen(0));
    assertEquals("Failed to return the green color component", 20, model.getGreen(1));
    assertEquals("Failed to return the blue color component", 3, model.getBlue(0));
    assertEquals("Failed to return the blue color component", 30, model.getBlue(1));
    assertEquals("Failed to return the alpha color component", 4, model.getAlpha(0));
    assertEquals("Failed to return the alpha color component", 40, model.getAlpha(1));
  }
  /**
   * Gets a specific pixel color from the specified buffered image
   *
   * @param image
   * @param i
   * @param j
   * @param color
   * @return
   */
  protected Color getPixelColor(BufferedImage image, int i, int j) {
    ColorModel cm = image.getColorModel();
    Raster raster = image.getRaster();
    Object pixel = raster.getDataElements(i, j, null);

    Color actual;
    if (cm.hasAlpha()) {
      actual =
          new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), cm.getAlpha(pixel));
    } else {
      actual = new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), 255);
    }
    return actual;
  }
Ejemplo n.º 3
0
  public static void main(String args[]) {
    ColorModel cm =
        new ColorModel(32) {
          public int getAlpha(int pixel) {
            return 255;
          }

          public int getBlue(int pixel) {
            return 255;
          }

          public int getGreen(int pixel) {
            return 255;
          }

          public int getRed(int pixel) {
            return 255;
          }
        };

    cm.hasAlpha();
    cm.isAlphaPremultiplied();
    cm.getTransferType();
    cm.getPixelSize();
    cm.getComponentSize();
    cm.getComponentSize();
    cm.getTransparency();
    cm.getNumComponents();
    cm.getNumColorComponents();
    cm.getRed(20);
    cm.getGreen(20);
    cm.getBlue(20);
    cm.getAlpha(20);
    cm.getRGB(20);
    cm.isAlphaPremultiplied();
    cm.isAlphaPremultiplied();

    cm = ColorModel.getRGBdefault();
  }
Ejemplo n.º 4
0
  public BufferedImage changeGrey() {
    PixelGrabber pg =
        new PixelGrabber(this.image.getSource(), 0, 0, this.iw, this.ih, this.pixels, 0, this.iw);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    int grey = 123;

    ColorModel cm = ColorModel.getRGBdefault();
    for (int i = 0; i < this.iw * this.ih; i++) {
      int alpha = cm.getAlpha(this.pixels[i]);
      int red;

      if (cm.getRed(this.pixels[i]) > grey) red = 255;
      else red = 0;
      int green;

      if (cm.getGreen(this.pixels[i]) > grey) green = 255;
      else green = 0;
      int blue;

      if (cm.getBlue(this.pixels[i]) > grey) blue = 255;
      else {
        blue = 0;
      }
      this.pixels[i] = (alpha << 24 | red << 16 | green << 8 | blue);
    }

    Image tempImg =
        Toolkit.getDefaultToolkit()
            .createImage(new MemoryImageSource(this.iw, this.ih, this.pixels, 0, this.iw));
    this.image = new BufferedImage(tempImg.getWidth(null), tempImg.getHeight(null), 4);
    this.image.createGraphics().drawImage(tempImg, 0, 0, null);
    return this.image;
  }
Ejemplo n.º 5
0
  public BufferedImage getMedian() {
    PixelGrabber pg =
        new PixelGrabber(this.image.getSource(), 0, 0, this.iw, this.ih, this.pixels, 0, this.iw);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    ColorModel cm = ColorModel.getRGBdefault();
    for (int i = 1; i < this.ih - 1; i++) {
      for (int j = 1; j < this.iw - 1; j++) {
        int alpha = cm.getAlpha(this.pixels[(i * this.iw + j)]);

        int red4 = cm.getRed(this.pixels[(i * this.iw + j - 1)]);
        int red5 = cm.getRed(this.pixels[(i * this.iw + j)]);
        int red6 = cm.getRed(this.pixels[(i * this.iw + j + 1)]);
        int red;

        if (red4 >= red5) {

          if (red5 >= red6) {
            red = red5;
          } else {

            if (red4 >= red6) red = red6;
            else red = red4;
          }
        } else {

          if (red4 > red6) {
            red = red4;
          } else {

            if (red5 > red6) red = red6;
            else {
              red = red5;
            }
          }
        }

        int green4 = cm.getGreen(this.pixels[(i * this.iw + j - 1)]);
        int green5 = cm.getGreen(this.pixels[(i * this.iw + j)]);
        int green6 = cm.getGreen(this.pixels[(i * this.iw + j + 1)]);
        int green;

        if (green4 >= green5) {

          if (green5 >= green6) {
            green = green5;
          } else {

            if (green4 >= green6) green = green6;
            else green = green4;
          }
        } else {

          if (green4 > green6) {
            green = green4;
          } else {

            if (green5 > green6) green = green6;
            else {
              green = green5;
            }
          }
        }

        int blue4 = cm.getBlue(this.pixels[(i * this.iw + j - 1)]);
        int blue5 = cm.getBlue(this.pixels[(i * this.iw + j)]);
        int blue6 = cm.getBlue(this.pixels[(i * this.iw + j + 1)]);
        int blue;

        if (blue4 >= blue5) {

          if (blue5 >= blue6) {
            blue = blue5;
          } else {

            if (blue4 >= blue6) blue = blue6;
            else blue = blue4;
          }
        } else {

          if (blue4 > blue6) {
            blue = blue4;
          } else {

            if (blue5 > blue6) blue = blue6;
            else {
              blue = blue5;
            }
          }
        }
        this.pixels[(i * this.iw + j)] = (alpha << 24 | red << 16 | green << 8 | blue);
      }
    }

    Image tempImg =
        Toolkit.getDefaultToolkit()
            .createImage(new MemoryImageSource(this.iw, this.ih, this.pixels, 0, this.iw));
    this.image = new BufferedImage(tempImg.getWidth(null), tempImg.getHeight(null), 4);
    this.image.createGraphics().drawImage(tempImg, 0, 0, null);
    return this.image;
  }
Ejemplo n.º 6
0
  // fast rendering algorithm
  // basically applies duplicates the picture and applies a size*size kernel
  // in only one pass.
  // the kernel is simulated by an horizontal and a vertical pass
  // implemented by Sebastien Petrucci
  private BufferedImage createShadowFast(final BufferedImage src) {
    int shadowSize = this.size;

    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();

    int dstWidth = srcWidth + size;
    int dstHeight = srcHeight + size;

    int left = (shadowSize - 1) >> 1;
    int right = shadowSize - left;

    int yStop = dstHeight - right;

    BufferedImage dst = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB);

    int shadowRgb = color.getRGB() & 0x00FFFFFF;

    int[] aHistory = new int[shadowSize];
    int historyIdx;

    int aSum;

    ColorModel srcColorModel = src.getColorModel();
    WritableRaster srcRaster = src.getRaster();
    int[] dstBuffer = ((DataBufferInt) dst.getRaster().getDataBuffer()).getData();

    int lastPixelOffset = right * dstWidth;
    float hSumDivider = 1.0f / size;
    float vSumDivider = opacity / size;

    // horizontal pass : extract the alpha mask from the source picture and
    // blur it into the destination picture
    for (int srcY = 0, dstOffset = left * dstWidth; srcY < srcHeight; srcY++) {

      // first pixels are empty
      for (historyIdx = 0; historyIdx < shadowSize; ) {
        aHistory[historyIdx++] = 0;
      }

      aSum = 0;
      historyIdx = 0;

      // compute the blur average with pixels from the source image
      for (int srcX = 0; srcX < srcWidth; srcX++) {

        int a = (int) (aSum * hSumDivider); // calculate alpha value
        dstBuffer[dstOffset++] = a << 24; // store the alpha value only
        // the shadow color will be added in the next pass

        aSum -= aHistory[historyIdx]; // substract the oldest pixel from the sum

        // extract the new pixel ...
        a = srcColorModel.getAlpha(srcRaster.getDataElements(srcX, srcY, null));
        aHistory[historyIdx] = a; // ... and store its value into history
        aSum += a; // ... and add its value to the sum

        if (++historyIdx >= shadowSize) {
          historyIdx -= shadowSize;
        }
      }

      // blur the end of the row - no new pixels to grab
      for (int i = 0; i < shadowSize; i++) {

        int a = (int) (aSum * hSumDivider);
        dstBuffer[dstOffset++] = a << 24;

        // substract the oldest pixel from the sum ... and nothing new to add !
        aSum -= aHistory[historyIdx];

        if (++historyIdx >= shadowSize) {
          historyIdx -= shadowSize;
        }
      }
    }

    // vertical pass
    for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {

      aSum = 0;

      // first pixels are empty
      for (historyIdx = 0; historyIdx < left; ) {
        aHistory[historyIdx++] = 0;
      }

      // and then they come from the dstBuffer
      for (int y = 0; y < right; y++, bufferOffset += dstWidth) {
        int a = dstBuffer[bufferOffset] >>> 24; // extract alpha
        aHistory[historyIdx++] = a; // store into history
        aSum += a; // and add to sum
      }

      bufferOffset = x;
      historyIdx = 0;

      // compute the blur average with pixels from the previous pass
      for (int y = 0; y < yStop; y++, bufferOffset += dstWidth) {

        int a = (int) (aSum * vSumDivider); // calculate alpha value
        dstBuffer[bufferOffset] = a << 24 | shadowRgb; // store alpha value + shadow color

        aSum -= aHistory[historyIdx]; // substract the oldest pixel from the sum

        a = dstBuffer[bufferOffset + lastPixelOffset] >>> 24; // extract the new pixel ...
        aHistory[historyIdx] = a; // ... and store its value into history
        aSum += a; // ... and add its value to the sum

        if (++historyIdx >= shadowSize) {
          historyIdx -= shadowSize;
        }
      }

      // blur the end of the column - no pixels to grab anymore
      for (int y = yStop; y < dstHeight; y++, bufferOffset += dstWidth) {

        int a = (int) (aSum * vSumDivider);
        dstBuffer[bufferOffset] = a << 24 | shadowRgb;

        aSum -= aHistory[historyIdx]; // substract the oldest pixel from the sum

        if (++historyIdx >= shadowSize) {
          historyIdx -= shadowSize;
        }
      }
    }

    return dst;
  }