Java Code Examples for processing.core.PImage#loadPixels()

The following examples show how to use processing.core.PImage#loadPixels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstellationPointMarker.java    From constellation with Apache License 2.0 6 votes vote down vote up
private void createImages() {
    try {
        final File templateImageFile = ConstellationInstalledFileLocator.locate(
                "modules/ext/data/" + TEMPLATE_IMAGE_PATH,
                "au.gov.asd.tac.constellation.views.mapview",
                false, ConstellationPointMarker.class.getProtectionDomain());
        final BufferedImage templateImage = ImageIO.read(templateImageFile);
        TEMPLATE_IMAGE = new PImage(templateImage.getWidth(), templateImage.getHeight(), PConstants.ARGB);
        TEMPLATE_IMAGE.loadPixels();
        templateImage.getRGB(0, 0, TEMPLATE_IMAGE.width, TEMPLATE_IMAGE.height, TEMPLATE_IMAGE.pixels, 0, TEMPLATE_IMAGE.width);
        TEMPLATE_IMAGE.updatePixels();

        POINT_X_OFFSET = TEMPLATE_IMAGE.width / 2;
        POINT_Y_OFFSET = TEMPLATE_IMAGE.height;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 2
Source File: Utility.java    From Project-16x16 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Scales a PImage object by a given amount.
 *
 * @param pBuffer Image to scale.
 * @param scaling Times to scale.
 * @return new PImage object transformed.
 */
public static PImage scale(PImage pBuffer, int scaling) {
	PImage originalImage = pBuffer;
	PImage tempImage = applet.createImage(PApplet.parseInt(originalImage.width * scaling),
			PApplet.parseInt(originalImage.height * scaling), PConstants.ARGB);
	tempImage.loadPixels();
	originalImage.loadPixels();
	for (int i = 0; i < originalImage.pixels.length; i++) {
		tempImage.pixels[i * scaling] = originalImage.pixels[i];
		tempImage.pixels[i * scaling + 1] = originalImage.pixels[i];
		tempImage.pixels[i * scaling + originalImage.width] = originalImage.pixels[i];
		tempImage.pixels[i * scaling + originalImage.width + 1] = originalImage.pixels[i];
	}
	tempImage.updatePixels();
	return pg(tempImage).get();
}
 
Example 3
Source File: DwUtils.java    From PixelFlow with MIT License 6 votes vote down vote up
static public PImage createSprite(PApplet papplet, int size, float exp1, float exp2, float mult){
  PImage pimg = papplet.createImage(size, size, PConstants.ARGB);
  pimg.loadPixels();
  for(int y = 0; y < size; y++){
    for(int x = 0; x < size; x++){
      int pid = y * size + x;
      
      float xn = ((x + 0.5f) / (float)size) * 2f - 1f;
      float yn = ((y + 0.5f) / (float)size) * 2f - 1f;
      float dd = (float) Math.sqrt(xn*xn + yn*yn);
      
      dd = clamp(dd, 0, 1);
      dd = (float) Math.pow(dd, exp1);
      dd = 1.0f - dd;
      dd = (float) Math.pow(dd, exp2);
      dd *= mult;
      dd = clamp(dd, 0, 1);
      pimg.pixels[pid] = ((int)(dd * 255)) << 24 | 0x00FFFFFF;
    }
  }
  pimg.updatePixels();
  return pimg;
}
 
Example 4
Source File: ColorDetection.java    From PapARt with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Get the occurences of a given color, given a error.
 *
 * @param c color to find.
 * @param threshold error margin
 * @return number of occurences.
 */
public int computeOccurencesOfColor(int c, int threshold) {

    // TODO: Hack for noCamera, better to be done. 
    if (paperScreen.cameraTracking == null) {
        return 0;
    }

    PImage out = getImage();
    if (out == null) {
        return 0;
    }

    out.loadPixels();
    int pxNb = picWidth * picHeight;
    int nbSameColor = 0;

    for (int k = 0; k < pxNb; k++) {
        int c2 = out.pixels[k];
        boolean isClose = MathUtils.colorDistRGB(c, c2, threshold);
        if (isClose) {
            nbSameColor++;
        }
    }
    return nbSameColor;
}
 
Example 5
Source File: PShapeSolid.java    From haxademic with MIT License 6 votes vote down vote up
public void updateWithTrigGradient(float time, float ampScale, float spreadMultiplier, PImage texture) {
	// deform from original copy, using vertexIndex as the key to find the shared index
	int vertexIndex = 0;
	texture.loadPixels();
	for (int j = 0; j < shape.getChildCount(); j++) {
		for (int i = 0; i < shape.getChild(j).getVertexCount(); i++) {
			int sharedVertexIndex = sharedVertexIndices.get(vertexIndex);
			PVector vOrig = vertices.get(vertexIndex);
			float vertexIndexPercent = (float)(sharedVertexIndex + 1) / (float)(sharedVertexIndices.size() + 1);
			float oscVal = P.sin((time * P.TWO_PI) + (spreadMultiplier * P.TWO_PI * vertexIndexPercent));
			float amp = 1.0f + ampScale + ampScale * oscVal; 
			shape.getChild(j).setVertex(i, vOrig.x * amp, vOrig.y * amp, vOrig.z * amp);
			if(i < shape.getChild(j).getVertexCount() - 2) { // i % 3 == 0 &&
				int newColor = ImageUtil.getPixelColor(texture, (int)P.map(oscVal, -1f, 1f, 1, texture.width - 2), (int)P.map(oscVal, -1f, 1f, 1, texture.height - 2));
				shape.getChild(j).setFill(newColor);
			}
			vertexIndex++;
		}
	}
}
 
Example 6
Source File: ColorDetection.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Compute the average color of the patch analyzed.
 */
public void computeColor() {

    // HACK -> TODO error management. 
    if (paperScreen.cameraTracking == null) {
        return;
    }

    PImage out = getImage();
    if (out == null) {
        return;
    }
    out.loadPixels();
    avgRed = 0;
    avgGreen = 0;
    avgBlue = 0;
    int pxNb = picWidth * picHeight;
    for (int k = 0; k < pxNb; k++) {
        int c = out.pixels[k];
        avgRed += c >> 16 & 0xFF;
        avgGreen += c >> 8 & 0xFF;
        avgBlue += c >> 0 & 0xFF;
    }

    avgRed = (avgRed / pxNb);
    avgGreen = (avgGreen / pxNb);
    avgBlue = avgBlue / pxNb;
    int r = avgRed << 16;
    int g = avgGreen << 8;
    int b = avgBlue;
    this.col = 255 << 24 | r | g | b;
}
 
Example 7
Source File: ImageUtils.java    From PapARt with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void createAnaglyph(PImage imgL, PImage imgR, PImage imgOut) {
    imgL.loadPixels();
    imgR.loadPixels();
    imgOut.loadPixels();
    int[] pL = imgL.pixels;
    int[] pR = imgR.pixels;
    int[] pO = imgOut.pixels;
    for (int i = 0; i < pL.length; i++) {
        pO[i] = (pR[i] >> 16) << 16 | (pL[i] >> 8) & 255 << 8 | pL[i] & 255;
        //            pO[i] = pL[i];
    }
    imgOut.updatePixels();
    //        imgL.updatePixels();
}
 
Example 8
Source File: Demo_SerialDevice_Arduino_LEDStrip.java    From haxademic with MIT License 5 votes vote down vote up
protected void updateLedLights() {
		// Notes:
		// we can only write 64 bytes per frame: https://arduino.stackexchange.com/questions/14401/arduino-serial-write-sending-more-than-64-bytes
		// though by modifying the Arduino buffer size, we can go up to 256 bytes: http://www.hobbytronics.co.uk/arduino-serial-buffer-size
		// larger sets of data would need to be chunked...
		
		// trigonometry-based color cycling
//		for (float i = 0; i < numLights; i++) {
//			serialDevice.device().write(ConvertUtil.intToByte(30 + P.round(20f * P.sin((p.frameCount + i * 10f) * 0.07f))));
//			serialDevice.device().write(ConvertUtil.intToByte(30 + P.round(20f * P.sin((p.frameCount + i * 10f) * 0.02f))));
//			serialDevice.device().write(ConvertUtil.intToByte(30 + P.round(20f * P.sin((p.frameCount + i * 10f) * 0.03f))));
//		}
		
		// pixel-sampling color send
		noiseTexture.update(2f, 0, p.frameCount * 0.01f, 0);
		ContrastFilter.instance(p).setContrast(2f);
		ContrastFilter.instance(p).applyTo(noiseTexture.texture());
		PImage readTexture = noiseTexture.texture();
		p.image(readTexture, 0, 0);
		readTexture.loadPixels();
		
		float skipPixels = (readTexture.width - 40) / numLights;
		for (int i = 0; i < numLights; i++) {
			// get pixel color from webcam
			int x = 20 + P.round(skipPixels * i);
			int y = readTexture.height / 2;
			int pixelColor = ImageUtil.getPixelColor(readTexture, x, y);
			
			p.fill(pixelColor);
			p.rect(x, y, 20, 20);
			
			// set color on LED strip - lights don't want to go above 127
			float bright = 0.5f * Mouse.xNorm;
			serialDevice.device().write(ConvertUtil.intToByte((int) (p.red(pixelColor) * bright)));
			serialDevice.device().write(ConvertUtil.intToByte((int) (p.green(pixelColor) * bright)));
			serialDevice.device().write(ConvertUtil.intToByte((int) (p.blue(pixelColor) * bright)));
		}
	}
 
Example 9
Source File: ImageUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static PImage getReversePImageFast( PImage image ) {
	PImage reverse = new PImage( image.width, image.height );
	reverse.loadPixels();
	for (int i = 0; i < image.width; i++) {
		for (int j = 0; j < image.height; j++) { 
			reverse.pixels[j*image.width+i] = image.pixels[(image.width - i - 1) + j*image.width]; // Reversing x to mirror the image
		}
	}
	reverse.updatePixels();
	return reverse;
}
 
Example 10
Source File: Spout.java    From haxademic with MIT License 5 votes vote down vote up
/**
 * Receive into image pixels.
 * 
 * @param img - the image to be used and returned
 * @return true if pixels were returned
 */
public PImage receivePixels(PImage img) {

	// If no sender, keep looking
	if(!bReceiverInitialized) {
		createReceiver("");
		return img;
	}

	boolean bInvert = false; // default for this function
	if(invertMode >= 0) bInvert = (invertMode == 1);

	if(dim[0] != img.width || dim[1] != img.height && dim[0] > 0 && dim[1] > 0) {
		img.resize(dim[0], dim[1]);
	}
	else {
		img.loadPixels();
		if(!JNISpout.receivePixels(dim, img.pixels, spoutPtr)) {
			JNISpout.releaseReceiver(spoutPtr);
			senderName = "";
			bReceiverInitialized = false;
		}
	    img.updatePixels();
	}

	return img;
}
 
Example 11
Source File: Demo_SerialDevice_NeoPixel.java    From haxademic with MIT License 4 votes vote down vote up
protected void updateLedLights() {
		// Notes:
		// we can only write 64 bytes per frame: https://arduino.stackexchange.com/questions/14401/arduino-serial-write-sending-more-than-64-bytes
		// though by modifying the Arduino buffer size, we can go up to 256 bytes: http://www.hobbytronics.co.uk/arduino-serial-buffer-size
		// larger sets of data would need to be chunked...
		
		// trigonometry-based color cycling
//		for (float i = 0; i < numLights; i++) {
//			serialDevice.device().write(ConvertUtil.intToByte(30 + P.round(20f * P.sin((p.frameCount + i * 10f) * 0.07f))));
//			serialDevice.device().write(ConvertUtil.intToByte(30 + P.round(20f * P.sin((p.frameCount + i * 10f) * 0.02f))));
//			serialDevice.device().write(ConvertUtil.intToByte(30 + P.round(20f * P.sin((p.frameCount + i * 10f) * 0.03f))));
//		}
		
		// pixel-sampling color send
		noiseTexture.update(2f, 0, p.frameCount * 0.01f, 0);
		ContrastFilter.instance(p).setContrast(2f);
		ContrastFilter.instance(p).applyTo(noiseTexture.texture());
		PImage readTexture = noiseTexture.texture();
		p.image(readTexture, 0, 0);
		readTexture.loadPixels();
		
		// set start byte for arduino
		colorsOut[0] = (byte) '<';
		colorsOut[colorsOut.length - 1] = (byte) '>';
		
		float skipPixels = (readTexture.width - 40) / numLights;
		if(p.frameCount % 10 == 0) {
			for (int i = 0; i < numLights; i++) {
				// get pixel color from webcam
				int x = 20 + P.round(skipPixels * i);
				int y = readTexture.height / 2;
				int pixelColor = ImageUtil.getPixelColor(readTexture, x, y);
				
				p.fill(pixelColor);
				p.rect(x, y, 20, 20);
				
				// set color on LED strip - lights don't want to go above 127
				float bright = 0.75f * Mouse.xNorm;
//				serialDevice.device().write(ConvertUtil.intToByte((int) (p.red(pixelColor) * bright)));
//				serialDevice.device().write(ConvertUtil.intToByte((int) (p.green(pixelColor) * bright)));
//				serialDevice.device().write(ConvertUtil.intToByte((int) (p.blue(pixelColor) * bright)));
				
				// write to byte array
				colorsOut[1 + i] = ConvertUtil.intToByte((int) (p.red(pixelColor) * bright));
//				colorsOut[1 + i * 3 + 0] = ConvertUtil.intToByte((int) (p.red(pixelColor) * bright));
//				colorsOut[1 + i * 3 + 1] = ConvertUtil.intToByte((int) (p.green(pixelColor) * bright));
//				colorsOut[1 + i * 3 + 2] = ConvertUtil.intToByte((int) (p.blue(pixelColor) * bright));
			}
			// do threaded write
			serialDevice.write(colorsOut);
		}
	}
 
Example 12
Source File: PShapeUtil.java    From haxademic with MIT License 4 votes vote down vote up
public static PShape shapeFromImage(PImage img) {
		img.loadPixels();
		PShape newShape = P.p.createShape(P.GROUP);
		newShape.setStroke(false);
		
		for( int x=0; x < img.width; x++ ){
			for(int y=0; y < img.height; y++){
				int pixelColor = ImageUtil.getPixelColor( img, x, y );
//				float pixelBrightness = P.p.brightness( pixelColor );
				if(pixelColor != ImageUtil.TRANSPARENT_PNG) {
//				if( pixelColor != ImageUtil.EMPTY_WHITE_INT && pixelColor != ImageUtil.WHITE_INT ) {
					P.p.fill(EasingColor.redFromColorInt(pixelColor), EasingColor.greenFromColorInt(pixelColor), EasingColor.blueFromColorInt(pixelColor), 255);
					P.p.noStroke();
					
					PShape sh = P.p.createShape();
					sh.beginShape(P.TRIANGLES);
					sh.fill( EasingColor.redFromColorInt(pixelColor), EasingColor.greenFromColorInt(pixelColor), EasingColor.blueFromColorInt(pixelColor), 255 );
					
					// BL, BR, TR, TL
					float size = 0.5f;

					// front
					sh.vertex(x - size, y + size,  size);
					sh.vertex(x + size, y + size,  size);
					sh.vertex(x + size, y - size,  size);
					
					sh.vertex(x - size, y + size,  size);
					sh.vertex(x + size, y - size,  size);
					sh.vertex(x - size, y - size,  size);

					// back
					sh.vertex(x - size, y + size,  -size);
					sh.vertex(x + size, y + size,  -size);
					sh.vertex(x + size, y - size,  -size);
					
					sh.vertex(x - size, y + size,  -size);
					sh.vertex(x + size, y - size,  -size);
					sh.vertex(x - size, y - size,  -size);

					// left
					sh.vertex(x - size, y + size, -size);
					sh.vertex(x - size, y + size,  size);
					sh.vertex(x - size, y - size,  size);

					sh.vertex(x - size, y + size, -size);
					sh.vertex(x - size, y - size,  size);
					sh.vertex(x - size, y - size, -size);

					// right
					sh.vertex(x + size, y + size, -size);
					sh.vertex(x + size, y + size,  size);
					sh.vertex(x + size, y - size,  size);

					sh.vertex(x + size, y + size, -size);
					sh.vertex(x + size, y - size,  size);
					sh.vertex(x + size, y - size, -size);
					
					// floor
					sh.vertex(x - size, y + size, -size);
					sh.vertex(x + size, y + size, -size);
					sh.vertex(x + size, y + size,  size);

					sh.vertex(x + size, y + size,  size);
					sh.vertex(x - size, y + size,  size);
					sh.vertex(x - size, y + size, -size);

					// ceiling
					sh.vertex(x - size, y - size, -size);
					sh.vertex(x + size, y - size, -size);
					sh.vertex(x + size, y - size,  size);

					sh.vertex(x + size, y - size,  size);
					sh.vertex(x - size, y - size,  size);
					sh.vertex(x - size, y - size, -size);

					sh.endShape();

					newShape.addChild(sh);
				}
			}
		}
		
		return newShape;
	}
 
Example 13
Source File: HaxMotionBlur.java    From haxademic with MIT License 4 votes vote down vote up
public void render(PGraphics p, Command drawCommand) {
	// draw the new frame
	drawCommand.execute(p, t);
	
	// save current frame to buffer
	_pastFrames.add(p.get());
	if(_pastFrames.size() > _blurFrames) {
		_pastFrames.remove(0);
	}

	// clear out "offscreen" pixel grid
	if(result == null) result = new int[p.width*p.height][3];
	for (int i=0; i<p.width*p.height; i++)
		for (int a=0; a<3; a++)
			result[i][a] = 0;
	
	// loop through any past frames and add them into the offscreen pixel grid 
	for (int f=0; f < _pastFrames.size(); f++) {
		PImage pastFrame = _pastFrames.get(f);
		pastFrame.loadPixels();
		
		for (int i=0; i<pastFrame.pixels.length; i++) {
			result[i][0] += pastFrame.pixels[i] >> 16 & 0xff;
			result[i][1] += pastFrame.pixels[i] >> 8 & 0xff;
			result[i][2] += pastFrame.pixels[i] & 0xff;
		}
	}

	// copy offscreen pixel grid to PApplet
	p.loadPixels();
	for (int i=0; i < p.pixels.length; i++)
		p.pixels[i] = 
			0xff << 24 | 
			(result[i][0]/_blurFrames) << 16 | 
			(result[i][1]/_blurFrames) << 8 | 
			(result[i][2]/_blurFrames);
	p.updatePixels();
	
	// redraw current frame on top of blur
	drawCommand.execute(p, t);
}
 
Example 14
Source File: Utility.java    From Project-16x16 with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Blurs the input PImage, producing a copy [the orignal is untouched] (much
 * faster than using {@link processing.core.PApplet#filter(int) filter(BLUR)}).
 * 
 * @param in         source PImage
 * @param radius     radius of blur effect
 * @param iterations the number of times to perform the blur; i.e. to increase
 *                   quality
 * @return PImage blurred PImage object
 */
public static PImage blurImage(PImage in, int radius, int iterations) {
	PImage out = new PImage(in.width, in.height);
	out.loadPixels();
	out.pixels = blur(in.pixels, in.width, in.height, radius, iterations);
	out.updatePixels();
	return out;
}