private static HgxImage ExtractHg2Internal(HGXHDR hdr, BinaryReader reader, string fileName, string outputDir,
                                                   HgxOptions options)
        {
            Stream stream = reader.BaseStream;

            List <Hg2FrameInfo> frameInfos = new List <Hg2FrameInfo>();
            Hg2FrameInfo        frameInfo  = null;
            long startPosition             = stream.Position;

            do
            {
                stream.Position = startPosition + (frameInfo?.Img.OffsetNext ?? 0);
                startPosition   = stream.Position;

                frameInfo = ReadHg2FrameInfo(reader, hdr, false);
                frameInfos.Add(frameInfo);
            } while (frameInfo.Img.OffsetNext != 0);

            HgxImage hg2Image = new HgxImage(Path.GetFileName(fileName), hdr, frameInfos.ToArray(), options);

            if (outputDir != null)
            {
                for (int imgIndex = 0; imgIndex < frameInfos.Count; imgIndex++)
                {
                    frameInfo = frameInfos[imgIndex];
                    string pngFile = hg2Image.Frames[imgIndex].GetFrameFilePath(outputDir, false);
                    ExtractHg2Image(reader, frameInfo, options, pngFile);
                }
            }

            return(hg2Image);
        }
 /// <summary>
 ///  Extracts the HG-X image information ONLY from open KIFINT archive stream and does not extract the actual
 ///  images.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <returns>The extracted <see cref="HgxImage"/> information.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/> or <paramref name="kifintStream"/> is null.
 /// </exception>
 public static HgxImage ExtractHgx(this KifintEntry entry, KifintStream kifintStream)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(HgxImage.Extract(stream, entry.FileName));
 }
 /// <summary>
 ///  Extracts the HG-X image information from the KIFINT entry's open KIFINT archive stream and saves all
 ///  images to the output <paramref name="outputDir"/>.
 /// </summary>
 /// <param name="entry">The entry to extract from.</param>
 /// <param name="kifintStream">The stream to the open KIFINT archive.</param>
 /// <param name="outputDir">The output directory to save the images to.</param>
 /// <param name="options">The options for manipulating the image during extraction.</param>
 /// <returns>The extracted <see cref="HgxImage"/> information.</returns>
 ///
 /// <exception cref="ArgumentNullException">
 ///  <paramref name="entry"/>, <paramref name="kifintStream"/>, or <paramref name="outputDir"/> is null.
 /// </exception>
 public static HgxImage ExtractHgxAndImages(this KifintEntry entry, KifintStream kifintStream, string outputDir, HgxOptions options)
 {
     if (entry == null)
     {
         throw new ArgumentNullException(nameof(entry));
     }
     using (var stream = entry.ExtractToStream(kifintStream))
         return(HgxImage.ExtractImages(stream, entry.FileName, outputDir, options));
 }
        private static HgxImage ExtractHg3Internal(HGXHDR hdr, BinaryReader reader, string fileName, string outputDir,
                                                   HgxOptions options)
        {
            Stream stream = reader.BaseStream;

            List <Hg3FrameInfo> frameInfos = new List <Hg3FrameInfo>();
            Hg3FrameInfo        frameInfo  = null;
            long startPosition             = stream.Position;

            do
            {
                stream.Position = startPosition + (frameInfo?.Header.OffsetNext ?? 0);

                // NEW-NEW METHOD: We now know the next offset ahead
                // of time from the HG3OFFSET we're going to read.
                // Usually skips 0 bytes, otherwise usually 1-7 bytes.
                startPosition = stream.Position;

                frameInfo = ReadHg3FrameInfo(reader, hdr, false);
                frameInfos.Add(frameInfo);
            } while (frameInfo.Header.OffsetNext != 0);

            HgxImage hg3Image = new HgxImage(Path.GetFileName(fileName), hdr, frameInfos.ToArray(), options);

            if (outputDir != null)
            {
                for (int imgIndex = 0; imgIndex < frameInfos.Count; imgIndex++)
                {
                    frameInfo = frameInfos[imgIndex];
                    string pngFile = hg3Image.Frames[imgIndex].GetFrameFilePath(outputDir, false);
                    ExtractHg3Image(reader, frameInfo, options, pngFile);
                }
            }

            return(hg3Image);
        }