1515import java .awt .image .ColorModel ;
1616import java .awt .image .ImageConsumer ;
1717import java .awt .image .WritableRaster ;
18+ import java .nio .file .Path ;
1819
20+ import swingjs .JSFileSystem .JSPath ;
1921import swingjs .api .Interface ;
2022import swingjs .api .js .DOMNode ;
2123import swingjs .api .js .HTML5Canvas ;
@@ -36,6 +38,7 @@ public class JSImagekit implements ImageConsumer {
3638 private static final int JPG = 1 ;
3739 private static final int GIF = 2 ;
3840 private static final int BMP = 3 ;
41+ private static final int VIDEO = 4 ;
3942 private static final int JPG_SOF0 = 0xC0FF ;
4043 private static final int JPG_SOF2 = 0xC2FF ;
4144
@@ -52,7 +55,7 @@ public class JSImagekit implements ImageConsumer {
5255 */
5356 public JSImage createImageFromBytes (byte [] data , int imageoffset ,
5457 int imagelength , String name ) {
55- return createImageFromBytesStatic (data , imageoffset , imagelength , name );
58+ return createImageFromBytesStatic (data , imageoffset , imagelength , name , UNK );
5659 }
5760
5861 private int width ;
@@ -153,8 +156,8 @@ public void setPixels(int x, int y, int w, int h, ColorModel model,
153156 JSUtil .notImplemented ("byte-based image pixels" );
154157 }
155158
156- private static JSImage createImageFromBytesStatic (byte [] data , int imageoffset ,
157- int imagelength , String name ) {
159+ private static JSImage createImageFromBytesStatic (byte [] data , int imageoffset , int imagelength , String name ,
160+ int imageType ) {
158161 int w = 0 , h = 0 ;
159162 int [] argb = null ;
160163 byte [] b = null ;
@@ -163,20 +166,24 @@ private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
163166 // this is from Component.createImage();
164167 w = imageoffset ;
165168 h = imagelength ;
169+ } else if (imageType == VIDEO ){
170+ b = data ;
171+ w = imageoffset ;
172+ h = imagelength ;
173+ type = "video" ;
166174 } else {
167175 if (imagelength < 0 )
168176 imagelength = data .length ;
169- // not implemented in JavaScript:
177+ // not implemented in JavaScript:
170178 // b = Arrays.copyOfRange(data, imageoffset, imagelength);
171179 int n = imagelength - imageoffset ;
172- System .arraycopy (data , imageoffset , b = new byte [n ], 0 , n );
173- if (b .length < 10 )//was 54??? I have no recollection of why that might be.
180+ System .arraycopy (data , imageoffset , b = new byte [n ], 0 , n );
181+ if (b .length < 10 )// was 54??? I have no recollection of why that might be.
174182 return null ;
175- switch (getSourceType (b )) {
183+ switch (imageType == UNK ? getSourceType (b ) : imageType ) {
176184 case BMP :
177185 // just get bytes directly
178- BMPDecoder ie = (BMPDecoder ) Interface .getInstance (
179- "javajs.img.BMPDecoder" , true );
186+ BMPDecoder ie = (BMPDecoder ) Interface .getInstance ("javajs.img.BMPDecoder" , true );
180187 Object [] o = ie .decodeWindowsBMP (b );
181188 if (o == null || o [0 ] == null )
182189 return null ;
@@ -212,17 +219,16 @@ private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
212219 type = "gif" ;
213220 break ;
214221 case UNK :
215- System .out .println ("JSImagekit: Unknown image type: " + b [0 ] + " "
216- + b [1 ] + " " + b [2 ] + " " + b [3 ]);
222+ System .out .println ("JSImagekit: Unknown image type: " + b [0 ] + " " + b [1 ] + " " + b [2 ] + " " + b [3 ]);
217223 data = null ;
218224 break ;
219225 }
220226 }
221227 if (w == 0 || h == 0 )
222228 return null ;
223- JSImage jsimage = new JSImage (argb , w , h , name );
229+ JSImage jsimage = new JSImage (argb , w , h , name );
224230 if (data != null && argb == null )
225- jsimage .getDOMImage ( b , type );
231+ jsimage .setImageNode ( null , b , type );
226232 return jsimage ;
227233 }
228234
@@ -298,5 +304,15 @@ public static JSGraphics2D createCanvasGraphics(int width, int height, String id
298304 return new JSGraphics2D (canvas );
299305 }
300306
307+ public Image createVideo (Path path ) {
308+ JSImage jsimage = new JSImage ((byte [])null , 1 , 1 , path .toString ());
309+ jsimage .setImageNode ((JSPath ) path , null , "video" );
310+ return jsimage ;
311+ }
312+
313+ public Image createVideo (byte [] bytes ) {
314+ return createImageFromBytesStatic (bytes , 1 , 1 , null , VIDEO );
315+ }
316+
301317
302318}
0 commit comments