private static unsafe byte[] Unrle(byte[] dataBuffer, byte[] cmdBuffer) { fixed(byte *pDataBuffer = dataBuffer) fixed(byte *pCmdBuffer = cmdBuffer) { unchecked { // Local BitBuffer byte *cmd = pCmdBuffer; int cmdLength = cmdBuffer.Length; int cmdBitIndex = 0; bool copyFlag = GetCmdBit(ref cmd, ref cmdLength, ref cmdBitIndex); uint unrleLength = GetCmdEliasGammaValue(ref cmd, ref cmdLength, ref cmdBitIndex); byte[] unrleBuffer = new byte[unrleLength]; fixed(byte *pUnrleBuffer = unrleBuffer) { uint dataLeft = (uint)dataBuffer.LongLength; uint unrleLeft = (uint)unrleBuffer.LongLength; byte *data = pDataBuffer; uint n; for (uint i = 0; i < unrleLength; i += n) { n = GetCmdEliasGammaValue(ref cmd, ref cmdLength, ref cmdBitIndex); if (copyFlag) { if (unrleLeft < n || dataLeft < n) { throw new HgxException("dataBuffer, cmdBuffer, or unrleBuffer ran out of data!"); } dataLeft -= n; Buffer.MemoryCopy(data, pUnrleBuffer + i, unrleLeft, n); data += n; } unrleLeft -= n; copyFlag = !copyFlag; } return(unrleBuffer); } } } }
/// <summary> /// Writes the bitmap buffer to <paramref name="pngFile"/> and optional performs expansion if /// <paramref name="expand"/> is true. /// </summary> /// <param name="buffer">The buffer to the image bits.</param> /// <param name="std">The HG3STDINFO containing image dimensions, etc.</param> /// <param name="expand">True if the image should be expanded to its full size.</param> /// <param name="pngFile">The path to the file to save to.</param> private static unsafe void WriteJpegAlphaMaskToPng(byte[] buffer, byte[] alpha, HG3STDINFO std, HgxOptions options, string pngFile) { bool expand = options.HasFlag(HgxOptions.Expand); expand = expand && (std.Width != std.TotalWidth || std.Height != std.TotalHeight); int offsetX = (expand ? std.OffsetX : 0); int offsetY = (expand ? std.OffsetY : 0); int width = (expand ? std.TotalWidth : std.Width); int height = (expand ? std.TotalHeight : std.Height); int depthBytes = 4; int jpgStride = std.Width * depthBytes; int stride = width * depthBytes; int bufferSize = height * stride; int alphaStride = std.Width; BitmapData jpgData = null, bmpData = null; using (var ms = new MemoryStream(buffer)) using (var jpeg = (Bitmap)Image.FromStream(ms)) using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) { try { Rectangle rect = new Rectangle(0, 0, std.Width, std.Height); jpgData = jpeg.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); bmpData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); byte *pJpg = (byte *)jpgData.Scan0.ToPointer(); byte *pBmp = (byte *)bmpData.Scan0.ToPointer(); // Copy over the jpeg pixels first if (expand) { for (int y = 0; y < std.Height; y++) { int src = y * jpgStride; int dst = (y + offsetY) * stride + offsetX * depthBytes; Buffer.MemoryCopy(pJpg + src, pBmp + dst, bufferSize, jpgStride); } } else { Buffer.MemoryCopy(pJpg, pBmp, bufferSize, bufferSize); } // Now apply the alpha to the pixels for (int y = 0; y < std.Height; y++) { int src = y * alphaStride; int dst = (y + offsetY) * stride; for (int x = 0; x < std.Width; x++) { int alphaIndex = src + x; int pixelIndex = dst + (x + offsetX) * depthBytes; pBmp[pixelIndex + 3] = alpha[alphaIndex]; } } } finally { if (jpgData != null) { jpeg.UnlockBits(jpgData); } if (bmpData != null) { bitmap.UnlockBits(bmpData); } } if (options.HasFlag(HgxOptions.Flip)) { bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); } bitmap.Save(pngFile, ImageFormat.Png); } }