public void PackBatch(AtlasImageBatch batch)
 {
     PackBatchInternal(batch);
     if (batch.SubBatches != null)
     {
         foreach (var subBatch in batch.SubBatches)
         {
             PackBatchInternal(subBatch);
         }
     }
 }
        private AtlasImageBatch CreateBatch(AtlasRootDescription desc, DirectoryInfo origin, DirectoryInfo dir)
        {
            var batch = new AtlasImageBatch(desc, dir);

            foreach (var file in dir.GetFiles("*", SearchOption.TopDirectoryOnly))
            {
                string relativeFilePath = PathHelper.GetNormalizedPath(PathHelper.GetRelativePath(origin, file));
                if (_exclusionSet.Contains(relativeFilePath))
                {
                    continue;
                }

                var imgInfo = Image.Identify(_config, file.OpenRead());
                if (imgInfo != null)
                {
                    string relativeImgPath = PathHelper.GetNormalizedPath(PathHelper.GetRelativePath(_directory, file));
                    relativeImgPath = Path.ChangeExtension(relativeImgPath, null);

                    if (!desc.IncludeRootName)
                    {
                        int firstSeparator = relativeImgPath.IndexOf('/');
                        if (firstSeparator != -1)
                        {
                            relativeImgPath = relativeImgPath.Substring(firstSeparator + 1);
                        }
                    }
                    batch.Images.Add(new AtlasImage(file, relativeImgPath, imgInfo.Width, imgInfo.Height));
                }
            }

            foreach (var subDir in dir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
            {
                string relativeSubDirPath = PathHelper.GetNormalizedPath(PathHelper.GetRelativePath(origin, subDir));
                if (_exclusionSet.Contains(relativeSubDirPath))
                {
                    continue;
                }

                var subBatch = CreateBatch(desc, origin, subDir);
                batch.SubBatches.Add(subBatch);
            }
            return(batch);
        }
        private void PackBatchInternal(AtlasImageBatch batch)
        {
            foreach (var img in batch.Images)
            {
                if (img.Width > MaxSize || img.Height > MaxSize)
                {
                    _singles.Add(img);
                    continue;
                }

                int index = 0;

TryInsert:
                AtlasPackerState state = _states[index];
                if (!state.Insert(img, out Rect rect))
                {
                    if (!state.Step())
                    {
                        index++;
                        while (index < _states.Count && _states[index].IsSingle)
                        {
                            index++;
                        }

                        if (index == _states.Count)
                        {
                            _states.Add(new AtlasPackerState(MaxSize, Spacing, false));
                        }
                    }

                    goto TryInsert;
                }

                state.Items.Add(new AtlasPackerState.Item(rect, img));
            }
        }