示例#1
0
        // revert graffiti image to original
        private void btn_restore_Click(object sender, EventArgs e)
        {
            if (txtb_jsrf_original_dir.Text == "")
            {
                MessageBox.Show("JSRF TEX folder (original files) in Settings is not defined, please select the folder."); tabControl1.SelectedIndex = 1; return;
            }
            if (!Directory.Exists(txtb_jsrf_original_dir.Text))
            {
                MessageBox.Show("Error: \"JSRF folder: original files\" in Settings does not exist."); return;
            }

            string subolfder    = "";
            int    split_dir_at = 0;

            string filepath = get_selected_graffiti_filepath();

            if (filepath == "")
            {
                return;
            }


            string[] folders = filepath.Split(Path.DirectorySeparatorChar);

            // find the "Media" folder in path and substract the parent folders from it
            for (int i = 0; i < folders.Length; i++)
            {
                if (folders[i] == "TEX")
                {
                    split_dir_at = i;
                }
            }
            for (int i = split_dir_at + 1; i < folders.Length; i++)
            {
                subolfder = subolfder + "\\" + folders[i];
            }


            string ori_filepath       = txtb_jsrf_original_dir.Text.TrimEnd(Path.DirectorySeparatorChar) + subolfder;
            string ori_filepath_noExt = ori_filepath.Replace(".tga", "");

            if (!File.Exists(ori_filepath_noExt + ".grf"))
            {
                MessageBox.Show("Error: original file (" + ori_filepath_noExt + ".grf" + ") does not exist."); return;
            }
            if (!File.Exists(ori_filepath_noExt + ".prs"))
            {
                MessageBox.Show("Error: original file (" + ori_filepath_noExt + ".prs" + ") does not exist."); return;
            }


            // replace the modded file by a copy of the original
            File.Copy(ori_filepath_noExt + ".grf", filepath, true);
            File.Copy(ori_filepath_noExt + ".grf", filepath.Replace(".tga", ".grf"), true);
            File.Copy(ori_filepath_noExt + ".prs", filepath.Replace(".tga", ".prs"), true);

            int size = Convert.ToInt16(Path.GetFileNameWithoutExtension(filepath).Split('_')[1]);

            // re-import graffiti picturebox item
            for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
            {
                Graffiti_PicBox item = (Graffiti_PicBox)flowLayoutPanel1.Controls[i];

                if (item.filepath == filepath)
                {
                    // convert to bmp so we can load it in the picturebox
                    img_convert(Path.GetFileNameWithoutExtension(filepath), tex_dir, "tga", "png", "");

                    // Graffiti_PicBox import = load_image_to_graffitiPicBox(filepath);

                    #region generate bitmap thumbnail

                    Bitmap bitmap       = LoadBitmap(filepath.Replace(".tga", ".png"));
                    Bitmap bitmap_thumb = bitmap;

                    // scale picturebox depending on the graffiti size
                    switch (size)
                    {
                    case 0:
                    case 1:
                        bitmap_thumb = new Bitmap(bitmap);
                        break;

                    case 2:
                        bitmap_thumb = new Bitmap(bitmap);
                        break;

                    case 3:
                        // resize image to save memory space
                        bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 3, bitmap.Height / 3));
                        break;

                    case 4:
                        // resize image to save memory space
                        bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 4, bitmap.Height / 4));
                        break;
                    }
                    bitmap.Dispose();

                    #endregion

                    // set image
                    item.set_image(bitmap_thumb);
                    item.Update();
                    break;
                }
            }



            System.Media.SystemSounds.Beep.Play();
        }
示例#2
0
        // imports .tga image as .bmp and update to corresponding graffiti picturebox (determined by file name)
        // and convert .tga to .grf
        private void btn_import_Click(object sender, EventArgs e)
        {
            string filepath = get_selected_graffiti_filepath();

            if (filepath == "")
            {
                return;
            }
            if (!File.Exists(filepath))
            {
                MessageBox.Show("Could not find file: \n" + filepath); return;
            }

            int size = Convert.ToInt16(Path.GetFileNameWithoutExtension(filepath).Split('_')[1]);


            #region check tga dimensions are correct

            //read TGA file to get X Y dimensions
            string tga_size;
            using (BinaryReader b = new BinaryReader(File.Open(filepath, FileMode.Open)))
            {
                // jump headers and stuff directly to what we want
                b.BaseStream.Position = 12;
                tga_size = b.ReadInt16().ToString() + " " + b.ReadInt16().ToString();
                b.Close();
                b.Dispose();
            }

            // if texture size from .tga doesn't match the selected graffiti size warn user and return
            if (tga_size != texture_sizes[size])
            {
                MessageBox.Show("Texture resolution for this graffiti size should be " + texture_sizes[size].Replace(" ", " by ") + " pixels.", "Error: invalid texture resolution.");
                return;
            }

            #endregion


            // re-import graffiti picturebox item
            for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
            {
                Graffiti_PicBox item = (Graffiti_PicBox)flowLayoutPanel1.Controls[i];

                if (item.filepath == filepath)
                {
                    // convert to bmp so we can load it in the picturebox
                    img_convert(Path.GetFileNameWithoutExtension(filepath), tex_dir, "tga", "png", "");

                    // Graffiti_PicBox import = load_image_to_graffitiPicBox(filepath);

                    #region generate bitmap thumbnail

                    Bitmap bitmap       = LoadBitmap(filepath.Replace(".tga", ".png"));
                    Bitmap bitmap_thumb = bitmap;

                    // scale picturebox depending on the graffiti size
                    switch (size)
                    {
                    case 0:
                    case 1:
                        bitmap_thumb = new Bitmap(bitmap);
                        break;

                    case 2:
                        bitmap_thumb = new Bitmap(bitmap);
                        break;

                    case 3:
                        // resize image to save memory space
                        bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 3, bitmap.Height / 3));
                        break;

                    case 4:
                        // resize image to save memory space
                        bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 4, bitmap.Height / 4));
                        break;
                    }
                    bitmap.Dispose();

                    #endregion

                    // set image
                    item.set_image(bitmap_thumb);
                    item.Update();
                    break;
                }
            }

            string filepath_noExt = filepath.Replace(".tga", "");

            IO.delete_safe(filepath_noExt + ".dds");

            // convert to DDS DXT3 (no mipmaps)
            img_convert(Path.GetFileNameWithoutExtension(filepath), tex_dir, "tga", "dds", "-format=DXT3");

            if (!File.Exists(filepath_noExt + ".dds"))
            {
                MessageBox.Show("Could not convert .tga to .dds", "Error");
                return;
            }

            IO.delete_safe(filepath_noExt + ".prs");
            // rename dds to prs
            File.Move(filepath_noExt + ".dds", filepath_noExt + ".prs");

            // save .tga as .grf
            IO.delete_safe(filepath_noExt + ".grf");
            // rename dds to prs
            File.Copy(filepath_noExt + ".tga", filepath_noExt + ".grf");

            // remove DDS header (128 bytes)
            try
            {
                byte[] bytes    = System.IO.File.ReadAllBytes(filepath_noExt + ".prs");
                byte[] headless = new byte[bytes.Length - 128];

                Array.Copy(bytes, 128, headless, 0, bytes.Length - 128);

                System.IO.File.WriteAllBytes(filepath_noExt + ".prs", headless);
            }
            catch (System.Exception excep)
            {
                MessageBox.Show("Failed converting dds to prs: " + excep.Message);
                return;
            }


            System.Media.SystemSounds.Beep.Play();
        }