@@ -231,26 +231,26 @@ private static final class Header {
231231
232232 int bitmapSize ;
233233
234- /** ビットマップファイルのヘッダを読み込みます. */
235- static final Header readFrom (InputStream in ) throws IOException {
234+ /**
235+ * ビットマップファイルのヘッダを読み込みます.
236+ * @throws IllegalArgumentException not a windows bitmap
237+ */
238+ static final Header readFrom (LittleEndianDataInputStream lin ) throws IOException {
236239
237240 Header header = new Header ();
238241
239- @ SuppressWarnings ("resource" )
240- LittleEndianDataInputStream iin = new LittleEndianDataInputStream (in );
241-
242- @ SuppressWarnings ("unused" )
243- int dummy ;
242+ byte [] signature = new byte [2 ];
244243
245244 // 14 bytes
246- dummy = iin .read ();
247- // Debug.println((byte) dummy);
248- dummy = iin .read ();
249- // Debug.println((char) dummy);
250- header .bitmapSize = iin .readInt ();
251- dummy = iin .readShort ();
252- dummy = iin .readShort ();
253- header .bitmapOffset = iin .readInt ();
245+
246+ lin .readFully (signature );
247+ if (signature [0 ] != 'B' || signature [1 ] != 'M' ) {
248+ throw new IllegalArgumentException ("not a windows bitmap" );
249+ }
250+ header .bitmapSize = lin .readInt ();
251+ lin .readShort ();
252+ lin .readShort ();
253+ header .bitmapOffset = lin .readInt ();
254254
255255 return header ;
256256 }
@@ -336,28 +336,25 @@ public String toString() {
336336 /**
337337 * ストリームからビットマップヘッダのインスタンスを作成します.
338338 */
339- static final WindowsBitmapHeader readFrom (InputStream in ) throws IOException {
339+ static final WindowsBitmapHeader readFrom (LittleEndianDataInputStream lin ) throws IOException {
340340
341341 WindowsBitmapHeader bh = new WindowsBitmapHeader ();
342342
343- @ SuppressWarnings ("resource" )
344- LittleEndianDataInputStream iin = new LittleEndianDataInputStream (in );
345-
346343 // read 40 bytes
347- bh .headerSize = iin .readInt ();
348- bh .width = iin .readInt ();
349- bh .height = iin .readInt ();
350- bh .planes = iin .readShort ();
351- bh .bits = iin .readShort ();
352- bh .compression = iin .readInt ();
353- bh .imageSize = iin .readInt ();
354- bh .ppmX = iin .readInt ();
355- bh .ppmY = iin .readInt ();
356- bh .usedColor = iin .readInt ();
357- bh .importantColor = iin .readInt ();
344+ bh .headerSize = lin .readInt ();
345+ bh .width = lin .readInt ();
346+ bh .height = lin .readInt ();
347+ bh .planes = lin .readShort ();
348+ bh .bits = lin .readShort ();
349+ bh .compression = lin .readInt ();
350+ bh .imageSize = lin .readInt ();
351+ bh .ppmX = lin .readInt ();
352+ bh .ppmY = lin .readInt ();
353+ bh .usedColor = lin .readInt ();
354+ bh .importantColor = lin .readInt ();
358355
359356//Debug.println("bitmap");
360- Debug .print (bh );
357+ // Debug.print(bh);
361358
362359 if (bh .usedColor == 0 ) {
363360 switch (bh .bits ) {
@@ -387,12 +384,13 @@ static final WindowsBitmapHeader readFrom(InputStream in) throws IOException {
387384 byte [] reds = new byte [bh .usedColor ];
388385 byte [] greens = new byte [bh .usedColor ];
389386 byte [] blues = new byte [bh .usedColor ];
387+ byte [] alphas = new byte [bh .usedColor ];
390388
391389 for (int i = 0 ; i < bh .usedColor ; i ++) {
392- blues [i ] = ( byte ) in . read ();
393- greens [i ] = ( byte ) in . read ();
394- reds [i ] = ( byte ) in . read ();
395- /* alpha */ in . read ();
390+ blues [i ] = lin . readByte ();
391+ greens [i ] = lin . readByte ();
392+ reds [i ] = lin . readByte ();
393+ alphas [ i ] = lin . readByte ();
396394//System.err.print("(" + i + ")");
397395//bh.palette[i].print();
398396 }
@@ -424,8 +422,10 @@ static final WindowsBitmapHeader readFrom(InputStream in) throws IOException {
424422 }
425423 }
426424
427- in .skip (bh .headerSize - 40 );
428- Debug .println ("skip: " + (bh .headerSize - 40 ));
425+ lin .skipBytes (bh .headerSize - 40 );
426+ if (bh .headerSize - 40 > 0 ) {
427+ Debug .println ("skip: " + (bh .headerSize - 40 ));
428+ }
429429
430430 return bh ;
431431 }
@@ -684,15 +684,9 @@ public byte[] get256ColorRleData() {
684684 // -------------------------------------------------------------------------
685685
686686 /** ビットマップイメージを読み込みます. */
687- private static byte [] readBitmap (InputStream in , int num ) throws IOException {
688-
689- byte buf [] = new byte [num ];
690-
691- int l = 0 ;
692- while (l < num ) {
693- l += in .read (buf , l , num - l );
694- }
695-
687+ private static byte [] readBitmap (LittleEndianDataInputStream in , int num ) throws IOException {
688+ byte [] buf = new byte [num ];
689+ in .readFully (buf , 0 , num );
696690 return buf ;
697691 }
698692
@@ -704,17 +698,19 @@ private static byte[] readBitmap(InputStream in, int num) throws IOException {
704698 */
705699 public static WindowsBitmap readFrom (InputStream in ) throws IOException {
706700
701+ LittleEndianDataInputStream lin = new LittleEndianDataInputStream (in );
702+
707703 WindowsBitmap bitmap = new WindowsBitmap ();
708704
709- bitmap .header = Header .readFrom (in );
710- bitmap .bitmapHeader = WindowsBitmapHeader .readFrom (in );
705+ bitmap .header = Header .readFrom (lin );
706+ bitmap .bitmapHeader = WindowsBitmapHeader .readFrom (lin );
711707
712708 if (bitmap .bitmapHeader .imageSize == 0 ) {
713709 bitmap .bitmapHeader .imageSize = bitmap .header .bitmapSize - bitmap .header .bitmapOffset ;
714710//Debug.println(b.bitmapHeader.imageSize);
715711 }
716712
717- bitmap .bitmap = readBitmap (in , bitmap .bitmapHeader .imageSize );
713+ bitmap .bitmap = readBitmap (lin , bitmap .bitmapHeader .imageSize );
718714
719715 return bitmap ;
720716 }
@@ -736,15 +732,15 @@ public static WindowsBitmap readFrom(InputStream in) throws IOException {
736732 *
737733 * </code></pre>
738734 */
739- protected static WindowsBitmap readFrom (InputStream in , int off , int size ) throws IOException {
735+ public static WindowsBitmap readFrom (LittleEndianDataInputStream lin , int off , int size ) throws IOException {
740736
741737 WindowsBitmap bitmap = new WindowsBitmap ();
742738
743739 bitmap .header = new Header ();
744740
745741 bitmap .header .bitmapSize = size ;
746742
747- bitmap .bitmapHeader = WindowsBitmapHeader .readFrom (in );
743+ bitmap .bitmapHeader = WindowsBitmapHeader .readFrom (lin );
748744
749745 bitmap .bitmapHeader .height /= 2 ;
750746 bitmap .header .bitmapOffset = bitmap .bitmapHeader .headerSize + 4 * bitmap .bitmapHeader .usedColor ;
@@ -753,7 +749,7 @@ protected static WindowsBitmap readFrom(InputStream in, int off, int size) throw
753749//b.bitmapHeader.print();
754750//included mask
755751//Debug.println("read: " + (b.header.bitmapSize - b.header.bitmapOffset));
756- bitmap .bitmap = readBitmap (in , bitmap .header .bitmapSize - bitmap .header .bitmapOffset );
752+ bitmap .bitmap = readBitmap (lin , bitmap .header .bitmapSize - bitmap .header .bitmapOffset );
757753
758754 return bitmap ;
759755 }
0 commit comments