NextPowerOfTwo() public static method

public static NextPowerOfTwo ( int value ) : int
value int
return int
Esempio n. 1
0
 public BinarySearch(int min, int max, int fuzziness, bool pot)
 {
     this._pot       = pot;
     this._fuzziness = pot ? 0 : fuzziness;
     this._min       = pot ? (int)(Math.Log(MathUtils.NextPowerOfTwo(min)) / Math.Log(2)) : min;
     this._max       = pot ? (int)(Math.Log(MathUtils.NextPowerOfTwo(max)) / Math.Log(2)) : max;
 }
Esempio n. 2
0
        public TexturePacker(string rootDir, Settings settings)
        {
            _settings = settings;

            if (_settings.PowerOfTwo)
            {
                if (_settings.MaxWidth != MathUtils.NextPowerOfTwo(_settings.MaxWidth))
                {
                    throw new Exception("If PowerOfTwo is true, MaxWidth must be a power of two: " + _settings.MaxWidth);
                }
                if (_settings.MaxHeight != MathUtils.NextPowerOfTwo(_settings.MaxHeight))
                {
                    throw new Exception("If PowerOfTwo is true, MaxHeight must be a power of two: " + _settings.MaxHeight);
                }
            }

            _maxRectsPacker = new MaxRectsPacker(settings);
            _imageProcessor = new ImageProcessor(rootDir, settings);
        }
Esempio n. 3
0
        private void WriteImages(string outputDir, List <Page> pages, string packFileName)
        {
            string imageName = Path.GetFileNameWithoutExtension(packFileName);

            int fileIndex = 0;

            foreach (Page page in pages)
            {
                int width    = page.Width;
                int height   = page.Height;
                int paddingX = _settings.PaddingX;
                int paddingY = _settings.PaddingY;

                if (_settings.DuplicatePadding)
                {
                    paddingX /= 2;
                    paddingY /= 2;
                }

                width  -= _settings.PaddingX;
                height -= _settings.PaddingY;

                if (_settings.EdgePadding)
                {
                    page.X  = paddingX;
                    page.Y  = paddingY;
                    width  += paddingX * 2;
                    height += paddingY * 2;
                }

                if (_settings.PowerOfTwo)
                {
                    width  = MathUtils.NextPowerOfTwo(width);
                    height = MathUtils.NextPowerOfTwo(height);
                }

                width  = Math.Max(_settings.MinWidth, width);
                height = Math.Max(_settings.MinHeight, height);

                if (_settings.ForceSquareOutput)
                {
                    if (width > height)
                    {
                        height = width;
                    }
                    else
                    {
                        width = height;
                    }
                }

                string outputFile;
                while (true)
                {
                    outputFile = Path.Combine(outputDir, imageName + (fileIndex++ == 0 ? "" : fileIndex.ToString()) + "." + _settings.OutputFormat);
                    if (!File.Exists(outputFile))
                    {
                        break;
                    }
                }

                page.ImageName = Path.GetFileName(outputFile);

                using (Bitmap canvas = new Bitmap(width, height, _settings.Format)) {
                    foreach (Rect rect in page.OutputRects)
                    {
                        Bitmap image = rect.Image;
                        int    iw    = image.Width;
                        int    ih    = image.Height;
                        int    rectX = page.X + rect.X;
                        int    rectY = page.Y + page.Height - rect.Y - rect.Height;

                        if (_settings.DuplicatePadding)
                        {
                            int amountX = _settings.PaddingX / 2;
                            int amountY = _settings.PaddingY / 2;

                            if (rect.Rotated)
                            {
                                // Copy corner pixels into corners of padding
                                for (int i = 1; i <= amountX; i++)
                                {
                                    for (int j = 1; j <= amountY; j++)
                                    {
                                        canvas.SetPixel(rectX - j, rectY + iw - 1 + i, image.GetPixel(0, 0));
                                        canvas.SetPixel(rectX + ih - 1 + j, rectY + iw - 1 + i, image.GetPixel(0, ih - 1));
                                        canvas.SetPixel(rectX - j, rectY - i, image.GetPixel(iw - 1, 0));
                                        canvas.SetPixel(rectX + ih - 1 + j, rectY - i, image.GetPixel(iw - 1, ih - 1));
                                    }
                                }

                                // Copy edge pixels into padding
                                for (int i = 1; i <= amountY; i++)
                                {
                                    for (int j = 0; j < iw; j++)
                                    {
                                        canvas.SetPixel(rectX - i, rectY + iw - 1 - j, image.GetPixel(j, 0));
                                        canvas.SetPixel(rectX + ih - 1 + i, rectY + iw - 1 - j, image.GetPixel(j, ih - 1));
                                    }
                                }

                                for (int i = 1; i <= amountX; i++)
                                {
                                    for (int j = 0; j < ih; j++)
                                    {
                                        canvas.SetPixel(rectX + j, rectY - i, image.GetPixel(iw - 1, j));
                                        canvas.SetPixel(rectX + j, rectY + iw - 1 + i, image.GetPixel(0, j));
                                    }
                                }
                            }
                            else
                            {
                                // Copy corner pixels into corners of padding
                                for (int i = 1; i <= amountX; i++)
                                {
                                    for (int j = 1; j <= amountY; j++)
                                    {
                                        canvas.SetPixel(rectX - i, rectY - j, image.GetPixel(0, 0));
                                        canvas.SetPixel(rectX - i, rectY + ih - 1 + j, image.GetPixel(0, ih - 1));
                                        canvas.SetPixel(rectX + iw - 1 + i, rectY - j, image.GetPixel(iw - 1, 0));
                                        canvas.SetPixel(rectX + iw - 1 + i, rectY + ih - 1 + j, image.GetPixel(iw - 1, ih - 1));
                                    }
                                }

                                // Copy edge pixels into padding
                                for (int i = 1; i <= amountY; i++)
                                {
                                    for (int j = 0; j < iw; j++)
                                    {
                                        canvas.SetPixel(rectX + j, rectY - i, image.GetPixel(j, 0));
                                        canvas.SetPixel(rectX + j, rectY + ih - 1 + i, image.GetPixel(j, ih - 1));
                                    }
                                }

                                for (int i = 1; i <= amountX; i++)
                                {
                                    for (int j = 0; j < ih; j++)
                                    {
                                        canvas.SetPixel(rectX - i, rectY + j, image.GetPixel(0, j));
                                        canvas.SetPixel(rectX + iw - 1 + i, rectY + j, image.GetPixel(iw - 1, j));
                                    }
                                }
                            }
                        }

                        // Copy image
                        if (rect.Rotated)
                        {
                            for (int i = 0; i < iw; i++)
                            {
                                for (int j = 0; j < ih; j++)
                                {
                                    canvas.SetPixel(rectX + j, rectY + iw - i - 1, image.GetPixel(i, j));
                                }
                            }
                        }
                        else
                        {
                            for (int i = 0; i < iw; i++)
                            {
                                for (int j = 0; j < ih; j++)
                                {
                                    canvas.SetPixel(rectX + i, rectY + j, image.GetPixel(i, j));
                                }
                            }
                        }
                    }

                    using (FileStream fstr = File.OpenWrite(outputFile)) {
                        try {
                            if (string.Compare(_settings.OutputFormat, "jpg", true) == 0)
                            {
                                ImageCodecInfo     codec       = GetEncoderInfo("image/jpeg");
                                EncoderParameter[] codecParams = new EncoderParameter[] {
                                    new EncoderParameter(Encoder.Quality, _settings.JpegQuality),
                                };

                                canvas.Save(fstr, codec, new EncoderParameters()
                                {
                                    Param = codecParams
                                });
                            }
                            else
                            {
                                if (_settings.PremultiplyAlpha)
                                {
                                    using (Bitmap pmaCanvas = PremultiplyAlpha(canvas)) {
                                        pmaCanvas.Save(fstr, ImageFormat.Png);
                                    }
                                }
                                else
                                {
                                    canvas.Save(fstr, ImageFormat.Png);
                                }
                            }
                        }
                        catch (Exception e) {
                            throw new Exception("Error writing file: " + outputFile, e);
                        }
                    }
                }
            }
        }