private void HandleReferedToSegmentNumbers(SegmentHeader segmentHeader)
        {
            int referredToSegmentCount = segmentHeader.GetReferredToSegmentCount();

            int[] referredToSegments = new int[referredToSegmentCount];

            int segmentNumber = segmentHeader.GetSegmentNumber();

            if (segmentNumber <= 256)
            {
                for (int i = 0; i < referredToSegmentCount; i++)
                {
                    referredToSegments[i] = reader.Readbyte();
                }
            }
            else if (segmentNumber <= 65536)
            {
                short[] buf = new short[2];
                for (int i = 0; i < referredToSegmentCount; i++)
                {
                    reader.Readbyte(buf);
                    referredToSegments[i] = BinaryOperation.GetInt16(buf);
                }
            }
            else
            {
                short[] buf = new short[4];
                for (int i = 0; i < referredToSegmentCount; i++)
                {
                    reader.Readbyte(buf);
                    referredToSegments[i] = BinaryOperation.GetInt32(buf);
                }
            }

            segmentHeader.SetReferredToSegments(referredToSegments);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("referredToSegments = ");
                for (int i = 0; i < referredToSegments.Length; i++)
                {
                    Console.WriteLine(referredToSegments[i] + " ");
                }
                Console.WriteLine("");
            }
        }
        private void ReadSegments()
        {
            bool finished = false;

            while (!reader.IsFinished() && !finished)
            {
                SegmentHeader segmentHeader = new SegmentHeader();
                ReadSegmentHeader(segmentHeader);

                // read the Segment data
                Segment segment = null;

                int   segmentType            = segmentHeader.GetSegmentType();
                int[] referredToSegments     = segmentHeader.GetReferredToSegments();
                int   noOfReferredToSegments = segmentHeader.GetReferredToSegmentCount();

                switch (segmentType)
                {
                case Segment.SYMBOL_DICTIONARY:

                    segment = new SymbolDictionarySegment(this);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_TEXT_REGION:

                    segment = new TextRegionSegment(this, false);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_TEXT_REGION:

                    segment = new TextRegionSegment(this, true);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_TEXT_REGION:

                    segment = new TextRegionSegment(this, true);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.PATTERN_DICTIONARY:

                    segment = new PatternDictionarySegment(this);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_HALFTONE_REGION:

                    segment = new HalftoneRegionSegment(this, false);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_HALFTONE_REGION:

                    segment = new HalftoneRegionSegment(this, true);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_HALFTONE_REGION:

                    segment = new HalftoneRegionSegment(this, true);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_GENERIC_REGION:

                    segment = new GenericRegionSegment(this, false);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_GENERIC_REGION:

                    segment = new GenericRegionSegment(this, true);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_GENERIC_REGION:

                    segment = new GenericRegionSegment(this, true);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_GENERIC_REFINEMENT_REGION:

                    segment = new RefinementRegionSegment(this, false, referredToSegments, noOfReferredToSegments);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_GENERIC_REFINEMENT_REGION:

                    segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_GENERIC_REFINEMENT_REGION:

                    segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.PAGE_INFORMATION:

                    segment = new PageInformationSegment(this);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                case Segment.END_OF_PAGE:
                    continue;

                case Segment.END_OF_STRIPE:

                    segment = new EndOfStripeSegment(this);

                    segment.SetSegmentHeader(segmentHeader);
                    break;

                case Segment.END_OF_FILE:

                    finished = true;

                    continue;

                case Segment.PROFILES:
                    break;

                case Segment.TABLES:
                    break;

                case Segment.EXTENSION:

                    segment = new ExtensionSegment(this);

                    segment.SetSegmentHeader(segmentHeader);

                    break;

                default:
                    break;
                }

                if (!randomAccessOrganisation)
                {
                    segment.ReadSegment();
                }
                segments.Add(segment);
            }

            if (randomAccessOrganisation)
            {
                foreach (Segment segment in segments)
                {
                    segment.ReadSegment();
                }
            }
        }