public async Task <String> EncodeText(BitmapDecoder imageDecoder, AnymapProperties properties) { var pixelDataProvider = await imageDecoder.GetPixelDataAsync(); var image = pixelDataProvider.DetachPixelData(); properties.SourcePixelFormat = imageDecoder.BitmapPixelFormat; UInt32 totalPixels = imageDecoder.OrientedPixelWidth * imageDecoder.OrientedPixelHeight; WriteTextDelegate write = null; bool isContainMaxValue = true; StringWriter stringWriter = new StringWriter(); // Format switch (properties.AnymapType) { case AnymapType.Bitmap: stringWriter.WriteLine("P1"); isContainMaxValue = false; write = WriteP1; break; case AnymapType.Graymap: stringWriter.WriteLine("P2"); write = WriteP2; break; case AnymapType.Pixmap: stringWriter.WriteLine("P3"); write = WriteP3; break; } // Size stringWriter.WriteLine("{0} {1}", imageDecoder.OrientedPixelWidth, imageDecoder.OrientedPixelHeight); properties.Width = imageDecoder.OrientedPixelWidth; properties.Height = imageDecoder.OrientedPixelHeight; // Maximum pixel value if (isContainMaxValue) { stringWriter.WriteLine("{0}", properties.MaxValue); } // Pixels write(image, stringWriter, totalPixels, properties); return(stringWriter.ToString()); }
private async Task <AnymapProperties> GetImageProperties(IRandomAccessStream stream, bool isContainMaxValue) { ulong size = stream.Size; stream.Seek(0); var dataReader = new DataReader(stream); uint bytesLoaded = await dataReader.LoadAsync((uint)size); byte[] bytes = new byte[bytesLoaded]; dataReader.ReadBytes(bytes); ASCIIEncoding ascii = new ASCIIEncoding(); String strAll = ascii.GetString(bytes); uint[] imageProperties = new uint[3]; uint imagePropertiesCounter = 0; string[] strs = strAll.Split(new String[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); int seekPos = 0; MatchCollection mc = null; foreach (string str in strs) { mc = Regex.Matches(Regex.Match(str, "[^#]{0,}").ToString(), @"\b\d+"); for (int i = 0; i < mc.Count; ++i) { imageProperties[imagePropertiesCounter++] = Convert.ToUInt32(mc[i].Value); if (imagePropertiesCounter == (isContainMaxValue ? 3 : 2)) { seekPos += (mc[i].Index + mc[i].Length + 1); break; } } if (imagePropertiesCounter == (isContainMaxValue ? 3 : 2)) { break; } seekPos += str.Length + 1; } if (imagePropertiesCounter != (isContainMaxValue ? 3 : 2)) { AnymapProperties badProperties = new AnymapProperties(); badProperties.Width = 0; badProperties.Height = 0; badProperties.MaxValue = 0; badProperties.BytesPerColor = 0; badProperties.StreamPosition = 0; return(badProperties); } dataReader.DetachBuffer(); dataReader.DetachStream(); dataReader.Dispose(); GC.Collect(); stream.Seek((ulong)(seekPos)); AnymapProperties properties = new AnymapProperties(); properties.Width = imageProperties[0]; properties.Height = imageProperties[1]; properties.MaxValue = (isContainMaxValue ? imageProperties[2] : 255); properties.BytesPerColor = (properties.MaxValue < 256) ? (uint)1 : (uint)2; properties.StreamPosition = (uint)(seekPos); return(properties); }
private async Task <DecodeResult> decode(IRandomAccessStream stream, String filename) { ulong size = stream.Size; DecodeResult result = new DecodeResult(); result.Type = 0; result.Width = 0; result.Height = 0; result.Bytes = null; result.Filename = filename; result.CurrentZoom = 1.0f; if (size >= 2) { stream.Seek(0); var dataReader = new DataReader(stream); await dataReader.LoadAsync((uint)2); string formatType = dataReader.ReadString(2); if (formatType[0] != 'P') { return(result); } AnymapProperties properties = null; byte[] decodedAnymap = null; uint bytesLoaded = 0; uint pixelsNum = 0; uint packedPixelsNum = 0; switch (formatType[1]) { case '1': properties = await GetImageProperties(stream, false); if (properties.MaxValue != 0) { decodedAnymap = new byte[properties.Width * properties.Height * 4]; stream.Seek(properties.StreamPosition); dataReader = new DataReader(stream); bytesLoaded = await dataReader.LoadAsync((uint)(stream.Size - properties.StreamPosition)); String strAll = dataReader.ReadString(bytesLoaded); if (properties.BytesPerColor == 1) { int resultIndex = 0; String[] strs = strAll.Split(new String[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); MatchCollection mc = null; foreach (string str in strs) { mc = Regex.Matches(Regex.Match(str, @"[^#]{0,}").ToString(), @"\b\d+"); for (int i = 0; i < mc.Count && resultIndex < decodedAnymap.Length; ++i) { decodedAnymap[resultIndex] = (byte)((Convert.ToUInt32(mc[i].Value) ^ 0x01) * 255); decodedAnymap[resultIndex + 1] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 2] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 3] = 255; resultIndex += 4; } } } } result.Type = 1; result.Width = (Int32)properties.Width; result.Height = (Int32)properties.Height; result.Bytes = decodedAnymap; result.DoubleBytesPerColor = properties.BytesPerColor == 2; break; case '2': properties = await GetImageProperties(stream, true); if (properties.MaxValue != 0) { stream.Seek(properties.StreamPosition); dataReader = new DataReader(stream); bytesLoaded = await dataReader.LoadAsync((uint)(stream.Size - properties.StreamPosition)); String strAll = dataReader.ReadString(bytesLoaded); if (properties.BytesPerColor == 1) { decodedAnymap = new byte[properties.Width * properties.Height * 4]; int resultIndex = 0; String[] strs = strAll.Split(new String[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); MatchCollection mc = null; foreach (string str in strs) { mc = Regex.Matches(Regex.Match(str, @"[^#]{0,}").ToString(), @"\b\d+"); for (int i = 0; i < mc.Count && resultIndex < decodedAnymap.Length; ++i) { decodedAnymap[resultIndex] = (byte)(Convert.ToUInt32(mc[i].Value) / (double)properties.MaxValue * 255); decodedAnymap[resultIndex + 1] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 2] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 3] = 255; resultIndex += 4; } } } else { decodedAnymap = new byte[properties.Width * properties.Height * 8]; int resultIndex = 0; String[] strs = strAll.Split(new String[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); MatchCollection mc = null; UInt16 tmp = 0; foreach (string str in strs) { mc = Regex.Matches(Regex.Match(str, @"[^#]{0,}").ToString(), @"\b\d+"); for (int i = 0; i < mc.Count && resultIndex < decodedAnymap.Length; ++i) { tmp = (UInt16)(Convert.ToUInt32(mc[i].Value) / (double)properties.MaxValue * 65535); decodedAnymap[resultIndex + 1] = (byte)(tmp >> 8); decodedAnymap[resultIndex] = (byte)(tmp & 0x00FF); decodedAnymap[resultIndex + 3] = decodedAnymap[resultIndex + 1]; decodedAnymap[resultIndex + 2] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 5] = decodedAnymap[resultIndex + 1]; decodedAnymap[resultIndex + 4] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 7] = 255; decodedAnymap[resultIndex + 6] = 255; resultIndex += 8; } } } } result.Type = 2; result.Width = (Int32)properties.Width; result.Height = (Int32)properties.Height; result.Bytes = decodedAnymap; result.DoubleBytesPerColor = properties.BytesPerColor == 2; break; case '3': result.HistogramValues = new List <List <HistogramValue> >(3); result.HistogramValues.Add(new List <HistogramValue>(256)); result.HistogramValues.Add(new List <HistogramValue>(256)); result.HistogramValues.Add(new List <HistogramValue>(256)); foreach (List <HistogramValue> list in result.HistogramValues) { for (int i = 0; i < 256; ++i) { list.Add(new HistogramValue { Brightness = i, Level = 0.0 }); } } properties = await GetImageProperties(stream, true); if (properties.MaxValue != 0) { stream.Seek(properties.StreamPosition); dataReader = new DataReader(stream); bytesLoaded = await dataReader.LoadAsync((uint)(stream.Size - properties.StreamPosition)); String strAll = dataReader.ReadString(bytesLoaded); if (properties.BytesPerColor == 1) { decodedAnymap = new byte[properties.Width * properties.Height * 4]; int resultIndex = 0; string[] strs = strAll.Split(new String[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); int BgraIndex = 2; MatchCollection mc = null; foreach (string str in strs) { mc = Regex.Matches(Regex.Match(str, @"[^#]{0,}").ToString(), @"\b\d+"); for (int i = 0; i < mc.Count; ++i) { decodedAnymap[resultIndex + BgraIndex] = (byte)(Convert.ToUInt32(mc[i].Value) / (double)properties.MaxValue * 255); result.HistogramValues[BgraIndex][decodedAnymap[resultIndex + BgraIndex]].Level += 1.0; --BgraIndex; if (BgraIndex == -1) { BgraIndex = 3; decodedAnymap[resultIndex + BgraIndex] = 255; resultIndex += 4; --BgraIndex; if (resultIndex >= decodedAnymap.Length) { break; } } } if (resultIndex >= decodedAnymap.Length) { break; } } } else { decodedAnymap = new byte[properties.Width * properties.Height * 8]; int resultIndex = 0; string[] strs = strAll.Split(new String[] { "\r", "\r\n", "\n" }, StringSplitOptions.None); MatchCollection mc = null; UInt16 tmp = 0; int rgbaIndex = 0; foreach (string str in strs) { mc = Regex.Matches(Regex.Match(str, @"[^#]{0,}").ToString(), @"\b\d+"); for (int i = 0; i < mc.Count; ++i) { tmp = (UInt16)(Convert.ToUInt32(mc[i].Value) / (double)properties.MaxValue * 65535); decodedAnymap[resultIndex + rgbaIndex + 1] = (byte)(tmp >> 8); decodedAnymap[resultIndex + rgbaIndex + 0] = (byte)(tmp & 0x00FF); rgbaIndex += 2; if (rgbaIndex == 6) { decodedAnymap[resultIndex + 7] = 255; decodedAnymap[resultIndex + 6] = 255; rgbaIndex = 0; resultIndex += 8; } } if (resultIndex >= decodedAnymap.Length) { break; } } } } result.Type = 3; result.Width = (Int32)properties.Width; result.Height = (Int32)properties.Height; result.Bytes = decodedAnymap; result.DoubleBytesPerColor = properties.BytesPerColor == 2; break; case '4': properties = await GetImageProperties(stream, false); pixelsNum = properties.Width * properties.Height; decodedAnymap = new byte[pixelsNum * 4]; stream.Seek(properties.StreamPosition); dataReader = new DataReader(stream); bytesLoaded = await dataReader.LoadAsync((uint)(stream.Size - properties.StreamPosition)); if (properties.BytesPerColor == 1) { int resultIndex = 0; int mod = (int)(properties.Width * properties.Height) % 8; int colmod = (int)(properties.Width) % 8; packedPixelsNum = (properties.Width / 8 + (uint)(colmod == 0 ? 0 : 1)) * properties.Height; uint col = 0; int num = 0; for (uint i = 0; i < packedPixelsNum; ++i) { if (col == properties.Width - colmod) { num = colmod == 0 ? 8 : colmod; unpackBitsPbm(decodedAnymap, resultIndex, num, dataReader.ReadByte()); col = 0; resultIndex += num * 4; } else { unpackBitsPbm(decodedAnymap, resultIndex, 8, dataReader.ReadByte()); col += 8; resultIndex += 32; } } } result.Type = 4; result.Width = (Int32)properties.Width; result.Height = (Int32)properties.Height; result.Bytes = decodedAnymap; result.DoubleBytesPerColor = properties.BytesPerColor == 2; break; case '5': properties = await GetImageProperties(stream, true); if (properties.MaxValue != 0) { stream.Seek(properties.StreamPosition); dataReader = new DataReader(stream); bytesLoaded = await dataReader.LoadAsync((uint)(stream.Size - properties.StreamPosition)); if (properties.BytesPerColor == 1) { decodedAnymap = new byte[properties.Width * properties.Height * 4]; uint resultIndex = 0; for (int i = 0; i < properties.Height; ++i) { for (int j = 0; j < properties.Width && dataReader.UnconsumedBufferLength >= 1; ++j) { decodedAnymap[resultIndex] = (byte)(dataReader.ReadByte() / (double)properties.MaxValue * 255); decodedAnymap[resultIndex + 1] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 2] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 3] = 255; resultIndex += 4; } } } else { decodedAnymap = new byte[properties.Width * properties.Height * 8]; uint resultIndex = 0; UInt16 tmp; for (int i = 0; i < properties.Height; ++i) { for (int j = 0; j < properties.Width && dataReader.UnconsumedBufferLength >= 1; ++j) { tmp = dataReader.ReadByte(); tmp <<= 8; tmp += dataReader.ReadByte(); tmp = (UInt16)(tmp / (Double)properties.MaxValue * 65535); decodedAnymap[resultIndex + 1] = (byte)(tmp >> 8); decodedAnymap[resultIndex] = (byte)(tmp & 0x00FF); decodedAnymap[resultIndex + 3] = decodedAnymap[resultIndex + 1]; decodedAnymap[resultIndex + 2] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 5] = decodedAnymap[resultIndex + 1]; decodedAnymap[resultIndex + 4] = decodedAnymap[resultIndex]; decodedAnymap[resultIndex + 7] = 255; decodedAnymap[resultIndex + 6] = 255; resultIndex += 8; } } } } result.Type = 5; result.Width = (Int32)properties.Width; result.Height = (Int32)properties.Height; result.Bytes = decodedAnymap; result.DoubleBytesPerColor = properties.BytesPerColor == 2; break; case '6': result.HistogramValues = new List <List <HistogramValue> >(3); result.HistogramValues.Add(new List <HistogramValue>(256)); result.HistogramValues.Add(new List <HistogramValue>(256)); result.HistogramValues.Add(new List <HistogramValue>(256)); foreach (List <HistogramValue> list in result.HistogramValues) { for (int i = 0; i < 256; ++i) { list.Add(new HistogramValue { Brightness = i, Level = 0.0 }); } } properties = await GetImageProperties(stream, true); if (properties.MaxValue != 0) { decodedAnymap = new byte[properties.Width * properties.Height * 4]; stream.Seek(properties.StreamPosition); dataReader = new DataReader(stream); bytesLoaded = await dataReader.LoadAsync((uint)(stream.Size - properties.StreamPosition)); if (properties.BytesPerColor == 1) { uint resultIndex = 0; for (int i = 0; i < properties.Height; ++i) { for (int j = 0; j < properties.Width && dataReader.UnconsumedBufferLength >= 3; ++j) { decodedAnymap[resultIndex + 2] = (byte)(dataReader.ReadByte() / (double)properties.MaxValue * 255); decodedAnymap[resultIndex + 1] = (byte)(dataReader.ReadByte() / (double)properties.MaxValue * 255); decodedAnymap[resultIndex] = (byte)(dataReader.ReadByte() / (double)properties.MaxValue * 255); decodedAnymap[resultIndex + 3] = 255; result.HistogramValues[0][decodedAnymap[resultIndex + 2]].Level += 1.0; result.HistogramValues[1][decodedAnymap[resultIndex + 1]].Level += 1.0; result.HistogramValues[2][decodedAnymap[resultIndex]].Level += 1.0; resultIndex += 4; } } } else { decodedAnymap = new byte[properties.Width * properties.Height * 8]; uint resultIndex = 0; UInt16 tmp; for (int i = 0; i < properties.Height; ++i) { for (int j = 0; j < properties.Width && dataReader.UnconsumedBufferLength >= 1; ++j) { tmp = dataReader.ReadByte(); tmp <<= 8; tmp += dataReader.ReadByte(); tmp = (UInt16)(tmp / (Double)properties.MaxValue * 65535); decodedAnymap[resultIndex + 1] = (byte)(tmp >> 8); decodedAnymap[resultIndex] = (byte)(tmp & 0x00FF); tmp = dataReader.ReadByte(); tmp <<= 8; tmp += dataReader.ReadByte(); tmp = (UInt16)(tmp / (Double)properties.MaxValue * 65535); decodedAnymap[resultIndex + 3] = (byte)(tmp >> 8); decodedAnymap[resultIndex + 2] = (byte)(tmp & 0x00FF); tmp = dataReader.ReadByte(); tmp <<= 8; tmp += dataReader.ReadByte(); tmp = (UInt16)(tmp / (Double)properties.MaxValue * 65535); decodedAnymap[resultIndex + 5] = (byte)(tmp >> 8); decodedAnymap[resultIndex + 4] = (byte)(tmp & 0x00FF); decodedAnymap[resultIndex + 7] = 255; decodedAnymap[resultIndex + 6] = 255; resultIndex += 8; } } } } result.Type = 6; result.Width = (Int32)properties.Width; result.Height = (Int32)properties.Height; result.Bytes = decodedAnymap; result.DoubleBytesPerColor = properties.BytesPerColor == 2; break; } dataReader.DetachBuffer(); dataReader.DetachStream(); dataReader.Dispose(); } //foreach (List<HistogramValue> list in result.HistogramValues) //{ // foreach (HistogramValue value in list) // { // value.Level /= result.Width * result.Height; // } //} GC.Collect(); return(result); }
private async void Convert_Click(object sender, RoutedEventArgs e) { ConverterTopCommandBar.IsEnabled = false; ConverterBottomCommandBar.IsEnabled = false; this.FileProgressBar.Visibility = Visibility.Visible; this.IsBinary.Visibility = Visibility.Collapsed; this.Depth8_16.Visibility = Visibility.Collapsed; this.FileTypeCombo.Visibility = Visibility.Collapsed; this.MaxPixelValue.Visibility = Visibility.Collapsed; this.ThresholdLevelTxt8.Visibility = Visibility.Collapsed; this.ThresholdLevelTxt16.Visibility = Visibility.Collapsed; this.NameCollisionCombo.Visibility = Visibility.Collapsed; if (outputFolder == null) { await this.ChangeFolder(); } var anymapType = (AnymapType)FileTypeCombo.SelectedIndex; var maxValue = Convert.ToUInt32(MaxPixelValue.Text); var threshold8 = Convert.ToByte(ThresholdLevelTxt8.Text); var threshold16 = Convert.ToUInt16(ThresholdLevelTxt16.Text); var bytesPerColor = Depth8_16.IsOn ? 2u : 1u; var creationCollisionOption = (CreationCollisionOption)NameCollisionCombo.SelectedIndex; var filesNum = InputFilesList.Items.Count; var isBinary = this.IsBinary.IsOn; Parallel.For(0, filesNum, async(i, state) => { using (var stream = await RandomAccessStreamReference.CreateFromFile(files[i]).OpenReadAsync()) { var imageDecoder = await BitmapDecoder.CreateAsync(stream); AnymapProperties properties = new AnymapProperties() { AnymapType = anymapType, Width = imageDecoder.OrientedPixelWidth, Height = imageDecoder.OrientedPixelHeight, MaxValue = maxValue, Threshold8 = threshold8, Threshold16 = threshold16, BytesPerColor = bytesPerColor, StreamPosition = 0, }; try { StorageFile newFile = await outputFolder.CreateFileAsync(outputFiles[i], creationCollisionOption); if (isBinary) { byte[] anymapBytes = await anymapEncoder.EncodeBinary(imageDecoder, properties); await FileIO.WriteBytesAsync(newFile, anymapBytes); } else { await FileIO.WriteTextAsync(newFile, await anymapEncoder.EncodeText(imageDecoder, properties)); } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { (this.InputFilesList.Items[i] as TextBlock).Visibility = Visibility.Collapsed; (this.OutputFilesList.Items[i] as TextBlock).Visibility = Visibility.Visible; this.FileProgressBar.Value = this.FileProgressBar.Value + 1; }); } catch (Exception ex) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { (this.InputFilesList.Items[i] as TextBlock).Visibility = Visibility.Collapsed; this.FileProgressBar.Value = this.FileProgressBar.Value + 1; //ContentDialog convert }); Debug.WriteLine(ex.Message); } } }); }
private void WriteP6(byte[] image, MemoryStream stream, UInt32 totalPixels, AnymapProperties properties) { Double ratio = 1.0; switch (properties.SourcePixelFormat) { case BitmapPixelFormat.Bgra8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { byte[] pixel = new byte[3]; for (UInt32 i = 0; i < totalPixels; ++i) { pixel[0] = (byte)(image[i * 4 + 2] * ratio); pixel[1] = (byte)(image[i * 4 + 1] * ratio); pixel[2] = (byte)(image[i * 4] * ratio); stream.Write(pixel, 0, 3); } } else { byte[] pixel = new byte[6]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(PackU16(image[i * 8 + 4], image[i * 8 + 5]) * ratio), ref pixel[0], ref pixel[1]); UnpackU16((UInt16)(PackU16(image[i * 8 + 2], image[i * 8 + 3]) * ratio), ref pixel[2], ref pixel[3]); UnpackU16((UInt16)(PackU16(image[i * 8 + 0], image[i * 8 + 1]) * ratio), ref pixel[4], ref pixel[5]); stream.Write(pixel, 0, 6); } } break; case BitmapPixelFormat.Gray16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; byte[] pixel = new byte[3]; for (UInt32 i = 0; i < totalPixels; ++i) { pixel[2] = pixel[1] = pixel[0] = (byte)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio); stream.Write(pixel, 0, 3); } } else { ratio = (Double)properties.MaxValue / 65535; byte[] pixel = new byte[6]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio), ref pixel[0], ref pixel[1]); pixel[4] = pixel[2] = pixel[0]; pixel[5] = pixel[3] = pixel[1]; stream.Write(pixel, 0, 6); } } break; case BitmapPixelFormat.Gray8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { byte[] pixel = new byte[3]; for (UInt32 i = 0; i < totalPixels; ++i) { pixel[2] = pixel[1] = pixel[0] = (byte)(image[i] * ratio); stream.Write(pixel, 0, 3); } } else { byte[] pixel = new byte[6]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(image[i] * ratio), ref pixel[0], ref pixel[1]); pixel[4] = pixel[2] = pixel[0]; pixel[5] = pixel[3] = pixel[1]; stream.Write(pixel, 0, 6); } } break; case BitmapPixelFormat.Rgba16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; byte[] pixel = new byte[3]; for (UInt32 i = 0; i < totalPixels; ++i) { pixel[0] = (byte)(PackU16(image[i * 8 + 0], image[i * 8 + 1]) * ratio); pixel[1] = (byte)(PackU16(image[i * 8 + 2], image[i * 8 + 3]) * ratio); pixel[2] = (byte)(PackU16(image[i * 8 + 4], image[i * 8 + 5]) * ratio); stream.Write(pixel, 0, 3); } } else { ratio = (Double)properties.MaxValue / 65535; byte[] pixel = new byte[6]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(PackU16(image[i * 8 + 0], image[i * 8 + 1]) * ratio), ref pixel[0], ref pixel[1]); UnpackU16((UInt16)(PackU16(image[i * 8 + 2], image[i * 8 + 3]) * ratio), ref pixel[2], ref pixel[3]); UnpackU16((UInt16)(PackU16(image[i * 8 + 4], image[i * 8 + 5]) * ratio), ref pixel[4], ref pixel[5]); stream.Write(pixel, 0, 6); } } break; case BitmapPixelFormat.Rgba8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { byte[] pixel = new byte[3]; for (UInt32 i = 0; i < totalPixels; ++i) { pixel[0] = (byte)(image[i * 4 + 0] * ratio); pixel[1] = (byte)(image[i * 4 + 1] * ratio); pixel[2] = (byte)(image[i * 4 + 2] * ratio); stream.Write(pixel, 0, 3); } } else { byte[] pixel = new byte[6]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(image[i * 4 + 0] * ratio), ref pixel[0], ref pixel[1]); UnpackU16((UInt16)(image[i * 4 + 1] * ratio), ref pixel[2], ref pixel[3]); UnpackU16((UInt16)(image[i * 4 + 2] * ratio), ref pixel[4], ref pixel[5]); stream.Write(pixel, 0, 6); } } break; case BitmapPixelFormat.Nv12: case BitmapPixelFormat.Yuy2: case BitmapPixelFormat.Unknown: break; } }
public async Task <byte[]> EncodeBinary(BitmapDecoder imageDecoder, AnymapProperties properties) { var pixelDataProvider = await imageDecoder.GetPixelDataAsync(); var image = pixelDataProvider.DetachPixelData(); properties.SourcePixelFormat = imageDecoder.BitmapPixelFormat; UInt32 totalPixels = imageDecoder.OrientedPixelWidth * imageDecoder.OrientedPixelHeight; UInt32 bytesNumForPixels = 0; Byte type = 0; WriteBinaryDelegate write = null; bool isContainMaxValue = true; switch (properties.AnymapType) { case AnymapType.Bitmap: bytesNumForPixels = (properties.Width / 8 + (properties.Width % 8 == 0 ? (UInt32)0 : (UInt32)1)) * properties.Height; type = (Byte)'4'; write = WriteP4; isContainMaxValue = false; break; case AnymapType.Graymap: bytesNumForPixels = totalPixels * properties.BytesPerColor; type = (Byte)'5'; write = WriteP5; break; case AnymapType.Pixmap: bytesNumForPixels = totalPixels * 3 * properties.BytesPerColor; type = (Byte)'6'; write = WriteP6; break; } UInt32 bytesForType = 3; UInt32 bytesForSize = (UInt32)properties.Width.ToString().Length + 1 + (UInt32)properties.Height.ToString().Length + 1; UInt32 bytesForMaxV = (UInt32)properties.MaxValue.ToString().Length + 1; UInt32 bytesNum = bytesForType + bytesForSize + (isContainMaxValue ? bytesForMaxV : 0) + bytesNumForPixels; byte[] anymap = new byte[bytesNum]; MemoryStream stream = new MemoryStream(anymap); // Format stream.WriteByte((Byte)'P'); stream.WriteByte(type); stream.WriteByte(0x0a); // Size byte[] widthArr = Encoding.ASCII.GetBytes(properties.Width.ToString()); stream.Write(widthArr, 0, widthArr.Length); stream.WriteByte((Byte)' '); byte[] heightArr = Encoding.ASCII.GetBytes(properties.Height.ToString()); stream.Write(heightArr, 0, heightArr.Length); stream.WriteByte(0x0a); // Maximum pixel value if (isContainMaxValue) { byte[] maxPixelValueArr = Encoding.ASCII.GetBytes(properties.MaxValue.ToString()); stream.Write(maxPixelValueArr, 0, maxPixelValueArr.Length); stream.WriteByte(0x0a); } // Pixels write(image, stream, totalPixels, properties); return(anymap); }
private void WriteP5(byte[] image, MemoryStream stream, UInt32 totalPixels, AnymapProperties properties) { Double ratio = 1.0; switch (properties.SourcePixelFormat) { case BitmapPixelFormat.Bgra8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { for (UInt32 i = 0; i < totalPixels; ++i) { stream.WriteByte((byte)(GrayscaleBgra8(image, i * 4) * ratio)); } } else { byte[] pixel = new byte[2]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(GrayscaleBgra8(image, i * 4) * ratio), ref pixel[0], ref pixel[1]); stream.Write(pixel, 0, 2); } } break; case BitmapPixelFormat.Gray16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; for (UInt32 i = 0; i < totalPixels; ++i) { stream.WriteByte((byte)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio)); } } else { ratio = (Double)properties.MaxValue / 65535; byte[] pixel = new byte[2]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio), ref pixel[0], ref pixel[1]); stream.Write(pixel, 0, 2); } } break; case BitmapPixelFormat.Gray8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { for (UInt32 i = 0; i < totalPixels; ++i) { stream.WriteByte((byte)(image[i] * ratio)); } } else { byte[] pixel = new byte[2]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio), ref pixel[0], ref pixel[1]); stream.Write(pixel, 0, 2); } } break; case BitmapPixelFormat.Rgba16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; UInt16[] sourcePixel = new UInt16[3]; for (UInt32 i = 0; i < totalPixels; ++i) { sourcePixel[0] = PackU16(image[i * 8 + 0], image[i * 8 + 1]); sourcePixel[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]); sourcePixel[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]); stream.WriteByte((byte)(GrayscaleRgba16(sourcePixel, 0) * ratio)); } } else { ratio = (Double)properties.MaxValue / 65535; UInt16[] sourcePixel = new UInt16[3]; byte[] pixel = new byte[2]; for (UInt32 i = 0; i < totalPixels; ++i) { sourcePixel[0] = PackU16(image[i * 8 + 0], image[i * 8 + 1]); sourcePixel[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]); sourcePixel[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]); UnpackU16((UInt16)(GrayscaleRgba16(sourcePixel, 0) * ratio), ref pixel[0], ref pixel[1]); stream.Write(pixel, 0, 2); } } break; case BitmapPixelFormat.Rgba8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { for (UInt32 i = 0; i < totalPixels; ++i) { stream.WriteByte((byte)(GrayscaleRgba8(image, i * 4) * ratio)); } } else { byte[] pixel = new byte[2]; for (UInt32 i = 0; i < totalPixels; ++i) { UnpackU16((UInt16)(GrayscaleRgba8(image, i * 4) * ratio), ref pixel[0], ref pixel[1]); stream.Write(pixel, 0, 2); } } break; case BitmapPixelFormat.Nv12: case BitmapPixelFormat.Yuy2: case BitmapPixelFormat.Unknown: break; } }
private void WriteP4(byte[] image, MemoryStream stream, UInt32 totalPixels, AnymapProperties properties) { PackBytePbmDelegate PackBytes = null; int threshold = 127; switch (properties.SourcePixelFormat) { case BitmapPixelFormat.Bgra8: PackBytes = PackBytePbmBgra8; threshold = properties.Threshold8; break; case BitmapPixelFormat.Gray16: PackBytes = PackBytePbmGray16; threshold = properties.Threshold16; break; case BitmapPixelFormat.Gray8: PackBytes = PackBytePbmGray8; threshold = properties.Threshold8; break; case BitmapPixelFormat.Rgba16: PackBytes = PackBytePbmRgba16; threshold = properties.Threshold16; break; case BitmapPixelFormat.Rgba8: PackBytes = PackBytePbmRgba8; threshold = properties.Threshold8; break; case BitmapPixelFormat.Nv12: case BitmapPixelFormat.Yuy2: case BitmapPixelFormat.Unknown: break; } int colmod = (int)properties.Width % 8; int col = 0; int packSize = 0; for (UInt32 i = 0; i < totalPixels;) { if (col == properties.Width - colmod) { // Last pack in a row packSize = colmod == 0 ? 8 : colmod; stream.WriteByte( PackBytes(image, i, packSize, threshold) ); col = 0; i += (UInt32)packSize; } else { stream.WriteByte( PackBytes(image, i, totalPixels - i > 8 ? 8 : (int)(totalPixels - i), threshold) ); col += 8; i += 8; } } }
private void WriteP3(byte[] image, StringWriter stringWriter, UInt32 totalPixels, AnymapProperties properties) { Double ratio = 1.0; switch (properties.SourcePixelFormat) { case BitmapPixelFormat.Bgra8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { stringWriter.Write("{0} {1} {2} ", (byte)(image[2] * ratio), (byte)(image[1] * ratio), (byte)(image[0] * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {1} {2} ", (byte)(image[i * 4 + 2] * ratio), (byte)(image[i * 4 + 1] * ratio), (byte)(image[i * 4] * ratio) ); } } else { stringWriter.Write("{0} {1} {2} ", (UInt16)(image[2] * ratio), (UInt16)(image[1] * ratio), (UInt16)(image[0] * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {1} {2} ", (UInt16)(image[i * 4 + 2] * ratio), (UInt16)(image[i * 4 + 1] * ratio), (UInt16)(image[i * 4 + 0] * ratio) ); } } break; case BitmapPixelFormat.Gray16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; // 255 / 256 stringWriter.Write("{0} {0} {0} ", (byte)(PackU16(image[0], image[1]) * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {0} {0} ", (byte)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio) ); } } else { ratio = (Double)properties.MaxValue / 65535; stringWriter.Write("{0} {0} {0} ", (UInt16)(PackU16(image[0], image[1]) * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {0} {0} ", (UInt16)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio) ); } } break; case BitmapPixelFormat.Gray8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { stringWriter.Write("{0} {0} {0} ", (byte)(image[0] * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {0} {0} ", (byte)(image[i] * ratio) ); } } else { stringWriter.Write("{0} {0} {0} ", (UInt16)(image[0] * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {0} {0} ", (UInt16)(image[i] * ratio) ); } } break; case BitmapPixelFormat.Rgba16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; // 255 / 256 stringWriter.Write("{0} {1} {2} ", (byte)(PackU16(image[0], image[1]) * ratio), (byte)(PackU16(image[2], image[3]) * ratio), (byte)(PackU16(image[4], image[5]) * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {1} {2} ", (byte)(PackU16(image[i * 8 + 0], image[i * 8 + 1]) * ratio), (byte)(PackU16(image[i * 8 + 2], image[i * 8 + 3]) * ratio), (byte)(PackU16(image[i * 8 + 4], image[i * 8 + 5]) * ratio) ); } } else { ratio = (Double)properties.MaxValue / 65535; stringWriter.Write("{0} {1} {2} ", (UInt16)(PackU16(image[0], image[1]) * ratio), (UInt16)(PackU16(image[2], image[3]) * ratio), (UInt16)(PackU16(image[4], image[5]) * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {1} {2} ", (UInt16)(PackU16(image[i * 8 + 0], image[i * 8 + 1]) * ratio), (UInt16)(PackU16(image[i * 8 + 2], image[i * 8 + 3]) * ratio), (UInt16)(PackU16(image[i * 8 + 4], image[i * 8 + 5]) * ratio) ); } } break; case BitmapPixelFormat.Rgba8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { stringWriter.Write("{0} {1} {2} ", (byte)(image[0] * ratio), (byte)(image[1] * ratio), (byte)(image[2] * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {1} {2} ", (byte)(image[i * 4] * ratio), (byte)(image[i * 4 + 1] * ratio), (byte)(image[i * 4 + 2] * ratio) ); } } else { stringWriter.Write("{0} {1} {2} ", (UInt16)(image[0] * ratio), (UInt16)(image[1] * ratio), (UInt16)(image[2] * ratio) ); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} {1} {2} ", (UInt16)(image[i * 4] * ratio), (UInt16)(image[i * 4 + 1] * ratio), (UInt16)(image[i * 4 + 2] * ratio) ); } } break; case BitmapPixelFormat.Nv12: case BitmapPixelFormat.Yuy2: case BitmapPixelFormat.Unknown: break; } }
private void WriteP2(byte[] image, StringWriter stringWriter, UInt32 totalPixels, AnymapProperties properties) { Double ratio = 1.0; switch (properties.SourcePixelFormat) { case BitmapPixelFormat.Bgra8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleBgra8(image, 0) * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleBgra8(image, i * 4) * ratio)); } } else { stringWriter.Write("{0} ", (UInt16)(AnymapEncoder.GrayscaleBgra8(image, 0) * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (UInt16)(AnymapEncoder.GrayscaleBgra8(image, i * 4) * ratio)); } } break; case BitmapPixelFormat.Gray16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; // 255 / 256 stringWriter.Write("{0} ", (byte)(PackU16(image[0], image[1])) * ratio); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (byte)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio)); } } else { ratio = (Double)properties.MaxValue / 65535; stringWriter.Write("{0} ", (UInt16)(PackU16(image[0], image[1]) * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (UInt16)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio)); } } break; case BitmapPixelFormat.Gray8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { stringWriter.Write("{0} ", (byte)(image[0] * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (byte)(image[i] * ratio)); } } else { stringWriter.Write("{0} ", (UInt16)(image[0] * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (UInt16)(image[i] * ratio)); } } break; case BitmapPixelFormat.Rgba16: if (properties.BytesPerColor == 1) { ratio = (Double)properties.MaxValue / 65280; // 255 / 256; byte[] pixel = new byte[2]; UInt16[] temp = new UInt16[3]; temp[0] = PackU16(image[0], image[1]); temp[1] = PackU16(image[2], image[3]); temp[2] = PackU16(image[4], image[5]); stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, 0) * ratio); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } temp[0] = PackU16(image[i * 8], image[i * 8 + 1]); temp[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]); temp[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]); stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba16(temp, i * 8) * ratio)); } } else { ratio = (Double)properties.MaxValue / 65535; byte[] pixel = new byte[2]; UInt16[] temp = new UInt16[3]; temp[0] = PackU16(image[0], image[1]); temp[1] = PackU16(image[2], image[3]); temp[2] = PackU16(image[4], image[5]); stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, 0) * ratio); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } temp[0] = PackU16(image[i * 8], image[i * 8 + 1]); temp[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]); temp[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]); stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, i * 8) * ratio); } } break; case BitmapPixelFormat.Rgba8: ratio = (Double)properties.MaxValue / 255; if (properties.BytesPerColor == 1) { stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, 0) * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, i * 4) * ratio)); } } else { stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, 0) * ratio)); for (UInt32 i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, i * 4) * ratio)); } } break; case BitmapPixelFormat.Nv12: case BitmapPixelFormat.Yuy2: case BitmapPixelFormat.Unknown: break; } }
private void WriteP1(byte[] image, StringWriter stringWriter, UInt32 totalPixels, AnymapProperties properties) { UInt32 i = 0; switch (properties.SourcePixelFormat) { case BitmapPixelFormat.Bgra8: stringWriter.Write("{0} ", AnymapEncoder.GrayscaleBgra8(image, 0) <= properties.Threshold8 ? 1 : 0); for (i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", AnymapEncoder.GrayscaleBgra8(image, i * 4) <= properties.Threshold8 ? 1 : 0); } break; case BitmapPixelFormat.Gray16: UInt16 pixel = PackU16(image[0], image[1]); stringWriter.Write("{0} ", pixel <= properties.Threshold16 ? 1 : 0); for (i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } pixel = PackU16(image[i * 2], image[i * 2 + 1]); stringWriter.Write("{0} ", pixel <= properties.Threshold16 ? 1 : 0); } break; case BitmapPixelFormat.Gray8: stringWriter.Write("{0} ", image[0] <= properties.Threshold8 ? 1 : 0); for (i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", image[i] <= properties.Threshold8 ? 1 : 0); } break; case BitmapPixelFormat.Rgba16: UInt16[] temp = new UInt16[3]; temp[0] = PackU16(image[0], image[1]); temp[1] = PackU16(image[2], image[3]); temp[2] = PackU16(image[4], image[5]); stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, 0) <= properties.Threshold16 ? 1 : 0); for (i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } temp[0] = PackU16(image[i * 8], image[i * 8 + 1]); temp[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]); temp[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]); stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, i * 8) <= properties.Threshold16 ? 1 : 0); } break; case BitmapPixelFormat.Rgba8: stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba8(image, 0) <= properties.Threshold8 ? 1 : 0); for (i = 1; i < totalPixels; ++i) { if (i % properties.Width == 0) { stringWriter.WriteLine(); } stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba8(image, i * 4) <= properties.Threshold8 ? 1 : 0); } break; case BitmapPixelFormat.Nv12: case BitmapPixelFormat.Yuy2: case BitmapPixelFormat.Unknown: break; } }