1+ package org .jetbrains .skija .examples .scenes ;
2+
3+ import java .io .*;
4+ import java .nio .*;
5+ import java .nio .file .*;
6+ import java .nio .file .Path ;
7+ import java .util .*;
8+ import java .util .function .*;
9+ import org .jetbrains .skija .*;
10+
11+ public class BitmapImageScene extends Scene {
12+ public final Image image ;
13+ public int x , y ;
14+
15+ public BitmapImageScene () {
16+ try {
17+ image = Image .makeFromEncoded (Files .readAllBytes (Path .of (file ("images/IMG_7098.jpeg" ))));
18+ } catch (IOException e ) {
19+ throw new RuntimeException (e );
20+ }
21+ }
22+
23+ public void advance (Canvas canvas , int width ) {
24+ canvas .restore ();
25+ x += 220 ;
26+ if (x + 220 >= width ) {
27+ x = 20 ;
28+ y += 240 ;
29+ }
30+ canvas .save ();
31+ canvas .translate (x , y );
32+ }
33+
34+ @ Override
35+ public void draw (Canvas canvas , int width , int height , float dpi , int xpos , int ypos ) {
36+ canvas .save ();
37+ canvas .translate (20 , 20 );
38+ x = 20 ;
39+ y = 20 ;
40+
41+ // Image
42+ canvas .drawImageRect (image , Rect .makeXYWH (0 , 0 , 200 , 200 ));
43+ canvas .drawString ("Image" , 0 , 220 , inter13 , blackFill );
44+ advance (canvas , width );
45+
46+ // Bitmap + Image.readPixels
47+ var bitmap = new Bitmap ();
48+ bitmap .allocPixels (ImageInfo .makeS32 (400 , 400 , ColorAlphaType .OPAQUE ));
49+ image .readPixels (bitmap );
50+ canvas .drawBitmapRect (bitmap , Rect .makeXYWH (0 , 0 , 200 , 200 ));
51+ canvas .drawString ("Image.readPixels" , 0 , 220 , inter13 , blackFill );
52+ advance (canvas , width );
53+
54+ // Bitmap + Image.readPixels(50, 50)
55+ var partialBitmap = new Bitmap ();
56+ partialBitmap .allocPixels (new ImageInfo (300 , 300 , ColorType .GRAY_8 , ColorAlphaType .OPAQUE ));
57+ image .readPixels (partialBitmap , 50 , 50 );
58+ canvas .drawBitmapRect (partialBitmap , Rect .makeXYWH (25 , 25 , 150 , 150 ));
59+ canvas .drawString ("Image.readPixels(50, 50)" , 0 , 220 , inter13 , blackFill );
60+ advance (canvas , width );
61+
62+ // Bitmap.makeFromImage
63+ var bitmapFromImage = Bitmap .makeFromImage (image );
64+ canvas .drawBitmapRect (bitmapFromImage , Rect .makeXYWH (0 , 0 , 200 , 200 ));
65+ canvas .drawString ("Bitmap.makeFromImage" , 0 , 220 , inter13 , blackFill );
66+ advance (canvas , width );
67+
68+ // Image.makeFromBitmap
69+ var imageFromBitmap = Image .makeFromBitmap (bitmap );
70+ canvas .drawImageRect (imageFromBitmap , Rect .makeXYWH (0 , 0 , 200 , 200 ));
71+ canvas .drawString ("Image.makeFromBitmap" , 0 , 220 , inter13 , blackFill );
72+ advance (canvas , width );
73+
74+ // Bitmap readPixels/installPixels
75+ var info = bitmapFromImage .getImageInfo ();
76+ var threshold = 100 + phase () * 100 ;
77+ byte [] pixels = bitmapFromImage .readPixels ();
78+ ByteBuffer buffer = ByteBuffer .wrap (pixels ); // Assume RGBA_8888
79+ Function <Integer , Integer > luminocity = color -> Color .getR (color ) + Color .getG (color ) + Color .getB (color );
80+ Comparator <Integer > cmp = (a , b ) -> Integer .compare (luminocity .apply (a ), luminocity .apply (b ));
81+ for (int x = 0 ; x < info .getWidth (); ++x ) {
82+ // read pixels
83+ Integer column [] = new Integer [info .getHeight ()];
84+ for (int y = 0 ; y < info .getHeight (); ++y )
85+ column [y ] = buffer .getInt ((y * info .getWidth () + x ) * info .getBytesPerPixel ());
86+
87+ // sort pixels
88+ var lastIdx = 0 ;
89+ for (int y = 0 ; y < info .getHeight () - 1 ; ++y ) {
90+ if (Math .abs (luminocity .apply (column [y ]) - luminocity .apply (column [y + 1 ])) > threshold ) {
91+ Arrays .parallelSort (column , lastIdx , y , cmp );
92+ lastIdx = y ;
93+ }
94+ }
95+ Arrays .parallelSort (column , lastIdx , info .getHeight (), cmp );
96+
97+ // write pixels
98+ for (int y = 0 ; y < info .getHeight (); ++y )
99+ buffer .putInt ((y * info .getWidth () + x ) * info .getBytesPerPixel (), column [y ]);
100+ }
101+ bitmapFromImage .installPixels (pixels );
102+ canvas .drawBitmapRect (bitmapFromImage , Rect .makeXYWH (0 , 0 , 200 , 200 ));
103+ canvas .drawString ("Bitmap.readPixels/installPixels" , 0 , 220 , inter13 , blackFill );
104+ advance (canvas , width );
105+
106+ // Image.makeRaster
107+ var imageFromPixels = Image .makeRaster (info , pixels , info .getMinRowBytes ());
108+ canvas .drawImageRect (imageFromPixels , Rect .makeXYWH (0 , 0 , 200 , 200 ));
109+ canvas .drawString ("Image.makeRaster" , 0 , 220 , inter13 , blackFill );
110+ advance (canvas , width );
111+
112+ // Image.makeRaster + Data
113+ var imageFromData = Image .makeRaster (info , Data .makeFromBytes (pixels ), info .getMinRowBytes ());
114+ canvas .drawImageRect (imageFromPixels , Rect .makeXYWH (0 , 0 , 200 , 200 ));
115+ canvas .drawString ("Image.makeRaster + Data" , 0 , 220 , inter13 , blackFill );
116+ advance (canvas , width );
117+
118+ bitmap .close ();
119+ partialBitmap .close ();
120+ bitmapFromImage .close ();
121+ imageFromBitmap .close ();
122+ imageFromPixels .close ();
123+ imageFromData .close ();
124+ }
125+ }
0 commit comments