/// <summary> /// Loads IMG file to texture using a subrect of source image. /// Origin of source image (0,0) is bottom-left corner. /// </summary> public static Texture2D GetTextureFromImg(string name, Rect subRect, TextureFormat format = TextureFormat.ARGB32, bool readOnly = true) { ImgFile imgFile = new ImgFile(Path.Combine(DaggerfallUnity.Instance.Arena2Path, name), FileUsage.UseMemory, readOnly); imgFile.LoadPalette(Path.Combine(DaggerfallUnity.Instance.Arena2Path, imgFile.PaletteName)); DFBitmap bitmap = imgFile.GetDFBitmap(); Color32[] colors = imgFile.GetColor32(bitmap, 0); // Invert Y as Unity textures have origin 0,0 at bottom-left and UI expects top-left subRect.y = bitmap.Height - subRect.height; Color32[] newColors = new Color32[(int)subRect.width * (int)subRect.height]; ImageProcessing.CopyColors( ref colors, ref newColors, new DFSize(bitmap.Width, bitmap.Height), new DFSize((int)subRect.width, (int)subRect.height), new DFPosition((int)subRect.x, (int)subRect.y), new DFPosition(0, 0), new DFSize((int)subRect.width, (int)subRect.height)); Texture2D texture = new Texture2D((int)subRect.width, (int)subRect.height, format, false); texture.SetPixels32(newColors, 0); texture.Apply(false, true); texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; return(texture); }
void LoadResources() { // Get background image and store base palette ImgFile img = new ImgFile(Path.Combine(DaggerfallUnity.Arena2Path, backgroundFile), FileUsage.UseMemory, true); backgroundBitmap = img.GetDFBitmap(0, 0); basePalette = backgroundBitmap.Palette; // Get scroll images and set palette GfxFile scroll0 = new GfxFile(Path.Combine(DaggerfallUnity.Arena2Path, scroll0File), FileUsage.UseMemory, true); GfxFile scroll1 = new GfxFile(Path.Combine(DaggerfallUnity.Arena2Path, scroll1File), FileUsage.UseMemory, true); scroll0.Palette = basePalette; scroll1.Palette = basePalette; // Build color buffers for all scroll frames ahead of time scrollFrames = new ScrollFrame[scrollFrameCount]; for (int frame = 0; frame < scrollFrameCount; frame++) { if (frame < 8) { scrollFrames[frame].colors = scroll0.GetColor32(0, frame); } else { scrollFrames[frame].colors = scroll1.GetColor32(0, frame - 8); } } }
public static Texture2D GetTextureFromCifRci(string name, int record, out DFPosition offset, int frame = 0, TextureFormat format = TextureFormat.ARGB32) { offset = new DFPosition(); DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } CifRciFile cifRciFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, name), FileUsage.UseMemory, true); Texture2D texture; if (!TextureReplacement.TryImportCifRci(name, record, frame, out texture)) { cifRciFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifRciFile.PaletteName)); DFBitmap bitmap = cifRciFile.GetDFBitmap(record, frame); texture = new Texture2D(bitmap.Width, bitmap.Height, format, false); texture.SetPixels32(cifRciFile.GetColor32(bitmap, 0)); texture.Apply(false, true); } texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; offset = cifRciFile.GetOffset(record); return(texture); }
/// <summary> /// Sharpen a DFBitmap. /// </summary> /// <param name="bitmap">Source DFBitmap.</param> /// <param name="passes">Number of sharpen passes.</param> /// <returns>Sharpened DFBitmap.</returns> public static DFBitmap Sharpen(DFBitmap bitmap, int passes = 1) { // Must be a colour format if (bitmap.Format == DFBitmap.Formats.Indexed) { return(null); } DFBitmap newBitmap = DFBitmap.CloneDFBitmap(bitmap); for (int i = 0; i < passes; i++) { // Create horizontal sobel matrix Filter sharpenMatrix = new Filter(3, 3); sharpenMatrix.MyFilter[0, 0] = -1; sharpenMatrix.MyFilter[1, 0] = -1; sharpenMatrix.MyFilter[2, 0] = -1; sharpenMatrix.MyFilter[0, 1] = 1; sharpenMatrix.MyFilter[1, 1] = 12; sharpenMatrix.MyFilter[2, 1] = 1; sharpenMatrix.MyFilter[0, 2] = -1; sharpenMatrix.MyFilter[1, 2] = -1; sharpenMatrix.MyFilter[2, 2] = -1; newBitmap = sharpenMatrix.ApplyFilter(newBitmap); } return(newBitmap); }
/// <summary> /// Gets block AutoMap by name. /// </summary> /// <param name="name">Name of block.</param> /// <param name="removeGroundFlats">Filters ground flat "speckles" from the AutoMap.</param> /// <returns>DFBitmap object.</returns> public DFBitmap GetBlockAutoMap(string name, bool removeGroundFlats) { // Test block is valid DFBlock dfBlock = GetBlock(name); if (string.IsNullOrEmpty(dfBlock.Name)) { return(new DFBitmap()); } // Create DFBitmap and copy data DFBitmap dfBitmap = new DFBitmap(); dfBitmap.Data = dfBlock.RmbBlock.FldHeader.AutoMapData; dfBitmap.Width = 64; dfBitmap.Height = 64; // Filter ground flats if specified if (removeGroundFlats) { for (int i = 0; i < dfBitmap.Data.Length; i++) { if (dfBitmap.Data[i] == 0xfb) { dfBitmap.Data[i] = 0x00; } } } return(dfBitmap); }
/// <summary> /// Converts full colour DFBitmap to Color32 array. /// </summary> /// <param name="bitmap">Source DFBitmap.</param> /// <param name="flipY">Flip Y rows so bottom becomes top.</param> /// <returns>Color32 array.</returns> public static Color32[] ConvertToColor32(DFBitmap bitmap, bool flipY = true) { // Must not be indexed if (bitmap.Format == DFBitmap.Formats.Indexed) { return(null); } // Create destination array Color32[] colors = new Color32[bitmap.Width * bitmap.Height]; int index = 0; for (int y = 0; y < bitmap.Height; y++) { int rowOffset = (flipY) ? (bitmap.Height - 1 - y) * bitmap.Stride : y * bitmap.Stride; for (int x = 0; x < bitmap.Width; x++) { // Get color bytes int pos = rowOffset + x * bitmap.FormatWidth; byte r = bitmap.Data[pos++]; byte g = bitmap.Data[pos++]; byte b = bitmap.Data[pos++]; byte a = bitmap.Data[pos]; // Store in array colors[index++] = new Color32(r, g, b, a); } } return(colors); }
/// <summary> /// Tint a weapon image based on metal type. /// </summary> /// <param name="srcBitmap">Source weapon image.</param> /// <param name="size">Image size.</param> /// <param name="metalType">Metal type for tint.</param> public static void TintWeaponImage(DFBitmap srcBitmap, MetalTypes metalType) { // Must be indexed format if (srcBitmap.Format != DFBitmap.Formats.Indexed) { return; } byte[] swaps = GetMetalColors(metalType); int rowPos; for (int y = 0; y < srcBitmap.Height; y++) { rowPos = y * srcBitmap.Width; for (int x = 0; x < srcBitmap.Width; x++) { byte index = srcBitmap.Data[rowPos + x]; if (index >= 0x70 && index <= 0x7f) { int offset = index - 0x70; srcBitmap.Data[rowPos + x] = swaps[offset]; } } } }
/// <summary> /// Pre-multiply bitmap alpha. /// </summary> /// <param name="bitmap">DFBitmap.</param> public static void PreMultiplyAlpha(DFBitmap bitmap) { // Must be a colour format if (bitmap.Format == DFBitmap.Formats.Indexed) { return; } // Pre-multiply alpha for each pixel int pos; float multiplier; for (int y = 0; y < bitmap.Height; y++) { pos = y * bitmap.Stride; for (int x = 0; x < bitmap.Width; x++) { multiplier = bitmap.Data[pos + 3] / 256f; bitmap.Data[pos] = (byte)(bitmap.Data[pos] * multiplier); bitmap.Data[pos + 1] = (byte)(bitmap.Data[pos + 1] * multiplier); bitmap.Data[pos + 2] = (byte)(bitmap.Data[pos + 2] * multiplier); pos += bitmap.FormatWidth; } } }
private static float GetIntensity(DFBitmap bitmap, int x, int y) { // Clamp X if (x < 0) { x = 0; } else if (x >= bitmap.Width) { x = bitmap.Width - 1; } // Clamp Y if (y < 0) { y = 0; } else if (y >= bitmap.Height) { y = bitmap.Height - 1; } // Get position int pos = y * bitmap.Stride + x * bitmap.FormatWidth; // Average intensity float intensity = ((bitmap.Data[pos + 0] + bitmap.Data[pos + 1] + bitmap.Data[pos + 2]) / 3) / 255f; return(intensity); }
/// <summary> /// Load crosshair texture. /// </summary> private void LoadCrosshairTexture() { // Load crosshair image ImageFileReader reader = new ImageFileReader(host.Arena2Path); reader.LibraryType = LibraryTypes.Cif; DFImageFile crosshair = reader.GetImageFile("PNTER.CIF"); DFBitmap crosshairBitmap = crosshair.GetBitmapFormat(4, 0, 0, DFBitmap.Formats.RGBA); // Create texture crosshairTexture = new Texture2D( host.GraphicsDevice, crosshairBitmap.Width, crosshairBitmap.Height, false, SurfaceFormat.Color); // Set data crosshairTexture.SetData <byte>( 0, null, crosshairBitmap.Data, 0, crosshairBitmap.Width * crosshairBitmap.Height * 4); }
/// <summary> /// Gets a Color32 array as emission map based for window textures. /// </summary> /// <param name="srcBitmap">Source bitmap.</param> /// <param name="emissionIndex">Index to receive emission colour.</param> /// <returns>Color32 array.</returns> public Color32[] GetWindowColors32(DFBitmap srcBitmap, int emissionIndex = 0xff) { // Create target array DFSize sz = new DFSize(srcBitmap.Width, srcBitmap.Height); Color32[] emissionColors = new Color32[sz.Width * sz.Height]; // Generate emissive parts of texture based on index int index, srcRow, dstRow; for (int y = 0; y < sz.Height; y++) { // Get row position srcRow = y * sz.Width; dstRow = (sz.Height - 1 - y) * sz.Width; // Write data for this row for (int x = 0; x < sz.Width; x++) { index = srcBitmap.Data[srcRow + x]; if (index == emissionIndex) { emissionColors[dstRow + x] = Color.white; } } } return(emissionColors); }
/// <summary> /// Changes mask index to another index. /// </summary> /// <param name="srcBitmap">Source image.</param> /// <param name="replaceWith">Index to substitute for mask index.</param> /// <returns>New DFBitmap with modified mask.</returns> public static DFBitmap ChangeMask(DFBitmap srcBitmap, byte replaceWith = 0) { const int mask = 0xff; // Handle null bitmap or data input if (srcBitmap == null || srcBitmap.Data == null) { return(new DFBitmap()); } // Clone bitmap DFBitmap dstBitmap = DFBitmap.CloneDFBitmap(srcBitmap, false); // Remove all instances of mask index for (int i = 0; i < srcBitmap.Data.Length; i++) { byte index = srcBitmap.Data[i]; if (index == mask) { dstBitmap.Data[i] = replaceWith; } else { dstBitmap.Data[i] = index; } } return(dstBitmap); }
public override Texture2DContent Import(string filename, ContentImporterContext context) { // Load Arena2Path.txt arena2Path = File.ReadAllText( Path.Combine(Path.GetDirectoryName(filename), Arena2PathTxt)); // Read input text string input = File.ReadAllText(filename); // Remove new lines input = input.Replace('\n', ' ').Trim(); input = input.Replace('\r', ' ').Trim(); // Get source information string[] lines = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string textureFilename = lines[0].Trim(); int record = Convert.ToInt32(lines[1].Trim()); int frame = Convert.ToInt32(lines[2].Trim()); // Get bitmap in RGBA format ImageFileReader fileReader = new ImageFileReader(arena2Path); DFImageFile imageFile = fileReader.LoadFile(textureFilename); DFBitmap dfBitmap = imageFile.GetBitmapFormat(record, frame, 0, DFBitmap.Formats.RGBA); // Set bitmap data BitmapContent bitmapContent = new PixelBitmapContent <Color>(dfBitmap.Width, dfBitmap.Height); bitmapContent.SetPixelData(dfBitmap.Data); Texture2DContent tc = new Texture2DContent(); tc.Faces[0] = bitmapContent; return(tc); }
protected override void Setup() { // Load native texture nativeTexture = DaggerfallUI.GetTextureFromImg(nativeImgName); if (!nativeTexture) { throw new Exception("CreateCharRaceSelect: Could not load native texture."); } // Load picker colours racePickerBitmap = DaggerfallUI.GetImgBitmap(racePickerImgName); // Setup native panel background NativePanel.BackgroundTexture = nativeTexture; // Add "Please select your home province..." prompt promptLabel = DaggerfallUI.AddTextLabel(DaggerfallUI.DefaultFont, new Vector2(0, 16), TextManager.Instance.GetLocalizedText("pleaseSelectYourHomeProvince"), NativePanel); promptLabel.HorizontalAlignment = HorizontalAlignment.Center; promptLabel.TextColor = DaggerfallUI.DaggerfallDefaultTextColor; promptLabel.ShadowColor = DaggerfallUI.DaggerfallDefaultShadowColor; promptLabel.ShadowPosition = DaggerfallUI.DaggerfallDefaultShadowPos; // Handle clicks NativePanel.OnMouseClick += ClickHandler; }
/// <summary> /// Gets a Color32 array for engine with a border. /// </summary> /// <param name="record">Record index.</param> /// <param name="frame">Frame index.</param> /// <param name="alphaIndex">Index to receive transparent alpha.</param> /// <param name="border">Number of pixels border to add around image.</param> /// <param name="sizeOut">Receives image dimensions with borders included.</param> /// <returns>Color32 array.</returns> public Color32[] GetColors32(int record, int frame, int alphaIndex, int border, out DFSize sizeOut) { // Get source bitmap DFBitmap srcBitmap = GetDFBitmap(record, frame); return(GetColors32(ref srcBitmap, alphaIndex, border, out sizeOut)); }
/// <summary> /// Get a preview of the file. /// </summary> /// <param name="Width">Width of preview surface.</param> /// <param name="Height">Height of preview surface.</param> /// <param name="Background">Colour of background.</param> /// <returns>Bitmap object.</returns> public override Bitmap GetPreview(int Width, int Height, Color Background) { // Setup int xpos = 0; int ypos = 0; Bitmap preview = new Bitmap(Width, Height); // Exit if no image loaded if (0 == RecordCount) { return(preview); } // Get graphics object Graphics gr = Graphics.FromImage(preview); gr.Clear(Background); // Get this record and frame DFBitmap dfb = GetDFBitmap(); Bitmap bmp = GetManagedBitmap(ref dfb, true, false); // Copy managed bitmap to preview frame Rectangle srcRect = new Rectangle(0, 0, dfb.Width, dfb.Height); Rectangle dstRect = new Rectangle(xpos, ypos, dfb.Width, dfb.Height); gr.DrawImage(bmp, dstRect, srcRect, GraphicsUnit.Pixel); return(preview); }
/// <summary> /// Gets a Color32 array for engine with minimum options. /// </summary> /// <param name="record">Index of record.</param> /// <param name="frame">Index of frame.</param> /// <param name="alphaIndex">Index to receive transparent alpha.</param> /// <returns>Color32 array.</returns> public Color32[] GetColors32(int record, int frame, int alphaIndex = -1) { DFBitmap srcBitmap = GetDFBitmap(record, frame); DFSize sz; return(GetColors32(ref srcBitmap, alphaIndex, 0, out sz)); }
/// <summary> /// Get extracted PAK data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Width = pakWidthValue; DFBitmap.Height = PakRowCount; DFBitmap.Data = pakExtractedBuffer; return(DFBitmap); }
Texture2D GetTextureFromImg(ImgFile img) { DFBitmap bitmap = img.GetDFBitmap(); Texture2D texture = new Texture2D(bitmap.Width, bitmap.Height, TextureFormat.ARGB32, false); texture.SetPixels32(img.GetColor32(bitmap, 0)); texture.Apply(false, true); return(texture); }
/// <summary> /// Get extracted heightmap data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetHeightMapDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Width = mapWidthValue; DFBitmap.Height = mapHeightValue; DFBitmap.Data = heightMapBuffer; return(DFBitmap); }
/// <summary> /// Get extracted PAK data as a managed bitmap. /// </summary> /// <returns>Bitmap object.</returns> public Bitmap GetManagedBitmap() { DFBitmap bmp = GetDFBitmap(); DFImageFile img = new ImgFile(); DFPalette pal = new DFPalette(); pal.MakeGrayscale(); img.Palette = pal; return(img.GetManagedBitmap(ref bmp, true, false)); }
public static Texture2D GetTextureFromImg(ImgFile img, TextureFormat format = TextureFormat.ARGB32, bool readOnly = true) { DFBitmap bitmap = img.GetDFBitmap(); Texture2D texture = new Texture2D(bitmap.Width, bitmap.Height, format, false); texture.SetPixels32(img.GetColor32(bitmap, 0)); texture.Apply(false, readOnly); texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; return(texture); }
/// <summary> /// Get extracted heightmap data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetHeightMapDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Format = DFBitmap.Formats.Indexed; DFBitmap.Width = MapWidthValue; DFBitmap.Height = MapHeightValue; DFBitmap.Stride = MapWidthValue; DFBitmap.Data = HeightMapBuffer; return(DFBitmap); }
// Read image data for all frames void ReadImageData(BinaryReader reader) { frames = new DFBitmap[header.FrameCount]; for (int i = 0; i < header.FrameCount; i++) { DFBitmap bitmap = new DFBitmap(header.Width, header.Height); bitmap.Data = reader.ReadBytes(header.Width * header.Height); bitmap.Palette = Palette; frames[i] = bitmap; } }
/// <summary> /// Get extracted PAK data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Format = DFBitmap.Formats.Indexed; DFBitmap.Width = pakWidthValue; DFBitmap.Height = PakRowCount; DFBitmap.Stride = pakWidthValue; DFBitmap.Data = pakExtractedBuffer; return(DFBitmap); }
/// <summary> /// Dye supported bitmap image based on dye index and type. /// </summary> /// <param name="srcBitmap">Source image.</param> /// <param name="dye">Dye index.</param> /// <param name="target">Dye target.</param> /// <returns>New DFBitmap dyed to specified colour.</returns> public static DFBitmap ChangeDye(DFBitmap srcBitmap, DyeColors dye, DyeTargets target) { const int clothingStart = 0x60; const int weaponsAndArmorStart = 0x70; // Clone bitmap and get colour table for swaps DFBitmap dstBitmap = DFBitmap.CloneDFBitmap(srcBitmap, false); byte[] swaps = GetDyeColorTable(dye, target); // Swaps range is based on target type int start; switch (target) { case DyeTargets.Clothing: start = clothingStart; break; case DyeTargets.WeaponsAndArmor: start = weaponsAndArmorStart; break; default: return(dstBitmap); } // Swap indices start through start + 15 with colour table int rowPos; for (int y = 0; y < srcBitmap.Height; y++) { rowPos = y * srcBitmap.Width; for (int x = 0; x < srcBitmap.Width; x++) { int srcOffset = rowPos + x; byte index = srcBitmap.Data[srcOffset]; if (index >= start && index <= start + 0x0f) { int tintOffset = index - start; dstBitmap.Data[srcOffset] = swaps[tintOffset]; } else { dstBitmap.Data[srcOffset] = index; } } } return(dstBitmap); }
/// <summary> /// Reads image data. /// </summary> /// <param name="index">Index of image.</param> private bool ReadImageData(int index) { // Read image if not already stored if (null == bitmaps[index]) { BinaryReader Reader = managedFile.GetReader(imageDataPosition + (frameDataLength * index)); bitmaps[index] = new DFBitmap(); bitmaps[index].Width = frameWidth; bitmaps[index].Height = frameHeight; bitmaps[index].Data = Reader.ReadBytes(frameDataLength); } return(true); }
/// <summary> /// Get a preview of the file. As many images as possible will be laid out onto the preview surface. /// </summary> /// <param name="Width">Width of preview surface.</param> /// <param name="Height">Height of preview surface.</param> /// <param name="Background">Colour of background.</param> /// <returns>Bitmap object.</returns> public override Bitmap GetPreview(int Width, int Height, Color Background) { // Setup int xpos = 0; int ypos = 0; int Record = 0; int RowMaxHeight = 0; Bitmap preview = new Bitmap(Width, Height); // Get graphics object Graphics gr = Graphics.FromImage(preview); gr.Clear(Background); do { // Exit if no more records if (Record >= RecordCount) { break; } // Get this record and frame DFBitmap dfb = GetDFBitmap(Record++, 0); Bitmap bmp = GetManagedBitmap(ref dfb, true, false); // Update max height for this row if (dfb.Height > RowMaxHeight) { RowMaxHeight = dfb.Height; } // Copy managed bitmap to preview frame Rectangle srcRect = new Rectangle(0, 0, dfb.Width, dfb.Height); Rectangle dstRect = new Rectangle(xpos, ypos, dfb.Width, dfb.Height); gr.DrawImage(bmp, dstRect, srcRect, GraphicsUnit.Pixel); // Increment X position with wrap around xpos += (dfb.Width + 1); if (xpos >= Width) { xpos = 0; ypos += (RowMaxHeight + 1); RowMaxHeight = 0; } } while (xpos < Width && ypos < Height); return(preview); }
/// <summary> /// Gets managed bitmap from specified record and frame. /// </summary> /// <param name="Record">Record index.</param> /// <param name="Frame">Frame index.</param> /// <param name="IndexedColour">True to maintain idexed colour, false to return RGB bitmap.</param> /// <param name="MakeTransparent">True to make colour 0x000000 transparent, otherwise false.</param> /// <returns>Bitmap object.</returns> public override Bitmap GetManagedBitmap(int Record, int Frame, bool IndexedColour, bool MakeTransparent) { // Get image DFBitmap frame = GetDFBitmap(Record, Frame); if (null == frame.Data) { return(new Bitmap(4, 4)); } // Set base palette base.MyPalette = GetDFPalette(Frame); return(base.GetManagedBitmap(ref frame, IndexedColour, MakeTransparent)); }
public override DFBitmap GetDFBitmap(int record, int frame) { DFBitmap result = new DFBitmap(); if (record == 0 && frame == 0 && managedFile.Length > 0) { BinaryReader reader = managedFile.GetReader(); byte[] data = reader.ReadBytes(managedFile.Length); DFSize sz = GetSize(0); result.Width = sz.Width; result.Height = sz.Height; result.Data = data; } return(result); }
/// <summary> /// Gets a Color32 array as emission map based for window textures. /// </summary> /// <param name="srcBitmap">Source bitmap.</param> /// <param name="emissionIndex">Index to receive emission colour.</param> /// <returns>Color32 array.</returns> public Color32[] GetWindowColors32(DFBitmap srcBitmap, int emissionIndex = 0xff) { // Must be an indexed format if (srcBitmap.Format != DFBitmap.Formats.Indexed) return null; // Create target array DFSize sz = new DFSize(srcBitmap.Width, srcBitmap.Height); Color32[] emissionColors = new Color32[sz.Width * sz.Height]; // Generate emissive parts of texture based on index int index, srcRow, dstRow; for (int y = 0; y < sz.Height; y++) { // Get row position srcRow = y * sz.Width; dstRow = (sz.Height - 1 - y) * sz.Width; // Write data for this row for (int x = 0; x < sz.Width; x++) { index = srcBitmap.Data[srcRow + x]; if (index == emissionIndex) emissionColors[dstRow + x] = Color.white; } } return emissionColors; }
/// <summary> /// Gets a Color32 array for engine with a border. /// </summary> /// <param name="srcBitmap">Source DFBitmap.</param> /// <param name="alphaIndex">Index to receive transparent alpha.</param> /// <param name="border">Number of pixels border to add around image.</param> /// <param name="sizeOut">Receives image dimensions with borders included.</param> /// <returns>Color32 array.</returns> public Color32[] GetColors32(DFBitmap srcBitmap, int alphaIndex, int border, out DFSize sizeOut) { // Must be an indexed format if (srcBitmap.Format != DFBitmap.Formats.Indexed) { sizeOut = new DFSize(); return null; } // Calculate dimensions int srcWidth = srcBitmap.Width; int srcHeight = srcBitmap.Height; int dstWidth = srcWidth + border * 2; int dstHeight = srcHeight + border * 2; // Create target array Color32[] colors = new Color32[dstWidth * dstHeight]; DFColor c; byte a; int index, srcRow, dstRow; for (int y = 0; y < srcHeight; y++) { // Get row position srcRow = y * srcWidth; dstRow = (dstHeight - 1 - border - y) * dstWidth; // Write data for this row for (int x = 0; x < srcWidth; x++) { index = srcBitmap.Data[srcRow + x]; c = myPalette.Get(index); if (alphaIndex == index) a = 0x00; else a = 0xff; colors[dstRow + border + x] = new Color32(c.R, c.G, c.B, a); } } sizeOut = new DFSize(dstWidth, dstHeight); return colors; }
/// <summary> /// Gets a Color32 array for engine with minimum options. /// </summary> /// <param name="srcBitmap">Source DFBitmap.</param> /// <param name="alphaIndex">Index to receive transparent alpha.</param> /// <returns>Color32 array.</returns> public Color32[] GetColors32(DFBitmap srcBitmap, int alphaIndex = -1) { DFSize sz; return GetColors32(srcBitmap, alphaIndex, 0, out sz); }
/// <summary> /// Get raw bytes for specified record and frame using a custom pixel format. /// </summary> /// <param name="record">Index of record.</param> /// <param name="frame">Index of frame.</param> /// <param name="alphaIndex">Index of alpha colour.</param> /// <param name="format">Specified pixel format to use.</param> /// <returns>DFBitmap object.</returns> public DFBitmap GetBitmapFormat(int record, int frame, byte alphaIndex, DFBitmap.Formats format) { // Get as indexed image if (format == DFBitmap.Formats.Indexed) return GetDFBitmap(record, frame); // Create new bitmap const int formatWidth = 4; DFBitmap srcBitmap = GetDFBitmap(record, frame); DFBitmap dstBitmap = new DFBitmap(); dstBitmap.Format = format; dstBitmap.Width = srcBitmap.Width; dstBitmap.Height = srcBitmap.Height; dstBitmap.Stride = dstBitmap.Width * formatWidth; dstBitmap.Data = new byte[dstBitmap.Stride * dstBitmap.Height]; // Write pixel data to array byte a, r, g, b; int srcPos = 0, dstPos = 0; for (int i = 0; i < dstBitmap.Width * dstBitmap.Height; i++) { // Write colour values byte index = srcBitmap.Data[srcPos++]; if (index != alphaIndex) { // Get colour values a = 0xff; r = myPalette.GetRed(index); g = myPalette.GetGreen(index); b = myPalette.GetBlue(index); // Write colour values switch (format) { case DFBitmap.Formats.RGBA: dstBitmap.Data[dstPos++] = r; dstBitmap.Data[dstPos++] = g; dstBitmap.Data[dstPos++] = b; dstBitmap.Data[dstPos++] = a; break; case DFBitmap.Formats.ARGB: dstBitmap.Data[dstPos++] = a; dstBitmap.Data[dstPos++] = r; dstBitmap.Data[dstPos++] = g; dstBitmap.Data[dstPos++] = b; break; default: throw new Exception("Unknown output format."); } } else { // Step over alpha pixels dstPos += formatWidth; } } return dstBitmap; }
/// <summary> /// Get extracted heightmap data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetHeightMapDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Format = DFBitmap.Formats.Indexed; DFBitmap.Width = mapWidthValue; DFBitmap.Height = mapHeightValue; DFBitmap.Stride = mapWidthValue; DFBitmap.Data = heightMapBuffer; return DFBitmap; }
/// <summary> /// Get extracted PAK data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Width = pakWidthValue; DFBitmap.Height = PakRowCount; DFBitmap.Data = pakExtractedBuffer; return DFBitmap; }
/// <summary> /// Reads image data. /// </summary> /// <param name="index">Index of image.</param> private bool ReadImageData(int index) { // Read image if not already stored if (null == bitmaps[index]) { BinaryReader Reader = managedFile.GetReader(imageDataPosition + (frameDataLength * index)); bitmaps[index] = new DFBitmap(); bitmaps[index].Width = frameWidth; bitmaps[index].Height = frameHeight; bitmaps[index].Data = Reader.ReadBytes(frameDataLength); } return true; }
/// <summary> /// Gets block AutoMap by name. /// </summary> /// <param name="name">Name of block.</param> /// <param name="removeGroundFlats">Filters ground flat "speckles" from the AutoMap.</param> /// <returns>DFBitmap object.</returns> public DFBitmap GetBlockAutoMap(string name, bool removeGroundFlats) { // Test block is valid DFBlock dfBlock = GetBlock(name); if (string.IsNullOrEmpty(dfBlock.Name)) return new DFBitmap(); // Create DFBitmap and copy data DFBitmap dfBitmap = new DFBitmap(); dfBitmap.Data = dfBlock.RmbBlock.FldHeader.AutoMapData; dfBitmap.Width = 64; dfBitmap.Height = 64; // Filter ground flats if specified if (removeGroundFlats) { for (int i = 0; i < dfBitmap.Data.Length; i++) { if (dfBitmap.Data[i] == 0xfb) dfBitmap.Data[i] = 0x00; } } return dfBitmap; }
/// <summary> /// Get extracted heightmap data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetHeightMapDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Width = mapWidthValue; DFBitmap.Height = mapHeightValue; DFBitmap.Data = heightMapBuffer; return DFBitmap; }
/// <summary> /// Gets a Color32 array for engine. /// </summary> /// <param name="srcBitmap">Source DFBitmap.</param> /// <param name="alphaIndex">Index to receive transparent alpha.</param> /// <param name="border">Number of pixels border to add around image.</param> /// <param name="sizeOut">Receives image dimensions with borders included.</param> /// <returns>Color32 array.</returns> public Color32[] GetColor32(DFBitmap srcBitmap, int alphaIndex, int border, out DFSize sizeOut) { // Calculate dimensions int srcWidth = srcBitmap.Width; int srcHeight = srcBitmap.Height; int dstWidth = srcWidth + border * 2; int dstHeight = srcHeight + border * 2; Color32[] colors = new Color32[dstWidth * dstHeight]; Color32 c = new Color32(); int index, offset, srcRow, dstRow; byte[] paletteData = myPalette.PaletteBuffer; for (int y = 0; y < srcHeight; y++) { // Get row position srcRow = y * srcWidth; dstRow = (dstHeight - 1 - border - y) * dstWidth; // Write data for this row for (int x = 0; x < srcWidth; x++) { index = srcBitmap.Data[srcRow + x]; offset = myPalette.HeaderLength + index * 3; c.r = paletteData[offset]; c.g = paletteData[offset + 1]; c.b = paletteData[offset + 2]; c.a = (alphaIndex == index) ? (byte)0 : (byte)255; colors[dstRow + border + x] = c; } } sizeOut = new DFSize(dstWidth, dstHeight); return colors; }
/// <summary> /// Get extracted PAK data as an indexed image. /// </summary> /// <returns>DFBitmap object.</returns> public DFBitmap GetDFBitmap() { DFBitmap DFBitmap = new DFBitmap(); DFBitmap.Format = DFBitmap.Formats.Indexed; DFBitmap.Width = pakWidthValue; DFBitmap.Height = PakRowCount; DFBitmap.Stride = pakWidthValue; DFBitmap.Data = pakExtractedBuffer; return DFBitmap; }