public void Export(Palette_18Bit basePalette, List <ResourceElement> elementList, string rootDir, string imagePathFormat, string palettePathFormat, IProgress <int> progress = null)
 {
     ResourceElement[] safeList = elementList.ToArray();
     for (int i = 0; i < safeList.Length; i++)
     {
         Export(basePalette, i, safeList[i], rootDir, imagePathFormat.F(i), palettePathFormat.F(i), progress);
     }
 }
        /// <summary>Imports, replacing existing data only if there is new data</summary>
        public void ImportMerge(List <ResourceElement> srcResourceList, Palette_18Bit basePalette, string rootDir, IProgress <int> progress = null)
        {
            // create seperate list
            List <ResourceElement> workingList = new List <ResourceElement>(EntryCount);

            for (int i = 0; i < EntryCount; i++)
            {
                workingList.Add((i < srcResourceList.Count) ? srcResourceList[i] : new ResourceElement());
            }
            Import(ref workingList, basePalette, rootDir, progress);
            srcResourceList.Clear();
            srcResourceList.AddRange(workingList);
        }
        private void Import(ref List <ResourceElement> workingList, Palette_18Bit basePalette, string rootDir, IProgress <int> progress = null)
        {
            foreach (string key in Entries.GetKeys())
            {
                if (!int.TryParse(key, out int ikey))
                {
                    continue;
                }

                ResourceExportEntry entry = Entries[key];
                if (ikey >= workingList.Count)
                {
                    continue;
                }

                Bitmap   image = new Bitmap(Path.Combine(rootDir, entry.ImagePath));
                IPalette pal   = basePalette;
                if (entry.UseOwnPalette == true)
                {
                    PaletteFile palfile = new PaletteFile();
                    palfile.ReadFromFile(Path.Combine(rootDir, entry.PalettePath));
                    pal = palfile.Palette;
                }

                if (entry.UsePalette)
                {
                    workingList[ikey].ImportImageDataAs8Bit(image, pal, entry.UseOwnPalette == false, out int _);
                }
                else
                {
                    workingList[ikey].ImportImageDataAs15Bit(image);
                }

                workingList[ikey].FrameSize     = entry.FrameSize;
                workingList[ikey].ImageOffset   = entry.ImageOffset;
                workingList[ikey].Alignment     = entry.Alignment;
                workingList[ikey].ImageHandle   = entry.ImageHandle;
                workingList[ikey].PaletteHandle = entry.PaletteHandle;

                progress?.Report(ikey);
            }
        }
        public void Export(Palette_18Bit basePalette, int key, ResourceElement element, string rootDir, string imagePath, string palettePath, IProgress <int> progress = null)
        {
            if (element.PaletteType == PaletteType.EMPTY_ENTRY)
            {
                return;
            }

            ResourceExportEntry expentry = new ResourceExportEntry();

            expentry.FrameSize     = element.FrameSize;
            expentry.ImageOffset   = element.ImageOffset;
            expentry.Alignment     = element.Alignment;
            expentry.ImageHandle   = element.ImageHandle;
            expentry.PaletteHandle = element.PaletteHandle;
            expentry.UsePalette    = element.BitsPerPixel == 8;
            if (expentry.UsePalette)
            {
                expentry.UseOwnPalette = element.PaletteHandle != 0;
            }
            else
            {
                expentry.UseOwnPalette = null;
            }

            HousePaletteFile dummy = new HousePaletteFile();
            // modify palette to remove unique colors.
            Palette_15Bit opal = element.Palette?.Clone();

            if (element.Palette != null)
            {
                element.Palette.MakeSpecialIndicesUnique(out _);
            }

            Bitmap image = element.GetBitmap(basePalette, dummy, false, false, -1);

            if (!Path.IsPathRooted(imagePath))
            {
                imagePath = Path.Combine(rootDir, imagePath);
            }
            Directory.CreateDirectory(Path.GetDirectoryName(imagePath));
            image.Save(imagePath);
            expentry.ImagePath = WinAPI.GetRelativePath(rootDir + @"\", imagePath);

            if (expentry.UseOwnPalette ?? false)
            {
                PaletteFile palfile = new PaletteFile();
                palfile.Palette.Import(element.Palette);
                if (!Path.IsPathRooted(palettePath))
                {
                    palettePath = Path.Combine(rootDir, palettePath);
                }
                Directory.CreateDirectory(Path.GetDirectoryName(palettePath));
                palfile.WriteToFile(palettePath);
                expentry.PalettePath = WinAPI.GetRelativePath(rootDir + @"\", palettePath);
            }

            // restore the original palette
            element.Palette = opal;

            Entries.Put(key.ToString(), expentry);
            progress?.Report(key);
        }
Example #5
0
        private void Save(Palette_18Bit palette, int index, string format, bool transparency, int houseIndex)
        {
            Image img = _resourceFile.Resources[index].GetBitmap(palette, HousePaletteFile, false, transparency, houseIndex);

            img.Save(format.F(index));
        }
        private async void bOK_Click(object sender, System.EventArgs e)
        {
            Enabled = false;
            bool   merge = rbMerge.Checked;
            string rpath = tbFilepath.Text;
            string rootDir;

            ResourceExportINIFile rfile = new ResourceExportINIFile();

            try
            {
                rootDir = Path.GetDirectoryName(rpath);
                rfile.ReadFromFile(rpath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Import failed.\nThe data file is in the incorrect format.\n\nException: {0}".F(ex.Message));
                Enabled = true;
                return;
            }

            try
            {
                if (rfile.EntryCount < _resourceFile.Resources.Count)
                {
                    if (MessageBox.Show("The entry count declared in the import data is {0}, but the current resource file has {1} entries.\nIf you continue, the resource file will be truncated. Entries above {0:0000} will be cleared.\nAre you sure you want to proceed?".F(rfile.EntryCount, _resourceFile.Resources.Count)
                                        , "Dune 2000 Editor"
                                        , MessageBoxButtons.YesNo)
                        != DialogResult.Yes)
                    {
                        return;
                    }
                }

                Progress <int> p = new Progress <int>();
                pbarProgress.Maximum = rfile.EntryCount;
                pbarProgress.Value   = 0;
                lblProgress.Text     = "Setting up...";
                p.ProgressChanged   += (s, id) =>
                {
                    pbarProgress.Value = id;
                    lblProgress.Text   = "{0} / {1}".F(id, rfile.EntryCount);
                };

                Palette_18Bit pal = BasePalette.Clone();
                await Task.Factory.StartNew(() =>
                {
                    if (merge)
                    {
                        rfile.ImportMerge(_resourceFile.Resources, pal, rootDir, p);
                    }
                    else
                    {
                        rfile.ImportReplace(_resourceFile.Resources, pal, rootDir, p);
                    }
                });

                //t.Wait();

                pbarProgress.Value = pbarProgress.Maximum;
                lblProgress.Text   = "Finished!";
                MessageBox.Show("Import completed!");
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (AggregateException ex)
            {
                MessageBox.Show("Import failed.\n\nReason: {0}".F(ex.InnerException?.Message ?? ex.Message));
            }
            finally
            {
                pbarProgress.Value = 0;
                lblProgress.Text   = "";
                Enabled            = true;
            }
        }
        private async void bOK_Click(object sender, System.EventArgs e)
        {
            Enabled = false;
            bool exportAll = cbSelectAll.Checked;

            List <int> selected = new List <int>(lboxItems.SelectedIndices.Count);

            if (!exportAll)
            {
                foreach (int s in lboxItems.SelectedIndices)
                {
                    selected.Add(s);
                }
            }

            try
            {
                string        rpath             = tbFilepath.Text;
                string        rootDir           = Path.GetDirectoryName(rpath);
                string        relImagePathFmt   = @"images\img_{0:0000}.png";
                string        relPalettePathFmt = @"palettes\pal_{0:0000}.bin";
                Palette_18Bit pal = BasePalette.Clone();
                Directory.CreateDirectory(rootDir);
                Directory.CreateDirectory(Path.Combine(rootDir, "images"));
                Directory.CreateDirectory(Path.Combine(rootDir, "palettes"));

                ResourceExportINIFile rfile = new ResourceExportINIFile();
                rfile.EntryCount = _resource.Resources.Count;

                Progress <int> p = new Progress <int>();
                pbarProgress.Maximum = rfile.EntryCount;
                pbarProgress.Value   = 0;
                lblProgress.Text     = "Setting up...";
                p.ProgressChanged   += (s, id) =>
                {
                    pbarProgress.Value = id;
                    lblProgress.Text   = "{0} / {1}".F(id, rfile.EntryCount);
                };

                await Task.Factory.StartNew(() =>
                {
                    if (exportAll)
                    {
                        rfile.Export(pal, _resource.Resources, rootDir, relImagePathFmt, relPalettePathFmt, p);
                    }
                    else
                    {
                        foreach (int index in selected.ToArray())
                        {
                            rfile.Export(pal, index, _resource.Resources[index], rootDir, relImagePathFmt.F(index), relPalettePathFmt.F(index), p);
                        }
                    }
                    rfile.WriteToFile(rpath);
                });

                pbarProgress.Value = pbarProgress.Maximum;
                lblProgress.Text   = "Finished!";
                MessageBox.Show("Export completed!");
            }
            catch (AggregateException ex)
            {
                MessageBox.Show("Export failed.\n\nReason: {0}".F(ex.InnerException?.Message ?? ex.Message));
            }
            finally
            {
                pbarProgress.Value = 0;
                lblProgress.Text   = "";
                Enabled            = true;
            }
        }
Example #8
0
        public Bitmap GetBitmap(Palette_18Bit basePalette, HousePaletteFile housePalette, bool includeFrame, bool makeTransparent, int houseIndex)
        {
            if (PaletteType == PaletteType.EMPTY_ENTRY)
            {
                // at least draw something so it does not error out.
                Bitmap empty = new Bitmap(1, 1);
                empty.MakeTransparent();
                return(empty);
            }

            Bitmap bmp = new Bitmap(ImageWidth, ImageHeight);

            if (BitsPerPixel == 8)
            {
                IPalette pal = (PaletteHandle == 0) ? (IPalette)basePalette.Clone() : Palette.Clone();
                if (houseIndex != -1)
                {
                    pal = housePalette.Merge(pal, houseIndex);
                }

                for (int x = 0; x < ImageWidth; x++)
                {
                    for (int y = 0; y < ImageHeight; y++)
                    {
                        int  pos   = x + y * ImageWidth;
                        byte data8 = ImageData[pos];

                        if (makeTransparent && data8 == 0)
                        {
                            bmp.SetPixel(x, y, Color.Transparent);
                        }
                        else if (makeTransparent && data8 == 1)
                        {
                            bmp.SetPixel(x, y, Color.FromArgb(128, 0, 0, 0));
                        }
                        else
                        {
                            bmp.SetPixel(x, y, pal.Get(data8));
                        }
                    }
                }
            }
            else if (BitsPerPixel == 16)
            {
                // doesn't care about palettes, as its image data is direct color
                for (int x = 0; x < ImageWidth; x++)
                {
                    for (int y = 0; y < ImageHeight; y++)
                    {
                        int    pos    = (x + y * ImageWidth) * 2;
                        ushort data16 = BitConverter.ToUInt16(ImageData, pos);
                        bmp.SetPixel(x, y, Palette_15Bit.ConvertColor(data16));
                    }
                }
            }
            else
            {
                throw new InvalidDataException("Unexpected bits per pixel! (Value = {0})".F(BitsPerPixel));
            }

            if (makeTransparent)
            {
                bmp.MakeTransparent(Palette.Get(0));
            }

            if (includeFrame)
            {
                Bitmap bmpFrame = new Bitmap(FrameWidth, FrameHeight);
                bmpFrame.MakeTransparent();
                Graphics g = Graphics.FromImage(bmpFrame);
                g.DrawImageUnscaled(bmp, new Point((FrameWidth - ImageWidth) / 2, (FrameHeight - ImageHeight) / 2));
                return(bmpFrame);
            }
            else
            {
                return(bmp);
            }
        }