-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bitmap.makeFromImage, Image.makeRaster, .makeFromBitmap, .getImageInf…
- Loading branch information
Showing
13 changed files
with
541 additions
and
142 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package org.jetbrains.skija.examples.scenes; | ||
|
||
import java.io.*; | ||
import java.nio.file.*; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
import org.jetbrains.skija.*; | ||
|
||
public class BitmapImageScene extends Scene { | ||
public final Image image; | ||
public int x, y; | ||
|
||
public BitmapImageScene() { | ||
try { | ||
image = Image.makeFromEncoded(Files.readAllBytes(Path.of(file("images/IMG_7098.jpeg")))); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void advance(Canvas canvas, int width) { | ||
canvas.restore(); | ||
x += 220; | ||
if (x + 220 >= width) { | ||
x = 20; | ||
y += 240; | ||
} | ||
canvas.save(); | ||
canvas.translate(x, y); | ||
} | ||
|
||
@Override | ||
public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int ypos) { | ||
canvas.save(); | ||
canvas.translate(20, 20); | ||
x = 20; | ||
y = 20; | ||
|
||
// Image | ||
canvas.drawImageRect(image, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Image", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Bitmap + Image.readPixels | ||
var bitmap = new Bitmap(); | ||
bitmap.allocPixels(ImageInfo.makeS32(400, 400, ColorAlphaType.OPAQUE)); | ||
image.readPixels(bitmap); | ||
canvas.drawBitmapRect(bitmap, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Image.readPixels", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Bitmap + Image.readPixels(50, 50) | ||
var partialBitmap = new Bitmap(); | ||
partialBitmap.allocPixels(new ImageInfo(300, 300, ColorType.GRAY_8, ColorAlphaType.OPAQUE)); | ||
image.readPixels(partialBitmap, 50, 50); | ||
canvas.drawBitmapRect(partialBitmap, Rect.makeXYWH(25, 25, 150, 150)); | ||
canvas.drawString("Image.readPixels(50, 50)", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Bitmap.makeFromImage | ||
var bitmapFromImage = Bitmap.makeFromImage(image); | ||
canvas.drawBitmapRect(bitmapFromImage, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Bitmap.makeFromImage", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Image.makeFromBitmap | ||
var imageFromBitmap = Image.makeFromBitmap(bitmap); | ||
canvas.drawImageRect(imageFromBitmap, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Image.makeFromBitmap", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Bitmap readPixels/installPixels | ||
var info = bitmapFromImage.getImageInfo(); | ||
int rowBytes = (int) info.getMinRowBytes(); | ||
int bpp = info.getBytesPerPixel(); | ||
var threshold = 0.25f + phase() * 1.25f; | ||
byte[] pixels = bitmapFromImage.readPixels(); | ||
for (int x = 0; x < info.getWidth(); ++x) { | ||
// read pixels | ||
Color4f column[] = new Color4f[info.getHeight()]; | ||
for (int y = 0; y < info.getHeight(); ++y) { | ||
// RGBA_8888 | ||
var red = pixels[y * rowBytes + x * bpp]; | ||
var green = pixels[y * rowBytes + x * bpp + 1]; | ||
var blue = pixels[y * rowBytes + x * bpp + 2]; | ||
var alpha = pixels[y * rowBytes + x * bpp + 3]; | ||
column[y] = new Color4f(Byte.toUnsignedInt(red) / 255f, | ||
Byte.toUnsignedInt(green) / 255f, | ||
Byte.toUnsignedInt(blue) / 255f, | ||
Byte.toUnsignedInt(alpha) / 255f); | ||
} | ||
|
||
// sort pixels | ||
var lastIdx = 0; | ||
|
||
for (int y = 0; y < info.getHeight() - 1; ++y) { | ||
var cur = column[y]; | ||
var next = column[y + 1]; | ||
if (Math.abs((cur.getR() + cur.getG() + cur.getB()) - (next.getR() + next.getG() + next.getB())) > threshold) { | ||
Arrays.<Color4f> parallelSort(column, lastIdx, y, (a, b) -> Float.compare(a.getR() + a.getG() + a.getB(), b.getR() + b.getG() + b.getB())); | ||
lastIdx = y; | ||
} | ||
} | ||
Arrays.<Color4f> parallelSort(column, lastIdx, info.getHeight(), (a, b) -> Float.compare(a.getR() + a.getG() + a.getB(), b.getR() + b.getG() + b.getB())); | ||
|
||
// write pixels | ||
for (int y = 0; y < info.getHeight(); ++y) { | ||
pixels[y * rowBytes + x * bpp] = Integer.valueOf((int) (column[y].getR() * 255f)).byteValue(); | ||
pixels[y * rowBytes + x * bpp + 1] = Integer.valueOf((int) (column[y].getG() * 255f)).byteValue(); | ||
pixels[y * rowBytes + x * bpp + 2] = Integer.valueOf((int) (column[y].getB() * 255f)).byteValue(); | ||
} | ||
} | ||
bitmapFromImage.installPixels(pixels); | ||
canvas.drawBitmapRect(bitmapFromImage, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Bitmap.readPixels/installPixels", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Image.makeRaster | ||
var imageFromPixels = Image.makeRaster(info, pixels, rowBytes); | ||
canvas.drawImageRect(imageFromPixels, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Image.makeRaster", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
// Image.makeRaster + Data | ||
var imageFromData = Image.makeRaster(info, Data.makeFromBytes(pixels), rowBytes); | ||
canvas.drawImageRect(imageFromPixels, Rect.makeXYWH(0, 0, 200, 200)); | ||
canvas.drawString("Image.makeRaster + Data", 0, 220, inter13, blackFill); | ||
advance(canvas, width); | ||
|
||
bitmap.close(); | ||
partialBitmap.close(); | ||
bitmapFromImage.close(); | ||
imageFromBitmap.close(); | ||
imageFromPixels.close(); | ||
imageFromData.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.