88import java .nio .Buffer ;
99import java .nio .ByteBuffer ;
1010import java .nio .ByteOrder ;
11- import java .nio .channels .Channels ;
1211import java .nio .channels .FileChannel ;
1312import java .util .Arrays ;
1413import java .util .Hashtable ;
@@ -580,11 +579,20 @@ public void dumpToFile(final ByteBuffer buf) {
580579 }
581580
582581 public static GLTexture loadFromFile (final GL4 gl , final String filename , final boolean linearInterpolation ,
583- final int mipmapLevels ) {
582+ final int mipmapLevels ) throws FileNotFoundException {
584583 return loadFromFile (gl , filename , linearInterpolation , true , mipmapLevels );
585584 }
586585
587586 public static GLTexture loadFromFile (final GL4 gl , final String filename , final boolean linearInterpolation ,
587+ final boolean generateMipmaps , final int maxMipmapLevels ) throws FileNotFoundException {
588+ FileInputStream inputStream = new FileInputStream (filename );
589+ final String type = filename .substring (filename .lastIndexOf ('.' )).toLowerCase ();
590+
591+ return loadFromFile (gl , inputStream , type , linearInterpolation , generateMipmaps , maxMipmapLevels );
592+ }
593+
594+ public static GLTexture loadFromFile (final GL4 gl , final InputStream input , final String type ,
595+ final boolean linearInterpolation ,
588596 final boolean generateMipmaps , final int maxMipmapLevels ) {
589597 BufferedImage bi ;
590598 BufferedImage flippedImage ;
@@ -594,36 +602,30 @@ public static GLTexture loadFromFile(final GL4 gl, final String filename, final
594602 int [] pixels = null ;
595603 GLTexture tex ;
596604
597- if (filename . substring ( filename . lastIndexOf ( '.' )) .toLowerCase ().endsWith ("tga" )) {
605+ if (type .toLowerCase ().endsWith ("tga" )) {
598606 byte [] buffer = null ;
599607
600608 try {
601- fis = new FileInputStream (filename );
602- channel = fis .getChannel ();
603- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
604- channel .transferTo (0 , channel .size (), Channels .newChannel (byteArrayOutputStream ));
605- buffer = byteArrayOutputStream .toByteArray ();
606-
607- channel .close ();
608- fis .close ();
609+ BufferedInputStream s = new BufferedInputStream (input );
610+ buffer = new byte [s .available ()];
611+ s .read (buffer );
612+ s .close ();
609613
610614 pixels = TGAReader .read (buffer , TGAReader .ARGB );
611615 final int width = TGAReader .getWidth (buffer );
612616 final int height = TGAReader .getHeight (buffer );
613617 bi = new BufferedImage (width , height , BufferedImage .TYPE_INT_ARGB );
614618 bi .setRGB (0 , 0 , width , height , pixels , 0 , width );
615619 } catch (final Exception e ) {
616- System .err .println ("GLTexture: could not read image from TGA" + filename + "." );
620+ System .err .println ("GLTexture: could not read image from TGA" );
617621 return null ;
618622 }
619623 } else {
620624 try {
621- fis = new FileInputStream (filename );
622- bi = ImageIO .read (fis );
623- fis .close ();
625+ bi = ImageIO .read (input );
624626
625627 } catch (final Exception e ) {
626- System .err .println ("GLTexture: could not read image from " + filename + "." );
628+ System .err .println ("GLTexture: could not read image. " + e . getMessage () );
627629 return null ;
628630 }
629631 }
@@ -652,7 +654,7 @@ public static GLTexture loadFromFile(final GL4 gl, final String filename, final
652654 int channelCount = bi .getColorModel ().getNumComponents ();
653655
654656 // work around a Java2D issue
655- if (channelCount == 3 && filename . substring ( filename . lastIndexOf ( "." )) .toLowerCase ().endsWith ("png" )) {
657+ if (channelCount == 3 && type .toLowerCase ().endsWith ("png" )) {
656658 channelCount = 4 ;
657659 }
658660
0 commit comments