CSJ2K.j2k.codestream.reader.FileBitstreamReaderAgent.readPosCompResLy C# (CSharp) Method

readPosCompResLy() private method

Reads packets of the current tile according to the position-component-resolution-layer progressiveness.
private readPosCompResLy ( int lys, int lye, int ress, int rese, int comps, int compe ) : bool
lys int Index of the first layer for each component and resolution. /// ///
lye int Index of the last layer. /// ///
ress int Index of the first resolution level. /// ///
rese int Index of the last resolution level. /// ///
comps int Index of the first component. /// ///
compe int Index of the last component. /// ///
return bool
        private bool readPosCompResLy(int[][] lys, int lye, int ress, int rese, int comps, int compe)
        {
            Coord nTiles = getNumTiles(null);
            Coord tileI = getTile(null);
            int x0siz = hd.ImgULX;
            int y0siz = hd.ImgULY;
            int xsiz = x0siz + hd.ImgWidth;
            int ysiz = y0siz + hd.ImgHeight;
            int xt0siz = TilePartULX;
            int yt0siz = TilePartULY;
            int xtsiz = NomTileWidth;
            int ytsiz = NomTileHeight;
            int tx0 = (tileI.x == 0)?x0siz:xt0siz + tileI.x * xtsiz;
            int ty0 = (tileI.y == 0)?y0siz:yt0siz + tileI.y * ytsiz;
            int tx1 = (tileI.x != nTiles.x - 1)?xt0siz + (tileI.x + 1) * xtsiz:xsiz;
            int ty1 = (tileI.y != nTiles.y - 1)?yt0siz + (tileI.y + 1) * ytsiz:ysiz;

            // Get precinct information (number,distance between two consecutive
            // precincts in the reference grid) in each component and resolution
            // level
            int t = TileIdx; // Current tile index
            PrecInfo prec; // temporary variable
            int p; // Current precinct index
            int gcd_x = 0; // Horiz. distance between 2 precincts in the ref. grid
            int gcd_y = 0; // Vert. distance between 2 precincts in the ref. grid
            int nPrec = 0; // Total number of found precincts
            int[][] nextPrec = new int[compe][]; // Next precinct index in each
            // component and resolution level
            int minlys = 100000; // minimum layer start index of each component
            int minx = tx1; // Horiz. offset of the second precinct in the
            // reference grid
            int miny = ty1; // Vert. offset of the second precinct in the
            // reference grid.
            int maxx = tx0; // Max. horiz. offset of precincts in the ref. grid
            int maxy = ty0; // Max. vert. offset of precincts in the ref. grid
            //Coord numPrec;
            for (int c = comps; c < compe; c++)
            {
                // components
                for (int r = ress; r < rese; r++)
                {
                    // resolution levels
                    if (c >= mdl.Length)
                        continue;
                    if (r > mdl[c])
                        continue;
                    nextPrec[c] = new int[mdl[c] + 1];
                    if (lys[c] != null && r < lys[c].Length && lys[c][r] < minlys)
                    {
                        minlys = lys[c][r];
                    }
                    p = pktDec.getNumPrecinct(c, r) - 1;
                    for (; p >= 0; p--)
                    {
                        prec = pktDec.getPrecInfo(c, r, p);
                        if (prec.rgulx != tx0)
                        {
                            if (prec.rgulx < minx)
                                minx = prec.rgulx;
                            if (prec.rgulx > maxx)
                                maxx = prec.rgulx;
                        }
                        if (prec.rguly != ty0)
                        {
                            if (prec.rguly < miny)
                                miny = prec.rguly;
                            if (prec.rguly > maxy)
                                maxy = prec.rguly;
                        }

                        if (nPrec == 0)
                        {
                            gcd_x = prec.rgw;
                            gcd_y = prec.rgh;
                        }
                        else
                        {
                            gcd_x = MathUtil.gcd(gcd_x, prec.rgw);
                            gcd_y = MathUtil.gcd(gcd_y, prec.rgh);
                        }
                        nPrec++;
                    } // precincts
                } // resolution levels
            } // components

            if (nPrec == 0)
            {
                throw new System.InvalidOperationException("Image cannot have no precinct");
            }

            int pyend = (maxy - miny) / gcd_y + 1;
            int pxend = (maxx - minx) / gcd_x + 1;
            int hlen, plen;
            int start;
            bool status = false;
            int lastByte = firstPackOff[t][curTilePart] + tilePartLen[t][curTilePart] - 1 - tilePartHeadLen[t][curTilePart];
            int numLayers = ((System.Int32) decSpec.nls.getTileDef(t));
            System.String strInfo = "Tile " + TileIdx + " (tile-part:" + curTilePart + "): offset, length, header length\n"; ;
            bool pph = false;
            if (((System.Boolean) decSpec.pphs.getTileDef(t)))
            {
                pph = true;
            }

            int y = ty0;
            int x = tx0;
            for (int py = 0; py <= pyend; py++)
            {
                // Vertical precincts
                for (int px = 0; px <= pxend; px++)
                {
                    // Horiz. precincts
                    for (int c = comps; c < compe; c++)
                    {
                        // Components
                        if (c >= mdl.Length)
                            continue;
                        for (int r = ress; r < rese; r++)
                        {
                            // Resolution levels
                            if (r > mdl[c])
                                continue;
                            if (nextPrec[c][r] >= pktDec.getNumPrecinct(c, r))
                            {
                                continue;
                            }
                            prec = pktDec.getPrecInfo(c, r, nextPrec[c][r]);
                            if ((prec.rgulx != x) || (prec.rguly != y))
                            {
                                continue;
                            }
                            for (int l = minlys; l < lye; l++)
                            {
                                // Layers
                                if (r >= lys[c].Length)
                                    continue;
                                if (l < lys[c][r] || l >= numLayers)
                                    continue;

                                start = in_Renamed.Pos;

                                // If packed packet headers are used, there is no
                                // need to check that there are bytes enough to
                                // read header
                                if (pph)
                                {
                                    pktDec.readPktHead(l, r, c, nextPrec[c][r], cbI[c][r], nBytes);
                                }

                                // If we are about to read outside of tile-part,
                                // skip to next tile-part
                                if (start > lastByte && curTilePart < firstPackOff[t].Length - 1)
                                {
                                    curTilePart++;
                                    in_Renamed.seek(firstPackOff[t][curTilePart]);
                                    lastByte = in_Renamed.Pos + tilePartLen[t][curTilePart] - 1 - tilePartHeadLen[t][curTilePart];
                                }

                                // Read SOP marker segment if necessary
                                status = pktDec.readSOPMarker(nBytes, nextPrec[c][r], c, r);

                                if (status)
                                {
                                    if (printInfo)
                                    {
                                        FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
                                    }
                                    return true;
                                }

                                if (!pph)
                                {
                                    status = pktDec.readPktHead(l, r, c, nextPrec[c][r], cbI[c][r], nBytes);
                                }

                                if (status)
                                {
                                    if (printInfo)
                                    {
                                        FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
                                    }
                                    return true;
                                }

                                // Store packet's head length
                                hlen = in_Renamed.Pos - start;
                                pktHL.Add((System.Int32) hlen);

                                // Reads packet's body
                                status = pktDec.readPktBody(l, r, c, nextPrec[c][r], cbI[c][r], nBytes);
                                plen = in_Renamed.Pos - start;
                                strInfo += (" Pkt l=" + l + ",r=" + r + ",c=" + c + ",p=" + nextPrec[c][r] + ": " + start + ", " + plen + ", " + hlen + "\n");

                                if (status)
                                {
                                    if (printInfo)
                                    {
                                        FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
                                    }
                                    return true;
                                }
                            } // layers
                            nextPrec[c][r]++;
                        } // Resolution levels
                    } // Components
                    if (px != pxend)
                    {
                        x = minx + px * gcd_x;
                    }
                    else
                    {
                        x = tx0;
                    }
                } // Horizontal precincts
                if (py != pyend)
                {
                    y = miny + py * gcd_y;
                }
                else
                {
                    y = ty0;
                }
            } // Vertical precincts

            if (printInfo)
            {
                FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.INFO, strInfo);
            }
            return false; // Decoding rate was not reached
        }