Ejemplo n.º 1
0
        /// <summary>Fill the subset.</summary>
        /// <param name="data">The memory-resident data image.
        /// This may be null if the image is to be read from a file.  This should
        /// be a multi-dimensional primitive array.</param>
        /// <param name="o">The tile to be filled.  This is a simple primitive array.</param>
        /// <param name="dims">The dimensions of the full image.</param>
        /// <param name="corners">The indices of the corner of the image.</param>
        /// <param name="lengths">The dimensions of the subset.</param>
        protected internal virtual void FillTile(Array data, Array o, int[] dims, int[] corners, int[] lengths)
        {
            int n = dims.Length;

            int[] posits     = new int[n];
            int   baseLength = ArrayFuncs.GetBaseLength(o);
            int   segment    = lengths[n - 1];

            Array.Copy(corners, 0, posits, 0, n);
            long currentOffset = 0;

            if (data == null)
            {
                //currentOffset = f.FilePointer;

                currentOffset = f.Position;
            }

            int outputOffset = 0;

            if (data != null && data.GetType().Equals(typeof(Array)) && !ArrayFuncs.IsArrayOfArrays(data))
            {
                int[] index = new int[posits.Length];
                Array.Copy(posits, 0, index, 0, n);
                index[index.Length - 1] -= 1;
                for (int i = outputOffset; ArrayFuncs.NextIndex(index, posits, lengths); ++i)
                {
                    o.SetValue(data.GetValue(index), i);
                }

                return;
            }

            do
            {
                // This implies there is some overlap in the last index
                // (in conjunction with other tests)
                int  mx           = dims.Length - 1;
                bool validSegment = posits[mx] + lengths[mx] >= 0 && posits[mx] < dims[mx];


                // Don't do anything for the current segment if anything but the
                // last index is out of range.
                if (validSegment)
                {
                    for (int i = 0; i < mx; i += 1)
                    {
                        if (posits[i] < 0 || posits[i] >= dims[i])
                        {
                            validSegment = false;
                            break;
                        }
                    }
                }

                if (validSegment)
                {
                    if (data != null)
                    {
                        FillMemData(data, posits, segment, o, outputOffset, 0);
                    }
                    else
                    {
                        int offset = GetOffset(dims, posits) * baseLength;

                        // Point to offset at real beginning
                        // of segment
                        int actualLen    = segment;
                        int actualOffset = offset;
                        int actualOutput = outputOffset;
                        if (posits[mx] < 0)
                        {
                            actualOffset -= posits[mx] * baseLength;
                            actualOutput -= posits[mx];
                            actualLen    += posits[mx];
                        }
                        if (posits[mx] + segment > dims[mx])
                        {
                            actualLen -= posits[mx] + segment - dims[mx];
                        }
                        FillFileData(o, actualOffset, actualOutput, actualLen);
                    }
                }
                outputOffset += segment;
            }while(IncrementPosition(corners, posits, lengths));
            if (data == null)
            {
                f.Seek(currentOffset, SeekOrigin.Begin);
            }
        }