@@ -370,103 +370,105 @@ public static byte[] compressT4_2D(final byte[] uncompressed, final int width,
370370 public static byte [] decompressT4_2D (final byte [] compressed , final int width ,
371371 final int height , final boolean hasFill ) throws ImageReadException {
372372 final BitInputStreamFlexible inputStream = new BitInputStreamFlexible (new ByteArrayInputStream (compressed ));
373- final BitArrayOutputStream outputStream = new BitArrayOutputStream ();
374- final int [] referenceLine = new int [width ];
375- for (int y = 0 ; y < height ; y ++) {
376- int rowLength = 0 ;
377- try {
378- T4_T6_Tables .Entry entry = CONTROL_CODES .decode (inputStream );
379- if (!isEOL (entry , hasFill )) {
380- throw new ImageReadException ("Expected EOL not found" );
381- }
382- final int tagBit = inputStream .readBits (1 );
383- if (tagBit == 0 ) {
384- // 2D
385- int codingA0Color = WHITE ;
386- int referenceA0Color = WHITE ;
387- int b1 = nextChangingElement (referenceLine , referenceA0Color , 0 );
388- int b2 = nextChangingElement (referenceLine , 1 - referenceA0Color , b1 + 1 );
389- for (int a0 = 0 ; a0 < width ;) {
390- int a1 ;
391- int a2 ;
392- entry = CONTROL_CODES .decode (inputStream );
393- if (entry == T4_T6_Tables .P ) {
394- fillRange (outputStream , referenceLine , a0 , b2 , codingA0Color );
395- a0 = b2 ;
396- } else if (entry == T4_T6_Tables .H ) {
397- final int a0a1 = readTotalRunLength (inputStream , codingA0Color );
398- a1 = a0 + a0a1 ;
399- fillRange (outputStream , referenceLine , a0 , a1 , codingA0Color );
400- final int a1a2 = readTotalRunLength (inputStream , 1 - codingA0Color );
401- a2 = a1 + a1a2 ;
402- fillRange (outputStream , referenceLine , a1 , a2 , 1 - codingA0Color );
403- a0 = a2 ;
404- } else {
405- int a1b1 ;
406- if (entry == T4_T6_Tables .V0 ) {
407- a1b1 = 0 ;
408- } else if (entry == T4_T6_Tables .VL1 ) {
409- a1b1 = -1 ;
410- } else if (entry == T4_T6_Tables .VL2 ) {
411- a1b1 = -2 ;
412- } else if (entry == T4_T6_Tables .VL3 ) {
413- a1b1 = -3 ;
414- } else if (entry == T4_T6_Tables .VR1 ) {
415- a1b1 = 1 ;
416- } else if (entry == T4_T6_Tables .VR2 ) {
417- a1b1 = 2 ;
418- } else if (entry == T4_T6_Tables .VR3 ) {
419- a1b1 = 3 ;
373+ try (final BitArrayOutputStream outputStream = new BitArrayOutputStream ()) {
374+ final int [] referenceLine = new int [width ];
375+ for (int y = 0 ; y < height ; y ++) {
376+ int rowLength = 0 ;
377+ try {
378+ T4_T6_Tables .Entry entry = CONTROL_CODES .decode (inputStream );
379+ if (!isEOL (entry , hasFill )) {
380+ throw new ImageReadException ("Expected EOL not found" );
381+ }
382+ final int tagBit = inputStream .readBits (1 );
383+ if (tagBit == 0 ) {
384+ // 2D
385+ int codingA0Color = WHITE ;
386+ int referenceA0Color = WHITE ;
387+ int b1 = nextChangingElement (referenceLine , referenceA0Color , 0 );
388+ int b2 = nextChangingElement (referenceLine , 1 - referenceA0Color , b1 + 1 );
389+ for (int a0 = 0 ; a0 < width ;) {
390+ int a1 ;
391+ int a2 ;
392+ entry = CONTROL_CODES .decode (inputStream );
393+ if (entry == T4_T6_Tables .P ) {
394+ fillRange (outputStream , referenceLine , a0 , b2 , codingA0Color );
395+ a0 = b2 ;
396+ } else if (entry == T4_T6_Tables .H ) {
397+ final int a0a1 = readTotalRunLength (inputStream , codingA0Color );
398+ a1 = a0 + a0a1 ;
399+ fillRange (outputStream , referenceLine , a0 , a1 , codingA0Color );
400+ final int a1a2 = readTotalRunLength (inputStream , 1 - codingA0Color );
401+ a2 = a1 + a1a2 ;
402+ fillRange (outputStream , referenceLine , a1 , a2 , 1 - codingA0Color );
403+ a0 = a2 ;
420404 } else {
421- throw new ImageReadException ("Invalid/unknown T.4 control code " + entry .bitString );
405+ int a1b1 ;
406+ if (entry == T4_T6_Tables .V0 ) {
407+ a1b1 = 0 ;
408+ } else if (entry == T4_T6_Tables .VL1 ) {
409+ a1b1 = -1 ;
410+ } else if (entry == T4_T6_Tables .VL2 ) {
411+ a1b1 = -2 ;
412+ } else if (entry == T4_T6_Tables .VL3 ) {
413+ a1b1 = -3 ;
414+ } else if (entry == T4_T6_Tables .VR1 ) {
415+ a1b1 = 1 ;
416+ } else if (entry == T4_T6_Tables .VR2 ) {
417+ a1b1 = 2 ;
418+ } else if (entry == T4_T6_Tables .VR3 ) {
419+ a1b1 = 3 ;
420+ } else {
421+ throw new ImageReadException ("Invalid/unknown T.4 control code " + entry .bitString );
422+ }
423+ a1 = b1 + a1b1 ;
424+ fillRange (outputStream , referenceLine , a0 , a1 , codingA0Color );
425+ a0 = a1 ;
426+ codingA0Color = 1 - codingA0Color ;
422427 }
423- a1 = b1 + a1b1 ;
424- fillRange (outputStream , referenceLine , a0 , a1 , codingA0Color );
425- a0 = a1 ;
426- codingA0Color = 1 - codingA0Color ;
427- }
428- referenceA0Color = changingElementAt (referenceLine , a0 );
429- if (codingA0Color == referenceA0Color ) {
430- b1 = nextChangingElement (referenceLine , referenceA0Color , a0 + 1 );
431- } else {
432- b1 = nextChangingElement (referenceLine , referenceA0Color , a0 + 1 );
433- b1 = nextChangingElement (referenceLine , 1 - referenceA0Color , b1 + 1 );
428+ referenceA0Color = changingElementAt (referenceLine , a0 );
429+ if (codingA0Color == referenceA0Color ) {
430+ b1 = nextChangingElement (referenceLine , referenceA0Color , a0 + 1 );
431+ } else {
432+ b1 = nextChangingElement (referenceLine , referenceA0Color , a0 + 1 );
433+ b1 = nextChangingElement (referenceLine , 1 - referenceA0Color , b1 + 1 );
434+ }
435+ b2 = nextChangingElement (referenceLine , 1 - codingA0Color , b1 + 1 );
436+ rowLength = a0 ;
434437 }
435- b2 = nextChangingElement ( referenceLine , 1 - codingA0Color , b1 + 1 );
436- rowLength = a0 ;
437- }
438- } else {
439- // 1D
440- int color = WHITE ;
441- for ( rowLength = 0 ; rowLength < width ;) {
442- final int runLength = readTotalRunLength ( inputStream , color ) ;
443- for ( int i = 0 ; i < runLength ; i ++) {
444- outputStream . writeBit ( color ) ;
445- referenceLine [ rowLength + i ] = color ;
438+ } else {
439+ // 1D
440+ int color = WHITE ;
441+ for ( rowLength = 0 ; rowLength < width ;) {
442+ final int runLength = readTotalRunLength ( inputStream , color );
443+ for ( int i = 0 ; i < runLength ; i ++) {
444+ outputStream . writeBit ( color );
445+ referenceLine [ rowLength + i ] = color ;
446+ }
447+ color = 1 - color ;
448+ rowLength += runLength ;
446449 }
447- color = 1 - color ;
448- rowLength += runLength ;
449450 }
451+ } catch (final IOException ioException ) {
452+ throw new ImageReadException ("Decompression error" , ioException );
453+ } catch (final HuffmanTreeException huffmanException ) {
454+ throw new ImageReadException ("Decompression error" , huffmanException );
450455 }
451- } catch (final IOException ioException ) {
452- throw new ImageReadException ("Decompression error" , ioException );
453- } catch (final HuffmanTreeException huffmanException ) {
454- throw new ImageReadException ("Decompression error" , huffmanException );
455- }
456456
457- if (rowLength == width ) {
458- outputStream .flush ();
459- } else if (rowLength > width ) {
460- throw new ImageReadException ("Unrecoverable row length error in image row " + y );
457+ if (rowLength == width ) {
458+ outputStream .flush ();
459+ } else if (rowLength > width ) {
460+ throw new ImageReadException ("Unrecoverable row length error in image row " + y );
461+ }
461462 }
462- }
463463
464- return outputStream .toByteArray ();
464+ return outputStream .toByteArray ();
465+ }
465466 }
466467
467468 public static byte [] compressT6 (final byte [] uncompressed , final int width , final int height )
468469 throws ImageWriteException {
469- try (BitInputStreamFlexible inputStream = new BitInputStreamFlexible (new ByteArrayInputStream (uncompressed ))) {
470+ try (final ByteArrayInputStream bais = new ByteArrayInputStream (uncompressed );
471+ BitInputStreamFlexible inputStream = new BitInputStreamFlexible (bais )) {
470472 final BitArrayOutputStream outputStream = new BitArrayOutputStream ();
471473 int [] referenceLine = new int [width ];
472474 int [] codingLine = new int [width ];
0 commit comments