public static void SaveImage(Stream stream, FreeImageBitmap bitmap) { string comment = bitmap.Comment; var vspHeader = new VspHeader(); if (!String.IsNullOrEmpty(comment)) { vspHeader.ParseComment(comment); } if (bitmap.ColorDepth != 4 && bitmap.ColorDepth != 8) { if (bitmap.ColorDepth > 8) { if (bitmap.ColorDepth == 32) { bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP); } if (vspHeader.is8Bit == 0) { bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 16); } else { bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 256); } } //throw new ArgumentException("image must be 4-bit or 8-bit"); } if ((bitmap.Width & 7) != 0) { int slackPixels = (bitmap.Width & 7); int pixelsToAdd = 8 - slackPixels; bitmap.EnlargeCanvas <RGBQUAD>(0, 0, pixelsToAdd, 0, new RGBQUAD(Color.Black)); } if (vspHeader.is8Bit == 0) { vspHeader.height = bitmap.Height; vspHeader.width = bitmap.Width / 8; SaveHeader(vspHeader, stream); SavePalette(bitmap.Palette, stream); SaveImageData(stream, bitmap); } else { vspHeader.height = bitmap.Height; if (vspHeader.width != bitmap.Width - 1) { } vspHeader.width = bitmap.Width - 1; SaveHeader(vspHeader, stream); var pmsHeader = vspHeader.ToPmsHeader(); Pms.SavePalette(stream, bitmap.Palette); Pms.SaveImageData8Bit(stream, bitmap); } }
public static void RemapPalette(FreeImageBitmap bitmap, Palette newPalette) { if (bitmap.ColorDepth > 8) { bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 256, newPalette); } var sourcePal = bitmap.Palette.AsArray.Select(c => c.uintValue).ToArray(); var destPal = newPalette.AsArray.Select(c => c.uintValue).ToArray(); int[] srcToDest = new int[256]; for (int i = 0; i < 256; i++) { srcToDest[i] = -1; } //first map identical colors { Dictionary <int, int> rgbToPaletteIndex = new Dictionary <int, int>(); for (int i = 0; i < 256; i++) { int c = (int)(destPal[i] & 0xFFFFFF); if (!rgbToPaletteIndex.ContainsKey(c)) { rgbToPaletteIndex.Add(c, i); } } for (int i = 0; i < 256; i++) { int c = (int)(sourcePal[i] & 0xFFFFFF); if (rgbToPaletteIndex.ContainsKey(c)) { srcToDest[i] = rgbToPaletteIndex[c]; } } } //map remaining colors { for (int i = 0; i < 256; i++) { if (srcToDest[i] == -1) { int c = (int)(sourcePal[i] & 0xFFFFFF); int minDistance = int.MaxValue; int minIndex = -1; for (int j = 0; j < 256; j++) { int c2 = (int)(destPal[j] & 0xFFFFFF); int distance = GetDistance(c, c2); if (distance < minDistance) { minDistance = distance; minIndex = j; } } srcToDest[i] = minIndex; } } } byte[] sequence = new byte[256]; byte[] srcToDestByte = new byte[256]; { for (int i = 0; i < 256; i++) { sequence[i] = (byte)i; srcToDestByte[i] = (byte)srcToDest[i]; } } bitmap.ApplyPaletteIndexMapping(sequence, srcToDestByte, 256, false); bitmap.Palette.AsArray = newPalette.AsArray; }
public static void SaveImage(Stream stream, FreeImageBitmap bitmap) { string comment = bitmap.Comment; var pmsHeader = new PmsHeader(); bool readComment = false; if (!string.IsNullOrEmpty(comment)) { if (pmsHeader.ParseComment(comment)) { readComment = true; } } if (pmsHeader.colorDepth == 0) { pmsHeader.colorDepth = 8; } bool is8Bit = !readComment || pmsHeader.colorDepth == 8; if (!readComment) { pmsHeader.version = 1; pmsHeader.headerSize = 48; } if (pmsHeader.signature == 0) { pmsHeader.signature = 0x00004d50; } if (pmsHeader.version == 2 && pmsHeader.headerSize == 0) { pmsHeader.headerSize = 64; } if (pmsHeader.headerSize < 48) { pmsHeader.headerSize = 48; } if (pmsHeader.headerSize > 64) { pmsHeader.headerSize = 64; } pmsHeader.addressOfComment = 0; pmsHeader.height = bitmap.Height; pmsHeader.width = bitmap.Width; if (is8Bit) { if (bitmap.ColorDepth > 8) { if (bitmap.ColorDepth == 32) { bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP); } bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 256); //throw new ArgumentException("image must be 8-bit"); } pmsHeader.addressOfPalette = pmsHeader.headerSize; pmsHeader.addressOfData = pmsHeader.addressOfPalette + 768; pmsHeader.colorDepth = 8; SaveHeader(stream, pmsHeader); SavePalette(stream, bitmap.Palette); SaveImageData8Bit(stream, bitmap); } else { bool hasAlphaChannel = false; var existingAlphaChannel = bitmap.GetChannel(FREE_IMAGE_COLOR_CHANNEL.FICC_ALPHA); if (existingAlphaChannel != null) { bool allPixelsOpaque = AllPixelsOpaque(existingAlphaChannel); hasAlphaChannel = !allPixelsOpaque; } if (bitmap.ColorDepth != 32) { bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP); } bool usingAlphaChannel = pmsHeader.shadowDepth == 8 || (pmsHeader.shadowDepth == 0 && hasAlphaChannel); pmsHeader.addressOfPalette = 0; pmsHeader.addressOfData = pmsHeader.headerSize; pmsHeader.colorDepth = 16; ushort[] image16 = new ushort[pmsHeader.width * pmsHeader.height]; byte[] image8 = null; if (usingAlphaChannel) { image8 = new byte[pmsHeader.width * pmsHeader.height]; } int o = 0; for (int y = 0; y < pmsHeader.height; y++) { var scanline = bitmap.GetScanlineFromTop32Bit(y); if (image8 == null) { for (int x = 0; x < pmsHeader.width; x++) { unchecked { int p = scanline[x]; int b = (p >> 0) & 0xFF; int g = (p >> 8) & 0xFF; int r = (p >> 16) & 0xFF; //int a = (p >> 24) & 0xFF; b = (b * 0x1F1F + 0x8000) >> 16; g = (g * 0x3F3F + 0x8000) >> 16; r = (r * 0x1F1F + 0x8000) >> 16; p = (b << 0) | (g << 5) | (r << 11); image16[o] = (ushort)p; o++; } } } else { for (int x = 0; x < pmsHeader.width; x++) { unchecked { int p = scanline[x]; int b = (p >> 0) & 0xFF; int g = (p >> 8) & 0xFF; int r = (p >> 16) & 0xFF; int a = (p >> 24) & 0xFF; b = (b * 0x1F1F + 0x8000) >> 16; g = (g * 0x3F3F + 0x8000) >> 16; r = (r * 0x1F1F + 0x8000) >> 16; p = (b << 0) | (g << 5) | (r << 11); image16[o] = (ushort)p; image8[o] = (byte)a; o++; } } } } MemoryStream ms = new MemoryStream(); SaveImageData16Bit(ms, image16, pmsHeader.width, pmsHeader.height); int imageSize = (int)ms.Length; if (usingAlphaChannel) { pmsHeader.addressOfPalette = imageSize + pmsHeader.addressOfData; } SaveHeader(stream, pmsHeader); ms.WriteTo(stream); ms.SetLength(0); if (usingAlphaChannel) { SaveImageData8Bit(stream, image8, pmsHeader.width, pmsHeader.height); } } }
/// <summary> /// Compile input images into WAD texture file. /// </summary> /// <param name="outputFilename">Output wad file path.</param> /// <param name="images">Input image files.</param> /// <param name="names">Names of textures.</param> /// <param name="reserverLastPalColor">Reserve last color in palette if name starts with {.</param> public static void CreateWad(string outputFilename, string[] images, string[] names, Color alphaReplacementColor, bool reserverLastPalColor = false) { using (FileStream fs = new FileStream(outputFilename, FileMode.Create)) using (BinaryWriter bw = new BinaryWriter(fs)) { //Convert bitmaps to 8bpp format List <FreeImageBitmap> imgs = new List <FreeImageBitmap>(); for (int i = 0; i < images.Length; i++) { //Quantize images FreeImageBitmap originalImage = new FreeImageBitmap(images[i]); //If texture will be transparent, reserve last color if enabled bool reserveLastClr = (names[i].StartsWith("{") && reserverLastPalColor); bool isTransparentImage = originalImage.IsTransparent; bool is8Bpp = originalImage.BitsPerPixel == 8; int r = reserveLastClr ? 1 : 0; if (isTransparentImage) { originalImage.SwapColors(new RGBQUAD(Color.Transparent), new RGBQUAD(alphaReplacementColor), false); } originalImage.Quantize(FREE_IMAGE_QUANTIZE.FIQ_NNQUANT, MaxPaletteColors - r); originalImage.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP); if (reserveLastClr) { if (isTransparentImage) { bool foundReplacementColor = false; for (int pindex = 0; pindex < originalImage.Palette.Length; pindex++) { RGBQUAD rgb = originalImage.Palette.GetValue(pindex); if (rgb.rgbRed == alphaReplacementColor.R && rgb.rgbGreen == alphaReplacementColor.G && rgb.rgbBlue == alphaReplacementColor.B) { var lastColor = originalImage.Palette.GetValue(MaxPaletteColors - 1); originalImage.Palette[pindex] = lastColor; originalImage.Palette[MaxPaletteColors - 1] = new RGBQUAD(alphaReplacementColor); originalImage.SwapPaletteIndices((byte)pindex, MaxPaletteColors - 1); foundReplacementColor = true; break; } } // If didn't found replacement, set directly last alpha color if (!foundReplacementColor) { originalImage.Palette[MaxPaletteColors - 1] = new RGBQUAD(alphaReplacementColor); } } else { originalImage.Palette[MaxPaletteColors - 1] = new RGBQUAD(alphaReplacementColor); } } imgs.Add(originalImage); } uint[] offsets = new uint[images.Length]; uint[] sizes = new uint[images.Length]; //WAD header bw.Write(WadHeaderId); bw.Write(images.Length); bw.Write(0); //This will be changed later //Write textures for (int i = 0; i < images.Length; i++) { uint posTextureStart = (uint)bw.BaseStream.Position; offsets[i] = posTextureStart; //Texture name byte[] name = CreateTextureName(names[i]); bw.Write(name, 0, name.Length); //Texture dimensions bw.Write(imgs[i].Width); bw.Write(imgs[i].Height); //Offsets uint posImage = (uint)(bw.BaseStream.Position - posTextureStart); bw.Write(posImage + 16); //image int pixelSize = ((imgs[i].Width) * (imgs[i].Height)); int m1 = ((imgs[i].Width / 2) * (imgs[i].Height / 2)); int m2 = ((imgs[i].Width / 4) * (imgs[i].Height / 4)); int m3 = ((imgs[i].Width / 8) * (imgs[i].Height / 8)); bw.Write((uint)(posImage + pixelSize + 16)); //mipmap1 bw.Write((uint)(posImage + pixelSize + m1 + 16)); //mipmap2 bw.Write((uint)(posImage + pixelSize + m1 + m2 + 16)); //mipmap3 //Write pixel data imgs[i].RotateFlip(RotateFlipType.RotateNoneFlipX); byte[] arr = new byte[imgs[i].Width * imgs[i].Height]; System.Runtime.InteropServices.Marshal.Copy(imgs[i].GetScanlinePointer(0), arr, 0, arr.Length); Array.Reverse(arr); bw.Write(arr); // //Mip map data int factor = 2; for (int a = 0; a < 3; a++) { int widthMM = (imgs[i].Width / factor); int heightMM = (imgs[i].Height / factor); using (FreeImageBitmap clBmp = new FreeImageBitmap(imgs[i])) { //TODO: Transparent png clBmp.Rescale(widthMM, heightMM, FREE_IMAGE_FILTER.FILTER_LANCZOS3); clBmp.Quantize(FREE_IMAGE_QUANTIZE.FIQ_NNQUANT, MaxPaletteColors, imgs[i].Palette); byte[] arrMM = new byte[widthMM * heightMM]; System.Runtime.InteropServices.Marshal.Copy(clBmp.GetScanlinePointer(0), arrMM, 0, arrMM.Length); Array.Reverse(arrMM); bw.Write(arrMM); } factor *= 2; } //Unknown 2 bytes bw.Write(new byte[] { 0x00, 0x01 }); //Write color palette for (int p = 0; p < imgs[i].Palette.Length; p++) { bw.Write(imgs[i].Palette[p].rgbRed); bw.Write(imgs[i].Palette[p].rgbGreen); bw.Write(imgs[i].Palette[p].rgbBlue); } //Padding bw.Write(new byte[] { 0x00, 0x00 }); sizes[i] = (uint)bw.BaseStream.Position - posTextureStart; } long posLumps = bw.BaseStream.Position; bw.Seek(8, SeekOrigin.Begin); bw.Write((uint)posLumps); bw.Seek((int)posLumps, SeekOrigin.Begin); //Write Lumps infos for (int i = 0; i < images.Length; i++) { bw.Write(offsets[i]); bw.Write(sizes[i]); bw.Write(sizes[i]); bw.Write((byte)0x43); bw.Write((byte)0); bw.Write(new byte[] { 0x00, 0x00 }); byte[] name = CreateTextureName(names[i]); bw.Write(name, 0, name.Length); } //Free resources for (int i = 0; i < imgs.Count; i++) { imgs[i].Dispose(); } } }