private static ImageBuffer LoadImageFromDisk(PartThumbnailWidget thumbnailWidget, string stlHashCode)
        {
            ImageBuffer tempImage     = new ImageBuffer(BigRenderSize.x, BigRenderSize.y);
            string      imageFileName = GetImageFileName(stlHashCode);

            if (File.Exists(imageFileName))
            {
                if (partExtension == ".png")
                {
                    if (ImageIO.LoadImageData(imageFileName, tempImage))
                    {
                        return(tempImage);
                    }
                }
                else
                {
                    if (ImageTgaIO.LoadImageData(imageFileName, tempImage))
                    {
                        return(tempImage);
                    }
                }
            }

            return(null);
        }
Example #2
0
        private static ImageBuffer LoadImageFromDisk(PartThumbnailWidget thumbnailWidget, string stlHashCode, Point2D size)
        {
            ImageBuffer tempImage     = new ImageBuffer(size.x, size.y, 32, new BlenderBGRA());
            string      imageFileName = GetFilenameForSize(stlHashCode, size);

            if (File.Exists(imageFileName))
            {
                if (partExtension == ".png")
                {
                    if (ImageIO.LoadImageData(imageFileName, tempImage))
                    {
                        return(tempImage);
                    }
                }
                else
                {
                    if (ImageTgaIO.LoadImageData(imageFileName, tempImage))
                    {
                        return(tempImage);
                    }
                }
            }

            return(null);
        }
Example #3
0
        public new static GameObject Load(String PathName)
        {
            // First we load up the Data In the Serialization file.
            String            gameDataObjectXMLPath = System.IO.Path.Combine(PathName, "ImageSequence");
            GameImageSequence sequenceLoaded        = LoadSerializationFileForFolder(gameDataObjectXMLPath);

            // Now lets look for and load up any images that we find.
            String[]      tgaFilesArray  = Directory.GetFiles(PathName, "*.tga");
            List <String> sortedTgaFiles = new List <string>(tgaFilesArray);

            // Make sure they are sorted.
            sortedTgaFiles.Sort();
            sequenceLoaded.m_Images = new ImageBuffer[sortedTgaFiles.Count];
            int imageIndex = 0;

            foreach (String tgaFile in sortedTgaFiles)
            {
                sequenceLoaded.m_Images[imageIndex] = new ImageBuffer(new BlenderPreMultBGRA());
                Stream imageStream = File.Open(tgaFile, FileMode.Open);
                ImageTgaIO.LoadImageData(sequenceLoaded.m_Images[imageIndex], imageStream, 32);
                imageIndex++;
            }

            if (sequenceLoaded.m_CenterOriginDurringPreprocessing)
            {
                sequenceLoaded.CenterOriginOffset();
            }

            if (sequenceLoaded.m_CropToVisibleDurringPreprocessing)
            {
                sequenceLoaded.CropToVisible();
            }

            return(sequenceLoaded);
        }
Example #4
0
        public static ImageBuffer LoadImageFromDisk(string stlHashCode)
        {
            try
            {
                ImageBuffer tempImage     = new ImageBuffer(BigRenderSize.x, BigRenderSize.y);
                string      imageFileName = GetImageFileName(stlHashCode);

                if (File.Exists(imageFileName))
                {
                    if (partExtension == ".png")
                    {
                        if (ImageIO.LoadImageData(imageFileName, tempImage))
                        {
                            return(tempImage);
                        }
                    }
                    else
                    {
                        if (ImageTgaIO.LoadImageData(tempImage, imageFileName))
                        {
                            return(tempImage);
                        }
                    }
                }
            }
            catch
            {
            }

            return(null);
        }
Example #5
0
        public void CompareToLionTGA()
        {
            LionShape   lionShape     = new LionShape();
            ImageBuffer renderedImage = new ImageBuffer(512, 400, 24, new BlenderBGR());
            byte        alpha         = (byte)(.1 * 255);

            for (int i = 0; i < lionShape.NumPaths; i++)
            {
                lionShape.Colors[i].Alpha0To255 = alpha;
            }

            Affine transform = Affine.NewIdentity();

            transform *= Affine.NewTranslation(-lionShape.Center.x, -lionShape.Center.y);
            transform *= Affine.NewTranslation(renderedImage.Width / 2, renderedImage.Height / 2);

            // This code renders the lion:
            VertexSourceApplyTransform transformedPathStorage = new VertexSourceApplyTransform(lionShape.Path, transform);
            Graphics2D renderer = renderedImage.NewGraphics2D();

            renderer.Clear(new RGBA_Floats(1.0, 1.0, 1.0, 1.0));
            renderer.Render(transformedPathStorage, lionShape.Colors, lionShape.PathIndex, lionShape.NumPaths);

            ImageTgaIO.Save(renderedImage, "TestOutput.tga");

            Stream      imageStream = File.Open("LionRenderMaster.tga", FileMode.Open);
            ImageBuffer masterImage = new ImageBuffer();

            ImageTgaIO.LoadImageData(masterImage, imageStream, 24);

            bool sameWidth  = masterImage.Width == renderedImage.Width;
            bool sameHeight = masterImage.Height == renderedImage.Height;

            Assert.IsTrue(sameWidth && sameHeight);
            Assert.IsTrue(masterImage.BitDepth == renderedImage.BitDepth);
            int unused;

            byte[] masterBuffer   = masterImage.GetBuffer(out unused);
            byte[] renderedBuffer = renderedImage.GetBuffer(out unused);
            Assert.IsTrue(masterBuffer.Length == renderedBuffer.Length);
            for (int i = 0; i < masterBuffer.Length; i++)
            {
                if (masterBuffer[i] != renderedBuffer[i])
                {
                    Assert.IsTrue(false);
                }
            }
        }
Example #6
0
        private static ImageBuffer LoadImageFromDisk(PartThumbnailWidget thumbnailWidget, string stlHashCode, Point2D size)
        {
            ImageBuffer tempImage = new ImageBuffer(size.x, size.y, 32, new BlenderBGRA());
            string      applicationUserDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
            string      folderToSavePrintsTo    = Path.Combine(applicationUserDataPath, "data", "temp", "thumbnails");
            string      tgaFileName             = Path.Combine(folderToSavePrintsTo, "{0}_{1}x{2}.tga".FormatWith(stlHashCode, size.x, size.y));

            if (File.Exists(tgaFileName))
            {
                if (ImageTgaIO.LoadImageData(tgaFileName, tempImage))
                {
                    return(tempImage);
                }
            }

            return(null);
        }
Example #7
0
        private void CheckTestAgainstControl(ImageBuffer testImage, string testTypeString)
        {
            Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory);

            // there is an assumption that we got to save valid images at least once.
            string      controlFileTga          = testTypeString + " Control.tga";
            string      imageFolder             = "ControlImages";
            string      testFailPathAndFileName = Path.Combine(imageFolder, testTypeString + " Test Fail.tga");
            ImageBuffer controlImage            = new ImageBuffer();

            if (!Directory.Exists(imageFolder))
            {
                Directory.CreateDirectory(imageFolder);
            }
            string controlPathAndFileName = Path.Combine(imageFolder, controlFileTga);

            if (File.Exists(controlPathAndFileName))
            {
                ImageTgaIO.LoadImageData(controlImage, controlPathAndFileName);

                bool testIsSameAsControl = controlImage.Equals(testImage);
                if (!testIsSameAsControl)
                {
                    // this image will be in the current output folder inside of imageFolder
                    ImageTgaIO.Save(testImage, testFailPathAndFileName);
                }
                else if (File.Exists(testFailPathAndFileName))
                {
                    // we don't want to have these confounding our results.
                    File.Delete(testFailPathAndFileName);
                }

                Assert.IsTrue(testIsSameAsControl);
                // If you want to create new control images select SetNextStatement to inside the else condition to create them.
            }
            else
            {
                ImageTgaIO.Save(testImage, controlPathAndFileName);
            }
        }
Example #8
0
        public static IObject3D Load(Stream fileStream, Action <double, string> reportProgress = null, IObject3D source = null)
        {
            IObject3D root = source ?? new Object3D();

            var time = Stopwatch.StartNew();

            // LOAD THE MESH DATA
            var objFile = new Obj();

            objFile.LoadObj(fileStream);

            IObject3D context = new Object3D();

            root.Children.Add(context);

            var mesh = new Mesh();

            context.SetMeshDirect(mesh);

            foreach (var vertex in objFile.VertexList)
            {
                mesh.Vertices.Add(new Vector3Float(vertex.X, vertex.Y, vertex.Z));
            }

            foreach (var face in objFile.FaceList)
            {
                for (int i = 0; i < face.VertexIndexList.Length; i++)
                {
                    if (face.VertexIndexList[i] >= objFile.TextureList.Count)
                    {
                        int a = 0;
                    }
                }

                mesh.Faces.Add(face.VertexIndexList[0] - 1, face.VertexIndexList[1] - 1, face.VertexIndexList[2] - 1, mesh.Vertices);
                if (face.VertexIndexList.Length == 4)
                {
                    // add the other side of the quad
                    mesh.Faces.Add(face.VertexIndexList[0] - 1, face.VertexIndexList[2] - 1, face.VertexIndexList[3] - 1, mesh.Vertices);
                }
            }

            // load and apply any texture
            if (objFile.Material != "")
            {
                // TODO: have consideration for this being in a shared zip file
                string pathToObj = Path.GetDirectoryName(((FileStream)fileStream).Name);
                //Try-catch block for when objFile.Material is not found
                try
                {
                    using (var materialsStream = File.OpenRead(Path.Combine(pathToObj, objFile.Material)))
                    {
                        var mtl = new Mtl();
                        mtl.LoadMtl(materialsStream);

                        foreach (var material in mtl.MaterialList)
                        {
                            if (!string.IsNullOrEmpty(material.DiffuseTextureFileName))
                            {
                                var pathToTexture = Path.Combine(pathToObj, material.DiffuseTextureFileName);
                                if (File.Exists(pathToTexture))
                                {
                                    var diffuseTexture = new ImageBuffer();

                                    // TODO: have consideration for this being in a shared zip file
                                    using (var imageStream = File.OpenRead(pathToTexture))
                                    {
                                        if (Path.GetExtension(material.DiffuseTextureFileName).ToLower() == ".tga")
                                        {
                                            ImageTgaIO.LoadImageData(diffuseTexture, imageStream, 32);
                                        }
                                        else
                                        {
                                            ImageIO.LoadImageData(imageStream, diffuseTexture);
                                        }
                                    }

                                    if (diffuseTexture.Width > 0 && diffuseTexture.Height > 0)
                                    {
                                        int meshFace = 0;
                                        for (int objFace = 0; objFace < objFile.FaceList.Count; objFace++, meshFace++)
                                        {
                                            var face = mesh.Faces[meshFace];

                                            var faceData = objFile.FaceList[objFace];

                                            int textureIndex0 = faceData.TextureVertexIndexList[0] - 1;
                                            var uv0           = new Vector2Float(objFile.TextureList[textureIndex0].X, objFile.TextureList[textureIndex0].Y);
                                            int textureIndex1 = faceData.TextureVertexIndexList[1] - 1;
                                            var uv1           = new Vector2Float(objFile.TextureList[textureIndex1].X, objFile.TextureList[textureIndex1].Y);
                                            int textureIndex2 = faceData.TextureVertexIndexList[2] - 1;
                                            var uv2           = new Vector2Float(objFile.TextureList[textureIndex2].X, objFile.TextureList[textureIndex2].Y);

                                            mesh.FaceTextures.Add(meshFace, new FaceTextureData(diffuseTexture, uv0, uv1, uv2));

                                            if (faceData.TextureVertexIndexList.Length == 4)
                                            {
                                                meshFace++;

                                                int textureIndex3 = faceData.TextureVertexIndexList[3] - 1;
                                                var uv3           = new Vector2Float(objFile.TextureList[textureIndex3].X, objFile.TextureList[textureIndex3].Y);

                                                mesh.FaceTextures.Add(meshFace, new FaceTextureData(diffuseTexture, uv0, uv2, uv3));
                                            }
                                        }

                                        context.Color = Color.White;
                                        root.Color    = Color.White;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    // Just continue as if obj.Material == "" to show object
                }
            }

            time.Stop();
            Debug.WriteLine(string.Format("OBJ Load in {0:0.00}s", time.Elapsed.TotalSeconds));

            time.Restart();
            bool hasValidMesh = root.Children.Where(item => item.Mesh.Faces.Count > 0).Any();

            Debug.WriteLine("hasValidMesh: " + time.ElapsedMilliseconds);

            reportProgress?.Invoke(1, "");

            return(hasValidMesh ? root : null);
        }
Example #9
0
        public static IObject3D Load(Stream fileStream, CancellationToken cancellationToken, Action <double, string> reportProgress = null, IObject3D source = null)
        {
            IObject3D root = source ?? new Object3D();

            Stopwatch time = Stopwatch.StartNew();

            // LOAD THE MESH DATA
            Obj objFile = new Obj();

            objFile.LoadObj(fileStream);

            IObject3D context = new Object3D();

            root.Children.Add(context);

            var mesh = new Mesh();

            context.SetMeshDirect(mesh);

            foreach (var vertex in objFile.VertexList)
            {
                mesh.CreateVertex(vertex.X, vertex.Y, vertex.Z, CreateOption.CreateNew, SortOption.WillSortLater);
            }

            foreach (var face in objFile.FaceList)
            {
                List <int> zeroBased = new List <int>(face.VertexIndexList.Length);
                foreach (var index in face.VertexIndexList)
                {
                    zeroBased.Add(index - 1);
                }
                mesh.CreateFace(zeroBased.ToArray(), CreateOption.CreateNew);
            }

            // load and apply any texture
            if (objFile.Material != "")
            {
                // TODO: have consideration for this being in a shared zip file
                string pathToObj = Path.GetDirectoryName(((FileStream)fileStream).Name);
                using (var materialsStream = File.OpenRead(Path.Combine(pathToObj, objFile.Material)))
                {
                    var mtl = new Mtl();
                    mtl.LoadMtl(materialsStream);

                    foreach (var material in mtl.MaterialList)
                    {
                        if (!string.IsNullOrEmpty(material.DiffuseTextureFileName))
                        {
                            var pathToTexture = Path.Combine(pathToObj, material.DiffuseTextureFileName);
                            if (File.Exists(pathToTexture))
                            {
                                ImageBuffer diffuseTexture = new ImageBuffer();

                                // TODO: have consideration for this being in a shared zip file
                                using (var ImageStream = File.OpenRead(pathToTexture))
                                {
                                    if (Path.GetExtension(material.DiffuseTextureFileName).ToLower() == ".tga")
                                    {
                                        ImageTgaIO.LoadImageData(diffuseTexture, ImageStream, 32);
                                    }
                                    else
                                    {
                                        AggContext.ImageIO.LoadImageData(ImageStream, diffuseTexture);
                                    }
                                }

                                if (diffuseTexture.Width > 0 && diffuseTexture.Height > 0)
                                {
                                    for (int faceIndex = 0; faceIndex < objFile.FaceList.Count; faceIndex++)
                                    {
                                        var faceData = objFile.FaceList[faceIndex];
                                        var polyFace = mesh.Faces[faceIndex];
                                        polyFace.SetTexture(0, diffuseTexture);
                                        int edgeIndex = 0;
                                        foreach (FaceEdge faceEdge in polyFace.FaceEdges())
                                        {
                                            int textureIndex = faceData.TextureVertexIndexList[edgeIndex] - 1;
                                            faceEdge.SetUv(0, new Vector2(objFile.TextureList[textureIndex].X,
                                                                          objFile.TextureList[textureIndex].Y));
                                            edgeIndex++;
                                        }
                                    }

                                    context.Color = Color.White;
                                    root.Color    = Color.White;
                                }
                            }
                        }
                    }
                }
            }

            time.Stop();
            Debug.WriteLine(string.Format("OBJ Load in {0:0.00}s", time.Elapsed.TotalSeconds));

            time.Restart();
            bool hasValidMesh = root.Children.Where(item => item.Mesh.Faces.Count > 0).Any();

            Debug.WriteLine("hasValidMesh: " + time.ElapsedMilliseconds);

            reportProgress?.Invoke(1, "");

            return((hasValidMesh) ? root : null);
        }