-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
869 additions
and
28 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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.util.*; | ||
import java.util.stream.*; | ||
import org.jetbrains.skija.*; | ||
|
||
public class CodecScene extends Scene { | ||
Paint stroke = new Paint().setColor(0x80CC3333).setMode(PaintMode.STROKE).setStrokeWidth(1); | ||
List<Pair<String, Bitmap>> formats = new ArrayList<>(); | ||
List<Pair<String, Codec>> orientations = new ArrayList<>(); | ||
|
||
static class Animation { | ||
Codec codec; | ||
Bitmap bitmap; | ||
int prevFrame = -1; | ||
int[] durations; | ||
long totalDuration; | ||
} | ||
|
||
List<Pair<String, Animation>> animations = new ArrayList<>(); | ||
|
||
float x, y; | ||
float rowH = 100; | ||
float columnW = 100; | ||
|
||
public CodecScene() { | ||
for (var file: new String[] {"bmp.bmp", "gif.gif", "favicon.ico", "dotpeek.ico", "jpeg.jpg", "png.png", "webp_lossy.webp", "webp_loseless.webp"}) { | ||
try (var codec = Codec.makeFromData(Data.makeFromFileName(file("images/codecs/" + file)))) { | ||
formats.add(new Pair(file + "\n" + codec.getEncodedImageFormat(), codec.readPixels())); | ||
} catch (Exception e) { | ||
formats.add(new Pair(file + "\n" + e.getMessage(), null)); | ||
} | ||
} | ||
|
||
for (var file: new String[] {"orient_tl.jpg", "orient_tr.jpg", "orient_br.jpg", "orient_bl.jpg", "orient_lt.jpg", "orient_lb.jpg", "orient_rb.jpg", "orient_rt.jpg",}) { | ||
orientations.add(new Pair(file, Codec.makeFromData(Data.makeFromFileName(file("images/codecs/" + file))))); | ||
} | ||
|
||
for (var file: new String[] {"animated.gif", "animated.webp"}) { | ||
var animation = new Animation(); | ||
animation.codec = Codec.makeFromData(Data.makeFromFileName(file("images/codecs/" + file))); | ||
animation.bitmap = new Bitmap(); | ||
animation.bitmap.allocPixels(animation.codec.getImageInfo()); | ||
animation.durations = Arrays.stream(animation.codec.getFramesInfo()).mapToInt(AnimationFrameInfo::getDuration).toArray(); | ||
animation.totalDuration = Arrays.stream(animation.durations).sum(); | ||
animations.add(new Pair(file, animation)); | ||
} | ||
} | ||
|
||
public void drawOne(Canvas canvas, int width, String s, Runnable draw) { | ||
if (x + columnW >= width) { | ||
x = 20; | ||
y += rowH + 60; | ||
} | ||
canvas.save(); | ||
canvas.translate(x, y); | ||
draw.run(); | ||
|
||
var lines = s.lines().toArray(); | ||
for (int i = 0; i < lines.length; ++i) | ||
canvas.drawString((String) lines[i], 0, rowH + 20 + i * 20, inter13, blackFill); | ||
canvas.restore(); | ||
x += columnW + 20; | ||
} | ||
|
||
@Override | ||
public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int ypos) { | ||
x = 20; | ||
y = 20; | ||
|
||
for (var pair: formats) { | ||
var label = pair.getFirst(); | ||
var bitmap = pair.getSecond(); | ||
if (bitmap == null) { | ||
drawOne(canvas, width, label, () -> { | ||
canvas.drawRect(Rect.makeXYWH(0, 0, columnW, rowH), stroke); | ||
canvas.drawLine(0, 0, columnW, rowH, stroke); | ||
canvas.drawLine(0, rowH, columnW, 0, stroke); | ||
}); | ||
} else { | ||
drawOne(canvas, width, label, () -> { canvas.drawBitmapRect(bitmap, Rect.makeXYWH(0, 0, columnW, rowH)); }); | ||
} | ||
} | ||
|
||
x = 20; | ||
y += rowH + 60; | ||
for (var pair: orientations) { | ||
var label = pair.getFirst(); | ||
var codec = pair.getSecond(); | ||
var origin = codec.getEncodedOrigin(); | ||
try (var bitmap = codec.readPixels()) { | ||
int bitmapWidth = origin.swapsWidthHeight() ? codec.getHeight() : codec.getWidth(); | ||
int bitmapHeight = origin.swapsWidthHeight() ? codec.getWidth() : codec.getHeight(); | ||
drawOne(canvas, width, label + "\n" + codec.getEncodedImageFormat(), () -> { | ||
canvas.save(); | ||
canvas.concat(origin.toMatrix(bitmapWidth, bitmapHeight)); | ||
canvas.drawBitmapRect(bitmap, Rect.makeXYWH(0, 0, codec.getWidth(), codec.getHeight())); | ||
canvas.restore(); | ||
canvas.drawRect(Rect.makeXYWH(0, 0, bitmapWidth, bitmapHeight), stroke); | ||
}); | ||
} | ||
} | ||
|
||
x = 20; | ||
y += rowH + 60; | ||
|
||
for (var pair: animations) { | ||
var label = pair.getFirst(); | ||
var animation = pair.getSecond(); | ||
var codec = animation.codec; | ||
|
||
int duration = 0; | ||
int frame = 0; | ||
long now = System.currentTimeMillis() % animation.totalDuration; | ||
for (; frame < animation.durations.length; ++frame) { | ||
duration += animation.durations[frame]; | ||
if (duration >= now) | ||
break; | ||
} | ||
int finalFrame = frame; | ||
|
||
drawOne(canvas, width, label + "\n" + codec.getEncodedImageFormat(), () -> { | ||
try (var bitmap = new Bitmap()) { | ||
bitmap.allocPixels(codec.getImageInfo()); | ||
codec.readPixels(bitmap, finalFrame); | ||
canvas.drawBitmapRect(bitmap, Rect.makeXYWH(0, 0, columnW, rowH)); | ||
} | ||
}); | ||
|
||
drawOne(canvas, width, label + "\n" + codec.getEncodedImageFormat() + " + priorFrame", () -> { | ||
codec.readPixels(animation.bitmap, finalFrame, animation.prevFrame); | ||
canvas.drawBitmapRect(animation.bitmap, Rect.makeXYWH(0, 0, columnW, rowH)); | ||
}); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <iostream> | ||
#include <jni.h> | ||
#include "SkBitmap.h" | ||
#include "SkCodec.h" | ||
#include "SkData.h" | ||
#include "interop.hh" | ||
|
||
static void deleteCodec(SkCodec* instance) { | ||
delete instance; | ||
} | ||
|
||
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_Codec__1nGetFinalizer(JNIEnv* env, jclass jclass) { | ||
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deleteCodec)); | ||
} | ||
|
||
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_Codec__1nMakeFromData | ||
(JNIEnv* env, jclass jclass, jlong dataPtr) { | ||
SkData* data = reinterpret_cast<SkData*>(static_cast<uintptr_t>(dataPtr)); | ||
std::unique_ptr<SkCodec> instance = SkCodec::MakeFromData(sk_ref_sp(data)); | ||
return reinterpret_cast<jlong>(instance.release()); | ||
} | ||
|
||
extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_Codec__1nGetImageInfo | ||
(JNIEnv* env, jclass jclass, jlong ptr) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
return skija::ImageInfo::toJava(env, instance->getInfo()); | ||
} | ||
|
||
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_Codec__1nGetSize | ||
(JNIEnv* env, jclass jclass, jlong ptr) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
return packISize(instance->dimensions()); | ||
} | ||
|
||
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_Codec__1nGetEncodedOrigin | ||
(JNIEnv* env, jclass jclass, jlong ptr) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
return static_cast<jint>(instance->getOrigin()); | ||
} | ||
|
||
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skija_Codec__1nGetEncodedImageFormat | ||
(JNIEnv* env, jclass jclass, jlong ptr) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
return static_cast<jint>(instance->getEncodedFormat()); | ||
} | ||
|
||
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skija_Codec__1nReadPixels | ||
(JNIEnv* env, jclass jclass, jlong ptr, jlong bitmapPtr, jint frame, jint priorFrame) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(static_cast<uintptr_t>(bitmapPtr)); | ||
SkCodec::Options opts; | ||
opts.fFrameIndex = frame; | ||
opts.fPriorFrame = priorFrame; | ||
SkCodec::Result result = instance->getPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), &opts); | ||
return static_cast<jint>(result); | ||
} | ||
|
||
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skija_Codec__1nGetFrameCount | ||
(JNIEnv* env, jclass jclass, jlong ptr) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
return instance->getFrameCount(); | ||
} | ||
|
||
extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_Codec__1nGetFrameInfo | ||
(JNIEnv* env, jclass jclass, jlong ptr, jint frame) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
SkCodec::FrameInfo info; | ||
instance->getFrameInfo(frame, &info); | ||
return skija::AnimationFrameInfo::toJava(env, info); | ||
} | ||
|
||
extern "C" JNIEXPORT jobject JNICALL Java_org_jetbrains_skija_Codec__1nGetFramesInfo | ||
(JNIEnv* env, jclass jclass, jlong ptr, jint frame) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
SkCodec::FrameInfo info; | ||
std::vector<SkCodec::FrameInfo> frames = instance->getFrameInfo(); | ||
jobjectArray res = env->NewObjectArray(frames.size(), skija::AnimationFrameInfo::cls, nullptr); | ||
for (int i = 0; i < frames.size(); ++i) { | ||
jobject infoObj = skija::AnimationFrameInfo::toJava(env, frames[i]); | ||
env->SetObjectArrayElement(res, i, infoObj); | ||
env->DeleteLocalRef(infoObj); | ||
} | ||
return res; | ||
} | ||
|
||
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skija_Codec__1nGetRepetitionCount | ||
(JNIEnv* env, jclass jclass, jlong ptr) { | ||
SkCodec* instance = reinterpret_cast<SkCodec*>(static_cast<uintptr_t>(ptr)); | ||
return instance->getRepetitionCount(); | ||
} |
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.