/// <summary>
 /// ReadRectangle reads a structure designed to hold a rectangle for a Wang Annotation.
 /// </summary>
 /// <remarks>
 /// This methods aplies when the specifications are mentionning the availability of a RECT structure.
 /// </remarks>
 /// <param name="coordinates">An array to retrieve the coordinates of the rectangle.</param>
 /// <param name="stream">The stream to read the data in.</param>
 /// <returns>true if success otherwise false.</returns>
 public static bool ReadRectangle(int[] coordinates, IWangStream stream)
 {
     if (stream.AvailableBytes() >= 4 * 4)
     {
         // The order is left, top, right, bottom.
         stream.ReadInts32(coordinates, 4);
         return(true);
     }
     return(false);
 }
        /// <summary>
        /// ReadPoints reads a structure designed to hold points.
        /// </summary>
        /// <remarks>
        /// This methods aplies when the specifications are mentionning the availability of a AN_POINTS structure.
        /// </remarks>
        /// <param name="stream">The stream to read the data in.</param>
        /// <param name="dataSize">The size of the data to read.</param>
        /// <returns>The data read, null if an error occured.</returns>
        public static int[] ReadPoints(IWangStream stream, int dataSize)
        {
            /*
             * This implementation is based on the following C++ typedef:
             * typedef struct tagAnPoints{
             *   int nMaxPoints;                // The maximum number of points; must
             *                                  // be equal to the value of nPoints.
             *   int nPoints;                   // The current number of points.
             *   POINT ptPoint[1];              // Points marking the beginning and
             *                                  // ending of the line segment(s); in
             *                                  // FULLSIZE (not scaled) coordinates
             *                                  // relative to the upper left corner
             *                                  // of lrBounds in
             *                                  // OIAN_MARK_ATTRIBUTES.
             *   } AN_POINTS;
             */

            // We need at least two integers.
            if (dataSize < 8)
            {
                return(null);
            }

            int max   = stream.ReadInt32();
            int count = stream.ReadInt32();

            // Although the available spec explains the opposite, max and count may be different
            if (max < count)
            {
                return(null);
            }

            // The size of the data is exactly the size of two integers
            // plus the size for the maximum number of points.
            if ((8 + max * 2 * 4) != dataSize)
            {
                return(null);
            }

            int coordinatesCount = 2 * count;

            int[] readData = new int[coordinatesCount];
            stream.ReadInts32(readData, coordinatesCount);

            // Although the available spec explains the opposite, max and count may be different
            // and we have to skip the unused data.
            // 2 coordinates, 4 bytes per coordinate
            stream.SkipBytes((max - count) * 2 * 4);

            return(readData);
        }