Example #1
0
        protected override Document OnLoad(Stream input)
        {
            var paa    = new PAA(input);
            var pixels = PAA.GetARGB32PixelData(paa, input);

            var doc    = new Document(paa.Width, paa.Height);
            var bitmap = Layer.CreateBackgroundLayer(doc.Width, doc.Height);

            unsafe
            {
                fixed(byte *pixelsB = pixels)
                {
                    var src = pixelsB;

                    for (int y = 0; y < paa.Height; ++y)
                    {
                        var dst = bitmap.Surface.GetRowPointer(y);
                        for (int x = 0; x < paa.Width; ++x)
                        {
                            *dst = ColorBgra.FromBgra(src[0], src[1], src[2], src[3]);
                            dst++;
                            src += 4;
                        }
                    }
                }
            }
            doc.Layers.Add(bitmap);
            return(doc);
        }
Example #2
0
        static void Main(string[] args)
        {
            var filePath = (args.Length > 0) ? args[0] : "";
            var ext      = Path.GetExtension(filePath);
            var isPAA    = ext.Equals(".paa", System.StringComparison.OrdinalIgnoreCase);
            var isPAC    = ext.Equals(".pac", System.StringComparison.OrdinalIgnoreCase);

            if (File.Exists(filePath) && (isPAA || isPAC))
            {
                //get raw pixel color data in ARGB32 format
                var paaStream = File.OpenRead(filePath);
                var paa       = new PAA(paaStream, isPAC);
                var pixels    = PAA.GetARGB32PixelData(paa, paaStream);

                //We use WPF stuff here to create the actual image file, so this is Windows only

                //create a BitmapSource
                var colors        = paa.Palette.Colors.Select(c => Color.FromRgb(c.R8, c.G8, c.B8)).ToList();
                var bitmapPalette = (colors.Count > 0) ? new BitmapPalette(colors) : null;
                var bms           = BitmapSource.Create(paa.Width, paa.Height, 300, 300, PixelFormats.Bgra32, bitmapPalette, pixels, paa.Width * 4);

                //save as png
                var pngFilePath = Path.ChangeExtension(filePath, ".png");
                var pngStream   = File.OpenWrite(pngFilePath);
                var pngEncoder  = new PngBitmapEncoder();
                pngEncoder.Frames.Add(BitmapFrame.Create(bms));
                pngEncoder.Save(pngStream);
            }
        }
Example #3
0
 private static void Convert(bool isPAC, string source, string target)
 {
     Console.WriteLine($"{source} -> {target}");
     using (var paaStream = File.OpenRead(source))
     {
         var paa    = new PAA(paaStream, isPAC);
         var pixels = PAA.GetARGB32PixelData(paa, paaStream);
         using (var image = Image.LoadPixelData <Bgra32>(pixels, paa.Width, paa.Height))
         {
             image.SaveAsPng(target);
         }
     }
 }
Example #4
0
        private static void CheckFile(string file)
        {
            string     ext       = Path.GetExtension(file);
            bool       isPAC     = ext != null && ext.Equals(".pac", StringComparison.OrdinalIgnoreCase);
            FileStream paaStream = File.OpenRead(file);
            PAA        paa       = new PAA(paaStream, isPAC);

            string[] splitName           = Path.GetFileNameWithoutExtension(file)?.ToLower().Split('_');
            string   type                = splitName.Last();
            bool     requireTransparency = paa.Palette.Colors.All(color => color.A8 != byte.MaxValue);

            if (types.ContainsKey(type) && types[type])
            {
                return;
            }
            types.Add(type, requireTransparency);
            Console.WriteLine("Require Transparency: " + file + " Type: " + type);
        }
Example #5
0
        static int Main(string[] args)
        {
            return(Parser.Default.ParseArguments <Options>(args)
                   .MapResult <Options, int>(o =>
            {
                if (!File.Exists(o.Source))
                {
                    Console.Error.WriteLine($"File '{o.Source}' does not exists.");
                    return 1;
                }

                var ext = Path.GetExtension(o.Source);
                var isPAA = ext.Equals(".paa", System.StringComparison.OrdinalIgnoreCase);
                var isPAC = ext.Equals(".pac", System.StringComparison.OrdinalIgnoreCase);
                if (!isPAA && !isPAC)
                {
                    Console.Error.WriteLine($"File '{o.Source}' is not a PAA or a PAC.");
                    return 2;
                }

                using (var paaStream = File.OpenRead(o.Source))
                {
                    var paa = new PAA(paaStream, isPAC);
                    var pixels = PAA.GetARGB32PixelData(paa, paaStream);
                    using (var image = Image.LoadPixelData <Bgra32>(pixels, paa.Width, paa.Height))
                    {
                        if (string.IsNullOrEmpty(o.Target))
                        {
                            o.Target = Path.ChangeExtension(o.Source, ".png");
                        }

                        image.SaveAsPng(o.Target);
                    }
                }
                return 0;
            },
                                             e => 3));
        }
Example #6
0
        private static void ProcessPAAConvert(object input)
        {
            try
            {
                lock (DataLock)
                {
                    runningTasks++;
                    UpdateTitle();
                }

                WorkTask data     = (WorkTask)input;
                string   filePath = data.inputPath;
                Program.LOG("Process File: " + filePath);
                //get raw pixel color data in ARGB32 format
                string ext = Path.GetExtension(filePath);

                bool isPac = ext != null && ext.Equals(".pac", StringComparison.OrdinalIgnoreCase);

                using (FileStream paaStream = File.OpenRead(filePath))
                {
                    PAA    paa    = new PAA(paaStream, isPac);
                    byte[] pixels = PAA.GetARGB32PixelData(paa, paaStream);
                    //create a BitmapSource
                    List <Color>  colors        = paa.Palette.Colors.Select(c => Color.FromArgb(c.A8, c.R8, c.G8, c.B8)).ToList();
                    BitmapPalette bitmapPalette = colors.Count > 0 ? new BitmapPalette(colors) : null;
                    BitmapSource  bms           = BitmapSource.Create(paa.Width, paa.Height, 300, 300, PixelFormats.Bgra32, bitmapPalette,
                                                                      pixels, paa.Width * 4);
                    using (FileStream pngStream = File.OpenWrite(data.outputPath))
                    {
                        PngBitmapEncoder pngEncoder = new PngBitmapEncoder();

                        BitmapFrame bmf = BitmapFrame.Create(bms);

                        int   size          = 256;
                        float percentWidth  = Math.Min(size / (float)bmf.PixelWidth, 1);
                        float percentHeight = Math.Min(size / (float)bmf.PixelHeight, 1);
                        float percent       = percentHeight < percentWidth ? percentHeight : percentWidth;
                        if (bmf.PixelWidth > size || bmf.PixelHeight > size)
                        {
                            bmf = FastResize(bmf, percent, percent);
                        }
                        pngEncoder.Frames.Add(bmf);
                        pngEncoder.Save(pngStream);

                        size          = 64;
                        percentWidth  = Math.Min(size / (float)bmf.PixelWidth, 1);
                        percentHeight = Math.Min(size / (float)bmf.PixelHeight, 1);
                        percent       = percentHeight < percentWidth ? percentHeight : percentWidth;
                        if (bmf.PixelWidth > size || bmf.PixelHeight > size)
                        {
                            using (FileStream pngStreamPreview = File.OpenWrite(data.previewOutputPath))
                            {
                                PngBitmapEncoder pngEncoderPreview = new PngBitmapEncoder();
                                bmf = FastResize(bmf, percent, percent);
                                pngEncoderPreview.Frames.Add(bmf);
                                pngEncoderPreview.Save(pngStreamPreview);
                            }
                        }
                    }
                }
                // Memory Cleanup
                Program.LOG("Processing Done: " + filePath);
            }
            catch (Exception)
            {
            }
            finally
            {
                lock (DataLock)
                {
                    done++;
                    runningTasks--;
                    UpdateTitle();
                }
                GC.Collect();
            }
        }