Beispiel #1
0
        /// <summary>
        /// Swaps two sub-images.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="libraryData">The library data.</param>
        /// <param name="swap">The swap request.</param>
        private void Swap(TexImage image, FreeImageTextureLibraryData libraryData, SwappingRequest swap)
        {
            Log.Info("Swapping image : sub-image " + swap.FirstSubImageIndex + " and " + swap.SecondSubImageIndex + " ...");

            if (swap.FirstSubImageIndex >= 0 && swap.FirstSubImageIndex < libraryData.Bitmaps.Length &&
                swap.SecondSubImageIndex >= 0 && swap.SecondSubImageIndex < libraryData.Bitmaps.Length)
            {
                // copy first image
                var firstImage = FreeImage.Copy(libraryData.Bitmaps[swap.FirstSubImageIndex], 0, 0, image.Width, image.Height);
                FreeImage.Paste(libraryData.Bitmaps[swap.FirstSubImageIndex], libraryData.Bitmaps[swap.SecondSubImageIndex], 0, 0, 256);
                FreeImage.Paste(libraryData.Bitmaps[swap.SecondSubImageIndex], firstImage, 0, 0, 256);

                // TODO: free firstImage?
            }
            else
            {
                Log.Warning("Cannot swap the sub-images " + swap.FirstSubImageIndex + " and " + swap.SecondSubImageIndex + " because there is only " + libraryData.Bitmaps.Length + " sub-images.");
            }

            // TODO: texture atlas update?
        }
Beispiel #2
0
        public override bool Process(ImageWI iwi)
        {
            Size s        = ImageWorker.GetCurrentSize(iwi);
            Size tileSize = new Size();

            tileSize.Width  = s.Width / this.XSliceCount;
            tileSize.Height = s.Height / this.YSliceCount;
            int i = 0;

            for (int x = 0; x < this.XSliceCount; x++)
            {
                for (int y = 0; y < this.YSliceCount; y++)
                {
                    int      left  = x * tileSize.Width;
                    int      top   = y * tileSize.Height;
                    FIBITMAP aTile = FreeImage.Copy(iwi.ImageHandle, left, top, left + tileSize.Width, top + tileSize.Height);
                    ImageWorker.SaveJPGImageHandle(aTile, new FileInfo(iwi.CurrentFile.AugmentFilename(string.Format("_tile_{0:000}", i))));
                    i++;
                }
            }
            return(true);
        }
Beispiel #3
0
 private uint _cropImage(uint handle, Rectangle crop)
 {
     return(FreeImage.Copy(handle, crop.Left, crop.Top, crop.Right, crop.Bottom));
 }
Beispiel #4
0
        public void Tile(int tileSizeResolution)
        {
            int xindex = 0;
            int yindex = 0;
            // calc max square size
            int max_x = ImageWidth / tileSizeResolution;
            int max_y = ImageHeight / tileSizeResolution;
            int tilecount = Math.Min(max_x, max_y);
            int cx, cy;

            cx = cy = 0;

            string fpath = Path.Combine(path, "tiles");

            Directory.CreateDirectory(fpath);


            for (int y = 0; y < tilecount; y++)
            {
                for (int x = 0; x < tilecount; x++)
                {
                    FIBITMAP section = FreeImage.Copy(dib, cx, cy, cx + tileSizeResolution, cy + tileSizeResolution);

                    int      count     = tileSizeResolution * tileSizeResolution;
                    ushort[] pixeldata = new ushort[count];
                    int      idx       = 0;

                    // Note: dib is stored upside down
                    for (int k = (int)FreeImage.GetHeight(section) - 1; k >= 0; k--)
                    {
                        List <int> row = new List <int> {
                        };

                        Scanline <ushort> line = new Scanline <ushort>(section, k);
                        foreach (ushort pixel in line)
                        {
                            pixeldata[idx] = pixel;
                            idx++;
                        }
                    }
                    string tilename = string.Format(@"tile_x{0}_y{1}.raw", xindex, yindex);
                    tilename = Path.Combine(fpath, tilename);

                    // clean up old file
                    if (File.Exists(tilename))
                    {
                        File.Delete(tilename);
                    }

                    using (Stream fs = File.OpenWrite(tilename))
                    {
                        using (BinaryWriter bw = new BinaryWriter(fs))
                        {
                            foreach (var pixel in pixeldata)
                            {
                                bw.Write(pixel);
                            }


                            bw.Flush();
                        }
                        fs.Close();
                    }
                    xindex++;
                    cx += tileSizeResolution - 1;

                    FreeImage.UnloadEx(ref section);
                }
                xindex = 0;
                cx     = 0;
                yindex++;
                cy += tileSizeResolution - 1;
            }
        }
Beispiel #5
0
        private TextureCubeContent ImportCubeTexture(FIBITMAP bitmap, ImageType imageType, ContentManager manager)
        {
            TextureCubeContent content = new TextureCubeContent();

            if (CubeFaces == null || CubeFaces.Length == 0)
            {
                int width  = (int)FreeImage.GetWidth(bitmap);
                int height = (int)FreeImage.GetHeight(bitmap);
                // image is a cube cross
                if (width > height) // horizontal cross
                {
                    width  = width / 4;
                    height = height / 3;
                    // +x
                    content.Faces[0][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width * 2, height, width * 3, height * 2));
                    // -x
                    content.Faces[1][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, 0, height, width, height * 2));
                    // +y
                    content.Faces[2][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, 0, width * 2, height));
                    // -y
                    content.Faces[3][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, height * 2, width * 2, height * 3));
                    // +z
                    content.Faces[4][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, height, width * 2, height * 2));
                    // -z
                    content.Faces[5][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width * 3, height, width * 4, height * 2));
                }
                else // vertical cross
                {
                    width  = width / 3;
                    height = height / 4;
                    // +x
                    content.Faces[0][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width * 2, height, width * 3, height * 2));
                    // -x
                    content.Faces[1][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, 0, height, width, height * 2));
                    // +y
                    content.Faces[2][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, 0, width * 2, height));
                    // -y
                    content.Faces[3][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, height * 2, width * 2, height * 3));
                    // +z
                    content.Faces[4][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, height, width * 2, height * 2));
                    // -z
                    content.Faces[5][0] = new BitmapContent(width, height, imageType, FreeImage.Copy(bitmap, width, height * 3, width * 2, height * 4));
                }
            }
            else
            {
                int width  = (int)FreeImage.GetWidth(bitmap);
                int height = (int)FreeImage.GetHeight(bitmap);

                try
                {
                    // main image file is +x and CubeFaces contains images for [-x, +y, -y, +z, -z]
                    content.Faces[0][0] = new BitmapContent(width, height, imageType, bitmap);
                    FIBITMAP bitmap2 = FreeImage.LoadEx(CubeFaces[0]);    // -x
                    content.Faces[1][0] = new BitmapContent(width, height, imageType, bitmap2);
                    bitmap2             = FreeImage.LoadEx(CubeFaces[1]); // +y
                    content.Faces[2][0] = new BitmapContent(width, height, imageType, bitmap2);
                    bitmap2             = FreeImage.LoadEx(CubeFaces[2]); // -y
                    content.Faces[3][0] = new BitmapContent(width, height, imageType, bitmap2);
                    bitmap2             = FreeImage.LoadEx(CubeFaces[3]); // +z
                    content.Faces[4][0] = new BitmapContent(width, height, imageType, bitmap2);
                    bitmap2             = FreeImage.LoadEx(CubeFaces[4]); // -z
                    content.Faces[5][0] = new BitmapContent(width, height, imageType, bitmap2);
                }
                catch (Exception)
                {
                    manager.Log.Error("Error loading cubemap files.");
                    content = null;
                }
            }

            return(content);
        }
Beispiel #6
0
        public void Tile(int tileSizeResolution)
        {
            int xindex = 0;
            int yindex = 0;
            // calc max square size
            int max_x = ImageWidth / tileSizeResolution;
            int max_y = ImageHeight / tileSizeResolution;
            int tilecount = Math.Min(max_x, max_y);
            int cx, cy;

            cx = cy = 0;

            string tilePath = Path.Combine(path, "tiles");

            Directory.CreateDirectory(tilePath);
            string pngPath = Path.Combine(tilePath, "png");

            Directory.CreateDirectory(pngPath);


            for (int y = 0; y < tilecount; y++)
            {
                for (int x = 0; x < tilecount; x++)
                {
                    // write png
                    string pngfile = string.Format(@"tile_x{0}_y{1}.png", xindex, yindex);
                    pngfile = Path.Combine(pngPath, pngfile);
                    FIBITMAP section = FreeImage.Copy(dib, cx, cy, cx + tileSizeResolution, cy + tileSizeResolution);
                    FreeImage.Save(FREE_IMAGE_FORMAT.FIF_PNG, section, pngfile, FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION);

                    int      count     = tileSizeResolution * tileSizeResolution;
                    ushort[] pixeldata = new ushort[count];
                    int      idx       = 0;

                    // Note: dib is stored upside down
                    for (int k = (int)FreeImage.GetHeight(section) - 1; k >= 0; k--)
                    {
                        List <int> row = new List <int> {
                        };

                        Scanline <ushort> line = new Scanline <ushort>(section, k);
                        foreach (ushort pixel in line)
                        {
                            pixeldata[idx] = pixel;
                            idx++;
                        }
                    }
                    string tilename = string.Format(@"tile_x{0}_y{1}.raw", xindex, yindex);
                    tilename = Path.Combine(tilePath, tilename);

                    // status
                    if (StatusEvent != null)
                    {
                        StatusEvent(this, $"Writing {tilename}");
                    }

                    // clean up old file
                    if (File.Exists(tilename))
                    {
                        File.Delete(tilename);
                    }

                    //unsafe
                    //{


                    //    string bmptilename = string.Format(@"tile_x{0}_y{1}.png", xindex, yindex);
                    //    bmptilename = Path.Combine(fpath, bmptilename);
                    //    Bitmap b = new Bitmap(tileSizeResolution + 1, tileSizeResolution + 1, PixelFormat.Format48bppRgb);
                    //    var bmd = b.LockBits(new Rectangle(0, 0, tileSizeResolution, tileSizeResolution), ImageLockMode.ReadWrite, PixelFormat.Format48bppRgb);

                    //    byte bitsPerPixel = 48;
                    //    ushort* scan0 = (ushort*)bmd.Scan0.ToPointer();

                    //    for (int i = 0; i < pixeldata.Length; i++)
                    //    {
                    //        ushort* data = scan0 + i * (bitsPerPixel / 16);
                    //        //*data = pixeldata[offset];

                    //        //data is a pointer to the first 16 bits of the 48-bit color data
                    //        data[0] = pixeldata[i];
                    //        data[1] = pixeldata[i];
                    //        data[2] = pixeldata[i];
                    //    }
                    //    b.UnlockBits(bmd);
                    //    //Image ib = Image.FromHbitmap(b.GetHbitmap());
                    //    b.Save(bmptilename, ImageFormat.Png);
                    //}
                    using (Stream fs = File.OpenWrite(tilename))
                    {
                        using (BinaryWriter bw = new BinaryWriter(fs))
                        {
                            foreach (var pixel in pixeldata)
                            {
                                bw.Write(pixel);
                            }
                            bw.Flush();
                        }
                        fs.Close();
                    }
                    xindex++;
                    cx += tileSizeResolution - 1;

                    FreeImage.UnloadEx(ref section);
                }
                xindex = 0;
                cx     = 0;
                yindex++;
                cy += tileSizeResolution - 1;
            }
        }