internal void CompressPng(Bitmap pBmp) { byte[] buf = new byte[pBmp.Width * pBmp.Height * 8]; mFormat = 2; mFormat2 = 0; mWidth = pBmp.Width; mHeight = pBmp.Height; int curPos = 0; for (int i = 0; i < mHeight; i++) { for (int j = 0; j < mWidth; j++) { Color curPixel = pBmp.GetPixel(j, i); buf[curPos] = curPixel.B; buf[curPos + 1] = curPixel.G; buf[curPos + 2] = curPixel.R; buf[curPos + 3] = curPixel.A; curPos += 4; } } mCompressedBytes = Compress(buf); //if (!mIsNew) // return; MemoryStream memStream = new MemoryStream(); WzBinaryWriter writer = new WzBinaryWriter(memStream, WzTool.GetIvByMapleVersion(WzMapleVersion.GMS)); writer.Write(2); for (int i = 0; i < 2; i++) { writer.Write((byte)(mCompressedBytes[i] ^ writer.WzKey[i])); } writer.Write(mCompressedBytes.Length - 2); for (int i = 2; i < mCompressedBytes.Length; i++) { writer.Write((byte)(mCompressedBytes[i] ^ writer.WzKey[i - 2])); } mCompressedBytes = memStream.GetBuffer(); writer.Close(); }
internal void CompressPng(Bitmap bmp) { byte[] buf = new byte[bmp.Width * bmp.Height * 8]; format = 2; format2 = 0; width = bmp.Width; height = bmp.Height; int curPos = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color curPixel = bmp.GetPixel(j, i); buf[curPos] = curPixel.B; buf[curPos + 1] = curPixel.G; buf[curPos + 2] = curPixel.R; buf[curPos + 3] = curPixel.A; curPos += 4; } } compressedBytes = Compress(buf); if (listWzUsed) { MemoryStream memStream = new MemoryStream(); WzBinaryWriter writer = new WzBinaryWriter(memStream, WzTool.GetIvByMapleVersion(WzMapleVersion.GMS)); writer.Write(2); for (int i = 0; i < 2; i++) { writer.Write((byte)(compressedBytes[i] ^ writer.WzKey[i])); } writer.Write(compressedBytes.Length - 2); for (int i = 2; i < compressedBytes.Length; i++) { writer.Write((byte)(compressedBytes[i] ^ writer.WzKey[i - 2])); } compressedBytes = memStream.GetBuffer(); writer.Close(); } }
internal WzImage ParseXMLWzImg(XmlElement imgElement) { string name = imgElement.GetAttribute("name"); WzImage result = new WzImage(name); foreach (XmlElement subelement in imgElement) { result.WzProperties.Add(ParsePropertyFromXMLElement(subelement)); } result.Changed = true; if (this.useMemorySaving) { string path = Path.GetTempFileName(); WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), iv); result.SaveImage(wzWriter); wzWriter.Close(); result.Dispose(); result = imgDeserializer.WzImageFromIMGFile(path, iv, name); } return(result); }
/// <summary> /// Saves a wz file to the disk, AKA repacking. /// </summary> /// <param name="path">Path to the output wz file</param> public void SaveToDisk(string path) { CreateVersionHash(); wzDir.SetHash(versionHash); string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP"; File.Create(tempFile).Close(); wzDir.GenerateDataFile(tempFile); WzTool.StringCache.Clear(); uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + 2)); WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv); wzWriter.Hash = (uint)versionHash; Header.FSize = totalLen - Header.FStart; for (int i = 0; i < 4; i++) { wzWriter.Write((byte)Header.Ident[i]); } wzWriter.Write((long)Header.FSize); wzWriter.Write(Header.FStart); wzWriter.WriteNullTerminatedString(Header.Copyright); long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position; if (extraHeaderLength > 0) { wzWriter.Write(new byte[(int)extraHeaderLength]); } wzWriter.Write(version); wzWriter.Header = Header; wzDir.SaveDirectory(wzWriter); wzWriter.StringCache.Clear(); FileStream fs = File.OpenRead(tempFile); wzDir.SaveImages(wzWriter, fs); fs.Close(); File.Delete(tempFile); wzWriter.StringCache.Clear(); wzWriter.Close(); }
internal void CompressPng(Bitmap bmp) { this.width = bmp.Width; this.height = bmp.Height; // https://github.com/eaxvac/Harepacker-resurrected // http://forum.ragezone.com/f921/release-harepacker-resurrected-1149521/ switch (nPixFormat + nMagLevel) { case 1: { byte[] buf = new byte[bmp.Width * bmp.Height * 2]; int curPos = 0; long ticksNow = DateTime.Now.Ticks; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { Color curPixel = bmp.GetPixel(col, row); // 4 bytes argb int B = (curPixel.B >> 4) & 0x0F; int G = (curPixel.G << 4) & 0xF0; int R = (curPixel.R >> 4) & 0x0F; int A = (curPixel.A << 4) & 0xF0; // confirmed // 1 byte for every argb buf[curPos] = (byte)(B | G); buf[curPos + 1] = (byte)(R | A); curPos += 2; // BG } } compressedBytes = Compress(buf); break; } case 2: default: // defaults to pixel format 2. Unsupported for now. { byte[] buf = new byte[bmp.Width * bmp.Height * 4]; this.nPixFormat = 2; this.nMagLevel = 0; int curPos = 0; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { Color curPixel = bmp.GetPixel(col, row); // 4 bytes buf[curPos] = curPixel.B; buf[curPos + 1] = curPixel.G; buf[curPos + 2] = curPixel.R; buf[curPos + 3] = curPixel.A; curPos += 4; // ARGB } } compressedBytes = Compress(buf); break; } } if (listWzUsed) { MemoryStream memStream = new MemoryStream(); WzBinaryWriter writer = new WzBinaryWriter(memStream, ((WzDirectory)parent).WzIv); writer.Write(2); for (int i = 0; i < 2; i++) { writer.Write((byte)(compressedBytes[i] ^ writer.WzKey[i])); } writer.Write(compressedBytes.Length - 2); for (int i = 2; i < compressedBytes.Length; i++) { writer.Write((byte)(compressedBytes[i] ^ writer.WzKey[i - 2])); } compressedBytes = memStream.GetBuffer(); writer.Close(); } }
internal int GenerateDataFile(string fileName) { size = 0; int entryCount = subDirs.Count + images.Count; if (entryCount == 0) { offsetSize = 1; return(size = 0); } size = WzTool.GetCompressedIntLength(entryCount); offsetSize = WzTool.GetCompressedIntLength(entryCount); WzBinaryWriter imgWriter = null; MemoryStream memStream = null; FileStream fileWrite = new FileStream(fileName, FileMode.Append, FileAccess.Write); for (int i = 0; i < images.Count; i++) { memStream = new MemoryStream(); imgWriter = new WzBinaryWriter(memStream, this.WzIv); images[i].SaveImage(imgWriter); images[i].checksum = 0; foreach (byte b in memStream.ToArray()) { images[i].checksum += b; } images[i].tempFileStart = fileWrite.Position; fileWrite.Write(memStream.ToArray(), 0, (int)memStream.Length); images[i].tempFileEnd = fileWrite.Position; memStream.Dispose(); images[i].UnparseImage(); int nameLen = WzTool.GetWzObjectValueLength(images[i].name, 4); size += nameLen; int imgLen = images[i].size; size += WzTool.GetCompressedIntLength(imgLen); size += imgLen; size += WzTool.GetCompressedIntLength(images[i].Checksum); size += 4; offsetSize += nameLen; offsetSize += WzTool.GetCompressedIntLength(imgLen); offsetSize += WzTool.GetCompressedIntLength(images[i].Checksum); offsetSize += 4; imgWriter.Close(); } fileWrite.Close(); for (int i = 0; i < subDirs.Count; i++) { int nameLen = WzTool.GetWzObjectValueLength(subDirs[i].name, 3); size += nameLen; size += subDirs[i].GenerateDataFile(fileName); size += WzTool.GetCompressedIntLength(subDirs[i].size); size += WzTool.GetCompressedIntLength(subDirs[i].checksum); size += 4; offsetSize += nameLen; offsetSize += WzTool.GetCompressedIntLength(subDirs[i].size); offsetSize += WzTool.GetCompressedIntLength(subDirs[i].checksum); offsetSize += 4; } return(size); }
internal int GenerateDataFile(string pFileName) { mSize = 0; int entryCount = mSubDirs.Count + mImages.Count; if (entryCount == 0) { mOffsetSize = 1; return(mSize = 0); } mSize = WzTool.GetCompressedIntLength(entryCount); mOffsetSize = WzTool.GetCompressedIntLength(entryCount); WzBinaryWriter imgWriter; MemoryStream memStream; FileStream fileWrite = new FileStream(pFileName, FileMode.Append, FileAccess.Write); foreach (WzImage t in mImages) { memStream = new MemoryStream(); imgWriter = new WzBinaryWriter(memStream, mWzIv); t.SaveImage(imgWriter); t.checksum = 0; foreach (byte b in memStream.ToArray()) { t.checksum += b; } t.mTempFileStart = fileWrite.Position; fileWrite.Write(memStream.ToArray(), 0, (int)memStream.Length); t.mTempFileEnd = fileWrite.Position; memStream.Dispose(); t.UnparseImage(); int nameLen = WzTool.GetWzObjectValueLength(t.mName, 4); mSize += nameLen; int imgLen = t.mSize; mSize += WzTool.GetCompressedIntLength(imgLen); mSize += imgLen; mSize += WzTool.GetCompressedIntLength(t.Checksum); mSize += 4; mOffsetSize += nameLen; mOffsetSize += WzTool.GetCompressedIntLength(imgLen); mOffsetSize += WzTool.GetCompressedIntLength(t.Checksum); mOffsetSize += 4; imgWriter.Close(); } fileWrite.Close(); foreach (WzDirectory t in mSubDirs) { int nameLen = WzTool.GetWzObjectValueLength(t.mName, 3); mSize += nameLen; mSize += t.GenerateDataFile(pFileName); mSize += WzTool.GetCompressedIntLength(t.mSize); mSize += WzTool.GetCompressedIntLength(t.mChecksum); mSize += 4; mOffsetSize += nameLen; mOffsetSize += WzTool.GetCompressedIntLength(t.mSize); mOffsetSize += WzTool.GetCompressedIntLength(t.mChecksum); mOffsetSize += 4; } return(mSize); }