// Attempt to allocate a slot, returns false if it could not allocate, which is very bad private bool allocateSlot(StitchHolder holder) { // Try to insert it into all current slots with both rotations foreach (StitchSlot slot in _slotList) { if (slot.AddSlot(holder)) return true; holder.Rotate(); if (slot.AddSlot(holder)) return true; holder.Rotate(); } // Make it bigger, and try inserting it in the new space return expandAndAllocateSlot(holder); }
// Try to make the resulting image bigger, and add the slot, returns false if it could not allocate private bool expandAndAllocateSlot(StitchHolder holder) { int i = Math.Min(holder.GetWidth(), holder.GetHeight()); bool flag = (CurrentWidth == 0 && CurrentHeight == 0); bool mw = CurrentWidth + i <= MAX_DIMENSIONS; bool mh = CurrentHeight + i <= MAX_DIMENSIONS; // If we are out of space if (!mw && !mh) return false; bool flag1 = mw && (flag || (CurrentWidth <= CurrentHeight)); int j = Math.Max(holder.GetWidth(), holder.GetHeight()); // Expanding it would run out of space if (MathUtil.NextPowerOf2((flag1 ? CurrentWidth : CurrentHeight) + j) > MAX_DIMENSIONS) return false; else { StitchSlot slot; if (flag1) { if (holder.GetWidth() > holder.GetHeight()) holder.Rotate(); if (CurrentHeight == 0) CurrentHeight = holder.GetHeight(); slot = new StitchSlot(CurrentWidth, 0, holder.GetWidth(), CurrentHeight); CurrentWidth += holder.GetWidth(); } else { slot = new StitchSlot(0, CurrentHeight, CurrentWidth, holder.GetHeight()); CurrentHeight += holder.GetHeight(); } if (!slot.AddSlot(holder)) return false; _slotList.Add(slot); return true; } }