static public CodeDecoder Decode(byte[] abImage) { PngReader xPng=new PngReader( new MemoryStream( abImage ) ); // ImageLines xLines=xPng.ReadRowsByte(); // byte[,] abData=new byte[xLines.ImgInfo.Cols, xLines.ImgInfo.Rows]; // int iBits = xLines.ImgInfo.BitspPixel; int iChannels = xLines.ImgInfo.Channels; // if ( iBits==1 ) { for (int y = 0; y < xLines.ImgInfo.Rows; y++) for (int x = 0; x < xLines.ImgInfo.Cols/8; x++) { byte b= xLines.ScanlinesB[y][x]; // for (int a=0; a<8; a++) abData[x*8+a, y] = (byte)( ( (b & (1<<(7-a))) !=0 ) ? 255 : 0 ); } } else if( iBits==8 ) { int iC=0; for (int y = 0; y < xLines.ImgInfo.Rows; y++) for (int x = 0; x < xLines.ImgInfo.Cols; x++, iC+=iChannels) abData[x,y]=xLines.ScanlinesB[y][iC]; } // return Decode( abData , xLines.ImgInfo.Cols , xLines.ImgInfo.Rows); }
public static async Task <StorageFile> WriteableBitmapToStorageFile(ushort[,] image, bool isScaleValues, List <KeyValuePair <string, string> > metadata) { //Setup image maxVal. var imgHeight = image.GetLength(0); var imgWidth = image.GetLength(1); float maxVal = 1; if (isScaleValues) { for (int i = 0; i < imgHeight; i++) { for (int j = 0; j < imgWidth; j++) { if (maxVal < image[i, j]) { maxVal = image[i, j]; } } } } string FileName = "MyFile.png"; var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { ImageInfo imgInfo = new ImageInfo(imgWidth, imgHeight, 16, false, true, false); PngWriter wrt = new PngWriter(stream.AsStreamForWrite(), imgInfo); PngMetadata da = wrt.GetMetadata(); foreach (var item in metadata) { if (item.Value != null) { da.SetText(item.Key, item.Value); } } ImageLines imLines = new ImageLines(imgInfo, ImageLine.ESampleType.INT, false, 0, imgHeight, 1); for (int i = 0; i < imLines.ImgInfo.Rows; i++) { for (int j = 0; j < imLines.ImgInfo.Cols; j++) { if (isScaleValues) { imLines.Scanlines[i][j] = (ushort)(ushort.MaxValue * (double)image[i, j] / maxVal); } else { imLines.Scanlines[i][j] = image[i, j]; } } } wrt.WriteRowsInt(imLines.Scanlines); wrt.End(); } return(file); }
static void testmirror(string orig, string origni, string truecolor) { string mirror = TestsHelper.addSuffixToName(orig, "_mirror"); string recov = TestsHelper.addSuffixToName(orig, "_recov"); long crc0 = 0; bool interlaced; bool palete; { PngReader pngr = FileHelper.CreatePngReader(orig); palete = pngr.ImgInfo.Indexed; PngHelperInternal.InitCrcForTests(pngr); pngr.SetUnpackedMode(true); interlaced = pngr.IsInterlaced(); PngWriter pngw = FileHelper.CreatePngWriter(mirror, pngr.ImgInfo, true); pngw.SetFilterType(FilterType.FILTER_CYCLIC); // just to test all filters pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL); pngw.SetUseUnPackedMode(true); for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine line = pngr.ReadRowInt(row); mirrorLine(line); pngw.WriteRow(line, row); } pngr.End(); crc0 = PngHelperInternal.GetCrctestVal(pngr); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL); pngw.End(); } // mirror again, now with BYTE (if depth<16) and loading all rows { PngReader pngr2 = FileHelper.CreatePngReader(mirror); pngr2.SetUnpackedMode(true); PngWriter pngw = FileHelper.CreatePngWriter(recov, pngr2.ImgInfo, true); pngw.SetFilterType(FilterType.FILTER_AGGRESSIVE); pngw.CopyChunksFirst(pngr2, ChunkCopyBehaviour.COPY_ALL); pngw.SetUseUnPackedMode(true); ImageLines lines = pngr2.ImgInfo.BitDepth < 16 ? pngr2.ReadRowsByte() : pngr2 .ReadRowsInt(); for (int row = 0; row < pngr2.ImgInfo.Rows; row++) { ImageLine line = lines.GetImageLineAtMatrixRow(row); mirrorLine(line); pngw.WriteRow(line, row); } pngr2.End(); pngw.End(); } // now check if (orig[11] != 'i') { TestsHelper.testCrcEquals(recov, crc0); } //if (interlaced) // additionalTestInterlaced(orig, origni); //if (palete && System.IO.File.Exists(truecolor)) // additionalTestPalette(orig, truecolor); }
private static void PngWriter(MemoryStream memoryStream, FileStream fileStream, bool modelpng = false) { PngReader pngReader = new PngReader(fileStream); EngineBinaryWriter binaryWriter = new EngineBinaryWriter(memoryStream, false); binaryWriter.Write(false); pngReader.ShouldCloseStream = false; pngReader.ChunkLoadBehaviour = ChunkLoadBehaviour.LOAD_CHUNK_NEVER; pngReader.MaxTotalBytesRead = 9223372036854775807L; ImageLines imageLines = pngReader.ReadRowsByte(); pngReader.End(); Image image = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int n = 0; for (int i = 0; i < image.Height; i++) { byte[] array = imageLines.ScanlinesB[i]; int num = 0; for (int j = 0; j < image.Width; j++) { byte r = array[num++]; byte g = array[num++]; byte b = array[num++]; byte a = array[num++]; image.Pixels[n++] = new Color(r, g, b, a); } } List <Image> list = new List <Image>(); if (IsPowerOf2(image.Width) && IsPowerOf2(image.Height)) { list.AddRange(Image.GenerateMipmaps(image, 2147483647)); } else { list.Add(image); modelpng = true; } if (modelpng) { goto IL_00; } binaryWriter.Write(image.Width); binaryWriter.Write(image.Height); binaryWriter.Write(1); for (int i = 0; i < image.Height; i++) { byte[] array = imageLines.ScanlinesB[i]; int num = 0; for (int j = 0; j < image.Width; j++) { byte r = array[num++]; byte g = array[num++]; byte b = array[num++]; byte a = array[num++]; binaryWriter.Write(new Color(r, g, b, a).PackedValue); } } return; IL_00: binaryWriter.Write(list[0].Width); binaryWriter.Write(list[0].Height); binaryWriter.Write(list.Count); foreach (Image current in list) { for (int j = 0; j < current.Pixels.Length; j++) { binaryWriter.Write(current.Pixels[j].PackedValue); } } }
private void _convert(string inputPdfPath) { _outputToInfo(inputPdfPath); // make a sub path in the same folder as this // put file(s) in there // single file has NAME.png // multiples are NAME_000.png // overwrite just uses folder always // otherwise, append output folder if it exists and is NOT empty try { outputFolder = _createOutputFolder(inputPdfPath, _overwrite); if (outputFolder == null) { _outputToInfo("Error"); return; } string historyFile = Path.Combine(outputFolder, "history.txt"); _outputToInfo("Output to: " + outputFolder); _outputToInfo(" "); using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer()) { rasterizer.Open(inputPdfPath, _lastInstalledVersion, false); if (_multiFiles) { string outputPath = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inputPdfPath)); int nFiles = 0; for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++) { if (cancel) { break; } if (_isPageNumberRequired(pageNumber)) { string pageFilePath = outputPath + "_" + pageNumber.ToString("000") + _imageExt; Image img = rasterizer.GetPage(_dpi, _dpi, pageNumber); img.Save(pageFilePath, _imageFmt); _outputToInfo(img); _outputToInfo(" " + Path.GetFileName(pageFilePath)); _outputToInfo(" Dimensions:" + img.Width + " x " + img.Height); _outputToInfo(); _history(historyFile, inputPdfPath, pageFilePath); nFiles++; } } _outputToInfo(" complete, " + nFiles + " files in:"); _outputToInfo(" " + outputFolder); _outputToInfo(); } else { // work in PNG here string outputPath = Path.GetTempPath(); string prefix = Guid.NewGuid().ToString().Substring(0, 7); List <string> pathList = new List <string>(); int w = 0, h = 0; for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++) { if (cancel) { break; } if (_isPageNumberRequired(pageNumber)) { string pageFilePath = Path.Combine(outputPath, prefix + "_" + pageNumber.ToString() + ".png"); Image img = rasterizer.GetPage(_dpi, _dpi, pageNumber); img.Save(pageFilePath, ImageFormat.Png); _outputToInfo(img); pathList.Add(pageFilePath); _outputToInfo(" building page: " + pageNumber + " of " + rasterizer.PageCount); _outputToInfo(); h += img.Height; w = Math.Max(w, img.Width); } } // combine!! if (cancel == false && pathList.Count > 0) { string bigImagePath = Path.Combine(outputPath, prefix + "_BIG" + ".png"); PngReader pngr = FileHelper.CreatePngReader(pathList[0]); // or you can use the constructor ImageInfo ii = new ImageInfo(w, h, pngr.ImgInfo.BitDepth, pngr.ImgInfo.Alpha, false, false); PngWriter pngw = FileHelper.CreatePngWriter(bigImagePath, ii, true); int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE; // tell to copy all 'safe' chunks pngw.CopyChunksFirst(pngr, chunkBehav); // copy some metadata from reader pngr.End(); int destRow = 0; int ic = 1; foreach (string path in pathList) { pngr = FileHelper.CreatePngReader(path); ImageLines iLines = pngr.ReadRowsInt(0, pngr.ImgInfo.Rows, 1); int nrr = iLines.Nrows; for (int i = 0; i < nrr; i++) { pngw.WriteRow(iLines.GetImageLineAtMatrixRow(i), destRow++); } _outputToInfo(" combining page:" + ic + " of " + pathList.Count); ic++; pngr.End(); File.Delete(path); // remove them as we go } pngw.CopyChunksLast(pngr, chunkBehav); // metadata after the image pixels? can happen pngw.End(); // dont forget this _autoCrop(bigImagePath); // convert to required type if not PNG string outputSingleFile = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(inputPdfPath) + _imageExt); Bitmap bm; if (_imageExt != ".png") { bm = new Bitmap(bigImagePath); bm.Save(outputSingleFile, _imageFmt); } else { bm = new Bitmap(bigImagePath); _moveFile(bigImagePath, outputSingleFile); } _outputToInfo(bm); _outputToInfo(" Dimensions:" + bm.Width + " x " + bm.Height); _outputToInfo(" Complete: "); _outputToInfo(" " + outputSingleFile); _history(historyFile, inputPdfPath, outputSingleFile); } else { _outputToInfo(" NO PAGES set! Try again."); } } } } catch (Exception ex) { _outputToInfo("Error"); _outputToInfo(ex.ToString()); throw; } }
private async void button1_Click(object sender, RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("PNG Image", new List <string>() { ".png" }); // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = "map2"; var file = await savePicker.PickSaveFileAsync(); if (file != null) { // Prevent updates to the remote version of the file until // we finish making changes and call CompleteUpdatesAsync. Windows.Storage.CachedFileManager.DeferUpdates(file); // write to file //await Windows.Storage.FileIO.WriteTextAsync(file, file.Name); var stream = (file.OpenStreamForWriteAsync()).Result; ImageInfo imgInfo = new ImageInfo(200, 100, 16, false, true, false); PngWriter wrt = new PngWriter(stream, imgInfo); Hjg.Pngcs.Chunks.PngMetadata metadata = wrt.GetMetadata(); metadata.SetText("test", "value"); ImageLines imLines = new ImageLines(imgInfo, ImageLine.ESampleType.INT, false, 0, 100, 1); // fill in image by random values. Random rnd = new Random(); for (int i = 0; i < imLines.ImgInfo.Rows; i++) { for (int j = 0; j < imLines.ImgInfo.Cols; j++) { imLines.Scanlines[i][j] = rnd.Next(0, ushort.MaxValue); } } for (int i = 0; i < imLines.ImgInfo.Rows; i++) { for (int j = 0; j < imLines.ImgInfo.Cols / 10; j++) { imLines.Scanlines[i][j] = (int)((ushort.MaxValue * 254.1) / 255.0); } } wrt.WriteRowsInt(imLines.Scanlines); wrt.End(); //ream.Dispose(); // Let Windows know that we're finished changing the file so // the other app can update the remote version of the file. // Completing updates may require Windows to ask for user input. Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { this.textBlock.Text = "File " + file.Name + " was saved."; } else { this.textBlock.Text = "File " + file.Name + " couldn't be saved."; } } else { this.textBlock.Text = "Action cancelled."; } }
public static Image Load(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } PngReader pngReader = new PngReader(stream); pngReader.ShouldCloseStream = false; pngReader.ChunkLoadBehaviour = ChunkLoadBehaviour.LOAD_CHUNK_NEVER; pngReader.MaxTotalBytesRead = long.MaxValue; ImageLines imageLines = pngReader.ReadRowsByte(); pngReader.End(); if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 4) { Image image = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int i = 0; int num = 0; for (; i < image.Height; i++) { byte[] array = imageLines.ScanlinesB[i]; int j = 0; int num2 = 0; for (; j < image.Width; j++) { byte r = array[num2++]; byte g = array[num2++]; byte b = array[num2++]; byte a = array[num2++]; image.Pixels[num++] = new Color(r, g, b, a); } } return(image); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 3) { Image image2 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int k = 0; int num3 = 0; for (; k < image2.Height; k++) { byte[] array2 = imageLines.ScanlinesB[k]; int l = 0; int num4 = 0; for (; l < image2.Width; l++) { byte r2 = array2[num4++]; byte g2 = array2[num4++]; byte b2 = array2[num4++]; image2.Pixels[num3++] = new Color(r2, g2, b2); } } return(image2); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 2 && imageLines.ImgInfo.Greyscale) { Image image3 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int m = 0; int num5 = 0; for (; m < image3.Height; m++) { byte[] array3 = imageLines.ScanlinesB[m]; int n = 0; int num6 = 0; for (; n < image3.Width; n++) { byte b3 = array3[num6++]; byte a2 = array3[num6++]; image3.Pixels[num5++] = new Color(b3, b3, b3, a2); } } return(image3); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 1 && imageLines.ImgInfo.Greyscale) { Image image4 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int num7 = 0; int num8 = 0; for (; num7 < image4.Height; num7++) { byte[] array4 = imageLines.ScanlinesB[num7]; int num9 = 0; int num10 = 0; for (; num9 < image4.Width; num9++) { byte b4 = array4[num10++]; image4.Pixels[num8++] = new Color(b4, b4, b4); } } return(image4); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 1 && imageLines.ImgInfo.Indexed) { PngChunkPLTE pngChunkPLTE = (PngChunkPLTE)pngReader.GetChunksList().GetById1("PLTE"); if (pngChunkPLTE == null) { throw new InvalidOperationException("PLTE chunk not found in indexed PNG."); } Image image5 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int num11 = 0; int num12 = 0; for (; num11 < image5.Height; num11++) { byte[] array5 = imageLines.ScanlinesB[num11]; int num13 = 0; int num14 = 0; for (; num13 < image5.Width; num13++) { byte n2 = array5[num14++]; int entry = pngChunkPLTE.GetEntry(n2); image5.Pixels[num12++] = new Color((entry >> 16) & 0xFF, (entry >> 8) & 0xFF, entry & 0xFF); } } return(image5); } throw new InvalidOperationException("Unsupported PNG pixel format."); }