예제 #1
0
  private void newImage(Buffer buffer) {
    Object data = buffer.getData();
    if (!(data instanceof int[])) return;
    RGBFormat format = (RGBFormat) buffer.getFormat();

    DirectColorModel dcm =
        new DirectColorModel(
            format.getBitsPerPixel(),
            format.getRedMask(),
            format.getGreenMask(),
            format.getBlueMask());

    sourceImage =
        new MemoryImageSource(
            format.getLineStride(),
            format.getSize().height,
            dcm,
            (int[]) data,
            0,
            format.getLineStride());
    sourceImage.setAnimated(true);
    sourceImage.setFullBufferUpdates(true);
    if (component != null) {
      destImage = component.createImage(sourceImage);
      component.prepareImage(destImage, component);
    }
  }
예제 #2
0
파일: Haikdu.java 프로젝트: cwvh/haikdu
  public void putRegion(
      int viewWidth,
      int viewHeight,
      int regWidth,
      int regHeight,
      int regOffX,
      int regOffY,
      int[] regBuffer) {
    if (image == null) {
      setPreferredSize(new Dimension(viewWidth, viewHeight));
      imageBuffer = new int[viewWidth * viewHeight];
      imageSource = new MemoryImageSource(viewWidth, viewHeight, imageBuffer, 0, viewWidth);
      imageSource.setAnimated(true);
      image = createImage(imageSource);
    }

    int destIndex = regOffX + regOffY * viewWidth;
    int srcIndex = 0;
    int extraRowGap = viewWidth - regWidth;
    for (int j = 0; j < regHeight; j++, destIndex += extraRowGap)
      for (int i = 0; i < regWidth; i++, srcIndex++, destIndex++)
        imageBuffer[destIndex] = regBuffer[srcIndex];
    imageSource.newPixels(regOffX, regOffY, regWidth, regHeight);
    repaint(regOffX, regOffY, regWidth, regHeight);
  }
예제 #3
0
 void createImage() {
   if (imageSource == null) {
     rgbCM = new DirectColorModel(32, 0xff0000, 0xff00, 0xff);
     imageSource = new MemoryImageSource(width, height, rgbCM, rgbPixels, 0, width);
     imageSource.setAnimated(true);
     imageSource.setFullBufferUpdates(true);
     awtImage = Toolkit.getDefaultToolkit().createImage(imageSource);
     newPixels = false;
   } else if (newPixels) {
     imageSource.newPixels(rgbPixels, rgbCM, 0, width);
     newPixels = false;
   } else imageSource.newPixels();
 }
예제 #4
0
  public myImageFade() {
    myImage1 = Toolkit.getDefaultToolkit().getImage("d:/1.jpg");
    myImage2 = Toolkit.getDefaultToolkit().getImage("d:/ball.gif");

    myImage1_scaled = myImage1.getScaledInstance(500, 500, Image.SCALE_FAST);
    myImage2_scaled = myImage2.getScaledInstance(500, 500, Image.SCALE_FAST);
    repaint();

    try {
      MediaTracker mt = new MediaTracker(this);
      mt.addImage(myImage1, 0);
      mt.addImage(myImage2, 0);
      mt.waitForAll();

      PixelGrabber grab1 =
          new PixelGrabber(myImage1, 0, 0, picWidth, picHeight, m_Img1Pix, 0, picWidth);
      PixelGrabber grab2 =
          new PixelGrabber(myImage2, 0, 0, picWidth, picHeight, m_Img2Pix, 0, picWidth);

      grab1.grabPixels();
      grab2.grabPixels();

      System.out.println(m_Img1Pix.length);

      System.out.println(m_Img1Pix[18500]);

      m_ImgSrc = new MemoryImageSource(picWidth, picHeight, m_Pix, 0, picWidth);
      m_ImgSrc.setAnimated(true);
      m_Img = createImage(m_ImgSrc);
    } catch (InterruptedException e) {
    }
  }
  public void notifyMoreDataReady(float[] bins) { // an array of floats

    if (recursion) System.err.println(" RECURSION ");
    nBins = bins.length;
    if (nBins == 0) return;

    recursion = true;

    if (size == null || nBins != size.height || nChunks != size.width) // || this is the or operator
    createGraphics(); //  Returns a Graphics2D object for rendering into the specified
                      // BufferedImage.

    int width = size.width;

    for (int i = 0;
        i < nBins;
        i++) { // this is recursing over all the rows (nbins is the number of rows)
      int bin = nBins - i - 1; // I think this makes it start from the top
      float val = mapper.eval(bins[bin]); //

      if (val < 0) val = 0.0f;
      if (val > 1.0) // I think this next section colours the pixels in the spectrum
      val = 1.0f; // display
      int c_r = (int) (255 * val);
      int c_g = c_r;
      int c_b = 255 - c_r;

      int color =
          (c_b)
              + (c_g << 8)
              + (c_r << 16)
              + (0xFF
                  << 24); // << operator means signed left shift-- it shifts the bits in the binary
                          // code to the left creating a bigger number the operand on the right
                          // specifies the amount of shift
      screenBuffer[i * width + ptr] =
          (255 << 24)
              + color; // I think loads the screenBuffer array. the index[i*width+ptr] is filled
                       // with the right operand
      // rgbarray[i] = color;
    }

    if (ptr % 1 == 0) { // ptr remainder(%) 1
      screenConverter.newPixels(
          ptr, 0, 1,
          nBins); // newPixels is method inside the MemoryImageSource i think it does this----Sends
                  // a rectangular region of the buffer of pixels to any ImageConsumers that are
                  // currently interested in the data for this image and notify them that an
                  // animation frame is complete.
      screenRepaint = true;
      repaint();
    }

    ptr =
        (ptr + 1)
            % size.width; // i think this may help in changing the co-ordinates ptr may be the x
                          // coordinate
    recursion = false;
  }
예제 #6
0
  public void shuffle(int p) {
    for (int i = 0; i < picWidth * picHeight; ++i) {
      m_Pix[i] = compPix(m_Img1Pix[i], m_Img2Pix[i], p);
    }

    m_ImgSrc.newPixels();
    revalidate();
  }
  void createGraphics() {

    synchronized (
        imagSync) { // The synchronized modifier allows the user to lock objects that are shared
      // If you have more than one of your threads running at the same time that
      // access shared objects, it may cause your object to be in an inconsitent state
      // Making a method synchronized will lock the object. This means that no other thread can call
      // a synchronized method on that object.

      size =
          new Dimension(
              nChunks,
              nBins); // The dimension class 'encapsulates the width and height of a component in a
                      // single object
      // In this case i think the object imageSync
      int width = nChunks;
      int height = nBins; // nBins is the height maybe the no.of rows which get coloured in

      screenBuffer =
          new int[width * height]; // i think this creates an integer array called screenBuffer

      screenConverter =
          new MemoryImageSource(
              width,
              height, // The MemoryImageSource class--- This class is an implementation of the
                      // ImageProducer interface which uses an array to produce pixel values for an
                      // Image.
              screenBuffer,
              0,
              width); // The MemoryImageSource is also capable of managing a memory image which
                      // varies over time to allow animation or custom rendering.
      screenConverter.setAnimated(
          true); // Changes this memory image into a multi-frame animation or a single-frame static
                 // image depending on the animated parameter.This is a multframe animation
      screenConverter.setFullBufferUpdates(
          false); // Specifies whether this animated memory image should always be updated by
                  // sending the complete buffer of pixels whenever there is a change.
      offscreen =
          Toolkit.getDefaultToolkit()
              .createImage(
                  screenConverter); // Returns an image which gets pixel data from the specified
                                    // file. there other possible create image
      setPreferredSize(size);
    }
  }
예제 #8
0
 public Image getSection(int x, int y, int width, int height) {
   if (width > imgWidth || height > imgHeight) {
     // create new image source and pixel array
     size = width * height;
     pixels = new int[size];
     imageSource = new MemoryImageSource(width, height, pixels, 0, width);
     imageSource.setAnimated(true);
     // imageSource.setFullBufferUpdates(true);
     image = Toolkit.getDefaultToolkit().createImage(imageSource);
   }
   if (width > imgWidth || height > imgHeight || x != oldX || y != oldY) {
     // recreate pixel array
     int skipWidth = 0;
     if (width < imgWidth && height <= imgHeight) {
       skipWidth = imgWidth - width;
     }
     int index = 0;
     for (int aY = 0; aY < height; aY++) {
       for (int aX = 0; aX < width; aX++) {
         if (index >= size) {
           System.out.println("oops...");
         }
         // pixels[index] = encodeColor(stats.getValueAt(x + aX, y +
         // aY));
         pixels[index] = mapColor(stats.getValueAt(x + aX, y + aY));
         index++;
       }
       index += skipWidth; // skip pixels to the right of clip area
     }
     oldX = x;
     oldY = y;
     imageSource.newPixels(0, 0, width, height);
     // imageSource.newPixels();
   }
   if (width > imgWidth || height > imgHeight) {
     imgWidth = width;
     imgHeight = height;
   }
   return image;
 }
예제 #9
0
  /** Processes the data and renders it to a component */
  public synchronized int process(Buffer buffer) {
    if (component == null) return BUFFER_PROCESSED_FAILED;

    Format inf = buffer.getFormat();
    if (inf == null) return BUFFER_PROCESSED_FAILED;

    if (inf != inputFormat || !buffer.getFormat().equals(inputFormat)) {
      if (setInputFormat(inf) != null) return BUFFER_PROCESSED_FAILED;
    }

    Object data = buffer.getData();
    if (!(data instanceof int[])) return BUFFER_PROCESSED_FAILED;

    if (lastBuffer != buffer) {
      lastBuffer = buffer;
      newImage(buffer);
    }

    sourceImage.newPixels(0, 0, inWidth, inHeight);

    Graphics g = component.getGraphics();
    if (g != null) {
      if (reqBounds == null) {
        bounds = component.getBounds();
        bounds.x = 0;
        bounds.y = 0;
      } else bounds = reqBounds;
      g.drawImage(
          destImage,
          bounds.x,
          bounds.y,
          bounds.width,
          bounds.height,
          0,
          0,
          inWidth,
          inHeight,
          component);
    }

    return BUFFER_PROCESSED_OK;
  }