예제 #1
0
        public static void SerializeAsJson(string targetPath, object toSerialize)
        {
            FileStream stream;

            Directory.CreateDirectory(Utils.SolveDirFromFileName(targetPath));

            File.Delete(targetPath);

            stream = !File.Exists(targetPath) ? new FileStream(targetPath, FileMode.CreateNew, FileAccess.ReadWrite)
                : new FileStream(targetPath, FileMode.Open, FileAccess.ReadWrite);

            using (stream)
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    JsonSerializer serializer = new JsonSerializer {
                        Formatting = Formatting.Indented
                    };
                    JsonWriter jsonWriter = new JsonTextWriter(writer);
                    serializer.Serialize(jsonWriter, toSerialize);
                }
            }

            Print.Line(ConsoleColor.Green, "Successfully serialized GlobalConfig to file: " + targetPath);
        }
예제 #2
0
        public static T DeserializeFromJson <T>(string pathToFile)
        {
            FileStream stream;
            T          loaded;

            stream = !File.Exists(pathToFile) ? new FileStream(pathToFile, FileMode.CreateNew, FileAccess.ReadWrite)
                : new FileStream(pathToFile, FileMode.Open, FileAccess.ReadWrite);

            using (stream)
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    JsonSerializer serializer = new JsonSerializer {
                        Formatting = Formatting.Indented
                    };
                    JsonReader jsonReader = new JsonTextReader(reader);
                    loaded = serializer.Deserialize <T>(jsonReader);
                }
            }

            if (loaded == null)
            {
                Print.Line(ConsoleColor.Red, "Couldn't load " + pathToFile);
                throw new IOException("Couldn't load file from " + pathToFile);
            }

            Print.Line(ConsoleColor.Green, "Successfully loaded " + pathToFile);

            return(loaded);
        }
예제 #3
0
        public static ChunkyConfig32bit LoadConfig(string pathToConfig, bool treatAsName = false)
        {
            ChunkyConfig32bit result = new ChunkyConfig32bit();

            if (treatAsName)
            {
                pathToConfig = Path.Combine(SavedConfigsPath, pathToConfig);
            }

            try
            {
                result = FileHelper.DeserializeFromJson <ChunkyConfig32bit>(pathToConfig);
            }
            catch (Exception e)
            {
                Print.Line(e);
                Print.Line(ConsoleColor.Yellow, "Returning a default config as a fallback.");
            }

            return(result);
        }
예제 #4
0
        public ChunkyBatchProcessor(ChunkyConfig32bit config)
        {
            _config = config;

            if (!Directory.Exists(config.SourcePath))
            {
                throw new Exception("Attempted to batch operate on a path but it didn't exist");
            }

            Print.Line(ConsoleColor.Cyan, "Starting to process images from: " + config.SourcePath);

            string[] files = Directory.GetFiles(config.SourcePath);

            foreach (string filePath in files)
            {
                if (filePath.EndsWith(".png") ||
                    filePath.EndsWith(".jpg") ||
                    filePath.EndsWith(".jpeg") ||
                    filePath.EndsWith(".bmp"))
                {
                    _processors.Enqueue(new ChunkyProcessor(new ChunkyConfig32bit(
                                                                config.Name,
                                                                config.TargetDir,
                                                                filePath,
                                                                config.TargetImageType,
                                                                config.TargetPixelFormat,
                                                                config.RemainderHandlingMode,
                                                                config.CompatibilityMode,
                                                                config.GenerateReconstruction,
                                                                config.GenerateVarianceComparison,
                                                                config.ChunkWidth,
                                                                config.ChunkHeight,
                                                                config.ChunkCountX,
                                                                config.ChunkCountY,
                                                                config.MinDepth,
                                                                config.MaxDepth)));
                }
            }
        }
예제 #5
0
        public bool Prompt <T>(string message, out T result, params string[] skipCmds)
        {
            Print.Line(message);
            string line = Console.ReadLine();

            result = default;

            if (skipCmds.Contains(line))
            {
                return(false);
            }

            #region integers
            if (typeof(T) == typeof(short))
            {
                if (!short.TryParse(line, out short s))
                {
                    if (!int.TryParse(line, out int i))
                    {
                        if (!long.TryParse(line, out long l))
                        {
                            PromptInvalidValueEntered?.Invoke(this, EventArgs.Empty);
                            return(Prompt <T>(message, out result, skipCmds));
                        }
                        result = (T)(object)(short)l;
                    }
                    result = (T)(object)(short)i;
                }
                result = (T)(object)s;
            }
            else if (typeof(T) == typeof(int))
            {
                if (!short.TryParse(line, out short s))
                {
                    if (!int.TryParse(line, out int i))
                    {
                        if (!long.TryParse(line, out long l))
                        {
                            PromptInvalidValueEntered?.Invoke(this, EventArgs.Empty);
                            return(Prompt <T>(message, out result, skipCmds));
                        }
                        result = (T)(object)(int)l;
                    }
                    result = (T)(object)i;
                }
                result = (T)(object)(int)s;
            }
            else if (typeof(T) == typeof(long))
            {
                if (!short.TryParse(line, out short s))
                {
                    if (!int.TryParse(line, out int i))
                    {
                        if (!long.TryParse(line, out long l))
                        {
                            PromptInvalidValueEntered?.Invoke(this, EventArgs.Empty);
                            return(Prompt <T>(message, out result, skipCmds));
                        }
                        result = (T)(object)l;
                    }
                    result = (T)(object)(long)i;
                }
                result = (T)(object)(long)s;
            }
            #endregion

            #region floats
            else if (typeof(T) == typeof(float))
            {
                if (!float.TryParse(line, out float f))
                {
                    if (!double.TryParse(line, out double d))
                    {
                        PromptInvalidValueEntered?.Invoke(this, EventArgs.Empty);
                        return(Prompt <T>(message, out result, skipCmds));
                    }
                    result = (T)(object)(float)d;
                }
                result = (T)(object)f;
            }
            else if (typeof(T) == typeof(double))
            {
                if (!float.TryParse(line, out float f))
                {
                    if (!double.TryParse(line, out double d))
                    {
                        PromptInvalidValueEntered?.Invoke(this, EventArgs.Empty);
                        return(Prompt <T>(message, out result, skipCmds));
                    }
                    result = (T)(object)d;
                }
                result = (T)(object)(double)f;
            }
            #endregion

            else if (typeof(T) == typeof(string))
            {
                result = (T)(object)line;
            }

            if (result == null)
            {
                PromptInvalidValueEntered?.Invoke(this, EventArgs.Empty);
                return(Prompt <T>(message, out result, skipCmds));
            }
            else
            {
                PromptSuccess?.Invoke(this, EventArgs.Empty);
                return(true);
            }
        }
예제 #6
0
        public void SaveResultToDisk(bool exportReconstruction = true, bool exportVarianceTest = true, string targetDir = null, string name = null)
        {
            name ??= Config.Name;
            targetDir ??= Config.TargetDir;
            if (State.Config.Verbose)
            {
                Print.Line(ConsoleColor.Magenta, "Starting to batch save to: " + targetDir);
            }

            string path = Path.Combine(targetDir, name);

            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);

                foreach (string filePath in files)
                {
                    File.Delete(filePath);
                }
            }
            Directory.CreateDirectory(path);

            for (int x = 0; x < Result.GetLength(0); x++)
            {
                for (int y = 0; y < Result.GetLength(1); y++)
                {
                    ChunkData chunk     = Result[x, y];
                    string    finalPath = Path.Combine(path, name + "-" + chunk.X + "-" + chunk.Y + ".png");

                    if (!Config.CompatibilityMode)
                    {
                        Thread thread = new Thread(() =>
                                                   chunk.Bitmap.Save(finalPath, ImageFormat.Png)
                                                   );
                        thread.Start();
                    }
                    else
                    {
                        chunk.Bitmap.Save(finalPath, ImageFormat.Png);
                    }

                    if (State.Config.Verbose)
                    {
                        Print.Line(ConsoleColor.Green, "Saved a file to: " + finalPath);
                    }
                }
            }

            if (exportReconstruction)
            {
                //Reconstructor reconstructor = new Reconstructor(Result, name, (short)OriginalBitmap.Width, (short)OriginalBitmap.Height);
                Reconstructor reconstructor = new Reconstructor(path, name, (short)OriginalBitmap.Width, (short)OriginalBitmap.Height);
                if (exportVarianceTest)
                {
                    reconstructor.ReconstructAndCompare(OriginalBitmap, path, Config.CompatibilityMode);
                }
                else
                {
                    reconstructor.Reconstruct(targetDir);
                }
            }
        }