Пример #1
0
        /// <summary>
        /// The btn save image_ click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        /// <remarks></remarks>
        private void btnSaveImage_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveImage = new SaveFileDialog();
            saveImage.InitialDirectory = Prefs.pathBitmapsFolder;
            saveImage.DefaultExt = "dds";
            saveImage.Filter = "DDS files (*.dds)|*.dds";
            if (saveImage.ShowDialog() == DialogResult.OK)
            {
                Prefs.pathBitmapsFolder = saveImage.FileName.Substring(0, saveImage.FileName.LastIndexOf('\\'));

                DDS.DDS_HEADER_STRUCTURE dds = new DDS.DDS_HEADER_STRUCTURE();
                ParsedBitmap.BitmapInfo bi = new ParsedBitmap.BitmapInfo(
                    ParsedBitmap.BitmapFormat.BITM_FORMAT_A8R8G8B8, false);
                bi.width = 224;
                bi.height = 207;
                bi.depth = 1;
                bi.bitsPerPixel = 32;
                dds.generate(ref bi);
                FileStream fs = new FileStream(saveImage.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                dds.WriteStruct(ref bw);
                bw.Write(
                    currentLevels[lbMapListing.SelectedIndex].screenShotRaw.MS.ToArray(),
                    0,
                    (int)currentLevels[lbMapListing.SelectedIndex].screenShotRaw.MS.Length);
                bw.Close();
                fs.Close();
            }
        }
Пример #2
0
        /// <summary>
        /// The btn load image_ click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        /// <remarks></remarks>
        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog getImage = new OpenFileDialog();
            getImage.InitialDirectory = Prefs.pathBitmapsFolder;
            getImage.DefaultExt = "dds";
            getImage.Filter = "DDS & Map files|*.dds;*.map|DDS files (*.dds)|*.dds|Map Files (*.map)|*.map";
            if (getImage.ShowDialog() == DialogResult.OK)
            {
                Prefs.pathBitmapsFolder = getImage.FileName.Substring(0, getImage.FileName.LastIndexOf('\\'));

                if (getImage.FileName.EndsWith("dds", StringComparison.OrdinalIgnoreCase))
                {

                    FileStream fs = new FileStream(getImage.FileName, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);

                    DDS.DDS_HEADER_STRUCTURE dds = new DDS.DDS_HEADER_STRUCTURE();
                    dds.ReadStruct(ref br);

                    int tempsize = dds.ddsd.width * dds.ddsd.height;
                    switch (dds.ddsd.ddfPixelFormat.FourCC)
                    {
                        case "DXT1":
                            tempsize /= 2;
                            break;
                        case "DXT2":
                        case "DXT3":
                        case "DXT4":
                        case "DXT5":

                            // tempsize /= 1;
                            break;

                            // for non-compressed
                        default:
                            tempsize *= dds.ddsd.ddfPixelFormat.RGBBitCount >> 3;
                            break;
                    }

                    int widthPad = 0;
                    if (dds.ddsd.width % 16 != 0)
                        widthPad = 16 - dds.ddsd.width % 16;

                    int byteStep = dds.ddsd.ddfPixelFormat.RGBBitCount / 8;
                    byte[] guh = new byte[tempsize + (dds.ddsd.height * widthPad * byteStep)];
                    if (widthPad == 0)
                    {
                        br.BaseStream.Read(guh, 0, guh.Length);
                    }
                    else
                    {
                        // Change data to include padding
                        for (int h = 0; h < dds.ddsd.height; h++)
                        {
                            br.BaseStream.Read(
                                guh, h * (dds.ddsd.width + widthPad) * byteStep, dds.ddsd.width * byteStep);
                        }
                    }

                    br.Close();
                    fs.Close();

                    int tagNum =
                        map.Functions.ForMeta.FindMetaByID(
                            currentLevels[lbMapListing.SelectedIndex].Preview_Image_Ident);
                    if (tagNum != -1)
                    {
                        map.SelectedMeta = Map.GetMetaFromTagIndex(tagNum, map, false, false);
                    }

                    #region Confirm 224x207 A8R8G8B8 file and save to CurrentLevel[].ScreenShot & Cur...[].ScreenShotRaw

                    ParsedBitmap.BitmapFormat bmf = DDS.getBitmapFormat(dds);
                    if ((bmf == ParsedBitmap.BitmapFormat.BITM_FORMAT_A8R8G8B8) && (dds.ddsd.width + widthPad == 224) &&
                        (dds.ddsd.height == 207))
                    {
                        currentLevels[lbMapListing.SelectedIndex].screenShotRaw.MS.Position = 0;
                        currentLevels[lbMapListing.SelectedIndex].screenShotRaw.MS.Write(
                            guh, 0, (int)Math.Min(map.SelectedMeta.raw.rawChunks[0].MS.Length, guh.Length));
                        currentLevels[lbMapListing.SelectedIndex].screenShotRaw.size =
                            (int)currentLevels[lbMapListing.SelectedIndex].screenShotRaw.MS.Length;
                        currentLevels[lbMapListing.SelectedIndex].screenShot = ParsedBitmap.DecodeBitm(
                            guh,
                            dds.ddsd.height,
                            dds.ddsd.width,
                            dds.ddsd.depth,
                            dds.ddsd.ddfPixelFormat.RGBBitCount,
                            ParsedBitmap.BitmapType.BITM_TYPE_2D,
                            bmf,
                            false,
                            null,
                            -1,
                            -1);
                        pbMapBitmap.Image = currentLevels[lbMapListing.SelectedIndex].screenShot;
                    }
                    else
                    {
                        MessageBox.Show("Bitmap must be a 224x207 A8R8G8B8 file");
                    }

                    #endregion
                }
                else if (getImage.FileName.EndsWith("map", StringComparison.OrdinalIgnoreCase))
                {
                    this.Enabled = false;
                    Map tempmap = Map.LoadFromFile(getImage.FileName);
                    this.Enabled = true;
                    if (tempmap == null)
                    {
                        MessageBox.Show("Unable to load image from map: " + getImage.FileName);
                        return;
                    }

                    // Load Map Picture as raw data (for transfer) and Bitmap (for display)
                    tempmap.OpenMap(MapTypes.Internal);
                    Meta m = MapForm.GetMapBitmapMeta(tempmap);

                    //////////////////////////////////
                    // Backwash for example has no internal picture, so it errors.
                    //////////////////////////////////
                    if (m != null)
                    {
                        currentLevels[lbMapListing.SelectedIndex].screenShotRaw = m.raw.rawChunks[0];
                        ParsedBitmap pm = new ParsedBitmap(ref m, tempmap);

                        currentLevels[lbMapListing.SelectedIndex].screenShot = pm.FindChunkAndDecode(
                            0, 0, 0, ref m, tempmap, 0, 0);
                    }
                    else
                    {
                        // No map listing found
                    }

                    tempmap.CloseMap();
                    pbMapBitmap.Image = currentLevels[lbMapListing.SelectedIndex].screenShot;
                }
            }
        }