@Override public float height() { if (heightSet) return height; assert img != null : "Image has not yet been set"; return img.height(); }
protected Canvas (Graphics gfx, Image image) { this.gfx = gfx; this.image = image; this.width = image.width(); this.height = image.height(); if (width <= 0 || height <= 0) throw new IllegalArgumentException( "Canvas must be > 0 in width and height: " + width + "x" + height); }
protected Canvas (Graphics gfx, Image image) { this.gfx = gfx; this.image = image; this.width = image.width(); this.height = image.height(); if (width <= 0 || height <= 0) throw new IllegalArgumentException( "Canvas must be > 0 in width and height: " + width + "x" + height); }
protected static ByteBuffer getRgba(Image image) { int w = (int) image.width(), h = (int) image.height(), size = w * h; int[] rawPixels = new int[size]; ByteBuffer pixels = ByteBuffer.allocateDirect(size * 4); pixels.order(ByteOrder.nativeOrder()); IntBuffer rgba = pixels.asIntBuffer(); image.getRgb(0, 0, w, h, rawPixels, 0, w); for (int i = 0; i < size; i++) { int argb = rawPixels[i]; // Order is inverted because this is read as a byte array, and we store intel ints. rgba.put(i, ((argb >> 16) & 0x0ff) | (argb & 0x0ff00ff00) | ((argb & 0xff) << 16)); } return pixels; }
@Override public Surface drawImage(Image image, float x, float y) { return drawImage(image, x, y, image.width(), image.height()); }
@Override public Canvas drawImage(Image img, float x, float y) { return drawImage(img, x, y, img.width(), img.height()); }
@Override public Surface drawImageCentered(Image img, float x, float y) { return drawImage(img, x - img.width()/2, y - img.height()/2); }
@Override public Canvas drawImageCentered(Image img, float x, float y) { return drawImage(img, x - img.width()/2, y - img.height()/2); }
@Override public void texSubImage2D(Image image, int target, int level, int xOffset, int yOffset, int format, int type) { gl.glTexSubImage2D(target, level, xOffset, yOffset, (int) image.width(), (int) image.height(), format, type, getRgba(image)); }
@Override public void texImage2D(Image image, int target, int level, int internalformat, int format, int type) { gl.glTexImage2D(target, level, internalformat, (int) image.width(), (int) image.height(), 0, format, type, getRgba(image)); }
@Override public void update(int delta) { elapsed += delta; if (osci != null) { // round the width so that it sometimes goes to zero; just to be sure zero doesn't choke float width = Math.round(Math.abs(FloatMath.sin(elapsed/1000f)) * osci.parent().width()); osci.setBounds(0, 0, width, osci.parent().height()); } }
// look for all (x,y) positions where target appears in desktop List<Loc> findMatches(Image desktop, Image target, float threshold) { List<Loc> locs; for (int y=0; y<desktop.height()-target.height(); y++) { for (int x=0; x<desktop.width()-target.width(); x++) { if (imageDistance(desktop, x, y, target) < threshold) { locs.append(Loc(x,y)); } } } return locs; } // computes the root mean squared error between a rectangular window in // bigImg and target. float imageDistance(Image bigImg, int bx, int by, Image target) { float dist = 0.0; for (int y=0; y<target.height(); y++) { for (int x=0; x<target.width(); x++) { // assume RGB images... for (int colorChannel=0; colorChannel<3; colorChannel++) { dist += Math.pow(target.getPixel(x,y) - bigImg.getPixel(bx+x,by+y), 2); } } } return Math.sqrt(dist) / target.width() / target.height(); }
public void render (Surface surf) { surf.setFillColor(0xFFCC99FF); surf.fillRect(0, 0, orange.width(), orange.height()); surf.drawImage(orange, 0, 0); } });
/** * Creates a texture with this image's bitmap data using {@code config}. NOTE: this creates a new * texture with every call. This is generally only needed if you plan to create multiple textures * from the same bitmap, with different configurations. Otherwise just use {@link #texture} to * create the image's "default" texture which will be shared by all callers. */ public Texture createTexture (Texture.Config config) { if (!isLoaded()) throw new IllegalStateException( "Cannot create texture from unready image: " + this); int texWidth = config.toTexWidth(pixelWidth()); int texHeight = config.toTexHeight(pixelHeight()); if (texWidth <= 0 || texHeight <= 0) throw new IllegalArgumentException( "Invalid texture size: " + texWidth + "x" + texHeight + " from: " + this); Texture tex = new Texture(gfx, gfx.createTexture(config), config, texWidth, texHeight, scale(), width(), height()); tex.update(this); // this will handle non-POT source image conversion return tex; }
/** * Creates a texture with this image's bitmap data using {@code config}. NOTE: this creates a new * texture with every call. This is generally only needed if you plan to create multiple textures * from the same bitmap, with different configurations. Otherwise just use {@link #texture} to * create the image's "default" texture which will be shared by all callers. */ public Texture createTexture (Texture.Config config) { if (!isLoaded()) throw new IllegalStateException( "Cannot create texture from unready image: " + this); int texWidth = config.toTexWidth(pixelWidth()); int texHeight = config.toTexHeight(pixelHeight()); if (texWidth <= 0 || texHeight <= 0) throw new IllegalArgumentException( "Invalid texture size: " + texWidth + "x" + texHeight + " from: " + this); Texture tex = new Texture(gfx, gfx.createTexture(config), config, texWidth, texHeight, scale(), width(), height()); tex.update(this); // this will handle non-POT source image conversion return tex; }
Box (String text, int color, float wid, float hei) { layer = graphics().createGroupLayer(wid, hei); layer.add(graphics().createImmediateLayer(this)); label = createLabel(text, layer, 0xff000000, color, 0, 0, 40); layer.addAt(label, (wid - label.image().width()) / 2, (hei - label.image().height()) / 2); layer.setHitTester(this); }
@Override public void onSuccess(Image image) { // once the image loads, create our layers imageLayer1 = graphics().createImageLayer(image); SurfaceImage surface1 = graphics().createSurface(image.width(), image.height()); surface1.surface().drawImage(image, 0, 0); surfaceLayer1 = graphics().createImageLayer(surface1); CanvasImage canvas1 = graphics().createImage(image.width(), image.height()); canvas1.canvas().drawImage(image, 0, 0); canvasLayer1 = graphics().createImageLayer(canvas1); imageLayer2 = graphics().createImageLayer(image); SurfaceImage surface2 = graphics().createSurface(image.width(), image.height()); surface2.surface().drawImage(image, 0, 0); surfaceLayer2 = graphics().createImageLayer(surface2); CanvasImage canvas2 = graphics().createImage(image.width(), image.height()); canvas2.canvas().drawImage(image, 0, 0); canvasLayer2 = graphics().createImageLayer(canvas2); // add layers to the rootLayer rootLayer.addAt(imageLayer1, offset, offset); rootLayer.addAt(surfaceLayer1, offset + width, offset); rootLayer.addAt(canvasLayer1, offset + 2 * width, offset); rootLayer.addAt(imageLayer2, offset, offset + 2 * height); rootLayer.addAt(surfaceLayer2, offset + width, offset + 2 * height); rootLayer.addAt(canvasLayer2, offset + 2 * width, offset + 2 * height); }
@Override public void paint(Canvas canvas, float parentAlpha) { if (!visible() || img == null || !img.isReady()) return; canvas.save(); canvas.setAlpha(parentAlpha * alpha()); transform(canvas); float width = width(), height = height(); boolean repX = img.repeatX(), repY = img.repeatY(); if (repX || repY) { if (pattern == null || repX != patternRepeatX || repY != patternRepeatY) { patternRepeatX = repX; patternRepeatY = repY; pattern = img.toPattern(); } canvas.setFillPattern(pattern); float xScale = repX ? 1 : width / img.width(), yScale = repY ? 1 : height / img.height(); canvas.scale(xScale, yScale); canvas.fillRect(0, 0, width / xScale, height / yScale); } else { canvas.drawImage(img, 0, 0, width, height); } canvas.restore(); } }
protected Entity createAsteroid (Size sz, float x, float y, float vx, float vy) { Entity ast = create(true); ast.add(type, size, sprite, opos, pos, vel, spin, radius); float side = sz.size; int iidx = rando.getInt(8); float ah = asteroids.height(); ImageLayer layer = new ImageLayer(asteroids.region(iidx*ah, 0, ah, ah)); layer.setOrigin(ah/2, ah/2); layer.setScale(side/ah); layer.setRotation(rando.getFloat(MathUtil.TAU)); int id = ast.id; type.set(id, ASTEROID); size.set(id, sz); sprite.set(id, layer); spin.set(id, rando.getInRange(-MAXSPIN, MAXSPIN)); opos.set(id, x, y); pos.set(id, x, y); vel.set(id, vx, vy); radius.set(id, side*0.425f); return ast; }
protected void fragment(String source, Image image, float ox, float oy) { float hw = image.width()/2f, hh = image.height()/2f; Image ul = image.subImage(0, 0, hw, hh); Image ur = image.subImage(hw, 0, hw, hh); Image ll = image.subImage(0, hh, hw, hh); Image lr = image.subImage(hw, hh, hw, hh); Image ctr = image.subImage(hw/2, hh/2, hw, hh); float dx = hw + 10, dy = hh + 10; GroupLayer group = graphics().createGroupLayer(); group.addAt(graphics().createImageLayer(ul), 0, 0); group.addAt(graphics().createImageLayer(ur), dx, 0); group.addAt(graphics().createImageLayer(ll), 0, dy); group.addAt(graphics().createImageLayer(lr), dx, dy); group.addAt(graphics().createImageLayer(ctr), dx/2, 2*dy); float xoff = image.width() + 20; group.addAt(scaleLayer(graphics().createImageLayer(ul), 2), xoff, 0); group.addAt(scaleLayer(graphics().createImageLayer(ur), 2), xoff+2*dx, 0); group.addAt(scaleLayer(graphics().createImageLayer(ll), 2), xoff, 2*dy); group.addAt(scaleLayer(graphics().createImageLayer(lr), 2), xoff+2*dx, 2*dy); graphics().rootLayer().addAt(group, ox, oy); addDescrip(source + " split into subimages, and scaled", ox, oy + image.height()*2 + 25, 3*image.width()+40); }